| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use WPSEO_Option_Llmstxt; |
| 6 |
use WPSEO_Option_Social; |
| 7 |
use WPSEO_Option_Titles; |
| 8 |
use WPSEO_Option_Tracking_Only; |
| 9 |
use WPSEO_Options; |
| 10 |
|
| 11 |
/** |
| 12 |
* A helper object for options. |
| 13 |
*/ |
| 14 |
class Options_Helper { |
| 15 |
|
| 16 |
/** |
| 17 |
* Retrieves a single field from any option for the SEO plugin. Keys are always unique. |
| 18 |
* |
| 19 |
* @codeCoverageIgnore We have to write test when this method contains own code. |
| 20 |
* |
| 21 |
* @param string $key The key it should return. |
| 22 |
* @param mixed $default_value The default value that should be returned if the key isn't set. |
| 23 |
* |
| 24 |
* @return mixed|null Returns value if found, $default_value if not. |
| 25 |
*/ |
| 26 |
public function get( $key, $default_value = null ) { |
| 27 |
return WPSEO_Options::get( $key, $default_value ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Sets a single field to the options. |
| 32 |
* |
| 33 |
* @param string $key The key to set. |
| 34 |
* @param mixed $value The value to set. |
| 35 |
* @param string $option_group The lookup table which represents the option_group where the key is stored. |
| 36 |
* |
| 37 |
* @return mixed|null Returns value if found. |
| 38 |
*/ |
| 39 |
public function set( $key, $value, $option_group = '' ) { |
| 40 |
return WPSEO_Options::set( $key, $value, $option_group ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get a specific default value for an option. |
| 45 |
* |
| 46 |
* @param string $option_name The option for which you want to retrieve a default. |
| 47 |
* @param string $key The key within the option who's default you want. |
| 48 |
* |
| 49 |
* @return mixed The default value. |
| 50 |
*/ |
| 51 |
public function get_default( $option_name, $key ) { |
| 52 |
return WPSEO_Options::get_default( $option_name, $key ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Retrieves the title separator. |
| 57 |
* |
| 58 |
* @return string The title separator. |
| 59 |
*/ |
| 60 |
public function get_title_separator() { |
| 61 |
$default = $this->get_default( 'wpseo_titles', 'separator' ); |
| 62 |
|
| 63 |
// Get the titles option and the separator options. |
| 64 |
$separator = $this->get( 'separator' ); |
| 65 |
$seperator_options = $this->get_separator_options(); |
| 66 |
|
| 67 |
// This should always be set, but just to be sure. |
| 68 |
if ( isset( $seperator_options[ $separator ] ) ) { |
| 69 |
// Set the new replacement. |
| 70 |
$replacement = $seperator_options[ $separator ]; |
| 71 |
} |
| 72 |
elseif ( isset( $seperator_options[ $default ] ) ) { |
| 73 |
$replacement = $seperator_options[ $default ]; |
| 74 |
} |
| 75 |
else { |
| 76 |
$replacement = \reset( $seperator_options ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Filter: 'wpseo_replacements_filter_sep' - Allow customization of the separator character(s). |
| 81 |
* |
| 82 |
* @param string $replacement The current separator. |
| 83 |
*/ |
| 84 |
return \apply_filters( 'wpseo_replacements_filter_sep', $replacement ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Retrieves a default value from the option titles. |
| 89 |
* |
| 90 |
* @param string $option_titles_key The key of the option title you wish to get. |
| 91 |
* |
| 92 |
* @return string The option title. |
| 93 |
*/ |
| 94 |
public function get_title_default( $option_titles_key ) { |
| 95 |
$default_titles = $this->get_title_defaults(); |
| 96 |
if ( ! empty( $default_titles[ $option_titles_key ] ) ) { |
| 97 |
return $default_titles[ $option_titles_key ]; |
| 98 |
} |
| 99 |
|
| 100 |
return ''; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Retrieves the default option titles. |
| 105 |
* |
| 106 |
* @codeCoverageIgnore We have to write test when this method contains own code. |
| 107 |
* |
| 108 |
* @return array The title defaults. |
| 109 |
*/ |
| 110 |
protected function get_title_defaults() { |
| 111 |
return WPSEO_Option_Titles::get_instance()->get_defaults(); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Retrieves the tracking only options. |
| 116 |
* |
| 117 |
* @codeCoverageIgnore We have to write test when this method contains own code. |
| 118 |
* |
| 119 |
* @return string[] The tracking only options. |
| 120 |
*/ |
| 121 |
public function get_tracking_only_options() { |
| 122 |
return \array_keys( WPSEO_Option_Tracking_Only::get_instance()->get_defaults() ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get the available separator options. |
| 127 |
* |
| 128 |
* @return array |
| 129 |
*/ |
| 130 |
protected function get_separator_options() { |
| 131 |
return WPSEO_Option_Titles::get_instance()->get_separator_options(); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Checks whether a social URL is valid, with empty strings being valid social URLs. |
| 136 |
* |
| 137 |
* @param string $url The url to be checked. |
| 138 |
* |
| 139 |
* @return bool Whether the URL is valid. |
| 140 |
*/ |
| 141 |
public function is_social_url_valid( $url ) { |
| 142 |
return $url === '' || WPSEO_Option_Social::get_instance()->validate_social_url( $url ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Checks whether a twitter id is valid, with empty strings being valid twitter id. |
| 147 |
* |
| 148 |
* @param string $twitter_id The twitter id to be checked. |
| 149 |
* |
| 150 |
* @return bool Whether the twitter id is valid. |
| 151 |
*/ |
| 152 |
public function is_twitter_id_valid( $twitter_id ) { |
| 153 |
return empty( $twitter_id ) || WPSEO_Option_Social::get_instance()->validate_twitter_id( $twitter_id, false ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Gets the limit for the other included pages. |
| 158 |
* |
| 159 |
* @return int The limit for the other included pages. |
| 160 |
*/ |
| 161 |
public function get_other_included_pages_limit() { |
| 162 |
return WPSEO_Option_Llmstxt::get_instance()->get_other_included_pages_limit(); |
| 163 |
} |
| 164 |
} |
| 165 |
|