PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.9
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / helpers / options-helper.php

options-helper.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.9, at src/helpers/options-helper.php

143 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Helpers;
4
5 use WPSEO_Option_Social;
6 use WPSEO_Option_Titles;
7 use WPSEO_Options;
8
9 /**
10 * A helper object for options.
11 */
12 class Options_Helper {
13
14 /**
15 * Retrieves a single field from any option for the SEO plugin. Keys are always unique.
16 *
17 * @codeCoverageIgnore We have to write test when this method contains own code.
18 *
19 * @param string $key The key it should return.
20 * @param mixed $default_value The default value that should be returned if the key isn't set.
21 *
22 * @return mixed|null Returns value if found, $default_value if not.
23 */
24 public function get( $key, $default_value = null ) {
25 return WPSEO_Options::get( $key, $default_value );
26 }
27
28 /**
29 * Sets a single field to the options.
30 *
31 * @param string $key The key to set.
32 * @param mixed $value The value to set.
33 *
34 * @return mixed|null Returns value if found.
35 */
36 public function set( $key, $value ) {
37 return WPSEO_Options::set( $key, $value );
38 }
39
40 /**
41 * Get a specific default value for an option.
42 *
43 * @param string $option_name The option for which you want to retrieve a default.
44 * @param string $key The key within the option who's default you want.
45 *
46 * @return mixed The default value.
47 */
48 public function get_default( $option_name, $key ) {
49 return WPSEO_Options::get_default( $option_name, $key );
50 }
51
52 /**
53 * Retrieves the title separator.
54 *
55 * @return string The title separator.
56 */
57 public function get_title_separator() {
58 $default = $this->get_default( 'wpseo_titles', 'separator' );
59
60 // Get the titles option and the separator options.
61 $separator = $this->get( 'separator' );
62 $seperator_options = $this->get_separator_options();
63
64 // This should always be set, but just to be sure.
65 if ( isset( $seperator_options[ $separator ] ) ) {
66 // Set the new replacement.
67 $replacement = $seperator_options[ $separator ];
68 }
69 elseif ( isset( $seperator_options[ $default ] ) ) {
70 $replacement = $seperator_options[ $default ];
71 }
72 else {
73 $replacement = \reset( $seperator_options );
74 }
75
76 /**
77 * Filter: 'wpseo_replacements_filter_sep' - Allow customization of the separator character(s).
78 *
79 * @api string $replacement The current separator.
80 */
81 return \apply_filters( 'wpseo_replacements_filter_sep', $replacement );
82 }
83
84 /**
85 * Retrieves a default value from the option titles.
86 *
87 * @param string $option_titles_key The key of the option title you wish to get.
88 *
89 * @return string The option title.
90 */
91 public function get_title_default( $option_titles_key ) {
92 $default_titles = $this->get_title_defaults();
93 if ( ! empty( $default_titles[ $option_titles_key ] ) ) {
94 return $default_titles[ $option_titles_key ];
95 }
96
97 return '';
98 }
99
100 /**
101 * Retrieves the default option titles.
102 *
103 * @codeCoverageIgnore We have to write test when this method contains own code.
104 *
105 * @return array The title defaults.
106 */
107 protected function get_title_defaults() {
108 return WPSEO_Option_Titles::get_instance()->get_defaults();
109 }
110
111 /**
112 * Get the available separator options.
113 *
114 * @return array
115 */
116 protected function get_separator_options() {
117 return WPSEO_Option_Titles::get_instance()->get_separator_options();
118 }
119
120 /**
121 * Validates a social URL.
122 *
123 * @param string $url The url to be validated.
124 *
125 * @return string|false The validated URL or false if the URL is not valid.
126 */
127 public function validate_social_url( $url ) {
128 return $url === '' || WPSEO_Option_Social::get_instance()->validate_social_url( $url );
129 }
130
131 /**
132 * Validates a twitter id.
133 *
134 * @param string $twitter_id The twitter id to be validated.
135 * @param bool $strip_at_sign Whether or not to strip the `@` sign.
136 *
137 * @return string|false The validated twitter id or false if it is not valid.
138 */
139 public function validate_twitter_id( $twitter_id, $strip_at_sign = true ) {
140 return WPSEO_Option_Social::get_instance()->validate_twitter_id( $twitter_id, $strip_at_sign );
141 }
142 }
143