class-customizer.php 6.2 KB

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