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

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