PluginProbe
WP Ultimate Post Grid / 2.7.0
WP Ultimate Post Grid v2.7.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.7.0, at helpers/shortcodes/grid_shortcode.php

189 lines 9.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_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 if( isset( $options['author'] ) ) {
81 $author_ids = str_replace( ',', ';', $options['author'] );
82 $author_ids = str_replace( 'current_user', get_current_user_id(), $author_ids );
83
84 $dynamic_rules[] = array(
85 'post_type' => 'wpupg_general',
86 'taxonomy' => 'author',
87 'values' => explode( ';', $author_ids ),
88 'type' => 'restrict',
89 );
90 unset( $options['author'] );
91 }
92
93 if( isset( $options['exclude_author'] ) ) {
94 $author_ids = str_replace( ',', ';', $options['exclude_author'] );
95 $author_ids = str_replace( 'current_user', get_current_user_id(), $author_ids );
96
97 $dynamic_rules[] = array(
98 'post_type' => 'wpupg_general',
99 'taxonomy' => 'author',
100 'values' => explode( ';', $author_ids ),
101 'type' => 'exclude',
102 );
103 unset( $options['exclude_author'] );
104 }
105
106 foreach( $options as $taxonomy => $terms ) {
107 if( taxonomy_exists( $taxonomy ) ) {
108 $dynamic_rules[] = array(
109 'post_type' => 'wpupg_dynamic',
110 'taxonomy' => $taxonomy,
111 'values' => explode( ';', str_replace( ',', ';', $terms ) ),
112 'type' => 'restrict',
113 );
114 }
115 }
116 }
117 }
118 if( count( $dynamic_rules ) > 0 || 'rand' === $grid->order_by() ) {
119 $grid->set_dynamic_rules( $dynamic_rules );
120 } else if ( WPUltimatePostGrid::option( 'cache_reset_front_end', '1' ) == '1' ) {
121 // Make sure latest version of grid is shown.
122 $grid_ids = get_option( 'wpupg_regenerate_grids_check', false );
123
124 if( $grid_ids && in_array( $grid->ID(), $grid_ids ) ) {
125 WPUltimatePostGrid::get()->helper( 'grid_cache' )->generate( $grid->ID() );
126 }
127 }
128
129 $link_type = $grid->link_type();
130 $link_target = $grid->link_target();
131 $layout_mode = $grid->layout_mode();
132 $centered = $grid->centered() ? 'true' : 'false';
133
134 $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 . '">';
135 $posts .= $grid->draw_posts();
136 $posts .= '</div>';
137
138 $output = apply_filters( 'wpupg_posts_shortcode', $posts, $grid );
139
140 // Message when no items match filter
141 if( $grid->empty_message() ) {
142 $output .= '<div id="wpupg-grid-' . esc_attr( $slug ) . '-empty" class="wpupg-grid-empty">' . $grid->empty_message() . '</div>';
143 }
144
145 $pagination = '';
146 if( $grid->pagination_type() == 'pages' ) {
147 $pagination_type = $grid->pagination_type();
148 $pagination_style = $grid->pagination_style();
149
150 $style_data = ' data-margin-vertical="' . $pagination_style['margin_vertical'] . '"';
151 $style_data .= ' data-margin-horizontal="' . $pagination_style['margin_horizontal'] . '"';
152 $style_data .= ' data-padding-vertical="' . $pagination_style['padding_vertical'] . '"';
153 $style_data .= ' data-padding-horizontal="' . $pagination_style['padding_horizontal'] . '"';
154 $style_data .= ' data-border-width="' . $pagination_style['border_width'] . '"';
155
156 $style_data .= ' data-background-color="' . $pagination_style['background_color'] . '"';
157 $style_data .= ' data-text-color="' . $pagination_style['text_color'] . '"';
158 $style_data .= ' data-border-color="' . $pagination_style['border_color'] . '"';
159
160 $style_data .= ' data-active-background-color="' . $pagination_style['background_active_color'] . '"';
161 $style_data .= ' data-active-text-color="' . $pagination_style['text_active_color'] . '"';
162 $style_data .= ' data-active-border-color="' . $pagination_style['border_active_color'] . '"';
163
164 $style_data .= ' data-hover-background-color="' . $pagination_style['background_hover_color'] . '"';
165 $style_data .= ' data-hover-text-color="' . $pagination_style['text_hover_color'] . '"';
166 $style_data .= ' data-hover-border-color="' . $pagination_style['border_hover_color'] . '"';
167
168 $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 . '>';
169 $pagination .= $grid->draw_pagination();
170 $pagination .= '</div>';
171 }
172
173 $output .= apply_filters( 'wpupg_pagination_shortcode', $pagination, $grid );
174
175 wp_localize_script( 'wpupg_grid', 'wpupg_grid_' . $grid->ID(), array(
176 'posts' => $grid->posts(),
177 ));
178 }
179 }
180
181 return $output;
182 }
183
184 public function tinymce_plugin( $plugin_array )
185 {
186 $plugin_array['wpupg_grid_shortcode'] = WPUltimatePostGrid::get()->coreUrl . '/js/tinymce_shortcode.js';
187 return $plugin_array;
188 }
189 }