class-customizer.php 6.7 KB

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