2.5.0 */ if ( ! file_exists( $this->log_dir ) ) { return; } try { $fp = fopen( $this->files[ $module ], $mode ); flock( $fp, LOCK_EX ); fwrite( $fp, $message ); flock( $fp, LOCK_UN ); fclose( $fp ); } catch ( Exception $e ) { $this->status = new WP_Error( 'log-write-error', $e->getMessage() ); } } /** * Cleanup on uninstall. * * @since 1.7.2 */ public static function cleanup() { if ( ! defined( 'WP_CONTENT_DIR' ) ) { define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); } $log_dir = WP_CONTENT_DIR . '/wphb-logs/'; // If no directory is present - exit. if ( ! is_dir( $log_dir ) ) { return; } try { $dir = opendir( $log_dir ); while ( false !== ( $file = readdir( $dir ) ) ) { if ( ( '.' === $file ) || ( '..' === $file ) ) { continue; } $full = $log_dir . $file; if ( is_dir( $full ) ) { rmdir( $full ); } else { unlink( $full ); } } closedir( $dir ); rmdir( $log_dir ); } catch ( Exception $e ) { error_log( '[' . current_time( 'mysql' ) . '] - Unable to clean Hummingbird log directory. Error: ' . $e->getMessage() ); } } /** * Check if module should log or not. * * @since 1.7.2 * * @param string $module Module to log for. * * @return bool */ private function should_log( $module ) { // Don't log if there's an error. if ( is_wp_error( $this->status ) ) { return false; } // See if the module is registered to log. if ( ! in_array( $module, $this->modules, true ) ) { return false; } $do_log = false; switch ( $module ) { case 'minify': // Log for minification only if debug is enabled. $options = Utils::get_module( 'minify' )->get_options(); if ( $options['log'] ) { $do_log = true; } break; default: // Default to logging only when wp debug is set. $debug = defined( 'WP_DEBUG' ) && WP_DEBUG; $debug_log = defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG; if ( $debug && $debug_log ) { $do_log = true; } break; } return $do_log; } /** * Main logging function. * * @since 1.7.2 * * @param mixed $message Data to write to log. * @param string $module Module slug. */ public function log( $message, $module ) { if ( ! $this->should_log( $module ) ) { return; } if ( ! is_string( $message ) || is_array( $message ) || is_object( $message ) ) { $message = print_r( $message, true ); } $message = '[' . date( 'c' ) . '] ' . $message . PHP_EOL; $this->write_file( 'a', $message, $module ); } /** * Getter method for $file. * * @since 1.9.2 * * @param string $module Module slug. * * @return string */ public function get_file( $module ) { return $this->files[ $module ]; } /** * Process logger actions. * * Accepts module name (slug) and action. So far only 'download' actions is supported. * * @since 1.9.2 */ public function process_actions() { if ( ! isset( $_GET['logs'] ) || ! isset( $_GET['module'] ) || ! check_admin_referer( 'wphb-log-action' ) ) { // Input var ok. return; } $action = sanitize_text_field( wp_unslash( $_GET['logs'] ) ); // Input var ok. $module = sanitize_text_field( wp_unslash( $_GET['module'] ) ); // Input var ok. // Not called by a registered module. if ( ! in_array( $module, $this->modules, true ) ) { return; } // Only allow these actions. if ( 'download' !== $action ) { return; } if ( method_exists( $this, $action ) ) { call_user_func( array( $this, $action ), $module ); } } /** * Download logs. * * @since 1.9.2 * * @param string $module Module slug. */ private function download( $module ) { if ( 'page_cache' === $module ) { $content = file_get_contents( WP_CONTENT_DIR . '/wphb-logs/page-caching-log.php' ); /* Remove