PluginProbe
WP Ultimate Post Grid / 2.6.0
WP Ultimate Post Grid v2.6.0
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.6.0, at helpers/models/grid.php

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