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