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

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