PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.3.1
Kubio AI Page Builder v2.3.1
2.8.5 2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / Core / Blocks / Query / QueryLoopBase.php
kubio / lib / src / Core / Blocks / Query Last commit date
QueryLoopBase.php 2 years ago QueryLoopItemBase.php 3 years ago
QueryLoopBase.php
153 lines
1 <?php
2
3 namespace Kubio\Core\Blocks\Query;
4
5 use Kubio\Core\Blocks\BlockContainerBase;
6 use Kubio\Core\Layout\LayoutHelper;
7 use Kubio\Core\LodashBasic;
8
9 class QueryLoopBase extends BlockContainerBase {
10 const CONTAINER = 'container';
11 const INNER = 'inner';
12 const CENTER = 'center';
13 const INNER_GAPS = 'innerGaps';
14
15 private $query_context = null;
16
17
18
19 public function mapPropsToElements() {
20 $layout_media = $this->getPropByMedia( 'layout' );
21 $layout_helper = new LayoutHelper( $layout_media );
22 $masonry = $this->getAttribute( 'masonry', false );
23 $masonryClasses = array();
24 if ( $masonry ) {
25 $masonryClasses[] = 'kubio-query-loop--use-masonry';
26 }
27 $map = array();
28 $map[ self::CONTAINER ] = array( 'className' => $layout_helper->getRowGapClasses() );
29 $map[ self::INNER ] = array(
30 'className' => LodashBasic::concat( $layout_helper->getRowGapInnerClasses(), $layout_helper->getRowAlignClasses(), $masonryClasses ),
31 'innerHTML' => $this->renderList(),
32 );
33
34 return $map;
35 }
36
37 public function renderList() {
38
39 $posts = $this->getPostList();
40 $content = "<!--kubio post list : start-->\n";
41
42 if ( empty( $posts ) && $this->isUsingMainQuery() ) {
43 $post_type = get_post_type_object( $this->getPostType() );
44 $label = $post_type->labels->name;
45
46 $not_found_text = $this->getAttribute( 'notFound' );
47
48 if ( $not_found_text === null ) {
49 $not_found_text = __( 'No {post_title} found!', 'kubio' );
50 }
51
52 $content .= '<h2 class="kubio-empty-query-result">' . str_replace( '{post_title}', $label, $not_found_text ) . '</h2>';
53 } else {
54 foreach ( (array) $posts as $post ) {
55 foreach ( $this->block_data['innerBlocks'] as $inner_block ) {
56 $content .= (
57 new \WP_Block(
58 $inner_block,
59 array(
60 'postType' => $post->post_type,
61 'postId' => $post->ID,
62 )
63 )
64 )->render();
65 }
66 }
67 }
68
69 $content .= "\n<!--kubio post list : end-->";
70
71 return $content;
72 }
73
74 public function getPostList() {
75 if ( $this->isUsingMainQuery() ) {
76 /**
77 * @var \WP_Query $wp_query ;
78 */
79 global $wp_query;
80
81 return $wp_query->posts;
82 }
83
84 return $this->getPostsQueryList();
85 }
86
87 public function getQueryContext() {
88 if ( ! $this->query_context ) {
89 $this->query_context = array_merge(
90 $this->queryDefault(),
91 $this->block_context
92 );
93 }
94
95 return $this->query_context;
96 }
97
98 public function queryDefault() {
99 return array(
100 'queryId' => 1,
101 'useMainQuery' => false,
102 'query' => array(
103 'postType' => 'post',
104 'sticky' => false,
105 'post__in' => array(),
106 'post__not_in' => array(),
107 'exclude' => array(),
108 'categoryIds' => array(),
109 'perPage' => get_option( 'posts_per_page' ),
110 'offset' => 0,
111 'tagIds' => array(),
112 'author' => array(),
113 'search' => array(),
114 'orderBy' => 'date',
115 'order' => 'DESC',
116 ),
117 );
118 }
119
120 public function getPostsQueryList() {
121 $context = $this->getQueryContext();
122 $page_key = isset( $context['queryId'] ) ? 'query-' . $context['queryId'] . '-page' : 'query-page';
123 $page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT );
124
125 /** @noinspection PhpParamsInspection */
126 $query_args = build_query_vars_from_query_block( (object) array( 'context' => $context ), $page );
127 $query_args['ignore_sticky_posts'] = $context['query']['sticky'] ? false : true;
128 $query = new \WP_Query( $query_args );
129
130 return $query->posts;
131
132 }
133
134 public function getPostType() {
135
136 if ( $this->isUsingMainQuery() ) {
137 $post_type = get_query_var( 'post_type', '' );
138 if ( ! $post_type ) {
139 $post_type = 'post';
140 }
141 }
142
143 $context = $this->getQueryContext();
144
145 return $context['query']['postType'];
146 }
147
148 public function isUsingMainQuery() {
149 $context = $this->getQueryContext();
150 return isset( $context['useMainQuery'] ) && $context['useMainQuery'];
151 }
152 }
153