| @@ -86,38 +86,38 @@ | ||
| 86 | 86 | <?php wp_nonce_field( 'top_ten_import_wpp_nonce', 'top_ten_import_wpp_nonce_field' ); ?> |
| 87 | 87 | <input type="hidden" name="action" value="top_ten_import_wpp"> |
| 88 | 88 | <table class="form-table"> |
| 89 | 89 | <tr> |
| 90 | - <th scope="row"><?php esc_html_e( 'Import Mode', 'top-ten' ); ?></th> | |
| 90 | + <th scope="row"><?php esc_html_e( 'Import Mode', 'top-10' ); ?></th> | |
| 91 | 91 | <td> |
| 92 | 92 | <fieldset> |
| 93 | - <legend class="screen-reader-text"><?php esc_html_e( 'Import Mode', 'top-ten' ); ?></legend> | |
| 93 | + <legend class="screen-reader-text"><?php esc_html_e( 'Import Mode', 'top-10' ); ?></legend> | |
| 94 | 94 | <label> |
| 95 | 95 | <input type="radio" name="import_mode" value="merge"> |
| 96 | - <span><?php esc_html_e( 'Merge data (add WPP counts to existing Top 10 counts)', 'top-ten' ); ?></span> | |
| 96 | + <span><?php esc_html_e( 'Merge data (add WPP counts to existing Top 10 counts)', 'top-10' ); ?></span> | |
| 97 | 97 | </label> |
| 98 | 98 | <br> |
| 99 | 99 | <label> |
| 100 | 100 | <input type="radio" name="import_mode" value="replace" checked="checked"> |
| 101 | - <span><?php esc_html_e( 'Replace data (replace Top 10 counts with WPP counts)', 'top-ten' ); ?></span> | |
| 101 | + <span><?php esc_html_e( 'Replace data (replace Top 10 counts with WPP counts)', 'top-10' ); ?></span> | |
| 102 | 102 | </label> |
| 103 | 103 | </fieldset> |
| 104 | - <p class="description"><?php esc_html_e( 'Merge mode will add WPP counts to existing Top 10 counts, while replace mode will replace Top 10 counts with WPP counts if they exists for the same post.', 'top-ten' ); ?></p> | |
| 104 | + <p class="description"><?php esc_html_e( 'Merge mode will add WPP counts to existing Top 10 counts, while replace mode will replace Top 10 counts with WPP counts if they exist for the same post.', 'top-10' ); ?></p> | |
| 105 | 105 | </td> |
| 106 | 106 | </tr> |
| 107 | 107 | <tr> |
| 108 | - <th scope="row"><?php esc_html_e( 'Data to Import', 'top-ten' ); ?></th> | |
| 108 | + <th scope="row"><?php esc_html_e( 'Data to Import', 'top-10' ); ?></th> | |
| 109 | 109 | <td> |
| 110 | 110 | <fieldset> |
| 111 | - <legend class="screen-reader-text"><?php esc_html_e( 'Data to Import', 'top-ten' ); ?></legend> | |
| 111 | + <legend class="screen-reader-text"><?php esc_html_e( 'Data to Import', 'top-10' ); ?></legend> | |
| 112 | 112 | <label> |
| 113 | 113 | <input type="radio" name="import_data" value="total" checked="checked"> |
| 114 | - <span><?php esc_html_e( 'Total counts only', 'top-ten' ); ?></span> | |
| 114 | + <span><?php esc_html_e( 'Total counts only', 'top-10' ); ?></span> | |
| 115 | 115 | </label> |
| 116 | 116 | <br> |
| 117 | 117 | <label> |
| 118 | 118 | <input type="radio" name="import_data" value="daily"> |
| 119 | - <span><?php esc_html_e( 'Daily counts only', 'top-ten' ); ?></span> | |
| 119 | + <span><?php esc_html_e( 'Daily counts only', 'top-10' ); ?></span> | |
| 120 | 120 | </label> |
| 121 | 121 | <br> |
| 122 | 122 | <label> |
| 123 | 123 | <input type="radio" name="import_data" value="both"> |
| @@ -127,27 +127,27 @@ | ||
| 127 | 127 | <p class="description"><?php esc_html_e( 'Total counts only will import only the total view counts from WPP, while daily counts only will import only the daily view counts from WPP. Both total and daily counts will import both total and daily view counts from WPP. If you have a very large dataset, it might be better to import total and daily counts separately.', 'top-10' ); ?></p> |
| 128 | 128 | </td> |
| 129 | 129 | </tr> |
| 130 | 130 | <tr> |
| 131 | - <th scope="row"><?php esc_html_e( 'Minimum View Count', 'top-ten' ); ?></th> | |
| 131 | + <th scope="row"><?php esc_html_e( 'Minimum View Count', 'top-10' ); ?></th> | |
| 132 | 132 | <td> |
| 133 | 133 | <input type="number" name="min_views" value="1" min="1" step="1"> |
| 134 | - <p class="description"><?php esc_html_e( 'Only import posts with at least this many views. Use this setting to filter out posts with very few views.', 'top-ten' ); ?></p> | |
| 134 | + <p class="description"><?php esc_html_e( 'Only import posts with at least this many views. Use this setting to filter out posts with very few views.', 'top-10' ); ?></p> | |
| 135 | 135 | </td> |
| 136 | 136 | </tr> |
| 137 | 137 | <tr> |
| 138 | - <th scope="row"><?php esc_html_e( 'Dry Run', 'top-ten' ); ?></th> | |
| 138 | + <th scope="row"><?php esc_html_e( 'Dry Run', 'top-10' ); ?></th> | |
| 139 | 139 | <td> |
| 140 | 140 | <label> |
| 141 | 141 | <input type="checkbox" name="dry_run" value="1" checked="checked"> |
| 142 | - <?php esc_html_e( 'Enable Dry Run (simulate import)', 'top-ten' ); ?> | |
| 142 | + <?php esc_html_e( 'Enable Dry Run (simulate import)', 'top-10' ); ?> | |
| 143 | 143 | </label> |
| 144 | - <p class="description"><?php esc_html_e( 'A dry run will simulate the import process without actually updating the database. This is useful for testing the import parameters.', 'top-ten' ); ?></p> | |
| 144 | + <p class="description"><?php esc_html_e( 'A dry run will simulate the import process without actually updating the database. This is useful for testing the import parameters.', 'top-10' ); ?></p> | |
| 145 | 145 | </td> |
| 146 | 146 | </tr> |
| 147 | 147 | <?php if ( is_multisite() && is_network_admin() ) : ?> |
| 148 | 148 | <tr> |
| 149 | - <th scope="row"><?php esc_html_e( 'Select Sites', 'top-ten' ); ?></th> | |
| 149 | + <th scope="row"><?php esc_html_e( 'Select Sites', 'top-10' ); ?></th> | |
| 150 | 150 | <td> |
| 151 | 151 | <?php |
| 152 | 152 | $sites = get_sites(); |
| 153 | 153 | if ( ! empty( $sites ) ) { |
| @@ -164,9 +164,9 @@ | ||
| 164 | 164 | </td> |
| 165 | 165 | </tr> |
| 166 | 166 | <?php endif; ?> |
| 167 | 167 | </table> |
| 168 | - <?php submit_button( esc_html__( 'Run Import', 'top-ten' ), 'primary', 'submit', true, array( 'id' => 'top-ten-wpp-import-submit' ) ); ?> | |
| 168 | + <?php submit_button( esc_html__( 'Run Import', 'top-10' ), 'primary', 'submit', true, array( 'id' => 'top-ten-wpp-import-submit' ) ); ?> | |
| 169 | 169 | |
| 170 | 170 | <p id="top-ten-wpp-import-progress" class="hidden notice notice-info"> |
| 171 | 171 | </p> |
| 172 | 172 | <p id="top-ten-wpp-import-results" class="hidden notice notice-info"> |
| @@ -292,17 +292,34 @@ | ||
| 292 | 292 | */ |
| 293 | 293 | private function wpp_tables_exist(): bool { |
| 294 | 294 | global $wpdb; |
| 295 | 295 | |
| 296 | + $tables = array( | |
| 297 | + $wpdb->prefix . 'popularpostsdata', | |
| 298 | + $wpdb->prefix . 'popularpostssummary', | |
| 299 | + ); | |
| 300 | + $key = implode( '|', $tables ); | |
| 301 | + static $cache = array(); | |
| 302 | + | |
| 303 | + if ( array_key_exists( $key, $cache ) ) { | |
| 304 | + return $cache[ $key ]; | |
| 305 | + } | |
| 306 | + | |
| 296 | 307 | $tables_exist = true; |
| 308 | + foreach ( $tables as $table ) { | |
| 309 | + $query = $wpdb->prepare( 'SELECT 1 FROM %i LIMIT 0', $table ); | |
| 310 | + $suppress_errors = $wpdb->suppress_errors(); | |
| 297 | 311 | |
| 298 | - $data_table = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}popularpostsdata'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 299 | - $summary_table = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}popularpostssummary'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 312 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared | |
| 313 | + if ( false === $wpdb->query( $query ) ) { | |
| 314 | + $tables_exist = false; | |
| 315 | + } | |
| 300 | 316 | |
| 301 | - if ( ! $data_table || ! $summary_table ) { | |
| 302 | - $tables_exist = false; | |
| 317 | + $wpdb->suppress_errors( $suppress_errors ); | |
| 303 | 318 | } |
| 304 | 319 | |
| 320 | + $cache[ $key ] = $tables_exist; | |
| 321 | + | |
| 305 | 322 | return $tables_exist; |
| 306 | 323 | } |
| 307 | 324 | |
| 308 | 325 | /** |
| @@ -361,14 +378,14 @@ | ||
| 361 | 378 | */ |
| 362 | 379 | public function handle_import_request() { |
| 363 | 380 | // Verify nonce. |
| 364 | 381 | if ( ! isset( $_POST['top_ten_import_wpp_nonce_field'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['top_ten_import_wpp_nonce_field'] ) ), 'top_ten_import_wpp_nonce' ) ) { |
| 365 | - wp_die( esc_html__( 'Nonce verification failed', 'top-ten' ) ); | |
| 382 | + wp_die( esc_html__( 'Nonce verification failed', 'top-10' ) ); | |
| 366 | 383 | } |
| 367 | 384 | |
| 368 | 385 | // Check user capabilities. |
| 369 | 386 | if ( ! current_user_can( 'manage_options' ) ) { |
| 370 | - wp_die( esc_html__( 'You are not allowed to perform this action', 'top-ten' ) ); | |
| 387 | + wp_die( esc_html__( 'You are not allowed to perform this action', 'top-10' ) ); | |
| 371 | 388 | } |
| 372 | 389 | |
| 373 | 390 | // Get import parameters using our helper method. |
| 374 | 391 | $params = $this->process_import_parameters( $_POST ); |
| @@ -383,9 +400,9 @@ | ||
| 383 | 400 | $sites[] = $params['blog_id']; |
| 384 | 401 | } |
| 385 | 402 | |
| 386 | 403 | if ( empty( $sites ) ) { |
| 387 | - wp_die( esc_html__( 'No sites selected for import.', 'top-ten' ) ); | |
| 404 | + wp_die( esc_html__( 'No sites selected for import.', 'top-10' ) ); | |
| 388 | 405 | } |
| 389 | 406 | |
| 390 | 407 | $import_results = array(); |
| 391 | 408 | |
| @@ -420,12 +437,12 @@ | ||
| 420 | 437 | $message = ''; |
| 421 | 438 | foreach ( $import_results as $blog_id => $result ) { |
| 422 | 439 | $blog_details = get_blog_details( $blog_id ); |
| 423 | 440 | /* translators: 1. Blog ID. */ |
| 424 | - $blog_name = $blog_details ? $blog_details->blogname : sprintf( __( 'Blog ID %d', 'top-ten' ), $blog_id ); | |
| 441 | + $blog_name = $blog_details ? $blog_details->blogname : sprintf( __( 'Blog ID %d', 'top-10' ), $blog_id ); | |
| 425 | 442 | $message .= sprintf( |
| 426 | 443 | /* translators: 1: Site name, 2: Blog ID, 3: Total records processed, 4: Daily records processed, 5: Dry run notice */ |
| 427 | - __( 'Site %1$s (ID: %2$d): Total Counts Processed: %3$d, Daily Records Processed: %4$d%5$s', 'top-ten' ), | |
| 444 | + __( 'Site %1$s (ID: %2$d): Total Counts Processed: %3$d, Daily Records Processed: %4$d%5$s', 'top-10' ), | |
| 428 | 445 | esc_html( $blog_name ), |
| 429 | 446 | $blog_id, |
| 430 | 447 | $result['total_counts'] ?? 0, |
| 431 | 448 | $result['daily_counts'] ?? 0, |