PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / trunk
ShopBuilder – WooCommerce Builder For Elementor vtrunk
3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.3.0 All 62 releases
shopbuilder / app / Models / PostQueryArgs.php

PostQueryArgs.php in ShopBuilder – WooCommerce Builder For Elementor trunk, at app/Models/PostQueryArgs.php

196 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class to build up post query args.
4 *
5 * @package RadiusTheme\SB
6 */
7
8 namespace RadiusTheme\SB\Models;
9
10 // Do not allow directly accessing this file.
11 use RadiusTheme\SB\Helpers\Fns;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit( 'This script cannot be accessed directly.' );
15 }
16
17 /**
18 * Class PostQueryArgs
19 *
20 * Builds post query arguments for product queries.
21 *
22 * @package RadiusTheme\SB
23 * Builds query arguments for fetching posts in ShopBuilder widgets.
24 */
25 class PostQueryArgs {
26 /**
27 * Query Args.
28 *
29 * @var array
30 */
31 private $args = [];
32
33 /**
34 * Meta values.
35 *
36 * @var array
37 */
38 private $meta = [];
39
40 /**
41 * Method to build args
42 *
43 * @param array $meta Meta values.
44 * @return array
45 */
46 public function buildArgs( array $meta ) {
47 $this->meta = $meta;
48 $this->getPostType();
49 $this->post_params()
50 ->include_exclude()
51 ->order()
52 ->pagination()
53 ->tax_params();
54
55 return apply_filters( 'rtsb/elements/post_query_args', $this->args, $this->meta );
56 }
57 /**
58 * Post type.
59 *
60 * @return void
61 */
62 private function getPostType() {
63 $this->args['post_type'] = [ 'post' ];
64 $this->args['post_status'] = 'publish';
65 }
66 /**
67 * Set the post type to "post" for blog queries.
68 *
69 * @return $this
70 */
71 private function post_params() {
72 $limit = ( ( empty( $this->meta['limit'] ) || '-1' === $this->meta['limit'] ) ? 10000000 : absint( $this->meta['limit'] ) );
73 $offset = ! empty( $this->meta['offset'] ) ? absint( $this->meta['offset'] ) : 0;
74 $this->args['posts_per_page'] = $limit;
75
76 if ( $offset ) {
77 $this->args['offset'] = $offset;
78 }
79
80 return $this;
81 }
82 /**
83 * Add include and exclude post IDs to the query.
84 *
85 * @return $this
86 */
87 private function include_exclude() {
88 if ( ! empty( $this->meta['post_in'] ) ) {
89 $this->args['post__in'] = array_map( 'intval', (array) $this->meta['post_in'] );
90 }
91
92 if ( ! empty( $this->meta['post_not_in'] ) ) {
93 // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in
94 $this->args['post__not_in'] = array_map( 'intval', (array) $this->meta['post_not_in'] );
95 }
96
97 return $this;
98 }
99 /**
100 * Set ordering parameters.
101 *
102 * @return $this
103 */
104 private function order() {
105 $order_by = ( isset( $this->meta['order_by'] ) ? esc_html( $this->meta['order_by'] ) : null );
106 $order = ( isset( $this->meta['order'] ) ? esc_html( $this->meta['order'] ) : null );
107 if ( $order ) {
108 $this->args['order'] = $order;
109 }
110
111 if ( $order_by ) {
112 $this->args['orderby'] = $order_by;
113
114 }
115
116 return $this;
117 }
118
119 /**
120 * Set pagination parameters.
121 *
122 * @return $this
123 */
124 private function pagination() {
125 $pagination = ! empty( $this->meta['pagination'] );
126 $limit = ( ( empty( $this->meta['limit'] ) || '-1' === $this->meta['limit'] ) ? 10000000 : absint( $this->meta['limit'] ) );
127
128 if ( $pagination ) {
129 unset( $this->args['offset'] );
130
131 $posts_per_page = ( ! empty( $this->meta['posts_per_page'] ) ? absint( $this->meta['posts_per_page'] ) : $limit );
132
133 if ( $posts_per_page > $limit ) {
134 $posts_per_page = $limit;
135 }
136 $this->args['posts_per_page'] = $posts_per_page;
137
138 // Prefer `paged` (archive pagination), fall back to `page` (paginated singular posts).
139 // Handles the case where the shop/archive is set as the front page.
140 $paged = get_query_var( 'paged' );
141 if ( ! $paged ) {
142 $paged = get_query_var( 'page' );
143 }
144 if ( ! $paged ) {
145 $paged = 1;
146 }
147
148 $offset = $posts_per_page * ( (int) $paged - 1 );
149 $this->args['paged'] = $paged;
150
151 if ( absint( $this->args['posts_per_page'] ) > $limit - $offset ) {
152 $this->args['posts_per_page'] = $limit - $offset;
153 $this->args['offset'] = $offset;
154 }
155 }
156
157 return $this;
158 }
159
160 /**
161 * Add taxonomy-related parameters (categories, tags).
162 *
163 * @return $this
164 */
165 private function tax_params() {
166 $categories = isset( $this->meta['categories'] ) ? array_filter( $this->meta['categories'] ) : [];
167 $tags = isset( $this->meta['tags'] ) ? array_filter( $this->meta['tags'] ) : [];
168 $relation = $this->meta['relation'] ?? 'AND';
169
170 if ( ! empty( $categories ) || ! empty( $tags ) ) {
171 $tax_query = [ 'relation' => $relation ];
172
173 if ( ! empty( $categories ) ) {
174 $tax_query[] = [
175 'taxonomy' => 'category',
176 'field' => 'term_id',
177 'terms' => $categories,
178 'operator' => 'IN',
179 ];
180 }
181
182 if ( ! empty( $tags ) ) {
183 $tax_query[] = [
184 'taxonomy' => 'post_tag',
185 'field' => 'term_id',
186 'terms' => $tags,
187 'operator' => 'IN',
188 ];
189 }
190 $this->args['tax_query'] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
191 }
192
193 return $this;
194 }
195 }
196