PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 1.6.4
Kubio AI Page Builder v1.6.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 3 years ago QueryLoopItemBase.php 3 years ago
QueryLoopBase.php
168 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 use Kubio\Core\StyleManager\DynamicStyles;
9
10 class QueryLoopBase extends BlockContainerBase {
11 const CONTAINER = 'container';
12 const INNER = 'inner';
13 const CENTER = 'center';
14 const INNER_GAPS = 'innerGaps';
15 const TYPOGRAPHY_HOLDERS = 'typographyHolders';
16
17 private $query_context = null;
18
19 public function mapDynamicStyleToElements() {
20 $dynamicStyles = array();
21
22 $typographyHoldersByMedia = $this->getStyleByMedia(
23 'typography.holders',
24 array(),
25 array(
26 'styledComponent' => $this->getDefaultElement(),
27 )
28 );
29
30
31 $dynamicStyles[ self::TYPOGRAPHY_HOLDERS ] = DynamicStyles::typographyHolders( $typographyHoldersByMedia );
32
33 return $dynamicStyles;
34 }
35
36 public function mapPropsToElements() {
37 $layout_media = $this->getPropByMedia( 'layout' );
38 $layout_helper = new LayoutHelper( $layout_media );
39 $masonry = $this->getAttribute( 'masonry', false );
40 $masonryClasses = array();
41 if ( $masonry ) {
42 $masonryClasses[] = 'kubio-query-loop--use-masonry';
43 }
44 $map = array();
45 $map[ self::CONTAINER ] = array( 'className' => $layout_helper->getRowGapClasses() );
46 $map[ self::INNER ] = array(
47 'className' => LodashBasic::concat( $layout_helper->getRowGapInnerClasses(), $layout_helper->getRowAlignClasses(), $masonryClasses ),
48 'innerHTML' => $this->renderList(),
49 );
50
51 return $map;
52 }
53
54 public function renderList() {
55
56 $posts = $this->getPostList();
57 $content = "<!--kubio post list : start-->\n";
58
59 if ( empty( $posts ) && $this->isUsingMainQuery() ) {
60 $post_type = get_post_type_object( $this->getPostType() );
61 $label = $post_type->labels->name;
62
63 $not_found_text = $this->getAttribute( 'notFound' );
64
65 if ( $not_found_text === null ) {
66 $not_found_text = __( 'No {post_title} found!', 'kubio' );
67 }
68
69 $content .= '<h2 class="kubio-empty-query-result">' . str_replace( '{post_title}', $label, $not_found_text ) . '</h2>';
70 } else {
71 foreach ( (array) $posts as $post ) {
72 foreach ( $this->block_data['innerBlocks'] as $inner_block ) {
73 $content .= (
74 new \WP_Block(
75 $inner_block,
76 array(
77 'postType' => $post->post_type,
78 'postId' => $post->ID,
79 )
80 )
81 )->render();
82 }
83 }
84 }
85
86 $content .= "\n<!--kubio post list : end-->";
87
88 return $content;
89 }
90
91 public function getPostList() {
92 if ( $this->isUsingMainQuery() ) {
93 /**
94 * @var \WP_Query $wp_query ;
95 */
96 global $wp_query;
97
98 return $wp_query->posts;
99 }
100
101 return $this->getPostsQueryList();
102 }
103
104 public function getQueryContext() {
105 if ( ! $this->query_context ) {
106 $this->query_context = array_merge(
107 $this->queryDefault(),
108 $this->block_context
109 );
110 }
111
112 return $this->query_context;
113 }
114
115 public function queryDefault() {
116 return array(
117 'queryId' => 1,
118 'useMainQuery' => false,
119 'query' => array(
120 'postType' => 'post',
121 'sticky' => false,
122 'post__in' => array(),
123 'post__not_in' => array(),
124 'exclude' => array(),
125 'categoryIds' => array(),
126 'perPage' => get_option( 'posts_per_page' ),
127 'offset' => 0,
128 'tagIds' => array(),
129 'author' => array(),
130 'search' => array(),
131 'orderBy' => 'date',
132 'order' => 'DESC',
133 ),
134 );
135 }
136
137 public function getPostsQueryList() {
138 $context = $this->getQueryContext();
139 $page_key = isset( $context['queryId'] ) ? 'query-' . $context['queryId'] . '-page' : 'query-page';
140 $page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT );
141
142 /** @noinspection PhpParamsInspection */
143 $query_args = build_query_vars_from_query_block( (object) array( 'context' => $context ), $page );
144 $query = new \WP_Query( $query_args );
145 return $query->posts;
146
147 }
148
149 public function getPostType() {
150
151 if ( $this->isUsingMainQuery() ) {
152 $post_type = get_query_var( 'post_type', '' );
153 if ( ! $post_type ) {
154 $post_type = 'post';
155 }
156 }
157
158 $context = $this->getQueryContext();
159
160 return $context['query']['postType'];
161 }
162
163 public function isUsingMainQuery() {
164 $context = $this->getQueryContext();
165 return isset( $context['useMainQuery'] ) && $context['useMainQuery'];
166 }
167 }
168