|
| 1 | +<?php |
| 2 | + |
| 3 | +// If this file is called directly, abort. |
| 4 | +if ( ! defined( 'WPINC' ) ) { |
| 5 | + die; |
| 6 | +} |
| 7 | + |
| 8 | +/** |
| 9 | + * Register BenchPress admin menu. |
| 10 | + * |
| 11 | + * @since 1.0.0 |
| 12 | + * @return void |
| 13 | + */ |
| 14 | +function benchpress_admin_menu() { |
| 15 | + add_menu_page( |
| 16 | + esc_html__( 'BenchPress', 'benchpress' ), |
| 17 | + esc_html__( 'BenchPress', 'benchpress' ), |
| 18 | + 'manage_options', |
| 19 | + 'benchpress', |
| 20 | + 'benchpress_render_page', |
| 21 | + plugin_dir_url( __FILE__ ) . '../assets/img/barbell-icon.svg', |
| 22 | + 2 |
| 23 | + ); |
| 24 | + |
| 25 | + add_submenu_page( |
| 26 | + 'benchpress', |
| 27 | + esc_html__( 'Snapshots', 'benchpress' ), |
| 28 | + esc_html__( 'Snapshots', 'benchpress' ), |
| 29 | + 'manage_options', |
| 30 | + 'benchpress-snapshots', |
| 31 | + 'benchpress_render_snapshots_page' |
| 32 | + ); |
| 33 | + |
| 34 | + add_submenu_page( |
| 35 | + 'benchpress', |
| 36 | + esc_html__( 'Settings', 'benchpress' ), |
| 37 | + esc_html__( 'Settings', 'benchpress' ), |
| 38 | + 'manage_options', |
| 39 | + 'benchpress-settings', |
| 40 | + 'benchpress_render_settings_page' |
| 41 | + ); |
| 42 | +} |
| 43 | +add_action( 'admin_menu', 'benchpress_admin_menu' ); |
| 44 | + |
| 45 | +/** |
| 46 | + * Renders the BenchPress Snapshots page in the WordPress® admin area. |
| 47 | + * |
| 48 | + * This function outputs the HTML markup for the BenchPress Snapshots page, |
| 49 | + * including buttons for clearing and downloading snapshots, a table displaying |
| 50 | + * snapshots, and a modal for viewing detailed snapshot data. |
| 51 | + * |
| 52 | + * @since 1.0.0 |
| 53 | + * @return void |
| 54 | + */ |
| 55 | +function benchpress_render_snapshots_page() { |
| 56 | + echo '<div class="wrap">'; |
| 57 | + benchpress_admin_header( esc_html__( 'BenchPress Settings', 'benchpress' ) ); |
| 58 | + |
| 59 | + $table = new BenchPress_Snapshots_Table(); |
| 60 | + $table->prepare_items(); |
| 61 | + $table->display(); |
| 62 | + |
| 63 | + // Modal HTML for viewing the snapshot data. |
| 64 | + echo ' |
| 65 | + <div id="snapshotModal" class="snapshot-modal" style="display:none;"> |
| 66 | + <div class="snapshot-modal-content"> |
| 67 | + <span class="snapshot-modal-close" style="cursor:pointer;">×</span> |
| 68 | + <h2>' . esc_html__( 'Snapshot Data', 'benchpress' ) . '</h2> |
| 69 | + <div id="snapshotModalData"></div> |
| 70 | + </div> |
| 71 | + </div>'; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Render the BenchPress plugin's main page with a refresh button. |
| 76 | + * |
| 77 | + * @since 1.0.0 |
| 78 | + * @return void |
| 79 | + */ |
| 80 | +function benchpress_render_page() { |
| 81 | + if ( ! current_user_can( 'manage_options' ) ) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + global $benchpress_start_time; |
| 86 | + $benchpress_execution_time = microtime( true ) - $benchpress_start_time; |
| 87 | + $formatted_execution_time = number_format( $benchpress_execution_time, 4 ); |
| 88 | + |
| 89 | + echo '<div class="wrap">'; |
| 90 | + benchpress_admin_header( esc_html__( 'BenchPress', 'benchpress' ) ); |
| 91 | + |
| 92 | + echo '<div id="benchpress-results">'; |
| 93 | + $table = new BenchPress_Table(); |
| 94 | + $table->prepare_items(); |
| 95 | + $table->display(); |
| 96 | + echo '<p>' . sprintf( esc_html__( 'Total Execution Time: %s seconds', 'benchpress' ), $formatted_execution_time ) . '</p>'; |
| 97 | + echo '</div></div>'; |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Render the BenchPress settings page. |
| 102 | + * |
| 103 | + * @since 1.0.0 |
| 104 | + * @return void |
| 105 | + */ |
| 106 | +function benchpress_render_settings_page() { |
| 107 | + if ( ! current_user_can( 'manage_options' ) ) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + // Save settings if the form is submitted and nonce is verified. |
| 112 | + if ( isset( $_POST['benchpress_settings_nonce'] ) && wp_verify_nonce( $_POST['benchpress_settings_nonce'], 'benchpress_save_settings' ) ) { |
| 113 | + update_option( 'benchpress_loop_count', absint( $_POST['benchpress_loop_count'] ) ); |
| 114 | + update_option( 'benchpress_enable_switch_vs_match', isset( $_POST['benchpress_enable_switch_vs_match'] ) ? 1 : 0 ); |
| 115 | + update_option( 'benchpress_query_type', sanitize_text_field( $_POST['benchpress_query_type'] ) ); |
| 116 | + update_option( 'benchpress_post_id', array_map( 'absint', $_POST['benchpress_post_id'] ?? [] ) ); |
| 117 | + update_option( 'benchpress_post_type', sanitize_text_field( $_POST['benchpress_post_type'] ) ); |
| 118 | + update_option( 'benchpress_post_count', absint( $_POST['benchpress_post_count'] ) ); |
| 119 | + update_option( 'benchpress_taxonomy', sanitize_text_field( $_POST['benchpress_taxonomy'] ) ); |
| 120 | + update_option( 'benchpress_tax_terms', sanitize_text_field( $_POST['benchpress_tax_terms'] ) ); |
| 121 | + update_option( 'benchpress_orderby', sanitize_text_field( $_POST['benchpress_orderby'] ) ); |
| 122 | + update_option( 'benchpress_order', in_array( $_POST['benchpress_order'], ['ASC', 'DESC'] ) ? $_POST['benchpress_order'] : 'ASC' ); |
| 123 | + update_option( 'benchpress_enable_transient_vs_query', isset( $_POST['benchpress_enable_transient_vs_query'] ) ? 1 : 0 ); |
| 124 | + update_option( 'benchpress_enable_meta_query_test', isset( $_POST['benchpress_enable_meta_query_test'] ) ? 1 : 0 ); |
| 125 | + } |
| 126 | + |
| 127 | + // Retrieve saved settings. |
| 128 | + $loop_count = get_option( 'benchpress_loop_count', 1000000 ); |
| 129 | + $enable_switch_vs_match = get_option( 'benchpress_enable_switch_vs_match', 1 ); |
| 130 | + $query_type = get_option( 'benchpress_query_type', 'single' ); |
| 131 | + $post_id = get_option( 'benchpress_post_id', [] ); |
| 132 | + $post_type = get_option( 'benchpress_post_type', 'post' ); |
| 133 | + $post_count = get_option( 'benchpress_post_count', 5 ); |
| 134 | + $taxonomy = get_option( 'benchpress_taxonomy', '' ); |
| 135 | + $tax_terms = get_option( 'benchpress_tax_terms', '' ); |
| 136 | + $orderby = get_option( 'benchpress_orderby', 'date' ); |
| 137 | + $order = get_option( 'benchpress_order', 'ASC' ); |
| 138 | + |
| 139 | + // Get public post types and taxonomies for dropdown options. |
| 140 | + $post_types = get_post_types( [ 'public' => true ], 'objects' ); |
| 141 | + $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' ); |
| 142 | + |
| 143 | + echo '<div class="wrap">'; |
| 144 | + benchpress_admin_header( esc_html__( 'BenchPress Settings', 'benchpress' ) ); |
| 145 | + echo '<form method="post">'; |
| 146 | + wp_nonce_field( 'benchpress_save_settings', 'benchpress_settings_nonce' ); |
| 147 | + |
| 148 | + // Benchmark Options Section. |
| 149 | + echo '<h2>' . esc_html__( 'Benchmark Options', 'benchpress' ) . '</h2>'; |
| 150 | + echo '<table class="form-table">'; |
| 151 | + echo '<tr><th>' . esc_html__( 'Loop Count for Benchmarks', 'benchpress' ) . '</th>'; |
| 152 | + echo '<td><input type="number" name="benchpress_loop_count" value="' . esc_attr( $loop_count ) . '" /></td></tr>'; |
| 153 | + // Only add this setting when PHP 8.0+ is installed. |
| 154 | + if ( version_compare( PHP_VERSION, '8.0', '>=' ) ) { |
| 155 | + echo '<tr><th>' . esc_html__( 'Enable Switch vs Match Benchmark', 'benchpress' ) . '</th>'; |
| 156 | + echo '<td><input type="checkbox" name="benchpress_enable_switch_vs_match" ' . checked( 1, $enable_switch_vs_match, false ) . ' /></td></tr>'; |
| 157 | + } |
| 158 | + echo '<tr><th>' . esc_html__( 'Enable Transient vs Direct Query Benchmark', 'benchpress' ) . '</th>'; |
| 159 | + echo '<td><input type="checkbox" name="benchpress_enable_transient_vs_query" ' . checked( 1, get_option( 'benchpress_enable_transient_vs_query', 1 ), false ) . ' /></td></tr>'; |
| 160 | + echo '<tr><th>' . esc_html__( 'Enable Post Meta Access Benchmark', 'benchpress' ) . '</th>'; |
| 161 | + echo '<td><input type="checkbox" name="benchpress_enable_meta_query_test" ' . checked( 1, get_option( 'benchpress_enable_meta_query_test', 1 ), false ) . ' /></td></tr>'; |
| 162 | + |
| 163 | + echo '</table>'; |
| 164 | + |
| 165 | + // WP_Query Customization Section. |
| 166 | + echo '<h2>' . esc_html__( 'WP_Query Settings', 'benchpress' ) . '</h2>'; |
| 167 | + echo '<table class="form-table">'; |
| 168 | + |
| 169 | + // Query Type. |
| 170 | + echo '<tr><th>' . esc_html__( 'Query Type', 'benchpress' ) . '</th>'; |
| 171 | + echo '<td> |
| 172 | + <select name="benchpress_query_type"> |
| 173 | + <option value="single" ' . selected( $query_type, 'single', false ) . '>' . esc_html__( 'Single Post', 'benchpress' ) . '</option> |
| 174 | + <option value="multiple" ' . selected( $query_type, 'multiple', false ) . '>' . esc_html__( 'Multiple Posts', 'benchpress' ) . '</option> |
| 175 | + </select> |
| 176 | + </td></tr>'; |
| 177 | + echo '</table>'; |
| 178 | + |
| 179 | + // Single Post ID selection (displayed only when "Single Post" is selected). |
| 180 | + echo '<div id="single-post-fields" style="display:none;">'; |
| 181 | + echo '<h3>' . esc_html__( 'Single Post Query', 'benchpress' ) . '</h3>'; |
| 182 | + echo '<table class="form-table">'; |
| 183 | + echo '<tr><th>' . esc_html__( 'Post ID', 'benchpress' ) . '</th>'; |
| 184 | + echo '<td><input type="number" name="benchpress_post_id[]" value="' . esc_attr( is_array( $post_id ) ? reset( $post_id ) : '' ) . '" /></td></tr>'; |
| 185 | + echo '</table>'; |
| 186 | + echo '</div>'; |
| 187 | + |
| 188 | + // Multiple Post Query settings (displayed only when "Multiple Posts" is selected). |
| 189 | + echo '<div id="multiple-post-fields" style="display:none;">'; |
| 190 | + echo '<h3>' . esc_html__( 'Multiple Post Query', 'benchpress' ) . '</h3>'; |
| 191 | + echo '<table class="form-table">'; |
| 192 | + |
| 193 | + // Post Type. |
| 194 | + echo '<tr><th>' . esc_html__( 'Post Type', 'benchpress' ) . '</th>'; |
| 195 | + echo '<td><select name="benchpress_post_type">'; |
| 196 | + foreach ( $post_types as $type => $obj ) { |
| 197 | + printf( |
| 198 | + '<option value="%s" %s>%s</option>', |
| 199 | + esc_attr( $type ), |
| 200 | + selected( $post_type, $type, false ), |
| 201 | + esc_html( $obj->label ) |
| 202 | + ); |
| 203 | + } |
| 204 | + echo '</select></td></tr>'; |
| 205 | + |
| 206 | + // Post Count. |
| 207 | + echo '<tr><th>' . esc_html__( 'Number of Posts', 'benchpress' ) . '</th>'; |
| 208 | + echo '<td><input type="number" name="benchpress_post_count" value="' . esc_attr( $post_count ) . '" /></td></tr>'; |
| 209 | + |
| 210 | + // Taxonomy and Terms. |
| 211 | + echo '<tr><th>' . esc_html__( 'Taxonomy', 'benchpress' ) . '</th>'; |
| 212 | + echo '<td><select name="benchpress_taxonomy">'; |
| 213 | + echo '<option value="">' . esc_html__( 'Select Taxonomy', 'benchpress' ) . '</option>'; |
| 214 | + foreach ( $taxonomies as $tax => $obj ) { |
| 215 | + printf( |
| 216 | + '<option value="%s" %s>%s</option>', |
| 217 | + esc_attr( $tax ), |
| 218 | + selected( $taxonomy, $tax, false ), |
| 219 | + esc_html( $obj->label ) |
| 220 | + ); |
| 221 | + } |
| 222 | + echo '</select></td></tr>'; |
| 223 | + |
| 224 | + // Terms (comma-separated). |
| 225 | + echo '<tr><th>' . esc_html__( 'Terms (comma-separated)', 'benchpress' ) . '</th>'; |
| 226 | + echo '<td><input type="text" name="benchpress_tax_terms" value="' . esc_attr( $tax_terms ) . '" /></td></tr>'; |
| 227 | + |
| 228 | + // Order By and Order. |
| 229 | + echo '<tr><th>' . esc_html__( 'Order By', 'benchpress' ) . '</th>'; |
| 230 | + echo '<td><select name="benchpress_orderby">'; |
| 231 | + $orderby_options = [ |
| 232 | + 'date' => esc_html__( 'Date', 'benchpress' ), |
| 233 | + 'title' => esc_html__( 'Title', 'benchpress' ), |
| 234 | + 'ID' => esc_html__( 'ID', 'benchpress' ), |
| 235 | + 'name' => esc_html__( 'Name', 'benchpress' ), |
| 236 | + 'author' => esc_html__( 'Author', 'benchpress' ), |
| 237 | + 'modified' => esc_html__( 'Modified', 'benchpress' ), |
| 238 | + 'rand' => esc_html__( 'Random', 'benchpress' ), |
| 239 | + ]; |
| 240 | + foreach ( $orderby_options as $value => $label ) { |
| 241 | + printf( |
| 242 | + '<option value="%s" %s>%s</option>', |
| 243 | + esc_attr( $value ), |
| 244 | + selected( $orderby, $value, false ), |
| 245 | + esc_html( $label ) |
| 246 | + ); |
| 247 | + } |
| 248 | + echo '</select></td></tr>'; |
| 249 | + |
| 250 | + echo '<tr><th>' . esc_html__( 'Order', 'benchpress' ) . '</th>'; |
| 251 | + echo '<td> |
| 252 | + <select name="benchpress_order"> |
| 253 | + <option value="ASC" ' . selected( $order, 'ASC', false ) . '>' . esc_html__( 'Ascending', 'benchpress' ) . '</option> |
| 254 | + <option value="DESC" ' . selected( $order, 'DESC', false ) . '>' . esc_html__( 'Descending', 'benchpress' ) . '</option> |
| 255 | + </select> |
| 256 | + </td></tr>'; |
| 257 | + echo '</table>'; |
| 258 | + echo '</div>'; |
| 259 | + |
| 260 | + submit_button( esc_html__( 'Save Settings', 'benchpress' ) ); |
| 261 | + echo '</form></div>'; |
| 262 | +} |
0 commit comments