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

Yoast / wordpress-seo / 5066322038

pending completion
5066322038

push

github

GitHub
Merge pull request #20316 from Yoast/JRF/ghactions-run-more-selectively

2550 of 29012 relevant lines covered (8.79%)

0.32 hits per line

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

0.0
/src/integrations/admin/indexing-notification-integration.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations\Admin;
4

5
use WPSEO_Addon_Manager;
6
use Yoast\WP\SEO\Conditionals\Admin_Conditional;
7
use Yoast\WP\SEO\Conditionals\Not_Admin_Ajax_Conditional;
8
use Yoast\WP\SEO\Conditionals\User_Can_Manage_Wpseo_Options_Conditional;
9
use Yoast\WP\SEO\Config\Indexing_Reasons;
10
use Yoast\WP\SEO\Helpers\Current_Page_Helper;
11
use Yoast\WP\SEO\Helpers\Environment_Helper;
12
use Yoast\WP\SEO\Helpers\Indexing_Helper;
13
use Yoast\WP\SEO\Helpers\Notification_Helper;
14
use Yoast\WP\SEO\Helpers\Product_Helper;
15
use Yoast\WP\SEO\Helpers\Short_Link_Helper;
16
use Yoast\WP\SEO\Integrations\Integration_Interface;
17
use Yoast\WP\SEO\Presenters\Admin\Indexing_Failed_Notification_Presenter;
18
use Yoast\WP\SEO\Presenters\Admin\Indexing_Notification_Presenter;
19
use Yoast_Notification;
20
use Yoast_Notification_Center;
21

22
/**
23
 * Class Indexing_Notification_Integration.
24
 *
25
 * @package Yoast\WP\SEO\Integrations\Admin
26
 */
27
class Indexing_Notification_Integration implements Integration_Interface {
28

29
        /**
30
         * The notification ID.
31
         */
32
        const NOTIFICATION_ID = 'wpseo-reindex';
33

34
        /**
35
         * The Yoast notification center.
36
         *
37
         * @var Yoast_Notification_Center
38
         */
39
        protected $notification_center;
40

41
        /**
42
         * The product helper.
43
         *
44
         * @var Product_Helper
45
         */
46
        protected $product_helper;
47

48
        /**
49
         * The current page helper.
50
         *
51
         * @var Current_Page_Helper
52
         */
53
        protected $page_helper;
54

55
        /**
56
         * The short link helper.
57
         *
58
         * @var Short_Link_Helper
59
         */
60
        protected $short_link_helper;
61

62
        /**
63
         * The notification helper.
64
         *
65
         * @var Notification_Helper
66
         */
67
        protected $notification_helper;
68

69
        /**
70
         * The indexing helper.
71
         *
72
         * @var Indexing_Helper
73
         */
74
        protected $indexing_helper;
75

76
        /**
77
         * The Addon Manager.
78
         *
79
         * @var WPSEO_Addon_Manager
80
         */
81
        protected $addon_manager;
82

83
        /**
84
         * The Environment Helper.
85
         *
86
         * @var Environment_Helper
87
         */
88
        protected $environment_helper;
89

90
        /**
91
         * Indexing_Notification_Integration constructor.
92
         *
93
         * @param Yoast_Notification_Center $notification_center The notification center.
94
         * @param Product_Helper            $product_helper      The product helper.
95
         * @param Current_Page_Helper       $page_helper         The current page helper.
96
         * @param Short_Link_Helper         $short_link_helper   The short link helper.
97
         * @param Notification_Helper       $notification_helper The notification helper.
98
         * @param Indexing_Helper           $indexing_helper     The indexing helper.
99
         * @param WPSEO_Addon_Manager       $addon_manager       The addon manager.
100
         * @param Environment_Helper        $environment_helper  The environment helper.
101
         */
102
        public function __construct(
103
                Yoast_Notification_Center $notification_center,
104
                Product_Helper $product_helper,
105
                Current_Page_Helper $page_helper,
106
                Short_Link_Helper $short_link_helper,
107
                Notification_Helper $notification_helper,
108
                Indexing_Helper $indexing_helper,
109
                WPSEO_Addon_Manager $addon_manager,
110
                Environment_Helper $environment_helper
111
        ) {
112
                $this->notification_center = $notification_center;
×
113
                $this->product_helper      = $product_helper;
×
114
                $this->page_helper         = $page_helper;
×
115
                $this->short_link_helper   = $short_link_helper;
×
116
                $this->notification_helper = $notification_helper;
×
117
                $this->indexing_helper     = $indexing_helper;
×
118
                $this->addon_manager       = $addon_manager;
×
119
                $this->environment_helper  = $environment_helper;
×
120
        }
121

122
        /**
123
         * Initializes the integration.
124
         *
125
         * Adds hooks and jobs to cleanup or add the notification when necessary.
126
         *
127
         * @return void
128
         */
129
        public function register_hooks() {
130
                if ( $this->page_helper->get_current_yoast_seo_page() === 'wpseo_dashboard' ) {
×
131
                        \add_action( 'admin_init', [ $this, 'maybe_cleanup_notification' ] );
×
132
                }
133

134
                if ( $this->indexing_helper->has_reason() ) {
×
135
                        \add_action( 'admin_init', [ $this, 'maybe_create_notification' ] );
×
136
                }
137

138
                \add_action( self::NOTIFICATION_ID, [ $this, 'maybe_create_notification' ] );
×
139
        }
140

141
        /**
142
         * Returns the conditionals based on which this loadable should be active.
143
         *
144
         * @return array The conditionals.
145
         */
146
        public static function get_conditionals() {
147
                return [
×
148
                        Admin_Conditional::class,
×
149
                        Not_Admin_Ajax_Conditional::class,
×
150
                        User_Can_Manage_Wpseo_Options_Conditional::class,
×
151
                ];
×
152
        }
153

154
        /**
155
         * Checks whether the notification should be shown and adds
156
         * it to the notification center if this is the case.
157
         */
158
        public function maybe_create_notification() {
159
                if ( ! $this->should_show_notification() ) {
×
160
                        return;
×
161
                }
162

163
                if ( ! $this->notification_center->get_notification_by_id( self::NOTIFICATION_ID ) ) {
×
164
                        $notification = $this->notification();
×
165
                        $this->notification_helper->restore_notification( $notification );
×
166
                        $this->notification_center->add_notification( $notification );
×
167
                }
168
        }
169

170
        /**
171
         * Checks whether the notification should not be shown anymore and removes
172
         * it from the notification center if this is the case.
173
         */
174
        public function maybe_cleanup_notification() {
175
                $notification = $this->notification_center->get_notification_by_id( self::NOTIFICATION_ID );
×
176

177
                if ( $notification === null ) {
×
178
                        return;
×
179
                }
180

181
                if ( $this->should_show_notification() ) {
×
182
                        return;
×
183
                }
184

185
                $this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID );
×
186
        }
187

188
        /**
189
         * Checks whether the notification should be shown.
190
         *
191
         * @return bool If the notification should be shown.
192
         */
193
        protected function should_show_notification() {
194
                if ( ! $this->environment_helper->is_production_mode() ) {
×
195
                        return false;
×
196
                }
197
                // Don't show a notification if the indexing has already been started earlier.
198
                if ( $this->indexing_helper->get_started() > 0 ) {
×
199
                        return false;
×
200
                }
201

202
                // We're about to perform expensive queries, let's inform.
203
                \add_filter( 'wpseo_unindexed_count_queries_ran', '__return_true' );
×
204

205
                // Never show a notification when nothing should be indexed.
206
                return $this->indexing_helper->get_limited_filtered_unindexed_count( 1 ) > 0;
×
207
        }
208

209
        /**
210
         * Returns an instance of the notification.
211
         *
212
         * @return Yoast_Notification The notification to show.
213
         */
214
        protected function notification() {
215
                $reason = $this->indexing_helper->get_reason();
×
216

217
                $presenter = $this->get_presenter( $reason );
×
218

219
                return new Yoast_Notification(
×
220
                        $presenter,
×
221
                        [
×
222
                                'type'         => Yoast_Notification::WARNING,
×
223
                                'id'           => self::NOTIFICATION_ID,
×
224
                                'capabilities' => 'wpseo_manage_options',
×
225
                                'priority'     => 0.8,
×
226
                        ]
×
227
                );
×
228
        }
229

230
        /**
231
         * Gets the presenter to use to show the notification.
232
         *
233
         * @param string $reason The reason for the notification.
234
         *
235
         * @return Indexing_Failed_Notification_Presenter|Indexing_Notification_Presenter
236
         */
237
        protected function get_presenter( $reason ) {
238
                if ( $reason === Indexing_Reasons::REASON_INDEXING_FAILED ) {
×
239
                        $presenter = new Indexing_Failed_Notification_Presenter( $this->product_helper, $this->short_link_helper, $this->addon_manager );
×
240
                }
241
                else {
242
                        $total_unindexed = $this->indexing_helper->get_filtered_unindexed_count();
×
243
                        $presenter       = new Indexing_Notification_Presenter( $this->short_link_helper, $total_unindexed, $reason );
×
244
                }
245

246
                return $presenter;
×
247
        }
248
}
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