PluginProbe
WP Ultimate Post Grid / 2.8.2
WP Ultimate Post Grid v2.8.2
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 / helpers / models / grid.php

grid.php in WP Ultimate Post Grid 2.8.2, at helpers/models/grid.php

681 lines 19.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WPUPG_Grid {
4
5 private $post;
6 private $meta;
7 private $fields = array(
8 'wpupg_centered',
9 'wpupg_empty_message',
10 'wpupg_images_only',
11 'wpupg_filter_count',
12 'wpupg_filter_inverse',
13 'wpupg_filter_limit',
14 'wpupg_filter_limit_exclude',
15 'wpupg_filter_match_parents',
16 'wpupg_filter_multiselect',
17 'wpupg_filter_multiselect_type',
18 'wpupg_filter_show_empty',
19 'wpupg_filter_type',
20 'wpupg_pagination_type',
21 'wpupg_post_types',
22 'wpupg_layout_mode',
23 'wpupg_limit_terms',
24 'wpupg_limit_posts',
25 'wpupg_limit_posts_number',
26 'wpupg_limit_posts_offset',
27 'wpupg_link_target',
28 'wpupg_link_type',
29 'wpupg_order_by',
30 'wpupg_order',
31 'wpupg_order_custom_key',
32 'wpupg_order_custom_key_numeric',
33 'wpupg_taxonomies',
34 'wpupg_template',
35 'wpupg_terms_hide_empty',
36 'wpupg_terms_images_only',
37 'wpupg_terms_order_by',
38 'wpupg_terms_order',
39 'wpupg_type',
40 );
41
42 // Pagination fields with defaults
43 private $pagination_fields = array(
44 'pages' => array(
45 'posts_per_page' => 20,
46 ),
47 'infinite_load' => array(
48 'initial_posts' => 20,
49 'posts_on_scroll' => 20,
50 ),
51 'load_more' => array(
52 'initial_posts' => 20,
53 'posts_on_click' => 20,
54 'button_text' => 'Load More',
55 ),
56 'load_filter' => array(
57 'initial_posts' => 20,
58 ),
59 );
60
61 private $pagination_style_fields = array(
62 'background_color' => '#2E5077',
63 'background_active_color' => '#1C3148',
64 'background_hover_color' => '#1C3148',
65 'text_color' => '#FFFFFF',
66 'text_active_color' => '#FFFFFF',
67 'text_hover_color' => '#FFFFFF',
68 'border_color' => '#1C3148',
69 'border_active_color' => '#1C3148',
70 'border_hover_color' => '#1C3148',
71 'border_width' => '1',
72 'margin_vertical' => '5',
73 'margin_horizontal' => '5',
74 'padding_vertical' => '5',
75 'padding_horizontal' => '10',
76 'alignment' => 'left',
77 );
78
79 // Filter style fields with defaults
80 private $filter_style_fields = array(
81 'isotope' => array(
82 'background_color' => '#2E5077',
83 'background_active_color' => '#1C3148',
84 'background_hover_color' => '#1C3148',
85 'text_color' => '#FFFFFF',
86 'text_active_color' => '#FFFFFF',
87 'text_hover_color' => '#FFFFFF',
88 'border_color' => '#1C3148',
89 'border_active_color' => '#1C3148',
90 'border_hover_color' => '#1C3148',
91 'border_width' => '1',
92 'margin_vertical' => '5',
93 'margin_horizontal' => '5',
94 'padding_vertical' => '5',
95 'padding_horizontal' => '10',
96 'alignment' => 'left',
97 'all_button_text' => 'Check Constructor',
98 'term_order' => 'alphabetical',
99 ),
100 'text' => array(
101 'placeholder_text' => '',
102 ),
103 );
104
105 private $dynamically_generated = false;
106
107 public function __construct( $post )
108 {
109 // Get associated post
110 if( is_object( $post ) && $post instanceof WP_Post ) {
111 $this->post = $post;
112 } else if( is_numeric( $post ) ) {
113 $this->post = get_post( $post );
114 } else {
115 throw new InvalidArgumentException( 'Grids can only be instantiated with a Post object or Post ID.' );
116 }
117
118 // Get metadata
119 $this->meta = get_post_custom( $this->post->ID );
120
121 // Defaults with expressions
122 $this->filter_style_fields['isotope']['all_button_text'] = __( 'All', 'wp-ultimate-post-grid' );
123 }
124
125 public function regenerate()
126 {
127 $generated = WPUltimatePostGrid::get()->helper( 'grid_cache' )->dynamic_generate( $this );
128
129 $this->dynamically_generated = true;
130 $this->meta['wpupg_posts'][0] = serialize( $generated['cache'] );
131 $this->meta['wpupg_filter'][0] = $generated['filter'];
132 }
133
134 public function is_present( $field )
135 {
136 switch( $field ) {
137 default:
138 $val = $this->meta( $field );
139 return isset( $val ) && trim( $val ) != '';
140 }
141 }
142
143 public function meta( $field )
144 {
145 if( isset( $this->meta[$field] ) ) {
146 return $this->meta[$field][0];
147 }
148
149 return null;
150 }
151
152 public function fields()
153 {
154 return $this->fields;
155 }
156
157 public function filter_style_fields()
158 {
159 return $this->filter_style_fields;
160 }
161
162 public function pagination_fields()
163 {
164 return $this->pagination_fields;
165 }
166
167 public function pagination_style_fields()
168 {
169 return $this->pagination_style_fields;
170 }
171
172 /**
173 * Grid fields
174 */
175
176 public function centered()
177 {
178 return $this->meta( 'wpupg_centered' );
179 }
180
181 public function empty_message()
182 {
183 return $this->meta( 'wpupg_empty_message' );
184 }
185
186 public function filter()
187 {
188 // No cache mode.
189 if ( false === $this->dynamically_generated && WPUltimatePostGrid::option( 'use_cache', '1' ) !== '1' ) {
190 $this->regenerate();
191 }
192
193 return $this->meta( 'wpupg_filter' );
194 }
195
196 public function filter_taxonomies()
197 {
198 $filter_taxonomies = maybe_unserialize( $this->meta( 'wpupg_filter_taxonomies' ) );
199 return is_array( $filter_taxonomies ) ? $filter_taxonomies : array();
200 }
201
202 public function filter_style()
203 {
204 $filter_style = maybe_unserialize( $this->meta( 'wpupg_filter_style' ) );
205 $filter_style = is_array( $filter_style ) ? $filter_style : array();
206
207 // Set defaults
208 foreach( $this->filter_style_fields() as $type => $defaults ) {
209 $filter_style[$type] = isset( $filter_style[$type] ) ? $filter_style[$type] + $defaults : $defaults;
210 }
211
212 return $filter_style;
213 }
214
215 public function filter_dropdown_labels()
216 {
217 $dropdown_labels = maybe_unserialize( $this->meta( 'wpupg_filter_dropdown_labels' ) );
218 return is_array( $dropdown_labels ) ? $dropdown_labels : array();
219 }
220
221 public function filter_count()
222 {
223 if( WPUltimatePostGrid::is_premium_active() ) {
224 return $this->meta( 'wpupg_filter_count' );
225 } else {
226 return false;
227 }
228 }
229
230 public function filter_inverse()
231 {
232 return $this->meta( 'wpupg_filter_inverse' );
233 }
234
235 public function filter_limit()
236 {
237 return $this->meta( 'wpupg_filter_limit' );
238 }
239
240 public function filter_limit_exclude()
241 {
242 return $this->meta( 'wpupg_filter_limit_exclude' );
243 }
244
245 public function filter_limit_terms()
246 {
247 $limit_terms = maybe_unserialize( $this->meta( 'wpupg_filter_limit_terms' ) );
248 return is_array( $limit_terms ) ? $limit_terms : array();
249 }
250
251 public function filter_match_parents()
252 {
253 return $this->meta( 'wpupg_filter_match_parents' );
254 }
255
256 public function filter_multiselect()
257 {
258 if( WPUltimatePostGrid::is_premium_active() ) {
259 return $this->meta( 'wpupg_filter_multiselect' );
260 } else {
261 return false;
262 }
263 }
264
265 public function filter_multiselect_type()
266 {
267 return $this->meta( 'wpupg_filter_multiselect_type' );
268 }
269
270 public function filter_show_empty()
271 {
272 return $this->meta( 'wpupg_filter_show_empty' );
273 }
274
275 public function filter_type()
276 {
277 return $this->meta( 'wpupg_filter_type' );
278 }
279
280 public function ID()
281 {
282 return $this->post->ID;
283 }
284
285 public function images_only()
286 {
287 return $this->meta( 'wpupg_images_only' );
288 }
289
290 public function layout_mode()
291 {
292 $layout_mode = $this->meta( 'wpupg_layout_mode' );
293 return $layout_mode ? $layout_mode : 'masonry';
294 }
295
296 public function limit_terms()
297 {
298 return $this->meta( 'wpupg_limit_terms' );
299 }
300
301 public function limit_terms_terms()
302 {
303 $limit_terms_terms = maybe_unserialize( $this->meta( 'wpupg_limit_terms_terms' ) );
304 return is_array( $limit_terms_terms ) ? $limit_terms_terms : array();
305 }
306
307 public function limit_terms_type()
308 {
309 return $this->meta( 'wpupg_limit_terms_type' );
310 }
311
312 public function limit_posts()
313 {
314 return $this->meta( 'wpupg_limit_posts' );
315 }
316
317 public function limit_posts_number()
318 {
319 $limit = intval( $this->meta( 'wpupg_limit_posts_number' ) );
320 return $limit > 0 ? $limit : '';
321 }
322
323 public function limit_posts_offset()
324 {
325 return intval( $this->meta( 'wpupg_limit_posts_offset' ) );
326 }
327
328 public function limit_rules()
329 {
330 $limit_rules = maybe_unserialize( $this->meta( 'wpupg_limit_rules' ) );
331 return is_array( $limit_rules ) ? $limit_rules : array();
332 }
333
334 public function link_target()
335 {
336 $link_target = $this->meta( 'wpupg_link_target' );
337 return $link_target ? $link_target : 'post';
338 }
339
340 public function link_type()
341 {
342 $link_type = $this->meta( 'wpupg_link_type' );
343 return $link_type ? $link_type : '_self';
344 }
345
346 public function order()
347 {
348 return $this->meta( 'wpupg_order' );
349 }
350
351 public function order_by()
352 {
353 return $this->meta( 'wpupg_order_by' );
354 }
355
356 public function order_custom_key()
357 {
358 return $this->meta( 'wpupg_order_custom_key' );
359 }
360
361 public function order_custom_key_numeric()
362 {
363 return $this->meta( 'wpupg_order_custom_key_numeric' );
364 }
365
366 public function pagination()
367 {
368 $pagination = maybe_unserialize( $this->meta( 'wpupg_pagination' ) );
369 $pagination = is_array( $pagination ) ? $pagination : array();
370
371 // Set defaults
372 foreach( $this->pagination_fields() as $type => $defaults ) {
373 $pagination[$type] = isset( $pagination[$type] ) ? $pagination[$type] + $defaults : $defaults;
374 }
375
376 return $pagination;
377 }
378
379 public function pagination_style()
380 {
381 $pagination_style = maybe_unserialize( $this->meta( 'wpupg_pagination_style' ) );
382 $pagination_style = is_array( $pagination_style ) ? $pagination_style : array();
383
384 // Set defaults
385 $pagination_style = $pagination_style + $this->pagination_style_fields();
386
387 return $pagination_style;
388 }
389
390 public function pagination_type()
391 {
392 return $this->meta( 'wpupg_pagination_type' );
393 }
394
395 public function posts()
396 {
397 // No cache mode.
398 if ( false === $this->dynamically_generated && WPUltimatePostGrid::option( 'use_cache', '1' ) !== '1' ) {
399 $this->regenerate();
400 }
401
402 $posts = maybe_unserialize( $this->meta( 'wpupg_posts' ) );
403 return is_array( $posts ) ? $posts : array();
404 }
405
406 public function post()
407 {
408 return $this->post;
409 }
410
411 public function post_status()
412 {
413 return in_array( 'attachment', $this->post_types() ) ? array( 'publish', 'inherit' ) : 'publish';
414 }
415
416 public function post_types()
417 {
418 $post_types = maybe_unserialize( $this->meta( 'wpupg_post_types' ) );
419 return is_array( $post_types ) ? $post_types : array();
420 }
421
422 public function slug()
423 {
424 return $this->post->post_name;
425 }
426
427 public function taxonomies()
428 {
429 $taxonomies = maybe_unserialize( $this->meta( 'wpupg_taxonomies' ) );
430 return is_array( $taxonomies ) ? $taxonomies : array();
431 }
432
433 public function template()
434 {
435 return WPUltimatePostGrid::addon( 'custom-templates' )->get_template( $this->template_id() );
436 }
437
438 public function template_id()
439 {
440 return $this->meta( 'wpupg_template' );
441 }
442
443 public function terms_hide_empty()
444 {
445 return $this->meta( 'wpupg_terms_hide_empty' );
446 }
447
448 public function terms_images_only()
449 {
450 return $this->meta( 'wpupg_terms_images_only' );
451 }
452
453 public function terms_order()
454 {
455 return $this->meta( 'wpupg_terms_order' );
456 }
457
458 public function terms_order_by()
459 {
460 return $this->meta( 'wpupg_terms_order_by' );
461 }
462
463 public function title()
464 {
465 return $this->post->post_title;
466 }
467
468 public function type()
469 {
470 $type = $this->meta( 'wpupg_type' );
471 return $type ? $type : 'posts';
472 }
473
474 /**
475 * Helper functions
476 */
477
478 public function set_dynamic_rules( $dynamic_rules )
479 {
480 if( WPUltimatePostGrid::is_premium_active() ) {
481 if ( $this->type() == 'terms' ) {
482 $this->meta['wpupg_limit_terms'][0] = 'on';
483 $this->meta['wpupg_limit_terms_type'][0] = $dynamic_rules['type'];
484 $this->meta['wpupg_limit_terms_terms'][0] = serialize( $dynamic_rules['values'] );
485 } else {
486 if ( false === $this->dynamically_generated || serialize( $dynamic_rules ) !== serialize( $this->dynamically_generated ) ) {
487 $this->meta['wpupg_limit_posts'][0] = 'on';
488
489 // Make sure to get original rules in case this grid was dynamically filtered before.
490 $limit_rules = maybe_unserialize( get_post_meta( $this->ID(), 'wpupg_limit_rules', true ) );
491 $limit_rules = is_array( $limit_rules ) ? $limit_rules : array();
492
493 $new_rules = array_merge( $limit_rules, $dynamic_rules );
494 $this->meta['wpupg_limit_rules'][0] = serialize( $new_rules );
495
496 $this->regenerate();
497 $this->dynamically_generated = $dynamic_rules;
498 }
499 }
500 }
501 }
502
503 public function get_posts( $page = 0, $post_ids = null )
504 {
505 $grid_posts = $this->posts();
506 $post_ids = is_null( $post_ids ) ? $grid_posts['all'] : array_intersect( $post_ids, $grid_posts['all'] );
507
508 if( count( $post_ids ) == 0 ) return array();
509
510 $posts_per_page = -1;
511
512 if( $page !== -2 && $this->pagination_type() == 'pages' ) {
513 $pagination = $this->pagination();
514 $posts_per_page = $pagination['pages']['posts_per_page'];
515 }
516
517 $offset = 0;
518 if( $page > 0 ) {
519 $offset = $page * $posts_per_page;
520 }
521
522 $args = array(
523 'post_type' => $this->post_types(), // "any" doesn't work for private post types.
524 'post_status' => 'any',
525 'order' => $this->order(),
526 'posts_per_page' => $posts_per_page,
527 'offset' => $offset,
528 'post__in' => $post_ids,
529 'ignore_sticky_posts' => true,
530 );
531
532 if( $this->order_by() == 'custom' ) {
533 $args['meta_key'] = $this->order_custom_key();
534 $args['orderby'] = $this->order_custom_key_numeric() ? 'meta_value_num' : 'meta_value';
535 } else {
536 $args['orderby'] = $this->order_by();
537 }
538
539 $args = apply_filters( 'wpupg_get_posts_args', $args, $page, $this );
540
541 if( $args['posts_per_page'] == -1 ) {
542 $args['nopaging'] = true;
543 }
544
545 $query = new WP_Query( $args );
546 $posts = $query->have_posts() ? $query->posts : array();
547
548 return $posts;
549 }
550
551 public function draw_posts( $page = 0, $post_ids = null )
552 {
553 if( $this->type() == 'terms' ) {
554 return $this->draw_terms();
555 }
556
557 // Draw Posts
558 $output = '';
559 $grid_posts = $this->posts();
560 $posts = $this->get_posts( $page, $post_ids );
561
562 foreach( $posts as $post ) {
563 $post_id = $post->ID;
564
565 $classes = array(
566 'wpupg-item',
567 'wpupg-page-' . $page,
568 'wpupg-post-' . $post_id,
569 'wpupg-type-' . $post->post_type,
570 );
571
572 if( isset( $grid_posts['terms'][$post_id] ) ) {
573 foreach( $grid_posts['terms'][$post_id] as $taxonomy => $terms ) {
574 foreach( $terms as $term ) {
575 $classes[] = 'wpupg-tax-' . $taxonomy . '-' . $term;
576 }
577 }
578 }
579
580 $classes = apply_filters( 'wpupg_output_grid_classes', $classes, $this );
581 $template = apply_filters( 'wpupg_output_grid_template', $this->template(), $this );
582 $post = apply_filters( 'wpupg_output_grid_post', $post, $this );
583
584 $output .= apply_filters( 'wpupg_output_grid_html', $template->output_string( $post, $classes ), $template, $post, $classes );
585 }
586
587 return $output;
588 }
589
590 public function draw_terms()
591 {
592 $output = '';
593
594 $args = array(
595 'orderby' => $this->terms_order_by(),
596 'order' => $this->terms_order(),
597 'hide_empty' => $this->terms_hide_empty(),
598 );
599
600 if( $this->limit_terms() ) {
601 $type = $this->limit_terms_type() == 'restrict' ? 'include' : 'exclude';
602
603 $args[$type] = $this->limit_terms_terms();
604 }
605
606 if( $this->terms_images_only() ) {
607 $args['meta_query'] = array(
608 array(
609 'key' => 'wpupg_custom_image',
610 'compare' => '!=',
611 'value' => '',
612 )
613 );
614 }
615
616 $terms = get_terms( $this->taxonomies(), $args );
617
618 foreach( $terms as $term ) {
619 // Emulate post object
620 $post = (object) array(
621 'term' => true,
622 'ID' => $term->term_id,
623 'post_author' => '',
624 'post_name' => $term->slug,
625 'post_type' => $term->taxonomy,
626 'post_title' => $term->name,
627 'post_date' => '0000-00-00 00:00:00',
628 'post_date_gmt' => '0000-00-00 00:00:00',
629 'post_content' => $term->description,
630 'post_excerpt' => $term->description,
631 'post_status' => 'publish',
632 'comment_status' => 'open',
633 'ping_status' => 'open',
634 'post_password' => '',
635 'post_parent' => $term->parent,
636 'post_modified' => '0000-00-00 00:00:00',
637 'post_modified_gmt' => '0000-00-00 00:00:00',
638 'comment_count' => '0',
639 'menu_order' => '0',
640 );
641
642 $classes = array(
643 'wpupg-item',
644 'wpupg-page-0',
645 'wpupg-post-' . $post->ID,
646 'wpupg-type-' . $post->post_type,
647 );
648
649 $classes = apply_filters( 'wpupg_output_grid_classes', $classes, $this );
650 $template = apply_filters( 'wpupg_output_grid_template', $this->template(), $this );
651 $post = apply_filters( 'wpupg_output_grid_post', $post, $this );
652
653 $output .= apply_filters( 'wpupg_output_grid_html', $template->output_string( $post, $classes ), $template, $post, $classes );
654 }
655
656 return $output;
657 }
658
659 public function draw_pagination()
660 {
661 $output = '';
662
663 $grid_posts = $this->posts();
664 $nbr_posts = count( $grid_posts['all'] );
665
666 $pagination = $this->pagination();
667 $pagination_type = $this->pagination_type();
668
669 $pagination = $pagination[$pagination_type];
670
671 if( $pagination_type == 'pages' ) {
672 $nbr_pages = ceil( $nbr_posts / floatval( $pagination['posts_per_page'] ) );
673
674 for( $page = 0; $page < $nbr_pages; $page++ ) {
675 $active = $page == 0 ? ' active' : '';
676 $output .= '<div class="wpupg-pagination-term wpupg-page-' . $page . $active . '" data-page="' . $page . '">' . ($page+1) . '</div>';
677 }
678 }
679 return $output;
680 }
681 }