PluginProbe
Depicter — Popup & Slider Builder / 1.5.2
Depicter — Popup & Slider Builder v1.5.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / DataSources / Posts.php

Posts.php in Depicter — Popup & Slider Builder 1.5.2, at app/src/DataSources/Posts.php

408 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\DataSources;
3
4 use Averta\Core\Utility\Arr;
5 use Averta\Core\Utility\Data;
6 use Averta\Core\Utility\Str;
7 use Averta\WordPress\Utility\JSON;
8
9
10 class Posts extends Base
11 {
12 /**
13 * Source name
14 *
15 * @var string
16 */
17 protected $type = 'wpPost';
18
19 /**
20 * Input params to filter source result
21 *
22 * @var array
23 */
24 protected $params = [
25 'postType' => 'post',
26 'perpage' => 5,
27 'excerptLength' => 55,
28 'offset' => 0,
29 'linkSlides' => true,
30 'orderBy' => 'date',
31 'order' => 'DESC',
32 'imageSource' => 'featured',
33 'excludedIds' => '',
34 'includedIds' => '',
35 'excludeNonThumbnail' => false,
36 'taxonomies' => '',
37 'page' => 1,
38 's' => ''
39 ];
40
41 /**
42 * List of valid dynamic tags for this source
43 *
44 * @var string[]
45 */
46 protected $tags = [
47 '{{{uuid}}}',
48 '{{{linkSlides}}}',
49 '{{{url}}}',
50
51 '{{{id}}}',
52 '{{{featuredImage}}}',
53 '{{{featuredImageSrc}}}',
54 '{{{author.name}}}',
55 '{{{author.page}}}',
56 '{{{title}}}',
57 '{{{excerpt}}}',
58 '{{{content}}}',
59 '{{{date}}}',
60 '{{{readMore}}}',
61 '{{categoryToStr}}', // converts the array of categories to this form cat1, cat2, cat3
62 '{{post_tagToStr}}' // converts the array of tags to this form tag1, tag2, tag3
63 ];
64
65 /**
66 * Prepares query arguments to get records
67 *
68 * @param array|object $args
69 *
70 * @return array
71 */
72 protected function prepare( $args ){
73 return Arr::merge( $args, $this->params );
74 }
75
76 /**
77 * Retrieves the list of records based on query params
78 *
79 * @param $args
80 *
81 * @return \WP_Query
82 */
83 protected function getRecords( $args ){
84
85 $queryArgs = [
86 'post_type' => $args['postType'],
87 'posts_per_page' => $args['perpage'],
88 'order' => $args['order'],
89 'orderby' => $args['orderBy'],
90 'offset' => $args['offset'],
91 'paged' => $args['page'],
92 's' => $args['s'],
93 'post__in' => $args['includedIds'],
94 'post__not_in' => $args['excludedIds'],
95 'tax_query' => [],
96 'meta_query' => []
97 ];
98
99 if ( !empty( $args['taxonomies'] ) ) {
100 $taxonomies = $args['taxonomies'];
101
102 if( JSON::isJson( $args['taxonomies'] ) ){
103 $taxonomies = JSON::decode( $args['taxonomies'] );
104 }
105
106 if( !empty( $taxonomies->category ) ){
107 $queryArgs['tax_query'][] = [
108 'taxonomy' => 'category',
109 'field' => 'slug',
110 'terms' => $taxonomies->category
111 ];
112 }
113
114 if( !empty( $taxonomies->post_tag ) ){
115 $queryArgs['tax_query'][] = [
116 'taxonomy' => 'post_tag',
117 'field' => 'slug',
118 'terms' => $taxonomies->post_tag
119 ];
120 }
121 }
122
123 if( Data::isTrue( $args['excludeNonThumbnail'] ) ){
124 $queryArgs['meta_query'][] = [
125 'key' => '_thumbnail_id',
126 'compare' => 'EXISTS'
127 ];
128 }
129
130 return new \WP_Query( $queryArgs );
131 }
132
133 /**
134 * Renders preview for query params
135 *
136 * @param array $args
137 *
138 * @return array
139 */
140 public function previewRecords( array $args = [] ) {
141 $args = $this->prepare( $args );
142 $availableRecords = $this->getRecords( $args );
143
144 $records = [];
145
146 if ( $availableRecords && $availableRecords->have_posts() ) {
147
148
149 foreach( $availableRecords->posts as $post ) {
150 $featuredImage = [];
151 if( has_post_thumbnail( $post->ID ) ){
152 $featuredImageId = get_post_thumbnail_id( $post->ID );
153 $imageInfo = wp_get_attachment_image_src( $featuredImageId, 'full' );
154 $featuredImage = [
155 'id' => $featuredImageId,
156 'url' => $imageInfo[0] ?: '',
157 'width' => $imageInfo[1] ?: '',
158 'height' => $imageInfo[2] ?: '',
159 ];
160 }
161
162 $excerpt = !empty( $args['excerptLength'] ) ? Str::trimByChars( get_the_excerpt( $post->ID ), $args['excerptLength'] ) : get_the_excerpt( $post->ID );
163 $postInfo = [
164 'id' => $post->ID,
165 'title' => get_the_title( $post->ID ),
166 'url' => get_permalink( $post->ID ),
167 'featuredImage' => $featuredImage,
168 'date' => get_the_date('', $post->ID ),
169 'excerpt' => $excerpt,
170 'author' => [
171 'name' => get_the_author_meta( 'display_name', $post->post_author ),
172 'page' => get_author_posts_url( $post->post_author ),
173 ],
174 'content' => get_the_content(null, false, $post->ID ),
175 'taxonomies'=> []
176 ];
177
178 $taxonomies = get_object_taxonomies( $args['postType'], 'objects' );
179
180 if ( !empty( $taxonomies ) ) {
181 foreach( $taxonomies as $taxonomySlug => $taxonomy ) {
182 $taxonomyInfo = [
183 "id" => $taxonomySlug,
184 "label" => $taxonomy->label,
185 "terms" => []
186 ];
187
188 if ( $terms = wp_get_post_terms( $post->ID, $taxonomySlug ) ) {
189 foreach( $terms as $term ) {
190 $taxonomyInfo[ "terms" ][] = [
191 'id' => $term->term_id,
192 'value' => $term->slug,
193 'label' => $term->name,
194 'link' => get_term_link( $term->term_id )
195 ];
196 }
197 }
198
199 $postInfo['taxonomies'][] = $taxonomyInfo;
200 }
201 }
202
203 $records[] = $postInfo;
204 }
205 }
206
207 return $records;
208 }
209
210 /**
211 * search records by title
212 *
213 * @param array $args
214 *
215 * @return array
216 */
217 public function searchRecordsByTitle( array $args = [] ) {
218 $args = $this->prepare( $args );
219
220 add_filter( 'posts_search', [ $this, 'searchByTitleOnly' ], 500, 2 );
221 $availableRecords = $this->getRecords( $args );
222 remove_filter( 'posts_search', [ $this, 'searchByTitleOnly' ], 500 );
223
224 $records = [];
225
226 if ( $availableRecords && $availableRecords->have_posts() ) {
227
228 foreach( $availableRecords->posts as $post ) {
229 $postInfo = [
230 'id' => $post->ID,
231 'title' => get_the_title( $post->ID ),
232 'url' => get_permalink( $post->ID )
233 ];
234
235 $records[] = $postInfo;
236 }
237 }
238
239 return $records;
240 }
241
242 /**
243 * Get list of tags and their values for the query result
244 *
245 * @param array $args
246 *
247 * @return array
248 */
249 public function getRecordsWithTags( array $args = [] ){
250 $args = $this->prepare( $args );
251 $availableRecords = $this->getRecords( $args );
252
253 $records = [];
254 if ( $availableRecords && $availableRecords->have_posts() ) {
255 // $taxonomies = get_object_taxonomies( $args['postType'], 'objects' );
256
257 foreach( $availableRecords->posts as $post ) {
258
259 $excerpt = !empty( $args['excerptLength'] ) ? Str::trimByChars( get_the_excerpt( $post->ID ), $args['excerptLength'] ) : get_the_excerpt( $post->ID );
260
261 $thumbnailId = get_post_thumbnail_id( $post->ID );
262 $featuredImageInfo = wp_get_attachment_image_src( $thumbnailId, 'full' );
263
264 $postInfo = [
265 '{{{uuid}}}' => $post->ID,
266 '{{{linkSlides}}}' => !! $args['linkSlides'],
267 '{{{url}}}' => get_permalink( $post->ID ),
268
269 '{{{id}}}' => $post->ID,
270 '{{{title}}}' => get_the_title( $post->ID ),
271 '{{{featuredImageSrc}}}' => ! empty( $featuredImageInfo[0] ) ? $featuredImageInfo[0] : '',
272 '{{{featuredImage}}}' => ! empty( $thumbnailId ) ? $thumbnailId : '',
273 '{{{date}}}' => get_the_date('', $post->ID ),
274 '{{{excerpt}}}' => $excerpt,
275 '{{{author.name}}}' => get_the_author_meta( 'display_name', $post->post_author ),
276 '{{{author.page}}}' => get_author_posts_url( $post->post_author ),
277 '{{{content}}}' => get_the_content(null, false, $post->ID ),
278 '{{categoryToStr}}' => $this->getTaxonomyTermsStr( $post->ID, 'category' ),
279 '{{post_tagToStr}}' => $this->getTaxonomyTermsStr( $post->ID, 'post_tag' ),
280 ];
281
282 $records[] = $postInfo;
283 }
284 }
285
286 return $records;
287 }
288
289 /**
290 * Get All Post Types info
291 *
292 * @param string $postType
293 *
294 * @return array
295 */
296 public function getTypes( string $postType = 'all' ) {
297 if ( $postType == 'all' ) {
298 $availablePostType = array(
299 'post' => get_post_type_object('post'),
300 'page' => get_post_type_object('page')
301 );
302 $postTypes = get_post_types(
303 array(
304 'public' => true,
305 '_builtin' => false
306 ),
307 'objects'
308 );
309
310 $postTypes = array_merge( $availablePostType, $postTypes );
311 } else {
312 if ( !post_type_exists( $postType ) ) {
313 return [];
314 }
315 $postTypes = [ $postType => get_post_type_object( $postType ) ];
316 }
317
318 $result = [];
319 foreach( $postTypes as $id => $providedPostType ) {
320
321 $postTypeInfo = [
322 'id' => $id,
323 'label' => $providedPostType->label,
324 'taxonomies' => []
325 ];
326
327 $taxonomies = get_object_taxonomies( $id, 'objects' );
328
329 foreach( $taxonomies as $taxonomySlug => $taxonomy ) {
330 $taxonomyInfo = [
331 "id" => $taxonomySlug,
332 "label" => $taxonomy->label,
333 "terms" => []
334 ];
335
336 $terms = get_terms([
337 'taxonomy' => $taxonomySlug,
338 'hide_empty' => false
339 ]);
340
341 if ( !empty( $terms ) ) {
342 foreach( $terms as $term ) {
343 $taxonomyInfo[ "terms" ][] = [
344 'id' => $term->term_id,
345 'value' => $term->slug,
346 'label' => $term->name
347 ];
348 }
349 }
350
351 $postTypeInfo["taxonomies"][] = $taxonomyInfo;
352 }
353
354 $result[] = $postTypeInfo;
355 }
356
357 return $result;
358 }
359
360 /**
361 * Get taxonomy terms string
362 *
363 * @param int $postID
364 * @param string $taxonomy
365 * @return string
366 */
367 protected function getTaxonomyTermsStr( $postID, $taxonomy ) {
368 $terms = get_the_terms( $postID, $taxonomy );
369 if ( !empty( $terms ) ) {
370 return join( ', ', wp_list_pluck($terms, 'name') );
371 }
372
373 return '';
374 }
375
376 /**
377 * Search by title in wp query
378 *
379 * @param string $search
380 * @param object $wp_query
381 * @return string $search
382 */
383 public function searchByTitleOnly( $search, $wp_query ) {
384 global $wpdb;
385 if ( empty( $search ) ) {
386 return $search;
387 }
388
389 $queryVars = $wp_query->query_vars;
390 $n = !empty($queryVars['exact']) ? '' : '%';
391 $search = '';
392 $searchAnd = '';
393 foreach ( (array) $queryVars['search_terms'] as $term ) {
394 $term = esc_sql( $wpdb->esc_like( $term ) );
395 $search .= " {$searchAnd} ( $wpdb->posts.post_title LIKE '{$n}{$term}{$n}' )";
396 $searchAnd = ' AND ';
397 }
398 if ( !empty( $search ) ) {
399 $search = " AND ( {$search} ) ";
400 if ( !is_user_logged_in() ) {
401 $search .= " AND ($wpdb->posts.post_password = '') ";
402 }
403 }
404 return $search;
405 }
406
407 }
408