Keine Beschreibung

class-jetpack-search-customize.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * Jetpack Search Overlay Customization
  4. *
  5. * @package automattic/jetpack
  6. */
  7. // Exit if file is accessed directly.
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. require_once __DIR__ . '/class.jetpack-search-helpers.php';
  12. require_once __DIR__ . '/class-jetpack-search-options.php';
  13. /**
  14. * Class to customize search on the site.
  15. *
  16. * @since 8.3.0
  17. */
  18. class Jetpack_Search_Customize {
  19. /**
  20. * Class initialization.
  21. *
  22. * @since 8.3.0
  23. */
  24. public function __construct() {
  25. add_action( 'customize_register', array( $this, 'customize_register' ) );
  26. add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts' ) );
  27. }
  28. /**
  29. * Initialize Customizer controls.
  30. *
  31. * @since 8.3.0
  32. *
  33. * @param WP_Customize_Manager $wp_customize Customizer instance.
  34. */
  35. public function customize_register( $wp_customize ) {
  36. require_once dirname( JETPACK__PLUGIN_FILE ) . '/modules/search/customize-controls/class-label-control.php';
  37. require_once dirname( JETPACK__PLUGIN_FILE ) . '/modules/search/customize-controls/class-excluded-post-types-control.php';
  38. $section_id = 'jetpack_search';
  39. $setting_prefix = Jetpack_Search_Options::OPTION_PREFIX;
  40. $wp_customize->add_section(
  41. $section_id,
  42. array(
  43. 'title' => esc_html__( 'Jetpack Search', 'jetpack' ),
  44. 'capability' => 'edit_theme_options',
  45. 'priority' => 200,
  46. )
  47. );
  48. $id = $setting_prefix . 'color_theme';
  49. $wp_customize->add_setting(
  50. $id,
  51. array(
  52. 'default' => 'light',
  53. 'transport' => 'postMessage',
  54. 'type' => 'option',
  55. )
  56. );
  57. $wp_customize->add_control(
  58. $id,
  59. array(
  60. 'label' => __( 'Theme', 'jetpack' ),
  61. 'description' => __( 'Select a theme for your search overlay.', 'jetpack' ),
  62. 'section' => $section_id,
  63. 'type' => 'radio',
  64. 'choices' => array(
  65. 'light' => __( 'Light', 'jetpack' ),
  66. 'dark' => __( 'Dark', 'jetpack' ),
  67. ),
  68. )
  69. );
  70. $id = $setting_prefix . 'result_format';
  71. $wp_customize->add_setting(
  72. $id,
  73. array(
  74. 'default' => 'minimal',
  75. 'transport' => 'postMessage',
  76. 'type' => 'option',
  77. )
  78. );
  79. $wp_customize->add_control(
  80. $id,
  81. array(
  82. 'label' => __( 'Result Format', 'jetpack' ),
  83. 'description' => __( 'Choose how the search results look.', 'jetpack' ),
  84. 'section' => $section_id,
  85. 'type' => 'select',
  86. 'choices' => array(
  87. 'minimal' => __( 'Minimal', 'jetpack' ),
  88. 'expanded' => __( 'Expanded (shows images)', 'jetpack' ),
  89. 'product' => __( 'Product (for WooCommerce stores)', 'jetpack' ),
  90. ),
  91. )
  92. );
  93. $id = $setting_prefix . 'default_sort';
  94. $wp_customize->add_setting(
  95. $id,
  96. array(
  97. 'default' => 'relevance',
  98. 'type' => 'option',
  99. )
  100. );
  101. $wp_customize->add_control(
  102. $id,
  103. array(
  104. 'choices' => array(
  105. 'relevance' => __( 'Relevance (recommended)', 'jetpack' ),
  106. 'newest' => __( 'Newest first', 'jetpack' ),
  107. 'oldest' => __( 'Oldest first', 'jetpack' ),
  108. ),
  109. 'description' => __( 'Pick the initial sort for your search results.', 'jetpack' ),
  110. 'label' => __( 'Default Sort', 'jetpack' ),
  111. 'section' => $section_id,
  112. 'type' => 'select',
  113. )
  114. );
  115. $id = $setting_prefix . 'overlay_trigger';
  116. $wp_customize->add_setting(
  117. $id,
  118. array(
  119. 'default' => Jetpack_Search_Options::OVERLAY_TRIGGER_IMMEDIATE,
  120. 'transport' => 'postMessage',
  121. 'type' => 'option',
  122. )
  123. );
  124. $wp_customize->add_control(
  125. $id,
  126. array(
  127. 'label' => __( 'Search Input Overlay Trigger', 'jetpack' ),
  128. 'description' => __( 'Select when your overlay should appear.', 'jetpack' ),
  129. 'section' => $section_id,
  130. 'type' => 'select',
  131. 'choices' => array(
  132. Jetpack_Search_Options::OVERLAY_TRIGGER_IMMEDIATE => __( 'Open when user starts typing', 'jetpack' ),
  133. Jetpack_Search_Options::OVERLAY_TRIGGER_RESULTS => __( 'Open when results are available', 'jetpack' ),
  134. Jetpack_Search_Options::OVERLAY_TRIGGER_SUBMIT => __( 'Open when user submits the form', 'jetpack' ),
  135. ),
  136. )
  137. );
  138. $id = $setting_prefix . 'excluded_post_types';
  139. $wp_customize->add_setting(
  140. $id,
  141. array(
  142. 'default' => '',
  143. 'type' => 'option',
  144. )
  145. );
  146. $wp_customize->add_control(
  147. new Excluded_Post_Types_Control(
  148. $wp_customize,
  149. $id,
  150. array(
  151. 'description' => __( 'Choose post types to exclude from search results. You must leave at least one post type unchecked.', 'jetpack' ),
  152. 'label' => __( 'Excluded Post Types', 'jetpack' ),
  153. 'section' => $section_id,
  154. )
  155. )
  156. );
  157. $id = $setting_prefix . 'highlight_color';
  158. $wp_customize->add_setting(
  159. $id,
  160. array(
  161. 'default' => '#FFC',
  162. 'transport' => 'postMessage',
  163. 'type' => 'option',
  164. )
  165. );
  166. $wp_customize->add_control(
  167. new WP_Customize_Color_Control(
  168. $wp_customize,
  169. $id,
  170. array(
  171. 'label' => __( 'Highlight Search Terms', 'jetpack' ),
  172. 'description' => __( 'Choose a color to highlight matching search terms.', 'jetpack' ),
  173. 'section' => $section_id,
  174. )
  175. )
  176. );
  177. $id = $setting_prefix . 'additional_settings_placeholder';
  178. $wp_customize->add_setting(
  179. $id,
  180. array( 'type' => 'option' )
  181. );
  182. $wp_customize->add_control(
  183. new Label_Control(
  184. $wp_customize,
  185. $id,
  186. array(
  187. 'label' => __( 'Additional Jetpack Search Settings', 'jetpack' ),
  188. 'section' => $section_id,
  189. )
  190. )
  191. );
  192. $id = $setting_prefix . 'enable_sort';
  193. $wp_customize->add_setting(
  194. $id,
  195. array(
  196. 'default' => '1',
  197. 'sanitize_callback' => array( 'Jetpack_Search_Helpers', 'sanitize_checkbox_value' ),
  198. 'sanitize_js_callback' => array( 'Jetpack_Search_Helpers', 'sanitize_checkbox_value_for_js' ),
  199. 'transport' => 'postMessage',
  200. 'type' => 'option',
  201. )
  202. );
  203. $wp_customize->add_control(
  204. $id,
  205. array(
  206. 'label' => __( 'Show sort selector', 'jetpack' ),
  207. 'section' => $section_id,
  208. 'type' => 'checkbox',
  209. )
  210. );
  211. $id = $setting_prefix . 'inf_scroll';
  212. $wp_customize->add_setting(
  213. $id,
  214. array(
  215. 'default' => '1',
  216. 'sanitize_callback' => array( 'Jetpack_Search_Helpers', 'sanitize_checkbox_value' ),
  217. 'sanitize_js_callback' => array( 'Jetpack_Search_Helpers', 'sanitize_checkbox_value_for_js' ),
  218. 'transport' => 'postMessage',
  219. 'type' => 'option',
  220. )
  221. );
  222. $wp_customize->add_control(
  223. $id,
  224. array(
  225. 'type' => 'checkbox',
  226. 'section' => $section_id,
  227. 'label' => __( 'Enable infinite scrolling', 'jetpack' ),
  228. )
  229. );
  230. $id = $setting_prefix . 'show_powered_by';
  231. $wp_customize->add_setting(
  232. $id,
  233. array(
  234. 'default' => '1',
  235. 'sanitize_callback' => array( 'Jetpack_Search_Helpers', 'sanitize_checkbox_value' ),
  236. 'sanitize_js_callback' => array( 'Jetpack_Search_Helpers', 'sanitize_checkbox_value_for_js' ),
  237. 'transport' => 'postMessage',
  238. 'type' => 'option',
  239. )
  240. );
  241. $wp_customize->add_control(
  242. $id,
  243. array(
  244. 'type' => 'checkbox',
  245. 'section' => $section_id,
  246. 'label' => __( 'Display "Powered by Jetpack"', 'jetpack' ),
  247. )
  248. );
  249. }
  250. /**
  251. * Enqueue assets for Customizer controls.
  252. *
  253. * @since 9.6.0
  254. */
  255. public function customize_controls_enqueue_scripts() {
  256. $script_relative_path = 'modules/search/customize-controls/customize-controls.js';
  257. $script_version = Jetpack_Search_Helpers::get_asset_version( $script_relative_path );
  258. $script_path = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
  259. wp_enqueue_script(
  260. 'jetpack-instant-search-customizer',
  261. $script_path,
  262. array( 'customize-controls' ),
  263. $script_version,
  264. true
  265. );
  266. }
  267. }