| 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_limit', |
| 11 |
'wpupg_filter_match_parents', |
| 12 |
'wpupg_filter_multiselect', |
| 13 |
'wpupg_filter_multiselect_type', |
| 14 |
'wpupg_filter_type', |
| 15 |
'wpupg_pagination_type', |
| 16 |
'wpupg_post_types', |
| 17 |
'wpupg_layout_mode', |
| 18 |
'wpupg_limit_posts', |
| 19 |
'wpupg_limit_posts_number', |
| 20 |
'wpupg_link_target', |
| 21 |
'wpupg_link_type', |
| 22 |
'wpupg_order_by', |
| 23 |
'wpupg_order', |
| 24 |
'wpupg_template', |
| 25 |
); |
| 26 |
|
| 27 |
// Pagination fields with defaults |
| 28 |
private $pagination_fields = array( |
| 29 |
'pages' => array( |
| 30 |
'posts_per_page' => 20, |
| 31 |
), |
| 32 |
'load_more' => array( |
| 33 |
'initial_posts' => 20, |
| 34 |
'posts_on_click' => 20, |
| 35 |
'button_text' => 'Load More', |
| 36 |
), |
| 37 |
'load_filter' => array( |
| 38 |
'initial_posts' => 20, |
| 39 |
), |
| 40 |
); |
| 41 |
|
| 42 |
private $pagination_style_fields = array( |
| 43 |
'background_color' => '#2E5077', |
| 44 |
'background_active_color' => '#1C3148', |
| 45 |
'background_hover_color' => '#1C3148', |
| 46 |
'text_color' => '#FFFFFF', |
| 47 |
'text_active_color' => '#FFFFFF', |
| 48 |
'text_hover_color' => '#FFFFFF', |
| 49 |
'border_color' => '#1C3148', |
| 50 |
'border_active_color' => '#1C3148', |
| 51 |
'border_hover_color' => '#1C3148', |
| 52 |
'border_width' => '1', |
| 53 |
'margin_vertical' => '5', |
| 54 |
'margin_horizontal' => '5', |
| 55 |
'padding_vertical' => '5', |
| 56 |
'padding_horizontal' => '10', |
| 57 |
'alignment' => 'left', |
| 58 |
); |
| 59 |
|
| 60 |
// Filter style fields with defaults |
| 61 |
private $filter_style_fields = array( |
| 62 |
'isotope' => array( |
| 63 |
'background_color' => '#2E5077', |
| 64 |
'background_active_color' => '#1C3148', |
| 65 |
'background_hover_color' => '#1C3148', |
| 66 |
'text_color' => '#FFFFFF', |
| 67 |
'text_active_color' => '#FFFFFF', |
| 68 |
'text_hover_color' => '#FFFFFF', |
| 69 |
'border_color' => '#1C3148', |
| 70 |
'border_active_color' => '#1C3148', |
| 71 |
'border_hover_color' => '#1C3148', |
| 72 |
'border_width' => '1', |
| 73 |
'margin_vertical' => '5', |
| 74 |
'margin_horizontal' => '5', |
| 75 |
'padding_vertical' => '5', |
| 76 |
'padding_horizontal' => '10', |
| 77 |
'alignment' => 'left', |
| 78 |
'all_button_text' => 'Check Constructor', |
| 79 |
), |
| 80 |
); |
| 81 |
|
| 82 |
public function __construct( $post ) |
| 83 |
{ |
| 84 |
// Get associated post |
| 85 |
if( is_object( $post ) && $post instanceof WP_Post ) { |
| 86 |
$this->post = $post; |
| 87 |
} else if( is_numeric( $post ) ) { |
| 88 |
$this->post = get_post( $post ); |
| 89 |
} else { |
| 90 |
throw new InvalidArgumentException( 'Grids can only be instantiated with a Post object or Post ID.' ); |
| 91 |
} |
| 92 |
|
| 93 |
// Get metadata |
| 94 |
$this->meta = get_post_custom( $this->post->ID ); |
| 95 |
|
| 96 |
// Defaults with expressions |
| 97 |
$this->filter_style_fields['isotope']['all_button_text'] = __( 'All', 'wp-ultimate-post-grid' ); |
| 98 |
} |
| 99 |
|
| 100 |
public function is_present( $field ) |
| 101 |
{ |
| 102 |
switch( $field ) { |
| 103 |
default: |
| 104 |
$val = $this->meta( $field ); |
| 105 |
return isset( $val ) && trim( $val ) != ''; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public function meta( $field ) |
| 110 |
{ |
| 111 |
if( isset( $this->meta[$field] ) ) { |
| 112 |
return $this->meta[$field][0]; |
| 113 |
} |
| 114 |
|
| 115 |
return null; |
| 116 |
} |
| 117 |
|
| 118 |
public function fields() |
| 119 |
{ |
| 120 |
return $this->fields; |
| 121 |
} |
| 122 |
|
| 123 |
public function filter_style_fields() |
| 124 |
{ |
| 125 |
return $this->filter_style_fields; |
| 126 |
} |
| 127 |
|
| 128 |
public function pagination_fields() |
| 129 |
{ |
| 130 |
return $this->pagination_fields; |
| 131 |
} |
| 132 |
|
| 133 |
public function pagination_style_fields() |
| 134 |
{ |
| 135 |
return $this->pagination_style_fields; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Grid fields |
| 140 |
*/ |
| 141 |
|
| 142 |
public function centered() |
| 143 |
{ |
| 144 |
return $this->meta( 'wpupg_centered' ); |
| 145 |
} |
| 146 |
|
| 147 |
public function filter() |
| 148 |
{ |
| 149 |
return $this->meta( 'wpupg_filter' ); |
| 150 |
} |
| 151 |
|
| 152 |
public function filter_taxonomies() |
| 153 |
{ |
| 154 |
$filter_taxonomies = maybe_unserialize( $this->meta( 'wpupg_filter_taxonomies' ) ); |
| 155 |
return is_array( $filter_taxonomies ) ? $filter_taxonomies : array(); |
| 156 |
} |
| 157 |
|
| 158 |
public function filter_style() |
| 159 |
{ |
| 160 |
$filter_style = maybe_unserialize( $this->meta( 'wpupg_filter_style' ) ); |
| 161 |
$filter_style = is_array( $filter_style ) ? $filter_style : array(); |
| 162 |
|
| 163 |
// Set defaults |
| 164 |
foreach( $this->filter_style_fields() as $type => $defaults ) { |
| 165 |
$filter_style[$type] = isset( $filter_style[$type] ) ? $filter_style[$type] + $defaults : $defaults; |
| 166 |
} |
| 167 |
|
| 168 |
return $filter_style; |
| 169 |
} |
| 170 |
|
| 171 |
public function filter_limit() |
| 172 |
{ |
| 173 |
return $this->meta( 'wpupg_filter_limit' ); |
| 174 |
} |
| 175 |
|
| 176 |
public function filter_limit_terms() |
| 177 |
{ |
| 178 |
$limit_terms = maybe_unserialize( $this->meta( 'wpupg_filter_limit_terms' ) ); |
| 179 |
return is_array( $limit_terms ) ? $limit_terms : array(); |
| 180 |
} |
| 181 |
|
| 182 |
public function filter_match_parents() |
| 183 |
{ |
| 184 |
return $this->meta( 'wpupg_filter_match_parents' ); |
| 185 |
} |
| 186 |
|
| 187 |
public function filter_multiselect() |
| 188 |
{ |
| 189 |
if( WPUltimatePostGrid::is_premium_active() ) { |
| 190 |
return $this->meta( 'wpupg_filter_multiselect' ); |
| 191 |
} else { |
| 192 |
return false; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
public function filter_multiselect_type() |
| 197 |
{ |
| 198 |
return $this->meta( 'wpupg_filter_multiselect_type' ); |
| 199 |
} |
| 200 |
|
| 201 |
public function filter_type() |
| 202 |
{ |
| 203 |
return $this->meta( 'wpupg_filter_type' ); |
| 204 |
} |
| 205 |
|
| 206 |
public function ID() |
| 207 |
{ |
| 208 |
return $this->post->ID; |
| 209 |
} |
| 210 |
|
| 211 |
public function images_only() |
| 212 |
{ |
| 213 |
return $this->meta( 'wpupg_images_only' ); |
| 214 |
} |
| 215 |
|
| 216 |
public function layout_mode() |
| 217 |
{ |
| 218 |
$layout_mode = $this->meta( 'wpupg_layout_mode' ); |
| 219 |
return $layout_mode ? $layout_mode : 'masonry'; |
| 220 |
} |
| 221 |
|
| 222 |
public function limit_posts() |
| 223 |
{ |
| 224 |
return $this->meta( 'wpupg_limit_posts' ); |
| 225 |
} |
| 226 |
|
| 227 |
public function limit_posts_number() |
| 228 |
{ |
| 229 |
$limit = intval( $this->meta( 'wpupg_limit_posts_number' ) ); |
| 230 |
return $limit > 0 ? $limit : ''; |
| 231 |
} |
| 232 |
|
| 233 |
public function limit_rules() |
| 234 |
{ |
| 235 |
$limit_rules = maybe_unserialize( $this->meta( 'wpupg_limit_rules' ) ); |
| 236 |
return is_array( $limit_rules ) ? $limit_rules : array(); |
| 237 |
} |
| 238 |
|
| 239 |
public function link_target() |
| 240 |
{ |
| 241 |
$link_target = $this->meta( 'wpupg_link_target' ); |
| 242 |
return $link_target ? $link_target : 'post'; |
| 243 |
} |
| 244 |
|
| 245 |
public function link_type() |
| 246 |
{ |
| 247 |
$link_type = $this->meta( 'wpupg_link_type' ); |
| 248 |
return $link_type ? $link_type : '_self'; |
| 249 |
} |
| 250 |
|
| 251 |
public function order() |
| 252 |
{ |
| 253 |
return $this->meta( 'wpupg_order' ); |
| 254 |
} |
| 255 |
|
| 256 |
public function order_by() |
| 257 |
{ |
| 258 |
return $this->meta( 'wpupg_order_by' ); |
| 259 |
} |
| 260 |
|
| 261 |
public function pagination() |
| 262 |
{ |
| 263 |
$pagination = maybe_unserialize( $this->meta( 'wpupg_pagination' ) ); |
| 264 |
$pagination = is_array( $pagination ) ? $pagination : array(); |
| 265 |
|
| 266 |
// Set defaults |
| 267 |
foreach( $this->pagination_fields() as $type => $defaults ) { |
| 268 |
$pagination[$type] = isset( $pagination[$type] ) ? $pagination[$type] + $defaults : $defaults; |
| 269 |
} |
| 270 |
|
| 271 |
return $pagination; |
| 272 |
} |
| 273 |
|
| 274 |
public function pagination_style() |
| 275 |
{ |
| 276 |
$pagination_style = maybe_unserialize( $this->meta( 'wpupg_pagination_style' ) ); |
| 277 |
$pagination_style = is_array( $pagination_style ) ? $pagination_style : array(); |
| 278 |
|
| 279 |
// Set defaults |
| 280 |
$pagination_style = $pagination_style + $this->pagination_style_fields(); |
| 281 |
|
| 282 |
return $pagination_style; |
| 283 |
} |
| 284 |
|
| 285 |
public function pagination_type() |
| 286 |
{ |
| 287 |
return $this->meta( 'wpupg_pagination_type' ); |
| 288 |
} |
| 289 |
|
| 290 |
public function posts() |
| 291 |
{ |
| 292 |
$posts = maybe_unserialize( $this->meta( 'wpupg_posts' ) ); |
| 293 |
return is_array( $posts ) ? $posts : array(); |
| 294 |
} |
| 295 |
|
| 296 |
public function post() |
| 297 |
{ |
| 298 |
return $this->post; |
| 299 |
} |
| 300 |
|
| 301 |
public function post_status() // TODO |
| 302 |
{ |
| 303 |
return 'publish'; |
| 304 |
} |
| 305 |
|
| 306 |
public function post_types() |
| 307 |
{ |
| 308 |
$post_types = maybe_unserialize( $this->meta( 'wpupg_post_types' ) ); |
| 309 |
return is_array( $post_types ) ? $post_types : array(); |
| 310 |
} |
| 311 |
|
| 312 |
public function slug() |
| 313 |
{ |
| 314 |
return $this->post->post_name; |
| 315 |
} |
| 316 |
|
| 317 |
public function template() |
| 318 |
{ |
| 319 |
return WPUltimatePostGrid::addon( 'custom-templates' )->get_template( $this->template_id() ); |
| 320 |
} |
| 321 |
|
| 322 |
public function template_id() |
| 323 |
{ |
| 324 |
return $this->meta( 'wpupg_template' ); |
| 325 |
} |
| 326 |
|
| 327 |
public function title() |
| 328 |
{ |
| 329 |
return $this->post->post_title; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Helper functions |
| 334 |
*/ |
| 335 |
|
| 336 |
public function set_dynamic_rules( $dynamic_rules ) |
| 337 |
{ |
| 338 |
if( WPUltimatePostGrid::is_premium_active() ) { |
| 339 |
$this->meta['wpupg_limit_posts'][0] = 'on'; |
| 340 |
|
| 341 |
$limit_rules = maybe_unserialize( $this->meta( 'wpupg_limit_rules' ) ); |
| 342 |
$limit_rules = is_array( $limit_rules ) ? $limit_rules : array(); |
| 343 |
|
| 344 |
$new_rules = array_merge( $limit_rules, $dynamic_rules ); |
| 345 |
$this->meta['wpupg_limit_rules'][0] = serialize( $new_rules ); |
| 346 |
|
| 347 |
$generated = WPUltimatePostGrid::get()->helper( 'grid_cache' )->dynamic_generate( $this ); |
| 348 |
|
| 349 |
$this->meta['wpupg_posts'][0] = serialize( $generated['cache'] ); |
| 350 |
$this->meta['wpupg_filter'][0] = $generated['filter']; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
public function get_posts( $page = 0, $post_ids = null ) |
| 355 |
{ |
| 356 |
$grid_posts = $this->posts(); |
| 357 |
$post_ids = is_null( $post_ids ) ? $grid_posts['all'] : array_intersect( $post_ids, $grid_posts['all'] ); |
| 358 |
|
| 359 |
if( count( $post_ids ) == 0 ) return array(); |
| 360 |
|
| 361 |
$posts_per_page = -1; |
| 362 |
|
| 363 |
if( $this->pagination_type() == 'pages' ) { |
| 364 |
$pagination = $this->pagination(); |
| 365 |
$posts_per_page = $pagination['pages']['posts_per_page']; |
| 366 |
} |
| 367 |
|
| 368 |
$offset = 0; |
| 369 |
if( $page > 0 ) { |
| 370 |
$offset = $page * $posts_per_page; |
| 371 |
} |
| 372 |
|
| 373 |
$args = array( |
| 374 |
'post_type' => 'any', |
| 375 |
'orderby' => $this->order_by(), |
| 376 |
'order' => $this->order(), |
| 377 |
'posts_per_page' => $posts_per_page, |
| 378 |
'offset' => $offset, |
| 379 |
'post__in' => $post_ids, |
| 380 |
'ignore_sticky_posts' => true, |
| 381 |
); |
| 382 |
|
| 383 |
$args = apply_filters( 'wpupg_get_posts_args', $args, $page, $this ); |
| 384 |
|
| 385 |
if( $args['posts_per_page'] == -1 ) { |
| 386 |
$args['nopaging'] = true; |
| 387 |
} |
| 388 |
|
| 389 |
$query = new WP_Query( $args ); |
| 390 |
$posts = $query->have_posts() ? $query->posts : array(); |
| 391 |
|
| 392 |
return $posts; |
| 393 |
} |
| 394 |
|
| 395 |
public function draw_posts( $page = 0, $post_ids = null ) |
| 396 |
{ |
| 397 |
$output = ''; |
| 398 |
$grid_posts = $this->posts(); |
| 399 |
$posts = $this->get_posts( $page, $post_ids ); |
| 400 |
|
| 401 |
foreach( $posts as $post ) { |
| 402 |
$post_id = $post->ID; |
| 403 |
|
| 404 |
$classes = array( |
| 405 |
'wpupg-item', |
| 406 |
'wpupg-page-' . $page, |
| 407 |
'wpupg-post-' . $post_id, |
| 408 |
'wpupg-type-' . $post->post_type, |
| 409 |
); |
| 410 |
|
| 411 |
if( isset( $grid_posts['terms'][$post_id] ) ) { |
| 412 |
foreach( $grid_posts['terms'][$post_id] as $taxonomy => $terms ) { |
| 413 |
foreach( $terms as $term ) { |
| 414 |
$classes[] = 'wpupg-tax-' . $taxonomy . '-' . $term; |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
$classes = apply_filters( 'wpupg_output_grid_classes', $classes, $this ); |
| 420 |
$template = apply_filters( 'wpupg_output_grid_template', $this->template(), $this ); |
| 421 |
$post = apply_filters( 'wpupg_output_grid_post', $post, $this ); |
| 422 |
|
| 423 |
$output .= apply_filters( 'wpupg_output_grid_html', $template->output_string( $post, $classes ), $template, $post, $classes ); |
| 424 |
} |
| 425 |
|
| 426 |
return $output; |
| 427 |
} |
| 428 |
|
| 429 |
public function draw_pagination() |
| 430 |
{ |
| 431 |
$output = ''; |
| 432 |
|
| 433 |
$grid_posts = $this->posts(); |
| 434 |
$nbr_posts = count( $grid_posts['all'] ); |
| 435 |
|
| 436 |
$pagination = $this->pagination(); |
| 437 |
$pagination_type = $this->pagination_type(); |
| 438 |
|
| 439 |
$pagination = $pagination[$pagination_type]; |
| 440 |
|
| 441 |
if( $pagination_type == 'pages' ) { |
| 442 |
$nbr_pages = ceil( $nbr_posts / floatval( $pagination['posts_per_page'] ) ); |
| 443 |
|
| 444 |
for( $page = 0; $page < $nbr_pages; $page++ ) { |
| 445 |
$active = $page == 0 ? ' active' : ''; |
| 446 |
$output .= '<div class="wpupg-pagination-term wpupg-page-' . $page . $active . '" data-page="' . $page . '">' . ($page+1) . '</div>'; |
| 447 |
} |
| 448 |
} |
| 449 |
return $output; |
| 450 |
} |
| 451 |
} |