• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 6ebbfba6f3acb97e17911989012bcf63226e2de5

25 Apr 2024 10:41AM UTC coverage: 53.487% (+1.1%) from 52.368%
6ebbfba6f3acb97e17911989012bcf63226e2de5

Pull #21339

github

web-flow
Merge 0f9b80d84 into 0bc542ae2
Pull Request #21339: 21318 refactor seo data provision to script data

7508 of 13725 branches covered (54.7%)

Branch coverage included in aggregate %.

182 of 203 new or added lines in 18 files covered. (89.66%)

810 existing lines in 1 file now uncovered.

29462 of 55394 relevant lines covered (53.19%)

40551.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/src/editors/framework/seo/posts/keywords-data-provider.php
1
<?php
2
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong
3
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
4
namespace Yoast\WP\SEO\Editors\Framework\Seo\Posts;
5

6
use WPSEO_Meta;
7
use Yoast\WP\SEO\Editors\Domain\Seo\Keywords;
8
use Yoast\WP\SEO\Editors\Domain\Seo\Seo_Plugin_Data_Interface;
9
use Yoast\WP\SEO\Editors\Framework\Seo\Keywords_Interface;
10
use Yoast\WP\SEO\Helpers\Meta_Helper;
11

12
/**
13
 * Describes if the keyword SEO data.
14
 */
15
class Keywords_Data_Provider extends Abstract_Post_Seo_Data_Provider implements Keywords_Interface {
16

17
        /**
18
         *  The meta helper.
19
         *
20
         * @var Meta_Helper $meta_helper
21
         */
22
        private $meta_helper;
23

24
        /**
25
         * The constructor.
26
         *
27
         * @param Meta_Helper $meta_helper The meta helper.
28
         */
NEW
29
        public function __construct( Meta_Helper $meta_helper ) {
×
NEW
30
                $this->meta_helper = $meta_helper;
×
31
        }
32

33
        /**
34
         * Counts the number of given keywords used for other posts other than the given post_id.
35
         *
36
         * @return array<string> The keyword and the associated posts that use it.
37
         */
38
        public function get_focus_keyword_usage(): array {
4✔
39
                $keyword = $this->meta_helper->get_value( 'focuskw', $this->post->ID );
4✔
40
                $usage   = [ $keyword => $this->get_keyword_usage_for_current_post( $keyword ) ];
4✔
41

42
                /**
43
                 * Allows enhancing the array of posts' that share their focus keywords with the post's related keywords.
44
                 *
45
                 * @param array<string> $usage   The array of posts' ids that share their focus keywords with the post.
46
                 * @param int   $post_id The id of the post we're finding the usage of related keywords for.
47
                 */
48
                return \apply_filters( 'wpseo_posts_for_related_keywords', $usage, $this->post->ID );
4✔
49
        }
50

51
        /**
52
         * Retrieves the post types for the given post IDs.
53
         *
54
         * @param array<string|array<string>> $post_ids_per_keyword An associative array with keywords as keys and an array of post ids where those keywords are used.
55
         * @return array<string|array<string>> The post types for the given post IDs.
56
         */
57
        public function get_post_types_for_all_ids( $post_ids_per_keyword ) {
4✔
58
                $post_type_per_keyword_result = [];
4✔
59
                foreach ( $post_ids_per_keyword as $keyword => $post_ids ) {
4✔
60
                        $post_type_per_keyword_result[ $keyword ] = WPSEO_Meta::post_types_for_ids( $post_ids );
4✔
61
                }
62

63
                return $post_type_per_keyword_result;
4✔
64
        }
65

66
        /**
67
         * Gets the keyword usage for the current post and the specified keyword.
68
         *
69
         * @param string $keyword The keyword to check the usage of.
70
         *
71
         * @return array<string> The post IDs which use the passed keyword.
72
         */
73
        private function get_keyword_usage_for_current_post( $keyword ) {
4✔
74
                return WPSEO_Meta::keyword_usage( $keyword, $this->post->ID );
4✔
75
        }
76

77
        /**
78
         * Method to return the Keyword domain object with SEO data.
79
         *
80
         * @return Seo_Plugin_Data_Interface The specific seo data.
81
         */
82
        public function get_data(): Seo_Plugin_Data_Interface {
4✔
83
                $keyword_usage = $this->get_focus_keyword_usage();
4✔
84
                return new Keywords( $keyword_usage, $this->get_post_types_for_all_ids( $keyword_usage ) );
4✔
85
        }
86
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc