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

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

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

66.67
/src/integrations/primary-category.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations;
4

5
use stdClass;
6
use WP_Error;
7
use WP_Post;
8
use WPSEO_Primary_Term;
9
use Yoast\WP\SEO\Conditionals\Primary_Category_Conditional;
10

11
/**
12
 * Adds customizations to the front end for the primary category.
13
 */
14
class Primary_Category implements Integration_Interface {
15

16
        /**
17
         * Returns the conditionals based on which this loadable should be active.
18
         *
19
         * In this case only when on the frontend, the post overview, post edit or new post admin page.
20
         *
21
         * @return array The conditionals.
22
         */
23
        public static function get_conditionals() {
×
24
                return [ Primary_Category_Conditional::class ];
×
25
        }
26

27
        /**
28
         * Registers a filter to change a post's primary category.
29
         *
30
         * @return void
31
         */
32
        public function register_hooks() {
×
33
                \add_filter( 'post_link_category', [ $this, 'post_link_category' ], 10, 3 );
×
34
        }
35

36
        /**
37
         * Filters post_link_category to change the category to the chosen category by the user.
38
         *
39
         * @param stdClass     $category   The category that is now used for the post link.
40
         * @param array|null   $categories This parameter is not used.
41
         * @param WP_Post|null $post       The post in question.
42
         *
43
         * @return array|object|WP_Error|null The category we want to use for the post link.
44
         */
45
        public function post_link_category( $category, $categories = null, $post = null ) {
8✔
46
                $post = \get_post( $post );
8✔
47
                if ( $post === null ) {
8✔
48
                        return $category;
2✔
49
                }
50

51
                $primary_category = $this->get_primary_category( $post );
6✔
52
                if ( $primary_category !== false && $primary_category !== $category->cat_ID ) {
6✔
53
                        $category = \get_category( $primary_category );
2✔
54
                }
55

56
                return $category;
6✔
57
        }
58

59
        /**
60
         * Get the id of the primary category.
61
         *
62
         * @codeCoverageIgnore It justs wraps a dependency.
63
         *
64
         * @param WP_Post $post The post in question.
65
         *
66
         * @return int Primary category id.
67
         */
68
        protected function get_primary_category( $post ) {
69
                $primary_term = new WPSEO_Primary_Term( 'category', $post->ID );
70

71
                return $primary_term->get_primary_term();
72
        }
73
}
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

© 2025 Coveralls, Inc