| 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 |
$no_output_mode = isset( $options['wpupg_no_output'] ) && $options['wpupg_no_output'] ? true : false; |
| 21 |
unset( $options['wpupg_no_output'] ); |
| 22 |
|
| 23 |
$post = get_page_by_path( $slug, OBJECT, WPUPG_POST_TYPE ); |
| 24 |
|
| 25 |
if( !is_null( $post ) ) { |
| 26 |
$grid = WPUPG_Grid_Manager::get( $post ); |
| 27 |
|
| 28 |
// Check if we need to filter the grid dynamically |
| 29 |
$dynamic_rules = array(); |
| 30 |
if( count( $options ) > 0 && WPUltimatePostGrid::is_premium_active() ) { |
| 31 |
|
| 32 |
// Term Grid. |
| 33 |
if ( $grid->type() == 'terms' ) { |
| 34 |
if( isset( $options['term_id'] ) ) { |
| 35 |
$term_ids = str_replace( ',', ';', $options['term_id'] ); |
| 36 |
$term_ids = str_replace( 'current_term', get_queried_object_id(), $term_ids ); |
| 37 |
|
| 38 |
$dynamic_rules = array( |
| 39 |
'values' => explode( ';', $term_ids ), |
| 40 |
'type' => 'restrict', |
| 41 |
); |
| 42 |
unset( $options['term_id'] ); |
| 43 |
} |
| 44 |
|
| 45 |
if( isset( $options['exclude_term_id'] ) ) { |
| 46 |
$term_ids = str_replace( ',', ';', $options['exclude_term_id'] ); |
| 47 |
$term_ids = str_replace( 'current_term', get_queried_object_id(), $term_ids ); |
| 48 |
|
| 49 |
$dynamic_rules = array( |
| 50 |
'values' => explode( ';', $term_ids ), |
| 51 |
'type' => 'exclude', |
| 52 |
); |
| 53 |
unset( $options['exclude_term_id'] ); |
| 54 |
} |
| 55 |
} else { |
| 56 |
// Post Grid. |
| 57 |
if( isset( $options['post_id'] ) ) { |
| 58 |
$post_ids = str_replace( ',', ';', $options['post_id'] ); |
| 59 |
$post_ids = str_replace( 'current_post', get_the_ID(), $post_ids ); |
| 60 |
|
| 61 |
$dynamic_rules[] = array( |
| 62 |
'post_type' => 'wpupg_general', |
| 63 |
'taxonomy' => 'id', |
| 64 |
'values' => explode( ';', $post_ids ), |
| 65 |
'type' => 'restrict', |
| 66 |
); |
| 67 |
unset( $options['post_id'] ); |
| 68 |
} |
| 69 |
|
| 70 |
if( isset( $options['exclude_post_id'] ) ) { |
| 71 |
$post_ids = str_replace( ',', ';', $options['exclude_post_id'] ); |
| 72 |
$post_ids = str_replace( 'current_post', get_the_ID(), $post_ids ); |
| 73 |
|
| 74 |
$dynamic_rules[] = array( |
| 75 |
'post_type' => 'wpupg_general', |
| 76 |
'taxonomy' => 'id', |
| 77 |
'values' => explode( ';', $post_ids ), |
| 78 |
'type' => 'exclude', |
| 79 |
); |
| 80 |
unset( $options['exclude_post_id'] ); |
| 81 |
} |
| 82 |
|
| 83 |
if( isset( $options['author'] ) ) { |
| 84 |
$author_ids = str_replace( ',', ';', $options['author'] ); |
| 85 |
$author_ids = str_replace( 'current_user', get_current_user_id(), $author_ids ); |
| 86 |
|
| 87 |
$dynamic_rules[] = array( |
| 88 |
'post_type' => 'wpupg_general', |
| 89 |
'taxonomy' => 'author', |
| 90 |
'values' => explode( ';', $author_ids ), |
| 91 |
'type' => 'restrict', |
| 92 |
); |
| 93 |
unset( $options['author'] ); |
| 94 |
} |
| 95 |
|
| 96 |
if( isset( $options['exclude_author'] ) ) { |
| 97 |
$author_ids = str_replace( ',', ';', $options['exclude_author'] ); |
| 98 |
$author_ids = str_replace( 'current_user', get_current_user_id(), $author_ids ); |
| 99 |
|
| 100 |
$dynamic_rules[] = array( |
| 101 |
'post_type' => 'wpupg_general', |
| 102 |
'taxonomy' => 'author', |
| 103 |
'values' => explode( ';', $author_ids ), |
| 104 |
'type' => 'exclude', |
| 105 |
); |
| 106 |
unset( $options['exclude_author'] ); |
| 107 |
} |
| 108 |
|
| 109 |
if( isset( $options['date'] ) ) { |
| 110 |
$date_condition = str_replace( '_today', '', $options['date'] ); |
| 111 |
$date_condition = in_array( $date_condition, array( 'before', 'after' ) ) ? $date_condition : 'is'; |
| 112 |
|
| 113 |
$dynamic_rules[] = array( |
| 114 |
'post_type' => 'wpupg_general', |
| 115 |
'taxonomy' => 'date', |
| 116 |
'values' => array( $date_condition, date( 'Y-m-d' ) ), |
| 117 |
'type' => 'restrict', |
| 118 |
); |
| 119 |
unset( $options['date'] ); |
| 120 |
} |
| 121 |
|
| 122 |
foreach( $options as $taxonomy => $terms ) { |
| 123 |
if( taxonomy_exists( $taxonomy ) ) { |
| 124 |
$dynamic_rules[] = array( |
| 125 |
'post_type' => 'wpupg_dynamic', |
| 126 |
'taxonomy' => $taxonomy, |
| 127 |
'values' => explode( ';', str_replace( ',', ';', $terms ) ), |
| 128 |
'type' => 'restrict', |
| 129 |
); |
| 130 |
} elseif( 'exclude_' === substr( $taxonomy, 0, 8 ) ) { |
| 131 |
$dynamic_rules[] = array( |
| 132 |
'post_type' => 'wpupg_dynamic', |
| 133 |
'taxonomy' => substr( $taxonomy, 8 ), |
| 134 |
'values' => explode( ';', str_replace( ',', ';', $terms ) ), |
| 135 |
'type' => 'exclude', |
| 136 |
); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
if( count( $dynamic_rules ) > 0 || 'rand' === $grid->order_by() ) { |
| 142 |
$grid->set_dynamic_rules( $dynamic_rules ); |
| 143 |
} else if ( WPUltimatePostGrid::option( 'cache_reset_front_end', '1' ) == '1' ) { |
| 144 |
// Make sure latest version of grid is shown. |
| 145 |
$grid_ids = get_option( 'wpupg_regenerate_grids_check', false ); |
| 146 |
|
| 147 |
if( $grid_ids && in_array( $grid->ID(), $grid_ids ) ) { |
| 148 |
WPUltimatePostGrid::get()->helper( 'grid_cache' )->generate( $grid->ID() ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if ( $no_output_mode ) { |
| 153 |
return ''; |
| 154 |
} |
| 155 |
|
| 156 |
$link_type = $grid->link_type(); |
| 157 |
$link_target = $grid->link_target(); |
| 158 |
$layout_mode = $grid->layout_mode(); |
| 159 |
$centered = $grid->centered() ? 'true' : 'false'; |
| 160 |
|
| 161 |
$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 . '">'; |
| 162 |
$posts .= $grid->draw_posts(); |
| 163 |
$posts .= '</div>'; |
| 164 |
|
| 165 |
$output = apply_filters( 'wpupg_posts_shortcode', $posts, $grid ); |
| 166 |
|
| 167 |
// Message when no items match filter |
| 168 |
if( $grid->empty_message() ) { |
| 169 |
$output .= '<div id="wpupg-grid-' . esc_attr( $slug ) . '-empty" class="wpupg-grid-empty">' . $grid->empty_message() . '</div>'; |
| 170 |
} |
| 171 |
|
| 172 |
$pagination = ''; |
| 173 |
if( $grid->pagination_type() == 'pages' ) { |
| 174 |
$pagination_type = $grid->pagination_type(); |
| 175 |
$pagination_style = $grid->pagination_style(); |
| 176 |
|
| 177 |
$style_data = ' data-margin-vertical="' . $pagination_style['margin_vertical'] . '"'; |
| 178 |
$style_data .= ' data-margin-horizontal="' . $pagination_style['margin_horizontal'] . '"'; |
| 179 |
$style_data .= ' data-padding-vertical="' . $pagination_style['padding_vertical'] . '"'; |
| 180 |
$style_data .= ' data-padding-horizontal="' . $pagination_style['padding_horizontal'] . '"'; |
| 181 |
$style_data .= ' data-border-width="' . $pagination_style['border_width'] . '"'; |
| 182 |
|
| 183 |
$style_data .= ' data-background-color="' . $pagination_style['background_color'] . '"'; |
| 184 |
$style_data .= ' data-text-color="' . $pagination_style['text_color'] . '"'; |
| 185 |
$style_data .= ' data-border-color="' . $pagination_style['border_color'] . '"'; |
| 186 |
|
| 187 |
$style_data .= ' data-active-background-color="' . $pagination_style['background_active_color'] . '"'; |
| 188 |
$style_data .= ' data-active-text-color="' . $pagination_style['text_active_color'] . '"'; |
| 189 |
$style_data .= ' data-active-border-color="' . $pagination_style['border_active_color'] . '"'; |
| 190 |
|
| 191 |
$style_data .= ' data-hover-background-color="' . $pagination_style['background_hover_color'] . '"'; |
| 192 |
$style_data .= ' data-hover-text-color="' . $pagination_style['text_hover_color'] . '"'; |
| 193 |
$style_data .= ' data-hover-border-color="' . $pagination_style['border_hover_color'] . '"'; |
| 194 |
|
| 195 |
$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 . '>'; |
| 196 |
$pagination .= $grid->draw_pagination(); |
| 197 |
$pagination .= '</div>'; |
| 198 |
} |
| 199 |
|
| 200 |
$output .= apply_filters( 'wpupg_pagination_shortcode', $pagination, $grid ); |
| 201 |
|
| 202 |
wp_enqueue_script( 'wpupg_public' ); |
| 203 |
if( WPUltimatePostGrid::is_premium_active() ) { |
| 204 |
wp_enqueue_script( 'wpupg_public_premium' ); |
| 205 |
} |
| 206 |
wp_localize_script( 'wpupg_public', 'wpupg_grid_' . $grid->ID(), array( |
| 207 |
'posts' => $grid->posts(), |
| 208 |
)); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
return $output; |
| 213 |
} |
| 214 |
|
| 215 |
public function tinymce_plugin( $plugin_array ) |
| 216 |
{ |
| 217 |
$plugin_array['wpupg_grid_shortcode'] = WPUltimatePostGrid::get()->coreUrl . '/js/tinymce_shortcode.js'; |
| 218 |
return $plugin_array; |
| 219 |
} |
| 220 |
} |