PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.6.3
ShopBuilder – WooCommerce Builder For Elementor v2.6.3
3.4.2 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 All 63 releases
shopbuilder / app / Models / PostQueryArgs.php

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

184 lines 4.3 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 class PostQueryArgs {
17 /**
18 * Query Args.
19 *
20 * @var array
21 */
22 private $args = [];
23
24 /**
25 * Meta values.
26 *
27 * @var array
28 */
29 private $meta = [];
30
31 /**
32 * Method to build args
33 *
34 * @param array $meta Meta values.
35 * @return array
36 */
37 public function buildArgs( array $meta ) {
38 $this->meta = $meta;
39 $this->getPostType();
40 $this->post_params()
41 ->include_exclude()
42 ->order()
43 ->pagination()
44 ->tax_params();
45
46 return apply_filters( 'rtsb/elements/post_query_args', $this->args, $this->meta );
47 }
48 /**
49 * Post type.
50 *
51 * @return void
52 */
53 private function getPostType() {
54 $this->args['post_type'] = [ 'post' ];
55 $this->args['post_status'] = 'publish';
56 }
57 /**
58 * Set the post type to "post" for blog queries.
59 *
60 * @return $this
61 */
62 private function post_params() {
63 $limit = ( ( empty( $this->meta['limit'] ) || '-1' === $this->meta['limit'] ) ? 10000000 : absint( $this->meta['limit'] ) );
64 $offset = ! empty( $this->meta['offset'] ) ? absint( $this->meta['offset'] ) : 0;
65 $this->args['posts_per_page'] = $limit;
66
67 if ( $offset ) {
68 $this->args['offset'] = $offset;
69 }
70
71 return $this;
72 }
73 /**
74 * Add include and exclude post IDs to the query.
75 *
76 * @return $this
77 */
78 private function include_exclude() {
79 if ( ! empty( $this->meta['post_in'] ) ) {
80 $this->args['post__in'] = array_map( 'intval', (array) $this->meta['post_in'] );
81 }
82
83 if ( ! empty( $this->meta['post_not_in'] ) ) {
84 // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in
85 $this->args['post__not_in'] = array_map( 'intval', (array) $this->meta['post_not_in'] );
86 }
87
88 return $this;
89 }
90 /**
91 * Set ordering parameters.
92 *
93 * @return $this
94 */
95 private function order() {
96 $order_by = ( isset( $this->meta['order_by'] ) ? esc_html( $this->meta['order_by'] ) : null );
97 $order = ( isset( $this->meta['order'] ) ? esc_html( $this->meta['order'] ) : null );
98 if ( $order ) {
99 $this->args['order'] = $order;
100 }
101
102 if ( $order_by ) {
103 $this->args['orderby'] = $order_by;
104
105 }
106
107 return $this;
108 }
109
110 /**
111 * Set pagination parameters.
112 *
113 * @return $this
114 */
115 private function pagination() {
116 $pagination = ! empty( $this->meta['pagination'] );
117 $limit = ( ( empty( $this->meta['limit'] ) || '-1' === $this->meta['limit'] ) ? 10000000 : absint( $this->meta['limit'] ) );
118
119 if ( $pagination ) {
120 unset( $this->args['offset'] );
121
122 $posts_per_page = ( ! empty( $this->meta['posts_per_page'] ) ? absint( $this->meta['posts_per_page'] ) : $limit );
123
124 if ( $posts_per_page > $limit ) {
125 $posts_per_page = $limit;
126 }
127
128 $this->args['posts_per_page'] = $posts_per_page;
129
130 if ( is_front_page() ) {
131 $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
132 } else {
133 $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
134 }
135
136 $offset = $posts_per_page * ( (int) $paged - 1 );
137 $this->args['paged'] = $paged;
138
139 if ( absint( $this->args['posts_per_page'] ) > $limit - $offset ) {
140 $this->args['posts_per_page'] = $limit - $offset;
141 $this->args['offset'] = $offset;
142 }
143 }
144
145 return $this;
146 }
147
148 /**
149 * Add taxonomy-related parameters (categories, tags).
150 *
151 * @return $this
152 */
153 private function tax_params() {
154 $categories = isset( $this->meta['categories'] ) ? array_filter( $this->meta['categories'] ) : [];
155 $tags = isset( $this->meta['tags'] ) ? array_filter( $this->meta['tags'] ) : [];
156 $relation = $this->meta['relation'] ?? 'AND';
157
158 if ( ! empty( $categories ) || ! empty( $tags ) ) {
159 $tax_query = [ 'relation' => $relation ];
160
161 if ( ! empty( $categories ) ) {
162 $tax_query[] = [
163 'taxonomy' => 'category',
164 'field' => 'term_id',
165 'terms' => $categories,
166 'operator' => 'IN',
167 ];
168 }
169
170 if ( ! empty( $tags ) ) {
171 $tax_query[] = [
172 'taxonomy' => 'post_tag',
173 'field' => 'term_id',
174 'terms' => $tags,
175 'operator' => 'IN',
176 ];
177 }
178 $this->args['tax_query'] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
179 }
180
181 return $this;
182 }
183 }
184