7
7
namespace OWC \PDC \Base \Repositories ;
8
8
9
9
use Closure ;
10
- use WP_Post ;
11
- use WP_Query ;
10
+ use OWC \PDC \Base \Exceptions \PropertyNotExistsException ;
12
11
use OWC \PDC \Base \Support \CreatesFields ;
13
12
use OWC \PDC \Base \Support \Traits \QueryHelpers ;
14
- use OWC \PDC \Base \Exceptions \PropertyNotExistsException ;
13
+ use WP_Post ;
14
+ use WP_Query ;
15
15
16
16
/**
17
17
* PDC item object with default quering and methods.
@@ -98,35 +98,29 @@ public function __construct()
98
98
99
99
/**
100
100
* Get all the items from the database.
101
- *
102
- * @return array
103
101
*/
104
102
public function all (): array
105
103
{
106
104
$ args = array_merge ($ this ->queryArgs , [
107
105
'post_type ' => [$ this ->posttype ],
108
106
]);
109
107
110
- $ this ->query = new WP_Query ($ args );
108
+ $ this ->query = new WP_Query ($ this -> cleanParams ( $ args) );
111
109
112
110
return array_map ([$ this , 'transform ' ], $ this ->getQuery ()->posts );
113
111
}
114
112
115
113
/**
116
114
* Find a particular pdc item by ID.
117
- *
118
- * @param int $id
119
- *
120
- * @return array
121
115
*/
122
- public function find (int $ id )
116
+ public function find (int $ id ): ? array
123
117
{
124
118
$ args = array_merge ($ this ->queryArgs , [
125
119
'p ' => $ id ,
126
120
'post_type ' => [$ this ->posttype ],
127
121
]);
128
122
129
- $ this ->query = new WP_Query ($ args );
123
+ $ this ->query = new WP_Query ($ this -> cleanParams ( $ args) );
130
124
131
125
if (empty ($ this ->getQuery ()->posts )) {
132
126
return null ;
@@ -137,19 +131,15 @@ public function find(int $id)
137
131
138
132
/**
139
133
* Find a particular pdc item by slug.
140
- *
141
- * @param string $slug
142
- *
143
- * @return array|null
144
134
*/
145
- public function findBySlug (string $ slug )
135
+ public function findBySlug (string $ slug ): ? array
146
136
{
147
137
$ args = array_merge ($ this ->queryArgs , [
148
138
'name ' => $ slug ,
149
139
'post_type ' => [$ this ->posttype ],
150
140
]);
151
141
152
- $ this ->query = new WP_Query ($ args );
142
+ $ this ->query = new WP_Query ($ this -> cleanParams ( $ args) );
153
143
154
144
if (empty ($ this ->getQuery ()->posts )) {
155
145
return null ;
@@ -158,6 +148,53 @@ public function findBySlug(string $slug)
158
148
return $ this ->transform (reset ($ this ->getQuery ()->posts ));
159
149
}
160
150
151
+ protected function cleanParams (array $ args ): array
152
+ {
153
+ $ args = $ this ->validatePostStatusParam ($ args );
154
+ $ args = $ this ->cleanWronglyNestedQueryParams ($ args , 'tax_query ' );
155
+ $ args = $ this ->cleanWronglyNestedQueryParams ($ args , 'meta_query ' );
156
+
157
+ return $ args ;
158
+ }
159
+
160
+ protected function validatePostStatusParam (array $ args ): array
161
+ {
162
+ if (empty ($ args ['post_status ' ])) {
163
+ return $ args ;
164
+ }
165
+
166
+ if (! is_string ($ args ['post_status ' ]) && ! is_array ($ args ['post_status ' ])) {
167
+ unset($ args ['post_status ' ]);
168
+
169
+ return $ args ;
170
+ }
171
+
172
+ if (is_string ($ args ['post_status ' ])) {
173
+ $ args ['post_status ' ] = [$ args ['post_status ' ]];
174
+ }
175
+
176
+ if (! \is_user_logged_in ()) {
177
+ $ args ['post_status ' ] = ['publish ' ];
178
+ }
179
+
180
+ return $ args ;
181
+ }
182
+
183
+ protected function cleanWronglyNestedQueryParams (array $ args , string $ key ): array
184
+ {
185
+ if (empty ($ args [$ key ]) || ! is_array ($ args [$ key ])) {
186
+ return $ args ;
187
+ }
188
+
189
+ foreach ($ args [$ key ] as &$ query ) {
190
+ if (is_array ($ query ) && ! empty ($ query [0 ])) {
191
+ $ query = call_user_func_array ('array_merge ' , $ query );
192
+ }
193
+ }
194
+
195
+ return $ args ;
196
+ }
197
+
161
198
/**
162
199
* Get the WP_Query object.
163
200
*
@@ -273,7 +310,7 @@ public function transform(WP_Post $post)
273
310
'date ' => $ post ->post_date ,
274
311
'slug ' => $ post ->post_name ,
275
312
'post_status ' => $ post ->post_status ,
276
- 'protected ' => ! $ this ->isAllowed ($ post )
313
+ 'protected ' => ! $ this ->isAllowed ($ post ),
277
314
];
278
315
279
316
$ data = $ this ->assignFields ($ data , $ post );
0 commit comments