/** * Recursively merge a variable number of arrays, using the left array as base, * giving priority to the right array. * * Difference with native array_merge_recursive(): * array_merge_recursive converts values with duplicate keys to arrays rather than * overwriting the value in the first array with the duplicate value in the second array. * * array_merge_recursive_distinct does not change the data types of the values in the arrays. * Matching keys' values in the second array overwrite those in the first array, as is the * case with array_merge. * * Freely based on information found on http://www.php.net/manual/en/function.array-merge-recursive.php * * {@internal Should be moved to a general utility class.}} * * @return array */ public static function array_merge_recursive_distinct() { $arrays = func_get_args(); if ( count( $arrays ) < 2 ) { if ( $arrays === [] ) { return []; } else { return $arrays[0]; } } $merged = array_shift( $arrays ); foreach ( $arrays as $array ) { foreach ( $array as $key => $value ) { if ( is_array( $value ) && ( isset( $merged[ $key ] ) && is_array( $merged[ $key ] ) ) ) { $merged[ $key ] = self::array_merge_recursive_distinct( $merged[ $key ], $value ); } else { $merged[ $key ] = $value; } } unset( $key, $value ); } return $merged; } /** * Counts the total of all the keywords being used for posts except the given one. * * @param string $keyword The keyword to be counted. * @param integer $post_id The id of the post to which the keyword belongs. * * @return array */ public static function keyword_usage( $keyword, $post_id ) { if ( empty( $keyword ) ) { return []; } /** * The indexable repository. * * @var Indexable_Repository */ $repository = YoastSEO()->classes->get( Indexable_Repository::class ); $post_ids = $repository->query() ->select( 'object_id' ) ->where( 'primary_focus_keyword', $keyword ) ->where( 'object_type', 'post' ) ->where_not_equal( 'object_id', $post_id ) ->limit( 2 ) ->find_array(); $callback = function ( $row ) { return (int) $row['object_id']; }; $post_ids = array_map( $callback, $post_ids ); /* * If Yoast SEO Premium is active, get the additional keywords as well. * We only check for the additional keywords if we've not already found two. * In that case there's no use for an additional query as we already know * that the keyword has been used multiple times before. */ if ( YoastSEO()->helpers->product->is_premium() && count( $post_ids ) < 2 ) { $query = [ 'meta_query' => [ [ 'key' => '_yoast_wpseo_focuskeywords', 'value' => sprintf( '"keyword":"%s"', $keyword ), 'compare' => 'LIKE', ], ], 'post__not_in' => [ $post_id ], 'fields' => 'ids', 'post_type' => 'any', /* * We only need to return zero, one or two results: * - Zero: keyword hasn't been used before * - One: Keyword has been used once before * - Two or more: Keyword has been used twice or more before */ 'posts_per_page' => 2, ]; $get_posts = new WP_Query( $query ); $post_ids = array_merge( $post_ids, $get_posts->posts ); } return $post_ids; } }