search.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. <?php
  2. /**
  3. * Jetpack Search: Jetpack_Search_Widget class
  4. *
  5. * @package Jetpack
  6. * @subpackage Jetpack Search
  7. * @since 5.0.0
  8. */
  9. add_action( 'widgets_init', 'jetpack_search_widget_init' );
  10. function jetpack_search_widget_init() {
  11. if ( ! Jetpack::is_active() || ! Jetpack::active_plan_supports( 'search' ) ) {
  12. return;
  13. }
  14. require_once JETPACK__PLUGIN_DIR . 'modules/search/class.jetpack-search-helpers.php';
  15. register_widget( 'Jetpack_Search_Widget' );
  16. }
  17. /**
  18. * Provides a widget to show available/selected filters on searches.
  19. *
  20. * @since 5.0.0
  21. *
  22. * @see WP_Widget
  23. */
  24. class Jetpack_Search_Widget extends WP_Widget {
  25. /**
  26. * The Jetpack_Search instance.
  27. *
  28. * @since 5.7.0
  29. * @var Jetpack_Search
  30. */
  31. protected $jetpack_search;
  32. /**
  33. * Number of aggregations (filters) to show by default.
  34. *
  35. * @since 5.8.0
  36. * @var int
  37. */
  38. const DEFAULT_FILTER_COUNT = 5;
  39. /**
  40. * Default sort order for search results.
  41. *
  42. * @since 5.8.0
  43. * @var string
  44. */
  45. const DEFAULT_SORT = 'relevance_desc';
  46. /**
  47. * Jetpack_Search_Widget constructor.
  48. *
  49. * @since 5.0.0
  50. */
  51. public function __construct() {
  52. parent::__construct(
  53. Jetpack_Search_Helpers::FILTER_WIDGET_BASE,
  54. /** This filter is documented in modules/widgets/facebook-likebox.php */
  55. apply_filters( 'jetpack_widget_name', esc_html__( 'Search', 'jetpack' ) ),
  56. array(
  57. 'classname' => 'jetpack-filters widget_search',
  58. 'description' => __( 'Replaces the default search with an Elasticsearch-powered search interface and filters.', 'jetpack' ),
  59. )
  60. );
  61. if (
  62. Jetpack_Search_Helpers::is_active_widget( $this->id ) &&
  63. ! Jetpack::is_module_active( 'search' )
  64. ) {
  65. Jetpack::activate_module( 'search', false, false );
  66. }
  67. if ( is_admin() ) {
  68. add_action( 'sidebar_admin_setup', array( $this, 'widget_admin_setup' ) );
  69. } else {
  70. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
  71. }
  72. add_action( 'jetpack_search_render_filters_widget_title', array( 'Jetpack_Search_Template_Tags', 'render_widget_title' ), 10, 3 );
  73. add_action( 'jetpack_search_render_filters', array( 'Jetpack_Search_Template_Tags', 'render_available_filters' ), 10, 2 );
  74. }
  75. /**
  76. * Enqueues the scripts and styles needed for the customizer.
  77. *
  78. * @since 5.7.0
  79. */
  80. public function widget_admin_setup() {
  81. wp_enqueue_style( 'widget-jetpack-search-filters', plugins_url( 'search/css/search-widget-admin-ui.css', __FILE__ ) );
  82. // Required for Tracks
  83. wp_register_script(
  84. 'jp-tracks',
  85. '//stats.wp.com/w.js',
  86. array(),
  87. gmdate( 'YW' ),
  88. true
  89. );
  90. wp_register_script(
  91. 'jp-tracks-functions',
  92. plugins_url( '_inc/lib/tracks/tracks-callables.js', JETPACK__PLUGIN_FILE ),
  93. array(),
  94. JETPACK__VERSION,
  95. false
  96. );
  97. wp_register_script(
  98. 'jetpack-search-widget-admin',
  99. plugins_url( 'search/js/search-widget-admin.js', __FILE__ ),
  100. array( 'jquery', 'jquery-ui-sortable', 'jp-tracks', 'jp-tracks-functions' ),
  101. JETPACK__VERSION
  102. );
  103. wp_localize_script( 'jetpack-search-widget-admin', 'jetpack_search_filter_admin', array(
  104. 'defaultFilterCount' => self::DEFAULT_FILTER_COUNT,
  105. 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(),
  106. 'tracksEventData' => array(
  107. 'is_customizer' => ( function_exists( 'is_customize_preview' ) && is_customize_preview() ) ? 1 : 0,
  108. ),
  109. 'i18n' => array(
  110. 'month' => Jetpack_Search_Helpers::get_date_filter_type_name( 'month', false ),
  111. 'year' => Jetpack_Search_Helpers::get_date_filter_type_name( 'year', false ),
  112. 'monthUpdated' => Jetpack_Search_Helpers::get_date_filter_type_name( 'month', true ),
  113. 'yearUpdated' => Jetpack_Search_Helpers::get_date_filter_type_name( 'year', true ),
  114. ),
  115. ) );
  116. wp_enqueue_script( 'jetpack-search-widget-admin' );
  117. }
  118. /**
  119. * Enqueue scripts and styles for the frontend.
  120. *
  121. * @since 5.8.0
  122. */
  123. public function enqueue_frontend_scripts() {
  124. if ( ! is_active_widget( false, false, $this->id_base, true ) ) {
  125. return;
  126. }
  127. wp_enqueue_script(
  128. 'jetpack-search-widget',
  129. plugins_url( 'search/js/search-widget.js', __FILE__ ),
  130. array( 'jquery' ),
  131. JETPACK__VERSION,
  132. true
  133. );
  134. wp_enqueue_style( 'jetpack-search-widget', plugins_url( 'search/css/search-widget-frontend.css', __FILE__ ) );
  135. }
  136. /**
  137. * Get the list of valid sort types/orders.
  138. *
  139. * @since 5.8.0
  140. *
  141. * @return array The sort orders.
  142. */
  143. private function get_sort_types() {
  144. return array(
  145. 'relevance|DESC' => is_admin() ? esc_html__( 'Relevance (recommended)', 'jetpack' ) : esc_html__( 'Relevance', 'jetpack' ),
  146. 'date|DESC' => esc_html__( 'Newest first', 'jetpack' ),
  147. 'date|ASC' => esc_html__( 'Oldest first', 'jetpack' )
  148. );
  149. }
  150. /**
  151. * Callback for an array_filter() call in order to only get filters for the current widget.
  152. *
  153. * @see Jetpack_Search_Widget::widget()
  154. *
  155. * @since 5.7.0
  156. *
  157. * @param array $item Filter item.
  158. *
  159. * @return bool Whether the current filter item is for the current widget.
  160. */
  161. function is_for_current_widget( $item ) {
  162. return isset( $item['widget_id'] ) && $this->id == $item['widget_id'];
  163. }
  164. /**
  165. * This method returns a boolean for whether the widget should show site-wide filters for the site.
  166. *
  167. * This is meant to provide backwards-compatibility for VIP, and other professional plan users, that manually
  168. * configured filters via `Jetpack_Search::set_filters()`.
  169. *
  170. * @since 5.7.0
  171. *
  172. * @return bool Whether the widget should display site-wide filters or not.
  173. */
  174. public function should_display_sitewide_filters() {
  175. $filter_widgets = get_option( 'widget_jetpack-search-filters' );
  176. // This shouldn't be empty, but just for sanity
  177. if ( empty( $filter_widgets ) ) {
  178. return false;
  179. }
  180. // If any widget has any filters, return false
  181. foreach ( $filter_widgets as $number => $widget ) {
  182. $widget_id = sprintf( '%s-%d', $this->id_base, $number );
  183. if ( ! empty( $widget['filters'] ) && is_active_widget( false, $widget_id, $this->id_base ) ) {
  184. return false;
  185. }
  186. }
  187. return true;
  188. }
  189. public function jetpack_search_populate_defaults( $instance ) {
  190. $instance = wp_parse_args( (array) $instance, array(
  191. 'title' => '',
  192. 'search_box_enabled' => true,
  193. 'user_sort_enabled' => true,
  194. 'sort' => self::DEFAULT_SORT,
  195. 'filters' => array( array() ),
  196. ) );
  197. return $instance;
  198. }
  199. /**
  200. * Responsible for rendering the widget on the frontend.
  201. *
  202. * @since 5.0.0
  203. *
  204. * @param array $args Widgets args supplied by the theme.
  205. * @param array $instance The current widget instance.
  206. */
  207. public function widget( $args, $instance ) {
  208. $instance = $this->jetpack_search_populate_defaults( $instance );
  209. $display_filters = false;
  210. if ( is_search() ) {
  211. if ( Jetpack_Search_Helpers::should_rerun_search_in_customizer_preview() ) {
  212. Jetpack_Search::instance()->update_search_results_aggregations();
  213. }
  214. $filters = Jetpack_Search::instance()->get_filters();
  215. if ( ! Jetpack_Search_Helpers::are_filters_by_widget_disabled() && ! $this->should_display_sitewide_filters() ) {
  216. $filters = array_filter( $filters, array( $this, 'is_for_current_widget' ) );
  217. }
  218. if ( ! empty( $filters ) ) {
  219. $display_filters = true;
  220. }
  221. }
  222. if ( ! $display_filters && empty( $instance['search_box_enabled'] ) && empty( $instance['user_sort_enabled'] ) ) {
  223. return;
  224. }
  225. $title = isset( $instance['title'] ) ? $instance['title'] : '';
  226. if ( empty( $title ) ) {
  227. $title = '';
  228. }
  229. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  230. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  231. echo $args['before_widget'];
  232. ?><div id="<?php echo esc_attr( $this->id ); ?>-wrapper"><?php
  233. if ( ! empty( $title ) ) {
  234. /**
  235. * Responsible for displaying the title of the Jetpack Search filters widget.
  236. *
  237. * @module search
  238. *
  239. * @since 5.7.0
  240. *
  241. * @param string $title The widget's title
  242. * @param string $args['before_title'] The HTML tag to display before the title
  243. * @param string $args['after_title'] The HTML tag to display after the title
  244. */
  245. do_action( 'jetpack_search_render_filters_widget_title', $title, $args['before_title'], $args['after_title'] );
  246. }
  247. $default_sort = isset( $instance['sort'] ) ? $instance['sort'] : self::DEFAULT_SORT;
  248. list( $orderby, $order ) = $this->sorting_to_wp_query_param( $default_sort );
  249. $current_sort = "{$orderby}|{$order}";
  250. // we need to dynamically inject the sort field into the search box when the search box is enabled, and display
  251. // it separately when it's not.
  252. if ( ! empty( $instance['search_box_enabled'] ) ) {
  253. Jetpack_Search_Template_Tags::render_widget_search_form( $instance['post_types'], $orderby, $order );
  254. }
  255. if ( ! empty( $instance['search_box_enabled'] ) && ! empty( $instance['user_sort_enabled'] ) ): ?>
  256. <div class="jetpack-search-sort-wrapper">
  257. <label>
  258. <?php esc_html_e( 'Sort by', 'jetpack' ); ?>
  259. <select class="jetpack-search-sort">
  260. <?php foreach ( $this->get_sort_types() as $sort => $label ) { ?>
  261. <option value="<?php echo esc_attr( $sort ); ?>" <?php selected( $current_sort, $sort ); ?>>
  262. <?php echo esc_html( $label ); ?>
  263. </option>
  264. <?php } ?>
  265. </select>
  266. </label>
  267. </div>
  268. <?php endif;
  269. if ( $display_filters ) {
  270. /**
  271. * Responsible for rendering filters to narrow down search results.
  272. *
  273. * @module search
  274. *
  275. * @since 5.8.0
  276. *
  277. * @param array $filters The possible filters for the current query.
  278. * @param array $post_types An array of post types to limit filtering to.
  279. */
  280. do_action(
  281. 'jetpack_search_render_filters',
  282. $filters,
  283. isset( $instance['post_types'] ) ? $instance['post_types'] : null
  284. );
  285. }
  286. $this->maybe_render_sort_javascript( $instance, $order, $orderby );
  287. echo "</div>";
  288. echo $args['after_widget'];
  289. }
  290. /**
  291. * Renders JavaScript for the sorting controls on the frontend.
  292. *
  293. * This JS is a bit complicated, but here's what it's trying to do:
  294. * - find the search form
  295. * - find the orderby/order fields and set default values
  296. * - detect changes to the sort field, if it exists, and use it to set the order field values
  297. *
  298. * @since 5.8.0
  299. *
  300. * @param array $instance The current widget instance.
  301. * @param string $order The order to initialize the select with.
  302. * @param string $orderby The orderby to initialize the select with.
  303. */
  304. private function maybe_render_sort_javascript( $instance, $order, $orderby ) {
  305. if ( ! empty( $instance['user_sort_enabled'] ) ) :
  306. ?>
  307. <script type="text/javascript">
  308. jQuery( document ).ready( function( $ ) {
  309. var orderByDefault = <?php echo wp_json_encode( $orderby ); ?>,
  310. orderDefault = <?php echo wp_json_encode( $order ); ?>,
  311. widgetId = <?php echo wp_json_encode( $this->id ); ?>,
  312. searchQuery = <?php echo wp_json_encode( get_query_var( 's', '' ) ); ?>,
  313. isSearch = <?php echo wp_json_encode( is_search() ); ?>;
  314. var container = $( '#' + widgetId + '-wrapper' ),
  315. form = container.find('.jetpack-search-form form'),
  316. orderBy = form.find( 'input[name=orderby]'),
  317. order = form.find( 'input[name=order]'),
  318. searchInput = form.find( 'input[name="s"]' );
  319. orderBy.val( orderByDefault );
  320. order.val( orderDefault );
  321. // Some themes don't set the search query, which results in the query being lost
  322. // when doing a sort selection. So, if the query isn't set, let's set it now. This approach
  323. // is chosen over running a regex over HTML for every search query performed.
  324. if ( isSearch && ! searchInput.val() ) {
  325. searchInput.val( searchQuery );
  326. }
  327. searchInput.addClass( 'show-placeholder' );
  328. container.find( '.jetpack-search-sort' ).change( function( event ) {
  329. var values = event.target.value.split( '|' );
  330. orderBy.val( values[0] );
  331. order.val( values[1] );
  332. form.submit();
  333. });
  334. } );
  335. </script>
  336. <?php
  337. endif;
  338. }
  339. /**
  340. * Convert a sort string into the separate order by and order parts.
  341. *
  342. * @since 5.8.0
  343. *
  344. * @param string $sort A sort string.
  345. *
  346. * @return array Order by and order.
  347. */
  348. private function sorting_to_wp_query_param( $sort ) {
  349. $parts = explode( '|', $sort );
  350. $orderby = isset( $_GET['orderby'] )
  351. ? $_GET['orderby']
  352. : $parts[0];
  353. $order = isset( $_GET['order'] )
  354. ? strtoupper( $_GET['order'] )
  355. : ( ( isset( $parts[1] ) && 'ASC' === strtoupper( $parts[1] ) ) ? 'ASC' : 'DESC' );
  356. return array( $orderby, $order );
  357. }
  358. /**
  359. * Updates a particular instance of the widget. Validates and sanitizes the options.
  360. *
  361. * @since 5.0.0
  362. *
  363. * @param array $new_instance New settings for this instance as input by the user via Jetpack_Search_Widget::form().
  364. * @param array $old_instance Old settings for this instance.
  365. *
  366. * @return array Settings to save.
  367. */
  368. public function update( $new_instance, $old_instance ) {
  369. $instance = array();
  370. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  371. $instance['search_box_enabled'] = empty( $new_instance['search_box_enabled'] ) ? '0' : '1';
  372. $instance['user_sort_enabled'] = empty( $new_instance['user_sort_enabled'] ) ? '0' : '1';
  373. $instance['sort'] = $new_instance['sort'];
  374. $instance['post_types'] = empty( $new_instance['post_types'] ) || empty( $instance['search_box_enabled'] )
  375. ? array()
  376. : array_map( 'sanitize_key', $new_instance['post_types'] );
  377. $filters = array();
  378. if ( isset( $new_instance['filter_type'] ) ) {
  379. foreach ( (array) $new_instance['filter_type'] as $index => $type ) {
  380. $count = intval( $new_instance['num_filters'][ $index ] );
  381. $count = min( 50, $count ); // Set max boundary at 20.
  382. $count = max( 1, $count ); // Set min boundary at 1.
  383. switch ( $type ) {
  384. case 'taxonomy':
  385. $filters[] = array(
  386. 'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
  387. 'type' => 'taxonomy',
  388. 'taxonomy' => sanitize_key( $new_instance['taxonomy_type'][ $index ] ),
  389. 'count' => $count,
  390. );
  391. break;
  392. case 'post_type':
  393. $filters[] = array(
  394. 'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
  395. 'type' => 'post_type',
  396. 'count' => $count,
  397. );
  398. break;
  399. case 'date_histogram':
  400. $filters[] = array(
  401. 'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
  402. 'type' => 'date_histogram',
  403. 'count' => $count,
  404. 'field' => sanitize_key( $new_instance['date_histogram_field'][ $index ] ),
  405. 'interval' => sanitize_key( $new_instance['date_histogram_interval'][ $index ] ),
  406. );
  407. break;
  408. }
  409. }
  410. }
  411. if ( ! empty( $filters ) ) {
  412. $instance['filters'] = $filters;
  413. }
  414. return $instance;
  415. }
  416. /**
  417. * Outputs the settings update form.
  418. *
  419. * @since 5.0.0
  420. *
  421. * @param array $instance Current settings.
  422. */
  423. public function form( $instance ) {
  424. $instance = $this->jetpack_search_populate_defaults( $instance );
  425. $title = strip_tags( $instance['title'] );
  426. $hide_filters = Jetpack_Search_Helpers::are_filters_by_widget_disabled();
  427. $classes = sprintf(
  428. 'jetpack-search-filters-widget %s %s %s',
  429. $hide_filters ? 'hide-filters' : '',
  430. $instance['search_box_enabled'] ? '' : 'hide-post-types',
  431. $this->id
  432. );
  433. ?>
  434. <div class="<?php echo esc_attr( $classes ); ?>">
  435. <p>
  436. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
  437. <?php esc_html_e( 'Title (optional):', 'jetpack' ); ?>
  438. </label>
  439. <input
  440. class="widefat"
  441. id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
  442. name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
  443. type="text"
  444. value="<?php echo esc_attr( $title ); ?>"
  445. />
  446. </p>
  447. <p>
  448. <label>
  449. <input
  450. type="checkbox"
  451. class="jetpack-search-filters-widget__search-box-enabled"
  452. name="<?php echo esc_attr( $this->get_field_name( 'search_box_enabled' ) ); ?>"
  453. <?php checked( $instance['search_box_enabled'] ); ?>
  454. />
  455. <?php esc_html_e( 'Show search box', 'jetpack' ); ?>
  456. </label>
  457. </p>
  458. <p>
  459. <label>
  460. <input
  461. type="checkbox"
  462. class="jetpack-search-filters-widget__sort-controls-enabled"
  463. name="<?php echo esc_attr( $this->get_field_name( 'user_sort_enabled' ) ); ?>"
  464. <?php checked( $instance['user_sort_enabled'] ); ?>
  465. <?php disabled( ! $instance['search_box_enabled'] ); ?>
  466. />
  467. <?php esc_html_e( 'Show sort selection dropdown', 'jetpack' ); ?>
  468. </label>
  469. </p>
  470. <p class="jetpack-search-filters-widget__post-types-select">
  471. <label><?php esc_html_e( 'Post types to search (minimum of 1):', 'jetpack' ); ?></label>
  472. <?php foreach ( get_post_types( array( 'exclude_from_search' => false ), 'objects' ) as $post_type ) : ?>
  473. <label>
  474. <input
  475. type="checkbox"
  476. value="<?php echo esc_attr( $post_type->name ); ?>"
  477. name="<?php echo esc_attr( $this->get_field_name( 'post_types' ) ); ?>[]"
  478. <?php checked( empty( $instance['post_types'] ) || in_array( $post_type->name, $instance['post_types'] ) ); ?>
  479. />&nbsp;
  480. <?php echo esc_html( $post_type->label ); ?>
  481. </label>
  482. <?php endforeach; ?>
  483. </p>
  484. <p>
  485. <label>
  486. <?php esc_html_e( 'Default sort order:', 'jetpack' ); ?>
  487. <select
  488. name="<?php echo esc_attr( $this->get_field_name( 'sort' ) ); ?>"
  489. class="widefat jetpack-search-filters-widget__sort-order">
  490. <?php foreach ( $this->get_sort_types() as $sort_type => $label ) { ?>
  491. <option value="<?php echo esc_attr( $sort_type ); ?>" <?php selected( $instance['sort'], $sort_type ); ?>>
  492. <?php echo esc_html( $label ); ?>
  493. </option>
  494. <?php } ?>
  495. </select>
  496. </label>
  497. </p>
  498. <?php if ( ! $hide_filters ): ?>
  499. <script class="jetpack-search-filters-widget__filter-template" type="text/template">
  500. <?php echo $this->render_widget_edit_filter( array(), true ); ?>
  501. </script>
  502. <div class="jetpack-search-filters-widget__filters">
  503. <?php foreach ( (array) $instance['filters'] as $filter ) : ?>
  504. <?php $this->render_widget_edit_filter( $filter ); ?>
  505. <?php endforeach; ?>
  506. </div>
  507. <p class="jetpack-search-filters-widget__add-filter-wrapper">
  508. <a class="button jetpack-search-filters-widget__add-filter" href="#">
  509. <?php esc_html_e( 'Add a filter', 'jetpack' ); ?>
  510. </a>
  511. </p>
  512. <noscript>
  513. <p class="jetpack-search-filters-help">
  514. <?php echo esc_html_e( 'Adding filters requires JavaScript!', 'jetpack' ); ?>
  515. </p>
  516. </noscript>
  517. <?php if ( is_customize_preview() ) : ?>
  518. <p class="jetpack-search-filters-help">
  519. <a href="https://jetpack.com/support/search/#filters-not-showing-up" target="_blank">
  520. <?php esc_html_e( "Why aren't my filters appearing?", 'jetpack' ); ?>
  521. </a>
  522. </p>
  523. <?php endif; ?>
  524. <?php endif; ?>
  525. </div>
  526. <?php
  527. }
  528. /**
  529. * We need to render HTML in two formats: an Underscore template (client-size)
  530. * and native PHP (server-side). This helper function allows for easy rendering
  531. * of attributes in both formats.
  532. *
  533. * @since 5.8.0
  534. *
  535. * @param string $name Attribute name.
  536. * @param string $value Attribute value.
  537. * @param bool $is_template Whether this is for an Underscore template or not.
  538. */
  539. private function render_widget_attr( $name, $value, $is_template ) {
  540. echo $is_template ? "<%= $name %>" : esc_attr( $value );
  541. }
  542. /**
  543. * We need to render HTML in two formats: an Underscore template (client-size)
  544. * and native PHP (server-side). This helper function allows for easy rendering
  545. * of the "selected" attribute in both formats.
  546. *
  547. * @since 5.8.0
  548. *
  549. * @param string $name Attribute name.
  550. * @param string $value Attribute value.
  551. * @param string $compare Value to compare to the attribute value to decide if it should be selected.
  552. * @param bool $is_template Whether this is for an Underscore template or not.
  553. */
  554. private function render_widget_option_selected( $name, $value, $compare, $is_template ) {
  555. $compare_json = wp_json_encode( $compare );
  556. echo $is_template ? "<%= $compare_json === $name ? 'selected=\"selected\"' : '' %>" : selected( $value, $compare );
  557. }
  558. /**
  559. * Responsible for rendering a single filter in the customizer or the widget administration screen in wp-admin.
  560. *
  561. * We use this method for two purposes - rendering the fields server-side, and also rendering a script template for Underscore.
  562. *
  563. * @since 5.7.0
  564. *
  565. * @param array $filter The filter to render.
  566. * @param bool $is_template Whether this is for an Underscore template or not.
  567. */
  568. public function render_widget_edit_filter( $filter, $is_template = false ) {
  569. $args = wp_parse_args( $filter, array(
  570. 'name' => '',
  571. 'type' => 'taxonomy',
  572. 'taxonomy' => '',
  573. 'post_type' => '',
  574. 'field' => '',
  575. 'interval' => '',
  576. 'count' => self::DEFAULT_FILTER_COUNT,
  577. ) );
  578. $args['name_placeholder'] = Jetpack_Search_Helpers::generate_widget_filter_name( $args );
  579. ?>
  580. <div class="jetpack-search-filters-widget__filter is-<?php $this->render_widget_attr( 'type', $args['type'], $is_template ); ?>">
  581. <p class="jetpack-search-filters-widget__type-select">
  582. <label>
  583. <?php esc_html_e( 'Filter Type:', 'jetpack' ); ?>
  584. <select name="<?php echo esc_attr( $this->get_field_name( 'filter_type' ) ); ?>[]" class="widefat filter-select">
  585. <option value="taxonomy" <?php $this->render_widget_option_selected( 'type', $args['type'], 'taxonomy', $is_template ); ?>>
  586. <?php esc_html_e( 'Taxonomy', 'jetpack' ); ?>
  587. </option>
  588. <option value="post_type" <?php $this->render_widget_option_selected( 'type', $args['type'], 'post_type', $is_template ); ?>>
  589. <?php esc_html_e( 'Post Type', 'jetpack' ); ?>
  590. </option>
  591. <option value="date_histogram" <?php $this->render_widget_option_selected( 'type', $args['type'], 'date_histogram', $is_template ); ?>>
  592. <?php esc_html_e( 'Date', 'jetpack' ); ?>
  593. </option>
  594. </select>
  595. </label>
  596. </p>
  597. <p class="jetpack-search-filters-widget__taxonomy-select">
  598. <label>
  599. <?php
  600. esc_html_e( 'Choose a taxonomy:', 'jetpack' );
  601. $seen_taxonomy_labels = array();
  602. ?>
  603. <select name="<?php echo esc_attr( $this->get_field_name( 'taxonomy_type' ) ); ?>[]" class="widefat taxonomy-select">
  604. <?php foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $taxonomy ) : ?>
  605. <option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php $this->render_widget_option_selected( 'taxonomy', $args['taxonomy'], $taxonomy->name, $is_template ); ?>>
  606. <?php
  607. $label = in_array( $taxonomy->label, $seen_taxonomy_labels )
  608. ? sprintf(
  609. /* translators: %1$s is the taxonomy name, %2s is the name of its type to help distinguish between several taxonomies with the same name, e.g. category and tag. */
  610. _x( '%1$s (%2$s)', 'A label for a taxonomy selector option', 'jetpack' ),
  611. $taxonomy->label,
  612. $taxonomy->name
  613. )
  614. : $taxonomy->label;
  615. echo esc_html( $label );
  616. $seen_taxonomy_labels[] = $taxonomy->label;
  617. ?>
  618. </option>
  619. <?php endforeach; ?>
  620. </select>
  621. </label>
  622. </p>
  623. <p class="jetpack-search-filters-widget__date-histogram-select">
  624. <label>
  625. <?php esc_html_e( 'Choose a field:', 'jetpack' ); ?>
  626. <select name="<?php echo esc_attr( $this->get_field_name( 'date_histogram_field' ) ); ?>[]" class="widefat date-field-select">
  627. <option value="post_date" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_date', $is_template ); ?>>
  628. <?php esc_html_e( 'Date', 'jetpack' ); ?>
  629. </option>
  630. <option value="post_date_gmt" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_date_gmt', $is_template ); ?>>
  631. <?php esc_html_e( 'Date GMT', 'jetpack' ); ?>
  632. </option>
  633. <option value="post_modified" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_modified', $is_template ); ?>>
  634. <?php esc_html_e( 'Modified', 'jetpack' ); ?>
  635. </option>
  636. <option value="post_modified_gmt" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_modified_gmt', $is_template ); ?>>
  637. <?php esc_html_e( 'Modified GMT', 'jetpack' ); ?>
  638. </option>
  639. </select>
  640. </label>
  641. </p>
  642. <p class="jetpack-search-filters-widget__date-histogram-select">
  643. <label>
  644. <?php esc_html_e( 'Choose an interval:' ); ?>
  645. <select name="<?php echo esc_attr( $this->get_field_name( 'date_histogram_interval' ) ); ?>[]" class="widefat date-interval-select">
  646. <option value="month" <?php $this->render_widget_option_selected( 'interval', $args['interval'], 'month', $is_template ); ?>>
  647. <?php esc_html_e( 'Month', 'jetpack' ); ?>
  648. </option>
  649. <option value="year" <?php $this->render_widget_option_selected( 'interval', $args['interval'], 'year', $is_template ); ?>>
  650. <?php esc_html_e( 'Year', 'jetpack' ); ?>
  651. </option>
  652. </select>
  653. </label>
  654. </p>
  655. <p class="jetpack-search-filters-widget__title">
  656. <label>
  657. <?php esc_html_e( 'Title:', 'jetpack' ); ?>
  658. <input
  659. class="widefat"
  660. type="text"
  661. name="<?php echo esc_attr( $this->get_field_name( 'filter_name' ) ); ?>[]"
  662. value="<?php $this->render_widget_attr( 'name', $args['name'], $is_template ); ?>"
  663. placeholder="<?php $this->render_widget_attr( 'name_placeholder', $args['name_placeholder'], $is_template ); ?>"
  664. />
  665. </label>
  666. </p>
  667. <p>
  668. <label>
  669. <?php esc_html_e( 'Maximum number of filters (1-50):', 'jetpack' ); ?>
  670. <input
  671. class="widefat filter-count"
  672. name="<?php echo esc_attr( $this->get_field_name( 'num_filters' ) ); ?>[]"
  673. type="number"
  674. value="<?php $this->render_widget_attr( 'count', $args['count'], $is_template ); ?>"
  675. min="1"
  676. max="50"
  677. step="1"
  678. required
  679. />
  680. </label>
  681. </p>
  682. <p class="jetpack-search-filters-widget__controls">
  683. <a href="#" class="delete"><?php esc_html_e( 'Remove', 'jetpack' ); ?></a>
  684. </p>
  685. </div>
  686. <?php }
  687. }