esc_url_raw( rest_url() ), 'nonce' => wp_create_nonce( 'wp_rest' ), ] ); } /** * Get the length of a value. * * @param mixed $value The input value. * * @return string */ private function get_length( $value ) { if ( is_array( $value ) || is_object( $value ) ) { // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- intended here. $length = strlen( serialize( $value ) ); } elseif ( is_string( $value ) || is_numeric( $value ) ) { $length = strlen( strval( $value ) ); } if ( ! isset( $length ) ) { return '-'; } return number_format( ( $length / 1024 ), 2 ); } /** * Print a value. * * @param mixed $value The input value. * * @return string */ private function print_value( $value ) { if ( is_array( $value ) || is_object( $value ) ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r -- intended here. return print_r( $value, 1 ); } return esc_html( $value ); } /** * Renders the admin page. */ public function render_admin_page() { $option_optimizer = get_option( 'option_optimizer', [ 'used_options' => [] ] ); $all_options = wp_load_alloptions(); // Filter out transients. $autoload_options = array_filter( $all_options, function ( $value, $key ) { return strpos( $key, '_transient_' ) === false; }, ARRAY_FILTER_USE_BOTH ); $unused_options = []; $non_autoloaded_options = []; // Get the autoloaded options that aren't used. foreach ( $autoload_options as $option => $value ) { if ( isset( $option_optimizer['used_options'][ $option ] ) ) { continue; } $unused_options[ $option ] = $value; } // Determine the options that _are_ used, but not auto-loaded. foreach ( $option_optimizer['used_options'] as $option => $count ) { if ( isset( $autoload_options[ $option ] ) ) { continue; } $non_autoloaded_options[ $option ] = $count; } // Some of the options that are used but not auto-loaded, may not exist. if ( ! empty( $non_autoloaded_options ) ) { $options_that_do_not_exist = []; $non_autoloaded_options_full = []; foreach ( $non_autoloaded_options as $option => $count ) { $value = get_option( $option, 'aaa-no-return-value' ); if ( $value === 'aaa-no-return-value' ) { $options_that_do_not_exist[ $option ] = $count; continue; } $non_autoloaded_options_full[ $option ] = [ 'count' => $count, 'value' => $value, ]; } } // Start HTML output. echo '
' . sprintf( // translators: %1$s is the date, %2$s is the number of options at stat, %3$s is the size at start in KB, %4$s is the number of options now, %5$s is the size in KB now. esc_html__( 'When you started on %1$s you had %2$s autoloaded options, for %3$sKB of memory. Now you have %4$s options, for %5$sKB of memory.', 'aaa-option-optimizer' ), esc_html( gmdate( 'Y-m-d', strtotime( $option_optimizer['starting_point_date'] ) ) ), isset( $option_optimizer['starting_point_num'] ) ? esc_html( $option_optimizer['starting_point_num'] ) : '-', number_format( ( $option_optimizer['starting_point_kb'] ), 1 ), esc_html( $result->count ), number_format( ( $result->autoload_size / 1024 ), 1 ) ) . '
'; echo '' . esc_html__( 'We\'ve found the following things you can maybe optimize:', 'aaa-option-optimizer' ) . '
'; echo '' . esc_html__( 'The following options are autoloaded on each pageload, but AAA Option Optimizer has not been able to detect them being used.', 'aaa-option-optimizer' ); echo '
| ' . esc_html__( 'Option', 'aaa-option-optimizer' ) . ' | '; echo '' . esc_html__( 'Size', 'aaa-option-optimizer' ) . ' | '; echo '' . esc_html__( 'Value', 'aaa-option-optimizer' ) . ' | '; echo '' . esc_html__( 'Actions', 'aaa-option-optimizer' ) . ' | '; echo '
|---|---|---|---|
| ' . esc_html( $option ) . ' | '; echo '' . esc_html( $this->get_length( $value ) ) . 'KB | '; echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped in get_popover_html. echo $this->get_popover_html( $option, $value ); echo ' | '; echo ''; echo ' |
' . esc_html__( 'All autoloaded options are in use.', 'aaa-option-optimizer' ) . '
'; } // Render differences. if ( ! empty( $non_autoloaded_options ) ) { echo '' . esc_html__( 'The following options are *not* autoloaded on each pageload, but AAA Option Optimizer has detected that they are being used. If one of the options below has been called a lot and is not very big, you might consider adding autoload to that option.', 'aaa-option-optimizer' ); echo '
| ' . esc_html__( 'Option', 'aaa-option-optimizer' ) . ' | '; echo '' . esc_html__( 'Size', 'aaa-option-optimizer' ) . ' | '; echo '' . esc_html__( 'Value', 'aaa-option-optimizer' ) . ' | '; echo '' . esc_html__( '# Calls', 'aaa-option-optimizer' ) . ' | '; echo '' . esc_html__( 'Actions', 'aaa-option-optimizer' ) . ' | '; echo '
|---|---|---|---|---|
| ' . esc_html( $option ) . ' | '; echo '' . esc_html( $this->get_length( $arr['value'] ) ) . 'KB | '; echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- output escaped in get_popover_html. echo $this->get_popover_html( $option, $arr['value'] ); echo ' | '; echo '' . esc_html( $arr['count'] ) . ' | '; echo ''; } echo ' |
' . esc_html__( 'All options that are used are autoloaded.', 'aaa-option-optimizer' ) . '
'; } if ( ! empty( $options_that_do_not_exist ) ) { echo '' . esc_html__( 'The following options are requested sometimes, but AAA Option Optimizer has detected that they do not exist. If one of the options below has been called a lot, it might help to create it with a value of false.', 'aaa-option-optimizer' ); echo '
| ' . esc_html__( 'Option', 'aaa-option-optimizer' ) . ' | '; echo '' . esc_html__( '# Calls', 'aaa-option-optimizer' ) . ' | '; echo '' . esc_html__( 'Actions', 'aaa-option-optimizer' ) . ' | '; echo '
|---|---|---|
| ' . esc_html( $option ) . ' | '; echo '' . esc_html( $count ) . ' | '; echo ''; } echo ' |
' . sprintf( esc_html__( 'Value of %s', 'aaa-option-optimizer' ), '' . esc_html( $name ) . '' ) . '
' . esc_html( $string ) . '