77namespace OWC \PDC \Base \Repositories ;
88
99use Closure ;
10- use WP_Post ;
11- use WP_Query ;
10+ use OWC \PDC \Base \Exceptions \PropertyNotExistsException ;
1211use OWC \PDC \Base \Support \CreatesFields ;
1312use OWC \PDC \Base \Support \Traits \QueryHelpers ;
14- use OWC \PDC \Base \Exceptions \PropertyNotExistsException ;
13+ use WP_Post ;
14+ use WP_Query ;
1515
1616/**
1717 * PDC item object with default quering and methods.
@@ -98,35 +98,29 @@ public function __construct()
9898
9999 /**
100100 * Get all the items from the database.
101- *
102- * @return array
103101 */
104102 public function all (): array
105103 {
106104 $ args = array_merge ($ this ->queryArgs , [
107105 'post_type ' => [$ this ->posttype ],
108106 ]);
109107
110- $ this ->query = new WP_Query ($ args );
108+ $ this ->query = new WP_Query ($ this -> cleanParams ( $ args) );
111109
112110 return array_map ([$ this , 'transform ' ], $ this ->getQuery ()->posts );
113111 }
114112
115113 /**
116114 * Find a particular pdc item by ID.
117- *
118- * @param int $id
119- *
120- * @return array
121115 */
122- public function find (int $ id )
116+ public function find (int $ id ): ? array
123117 {
124118 $ args = array_merge ($ this ->queryArgs , [
125119 'p ' => $ id ,
126120 'post_type ' => [$ this ->posttype ],
127121 ]);
128122
129- $ this ->query = new WP_Query ($ args );
123+ $ this ->query = new WP_Query ($ this -> cleanParams ( $ args) );
130124
131125 if (empty ($ this ->getQuery ()->posts )) {
132126 return null ;
@@ -137,19 +131,15 @@ public function find(int $id)
137131
138132 /**
139133 * Find a particular pdc item by slug.
140- *
141- * @param string $slug
142- *
143- * @return array|null
144134 */
145- public function findBySlug (string $ slug )
135+ public function findBySlug (string $ slug ): ? array
146136 {
147137 $ args = array_merge ($ this ->queryArgs , [
148138 'name ' => $ slug ,
149139 'post_type ' => [$ this ->posttype ],
150140 ]);
151141
152- $ this ->query = new WP_Query ($ args );
142+ $ this ->query = new WP_Query ($ this -> cleanParams ( $ args) );
153143
154144 if (empty ($ this ->getQuery ()->posts )) {
155145 return null ;
@@ -158,6 +148,53 @@ public function findBySlug(string $slug)
158148 return $ this ->transform (reset ($ this ->getQuery ()->posts ));
159149 }
160150
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+
161198 /**
162199 * Get the WP_Query object.
163200 *
@@ -273,7 +310,7 @@ public function transform(WP_Post $post)
273310 'date ' => $ post ->post_date ,
274311 'slug ' => $ post ->post_name ,
275312 'post_status ' => $ post ->post_status ,
276- 'protected ' => ! $ this ->isAllowed ($ post )
313+ 'protected ' => ! $ this ->isAllowed ($ post ),
277314 ];
278315
279316 $ data = $ this ->assignFields ($ data , $ post );
0 commit comments