e. * @return array The overview data for the given post type. */ public function getPostTypeOverview( $postType ) { $overviewData = aioseo()->core->cache->get( $postType . '_overview_data' ); if ( null !== $overviewData ) { return $overviewData; } $eligiblePostTypes = aioseo()->helpers->getTruSeoEligiblePostTypes(); if ( ! in_array( $postType, $eligiblePostTypes, true ) ) { return [ 'total' => 0, 'withoutFocusKeyphrase' => 0, 'needsImprovement' => 0, 'okay' => 0, 'good' => 0 ]; } $specialPageIds = aioseo()->helpers->getSpecialPageIds(); $implodedPageIdPlaceholders = array_fill( 0, count( $specialPageIds ), '%d' ); $implodedPageIdPlaceholders = implode( ', ', $implodedPageIdPlaceholders ); global $wpdb; $overviewData = $wpdb->get_row( // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber $wpdb->prepare( "SELECT COUNT(*) as total, COALESCE( SUM(CASE WHEN ap.keyphrases = '' OR ap.keyphrases IS NULL OR ap.keyphrases LIKE %s THEN 1 ELSE 0 END), 0) as withoutFocusKeyphrase, COALESCE( SUM(CASE WHEN ap.seo_score < 50 AND NOT (ap.keyphrases = '' OR ap.keyphrases IS NULL OR ap.keyphrases LIKE %s) THEN 1 ELSE 0 END), 0) as needsImprovement, COALESCE( SUM(CASE WHEN ap.seo_score BETWEEN 50 AND 79 AND NOT (ap.keyphrases = '' OR ap.keyphrases IS NULL OR ap.keyphrases LIKE %s) THEN 1 ELSE 0 END), 0) as okay, COALESCE( SUM(CASE WHEN ap.seo_score >= 80 AND NOT (ap.keyphrases = '' OR ap.keyphrases IS NULL OR ap.keyphrases LIKE %s) THEN 1 ELSE 0 END), 0) as good FROM {$wpdb->posts} as p LEFT JOIN {$wpdb->prefix}aioseo_posts as ap ON ap.post_id = p.ID WHERE p.post_status = 'publish' AND p.post_type = %s AND p.ID NOT IN ( $implodedPageIdPlaceholders )", // phpcs:enable '{"focus":{"keyphrase":""%', '{"focus":{"keyphrase":""%', '{"focus":{"keyphrase":""%', '{"focus":{"keyphrase":""%', $postType, ...array_values( $specialPageIds ) ), ARRAY_A ); // Ensure sure all the values are integers. foreach ( $overviewData as $key => $value ) { $overviewData[ $key ] = (int) $value; } // Give me the raw SQL of the query. aioseo()->core->cache->update( $postType . '_overview_data', $overviewData, HOUR_IN_SECONDS ); return $overviewData; } /** * Change the JOIN and WHERE clause to filter just the posts we need to show depending on the query string. * * @since 4.2.0 * * @param array $clauses Associative array of the clauses for the query. * @param \WP_Query $query The WP_Query instance (passed by reference). * @return array The clauses array updated. */ public function changeClausesToFilterPosts( $clauses, $query = null ) { if ( ! is_admin() || ! $query->is_main_query() ) { return $clauses; } $filter = filter_input( INPUT_GET, 'aioseo-filter' ); if ( empty( $filter ) ) { return $clauses; } $whereClause = ''; $noKeyphrasesClause = "(aioseo_p.keyphrases = '' OR aioseo_p.keyphrases IS NULL OR aioseo_p.keyphrases LIKE '{\"focus\":{\"keyphrase\":\"\"%')"; switch ( $filter ) { case 'withoutFocusKeyphrase': $whereClause = " AND $noKeyphrasesClause "; break; case 'needsImprovement': $whereClause = " AND aioseo_p.seo_score < 50 AND NOT $noKeyphrasesClause "; break; case 'okay': $whereClause = " AND aioseo_p.seo_score BETWEEN 50 AND 80 AND NOT $noKeyphrasesClause "; break; case 'good': $whereClause = " AND aioseo_p.seo_score > 80 AND NOT $noKeyphrasesClause "; break; } $prefix = aioseo()->core->db->prefix; $postsTable = aioseo()->core->db->db->posts; $clauses['join'] .= " LEFT JOIN {$prefix}aioseo_posts AS aioseo_p ON ({$postsTable}.ID = aioseo_p.post_id) "; $clauses['where'] .= $whereClause; add_action( 'wp', [ $this, 'filterPostsAfterChangingClauses' ] ); return $clauses; } /** * Filter the posts array to remove the ones that are not eligible for page analysis. * Hooked into `wp` action hook. * * @since 4.7.1 * * @return void */ public function filterPostsAfterChangingClauses() { remove_action( 'wp', [ $this, 'filterPostsAfterChangingClauses' ] ); // phpcs:disable Squiz.NamingConventions.ValidVariableName global $wp_query; if ( ! empty( $wp_query->posts ) && is_array( $wp_query->posts ) ) { $wp_query->posts = array_filter( $wp_query->posts, function ( $post ) { return aioseo()->helpers->isTruSeoEligible( $post->ID ); } ); // Update `post_count` for pagination. if ( isset( $wp_query->post_count ) ) { $wp_query->post_count = count( $wp_query->posts ); } } // phpcs:enable Squiz.NamingConventions.ValidVariableName } }