summaryrefslogtreecommitdiff
blob: a212af00e4586c3c16ddda734a5a466418157d88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
 * Template tags class used primarily for rendering widget-related HTML.
 *
 * Currently, this package can only run in the Jetpack plugin due to its usage of Jetpack_Search.
 * Once Jetpack_Search has been migrated to the package as Classic_Search,
 * this library will be independent from the Jetpack plugin.
 *
 * @package    automattic/jetpack-search
 */

namespace Automattic\Jetpack\Search;

/**
 * Class that has various methods for outputting functionality into a theme that doesn't support widgets.
 * Additionally the widget itself makes use of these class.
 *
 * @since 5.8.0
 */
class Template_Tags {

	/**
	 * Renders all available filters that can be used to filter down search results on the frontend.
	 *
	 * @since 5.8.0
	 *
	 * @param array $filters    The available filters for the current query.
	 * @param array $post_types An array of post types to make filterable.
	 */
	public static function render_available_filters( $filters = null, $post_types = null ) {
		if ( is_null( $filters ) ) {
			// TODO: Must be migrated to use Classic_Search once the migration is underway.
			$filters = \Jetpack_Search::instance()->get_filters();
		}

		if ( is_null( $post_types ) ) {
			$post_types = get_post_types( array( 'exclude_from_search' => false ) );
		}

		/**
		 * If the post types specified by the widget differ from the default set of searchable post types,
		 * then we need to track their state.
		 */
		$active_post_types = array();
		if ( Helper::post_types_differ_searchable( $post_types ) ) {
			// get the active filter buckets from the query.
			// TODO: Must be migrated to use Classic_Search once the migration is underway.
			$active_buckets          = \Jetpack_Search::instance()->get_active_filter_buckets();
			$post_types_differ_query = Helper::post_types_differ_query( $post_types );

			// remove any post_type filters from display if the current query
			// already specifies to match all post types.
			if ( ! $post_types_differ_query ) {
				$active_buckets = array_filter( $active_buckets, array( __CLASS__, 'is_not_post_type_filter' ) );
			}

			$active_post_types = Helper::get_active_post_types( $active_buckets );
			if ( empty( $active_post_types ) ) {
				$active_post_types = $post_types;
			}

			if ( $post_types_differ_query ) {
				$filters = Helper::ensure_post_types_on_remove_url( $filters, $post_types );
			} else {
				$filters = Helper::remove_active_from_post_type_buckets( $filters );
			}
		} else {
			$post_types = array();
		}

		foreach ( (array) $filters as $filter ) {
			if ( 'post_type' === $filter['type'] ) {
				self::render_filter( $filter, $post_types );
			} else {
				self::render_filter( $filter, $active_post_types );
			}
		}
	}

	/**
	 * Renders filters for instant search.
	 *
	 * @param array $filters    The available filters for the current query.
	 */
	public static function render_instant_filters( $filters = null ) {
		if ( is_null( $filters ) ) {
			// TODO: Must be migrated to use Classic_Search once the migration is underway.
			$filters = \Jetpack_Search::instance()->get_filters();
		}

		foreach ( (array) $filters as $filter ) {
			self::render_instant_filter( $filter );
		}
	}

	/**
	 * Renders a single filter that can be applied to the current search.
	 *
	 * @since 5.8.0
	 *
	 * @param array $filter             The filter to render.
	 * @param array $default_post_types The default post types for this filter.
	 */
	public static function render_filter( $filter, $default_post_types ) {
		if ( empty( $filter ) || empty( $filter['buckets'] ) ) {
			return;
		}

		$query_vars = null;
		foreach ( $filter['buckets'] as $item ) {
			if ( $item['active'] ) {
				$query_vars = array_keys( $item['query_vars'] );
				break;
			}
		}
		$clear_url = null;
		if ( ! empty( $query_vars ) ) {
			$clear_url = Helper::remove_query_arg( $query_vars );
			if ( ! empty( $default_post_types ) ) {
				$clear_url = Helper::add_post_types_to_url( $clear_url, $default_post_types );
			}
		}

		?>
		<h4 class="jetpack-search-filters-widget__sub-heading">
			<?php echo esc_html( $filter['name'] ); ?>
		</h4>
		<?php if ( $clear_url ) : ?>
			<div class="jetpack-search-filters-widget__clear">
				<a href="<?php echo esc_url( $clear_url ); ?>">
					<?php esc_html_e( '< Clear Filters', 'jetpack-search-pkg' ); ?>
				</a>
			</div>
		<?php endif; ?>
		<ul class="jetpack-search-filters-widget__filter-list">
			<?php
			foreach ( $filter['buckets'] as $item ) :
				$url = ( empty( $item['active'] ) ) ? $item['url'] : $item['remove_url'];
				?>
				<li>
					<label>
						<input type="checkbox"<?php checked( ! empty( $item['active'] ) ); ?> disabled="disabled" />&nbsp;
						<a href="<?php echo esc_url( $url ); ?>">
							<?php
								echo esc_html( $item['name'] );
								echo '&nbsp;';
								echo esc_html(
									sprintf(
										'(%s)',
										number_format_i18n( absint( $item['count'] ) )
									)
								);
							?>
						</a>
					</label>
				</li>
			<?php endforeach; ?>
		</ul>
		<?php
	}

	/**
	 * Renders a single filter for instant search.
	 *
	 * @since 8.3.0
	 *
	 * @param array $filter             The filter to render.
	 */
	public static function render_instant_filter( $filter ) {
		if ( empty( $filter ) || empty( $filter['buckets'] ) ) {
			return;
		}

		$data_base = '';
		$qv        = $filter['buckets'][0]['query_vars'];
		$tax_key   = '';
		switch ( $filter['buckets'][0]['type'] ) {
			case 'taxonomy':
				$data_base = 'data-filter-type="' . esc_attr( $filter['buckets'][0]['type'] ) . '" ';
				$tax_key   = key( $qv );
				if ( 'category_name' === $tax_key ) {
					$data_base .= 'data-taxonomy="category"';
				} elseif ( 'tag' === $tax_key ) {
					$data_base .= 'data-taxonomy="post_tag"';
				} else {
					$data_base .= 'data-taxonomy="' . esc_attr( $tax_key ) . '"';
				}
				break;
			case 'post_type':
				$data_base = 'data-filter-type="post_types" ';
				break;
			case 'date_histogram':
				if ( $filter['buckets'][0]['query_vars']['monthnum'] ) {
					$data_base = 'data-filter-type="month_post_date" ';
				} else {
					$data_base = 'data-filter-type="year_post_date" ';
				}
				break;
		}

		?>
		<h4 class="jetpack-search-filters-widget__sub-heading">
			<?php echo esc_html( $filter['name'] ); ?>
		</h4>
		<ul class="jetpack-search-filters-widget__filter-list">
			<?php
			foreach ( $filter['buckets'] as $item ) :
				$data_str = $data_base . ' ';
				switch ( $filter['buckets'][0]['type'] ) {
					case 'taxonomy':
						$data_str .= 'data-val="' . esc_attr( $item['query_vars'][ $tax_key ] ) . '"';
						break;
					case 'post_type':
						$data_str .= 'data-val="' . esc_attr( $item['query_vars']['post_type'] ) . '"';
						break;
					case 'date_histogram':
						if ( $item['query_vars']['monthnum'] ) {
							$d = sprintf( '%d-%02d-01 00:00:00', $item['query_vars']['year'], $item['query_vars']['monthnum'] );
						} else {
							$d = sprintf( '%d-01-01 00:00:00', $item['query_vars']['year'] );
						}
						$data_str .= 'data-val="' . esc_attr( $d ) . '" ';
						break;
				}
				?>
				<li>
				<?php // TODO: Figure out how to properly escape $data_str below. ?>
				<a href="#" class="jetpack-search-filter__link" <?php echo $data_str; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
						<?php
							echo esc_html( $item['name'] );
							echo '&nbsp;';
							echo esc_html(
								sprintf(
									'(%s)',
									number_format_i18n( absint( $item['count'] ) )
								)
							);
						?>
					</a>
				</li>
			<?php endforeach; ?>
		</ul>
		<?php
	}

	/**
	 * Outputs the search widget's title.
	 *
	 * @since 5.8.0
	 *
	 * @param string $title        The widget's title.
	 * @param string $before_title The HTML tag to display before the title.
	 * @param string $after_title  The HTML tag to display after the title.
	 */
	public static function render_widget_title( $title, $before_title, $after_title ) {
		// This doesn't need to be escaped because it's provided by WP and goes through filters, where it is not escaped.
		echo $before_title . $title . $after_title; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}

	/**
	 * Responsible for rendering the search box within our widget on the frontend.
	 *
	 * @since 5.8.0
	 *
	 * @param array  $post_types Array of post types to limit search results to.
	 * @param string $orderby    How to order the search results.
	 * @param string $order      In what direction to order the search results.
	 */
	public static function render_widget_search_form( $post_types, $orderby, $order ) {
		$form = get_search_form( false );

		$fields_to_inject = array(
			'orderby' => $orderby,
			'order'   => $order,
		);

		// If the widget has specified post types to search within and IF the post types differ
		// from the default post types that would have been searched, set the selected post
		// types via hidden inputs.
		if ( Helper::post_types_differ_searchable( $post_types ) ) {
			$fields_to_inject['post_type'] = implode( ',', $post_types );
		}

		$form = self::inject_hidden_form_fields( $form, $fields_to_inject );

		echo '<div class="jetpack-search-form">';
		// TODO: Figure out how to properly escape this.
		echo $form; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo '</div>';
	}

	/**
	 * Modifies an HTML form to add some additional hidden fields.
	 *
	 * @since 5.8.0
	 *
	 * @param string $form   The form HTML to modify.
	 * @param array  $fields Array of hidden fields to add. Key is field name and value is the field value.
	 *
	 * @return string The modified form HTML.
	 */
	private static function inject_hidden_form_fields( $form, $fields ) {
		$form_injection = '';

		foreach ( $fields as $field_name => $field_value ) {
			$form_injection .= sprintf(
				'<input type="hidden" name="%s" value="%s" />',
				esc_attr( $field_name ),
				esc_attr( $field_value )
			);
		}

		// This shouldn't need to be escaped since we've escaped above as we built $form_injection.
		$form = str_replace(
			'</form>',
			$form_injection . '</form>',
			$form
		);

		return $form;
	}

	/**
	 * Internal method for filtering out non-post_type filters.
	 *
	 * @since 5.8.0
	 *
	 * @param array $filter Filter object.
	 *
	 * @return bool
	 */
	private static function is_not_post_type_filter( $filter ) {
		return 'post_type' !== $filter['type'];
	}
}