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

Yoast / wordpress-seo / dd6e866a9e6d253114633104d9e3858d807178ba

19 Jun 2024 10:03AM UTC coverage: 48.628% (-4.3%) from 52.936%
dd6e866a9e6d253114633104d9e3858d807178ba

push

github

web-flow
Merge pull request #21431 from Yoast/21429-update-copy-in-the-introduction-and-consent-modals

Updates the copy for the introduction and consent modals

7441 of 13454 branches covered (55.31%)

Branch coverage included in aggregate %.

0 of 3 new or added lines in 2 files covered. (0.0%)

3718 existing lines in 107 files now uncovered.

25100 of 53464 relevant lines covered (46.95%)

62392.47 hits per line

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

37.5
/inc/options/class-wpseo-option-social.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Internals\Options
6
 */
7

8
/**
9
 * Option: wpseo_social.
10
 */
11
class WPSEO_Option_Social extends WPSEO_Option {
12

13
        /**
14
         * Option name.
15
         *
16
         * @var string
17
         */
18
        public $option_name = 'wpseo_social';
19

20
        /**
21
         * Array of defaults for the option.
22
         *
23
         * Shouldn't be requested directly, use $this->get_defaults();
24
         *
25
         * @var array
26
         */
27
        protected $defaults = [
28
                // Form fields.
29
                'facebook_site'         => '', // Text field.
30
                'instagram_url'         => '',
31
                'linkedin_url'          => '',
32
                'myspace_url'           => '',
33
                'og_default_image'      => '', // Text field.
34
                'og_default_image_id'   => '',
35
                'og_frontpage_title'    => '', // Text field.
36
                'og_frontpage_desc'     => '', // Text field.
37
                'og_frontpage_image'    => '', // Text field.
38
                'og_frontpage_image_id' => '',
39
                'opengraph'             => true,
40
                'pinterest_url'         => '',
41
                'pinterestverify'       => '',
42
                'twitter'               => true,
43
                'twitter_site'          => '', // Text field.
44
                'twitter_card_type'     => 'summary_large_image',
45
                'youtube_url'           => '',
46
                'wikipedia_url'         => '',
47
                'other_social_urls'     => [],
48
                'mastodon_url'          => '',
49
        ];
50

51
        /**
52
         * Array of sub-options which should not be overloaded with multi-site defaults.
53
         *
54
         * @var array
55
         */
56
        public $ms_exclude = [
57
                /* Privacy. */
58
                'pinterestverify',
59
        ];
60

61
        /**
62
         * Array of allowed twitter card types.
63
         *
64
         * While we only have the options summary and summary_large_image in the
65
         * interface now, we might change that at some point.
66
         *
67
         * {@internal Uncomment any of these to allow them in validation *and* automatically
68
         *            add them as a choice in the options page.}}
69
         *
70
         * @var array
71
         */
72
        public static $twitter_card_types = [
73
                'summary_large_image' => '',
74
                // 'summary'             => '',
75
                // 'photo'               => '',
76
                // 'gallery'             => '',
77
                // 'app'                 => '',
78
                // 'player'              => '',
79
                // 'product'             => '',
80
        ];
81

82
        /**
83
         * Add the actions and filters for the option.
84
         */
85
        protected function __construct() {
×
86
                parent::__construct();
×
87

88
                add_filter( 'admin_title', [ 'Yoast_Input_Validation', 'add_yoast_admin_document_title_errors' ] );
×
89
        }
90

91
        /**
92
         * Get the singleton instance of this class.
93
         *
94
         * @return object
95
         */
96
        public static function get_instance() {
×
97
                if ( ! ( self::$instance instanceof self ) ) {
×
98
                        self::$instance = new self();
×
99
                }
100

101
                return self::$instance;
×
102
        }
103

104
        /**
105
         * Translate/set strings used in the option defaults.
106
         *
107
         * @return void
108
         */
109
        public function translate_defaults() {
×
110
                self::$twitter_card_types['summary_large_image'] = __( 'Summary with large image', 'wordpress-seo' );
×
111
        }
112

113
        /**
114
         * Validate the option.
115
         *
116
         * @param array $dirty New value for the option.
117
         * @param array $clean Clean value for the option, normally the defaults.
118
         * @param array $old   Old value of the option.
119
         *
120
         * @return array Validated clean value for the option to be saved to the database.
121
         */
122
        protected function validate_option( $dirty, $clean, $old ) {
18✔
123

124
                foreach ( $clean as $key => $value ) {
18✔
125
                        switch ( $key ) {
9✔
126
                                /* Text fields. */
127
                                case 'og_frontpage_desc':
18✔
128
                                case 'og_frontpage_title':
18✔
129
                                        if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
×
130
                                                $clean[ $key ] = WPSEO_Utils::sanitize_text_field( $dirty[ $key ] );
×
131
                                        }
132
                                        break;
×
133

134
                                case 'og_default_image_id':
18✔
135
                                case 'og_frontpage_image_id':
12✔
136
                                        if ( isset( $dirty[ $key ] ) ) {
6✔
137
                                                $clean[ $key ] = (int) $dirty[ $key ];
4✔
138

139
                                                if ( $dirty[ $key ] === '' ) {
4✔
140
                                                        $clean[ $key ] = $dirty[ $key ];
2✔
141
                                                }
142
                                        }
143
                                        break;
6✔
144

145
                                /* URL text fields - no ftp allowed. */
146
                                case 'facebook_site':
12✔
147
                                case 'instagram_url':
4✔
148
                                case 'linkedin_url':
4✔
149
                                case 'myspace_url':
4✔
150
                                case 'pinterest_url':
4✔
151
                                case 'og_default_image':
4✔
152
                                case 'og_frontpage_image':
4✔
153
                                case 'youtube_url':
4✔
154
                                case 'wikipedia_url':
4✔
155
                                case 'mastodon_url':
4✔
156
                                        $this->validate_url( $key, $dirty, $old, $clean );
8✔
157
                                        break;
8✔
158

159
                                case 'pinterestverify':
4✔
160
                                        $this->validate_verification_string( $key, $dirty, $old, $clean );
×
161
                                        break;
×
162

163
                                /* Twitter user name. */
164
                                case 'twitter_site':
4✔
165
                                        if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
×
166
                                                $twitter_id = $this->validate_twitter_id( $dirty[ $key ] );
×
167

168
                                                if ( $twitter_id ) {
×
169
                                                        $clean[ $key ] = $twitter_id;
×
170
                                                }
171
                                                else {
172
                                                        if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) {
×
173
                                                                $twitter_id = sanitize_text_field( ltrim( $old[ $key ], '@' ) );
×
174
                                                                if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) {
×
175
                                                                        $clean[ $key ] = $twitter_id;
×
176
                                                                }
177
                                                        }
178
                                                        if ( function_exists( 'add_settings_error' ) ) {
×
179
                                                                add_settings_error(
×
180
                                                                        $this->group_name, // Slug title of the setting.
×
181
                                                                        $key, // Suffix-ID for the error message box.
×
182
                                                                        sprintf(
×
183
                                                                                /* translators: %s expands to a twitter user name. */
184
                                                                                __( '%s does not seem to be a valid Twitter Username. Please correct.', 'wordpress-seo' ),
×
185
                                                                                '<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>'
×
186
                                                                        ), // The error message.
187
                                                                        'error' // Message type.
×
188
                                                                );
189
                                                        }
190
                                                }
191
                                                unset( $twitter_id );
×
192

193
                                                Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $dirty[ $key ] );
×
194
                                        }
195
                                        break;
×
196

197
                                case 'twitter_card_type':
4✔
198
                                        if ( isset( $dirty[ $key ], self::$twitter_card_types[ $dirty[ $key ] ] ) && $dirty[ $key ] !== '' ) {
×
199
                                                $clean[ $key ] = $dirty[ $key ];
×
200
                                        }
201
                                        break;
×
202

203
                                /* Boolean fields. */
204
                                case 'opengraph':
4✔
205
                                case 'twitter':
4✔
206
                                        $clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
×
207
                                        break;
×
208

209
                                /* Array fields. */
210
                                case 'other_social_urls':
4✔
211
                                        if ( isset( $dirty[ $key ] ) ) {
4✔
212
                                                $items = $dirty[ $key ];
2✔
213
                                                if ( ! is_array( $items ) ) {
2✔
214
                                                        $items = json_decode( $dirty[ $key ], true );
×
215
                                                }
216

217
                                                if ( is_array( $items ) ) {
2✔
218
                                                        foreach ( $items as $item_key => $item ) {
2✔
219
                                                                $validated_url = $this->validate_social_url( $item );
2✔
220

221
                                                                if ( $validated_url === false ) {
2✔
222
                                                                        // Restore the previous URL values, if any.
223
                                                                        $old_urls = ( isset( $old[ $key ] ) ) ? $old[ $key ] : [];
×
224
                                                                        foreach ( $old_urls as $old_item_key => $old_url ) {
×
225
                                                                                if ( $old_url !== '' ) {
×
226
                                                                                        $url = WPSEO_Utils::sanitize_url( $old_url );
×
227
                                                                                        if ( $url !== '' ) {
×
228
                                                                                                $clean[ $key ][ $old_item_key ] = $url;
×
229
                                                                                        }
230
                                                                                }
231
                                                                        }
232
                                                                        break;
×
233
                                                                }
234

235
                                                                // The URL format is valid, let's sanitize it.
236
                                                                $url = WPSEO_Utils::sanitize_url( $validated_url );
2✔
237
                                                                if ( $url !== '' ) {
2✔
238
                                                                        $clean[ $key ][ $item_key ] = $url;
2✔
239
                                                                }
240
                                                        }
241
                                                }
242
                                        }
243

244
                                        break;
4✔
245
                        }
246
                }
247

248
                return $clean;
18✔
249
        }
250

251
        /**
252
         * Validates a social URL.
253
         *
254
         * @param string $url The url to be validated.
255
         *
256
         * @return string|false The validated URL or false if the URL is not valid.
257
         */
258
        public function validate_social_url( $url ) {
×
259
                $validated_url = filter_var( WPSEO_Utils::sanitize_url( trim( $url ) ), FILTER_VALIDATE_URL );
×
260

261
                return $validated_url;
×
262
        }
263

264
        /**
265
         * Validates a twitter id.
266
         *
267
         * @param string $twitter_id    The twitter id to be validated.
268
         * @param bool   $strip_at_sign Whether or not to strip the `@` sign.
269
         *
270
         * @return string|false The validated twitter id or false if it is not valid.
271
         */
UNCOV
272
        public function validate_twitter_id( $twitter_id, $strip_at_sign = true ) {
×
UNCOV
273
                $twitter_id = ( $strip_at_sign ) ? sanitize_text_field( ltrim( $twitter_id, '@' ) ) : sanitize_text_field( $twitter_id );
×
274

275
                /*
276
                 * From the Twitter documentation about twitter screen names:
277
                 * Typically a maximum of 15 characters long, but some historical accounts
278
                 * may exist with longer names.
279
                 * A username can only contain alphanumeric characters (letters A-Z, numbers 0-9)
280
                 * with the exception of underscores.
281
                 *
282
                 * @link https://support.twitter.com/articles/101299-why-can-t-i-register-certain-usernames
283
                 */
UNCOV
284
                if ( preg_match( '`^[A-Za-z0-9_]{1,25}$`', $twitter_id ) ) {
×
UNCOV
285
                        return $twitter_id;
×
286
                }
287

UNCOV
288
                if ( preg_match( '`^http(?:s)?://(?:www\.)?(?:twitter|x)\.com/(?P<handle>[A-Za-z0-9_]{1,25})/?$`', $twitter_id, $matches ) ) {
×
UNCOV
289
                        return $matches['handle'];
×
290
                }
291

UNCOV
292
                return false;
×
293
        }
294

295
        /**
296
         * Clean a given option value.
297
         *
298
         * @param array       $option_value          Old (not merged with defaults or filtered) option value to
299
         *                                           clean according to the rules for this option.
300
         * @param string|null $current_version       Optional. Version from which to upgrade, if not set,
301
         *                                           version specific upgrades will be disregarded.
302
         * @param array|null  $all_old_option_values Optional. Only used when importing old options to have
303
         *                                           access to the real old values, in contrast to the saved ones.
304
         *
305
         * @return array Cleaned option.
306
         */
307
        protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
×
308

309
                /* Move options from very old option to this one. */
310
                $old_option = null;
×
311
                if ( isset( $all_old_option_values ) ) {
×
312
                        // Ok, we have an import.
313
                        if ( isset( $all_old_option_values['wpseo_indexation'] ) && is_array( $all_old_option_values['wpseo_indexation'] ) && $all_old_option_values['wpseo_indexation'] !== [] ) {
×
314
                                $old_option = $all_old_option_values['wpseo_indexation'];
×
315
                        }
316
                }
317
                else {
318
                        $old_option = get_option( 'wpseo_indexation' );
×
319
                }
320

321
                if ( is_array( $old_option ) && $old_option !== [] ) {
×
322
                        $move = [
323
                                'opengraph',
×
324
                        ];
325
                        foreach ( $move as $key ) {
×
326
                                if ( isset( $old_option[ $key ] ) && ! isset( $option_value[ $key ] ) ) {
×
327
                                        $option_value[ $key ] = $old_option[ $key ];
×
328
                                }
329
                        }
330
                        unset( $move, $key );
×
331
                }
332
                unset( $old_option );
×
333

334
                return $option_value;
×
335
        }
336
}
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