From 2ab8175f147c41253b0cadda4d6efacd87795d07 Mon Sep 17 00:00:00 2001 From: Robin Cornett Date: Wed, 17 Aug 2016 09:49:13 -0400 Subject: [PATCH] Check term image check on settings page Query db directly rather than cycling through all taxonomies/terms in order to reduce load on server. --- ...s-displayfeaturedimagegenesis-settings.php | 51 +++++++++++++------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/includes/class-displayfeaturedimagegenesis-settings.php b/includes/class-displayfeaturedimagegenesis-settings.php index 3010d94..66788df 100644 --- a/includes/class-displayfeaturedimagegenesis-settings.php +++ b/includes/class-displayfeaturedimagegenesis-settings.php @@ -73,14 +73,16 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen * @since 1.4.0 */ public function do_settings_form() { - if ( $this->terms_need_updating() ) { - $this->update_delete_term_meta(); - } $page_title = get_admin_page_title(); echo '
'; printf( '

%s

', esc_attr( $page_title ) ); if ( $this->terms_need_updating() ) { - $this->term_meta_notice(); + if ( $this->check_database() ) { + $this->update_delete_term_meta(); + $this->term_meta_notice(); + } else { + update_option( 'displayfeaturedimagegenesis_updatedterms', true ); + } } $active_tab = $this->get_active_tab(); echo $this->do_tabs( $active_tab ); @@ -574,14 +576,17 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen if ( 'appearance_page_displayfeaturedimagegenesis' !== $screen->id ) { return; } - $terms = $this->get_affected_terms(); - if ( empty( $terms ) ) { + $rows = $this->check_database(); + if ( empty( $rows ) ) { + update_option( 'displayfeaturedimagegenesis_updatedterms', true ); return; } $message = sprintf( '

%s

', __( 'WordPress 4.4 introduces term metadata for categories, tags, and other taxonomies. This is your opportunity to optionally update all impacted terms on your site to use the new metadata.', 'display-featured-image-genesis' ) ); $message .= sprintf( '

%s

', __( 'This will modify your database (potentially many entries at once), so if you\'d rather do it yourself, you can. Here\'s a list of the affected terms:', 'display-featured-image-genesis' ) ); $message .= ''; @@ -598,7 +603,7 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen 'class' => 'button-primary', ), array( - 'value' => __( 'Dismiss (I\'ve got this!)', 'display-featured-image-genesis' ), + 'value' => __( 'Dismiss (Not Recommended)', 'display-featured-image-genesis' ), 'name' => 'displayfeaturedimagegenesis_termmetadismiss', 'class' => 'button-secondary', ), @@ -626,10 +631,10 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen if ( ! check_admin_referer( 'displayfeaturedimagegenesis_metanonce', 'displayfeaturedimagegenesis_metanonce' ) ) { return; } - $terms = $this->get_affected_terms(); + $terms = $this->check_database(); foreach ( $terms as $term ) { - $term_id = $term->term_id; - $option = get_option( "displayfeaturedimagegenesis_{$term_id}" ); + $term_id = str_replace( 'displayfeaturedimagegenesis_', '', $term ); + $option = get_option( $term ); if ( false !== $option ) { $image_id = (int) displayfeaturedimagegenesis_check_image_id( $option['term_image'] ); update_term_meta( $term_id, 'displayfeaturedimagegenesis', $image_id ); @@ -651,21 +656,22 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen * @param array $term_ids empty array * @return array all terms with featured images * @since 2.4.0 + * @deprecated 2.7.0 by check_database() due to heavy load on sites with many terms */ protected function get_affected_terms( $affected_terms = array() ) { - $args = array( + $args = apply_filters( 'displayfeaturedimagegenesis_get_taxonomies', array( 'public' => true, 'show_ui' => true, - ); - $taxonomies = get_taxonomies( $args, 'objects' ); + ) ); + $taxonomies = get_taxonomies( $args ); foreach ( $taxonomies as $tax ) { - $args = array( + $args = array( 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false, ); - $terms = get_terms( $tax->name, $args ); + $terms = get_terms( $tax, $args ); foreach ( $terms as $term ) { $term_id = $term->term_id; $option = get_option( "displayfeaturedimagegenesis_{$term_id}", false ); @@ -690,4 +696,17 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen } return false; } + + /** + * Check the database for term images stored as options. + * @return array|bool + * @since 2.7.0 + */ + protected function check_database() { + global $wpdb; + + $query = $wpdb->get_col( "select * from $wpdb->options where option_name like 'displayfeaturedimagegenesis_%' and option_name != 'displayfeaturedimagegenesis_updatedterms'", 1 ); + + return ! empty( $query ) ? $query : false; + } }