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

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