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

403 lines 9.3 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->category ) ){
92 $queryArgs['tax_query'][] = [
93 'taxonomy' => 'category',
94 'field' => 'slug',
95 'terms' => $taxonomies->category
96 ];
97 }
98
99 if( !empty( $taxonomies->post_tag ) ){
100 $queryArgs['tax_query'][] = [
101 'taxonomy' => 'post_tag',
102 'field' => 'slug',
103 'terms' => $taxonomies->post_tag
104 ];
105 }
106 }
107
108 if( Data::isTrue( $args['excludeNonThumbnail'] ) ){
109 $queryArgs['meta_query'][] = [
110 'key' => '_thumbnail_id',
111 'compare' => 'EXISTS'
112 ];
113 }
114
115 return new \WP_Query( $queryArgs );
116 }
117
118 public function previewRecords( array $args = [] ) {
119 $args = $this->prepare( $args );
120 $availableRecords = $this->getRecords( $args );
121
122 $records = [];
123
124 if ( $availableRecords && $availableRecords->have_posts() ) {
125 foreach( $availableRecords->posts as $post ) {
126 $featuredImage = [];
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 'url' => $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('', $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 'taxonomies'=> []
152 ];
153
154 $taxonomies = get_object_taxonomies( $args['postType'], 'objects' );
155
156 if ( !empty( $taxonomies ) ) {
157 foreach( $taxonomies as $taxonomySlug => $taxonomy ) {
158 $taxonomyInfo = [
159 "id" => $taxonomySlug,
160 "label" => $taxonomy->label,
161 "terms" => []
162 ];
163
164 if ( $terms = wp_get_post_terms( $post->ID, $taxonomySlug ) ) {
165 foreach( $terms as $term ) {
166 $taxonomyInfo[ "terms" ][] = [
167 'id' => $term->term_id,
168 'value' => $term->slug,
169 'label' => $term->name,
170 'link' => get_term_link( $term->term_id )
171 ];
172 }
173 }
174 }
175 }
176
177 $records[] = $postInfo;
178 }
179 }
180
181 return $records;
182 }
183
184 public function previewRecords2( array $args = [] ) {
185 $args = $this->prepare( $args );
186 $availableRecords = $this->getRecords( $args );
187
188 $records = [];
189
190 if ( $availableRecords && $availableRecords->have_posts() ) {
191 foreach( $availableRecords->posts as $post ) {
192 $item = [];
193 $args['post'] = $post;
194
195 $assetGroups = $this->getAssetGroupNames();
196 foreach( $assetGroups as $assetGroup ){
197 if( $module = \Depicter::dataSource()->tagsManager()->getModule( $assetGroup ) ){
198 $item[ $assetGroup ] = $module->getValuesForRecord( $args );
199 }
200 }
201
202 $records[] = $item;
203 }
204 }
205
206 return $records;
207 }
208
209 /**
210 * search records by title
211 *
212 * @param array $args
213 *
214 * @return array
215 */
216 public function searchRecordsByTitle( array $args = [] ) {
217 $args = $this->prepare( $args );
218
219 add_filter( 'posts_search', [ $this, 'searchByTitleOnly' ], 500, 2 );
220 $availableRecords = $this->getRecords( $args );
221 remove_filter( 'posts_search', [ $this, 'searchByTitleOnly' ], 500 );
222
223 $records = [];
224
225 if ( $availableRecords && $availableRecords->have_posts() ) {
226
227 foreach( $availableRecords->posts as $post ) {
228 $postInfo = [
229 'id' => $post->ID,
230 'title' => get_the_title( $post->ID ),
231 'url' => get_permalink( $post->ID )
232 ];
233
234 $records[] = $postInfo;
235 }
236 }
237
238 return $records;
239 }
240
241 /**
242 * Get list of datasheets and corresponding required arguments
243 *
244 * @param array $args
245 *
246 * @return array
247 */
248 public function getDataSheetArgs( array $args = [] ){
249 $args = $this->prepare( $args );
250 $availableRecords = $this->getRecords( $args );
251
252 $dataSheetArgs = Arr::merge( $args, $this->properties );
253 $records = [];
254
255 if ( $availableRecords && $availableRecords->have_posts() ) {
256
257 foreach( $availableRecords->posts as $post ) {
258 $postArgs = $dataSheetArgs;
259 $postArgs['post'] = $post;
260 $records[] = $postArgs;
261 }
262 }
263
264 return $records;
265 }
266
267 /**
268 * Get All Post Types info
269 *
270 * @param string $postType
271 *
272 * @return array
273 */
274 public function getTypes( string $postType = 'all' ) {
275 if ( $postType == 'all' ) {
276 $postTypes = get_post_types(
277 array(
278 'public' => true,
279 '_builtin' => false
280 ),
281 'objects'
282 );
283
284 // $postTypes = array_merge( $availablePostType, $postTypes );
285 } else {
286 if ( !post_type_exists( $postType ) ) {
287 return [];
288 }
289 $postTypes = [ $postType => get_post_type_object( $postType ) ];
290 }
291
292 $result = [];
293 foreach( $postTypes as $id => $providedPostType ) {
294
295 $postTypeInfo = [
296 'id' => $id,
297 'label' => $providedPostType->label,
298 'taxonomies' => []
299 ];
300
301 $taxonomies = get_object_taxonomies( $id, 'objects' );
302
303 foreach( $taxonomies as $taxonomySlug => $taxonomy ) {
304 $taxonomyInfo = [
305 "id" => $taxonomySlug,
306 "label" => $taxonomy->label,
307 "terms" => []
308 ];
309
310 $terms = get_terms([
311 'taxonomy' => $taxonomySlug,
312 'hide_empty' => false
313 ]);
314
315 if ( !empty( $terms ) ) {
316 foreach( $terms as $term ) {
317 $taxonomyInfo[ "terms" ][] = [
318 'id' => $term->term_id,
319 'value' => $term->slug,
320 'label' => $term->name
321 ];
322 }
323 }
324
325 $postTypeInfo["taxonomies"][] = $taxonomyInfo;
326 }
327
328 $result[] = $postTypeInfo;
329 }
330
331 return $result;
332 }
333
334 /**
335 * Search by title in wp query
336 *
337 * @param string $search
338 * @param object $wp_query
339 * @return string $search
340 */
341 public function searchByTitleOnly( $search, $wp_query ) {
342 global $wpdb;
343
344 if ( empty( $search ) ) {
345 return $search;
346 }
347
348 $queryVars = $wp_query->query_vars;
349 $n = !empty( $queryVars['exact'] ) ? '' : '%';
350 $search = '';
351 $searchAnd = '';
352 foreach ( (array) $queryVars['search_terms'] as $term ) {
353 $term = esc_sql( $wpdb->esc_like( $term ) );
354 $search .= " {$searchAnd} ( $wpdb->posts.post_title LIKE '{$n}{$term}{$n}' )";
355 $searchAnd = ' AND ';
356 }
357 if ( !empty( $search ) ) {
358 $search = " AND ( {$search} ) ";
359 if ( !is_user_logged_in() ) {
360 $search .= " AND ($wpdb->posts.post_password = '') ";
361 }
362 }
363
364 return $search;
365 }
366
367 /**
368 * Get taxonomy terms string
369 *
370 * @param int $postID
371 * @param string $taxonomy
372 * @return string
373 */
374 protected function getTaxonomyTermsStr( $postID, $taxonomy ) {
375 $terms = get_the_terms( $postID, $taxonomy );
376
377 if ( !empty( $terms ) ) {
378 return join( ', ', wp_list_pluck($terms, 'name') );
379 }
380
381 return '';
382 }
383
384 /**
385 * Get list of asset groups for this dataSource
386 *
387 * @param $args
388 *
389 * @return array
390 */
391 public function getAssets( $args ){
392 $assetGroupNames = $this->getAssetGroupNames();
393
394 $groups = [];
395 foreach( $assetGroupNames as $assetGroupName ){
396 $groups[ $assetGroupName ] = $args;
397 }
398
399 return \Depicter::dataSource()->tagsManager()->getAssetsInGroups( $groups );
400 }
401
402 }
403