GridHoverLayoutRenderer.php
1 year ago
GridLayoutRenderer.php
1 year ago
ListLayoutRender.php
1 year ago
GridLayoutRenderer.php
176 lines
| 1 | <?php |
| 2 | |
| 3 | namespace RT\ThePostGrid\Divi\Render; |
| 4 | |
| 5 | use WP_Query; |
| 6 | use RT\ThePostGrid\Helpers\Fns; |
| 7 | use RT\ThePostGrid\Helpers\DiviFns; |
| 8 | |
| 9 | class GridLayoutRenderer { |
| 10 | |
| 11 | protected $settings; |
| 12 | protected $prefix; |
| 13 | protected $query; |
| 14 | protected $query_args; |
| 15 | protected $layoutID; |
| 16 | protected $post_data; |
| 17 | protected $template_path; |
| 18 | protected $layout; |
| 19 | protected $layout_style; |
| 20 | protected $primary_color; |
| 21 | protected $secondary_color; |
| 22 | protected $dynamic_class; |
| 23 | |
| 24 | public function __construct( array $settings, string $prefix ) { |
| 25 | $this->settings = $settings; |
| 26 | $this->prefix = $prefix; |
| 27 | |
| 28 | $this->normalize_settings(); |
| 29 | $this->setup_query(); |
| 30 | $this->prepare_render_data(); |
| 31 | |
| 32 | DiviFns::get_script_depends( $settings, $prefix ); |
| 33 | } |
| 34 | |
| 35 | protected function normalize_settings() { |
| 36 | if ( ! rtTPG()->hasPro() && ! in_array( $this->settings[ $this->prefix . '_layout' ], [ 'grid-layout1', 'grid-layout4', 'grid-layout3' ] ) ) { |
| 37 | $this->settings[ $this->prefix . '_layout' ] = 'grid-layout1'; |
| 38 | } |
| 39 | |
| 40 | $this->layout = $this->settings[ $this->prefix . '_layout' ] ?? 'grid-layout1'; |
| 41 | $this->layout_style = $this->settings['grid_layout_style'] ?? 'default'; |
| 42 | $this->layoutID = 'rt-tpg-container-' . wp_rand(); |
| 43 | $this->primary_color = ! empty( $this->settings['layout_primary_color'] ) ? $this->settings['layout_primary_color'] : ''; |
| 44 | $this->secondary_color = ! empty( $this->settings['layout_secondary_color'] ) ? $this->settings['layout_secondary_color'] : ''; |
| 45 | } |
| 46 | |
| 47 | protected function setup_query() { |
| 48 | $this->query_args = DiviFns::get_post_query( $this->settings, $this->prefix ); |
| 49 | $this->query = new WP_Query( $this->query_args ); |
| 50 | } |
| 51 | |
| 52 | protected function prepare_render_data() { |
| 53 | $posts_per_page = $this->settings['display_per_page'] ?? $this->settings['post_limit']; |
| 54 | $this->post_data = DiviFns::get_data_set( $this->settings, $this->query->max_num_pages, $posts_per_page, $this->prefix, 'divi' ); |
| 55 | |
| 56 | if ( isset( $this->settings['category_source'] ) ) { |
| 57 | $this->post_data[ $this->settings['post_type'] . '_taxonomy' ] = $this->settings['category_source']; |
| 58 | } |
| 59 | if ( isset( $this->settings['tag_source'] ) ) { |
| 60 | $this->post_data[ $this->settings['post_type'] . '_tags' ] = $this->settings['tag_source']; |
| 61 | } |
| 62 | |
| 63 | $this->template_path = Fns::tpg_template_path( $this->post_data, 'divi' ); |
| 64 | $this->dynamic_class = Fns::get_dynamic_class_gutenberg( $this->settings, 'divi' ); |
| 65 | } |
| 66 | |
| 67 | protected function get_wrapper_classes(): array { |
| 68 | $classes = []; |
| 69 | $layout = $this->layout; |
| 70 | $style = $this->layout_style; |
| 71 | $classes[] = str_replace( '-2', '', $layout ); |
| 72 | $classes[] = 'grid-behaviour'; |
| 73 | $classes[] = ( in_array( $layout, [ 'grid-layout2' ] ) ) ? 'tpg-even' : $style; |
| 74 | $classes[] = $this->prefix . '_layout_wrapper'; |
| 75 | |
| 76 | if ( 'masonry' === $style && in_array( $layout, [ 'grid-layout1', 'grid-layout3', 'grid-layout4' ] ) ) { |
| 77 | $classes[] = 'tpg-masonry'; |
| 78 | } |
| 79 | if ( in_array( $layout, [ 'grid-layout6', 'grid-layout6-2' ] ) && ( $this->settings['middle_border'] ?? 'yes' ) === 'no' ) { |
| 80 | $classes[] = 'disable-middle-border'; |
| 81 | } |
| 82 | |
| 83 | return $classes; |
| 84 | } |
| 85 | |
| 86 | protected function get_header_markup(): string { |
| 87 | ob_start(); |
| 88 | $is_carousel = ''; |
| 89 | |
| 90 | if ( rtTPG()->hasPro() && 'carousel' === ( $this->settings['filter_btn_style'] ?? '' ) && 'button' === ( $this->settings['filter_type'] ?? '' ) ) { |
| 91 | $is_carousel = 'carousel'; |
| 92 | } |
| 93 | ?> |
| 94 | <div class='tpg-header-wrapper <?php echo esc_attr( $is_carousel ); ?>'> |
| 95 | <?php |
| 96 | Fns::get_section_title( $this->settings, true ); |
| 97 | Fns::print_html( Fns::get_frontend_filter_markup( $this->settings, 'divi' ) ); |
| 98 | ?> |
| 99 | </div> |
| 100 | <?php |
| 101 | return ob_get_clean(); |
| 102 | } |
| 103 | |
| 104 | public function block_css() { |
| 105 | $css = ''; |
| 106 | if ( ! empty( $this->primary_color ) ) { |
| 107 | $css .= "--tpg-primary-color: $this->primary_color;"; |
| 108 | } |
| 109 | if ( ! empty( $this->secondary_color ) ) { |
| 110 | $css .= "--tpg-secondary-color: $this->secondary_color;"; |
| 111 | } |
| 112 | |
| 113 | return $css; |
| 114 | } |
| 115 | |
| 116 | protected function get_posts_markup(): string { |
| 117 | ob_start(); |
| 118 | |
| 119 | if ( 'current_query' == $this->settings['post_type'] && ( is_archive() || is_home() ) ) { |
| 120 | global $wp_query; |
| 121 | $this->query = $wp_query; |
| 122 | } |
| 123 | |
| 124 | if ( $this->query->have_posts() ) { |
| 125 | $pCount = 1; |
| 126 | while ( $this->query->have_posts() ) { |
| 127 | $this->query->the_post(); |
| 128 | set_query_var( 'tpg_post_count', $pCount ); |
| 129 | set_query_var( 'tpg_total_posts', $this->query->post_count ); |
| 130 | Fns::tpg_template( $this->post_data, 'divi' ); |
| 131 | $pCount ++; |
| 132 | } |
| 133 | } else { |
| 134 | printf( |
| 135 | "<div class='no_posts_found_text'>%s</div>", |
| 136 | esc_html( $this->settings['no_posts_found_text'] ?? __( 'No post found', 'the-post-grid' ) ) |
| 137 | ); |
| 138 | } |
| 139 | wp_reset_postdata(); |
| 140 | |
| 141 | return ob_get_clean(); |
| 142 | } |
| 143 | |
| 144 | public function render(): string { |
| 145 | ob_start(); |
| 146 | ?> |
| 147 | <div class="<?php echo esc_attr( $this->dynamic_class ); ?>" style='<?php echo esc_attr( $this->block_css() ) ?>'> |
| 148 | <div class="rt-container-fluid rt-tpg-container tpg-el-main-wrapper tpg-divi clearfix <?php echo esc_attr( $this->layout . '-main' ); ?>" |
| 149 | id="<?php echo esc_attr( $this->layoutID ); ?>" |
| 150 | data-layout="<?php echo esc_attr( $this->layout ); ?>" |
| 151 | data-grid-style="<?php echo esc_attr( $this->layout_style ); ?>" |
| 152 | data-sc-id="elementor" |
| 153 | data-el-settings='<?php echo Fns::is_filter_enable( $this->settings ) ? esc_attr( htmlspecialchars( wp_json_encode( $this->post_data ) ) ) : ''; ?>' |
| 154 | data-el-query='<?php echo Fns::is_filter_enable( $this->settings ) ? esc_attr( htmlspecialchars( wp_json_encode( $this->query_args ) ) ) : ''; ?>' |
| 155 | data-el-path='<?php echo Fns::is_filter_enable( $this->settings ) ? esc_attr( $this->template_path ) : ''; ?>'> |
| 156 | |
| 157 | <?php |
| 158 | Fns::render_loader_spinner(); |
| 159 | echo $this->get_header_markup(); |
| 160 | ?> |
| 161 | |
| 162 | <div class="rt-row rt-content-loader gutenberg-inner <?php echo esc_attr( implode( ' ', $this->get_wrapper_classes() ) ); ?>"> |
| 163 | <?php echo $this->get_posts_markup(); ?> |
| 164 | </div> |
| 165 | |
| 166 | <?php Fns::print_html( Fns::get_pagination_markup( $this->query, $this->settings ) ); ?> |
| 167 | </div> |
| 168 | </div> |
| 169 | <?php |
| 170 | |
| 171 | do_action( 'tpg_divi_script' ); |
| 172 | |
| 173 | return ob_get_clean(); |
| 174 | } |
| 175 | |
| 176 | } |