PluginProbe
WP Ultimate Post Grid / 2.5.0
WP Ultimate Post Grid v2.5.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 / shortcodes / grid_shortcode.php

grid_shortcode.php in WP Ultimate Post Grid 2.5.0, at helpers/shortcodes/grid_shortcode.php

166 lines 8.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_Shortcode {
4
5 public function __construct()
6 {
7 add_shortcode( 'wpupg-grid', array( $this, 'shortcode' ) );
8
9 // add_filter( 'mce_external_plugins', array( $this, 'tinymce_plugin' ) );
10 }
11
12 public function shortcode( $options )
13 {
14 $output = '';
15
16 $slug = strtolower( trim( $options['id'] ) );
17
18 if( $slug ) {
19 unset( $options['id'] );
20 $post = get_page_by_path( $slug, OBJECT, WPUPG_POST_TYPE );
21
22 if( !is_null( $post ) ) {
23 $grid = new WPUPG_Grid( $post );
24
25 // Check if we need to filter the grid dynamically
26 $dynamic_rules = array();
27 if( count( $options ) > 0 && WPUltimatePostGrid::is_premium_active() ) {
28
29 // Term Grid.
30 if ( $grid->type() == 'terms' ) {
31 if( isset( $options['term_id'] ) ) {
32 $term_ids = str_replace( ',', ';', $options['term_id'] );
33 $term_ids = str_replace( 'current_term', get_queried_object_id(), $term_ids );
34
35 $dynamic_rules = array(
36 'values' => explode( ';', $term_ids ),
37 'type' => 'restrict',
38 );
39 unset( $options['term_id'] );
40 }
41
42 if( isset( $options['exclude_term_id'] ) ) {
43 $term_ids = str_replace( ',', ';', $options['exclude_term_id'] );
44 $term_ids = str_replace( 'current_term', get_queried_object_id(), $term_ids );
45
46 $dynamic_rules = array(
47 'values' => explode( ';', $term_ids ),
48 'type' => 'exclude',
49 );
50 unset( $options['exclude_term_id'] );
51 }
52 } else {
53 // Post Grid.
54 if( isset( $options['post_id'] ) ) {
55 $post_ids = str_replace( ',', ';', $options['post_id'] );
56 $post_ids = str_replace( 'current_post', get_the_ID(), $post_ids );
57
58 $dynamic_rules[] = array(
59 'post_type' => 'wpupg_general',
60 'taxonomy' => 'id',
61 'values' => explode( ';', $post_ids ),
62 'type' => 'restrict',
63 );
64 unset( $options['post_id'] );
65 }
66
67 if( isset( $options['exclude_post_id'] ) ) {
68 $post_ids = str_replace( ',', ';', $options['exclude_post_id'] );
69 $post_ids = str_replace( 'current_post', get_the_ID(), $post_ids );
70
71 $dynamic_rules[] = array(
72 'post_type' => 'wpupg_general',
73 'taxonomy' => 'id',
74 'values' => explode( ';', $post_ids ),
75 'type' => 'exclude',
76 );
77 unset( $options['exclude_post_id'] );
78 }
79
80 foreach( $options as $taxonomy => $terms ) {
81 if( taxonomy_exists( $taxonomy ) ) {
82 $dynamic_rules[] = array(
83 'post_type' => 'wpupg_dynamic',
84 'taxonomy' => $taxonomy,
85 'values' => explode( ';', str_replace( ',', ';', $terms ) ),
86 'type' => 'restrict',
87 );
88 }
89 }
90 }
91 }
92 if( count( $dynamic_rules ) > 0 || 'rand' === $grid->order_by() ) {
93 $grid->set_dynamic_rules( $dynamic_rules );
94 } else {
95 // Make sure latest version of grid is shown.
96 $grid_ids = get_option( 'wpupg_regenerate_grids_check', false );
97
98 if( $grid_ids && in_array( $grid->ID(), $grid_ids ) ) {
99 WPUltimatePostGrid::get()->helper( 'grid_cache' )->generate( $grid->ID() );
100
101 unset( $grid_ids[ array_search( $grid->ID(), $grid_ids ) ] );
102 update_option( 'wpupg_regenerate_grids_check', $grid_ids );
103 }
104 }
105
106 $link_type = $grid->link_type();
107 $link_target = $grid->link_target();
108 $layout_mode = $grid->layout_mode();
109 $centered = $grid->centered() ? 'true' : 'false';
110
111 $posts = '<div id="wpupg-grid-' . esc_attr( $slug ) . '" class="wpupg-grid" data-grid="' . esc_attr( $slug ) . '" data-grid-id="' . $grid->ID() . '" data-link-type="' . $link_type . '" data-link-target="' . $link_target . '" data-layout-mode="' . $layout_mode . '" data-centered="' . $centered . '">';
112 $posts .= $grid->draw_posts();
113 $posts .= '</div>';
114
115 $output = apply_filters( 'wpupg_posts_shortcode', $posts, $grid );
116
117 // Message when no items match filter
118 if( $grid->empty_message() ) {
119 $output .= '<div id="wpupg-grid-' . esc_attr( $slug ) . '-empty" class="wpupg-grid-empty">' . $grid->empty_message() . '</div>';
120 }
121
122 $pagination = '';
123 if( $grid->pagination_type() == 'pages' ) {
124 $pagination_type = $grid->pagination_type();
125 $pagination_style = $grid->pagination_style();
126
127 $style_data = ' data-margin-vertical="' . $pagination_style['margin_vertical'] . '"';
128 $style_data .= ' data-margin-horizontal="' . $pagination_style['margin_horizontal'] . '"';
129 $style_data .= ' data-padding-vertical="' . $pagination_style['padding_vertical'] . '"';
130 $style_data .= ' data-padding-horizontal="' . $pagination_style['padding_horizontal'] . '"';
131 $style_data .= ' data-border-width="' . $pagination_style['border_width'] . '"';
132
133 $style_data .= ' data-background-color="' . $pagination_style['background_color'] . '"';
134 $style_data .= ' data-text-color="' . $pagination_style['text_color'] . '"';
135 $style_data .= ' data-border-color="' . $pagination_style['border_color'] . '"';
136
137 $style_data .= ' data-active-background-color="' . $pagination_style['background_active_color'] . '"';
138 $style_data .= ' data-active-text-color="' . $pagination_style['text_active_color'] . '"';
139 $style_data .= ' data-active-border-color="' . $pagination_style['border_active_color'] . '"';
140
141 $style_data .= ' data-hover-background-color="' . $pagination_style['background_hover_color'] . '"';
142 $style_data .= ' data-hover-text-color="' . $pagination_style['text_hover_color'] . '"';
143 $style_data .= ' data-hover-border-color="' . $pagination_style['border_hover_color'] . '"';
144
145 $pagination .= '<div id="wpupg-grid-' . esc_attr( $slug ) . '-pagination" class="wpupg-pagination wpupg-pagination-' . $pagination_type . '" style="text-align: ' . $pagination_style['alignment'] . ';" data-grid="' . esc_attr( $slug ) . '" data-type="' . $pagination_type . '"' . $style_data . '>';
146 $pagination .= $grid->draw_pagination();
147 $pagination .= '</div>';
148 }
149
150 $output .= apply_filters( 'wpupg_pagination_shortcode', $pagination, $grid );
151
152 wp_localize_script( 'wpupg_grid', 'wpupg_grid_' . $grid->ID(), array(
153 'posts' => $grid->posts(),
154 ));
155 }
156 }
157
158 return $output;
159 }
160
161 public function tinymce_plugin( $plugin_array )
162 {
163 $plugin_array['wpupg_grid_shortcode'] = WPUltimatePostGrid::get()->coreUrl . '/js/tinymce_shortcode.js';
164 return $plugin_array;
165 }
166 }