|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Server-Timing API admin integration file. |
| 4 | + * |
| 5 | + * @package performance-lab |
| 6 | + */ |
| 7 | + |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { |
| 9 | + exit; // Exit if accessed directly. |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Adds the Server-Timing page to the Tools menu. |
| 14 | + * |
| 15 | + * @since n.e.x.t |
| 16 | + */ |
| 17 | +function perflab_add_server_timing_page() { |
| 18 | + $hook_suffix = add_management_page( |
| 19 | + __( 'Server-Timing', 'performance-lab' ), |
| 20 | + __( 'Server-Timing', 'performance-lab' ), |
| 21 | + 'manage_options', |
| 22 | + PERFLAB_SERVER_TIMING_SCREEN, |
| 23 | + 'perflab_render_server_timing_page' |
| 24 | + ); |
| 25 | + |
| 26 | + // Add the following hooks only if the screen was successfully added. |
| 27 | + if ( false !== $hook_suffix ) { |
| 28 | + add_action( "load-{$hook_suffix}", 'perflab_load_server_timing_page' ); |
| 29 | + } |
| 30 | + |
| 31 | + return $hook_suffix; |
| 32 | +} |
| 33 | +add_action( 'admin_menu', 'perflab_add_server_timing_page' ); |
| 34 | + |
| 35 | +/** |
| 36 | + * Initializes settings sections and fields for the Server-Timing page. |
| 37 | + * |
| 38 | + * @since n.e.x.t |
| 39 | + */ |
| 40 | +function perflab_load_server_timing_page() { |
| 41 | + add_settings_section( |
| 42 | + 'benchmarking', |
| 43 | + __( 'Benchmarking', 'performance-lab' ), |
| 44 | + static function() { |
| 45 | + ?> |
| 46 | + <p> |
| 47 | + <?php |
| 48 | + echo wp_kses( |
| 49 | + sprintf( |
| 50 | + /* translators: %s: Server-Timing */ |
| 51 | + __( 'In this section, you can provide hook names to include measurements for them in the %s header.', 'performance-lab' ), |
| 52 | + '<code>Server-Timing</code>' |
| 53 | + ), |
| 54 | + array( 'code' => array() ) |
| 55 | + ); |
| 56 | + ?> |
| 57 | + <br> |
| 58 | + <?php |
| 59 | + echo wp_kses( |
| 60 | + __( 'For any hook name provided, the <strong>cumulative duration between all callbacks</strong> attached to the hook is measured, in milliseconds.', 'performance-lab' ), |
| 61 | + array( 'strong' => array() ) |
| 62 | + ); |
| 63 | + if ( ! perflab_server_timing_use_output_buffer() ) { |
| 64 | + ?> |
| 65 | + <br> |
| 66 | + <?php |
| 67 | + echo wp_kses( |
| 68 | + sprintf( |
| 69 | + /* translators: 1: Server-Timing, 2: template_include */ |
| 70 | + __( 'Since the %1$s header is sent before the template is loaded, only hooks before the %2$s filter can be measured.', 'performance-lab' ), |
| 71 | + '<code>Server-Timing</code>', |
| 72 | + '<code>template_include</code>' |
| 73 | + ), |
| 74 | + array( 'code' => array() ) |
| 75 | + ); |
| 76 | + } |
| 77 | + ?> |
| 78 | + </p> |
| 79 | + <?php |
| 80 | + }, |
| 81 | + PERFLAB_SERVER_TIMING_SCREEN |
| 82 | + ); |
| 83 | + |
| 84 | + /* |
| 85 | + * For all settings fields, the field slug, option sub key, and label |
| 86 | + * suffix have to match for the rendering in the callback to be |
| 87 | + * semantically correct. |
| 88 | + */ |
| 89 | + add_settings_field( |
| 90 | + 'benchmarking_actions', |
| 91 | + __( 'Actions', 'performance-lab' ), |
| 92 | + static function() { |
| 93 | + perflab_render_server_timing_page_field( 'benchmarking_actions' ); |
| 94 | + }, |
| 95 | + PERFLAB_SERVER_TIMING_SCREEN, |
| 96 | + 'benchmarking', |
| 97 | + array( 'label_for' => 'server_timing_benchmarking_actions' ) |
| 98 | + ); |
| 99 | + add_settings_field( |
| 100 | + 'benchmarking_filters', |
| 101 | + __( 'Filters', 'performance-lab' ), |
| 102 | + static function() { |
| 103 | + perflab_render_server_timing_page_field( 'benchmarking_filters' ); |
| 104 | + }, |
| 105 | + PERFLAB_SERVER_TIMING_SCREEN, |
| 106 | + 'benchmarking', |
| 107 | + array( 'label_for' => 'server_timing_benchmarking_filters' ) |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Renders the Server-Timing page. |
| 113 | + * |
| 114 | + * @since n.e.x.t |
| 115 | + */ |
| 116 | +function perflab_render_server_timing_page() { |
| 117 | + ?> |
| 118 | + <div class="wrap"> |
| 119 | + <?php settings_errors(); ?> |
| 120 | + <h1> |
| 121 | + <?php esc_html_e( 'Server-Timing', 'performance-lab' ); ?> |
| 122 | + </h1> |
| 123 | + |
| 124 | + <form action="options.php" method="post"> |
| 125 | + <?php settings_fields( PERFLAB_SERVER_TIMING_SCREEN ); ?> |
| 126 | + <?php do_settings_sections( PERFLAB_SERVER_TIMING_SCREEN ); ?> |
| 127 | + <?php submit_button(); ?> |
| 128 | + </form> |
| 129 | + </div> |
| 130 | + <?php |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Renders a field for the given Server-Timing option. |
| 135 | + * |
| 136 | + * @since n.e.x.t |
| 137 | + * |
| 138 | + * @param string $slug Slug of the field and sub-key in the Server-Timing option. |
| 139 | + */ |
| 140 | +function perflab_render_server_timing_page_field( $slug ) { |
| 141 | + $options = (array) get_option( PERFLAB_SERVER_TIMING_SETTING, array() ); |
| 142 | + |
| 143 | + // Value for the sub-key is an array of hook names. |
| 144 | + $value = ''; |
| 145 | + if ( isset( $options[ $slug ] ) ) { |
| 146 | + $value = implode( "\n", $options[ $slug ] ); |
| 147 | + } |
| 148 | + |
| 149 | + $field_id = "server_timing_{$slug}"; |
| 150 | + $field_name = PERFLAB_SERVER_TIMING_SETTING . '[' . $slug . ']'; |
| 151 | + $description_id = "{$field_id}_description"; |
| 152 | + |
| 153 | + ?> |
| 154 | + <textarea |
| 155 | + id="<?php echo esc_attr( $field_id ); ?>" |
| 156 | + name="<?php echo esc_attr( $field_name ); ?>" |
| 157 | + aria-describedby="<?php echo esc_attr( $description_id ); ?>" |
| 158 | + class="large-text code" |
| 159 | + rows="8" |
| 160 | + ><?php echo esc_textarea( $value ); ?></textarea> |
| 161 | + <p id="<?php echo esc_attr( $description_id ); ?>" class="description"> |
| 162 | + <?php esc_html_e( 'Enter a single hook name per line.', 'performance-lab' ); ?> |
| 163 | + </p> |
| 164 | + <?php |
| 165 | +} |
0 commit comments