Skip to content

Commit

Permalink
randomize debug log filename
Browse files Browse the repository at this point in the history
And improve temp debug mode and separate debugging from Easy IO plugin by using a static property instead of a single global variable.
Also reduce the retries for an image in scheduled/cron mode to 5 (from 15).
  • Loading branch information
nosilver4u committed Aug 30, 2023
1 parent e254c82 commit b02db4b
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
include:
- php: 7.3
env: WP_VERSION=latest WP_MULTISITE=1 WPSNIFF=1
- php: 8.1
- php: 8.2
env: WP_VERSION=latest WP_MULTISITE=1 WPSNIFF=1
- php: 8.0
env: WP_VERSION=5.9 WP_MULTISITE=0
Expand Down
16 changes: 6 additions & 10 deletions aux-optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ function ewww_image_optimizer_aux_images() {
ewwwio_debug_message( '<b>' . __FUNCTION__ . '()</b>' );
$output = '';

global $eio_debug;
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_debug' ) ) {
ewwwio_debug_info();
echo '<div style="clear:both;"></div>';
if ( ewwwio_is_file( EWWWIO_CONTENT_DIR . 'debug.log' ) ) {
if ( ewwwio_is_file( ewwwio()->debug_log_path() ) ) {
?>
<h2><?php esc_html_e( 'Debug Log', 'ewww-image-optimizer' ); ?></h2>
<p>
Expand All @@ -43,38 +42,35 @@ function ewww_image_optimizer_aux_images() {
echo '<p><button id="ewww-copy-debug" class="button button-secondary" type="button">' . esc_html__( 'Copy', 'ewww-image-optimizer' ) . '</button></p>';
echo '<div id="ewww-debug-info" contenteditable="true">' .
wp_kses(
$eio_debug,
EWWW\Base::$debug_data,
array(
'br' => array(),
'b' => array(),
)
) .
'</div>';
} else {
ewww_image_optimizer_set_option( 'ewww_image_optimizer_debug', true );
ewwwio_debug_info();
ewww_image_optimizer_set_option( 'ewww_image_optimizer_debug', false );
}
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_enable_help' ) ) {
$current_user = wp_get_current_user();
$help_email = $current_user->user_email;
$hs_debug = '';
if ( ! empty( $eio_debug ) ) {
$hs_debug = str_replace( array( "'", '<br>', '<b>', '</b>', '=>' ), array( "\'", '\n', '**', '**', '=' ), $eio_debug );
if ( ! empty( EWWW\Base::$debug_data ) ) {
$hs_debug = str_replace( array( "'", '<br>', '<b>', '</b>', '=>' ), array( "\'", '\n', '**', '**', '=' ), EWWW\Base::$debug_data );
}
?>
<script type="text/javascript">!function(e,t,n){function a(){var e=t.getElementsByTagName("script")[0],n=t.createElement("script");n.type="text/javascript",n.async=!0,n.src="https://beacon-v2.helpscout.net",e.parentNode.insertBefore(n,e)}if(e.Beacon=n=function(t,n,a){e.Beacon.readyQueue.push({method:t,options:n,data:a})},n.readyQueue=[],"complete"===t.readyState)return a();e.attachEvent?e.attachEvent("onload",a):e.addEventListener("load",a,!1)}(window,document,window.Beacon||function(){});</script>
<script type="text/javascript">
window.Beacon('init', 'aa9c3d3b-d4bc-4e9b-b6cb-f11c9f69da87');
Beacon( 'prefill', {
email: '<?php echo esc_js( $help_email ); ?>',
text: '\n\n----------------------------------------\n<?php echo wp_kses_post( $hs_debug ); ?>',
text: '\n\n---------------------------------------\n<?php echo wp_kses_post( $hs_debug ); ?>',
});
</script>
<?php
}
$eio_debug = '';
ewwwio_memory( __FUNCTION__ );
ewwwio()->temp_debug_end();
}

/**
Expand Down
3 changes: 1 addition & 2 deletions bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -2162,10 +2162,9 @@ function ewww_image_optimizer_bulk_loop( $hook = '', $delay = 0 ) {
$output['results'] .= sprintf( '<p>' . esc_html( _n( 'Elapsed: %s second', 'Elapsed: %s seconds', $elapsed, 'ewww-image-optimizer' ) ) . '</p>', number_format_i18n( $elapsed, 1 ) );
// Store the updated list of attachment IDs back in the 'bulk_attachments' option.
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_debug' ) ) {
global $eio_debug;
$debug_button = esc_html__( 'Show Debug Output', 'ewww-image-optimizer' );
$debug_id = uniqid();
$output['results'] .= "<button type='button' class='ewww-show-debug-meta button button-secondary' data-id='$debug_id'>$debug_button</button><div class='ewww-debug-meta-$debug_id' style='background-color:#f1f1f1;display:none;'>$eio_debug</div>";
$output['results'] .= "<button type='button' class='ewww-show-debug-meta button button-secondary' data-id='$debug_id'>$debug_button</button><div class='ewww-debug-meta-$debug_id' style='background-color:#f1f1f1;display:none;'>" . EWWW\Base::$debug_data . '</div>';
}
if ( ! empty( $next_image->file ) ) {
$next_file = esc_html( $next_image->file );
Expand Down
10 changes: 9 additions & 1 deletion classes/class-background-process-image.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Class for Background optimization of individual images.
* Class for Background optimization of individual images, used by scheduled optimization.
*
* @link https://ewww.io
* @package EWWW_Image_Optimizer
Expand Down Expand Up @@ -35,6 +35,14 @@ class Background_Process_Image extends Background_Process {
*/
protected $active_queue = 'single-async';

/**
* Attempts limit, shorter for scheduled opt.
*
* @var int
* @access protected
*/
protected $max_attempts = 5;

/**
* Runs optimization for a file from the image queue.
*
Expand Down
98 changes: 70 additions & 28 deletions classes/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@
*/
class Base {

/**
* Data that has been sent to the debugger, appended as the plugin operates.
*
* @access public
* @var string $debug_data
*/
public static $debug_data = '';

/**
* Temporarily enable debug mode, used to collect system info on specific pages.
*
* @access public
* @var bool $temp_debug
*/
public static $temp_debug = false;

/**
* Content directory (URL) for the plugin to use.
*
Expand Down Expand Up @@ -231,6 +247,17 @@ public function __construct( $debug = false ) {
if ( ! $debug ) {
return;
}
// Check to see if we're in the wp-admin to enable debugging temporarily.
// Done after the above, because this means we are constructing the Plugin() object
// which is the very first object initialized.
if (
! self::$temp_debug &&
is_admin() &&
! wp_doing_ajax() &&
! $this->get_option( $this->prefix . 'debug' )
) {
self::$temp_debug = true;
}
$this->debug_message( '<b>' . __METHOD__ . '()</b>' );
$this->debug_message( "plugin (resource) content_url: $this->content_url" );
$this->debug_message( "plugin (resource) content_dir: $this->content_dir" );
Expand Down Expand Up @@ -271,23 +298,34 @@ public function set_content_dir( $sub_folder ) {
}

/**
* Saves the in-memory debug log to a logfile in the plugin folder.
* Get the path to the current debug log, if one exists. Otherwise, generate a new filename.
*
* @global string $eio_debug The in-memory debug log.
* @return string The full path to the debug log.
*/
public function debug_log_path() {
$potential_logs = \scandir( $this->content_dir );
if ( $this->is_iterable( $potential_logs ) ) {
foreach ( $potential_logs as $potential_log ) {
if ( $this->str_ends_with( $potential_log, '.log' ) && false !== strpos( $potential_log, strtolower( __NAMESPACE__ ) . '-debug-' ) && is_file( $this->content_dir . $potential_log ) ) {
return $this->content_dir . $potential_log;
}
}
}
return $this->content_dir . strtolower( __NAMESPACE__ ) . '-debug-' . uniqid() . '.log';
}

/**
* Saves the in-memory debug log to a logfile in the plugin folder.
*/
public function debug_log() {
global $eio_debug;
global $ewwwio_temp_debug;
global $easyio_temp_debug;
$debug_log = $this->content_dir . 'debug.log';
$debug_log = $this->debug_log_path();
if ( ! \is_dir( $this->content_dir ) && \is_writable( WP_CONTENT_DIR ) ) {
\wp_mkdir_p( $this->content_dir );
}
$debug_enabled = $this->get_option( $this->prefix . 'debug' );
if (
! empty( $eio_debug ) &&
empty( $easyio_temp_debug ) &&
$debug_enabled &&
! empty( self::$debug_data ) &&
empty( self::$temp_debug ) &&
$this->get_option( $this->prefix . 'debug' ) &&
\is_dir( $this->content_dir ) &&
\is_writable( $this->content_dir )
) {
Expand All @@ -299,24 +337,22 @@ public function debug_log() {
} else {
if ( \filesize( $debug_log ) + 4000000 + \memory_get_usage( true ) > $memory_limit ) {
\unlink( $debug_log );
\clearstatcache();
$debug_log = $this->debug_log_path();
\touch( $debug_log );
}
}
if ( \filesize( $debug_log ) + \strlen( $eio_debug ) + 4000000 + \memory_get_usage( true ) <= $memory_limit && \is_writable( $debug_log ) ) {
$eio_debug = \str_replace( '<br>', "\n", $eio_debug );
\file_put_contents( $debug_log, $timestamp . $eio_debug, FILE_APPEND );
if ( \filesize( $debug_log ) + \strlen( self::$debug_data ) + 4000000 + \memory_get_usage( true ) <= $memory_limit && \is_writable( $debug_log ) ) {
self::$debug_data = \str_replace( '<br>', "\n", self::$debug_data );
\file_put_contents( $debug_log, $timestamp . self::$debug_data, FILE_APPEND );
}
}
$eio_debug = '';
self::$debug_data = '';
}

/**
* Adds information to the in-memory debug log.
*
* @global string $eio_debug The in-memory debug log.
* @global bool $easyio_temp_debug Indicator that we are temporarily debugging on the wp-admin.
* @global bool $ewwwio_temp_debug Indicator that we are temporarily debugging on the wp-admin.
*
* @param string $message Debug information to add to the log.
*/
public function debug_message( $message ) {
Expand All @@ -339,23 +375,29 @@ public function debug_message( $message ) {
\WP_CLI::debug( $message );
return;
}
global $ewwwio_temp_debug;
global $easyio_temp_debug;
if ( $easyio_temp_debug || $ewwwio_temp_debug || $this->get_option( $this->prefix . 'debug' ) ) {
if ( self::$temp_debug || $this->get_option( $this->prefix . 'debug' ) ) {
$memory_limit = $this->memory_limit();
if ( \strlen( $message ) + 4000000 + \memory_get_usage( true ) <= $memory_limit ) {
global $eio_debug;
$message = \str_replace( "\n\n\n", '<br>', $message );
$message = \str_replace( "\n\n", '<br>', $message );
$message = \str_replace( "\n", '<br>', $message );
$eio_debug .= "$message<br>";
$message = \str_replace( "\n\n\n", '<br>', $message );
$message = \str_replace( "\n\n", '<br>', $message );
$message = \str_replace( "\n", '<br>', $message );
self::$debug_data .= "$message<br>";
} else {
global $eio_debug;
$eio_debug = "not logging message, memory limit is $memory_limit";
self::$debug_data = "not logging message, memory limit is $memory_limit";
}
}
}

/**
* Clears temp debugging mode and flushes the debug data if needed.
*/
public function temp_debug_end() {
if ( ! $this->get_option( $this->prefix . 'debug' ) ) {
self::$debug_data = '';
}
self::$temp_debug = false;
}

/**
* Escape any spaces in the filename.
*
Expand Down
5 changes: 1 addition & 4 deletions classes/class-ewww-nextgen.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,6 @@ public function ewww_manage_images_row_actions( $actions ) {

/**
* Output the html for the bulk optimize page.
*
* @global string $eio_debug In-memory debug log.
*/
public function ewww_ngg_bulk_preview() {
if ( ! empty( $_REQUEST['doaction'] ) ) {
Expand Down Expand Up @@ -738,9 +736,8 @@ public function ewww_ngg_bulk_preview() {
}
echo '</div>';
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_debug' ) ) {
global $eio_debug;
echo '<div style="clear:both;"></div>';
echo '<p><strong>' . esc_html__( 'Debugging Information', 'ewww-image-optimizer' ) . ':</strong></p><div style="border:1px solid #e5e5e5;background:#fff;overflow:auto;height:300px;width:800px;">' . wp_kses_post( $eio_debug ) . '</div>';
echo '<p><strong>' . esc_html__( 'Debugging Information', 'ewww-image-optimizer' ) . ':</strong></p><div style="border:1px solid #e5e5e5;background:#fff;overflow:auto;height:300px;width:800px;">' . wp_kses_post( EWWW\Base::$debug_data ) . '</div>';
}
echo '</div>';
}
Expand Down
Loading

0 comments on commit b02db4b

Please sign in to comment.