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