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