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