PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
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 trunk, at app/src/DataSources/Posts.php

464 lines 12.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 use Averta\WordPress\Utility\Post;
9
10 class Posts extends DataSourceBase implements DataSourceInterface
11 {
12 /**
13 * DataSource name
14 *
15 * @var string
16 */
17 protected string $type = 'wpPost';
18
19 /**
20 * DataSource properties
21 *
22 * @var array
23 */
24 protected array $properties = [
25 'type' => 'wpPost',
26 'postType' => 'post'
27 ];
28
29 /**
30 * Default input params for retrieving dataSource records
31 *
32 * @var array
33 */
34 protected array $defaultInputParams = [
35 'postType' => 'post',
36 'perpage' => 5,
37 'excerptLength' => 55,
38 'offset' => 0,
39 'linkSlides' => false,
40 'orderBy' => 'post__in',
41 'order' => 'DESC',
42 'imageSource' => 'featured',
43 'excludedIds' => '',
44 'includedIds' => '',
45 'excludeNonThumbnail' => false,
46 'taxonomies' => '',
47 'page' => 1,
48 's' => ''
49 ];
50
51 /**
52 * Asset groups of this DataSource
53 *
54 * @var array
55 */
56 protected array $assetGroupNames = [ 'post', 'taxonomy', 'acf', 'metaboxio' ];
57
58 /**
59 * Retrieves the list of records based on query params
60 *
61 * @param $args
62 *
63 * @return \WP_Query
64 */
65 protected function getRecords( $args ){
66
67 $queryArgs = [
68 'post_type' => $args['postType'],
69 'posts_per_page' => $args['perpage'],
70 'order' => $args['order'],
71 'orderby' => $args['orderBy'],
72 'offset' => $args['offset'],
73 'paged' => $args['page'],
74 'post__in' => $args['includedIds'],
75 'post__not_in' => $args['excludedIds'],
76 'tax_query' => [],
77 'meta_query' => [],
78 'post_status' => 'publish',
79 'ignore_sticky_posts' => true
80 ];
81
82 if( !empty( $args['s'] ) ){
83 $queryArgs['s'] = $args['s'];
84 }
85
86 if ( !empty( $args['taxonomies'] ) ) {
87 $taxonomies = $args['taxonomies'];
88
89 if( JSON::isJson( $args['taxonomies'] ) ){
90 $taxonomies = JSON::decode( $args['taxonomies'] );
91 }
92
93 if ( !empty( $taxonomies ) ) {
94 foreach( $taxonomies as $taxonomySlug => $value ) {
95 if ( !empty( $value ) ) {
96 $queryArgs['tax_query'][] = [
97 'taxonomy' => $taxonomySlug,
98 'field' => 'slug',
99 'terms' => $value
100 ];
101 }
102 }
103 }
104 }
105
106 if( Data::isTrue( $args['excludeNonThumbnail'] ) ){
107 $queryArgs['meta_query'][] = [
108 'key' => '_thumbnail_id',
109 'compare' => 'EXISTS'
110 ];
111 }
112
113 return new \WP_Query( $queryArgs );
114 }
115
116 public function previewRecords( array $args = [] ) {
117 $args = $this->prepare( $args );
118
119 $records = [];
120
121 // Check if user selects to include sticky posts then prepend them first because we ignore the sticky posts in the main query
122 $stickyPostIDs = get_option('sticky_posts');
123 $includedIds = $args['includedIds'];
124 if ( !empty( $args['sticky'] ) && Data::isTrue( $args['sticky'] ) && ! empty( $stickyPostIDs ) && $args['postType'] == 'post' ) {
125 $args['includedIds'] = $stickyPostIDs;
126 $wp_query = $this->getRecords( $args );
127 $records = Arr::merge( $this->extractRecordsArray( $wp_query, $args ), $records );
128
129 // Modify query args to get remaining posts without considering sticky posts
130 $args['perpage'] = count( $stickyPostIDs ) > $args['perpage'] ? 0 : $args['perpage'] - count( $stickyPostIDs);
131 $args['includedIds'] = $includedIds;
132 $args['excludedIds'] = Arr::merge( $stickyPostIDs, $args['excludedIds']);
133 }
134
135 if ( $args['perpage'] ) {
136 $wp_query = $this->getRecords( $args );
137 $records = Arr::merge( $this->extractRecordsArray( $wp_query, $args ), $records );
138 }
139
140 return $records;
141 }
142
143 public function extractRecordsArray( $wp_query, $args ) {
144 $records = [];
145
146 if ( $wp_query && $wp_query->have_posts() ) {
147 $acfModule = \Depicter::resolve( 'depicter.dataSources.tags.acf' );
148 $acfModuleAvailable = $acfModule->isAvailable();
149 $acfFields = $acfModule->getFieldGroups( $args );
150
151 foreach( $wp_query->posts as $post ) {
152 $featuredImage = null;
153 if( has_post_thumbnail( $post->ID ) ){
154 $featuredImageId = get_post_thumbnail_id( $post->ID );
155 $imageInfo = wp_get_attachment_image_src( $featuredImageId, 'full' );
156 $featuredImage = [
157 'id' => $featuredImageId,
158 'src' => $imageInfo[0] ?: '',
159 'width' => $imageInfo[1] ?: '',
160 'height' => $imageInfo[2] ?: '',
161 ];
162 }
163
164 $content = get_the_content(null, false, $post->ID );
165 $excerpt = Post::getExcerptTrimmedByWords( $post->ID );
166 if ( ! empty( $args['excerptLength'] ) ) {
167 $excerpt = Str::trimByChars( $excerpt, $args['excerptLength'] );
168 }
169
170 $postInfo = [
171 'id' => $post->ID,
172 'title' => get_the_title( $post->ID ),
173 'url' => get_permalink( $post->ID ),
174 'featuredImage' => $featuredImage,
175 'date' => get_the_date('Y-m-d h:m:s', $post->ID ),
176 'excerpt' => $excerpt,
177 'author' => [
178 'name' => get_the_author_meta( 'display_name', $post->post_author ),
179 'page' => get_author_posts_url( $post->post_author ),
180 ],
181 'content' => $content,
182 'taxonomy'=> []
183 ];
184
185 // append taxonomy data
186 $taxonomies = get_object_taxonomies( $args['postType'], 'objects' );
187
188 if ( !empty( $taxonomies ) ) {
189 foreach( $taxonomies as $taxonomySlug => $taxonomy ) {
190 $taxonomyInfo = [
191 "id" => $taxonomySlug,
192 "label" => $taxonomy->label,
193 "terms" => []
194 ];
195
196 if ( $terms = wp_get_post_terms( $post->ID, $taxonomySlug ) ) {
197 foreach( $terms as $term ) {
198 $taxonomyInfo[ "terms" ][] = [
199 'id' => $term->term_id,
200 'value' => $term->slug,
201 'label' => $term->name,
202 'link' => get_term_link( $term->term_id )
203 ];
204 }
205 } else {
206 $taxonomyInfo[ "terms" ] = null;
207 }
208
209 $postInfo['taxonomy'][ $taxonomySlug ] = $taxonomyInfo;
210 }
211 }
212
213 // append acf data
214 if( $acfModuleAvailable ){
215 $postInfo['acf'] = [];
216
217 if ( !empty( $acfFields ) ) {
218 foreach( $acfFields as $fieldId => $field ) {
219 $postInfo['acf'][ $fieldId ] = $acfModule->getSlugValue( $fieldId, ['post' => $post->ID ] );
220 if( $field['type'] === 'image' ){
221 $imageInfo = wp_get_attachment_image_src( $postInfo['acf'][ $fieldId ], 'full' );
222 $postInfo['acf'][ $fieldId ] = [
223 'id' => $postInfo['acf'][ $fieldId ],
224 'src' => $imageInfo[0] ?? '',
225 'width' => $imageInfo[1] ?? '',
226 'height' => $imageInfo[2] ?? '',
227 ];
228 }
229 }
230 }
231 }
232
233 $records[] = $postInfo;
234 }
235 }
236
237 return $records;
238 }
239
240 public function previewRecords2( array $args = [] ) {
241 $args = $this->prepare( $args );
242 $availableRecords = $this->getRecords( $args );
243
244 $records = [];
245
246 if ( $availableRecords && $availableRecords->have_posts() ) {
247 foreach( $availableRecords->posts as $post ) {
248 $item = [];
249 $args['post'] = $post;
250
251 $assetGroups = $this->getAssetGroupNames();
252 foreach( $assetGroups as $assetGroup ){
253 if( $module = \Depicter::dataSource()->tagsManager()->getModule( $assetGroup ) ){
254 $item[ $assetGroup ] = $module->getValuesForRecord( $args );
255 }
256 }
257
258 $records[] = $item;
259 }
260 }
261
262 return $records;
263 }
264
265 /**
266 * search records by title
267 *
268 * @param array $args
269 *
270 * @return array
271 */
272 public function searchRecordsByTitle( array $args = [] ) {
273 $args = $this->prepare( $args );
274
275 add_filter( 'posts_search', [ $this, 'searchByTitleOnly' ], 500, 2 );
276 $availableRecords = $this->getRecords( $args );
277 remove_filter( 'posts_search', [ $this, 'searchByTitleOnly' ], 500 );
278
279 $records = [];
280
281 if ( $availableRecords && $availableRecords->have_posts() ) {
282
283 foreach( $availableRecords->posts as $post ) {
284 $postInfo = [
285 'id' => $post->ID,
286 'title' => get_the_title( $post->ID ),
287 'url' => get_permalink( $post->ID )
288 ];
289
290 $records[] = $postInfo;
291 }
292 }
293
294 return $records;
295 }
296
297 /**
298 * Get list of datasheets and corresponding required arguments
299 *
300 * @param array $args
301 *
302 * @return array
303 */
304 public function getDataSheetArgs( array $args = [] ){
305 $args = $this->prepare( $args );
306 $records = [];
307
308 // Check if user selects to include sticky posts then prepend them first because we ignore the sticky posts in the main query
309 $stickyPostIDs = get_option('sticky_posts');
310 $includedIds = $args['includedIds'];
311 if ( !empty( $args['sticky'] ) && Data::isTrue( $args['sticky'] ) && ! empty( $stickyPostIDs ) && $args['postType'] == 'post' ) {
312 $args['includedIds'] = $stickyPostIDs;
313 $dataSheetArgs = Arr::merge( $args, $this->properties );
314 $wp_query = $this->getRecords( $args );
315 if ( $wp_query && $wp_query->have_posts() ) {
316
317 foreach( $wp_query->posts as $post ) {
318 $postArgs = $dataSheetArgs;
319 $postArgs['post'] = $post;
320 $records[] = $postArgs;
321 }
322 }
323
324 // Modify query args to get remaining posts without considering sticky posts
325 $args['perpage'] = count( $stickyPostIDs ) > $args['perpage'] ? 0 : $args['perpage'] - count( $stickyPostIDs);
326 $args['includedIds'] = $includedIds;
327 $args['excludedIds'] = Arr::merge( $stickyPostIDs, $args['excludedIds']);
328 }
329
330 if ( $args['perpage'] ) {
331 $wp_query = $this->getRecords( $args );
332 $dataSheetArgs = Arr::merge( $args, $this->properties );
333 if ( $wp_query && $wp_query->have_posts() ) {
334
335 foreach( $wp_query->posts as $post ) {
336 $postArgs = $dataSheetArgs;
337 $postArgs['post'] = $post;
338 $records[] = $postArgs;
339 }
340 }
341 }
342
343 return $records;
344 }
345
346 /**
347 * Get All Post Types info
348 *
349 * @param string $postType
350 *
351 * @return array
352 */
353 public function getTypes( string $postType = 'all' ) {
354 if ( $postType == 'all' ) {
355 $postTypes = get_post_types(
356 array(
357 'public' => true,
358 '_builtin' => false
359 ),
360 'objects'
361 );
362
363 // $postTypes = array_merge( $availablePostType, $postTypes );
364 } else {
365 if ( !post_type_exists( $postType ) ) {
366 return [];
367 }
368 $postTypes = [ $postType => get_post_type_object( $postType ) ];
369 }
370
371 $result = [];
372 foreach( $postTypes as $id => $providedPostType ) {
373
374 $postTypeInfo = [
375 'id' => $id,
376 'label' => $providedPostType->label,
377 'taxonomies' => []
378 ];
379
380 $taxonomies = get_object_taxonomies( $id, 'objects' );
381
382 foreach( $taxonomies as $taxonomySlug => $taxonomy ) {
383 $taxonomyInfo = [
384 "id" => $taxonomySlug,
385 "label" => $taxonomy->label,
386 "terms" => []
387 ];
388
389 $terms = get_terms([
390 'taxonomy' => $taxonomySlug,
391 'hide_empty' => false
392 ]);
393
394 if ( !empty( $terms ) ) {
395 foreach( $terms as $term ) {
396 $taxonomyInfo[ "terms" ][] = [
397 'id' => $term->term_id,
398 'value' => $term->slug,
399 'label' => $term->name
400 ];
401 }
402 }
403
404 $postTypeInfo["taxonomies"][] = $taxonomyInfo;
405 }
406
407 $result[] = $postTypeInfo;
408 }
409
410 return $result;
411 }
412
413 /**
414 * Search by title in wp query
415 *
416 * @param string $search
417 * @param object $wp_query
418 * @return string $search
419 */
420 public function searchByTitleOnly( $search, $wp_query ) {
421 global $wpdb;
422
423 if ( empty( $search ) ) {
424 return $search;
425 }
426
427 $queryVars = $wp_query->query_vars;
428 $n = !empty( $queryVars['exact'] ) ? '' : '%';
429 $search = '';
430 $searchAnd = '';
431 foreach ( (array) $queryVars['search_terms'] as $term ) {
432 $term = esc_sql( $wpdb->esc_like( $term ) );
433 $search .= " {$searchAnd} ( $wpdb->posts.post_title LIKE '{$n}{$term}{$n}' )";
434 $searchAnd = ' AND ';
435 }
436 if ( !empty( $search ) ) {
437 $search = " AND ( {$search} ) ";
438 if ( !is_user_logged_in() ) {
439 $search .= " AND ($wpdb->posts.post_password = '') ";
440 }
441 }
442
443 return $search;
444 }
445
446 /**
447 * Get taxonomy terms string
448 *
449 * @param int $postID
450 * @param string $taxonomy
451 * @return string
452 */
453 protected function getTaxonomyTermsStr( $postID, $taxonomy ) {
454 $terms = get_the_terms( $postID, $taxonomy );
455
456 if ( !empty( $terms ) ) {
457 return join( ', ', wp_list_pluck($terms, 'name') );
458 }
459
460 return '';
461 }
462
463 }
464