PluginProbe
WP Ultimate Post Grid / 4.1.1
WP Ultimate Post Grid v4.1.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / includes / public / class-wpupg-grid-output.php

class-wpupg-grid-output.php in WP Ultimate Post Grid 4.1.1, at includes/public/class-wpupg-grid-output.php

520 lines 15.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Responsible for outputting grids.
4 *
5 * @link https://bootstrapped.ventures
6 * @since 3.0.0
7 *
8 * @package WP_Ultimate_Post_Grid
9 * @subpackage WP_Ultimate_Post_Grid/includes/public
10 */
11
12 /**
13 * Responsible for outputting grids.
14 *
15 * @since 3.0.0
16 * @package WP_Ultimate_Post_Grid
17 * @subpackage WP_Ultimate_Post_Grid/includes/public
18 * @author Brecht Vandersmissen <brecht@bootstrapped.ventures>
19 */
20 class WPUPG_Grid_Output {
21
22 /**
23 * Current grid getting output.
24 *
25 * @since 3.8.0
26 * @access private
27 * @var array $current_grid mixed Current grid getting output.
28 */
29 private static $current_grid = false;
30
31 /**
32 * Get the current grid getting output.
33 *
34 * @since 3.8.0
35 */
36 public static function get_current_grid() {
37 return self::$current_grid;
38 }
39
40 /**
41 * Output the entire grid.
42 *
43 * @since 3.0.0
44 * @param mixed $grid Grid to output.
45 * @param mixed $args Optional arguments.
46 */
47 public static function entire( $grid, $args = array() ) {
48 $output = '';
49
50 $classes = array(
51 'wpupg-grid-with-filters',
52 );
53
54 if ( isset( $args['align'] ) && $args['align'] ) {
55 $classes[] = 'align' . $args['align'];
56 }
57 unset( $args['align'] );
58
59 // Display filters on side.
60 if ( 'left' === $grid->filters_style( 'display' ) || 'right' === $grid->filters_style( 'display' ) ) {
61 $classes[] = 'wpupg-grid-with-filters-side';
62 $classes[] = 'wpupg-grid-with-filters-side-' . $grid->filters_style( 'display' );
63
64 $output .= '<style>';
65 $output .= '#wpupg-grid-with-filters-' . $grid->slug_or_id() . ' .wpupg-grid-filters {';
66 $output .= 'flex-basis: ' . $grid->filters_style( 'width' ) . 'px;';
67 $output .= '}';
68 $output .= '</style>';
69 }
70
71 $output .= '<div id="wpupg-grid-with-filters-' . esc_attr( $grid->slug_or_id() ) . '" class="' . esc_attr( implode( ' ', $classes ) ) . '">';
72
73 $output .= self::filters( $grid, $args );
74 $output .= self::grid( $grid, $args );
75
76 $output .= '</div>';
77
78 return $output;
79 }
80
81 /**
82 * Output filters for a specific grid.
83 *
84 * @since 3.0.0
85 * @param mixed $grid Grid to output.
86 * @param mixed $args Optional arguments.
87 */
88 public static function filters( $grid, $args = array() ) {
89 $output = '';
90
91 if ( $grid->filters_enabled() ) {
92 $filters = $grid->filters();
93
94 $classes = array(
95 'wpupg-grid-filters',
96 'wpupg-grid-filters-display-' . $grid->filters_style( 'display' ),
97 );
98
99 if ( 'inline' === $grid->filters_style( 'display' ) ) {
100 $classes[] = 'wpupg-grid-filters-align-' . $grid->filters_style( 'alignment' );
101
102 $output .= '<style>';
103 $output .= '#wpupg-grid-' . $grid->slug_or_id() . '-filters .wpupg-filter-container {';
104 $output .= 'padding: ' . $grid->filters_style( 'spacing_vertical' ) . 'px ' . $grid->filters_style( 'spacing_horizontal' ) . 'px;';
105 $output .= '}';
106 $output .= '</style>';
107 }
108
109 $output .= '<div id="wpupg-grid-' . esc_attr( $grid->slug_or_id() ) . '-filters" class="' . esc_attr( implode( ' ', $classes ) ) . '">';
110
111 foreach ( $filters as $index => $filter ) {
112 // Make sure filter ID is set.
113 if ( ! $filter['id'] ) {
114 $filter['id'] = $index + 1;
115 }
116
117 $output .= self::filter( $grid, $filter, $args );
118 }
119
120 $output .= '</div>';
121 }
122
123 return $output;
124 }
125
126 /**
127 * Output a specific filter for a specific grid.
128 *
129 * @since 3.0.0
130 * @param mixed $grid Grid to output.
131 * @param mixed $filter Filter to output.
132 * @param mixed $args Optional arguments.
133 */
134 public static function filter( $grid, $filter, $args = array() ) {
135 $output = '';
136
137 $filter_output = apply_filters( 'wpupg_output_filter', '', $grid, $filter, $args );
138 if ( $filter_output ) {
139 $container_classes = array(
140 'wpupg-filter-container',
141 'wpupg-filter-container-label-' . $grid->filters_style( 'label_display' ),
142 );
143
144 if ( isset( $args['align'] ) && $args['align'] ) {
145 $container_classes[] = 'align' . $args['align'];
146 }
147
148 // Responsive options.
149 $using_toggle = false;
150
151 foreach ( array( 'desktop', 'tablet', 'mobile' ) as $size ) {
152 $behaviour = isset( $filter['options']['responsive'] ) && isset( $filter['options']['responsive'][ $size ] ) ? $filter['options']['responsive'][ $size ] : 'show';
153
154 if ( 'show' !== $behaviour ) {
155 $container_classes[] = 'wpupg-filter-container-' . $size . '-' . esc_attr( $behaviour );
156 }
157
158 if ( 'toggle' === substr( $behaviour, 0, 6 ) ) {
159 $container_classes[] = 'wpupg-filter-container-' . $size . '-toggle';
160 $using_toggle = true;
161 }
162 }
163
164 $output .= '<div id="wpupg-grid-' . esc_attr( $grid->slug_or_id() ) . '-filter-' . $filter['id'] . '-container" class="' . esc_attr( implode( ' ', $container_classes ) ) .'">';
165
166 // Optional label output.
167 if ( ( isset( $filter['label'] ) && $filter['label'] ) || $using_toggle ) {
168 $label_classes = array(
169 'wpupg-filter-label',
170 'wpupg-filter-label-' . $grid->filters_style( 'label_style' ),
171 'wpupg-filter-label-align-' . $grid->filters_style( 'label_alignment' ),
172 );
173
174 $output .= '<div id="wpupg-grid-' . esc_attr( $grid->slug_or_id() ) . '-filter-' . $filter['id'] . '-label" class="' . esc_attr( implode( ' ', $label_classes ) ) .'">';
175
176 if ( $using_toggle ) {
177 $output .= '<span class="wpupg-filter-toggle-container">';
178
179 switch ( $grid->responsive_toggle_style() ) {
180 case 'arrow':
181 $icon_closed_svg = 'up-arrow.svg';
182 $icon_open_svg = 'down-arrow.svg';
183 break;
184 case 'triangle':
185 $icon_closed_svg = 'triangle-right.svg';
186 $icon_open_svg = 'triangle-down.svg';
187 break;
188 case 'plus':
189 $icon_closed_svg = 'plus.svg';
190 $icon_open_svg = 'minus.svg';
191 break;
192 case 'custom':
193 default:
194 $icon_closed_svg = false;
195 $icon_open_svg = false;
196
197 $icon_closed = $grid->responsive_toggle_style_closed();
198 $icon_open = $grid->responsive_toggle_style_open();
199 break;
200 }
201
202 // Load svg icons.
203 if ( $icon_closed_svg ) {
204 ob_start();
205 include( WPUPG_DIR . 'assets/icons/public/' . $icon_closed_svg );
206 $icon_closed = ob_get_contents();
207 ob_end_clean();
208 }
209 if ( $icon_open_svg ) {
210 ob_start();
211 include( WPUPG_DIR . 'assets/icons/public/' . $icon_open_svg );
212 $icon_open = ob_get_contents();
213 ob_end_clean();
214 }
215
216 $output .= '<span class="wpupg-filter-toggle-closed" aria-label="' . __( 'Show filter options', 'wp-ultimate-post-grid' ) . '" tabindex="0" role="button">' . $icon_closed . '</span>';
217 $output .= '<span class="wpupg-filter-toggle-open" aria-label="' . __( 'Hide filter options', 'wp-ultimate-post-grid' ) . '" tabindex="0" role="button">' . $icon_open . '</span>';
218 $output .= '</span>';
219 }
220
221 $output .= $filter['label'];
222 $output .= '</div>';
223
224 $output .= '<style>';
225 $output .= '#wpupg-grid-' . $grid->slug_or_id() . '-filter-' . $filter['id'] . '-label {';
226 $output .= 'font-size: ' . $grid->filters_style( 'label_font_size' ) . 'px;';
227 $output .= '}';
228 $output .= '</style>';
229 }
230
231 // Filter output.
232 $filter_classes = array(
233 'wpupg-filter',
234 'wpupg-filter-' . esc_attr( $filter['type'] ),
235 );
236
237 if ( 'dynamic_order' === $filter['type'] ) {
238 $filter_classes[] = 'wpupg-filter-dynamic-order-' . $filter['options']['display'];
239 }
240
241 $output .= '<div id="wpupg-grid-' . esc_attr( $grid->slug_or_id() ) . '-filter-' . $filter['id'] . '" class="' . esc_attr( implode( ' ', $filter_classes ) ) .'">';
242 $output .= $filter_output;
243 $output .= '</div>';
244
245 $output .= '</div>';
246 }
247
248 return $output;
249 }
250
251 /**
252 * Output a specific grid.
253 *
254 * @since 3.0.0
255 * @param mixed $grid Grid to output.
256 * @param mixed $args Optional arguments.
257 */
258 public static function grid( $grid, $args = array() ) {
259 $output = '';
260 $style = array();
261
262 $classes = array(
263 'wpupg-grid-container',
264 );
265
266 if ( isset( $args['align'] ) && $args['align'] ) {
267 $classes[] = 'align' . $args['align'];
268 }
269
270 $output = '<div id="wpupg-grid-container-' . $grid->slug_or_id() . '" class="' . esc_attr( implode( ' ', $classes ) ) . '">';
271
272 // Add styling.
273 $is_preview = isset( $args['preview'] ) && $args['preview'];
274 $output .= WPUPG_Grid_Layout::get_style( $grid, $is_preview );
275
276 // Add template styling if preview.
277 if ( $is_preview ) {
278 $template_css = WPUPG_Template_Manager::get_template_css( $grid->template() );
279 $output .= '<style>' . $template_css . '</style>';
280 } else {
281 // Make sure template CSS gets loaded, even if there aren't any grid items yet.
282 $template = WPUPG_Template_Manager::get_template_by_slug( $grid->template() );
283 WPUPG_Template_Manager::add_used_template( $template );
284 }
285
286 // Add grid output.
287 if ( ! isset( $args['type'] ) ) {
288 $args['type'] = 'initial';
289 }
290
291 $items = self::items( $grid, $args );
292 $output .= '<div id="wpupg-grid-' . esc_attr( $grid->slug_or_id() ) . '" class="wpupg-grid wpupg-grid-loading" data-grid-id="' . $grid->id() . '">' . $items['html'] . '</div>';
293
294 // Optional empty message.
295 if ( $grid->empty_message() ) {
296 $output .= '<div id="wpupg-grid-' . esc_attr( $grid->slug_or_id() ) . '-empty" class="wpupg-grid-empty">' . $grid->empty_message() . '</div>';
297 }
298
299 // Output pagination.
300 $pagination_output = apply_filters( 'wpupg_output_pagination', '', $grid, $args );
301 if ( $pagination_output ) {
302 $output .= '<div id="wpupg-grid-' . esc_attr( $grid->slug_or_id() ) . '-pagination" class="wpupg-pagination wpupg-pagination-' . esc_attr( $grid->pagination_type() ) . '">';
303 $output .= $pagination_output;
304 $output .= '</div>';
305 }
306
307 // Optionally output metadata.
308 if ( $grid->metadata() && 1 < count( $items['metadata'] ) ) {
309 $output .= self::metadata( $grid, $items );
310 }
311
312 $output .= '</div>';
313
314 return $output;
315 }
316
317 /**
318 * Output the grid metadata.
319 *
320 * @since 3.6.0
321 * @param mixed $grid Grid to output the metadata for.
322 * @param mixed $items Items metadata to output.
323 */
324 public static function metadata( $grid, $items = array() ) {
325 $metadata = array(
326 '@context' => 'http://schema.org',
327 '@type' => 'ItemList',
328 'itemListElement' => array(),
329 'numberOfItems' => count( $items['metadata'] ),
330 );
331
332 // Add link to current post.
333 $post = get_post();
334 if ( $post ) {
335 $metadata['url'] = get_permalink( $post );
336 }
337
338 // Optionally add name and description.
339 if ( $grid->metadata_name() ) {
340 $metadata['name'] = wp_strip_all_tags( $grid->metadata_name() );
341 }
342 if ( $grid->metadata_description() ) {
343 $metadata['description'] = wp_strip_all_tags( $grid->metadata_description() );
344 }
345
346 // Add items.
347 foreach ( $items['metadata'] as $item ) {
348 $metadata['itemListElement'][] = $item;
349 }
350
351 return '<script type="application/ld+json">' . wp_json_encode( $metadata ) . '</script>';
352 }
353
354 /**
355 * Output the grid items.
356 *
357 * @since 3.0.0
358 * @param mixed $grid Grid to output the items for.
359 * @param mixed $args Optional arguments.
360 */
361 public static function items( $grid, $args = array() ) {
362 $metadata = array();
363 $output = '';
364
365 // Get current postdata.
366 global $post;
367 $current_post = $post;
368
369 $counter = 1;
370 $ids = $grid->ids( $args );
371 foreach ( $ids as $id ) {
372 $item = self::item( $grid, $id, $args, $counter );
373
374 $output .= $item['output'];
375
376 if ( isset( $item['url'] ) && $item['url'] ) {
377 $metadata[] = array(
378 '@type' => 'ListItem',
379 'position' => $counter,
380 'url' => $item['url'],
381 );
382
383 $counter++;
384 }
385 }
386
387 // Reset postdata to before the grid output.
388 $post = $current_post;
389 setup_postdata( $post );
390
391 return array(
392 'ids' => $ids,
393 'html' => $output,
394 'metadata' => $metadata,
395 );
396 }
397
398 /**
399 * Output a grid item.
400 *
401 * @since 3.0.0
402 * @param mixed $grid Grid to output the items for.
403 * @param mixed $item_id ID of the item to output.
404 * @param mixed $args Optional arguments.
405 * @param mixed $counter Optional counter.
406 */
407 public static function item( $grid, $item_id, $args = array(), $counter = 0 ) {
408 self::$current_grid = $grid;
409
410 $output = '';
411 $url = false;
412
413 $item = WPUPG_Item_Manager::get_item( $item_id, $grid->type() );
414 $item = apply_filters( 'wpupg_output_item', $item, $grid, $args );
415
416 if ( $item ) {
417 // Set gobal post object.
418 if ( is_a( $item, 'WPUPG_Item_Post' ) ) {
419 global $post;
420 $post = $item->post();
421 setup_postdata( $post );
422 }
423
424 // Get classes.
425 $classes = $item->classes();
426
427 $template = apply_filters( 'wpupg_output_item_template', $grid->template(), $grid );
428 $classes[] = 'wpupg-template-' . $template;
429
430 // Taxonomy terms.
431 $item_terms = $grid->get_terms_for_item( $item_id );
432
433 foreach ( $item_terms as $taxonomy => $terms ) {
434 foreach ( $terms['terms'] as $term ) {
435 $classes[] = 'wpupg-tax-' . $taxonomy . '-' . $term;
436 $classes[] = 'wpupg-parent-tax-' . $taxonomy . '-' . $term;
437 }
438 foreach ( $terms['parent_terms'] as $term ) {
439 $classes[] = 'wpupg-parent-tax-' . $taxonomy . '-' . $term;
440 }
441 }
442
443 // Get data.
444 $data = array();
445 $data['id'] = $item_id;
446
447 // Custom fields.
448 $custom_fields = $grid->filters_custom_fields();
449
450 foreach ( $custom_fields as $custom_field ) {
451 $custom_field_value = $item->custom_field( $custom_field );
452
453 if ( is_string( $custom_field_value ) ) {
454 $data[ 'wpupg-cf-' . $custom_field ] = $custom_field_value;
455 }
456 }
457
458 $classes = apply_filters( 'wpupg_output_item_classes', $classes, $grid, $item, $args );
459 $data = apply_filters( 'wpupg_output_item_data', $data, $grid, $item, $args );
460 $html = apply_filters( 'wpupg_output_item_html', WPUPG_Template_Manager::get_template( $item, $template ), $template, $item, $classes );
461
462 // Prevent duplicate classes.
463 $classes = array_unique( $classes );
464
465 // Construct data string.
466 $data_string = '';
467 foreach ( $data as $data_key => $data_value ) {
468 $data_string .= ' data-' . $data_key . '="' . esc_attr( $data_value ) . '"';
469 }
470
471 // Don't actually add link when previewing.
472 if ( isset( $args['preview'] ) && $args['preview'] && $item->link( $grid ) ) {
473 return array(
474 'output' => '<div class="' . esc_attr( implode( ' ', $classes ) ) . '" style="cursor: pointer;"' . $data_string .'>' . $html . '</div>',
475 'metadata' => false,
476 );
477 }
478
479 // Check if this item has a link.
480 $link = false;
481 if ( $item->link( $grid ) ) {
482 $link = 'image' === $grid->link_type() ? $item->image_url( 'full' ) : $item->url();
483 $link = apply_filters( 'wpupg_output_item_link', $link, $grid, $item, $args );
484 }
485
486 if ( $link ) {
487 $classes[] = 'wpupg-item-link';
488
489 // Optional rel attribute.
490 $rel = '';
491 if ( 'image' === $grid->link_type() ) {
492 $rel = ' rel="lightbox"';
493 } elseif( 'nofollow' === $item->link_nofollow() ) {
494 $rel = ' rel="nofollow"';
495 }
496
497 // Strip any other links in HTML.
498 $html = str_ireplace( '<a ', '<span ', $html );
499 $html = str_ireplace( '<a', '<span', $html );
500 $html = str_ireplace( '</a>', '</span>', $html );
501
502 $output = '<a class="' . esc_attr( implode( ' ', $classes ) ) . '" href="' . esc_attr( $link ) . '" target="' . esc_attr( $item->link_target( $grid ) ) . '"' . $rel . '' . $data_string .'>' . $html . '</a>';
503 $url = $link;
504 } else {
505 $output = '<div class="' . esc_attr( implode( ' ', $classes ) ) . '"' . $data_string .'>' . $html . '</div>';
506 }
507 }
508
509 // Allow filtering of full item output.
510 $output = apply_filters( 'wpupg_output_item_container', $output, $grid, $args, $counter );
511
512 self::$current_grid = false;
513
514 return array(
515 'output' => $output,
516 'url' => $url,
517 );
518 }
519 }
520