Plugin Directory

source: jeg-elementor-kit/trunk/class/elements/views/class-post-block-view.php

Last change on this file was 3038362, checked in by fauzanjegstudio, 14 months ago

Update Jeg Elementor Kit v2.6.3

  • Property svn:eol-style set to native
File size: 13.5 KB
Line 
1<?php
2/**
3 * Post Block View Class
4 *
5 * @package jeg-elementor-kit
6 * @author Jegtheme
7 * @since 1.0.0
8 */
9
10namespace Jeg\Elementor_Kit\Elements\Views;
11
12/**
13 * Class Post_Block_View
14 *
15 * @package Jeg\Elementor_Kit\Elements\Views
16 */
17class Post_Block_View extends View_Abstract {
18        /**
19         * Render Module
20         *
21         * @param  array  $attr         Array of attribute.
22         * @param  string $column_class Class string.
23         * @return string
24         */
25        public function render_element( $attr, $column_class ) {
26                $attr = $this->filter_post_attribute( $attr );
27                $this->set_attribute( $attr );
28
29                $content        = $this->render_block_element();
30                $settings       = $this->render_settings();
31                $postblock_type = 'postblock-' . esc_attr( $this->attribute['sg_content_postblock_type'] );
32                $pagination     = 'jkit-pagination-' . esc_attr( $this->attribute['pagination_mode'] );
33
34                return $this->render_wrapper(
35                        'postblock',
36                        $content,
37                        array( $postblock_type, $pagination, 'post-element' ),
38                        array(
39                                'id'       => $this->unique_id,
40                                'settings' => $settings,
41                        )
42                );
43        }
44
45        /**
46         * Render result element
47         *
48         * @param array $results Result element.
49         * @param array $attr Options.
50         */
51        public function render_result_element( $results, $attr ) {
52                $attr = $this->filter_post_attribute( $attr );
53                $this->set_attribute( $attr );
54
55                $content  = $this->render_result_block_element( $results );
56                $settings = $this->render_settings();
57
58                $breakpoint     = 'type-1' === $this->attribute['sg_content_postblock_type'] || 'type-4' === $this->attribute['sg_content_postblock_type'] ? 'break-point-' . esc_attr( $this->attribute['sg_content_breakpoint'] ) : '';
59                $postblock_type = 'postblock-' . esc_attr( $this->attribute['sg_content_postblock_type'] );
60                $pagination     = 'jkit-pagination-' . esc_attr( $this->attribute['pagination_mode'] );
61
62                return $this->render_wrapper(
63                        'postblock',
64                        $content,
65                        array( $postblock_type, $pagination, $breakpoint, 'post-element' ),
66                        array(
67                                'id'       => $this->unique_id,
68                                'settings' => $settings,
69                        )
70                );
71        }
72
73        /**
74         * Ajax request handler
75         */
76        public function ajax_request() {
77                // @codingStandardsIgnoreStart sanitize value using jeg_sanitize_array
78                $attr        = jeg_sanitize_array( $_REQUEST['data'] );
79                // @codingStandardsIgnoreEnd
80                $query_param = $this->build_ajax_query( $attr );
81                $results     = $this->build_query( $query_param );
82
83                $this->set_attribute( $attr['attr'] );
84
85                if ( ! empty( $results['result'] ) ) {
86                        if ( 1 === intval( $attr['current_page'] ) && 'nextprev' !== $attr['attr']['pagination_mode'] ) {
87                                $content = $this->render_column( $results['result'] );
88                        } else {
89                                $content = $this->render_column_alt( $results['result'] );
90                        }
91                } else {
92                        $content = $this->empty_content();
93                }
94
95                wp_send_json(
96                        array(
97                                'content' => $content,
98                                'next'    => $results['next'],
99                                'prev'    => $results['prev'],
100                        )
101                );
102        }
103
104        /**
105         * Get post excerpt
106         *
107         * @param  int|\WP_Post $post Post object.
108         * @return mixed
109         */
110        protected function get_excerpt( $post ) {
111                $excerpt        = null;
112                $excerpt_enable = 'yes' === $this->attribute['sg_content_excerpt_enable'];
113
114                if ( $excerpt_enable ) {
115                        $excerpt = $post->post_excerpt;
116
117                        if ( empty( $excerpt ) ) {
118                                $excerpt = $post->post_content;
119                        }
120
121                        $excerpt = preg_replace( '/\[[^\]]+\]/', '', $excerpt );
122                        $excerpt = wp_trim_words( $excerpt, $this->excerpt_length(), $this->excerpt_more() );
123                        $excerpt = apply_filters( 'jeg_module_excerpt', $excerpt, $post->ID, $this->excerpt_length(), $this->excerpt_more() );
124                        $excerpt = '<div class="jkit-post-excerpt"><p>' . $excerpt . '</p></div>';
125                }
126
127                return $excerpt;
128        }
129
130        /**
131         * Get post read more button
132         *
133         * @param  int|\WP_Post $post Post object.
134         * @return mixed
135         */
136        protected function get_readmore( $post, $post_title ) {
137                $readmore        = null;
138                $readmore_enable = 'yes' === $this->attribute['sg_content_readmore_enable'];
139
140                if ( $readmore_enable ) {
141                        $icon          = $this->render_icon_element( $this->attribute['sg_content_readmore_icon'] );
142                        $icon_position = esc_attr( $this->attribute['sg_content_readmore_icon_position'] );
143                        $text          = esc_attr( $this->attribute['sg_content_readmore_text'] );
144
145                        if ( 'before' === $icon_position ) {
146                                $readmore = $icon . $text;
147                        } else {
148                                $readmore = $text . $icon;
149                        }
150
151                        $readmore =
152                        '<div class="jkit-meta-readmore icon-position-' . $icon_position . '">
153                <a title="' . $post_title . '" href="' . esc_url( get_the_permalink( $post ) ) . '" class="jkit-readmore">' . $readmore . '</a>
154            </div>';
155                }
156
157                return $readmore;
158        }
159
160        /**
161         * Get comment bubble icon
162         *
163         * @param  int|\WP_Post $post Post object.
164         * @return mixed
165         */
166        protected function get_comment_bubble( $post ) {
167                $comment        = null;
168                $comment_enable = 'yes' === $this->attribute['sg_content_comment_enable'];
169
170                if ( $comment_enable ) {
171                        $number        = jkit_get_comments_number( $post->ID );
172                        $icon          = $this->render_icon_element( $this->attribute['sg_content_comment_icon'] );
173                        $icon_position = esc_attr( $this->attribute['sg_content_comment_icon_position'] );
174
175                        if ( 'before' === $icon_position ) {
176                                $comment = $icon . '<span>' . $number . '</span>';
177                        } else {
178                                $comment = '<span>' . $number . '</span>' . $icon;
179                        }
180
181                        $comment =
182                        '<div class="jkit-meta-comment icon-position-' . $icon_position . '">
183                <a href="' . jkit_get_respond_link( $post->ID ) . '" >
184                    ' . $comment . '
185                </a>
186            </div>';
187                }
188
189                return $comment;
190        }
191
192        /**
193         * Get excerpt length
194         *
195         * @return int
196         */
197        public function excerpt_length() {
198                if ( isset( $this->attribute['sg_content_excerpt_length'] ) ) {
199                        if ( isset( $this->attribute['sg_content_excerpt_length']['size'] ) ) {
200                                return intval( $this->attribute['sg_content_excerpt_length']['size'] );
201                        }
202
203                        return intval( $this->attribute['sg_content_excerpt_length'] );
204                } else {
205                        return 20;
206                }
207        }
208
209        /**
210         * Get excerpt more
211         *
212         * @return string
213         */
214        public function excerpt_more() {
215                return isset( $this->attribute['sg_content_excerpt_more'] ) ? esc_attr( $this->attribute['sg_content_excerpt_more'] ) : ' ...';
216        }
217
218        /**
219         * Filter keys to ajax post request
220         *
221         * @return string
222         */
223        public function get_ajax_param() {
224                return array(
225                        'lang',
226                        'post_type',
227                        'number_post',
228                        'post_offset',
229                        'unique_content',
230                        'include_post',
231                        'exclude_post',
232                        'include_category',
233                        'exclude_category',
234                        'include_author',
235                        'include_tag',
236                        'exclude_tag',
237                        'sort_by',
238                        'sg_content_postblock_type',
239                        'sg_content_element_order',
240                        'sg_content_breakpoint',
241                        'sg_content_image_size_imagesize_size',
242                        'sg_content_title_html_tag',
243                        'sg_content_category_enable',
244                        'sg_content_excerpt_enable',
245                        'sg_content_excerpt_length',
246                        'sg_content_excerpt_more',
247                        'sg_content_readmore_enable',
248                        'sg_content_readmore_icon',
249                        'sg_content_readmore_icon_position',
250                        'sg_content_readmore_text',
251                        'sg_content_comment_enable',
252                        'sg_content_comment_icon',
253                        'sg_content_comment_icon_position',
254                        'sg_content_meta_enable',
255                        'sg_content_meta_author_enable',
256                        'sg_content_meta_author_by_text',
257                        'sg_content_meta_author_icon',
258                        'sg_content_meta_author_icon_position',
259                        'sg_content_meta_date_enable',
260                        'sg_content_meta_date_type',
261                        'sg_content_meta_date_format',
262                        'sg_content_meta_date_format_custom',
263                        'sg_content_meta_date_icon',
264                        'sg_content_meta_date_icon_position',
265                        'st_category_position',
266                        'pagination_mode',
267                        'pagination_loadmore_text',
268                        'pagination_loading_text',
269                        'pagination_number_post',
270                        'pagination_scroll_limit',
271                        'pagination_icon',
272                        'pagination_icon_position',
273                );
274        }
275
276        /**
277         * Build primary category element
278         *
279         * @param  int $post_id Post ID.
280         * @return array|null|object|string|\WP_Error
281         */
282        public function get_primary_category( $post_id ) {
283                $cat_id          = jeg_get_primary_category( $post_id );
284                $category_enable = 'yes' === $this->attribute['sg_content_category_enable'];
285                $category        = '';
286
287                if ( $category_enable && $cat_id ) {
288                        $category = get_category( $cat_id );
289                        $position = 'type-3' === $this->attribute['sg_content_postblock_type'] ? 'position-' . esc_attr( $this->attribute['st_category_position'] ) : '';
290                        $class    = 'class="category-' . esc_attr( $category->slug ) . '"';
291                        $category = '<div class="jkit-post-category ' . $position . '"><span><a href="' . esc_url( get_category_link( $cat_id ) ) . '" ' . $class . '>' . esc_attr( $category->name ) . '</a></span></div>';
292                }
293
294                return $category;
295        }
296
297        /**
298         * Build post meta 1
299         *
300         * @param  int|\WP_Post $post Post object.
301         * @return mixed
302         */
303        public function post_meta( $post ) {
304                $meta        = null;
305                $meta_enable = $this->attribute['sg_content_meta_enable'];
306
307                if ( $meta_enable ) {
308                        $author_output = null;
309                        $date_output   = null;
310
311                        $author_enable = $this->attribute['sg_content_meta_author_enable'];
312                        $date_enable   = $this->attribute['sg_content_meta_date_enable'];
313
314                        if ( $author_enable ) {
315                                $author      = $post->post_author;
316                                $author_url  = esc_url( get_author_posts_url( $author ) );
317                                $author_name = esc_attr( get_the_author_meta( 'display_name', $author ) );
318                                $author_by   = esc_attr( $this->attribute['sg_content_meta_author_by_text'] );
319
320                                $icon          = $this->render_icon_element( $this->attribute['sg_content_meta_author_icon'] );
321                                $icon_position = esc_attr( $this->attribute['sg_content_meta_author_icon_position'] );
322
323                                if ( 'before' === $icon_position ) {
324                                        $author_output = '<div class="jkit-meta-author icon-position-' . $icon_position . '">' . $icon . '<span class="by">' . $author_by . '</span><a href="' . $author_url . '">' . $author_name . '</a></div>';
325                                } else {
326                                        $author_output = '<div class="jkit-meta-author icon-position-' . $icon_position . '"><span class="by">' . $author_by . '</span><a href="' . $author_url . '">' . $author_name . '</a>' . $icon . '</div>';
327                                }
328                        }
329
330                        if ( $date_enable ) {
331                                $icon          = $this->render_icon_element( $this->attribute['sg_content_meta_date_icon'] );
332                                $icon_position = esc_attr( $this->attribute['sg_content_meta_date_icon_position'] );
333
334                                if ( 'before' === $icon_position ) {
335                                        $date_output = '<div class="jkit-meta-date icon-position-' . $icon_position . '">' . $icon . $this->format_date( $post ) . '</div>';
336                                } else {
337                                        $date_output = '<div class="jkit-meta-date icon-position-' . $icon_position . '">' . $this->format_date( $post ) . $icon . '</div>';
338                                }
339                        }
340
341                        $meta = '<div class="jkit-post-meta">' . $author_output . $date_output . '</div>';
342                }
343
344                return apply_filters( 'jkit_post_block_meta', $meta, $post, $this );
345        }
346
347        /**
348         * Format Date for frontend view.
349         *
350         * @param  int|\WP_Post $post Post object.
351         * @return mixed
352         */
353        public function format_date( $post ) {
354                $date_type = isset( $this->attribute['sg_content_meta_date_type'] ) ? $this->attribute['sg_content_meta_date_type'] : 'published';
355
356                if ( 'both' === $date_type ) {
357                        $output = $this->get_post_date( $post, $this->attribute['sg_content_meta_date_format'], 'published', $this->attribute['sg_content_meta_date_format_custom'] );
358                        $output = $output . esc_html__( ' - Updated on ', 'jeg-elementor-kit' );
359                        $output = $output . $this->get_post_date( $post, $this->attribute['sg_content_meta_date_format'], 'modified', $this->attribute['sg_content_meta_date_format_custom'] );
360                } else {
361                        $output = $this->get_post_date( $post, $this->attribute['sg_content_meta_date_format'], $date_type, $this->attribute['sg_content_meta_date_format_custom'] );
362                }
363
364                return $output;
365        }
366
367        /**
368         * Build column type 1 method
369         *
370         * @param array $results Result element.
371         * @return string
372         */
373        public function build_column( $results ) {
374                $block      = '';
375                $image_size = esc_attr( $this->attribute['sg_content_image_size_imagesize_size'] );
376                $html_tag   = \Elementor\Utils::validate_html_tag( $this->attribute['sg_content_title_html_tag'] );
377                $type       = esc_attr( $this->attribute['sg_content_postblock_type'] );
378                $order      = explode( ',', $this->attribute['sg_content_element_order'] );
379
380                foreach ( $results as $post ) {
381                        $thumbnail        = $this->get_thumbnail( $post->ID, $image_size );
382                        $primary_category = $this->get_primary_category( $post->ID );
383                        $post_url         = esc_url( get_the_permalink( $post ) );
384                        $post_title       = esc_attr( get_the_title( $post ) );
385                        $content          = '';
386
387                        foreach ( $order as $item ) {
388                                if ( 'title' === $item ) {
389                                        $content .=
390                                                '<' . $html_tag . ' class="jkit-post-title">
391                                                        <a href="' . $post_url . '">' . $post_title . '</a>
392                                                </' . $html_tag . '>';
393                                }
394
395                                if ( 'meta' === $item ) {
396                                        $content .= $this->post_meta( $post );
397                                }
398
399                                if ( 'excerpt' === $item ) {
400                                        $content .= $this->get_excerpt( $post );
401                                }
402
403                                if ( 'read' === $item ) {
404                                        $content .=
405                                                '<div class="jkit-post-meta-bottom">
406                                                        ' . $this->get_readmore( $post, $post_title ) . $this->get_comment_bubble( $post ) . '
407                                                </div>';
408                                }
409                        }
410
411                        $thumb = jkit_edit_post( $post->ID ) . '<a aria-label="' . $post_title . '" href="' . $post_url . '">' . $thumbnail . '</a>';
412
413                        if ( 'type-3' === $type ) {
414                                $block .=
415                                '<article ' . jeg_post_class( 'jkit-post', $post->ID ) . '>
416                    <div class="jkit-thumb">' . $thumb . $primary_category . '</div>
417                    <div class="jkit-postblock-content">' . $content . '</div>
418                </article>';
419                        } else {
420                                $block .=
421                                '<article ' . jeg_post_class( 'jkit-post', $post->ID ) . '>
422                    <div class="jkit-thumb">' . $thumb . '</div>
423                    <div class="jkit-postblock-content">' . $primary_category . $content . '</div>
424                </article>';
425                        }
426                }
427
428                return $block;
429        }
430}
Note: See TracBrowser for help on using the repository browser.