BlockController
8 months ago
BlockBase.php
8 months ago
GridHoverLayout.php
8 months ago
GridLayout.php
8 months ago
ListLayout.php
8 months ago
RttpgRow.php
8 months ago
SectionTitle.php
8 months ago
BlockBase.php
323 lines
| 1 | <?php |
| 2 | |
| 3 | namespace RT\ThePostGrid\Controllers\Blocks; |
| 4 | |
| 5 | use RT\ThePostGrid\Helpers\Fns; |
| 6 | |
| 7 | abstract class BlockBase { |
| 8 | public $last_post_id; |
| 9 | |
| 10 | abstract public function get_attributes(); |
| 11 | |
| 12 | abstract public function render_block( $data ); |
| 13 | |
| 14 | /** |
| 15 | * Script controller |
| 16 | * |
| 17 | * @param $data |
| 18 | * |
| 19 | * @return void |
| 20 | */ |
| 21 | public function get_script_depends( $data ) { |
| 22 | |
| 23 | $settings = get_option( rtTPG()->options['settings'] ); |
| 24 | $prefix = $data['prefix']; |
| 25 | $this->last_post_id = Fns::get_last_post_id(); |
| 26 | |
| 27 | if ( rtTPG()->hasPro() && ( $data['is_thumb_lightbox'] === 'show' || 'popup' == $data['post_link_type'] || 'multi_popup' == $data['post_link_type'] || $data[ $prefix . '_layout' ] == 'grid-layout7' || $data[ $prefix . '_layout' ] == 'slider-layout4' ) ) { |
| 28 | wp_enqueue_style( 'rt-magnific-popup' ); |
| 29 | wp_enqueue_script( 'rt-magnific-popup' ); |
| 30 | } |
| 31 | |
| 32 | if ( rtTPG()->hasPro() && ( 'popup' == $data['post_link_type'] || 'multi_popup' == $data['post_link_type'] ) ) { |
| 33 | wp_enqueue_script( 'rt-scrollbar' ); |
| 34 | add_action( 'wp_footer', [ Fns::class, 'get_modal_markup' ] ); |
| 35 | } |
| 36 | |
| 37 | if ( rtTPG()->hasPro() && 'button' == $data['filter_type'] && 'carousel' == $data['filter_btn_style'] ) { |
| 38 | wp_enqueue_script( 'swiper' ); |
| 39 | } |
| 40 | |
| 41 | if ( isset( $data['grid_layout_style'] ) && 'masonry' === $data['grid_layout_style'] ) { |
| 42 | wp_enqueue_script( 'rt-isotope-js' ); |
| 43 | } |
| 44 | |
| 45 | if ( 'show' == $data['show_pagination'] && 'pagination_ajax' == $data['pagination_type'] ) { |
| 46 | wp_enqueue_script( 'rt-pagination' ); |
| 47 | } |
| 48 | |
| 49 | if ( isset( $settings['tpg_load_script'] ) ) { |
| 50 | wp_enqueue_style( 'rt-fontawsome' ); |
| 51 | wp_enqueue_style( 'rt-flaticon' ); |
| 52 | wp_enqueue_style( 'rt-tpg-block' ); |
| 53 | if ( $data['prefix'] === 'slider' ) { |
| 54 | wp_enqueue_style( 'swiper' ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | wp_enqueue_script( 'imagesloaded' ); |
| 59 | if ( $data['prefix'] === 'slider' ) { |
| 60 | wp_enqueue_script( 'swiper' ); |
| 61 | } |
| 62 | wp_enqueue_script( 'rt-tpg' ); |
| 63 | wp_enqueue_script( 'rttpg-block-pro' ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Post Query for gutenberg |
| 68 | * |
| 69 | * @param $data |
| 70 | * @param $prefix |
| 71 | * |
| 72 | * @return array |
| 73 | */ |
| 74 | public function post_query_guten( $data, $prefix = '' ) { |
| 75 | $_post_type = ! empty( $data['post_type'] ) ? esc_html( $data['post_type'] ) : 'post'; |
| 76 | $_post_types = ! empty( $data['post_types'] ) ? Fns::escape_array( $data['post_types'] ) : [ 'post' ]; |
| 77 | |
| 78 | if ( rtTPG()->hasPro() && 'yes' === $data['multiple_post_type'] ) { |
| 79 | $post_type = Fns::available_post_types( $_post_types, true ); |
| 80 | } else { |
| 81 | $post_type = Fns::available_post_type( $_post_type ); |
| 82 | } |
| 83 | |
| 84 | $args = [ |
| 85 | 'post_type' => $post_type, |
| 86 | 'post_status' => 'publish', |
| 87 | ]; |
| 88 | |
| 89 | if ( 'current_query' == $data['post_type'] && is_archive() ) { |
| 90 | return $args; |
| 91 | } |
| 92 | |
| 93 | if ( $data['post_id'] ) { |
| 94 | $post_ids = explode( ',', esc_html( $data['post_id'] ) ); |
| 95 | $post_ids = array_map( 'trim', $post_ids ); |
| 96 | $args['post__in'] = $post_ids; |
| 97 | } |
| 98 | |
| 99 | if ( $prefix !== 'slider' && 'show' === $data['show_pagination'] ) { |
| 100 | $_paged = is_front_page() ? 'page' : 'paged'; |
| 101 | $args['paged'] = get_query_var( $_paged ) ? intval( get_query_var( $_paged ) ) : 1; |
| 102 | } |
| 103 | |
| 104 | if ( rtTPG()->hasPro() && 'yes' == $data['ignore_sticky_posts'] ) { |
| 105 | $args['ignore_sticky_posts'] = 1; |
| 106 | } |
| 107 | |
| 108 | if ( $orderby = $data['orderby'] ) { |
| 109 | if ( ! rtTPG()->hasPro() && 'rand' == $orderby ) { |
| 110 | $orderby = 'date'; |
| 111 | } |
| 112 | $args['orderby'] = esc_html( $orderby ); |
| 113 | |
| 114 | if ( 'include_only' == $orderby ) { |
| 115 | $args['orderby'] = 'post__in'; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if ( $data['order'] ) { |
| 120 | $args['order'] = esc_html( $data['order'] ); |
| 121 | } |
| 122 | |
| 123 | if ( $data['instant_query'] ) { |
| 124 | $args = Fns::get_instant_query( $data['instant_query'], $args ); |
| 125 | } |
| 126 | |
| 127 | if ( $data['author'] ) { |
| 128 | $args['author__in'] = esc_html( $data['author'] ); |
| 129 | } |
| 130 | |
| 131 | if ( rtTPG()->hasPro() && ( $data['start_date'] || $data['end_date'] ) ) { |
| 132 | $args['date_query'] = [ |
| 133 | [ |
| 134 | 'after' => trim( esc_html( $data['start_date'] ) ), |
| 135 | 'before' => trim( esc_html( $data['end_date'] ) ), |
| 136 | 'inclusive' => true, |
| 137 | ], |
| 138 | ]; |
| 139 | } |
| 140 | |
| 141 | $_taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 142 | $_taxonomy_list = $data['taxonomy_lists']; |
| 143 | $filtered_taxonomy_lists = []; |
| 144 | |
| 145 | if ( isset( $_taxonomy_list ) && ! empty( $_taxonomy_list ) ) { |
| 146 | foreach ( $_taxonomies as $index => $object ) { |
| 147 | if ( in_array( $object->name, Fns::get_excluded_taxonomy() ) ) { |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | if ( ! isset( $_taxonomy_list[ $object->name ]['options'] ) ) { |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | $_term_list = wp_list_pluck( $_taxonomy_list[ $object->name ]['options'], 'value' ); |
| 156 | if ( ! empty( $_term_list ) ) { |
| 157 | //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 158 | $args['tax_query'][] = [ |
| 159 | 'taxonomy' => $object->name, |
| 160 | 'field' => 'term_id', |
| 161 | 'terms' => $_term_list, |
| 162 | ]; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if ( ! empty( $args['tax_query'] ) && $data['relation'] ) { |
| 168 | $args['tax_query']['relation'] = esc_html( $data['relation'] ); //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 169 | } |
| 170 | |
| 171 | if ( $data['post_keyword'] ) { |
| 172 | $args['s'] = esc_html( $data['post_keyword'] ); |
| 173 | } |
| 174 | |
| 175 | $offset_posts = $excluded_ids = []; |
| 176 | if ( $data['exclude'] || $data['offset'] ) { |
| 177 | if ( $data['exclude'] ) { |
| 178 | $excluded_ids = explode( ',', esc_html( $data['exclude'] ) ); |
| 179 | $excluded_ids = array_map( 'trim', $excluded_ids ); |
| 180 | } |
| 181 | |
| 182 | if ( $data['offset'] ) { |
| 183 | $_temp_args = $args; |
| 184 | unset( $_temp_args['paged'] ); |
| 185 | $_temp_args['posts_per_page'] = esc_html( $data['offset'] ); |
| 186 | $_temp_args['fields'] = 'ids'; |
| 187 | |
| 188 | $offset_posts = get_posts( $_temp_args ); |
| 189 | } |
| 190 | |
| 191 | $excluded_post_ids = array_merge( $offset_posts, $excluded_ids ); |
| 192 | $args['post__not_in'] = array_unique( $excluded_post_ids ); //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in |
| 193 | } |
| 194 | |
| 195 | if ( $prefix !== 'slider' ) { |
| 196 | if ( $data['post_limit'] ) { |
| 197 | $tempArgs = $args; |
| 198 | $tempArgs['posts_per_page'] = esc_html( $data['post_limit'] ); |
| 199 | $tempArgs['paged'] = 1; |
| 200 | $tempArgs['fields'] = 'ids'; |
| 201 | if ( ! empty( $offset_posts ) ) { |
| 202 | $tempArgs['post__not_in'] = $offset_posts; //phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in |
| 203 | } |
| 204 | $tempQ = new \WP_Query( apply_filters( 'tpg_sc_temp_query_args', $tempArgs ) ); |
| 205 | if ( ! empty( $tempQ->posts ) ) { |
| 206 | $args['post__in'] = $tempQ->posts; |
| 207 | $args['posts_per_page'] = ( 'show' == $data['show_pagination'] && $data['display_per_page'] ) ? esc_html( $data['display_per_page'] ) : esc_html( $data['post_limit'] ); |
| 208 | } |
| 209 | } else { |
| 210 | $_posts_per_page = 9; |
| 211 | if ( 'grid' === $prefix ) { |
| 212 | if ( $data['grid_layout'] == 'grid-layout5' ) { |
| 213 | $_posts_per_page = 5; |
| 214 | } elseif ( in_array( $data['grid_layout'], [ 'grid-layout6', 'grid-layout6-2' ] ) ) { |
| 215 | $_posts_per_page = 3; |
| 216 | } elseif ( in_array( $data['grid_layout'], [ 'grid-layout5', 'grid-layout5-2' ] ) ) { |
| 217 | $_posts_per_page = 5; |
| 218 | } |
| 219 | } elseif ( 'list' === $prefix ) { |
| 220 | if ( in_array( $data['list_layout'], [ 'list-layout2', 'list-layout2-2' ] ) ) { |
| 221 | $_posts_per_page = 7; |
| 222 | } elseif ( in_array( $data['list_layout'], [ 'list-layout3', 'list-layout3-2' ] ) ) { |
| 223 | $_posts_per_page = 5; |
| 224 | } |
| 225 | } elseif ( 'grid_hover' === $prefix ) { |
| 226 | if ( in_array( $data['grid_hover_layout'], [ 'grid_hover-layout4', 'grid_hover-layout4-2' ] ) ) { |
| 227 | $_posts_per_page = 7; |
| 228 | } elseif ( in_array( |
| 229 | $data['grid_hover_layout'], |
| 230 | [ |
| 231 | 'grid_hover-layout5', |
| 232 | 'grid_hover-layout5-2', |
| 233 | ] |
| 234 | ) ) { |
| 235 | $_posts_per_page = 3; |
| 236 | } elseif ( in_array( |
| 237 | $data['grid_hover_layout'], |
| 238 | [ |
| 239 | 'grid_hover-layout6', |
| 240 | 'grid_hover-layout6-2', |
| 241 | 'grid_hover-layout9', |
| 242 | 'grid_hover-layout9-2', |
| 243 | 'grid_hover-layout10', |
| 244 | 'grid_hover-layout11', |
| 245 | ] |
| 246 | ) |
| 247 | ) { |
| 248 | $_posts_per_page = 4; |
| 249 | } elseif ( in_array( |
| 250 | $data['grid_hover_layout'], |
| 251 | [ |
| 252 | 'grid_hover-layout7', |
| 253 | 'grid_hover-layout7-2', |
| 254 | 'grid_hover-layout8', |
| 255 | ] |
| 256 | ) ) { |
| 257 | $_posts_per_page = 5; |
| 258 | } elseif ( in_array( |
| 259 | $data['grid_hover_layout'], |
| 260 | [ |
| 261 | 'grid_hover-layout6', |
| 262 | 'grid_hover-layout6-2', |
| 263 | ] |
| 264 | ) |
| 265 | ) { |
| 266 | $_posts_per_page = 4; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | $args['posts_per_page'] = intval( $data['display_per_page'] ?: $_posts_per_page ); |
| 271 | } |
| 272 | } else { |
| 273 | $slider_per_page = $data['post_limit']; |
| 274 | if ( $data['slider_layout'] == 'slider-layout10' ) { |
| 275 | $slider_reminder = ( intval( $data['post_limit'], 10 ) % 5 ); |
| 276 | if ( $slider_reminder ) { |
| 277 | $slider_per_page = ( $data['post_limit'] - $slider_reminder + 5 ); |
| 278 | } |
| 279 | } |
| 280 | $args['posts_per_page'] = intval( $slider_per_page ); |
| 281 | } |
| 282 | |
| 283 | // Builder query |
| 284 | if ( ! empty( $data['is_builder'] ) && $data['is_builder'] === 'yes' ) { |
| 285 | $args['posts_per_page'] = get_option( 'posts_per_page' ); |
| 286 | if ( is_tag() ) { |
| 287 | $args['tag'] = get_query_var( 'tag' ); |
| 288 | } |
| 289 | |
| 290 | if ( is_category() ) { |
| 291 | $args['cat'] = get_query_var( 'cat' ); |
| 292 | } |
| 293 | |
| 294 | if ( is_author() ) { |
| 295 | $args['author'] = get_query_var( 'author' ); |
| 296 | } |
| 297 | |
| 298 | if ( is_date() ) { |
| 299 | $year = get_query_var( 'year' ); |
| 300 | $monthnum = get_query_var( 'monthnum' ); |
| 301 | $day = get_query_var( 'day' ); |
| 302 | |
| 303 | $args = [ |
| 304 | 'date_query' => [ |
| 305 | [ |
| 306 | 'year' => $year, |
| 307 | 'month' => $monthnum, |
| 308 | 'day' => $day, |
| 309 | ], |
| 310 | ], |
| 311 | ]; |
| 312 | } |
| 313 | |
| 314 | if ( is_search() ) { |
| 315 | $search = get_query_var( 's' ); |
| 316 | $args['s'] = esc_html( $search ); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return apply_filters( 'tpg_sc_query_args', $args ); |
| 321 | } |
| 322 | } |
| 323 |