• 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

89.71
/src/integrations/watchers/addon-update-watcher.php
1
<?php
2

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

5
use Yoast\WP\SEO\Conditionals\Admin_Conditional;
6
use Yoast\WP\SEO\Integrations\Integration_Interface;
7

8
/**
9
 * Enables Yoast add-on auto updates when Yoast SEO is enabled and the other way around.
10
 *
11
 * Also removes the auto-update toggles from the Yoast SEO add-ons.
12
 */
13
class Addon_Update_Watcher implements Integration_Interface {
14

15
        /**
16
         * ID string used by WordPress to identify the free plugin.
17
         *
18
         * @var string
19
         */
20
        const WPSEO_FREE_PLUGIN_ID = 'wordpress-seo/wp-seo.php';
21

22
        /**
23
         * A list of Yoast add-on identifiers.
24
         *
25
         * @var string[]
26
         */
27
        const ADD_ON_PLUGIN_FILES = [
28
                'wordpress-seo-premium/wp-seo-premium.php',
29
                'wpseo-video/video-seo.php',
30
                'wpseo-local/local-seo.php', // When installing Local through a released zip, the path is different from the path on a dev environment.
31
                'wpseo-woocommerce/wpseo-woocommerce.php',
32
                'wpseo-news/wpseo-news.php',
33
                'acf-content-analysis-for-yoast-seo/yoast-acf-analysis.php', // When installing ACF for Yoast through a released zip, the path is different from the path on a dev environment.
34
        ];
35

36
        /**
37
         * Registers the hooks.
38
         */
39
        public function register_hooks() {
2✔
40
                \add_action( 'add_site_option_auto_update_plugins', [ $this, 'call_toggle_auto_updates_with_empty_array' ], 10, 2 );
2✔
41
                \add_action( 'update_site_option_auto_update_plugins', [ $this, 'toggle_auto_updates_for_add_ons' ], 10, 3 );
2✔
42
                \add_filter( 'plugin_auto_update_setting_html', [ $this, 'replace_auto_update_toggles_of_addons' ], 10, 2 );
2✔
43
                \add_action( 'activated_plugin', [ $this, 'maybe_toggle_auto_updates_for_new_install' ] );
2✔
44
        }
1✔
45

46
        /**
47
         * Returns the conditionals based on which this loadable should be active.
48
         *
49
         * @return string[] The conditionals.
50
         */
51
        public static function get_conditionals() {
2✔
52
                return [ Admin_Conditional::class ];
2✔
53
        }
54

55
        /**
56
         * Replaces the auto-update toggle links for the Yoast add-ons
57
         * with a text explaining that toggling the Yoast SEO auto-update setting
58
         * automatically toggles the one for the setting for the add-ons as well.
59
         *
60
         * @param string $old_html The old HTML.
61
         * @param string $plugin   The plugin.
62
         *
63
         * @return string The new HTML, with the auto-update toggle link replaced.
64
         */
65
        public function replace_auto_update_toggles_of_addons( $old_html, $plugin ) {
48✔
66
                if ( ! \is_string( $old_html ) ) {
48✔
67
                        return $old_html;
2✔
68
                }
69

70
                $not_a_yoast_addon = ! \in_array( $plugin, self::ADD_ON_PLUGIN_FILES, true );
46✔
71

72
                if ( $not_a_yoast_addon ) {
46✔
73
                        return $old_html;
2✔
74
                }
75

76
                $auto_updated_plugins = \get_site_option( 'auto_update_plugins' );
44✔
77

78
                if ( $this->are_auto_updates_enabled( self::WPSEO_FREE_PLUGIN_ID, $auto_updated_plugins ) ) {
44✔
79
                        return \sprintf(
10✔
80
                                '<em>%s</em>',
10✔
81
                                \sprintf(
10✔
82
                                /* Translators: %1$s resolves to Yoast SEO. */
83
                                        \esc_html__( 'Auto-updates are enabled based on this setting for %1$s.', 'wordpress-seo' ),
10✔
84
                                        'Yoast SEO'
10✔
85
                                )
5✔
86
                        );
5✔
87
                }
88

89
                return \sprintf(
34✔
90
                        '<em>%s</em>',
34✔
91
                        \sprintf(
34✔
92
                        /* Translators: %1$s resolves to Yoast SEO. */
93
                                \esc_html__( 'Auto-updates are disabled based on this setting for %1$s.', 'wordpress-seo' ),
34✔
94
                                'Yoast SEO'
34✔
95
                        )
17✔
96
                );
17✔
97
        }
98

99
        /**
100
         * Handles the situation where the auto_update_plugins option did not previously exist.
101
         *
102
         * @param string      $option The name of the option that is being created.
103
         * @param array|mixed $value  The new (and first) value of the option that is being created.
104
         */
105
        public function call_toggle_auto_updates_with_empty_array( $option, $value ) {
2✔
106
                if ( $option !== 'auto_update_plugins' ) {
2✔
107
                        return;
×
108
                }
109

110
                $this->toggle_auto_updates_for_add_ons( $option, $value, [] );
2✔
111
        }
1✔
112

113
        /**
114
         * Enables premium auto updates when free are enabled and the other way around.
115
         *
116
         * @param string $option    The name of the option that has been updated.
117
         * @param array  $new_value The new value of the `auto_update_plugins` option.
118
         * @param array  $old_value The old value of the `auto_update_plugins` option.
119
         *
120
         * @return void
121
         */
122
        public function toggle_auto_updates_for_add_ons( $option, $new_value, $old_value ) {
12✔
123
                if ( $option !== 'auto_update_plugins' ) {
12✔
124
                        // If future versions of WordPress change this filter's behavior, our behavior should stay consistent.
125
                        return;
2✔
126
                }
127

128
                if ( ! \is_array( $old_value ) || ! \is_array( $new_value ) ) {
10✔
129
                        return;
4✔
130
                }
131

132
                $auto_updates_are_enabled  = $this->are_auto_updates_enabled( self::WPSEO_FREE_PLUGIN_ID, $new_value );
6✔
133
                $auto_updates_were_enabled = $this->are_auto_updates_enabled( self::WPSEO_FREE_PLUGIN_ID, $old_value );
6✔
134

135
                if ( $auto_updates_are_enabled === $auto_updates_were_enabled ) {
6✔
136
                        // Auto-updates for Yoast SEO have stayed the same, so have neither been enabled or disabled.
137
                        return;
2✔
138
                }
139

140
                $auto_updates_have_been_enabled = $auto_updates_are_enabled && ! $auto_updates_were_enabled;
4✔
141

142
                if ( $auto_updates_have_been_enabled ) {
4✔
143
                        $this->enable_auto_updates_for_addons( $new_value );
2✔
144
                        return;
2✔
145
                }
146
                else {
147
                        $this->disable_auto_updates_for_addons( $new_value );
2✔
148
                        return;
2✔
149
                }
150

151
                if ( ! $auto_updates_are_enabled ) {
152
                        return;
153
                }
154

155
                $auto_updates_have_been_removed = false;
156
                foreach ( self::ADD_ON_PLUGIN_FILES as $addon ) {
157
                        if ( ! $this->are_auto_updates_enabled( $addon, $new_value ) ) {
158
                                $auto_updates_have_been_removed = true;
159
                                break;
160
                        }
161
                }
162

163
                if ( $auto_updates_have_been_removed ) {
164
                        $this->enable_auto_updates_for_addons( $new_value );
165
                }
166
        }
167

168
        /**
169
         * Trigger a change in the auto update detection whenever a new Yoast addon is activated.
170
         *
171
         * @param string $plugin The plugin that is activated.
172
         *
173
         * @return void
174
         */
175
        public function maybe_toggle_auto_updates_for_new_install( $plugin ) {
×
176
                $not_a_yoast_addon = ! \in_array( $plugin, self::ADD_ON_PLUGIN_FILES, true );
×
177

178
                if ( $not_a_yoast_addon ) {
×
179
                        return;
×
180
                }
181

182
                $enabled_auto_updates = \get_site_option( 'auto_update_plugins' );
×
183
                $this->toggle_auto_updates_for_add_ons( 'auto_update_plugins', $enabled_auto_updates, [] );
×
184
        }
185

186
        /**
187
         * Enables auto-updates for all addons.
188
         *
189
         * @param string[] $auto_updated_plugins The current list of auto-updated plugins.
190
         */
191
        protected function enable_auto_updates_for_addons( $auto_updated_plugins ) {
2✔
192
                $plugins = \array_unique( \array_merge( $auto_updated_plugins, self::ADD_ON_PLUGIN_FILES ) );
2✔
193
                \update_site_option( 'auto_update_plugins', $plugins );
2✔
194
        }
1✔
195

196
        /**
197
         * Disables auto-updates for all addons.
198
         *
199
         * @param string[] $auto_updated_plugins The current list of auto-updated plugins.
200
         */
201
        protected function disable_auto_updates_for_addons( $auto_updated_plugins ) {
2✔
202
                $plugins = \array_values( \array_diff( $auto_updated_plugins, self::ADD_ON_PLUGIN_FILES ) );
2✔
203
                \update_site_option( 'auto_update_plugins', $plugins );
2✔
204
        }
1✔
205

206
        /**
207
         * Checks whether auto updates for a plugin are enabled.
208
         *
209
         * @param string $plugin_id            The plugin ID.
210
         * @param array  $auto_updated_plugins The array of auto updated plugins.
211
         *
212
         * @return bool Whether auto updates for a plugin are enabled.
213
         */
214
        protected function are_auto_updates_enabled( $plugin_id, $auto_updated_plugins ) {
36✔
215
                if ( $auto_updated_plugins === false || ! \is_array( $auto_updated_plugins ) ) {
36✔
216
                        return false;
20✔
217
                }
218

219
                return \in_array( $plugin_id, $auto_updated_plugins, true );
16✔
220
        }
221
}
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