| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Customizer |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class with functionality to support WP SEO settings in WordPress Customizer. |
| 10 |
*/ |
| 11 |
class WPSEO_Customizer { |
| 12 |
|
| 13 |
/** |
| 14 |
* Holds the customize manager. |
| 15 |
* |
| 16 |
* @var WP_Customize_Manager |
| 17 |
*/ |
| 18 |
protected $wp_customize; |
| 19 |
|
| 20 |
/** |
| 21 |
* Template for the setting IDs used for the customizer. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $setting_template = 'wpseo_titles[%s]'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Default arguments for the breadcrumbs customizer settings object. |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
private $default_setting_args = [ |
| 33 |
'default' => '', |
| 34 |
'type' => 'option', |
| 35 |
'transport' => 'refresh', |
| 36 |
]; |
| 37 |
|
| 38 |
/** |
| 39 |
* Default arguments for the breadcrumbs customizer control object. |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
private $default_control_args = [ |
| 44 |
'label' => '', |
| 45 |
'type' => 'text', |
| 46 |
'section' => 'wpseo_breadcrumbs_customizer_section', |
| 47 |
'settings' => '', |
| 48 |
'context' => '', |
| 49 |
]; |
| 50 |
|
| 51 |
/** |
| 52 |
* Construct Method. |
| 53 |
*/ |
| 54 |
public function __construct() { |
| 55 |
add_action( 'customize_register', [ $this, 'wpseo_customize_register' ] ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Function to support WordPress Customizer. |
| 60 |
* |
| 61 |
* @param WP_Customize_Manager $wp_customize Manager class instance. |
| 62 |
*/ |
| 63 |
public function wpseo_customize_register( $wp_customize ) { |
| 64 |
if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
$this->wp_customize = $wp_customize; |
| 69 |
|
| 70 |
$this->breadcrumbs_section(); |
| 71 |
$this->breadcrumbs_blog_show_setting(); |
| 72 |
$this->breadcrumbs_separator_setting(); |
| 73 |
$this->breadcrumbs_home_setting(); |
| 74 |
$this->breadcrumbs_prefix_setting(); |
| 75 |
$this->breadcrumbs_archiveprefix_setting(); |
| 76 |
$this->breadcrumbs_searchprefix_setting(); |
| 77 |
$this->breadcrumbs_404_setting(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Add the breadcrumbs section to the customizer. |
| 82 |
*/ |
| 83 |
private function breadcrumbs_section() { |
| 84 |
$section_args = [ |
| 85 |
/* translators: %s is the name of the plugin */ |
| 86 |
'title' => sprintf( __( '%s Breadcrumbs', 'wordpress-seo' ), 'Yoast SEO' ), |
| 87 |
'priority' => 999, |
| 88 |
'active_callback' => [ $this, 'breadcrumbs_active_callback' ], |
| 89 |
]; |
| 90 |
|
| 91 |
$this->wp_customize->add_section( 'wpseo_breadcrumbs_customizer_section', $section_args ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns whether or not the breadcrumbs are active. |
| 96 |
* |
| 97 |
* @return bool |
| 98 |
*/ |
| 99 |
public function breadcrumbs_active_callback() { |
| 100 |
return current_theme_supports( 'yoast-seo-breadcrumbs' ) || WPSEO_Options::get( 'breadcrumbs-enable' ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Adds the breadcrumbs show blog checkbox. |
| 105 |
*/ |
| 106 |
private function breadcrumbs_blog_show_setting() { |
| 107 |
$index = 'breadcrumbs-display-blog-page'; |
| 108 |
$control_args = [ |
| 109 |
'label' => __( 'Show blog page in breadcrumbs', 'wordpress-seo' ), |
| 110 |
'type' => 'checkbox', |
| 111 |
'active_callback' => [ $this, 'breadcrumbs_blog_show_active_cb' ], |
| 112 |
]; |
| 113 |
|
| 114 |
$this->add_setting_and_control( $index, $control_args ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Returns whether or not to show the breadcrumbs blog show option. |
| 119 |
* |
| 120 |
* @return bool |
| 121 |
*/ |
| 122 |
public function breadcrumbs_blog_show_active_cb() { |
| 123 |
return get_option( 'show_on_front' ) === 'page'; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Adds the breadcrumbs separator text field. |
| 128 |
*/ |
| 129 |
private function breadcrumbs_separator_setting() { |
| 130 |
$index = 'breadcrumbs-sep'; |
| 131 |
$control_args = [ |
| 132 |
'label' => __( 'Breadcrumbs separator:', 'wordpress-seo' ), |
| 133 |
]; |
| 134 |
$id = 'wpseo-breadcrumbs-separator'; |
| 135 |
|
| 136 |
$this->add_setting_and_control( $index, $control_args, $id ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Adds the breadcrumbs home anchor text field. |
| 141 |
*/ |
| 142 |
private function breadcrumbs_home_setting() { |
| 143 |
$index = 'breadcrumbs-home'; |
| 144 |
$control_args = [ |
| 145 |
'label' => __( 'Anchor text for the homepage:', 'wordpress-seo' ), |
| 146 |
]; |
| 147 |
|
| 148 |
$this->add_setting_and_control( $index, $control_args ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Adds the breadcrumbs prefix text field. |
| 153 |
*/ |
| 154 |
private function breadcrumbs_prefix_setting() { |
| 155 |
$index = 'breadcrumbs-prefix'; |
| 156 |
$control_args = [ |
| 157 |
'label' => __( 'Prefix for breadcrumbs:', 'wordpress-seo' ), |
| 158 |
]; |
| 159 |
|
| 160 |
$this->add_setting_and_control( $index, $control_args ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Adds the breadcrumbs archive prefix text field. |
| 165 |
*/ |
| 166 |
private function breadcrumbs_archiveprefix_setting() { |
| 167 |
$index = 'breadcrumbs-archiveprefix'; |
| 168 |
$control_args = [ |
| 169 |
'label' => __( 'Prefix for archive pages:', 'wordpress-seo' ), |
| 170 |
]; |
| 171 |
|
| 172 |
$this->add_setting_and_control( $index, $control_args ); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Adds the breadcrumbs search prefix text field. |
| 177 |
*/ |
| 178 |
private function breadcrumbs_searchprefix_setting() { |
| 179 |
$index = 'breadcrumbs-searchprefix'; |
| 180 |
$control_args = [ |
| 181 |
'label' => __( 'Prefix for search result pages:', 'wordpress-seo' ), |
| 182 |
]; |
| 183 |
|
| 184 |
$this->add_setting_and_control( $index, $control_args ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Adds the breadcrumb 404 prefix text field. |
| 189 |
*/ |
| 190 |
private function breadcrumbs_404_setting() { |
| 191 |
$index = 'breadcrumbs-404crumb'; |
| 192 |
$control_args = [ |
| 193 |
'label' => __( 'Breadcrumb for 404 pages:', 'wordpress-seo' ), |
| 194 |
]; |
| 195 |
|
| 196 |
$this->add_setting_and_control( $index, $control_args ); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Adds the customizer setting and control. |
| 201 |
* |
| 202 |
* @param string $index Array key index to use for the customizer setting. |
| 203 |
* @param array $control_args Customizer control object arguments. |
| 204 |
* Only those different from the default need to be passed. |
| 205 |
* @param string|null $id Optional. Customizer control object ID. |
| 206 |
* Will default to 'wpseo-' . $index. |
| 207 |
* @param array $custom_settings Optional. Customizer setting arguments. |
| 208 |
* Only those different from the default need to be passed. |
| 209 |
*/ |
| 210 |
private function add_setting_and_control( $index, $control_args, $id = null, $custom_settings = [] ) { |
| 211 |
$setting = sprintf( $this->setting_template, $index ); |
| 212 |
$control_args = array_merge( $this->default_control_args, $control_args ); |
| 213 |
$control_args['settings'] = $setting; |
| 214 |
|
| 215 |
$settings_args = $this->default_setting_args; |
| 216 |
if ( ! empty( $custom_settings ) ) { |
| 217 |
$settings_args = array_merge( $settings_args, $custom_settings ); |
| 218 |
} |
| 219 |
|
| 220 |
if ( ! isset( $id ) ) { |
| 221 |
$id = 'wpseo-' . $index; |
| 222 |
} |
| 223 |
|
| 224 |
$this->wp_customize->add_setting( $setting, $settings_args ); |
| 225 |
|
| 226 |
$control = new WP_Customize_Control( $this->wp_customize, $id, $control_args ); |
| 227 |
$this->wp_customize->add_control( $control ); |
| 228 |
} |
| 229 |
} |
| 230 |
|