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

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