• 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

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

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

5
use WPSEO_Addon_Manager;
6
use WPSEO_Admin_Asset_Manager;
7
use Yoast\WP\SEO\Conditionals\Admin_Conditional;
8
use Yoast\WP\SEO\Helpers\Options_Helper;
9
use Yoast\WP\SEO\Helpers\Product_Helper;
10
use Yoast\WP\SEO\Integrations\Integration_Interface;
11
use Yoast\WP\SEO\Presenters\Admin\Notice_Presenter;
12

13
/**
14
 * WorkoutsIntegration class
15
 */
16
class Workouts_Integration implements Integration_Interface {
17

18
        /**
19
         * The admin asset manager.
20
         *
21
         * @var WPSEO_Admin_Asset_Manager
22
         */
23
        private $admin_asset_manager;
24

25
        /**
26
         * The addon manager.
27
         *
28
         * @var WPSEO_Addon_Manager
29
         */
30
        private $addon_manager;
31

32
        /**
33
         * The options helper.
34
         *
35
         * @var Options_Helper
36
         */
37
        private $options_helper;
38

39
        /**
40
         * The product helper.
41
         *
42
         * @var Product_Helper
43
         */
44
        private $product_helper;
45

46
        /**
47
         * {@inheritDoc}
48
         */
49
        public static function get_conditionals() {
×
50
                return [ Admin_Conditional::class ];
×
51
        }
52

53
        /**
54
         * Workouts_Integration constructor.
55
         *
56
         * @param WPSEO_Addon_Manager       $addon_manager       The addon manager.
57
         * @param WPSEO_Admin_Asset_Manager $admin_asset_manager The admin asset manager.
58
         * @param Options_Helper            $options_helper      The options helper.
59
         * @param Product_Helper            $product_helper      The product helper.
60
         */
61
        public function __construct(
×
62
                WPSEO_Addon_Manager $addon_manager,
63
                WPSEO_Admin_Asset_Manager $admin_asset_manager,
64
                Options_Helper $options_helper,
65
                Product_Helper $product_helper
66
        ) {
67
                $this->addon_manager       = $addon_manager;
×
68
                $this->admin_asset_manager = $admin_asset_manager;
×
69
                $this->options_helper      = $options_helper;
×
70
                $this->product_helper      = $product_helper;
×
71
        }
72

73
        /**
74
         * {@inheritDoc}
75
         */
76
        public function register_hooks() {
×
77
                \add_filter( 'wpseo_submenu_pages', [ $this, 'add_submenu_page' ], 8 );
×
78
                \add_filter( 'wpseo_submenu_pages', [ $this, 'remove_old_submenu_page' ], 10 );
×
79
                \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ], 11 );
×
80
        }
81

82
        /**
83
         * Adds the workouts submenu page.
84
         *
85
         * @param array $submenu_pages The Yoast SEO submenu pages.
86
         *
87
         * @return array The filtered submenu pages.
88
         */
89
        public function add_submenu_page( $submenu_pages ) {
×
90
                $submenu_pages[] = [
×
91
                        'wpseo_dashboard',
×
92
                        '',
×
93
                        \__( 'Workouts', 'wordpress-seo' ) . ' <span class="yoast-badge yoast-premium-badge"></span>',
×
94
                        'edit_others_posts',
×
95
                        'wpseo_workouts',
×
96
                        [ $this, 'render_target' ],
×
97
                ];
98

99
                return $submenu_pages;
×
100
        }
101

102
        /**
103
         * Removes the workouts submenu page from older Premium versions
104
         *
105
         * @param array $submenu_pages The Yoast SEO submenu pages.
106
         *
107
         * @return array The filtered submenu pages.
108
         */
109
        public function remove_old_submenu_page( $submenu_pages ) {
×
110
                if ( ! $this->should_update_premium() ) {
×
111
                        return $submenu_pages;
×
112
                }
113

114
                // Copy only the Workouts page item that comes first in the array.
115
                $result_submenu_pages      = [];
×
116
                $workouts_page_encountered = false;
×
117
                foreach ( $submenu_pages as $item ) {
×
118
                        if ( $item[4] !== 'wpseo_workouts' || ! $workouts_page_encountered ) {
×
119
                                $result_submenu_pages[] = $item;
×
120
                        }
121
                        if ( $item[4] === 'wpseo_workouts' ) {
×
122
                                $workouts_page_encountered = true;
×
123
                        }
124
                }
125

126
                return $result_submenu_pages;
×
127
        }
128

129
        /**
130
         * Enqueue the workouts app.
131
         */
132
        public function enqueue_assets() {
×
133
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Date is not processed or saved.
134
                if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'wpseo_workouts' ) {
×
135
                        return;
×
136
                }
137

138
                if ( $this->should_update_premium() ) {
×
139
                        \wp_dequeue_script( 'yoast-seo-premium-workouts' );
×
140
                }
141

142
                $this->admin_asset_manager->enqueue_style( 'workouts' );
×
143

144
                $workouts_option = $this->get_workouts_option();
×
145

146
                $this->admin_asset_manager->enqueue_script( 'workouts' );
×
147
                $this->admin_asset_manager->localize_script(
×
148
                        'workouts',
×
149
                        'wpseoWorkoutsData',
×
150
                        [
151
                                'workouts'                  => $workouts_option,
×
152
                                'homeUrl'                   => \home_url(),
×
153
                                'pluginUrl'                 => \esc_url( \plugins_url( '', \WPSEO_FILE ) ),
×
154
                                'toolsPageUrl'              => \esc_url( \admin_url( 'admin.php?page=wpseo_tools' ) ),
×
155
                                'usersPageUrl'              => \esc_url( \admin_url( 'users.php' ) ),
×
156
                                'firstTimeConfigurationUrl' => \esc_url( \admin_url( 'admin.php?page=wpseo_dashboard#top#first-time-configuration' ) ),
×
157
                                'isPremium'                 => $this->product_helper->is_premium(),
×
158
                                'shouldUpdatePremium'       => $this->should_update_premium(),
×
159
                                'upsellText'                => $this->get_upsell_text(),
×
160
                                'upsellLink'                => $this->get_upsell_link(),
×
161
                        ]
162
                );
163
        }
164

165
        /**
166
         * Renders the target for the React to mount to.
167
         */
168
        public function render_target() {
×
169
                if ( $this->should_update_premium() ) {
×
170
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in get_update_premium_notice.
171
                        echo $this->get_update_premium_notice();
×
172
                }
173

174
                echo '<div id="wpseo-workouts-container-free" class="yoast"></div>';
×
175
        }
176

177
        /**
178
         * Gets the workouts option.
179
         *
180
         * @return mixed|null Returns workouts option if found, null if not.
181
         */
182
        private function get_workouts_option() {
×
183
                $workouts_option = $this->options_helper->get( 'workouts_data' );
×
184

185
                // This filter is documented in src/routes/workouts-route.php.
186
                return \apply_filters( 'Yoast\WP\SEO\workouts_options', $workouts_option );
×
187
        }
188

189
        /**
190
         * Returns the notification to show when Premium needs to be updated.
191
         *
192
         * @return string The notification to update Premium.
193
         */
194
        private function get_update_premium_notice() {
×
195
                $url = $this->get_upsell_link();
×
196

197
                if ( $this->has_premium_subscription_expired() ) {
×
198
                        /* translators: %s: expands to 'Yoast SEO Premium'. */
199
                        $title = \sprintf( \__( 'Renew your subscription of %s', 'wordpress-seo' ), 'Yoast SEO Premium' );
×
200
                        $copy  = \sprintf(
×
201
                                /* translators: %s: expands to 'Yoast SEO Premium'. */
202
                                \esc_html__(
×
203
                                        'Accessing the latest workouts requires an updated version of %s (at least 17.7), but it looks like your subscription has expired. Please renew your subscription to update and gain access to all the latest features.',
×
204
                                        'wordpress-seo'
×
205
                                ),
206
                                'Yoast SEO Premium'
×
207
                        );
208
                        $button = '<a class="yoast-button yoast-button-upsell yoast-button--small" href="' . \esc_url( $url ) . '" target="_blank">'
×
209
                                        . \esc_html__( 'Renew your subscription', 'wordpress-seo' )
×
210
                                        /* translators: Hidden accessibility text. */
211
                                        . '<span class="screen-reader-text">' . \__( '(Opens in a new browser tab)', 'wordpress-seo' ) . '</span>'
×
212
                                        . '<span aria-hidden="true" class="yoast-button-upsell__caret"></span>'
×
213
                                        . '</a>';
×
214
                }
215
                elseif ( $this->has_premium_subscription_activated() ) {
×
216
                        /* translators: %s: expands to 'Yoast SEO Premium'. */
217
                        $title = \sprintf( \__( 'Update to the latest version of %s', 'wordpress-seo' ), 'Yoast SEO Premium' );
×
218
                        $copy  = \sprintf(
×
219
                                /* translators: 1: expands to 'Yoast SEO Premium', 2: Link start tag to the page to update Premium, 3: Link closing tag. */
220
                                \esc_html__( 'It looks like you\'re running an outdated version of %1$s, please %2$supdate to the latest version (at least 17.7)%3$s to gain access to our updated workouts section.', 'wordpress-seo' ),
×
221
                                'Yoast SEO Premium',
×
222
                                '<a href="' . \esc_url( $url ) . '">',
×
223
                                '</a>'
×
224
                        );
225
                        $button = null;
×
226
                }
227
                else {
228
                        /* translators: %s: expands to 'Yoast SEO Premium'. */
229
                        $title      = \sprintf( \__( 'Activate your subscription of %s', 'wordpress-seo' ), 'Yoast SEO Premium' );
×
230
                        $url_button = 'https://yoa.st/workouts-activate-notice-help';
×
231
                        $copy       = \sprintf(
×
232
                                /* translators: 1: expands to 'Yoast SEO Premium', 2: Link start tag to the page to update Premium, 3: Link closing tag. */
233
                                \esc_html__( 'It looks like you’re running an outdated and unactivated version of %1$s, please activate your subscription in %2$sMyYoast%3$s and update to the latest version (at least 17.7) to gain access to our updated workouts section.', 'wordpress-seo' ),
×
234
                                'Yoast SEO Premium',
×
235
                                '<a href="' . \esc_url( $url ) . '">',
×
236
                                '</a>'
×
237
                        );
238
                        $button = '<a class="yoast-button yoast-button--primary yoast-button--small" href="' . \esc_url( $url_button ) . '" target="_blank">'
×
239
                                        . \esc_html__( 'Get help activating your subscription', 'wordpress-seo' )
×
240
                                        /* translators: Hidden accessibility text. */
241
                                        . '<span class="screen-reader-text">' . \__( '(Opens in a new browser tab)', 'wordpress-seo' ) . '</span>'
×
242
                                        . '</a>';
×
243
                }
244

245
                $notice = new Notice_Presenter(
×
246
                        $title,
×
247
                        $copy,
×
248
                        null,
×
249
                        $button
×
250
                );
251

252
                return $notice->present();
×
253
        }
254

255
        /**
256
         * Check whether Premium should be updated.
257
         *
258
         * @return bool Returns true when Premium is enabled and the version is below 17.7.
259
         */
260
        private function should_update_premium() {
×
261
                $premium_version = $this->product_helper->get_premium_version();
×
262
                return $premium_version !== null && \version_compare( $premium_version, '17.7-RC1', '<' );
×
263
        }
264

265
        /**
266
         * Check whether the Premium subscription has expired.
267
         *
268
         * @return bool Returns true when Premium subscription has expired.
269
         */
270
        private function has_premium_subscription_expired() {
×
271
                $subscription = $this->addon_manager->get_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG );
×
272

273
                return ( isset( $subscription->expiry_date ) && ( \strtotime( $subscription->expiry_date ) - \time() ) < 0 );
×
274
        }
275

276
        /**
277
         * Check whether the Premium subscription is activated.
278
         *
279
         * @return bool Returns true when Premium subscription is activated.
280
         */
281
        private function has_premium_subscription_activated() {
×
282
                return $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG );
×
283
        }
284

285
        /**
286
         * Returns the upsell/update copy to show in the card buttons.
287
         *
288
         * @return string Returns a string with the upsell/update copy for the card buttons.
289
         */
290
        private function get_upsell_text() {
×
291
                if ( ! $this->product_helper->is_premium() || ! $this->should_update_premium() ) {
×
292
                        // Use the default defined in the component.
293
                        return '';
×
294
                }
295
                if ( $this->has_premium_subscription_expired() ) {
×
296
                        return \sprintf(
×
297
                                /* translators: %s: expands to 'Yoast SEO Premium'. */
298
                                \__( 'Renew %s', 'wordpress-seo' ),
×
299
                                'Yoast SEO Premium'
×
300
                        );
301
                }
302
                if ( $this->has_premium_subscription_activated() ) {
×
303
                        return \sprintf(
×
304
                                /* translators: %s: expands to 'Yoast SEO Premium'. */
305
                                \__( 'Update %s', 'wordpress-seo' ),
×
306
                                'Yoast SEO Premium'
×
307
                        );
308
                }
309
                return \sprintf(
×
310
                        /* translators: %s: expands to 'Yoast SEO Premium'. */
311
                        \__( 'Activate %s', 'wordpress-seo' ),
×
312
                        'Yoast SEO Premium'
×
313
                );
314
        }
315

316
        /**
317
         * Returns the upsell/update link to show in the card buttons.
318
         *
319
         * @return string Returns a string with the upsell/update link for the card buttons.
320
         */
321
        private function get_upsell_link() {
×
322
                if ( ! $this->product_helper->is_premium() || ! $this->should_update_premium() ) {
×
323
                        // Use the default defined in the component.
324
                        return '';
×
325
                }
326
                if ( $this->has_premium_subscription_expired() ) {
×
327
                        return 'https://yoa.st/workout-renew-notice';
×
328
                }
329
                if ( $this->has_premium_subscription_activated() ) {
×
330
                        return \wp_nonce_url( \self_admin_url( 'update.php?action=upgrade-plugin&plugin=wordpress-seo-premium/wp-seo-premium.php' ), 'upgrade-plugin_wordpress-seo-premium/wp-seo-premium.php' );
×
331
                }
332
                return 'https://yoa.st/workouts-activate-notice-myyoast';
×
333
        }
334
}
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