PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.1.0
Post Grid Gutenberg Blocks – PostX v5.1.0
5.1.1 5.1.0 5.0.41 5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 All 240 releases
ultimate-post / addons / dynamic_content / includes / DCService.php

DCService.php in Post Grid Gutenberg Blocks – PostX 5.1.0, at addons/dynamic_content/includes/DCService.php

620 lines 14.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ULTP;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 /**
9 *
10 * Dynamic Content
11 *
12 * @package ULTP
13 * @since 4.1.1
14 */
15 final class DCService {
16
17 /**
18 * Gets the dynamic data to show in the editor
19 *
20 * @since 4.1.1
21 *
22 * @param array $args - Arguments
23 *
24 * @return string
25 */
26 public static function get_dynamic_data( $args ) {
27 $post_id = $args['post_id'];
28 $key = $args['key'];
29 $data_type = $args['data_type'];
30
31 $res = '';
32 $process_string = true;
33
34 switch ( $data_type ) {
35 case 'link':
36 $res = self::get_link( $post_id, $key );
37 $process_string = false;
38 break;
39 case 'post_types':
40 $res = self::get_post_types();
41 $process_string = false;
42 break;
43 case 'post_info':
44 $res = self::get_post_info( $post_id, $key );
45 break;
46 case 'author_info':
47 $res = self::get_author_info( $post_id, $key );
48 break;
49 case 'site_info':
50 $res = self::get_site_info( $key );
51 break;
52 case 'cf':
53 $res = self::get_field_value( $post_id, $key );
54 $process_string = false;
55 break;
56 case 'comment_count':
57 $res = self::get_comment_count( $post_id );
58 break;
59 }
60
61 return $process_string ? self::process( $res ) : $res;
62 }
63
64 /**
65 * Gets the comment count for a post
66 *
67 * @since 4.1.1
68 *
69 * @param string|int|null $post_id - Post Id.
70 *
71 * @return string
72 */
73 public static function get_comment_count( $post_id ) {
74 $res = '';
75
76 if ( $post_id !== 0 ) {
77 $comments = get_comment_count( $post_id );
78 $res = $comments['total_comments'];
79 }
80
81 return $res;
82 }
83
84 /**
85 * Applies dynamic content to RichText
86 *
87 * @since 4.1.1
88 *
89 * @param string|int|null $post_id - Post Id.
90 * @param string $content_src - Key value.
91 *
92 * @return string
93 */
94 public static function get_dc_content_for_image( $post_id, $content_src ) {
95 $res = '';
96
97 if ( self::is_field_key( $content_src ) ) {
98 $res = self::get_field_value( $post_id, $content_src );
99 } elseif (
100 str_starts_with( $content_src, 'link_' )
101 ) {
102 $res = self::get_link( $post_id, $content_src );
103 }
104
105 if ( empty( $res ) ) {
106 $res = ULTP_URL . 'assets/img/ultp-placeholder.jpg';
107 }
108
109 return $res;
110 }
111
112 /**
113 * Applies dynamic content to RichText
114 *
115 * @since 4.1.1
116 *
117 * @param array $attr - block attributes.
118 *
119 * @return string
120 */
121 public static function get_dc_content_for_rich_text( &$attr ) {
122 $text = '';
123 $url = '';
124
125 $data_src = $attr['dc']['dataSrc'];
126 $content_src = $attr['dc']['contentSrc'];
127 $link_src = $attr['dc']['linkSrc'];
128 $link_enabled = $attr['dc']['linkEnabled'];
129 $post_id = $attr['dc']['postId'];
130 $fallback = $attr['dc']['fallback'];
131 $max_length = $attr['dc']['maxCharLen'];
132
133 if ( str_starts_with( $data_src, 'site_' ) ) {
134 $text = self::get_site_info( $data_src );
135 } elseif ( str_starts_with( $content_src, 'post_' ) ) {
136 $text = self::get_post_info( $post_id, $content_src );
137 } elseif ( str_starts_with( $content_src, 'a_' ) ) {
138 $text = self::get_author_info( $post_id, $content_src );
139 } elseif ( self::is_field_key( $content_src ) ) {
140 $text = self::get_field_value( $post_id, $content_src );
141 }
142
143 if ( ! empty( $text ) && ! empty( $max_length ) && is_string( $max_length ) && intval( $max_length ) > 0 ) {
144 $text = self::limit_str( $text, intval( $max_length ) );
145 }
146
147 if ( empty( $text ) ) {
148 $text = $fallback;
149 }
150
151 $text = $attr['dc']['beforeText'] . $text . $attr['dc']['afterText'];
152
153 if ( ( $link_enabled && $link_src ) ) {
154 $url = self::get_link( $post_id, $link_src );
155 }
156
157 return array( $text, $url );
158 }
159
160 /**
161 * Get Html of Dynamic Content for Blocks
162 *
163 * @since 4.1.1
164 *
165 * @param array $attr
166 *
167 * @return string
168 */
169 public static function get_dc_content_for_block( &$attr, $dcContent ) {
170 for ( $i = 0; $i < count( $attr['dcFields'] ); $i++ ) {
171 $dcGroup = $attr['dcFields'][ $i ];
172
173 if ( ! isset( $dcGroup['fields'] ) || ! isset( $dcGroup['id'] ) ) {
174 continue;
175 }
176
177 $dcContent[ $i ] .= '<div class="ultp-dynamic-content-group-common ultp-dynamic-content-group-' . $dcGroup['id'] . '">';
178
179 for ( $j = 0; $j < count( $dcGroup['fields'] ); $j++ ) {
180 $dcField = $dcGroup['fields'][ $j ];
181
182 $url = '';
183
184 if ( ( $dcField['linkEnabled'] && $dcField['linkSrc'] ) ) {
185 $url = self::get_link( $dcField['postId'], $dcField['linkSrc'] );
186 $url = esc_url( $url );
187 }
188
189 if ( ! empty( $url ) ) {
190 $dcContent[ $i ] .= '<a class="ultp-dynamic-content-field-anchor" href="' . $url . '">';
191 }
192
193 $dcContent[ $i ] .= '<div class="ultp-dynamic-content-field-common ultp-dynamic-content-field-' . $dcField['id'] . '">';
194 if ( ! empty( $dcField['icon'] ) ) {
195 $dcContent[ $i ] .= '<span class="ultp-dynamic-content-field-icon">' . ultimate_post()->get_svg_icon( $dcField['icon'] ) . '</span>';
196 }
197 $dcContent[ $i ] .= $dcField['beforeText'] ? '<p class="ultp-dynamic-content-field-before">' . $dcField['beforeText'] . '</p>' : '';
198
199 $text = '';
200
201 if ( str_starts_with( $dcField['dataSrc'], 'site_' ) ) {
202 $text = self::get_site_info( $dcField['dataSrc'] );
203 } elseif ( str_starts_with( $dcField['contentSrc'], 'post_' ) ) {
204 $text = self::get_post_info( $dcField['postId'], $dcField['contentSrc'] );
205 } elseif ( str_starts_with( $dcField['contentSrc'], 'a_' ) ) {
206 $text = self::get_author_info( $dcField['postId'], $dcField['contentSrc'] );
207 } elseif ( self::is_field_key( $dcField['contentSrc'] ) ) {
208 $text = self::get_field_value( $dcField['postId'], $dcField['contentSrc'] );
209 }
210
211 if ( ! empty( $text ) && ! empty( $dcField['maxCharLen'] ) && intval( $dcField['maxCharLen'] ) > 0 ) {
212 $text = self::limit_str( $text, intval( $dcField['maxCharLen'] ) );
213 }
214
215 if ( empty( $text ) && '0' !== $text && 0 !== $text ) {
216 $text = $dcField['fallback'];
217 }
218
219 $dcContent[ $i ] .= '<p class="ultp-dynamic-content-field-dc">' . sanitize_text_field( $text ) . '</p>';
220
221 $dcContent[ $i ] .= $dcField['afterText'] ? '<p class="ultp-dynamic-content-field-after">' . $dcField['afterText'] . '</p>' : '';
222 $dcContent[ $i ] .= '</div>';
223
224 if ( ! empty( $url ) ) {
225 $dcContent[ $i ] .= '</a>';
226 }
227 }
228
229 $dcContent[ $i ] .= '</div>';
230 }
231
232 return $dcContent;
233 }
234
235
236 /**
237 * Get field value
238 *
239 * @since 4.1.1
240 *
241 * @param int $post_id
242 * @param string $meta_key
243 *
244 * @return string
245 */
246 public static function get_field_value( $post_id, $meta_key ) {
247 if ( empty( $meta_key ) ) {
248 return '';
249 }
250
251 if ( empty( $post_id ) ) {
252 if ( in_the_loop() ) {
253 $post_id = get_the_ID();
254 } else {
255 global $post;
256 $post_id = isset( $post->ID ) ? $post->ID : '';
257 }
258 }
259
260 if ( self::check_prefix( $meta_key, 'cmeta_' ) ) {
261 if ( empty( $post_id ) ) {
262 return '';
263 }
264 return get_post_meta( $post_id, self::maybe_remove_prefix( $meta_key, 'cmeta_' ), true );
265 }
266
267 // Other field sources are answered by PostX Pro.
268 $value = apply_filters( 'ultp_dc_field_value', null, $meta_key, $post_id );
269 if ( null !== $value ) {
270 return $value;
271 }
272
273 return '';
274 }
275
276 /**
277 * Post info
278 *
279 * @since 4.1.1
280 *
281 * @param int $post_id Post ID.
282 * @param string $key Key value.
283 *
284 * @return string
285 */
286 public static function get_post_info( $post_id, $key ) {
287 if ( empty( $key ) ) {
288 return '';
289 }
290
291 $key = self::maybe_remove_prefix( $key, 'post_' );
292
293 $res = '';
294
295 if ( empty( $post_id ) ) {
296 if ( in_the_loop() ) {
297 $post_id = get_the_ID();
298 } else {
299 global $post;
300 $post_id = isset( $post->ID ) ? $post->ID : '';
301 }
302 }
303
304 switch ( $key ) {
305 case 'id':
306 $res = $post_id;
307 break;
308 case 'title':
309 $res = get_the_title( $post_id );
310 break;
311 case 'excerpt':
312 if ( ! doing_filter( 'get_the_excerpt' ) ) {
313 $res = get_the_excerpt( $post_id );
314 }
315 break;
316 case 'date':
317 $res = get_the_date( '', $post_id );
318 break;
319 case 'comments':
320 $res = strval( get_comment_count( $post_id )['all'] );
321 break;
322 }
323
324 return $res;
325 }
326
327 /**
328 * Author info
329 *
330 * @since 4.1.1
331 *
332 * @param int $post_id
333 * @param string $key
334 *
335 * @return string
336 */
337 public static function get_author_info( $post_id, $key ) {
338 if ( empty( $key ) ) {
339 return '';
340 }
341
342 // Removes prefix if present
343 $key = self::maybe_remove_prefix( $key, 'a_' );
344
345 if ( empty( $post_id ) ) {
346 if ( in_the_loop() ) {
347 $post = get_post();
348 } else {
349 global $post;
350 }
351 } else {
352 $post = get_post( $post_id );
353 }
354
355 if ( ! $post instanceof \WP_Post ) {
356 return '';
357 }
358
359 return get_the_author_meta( $key, intval( $post->post_author ) );
360 }
361
362 /**
363 * Site info
364 *
365 * @since 4.1.1
366 *
367 * @param string $key
368 *
369 * @return string
370 */
371 public static function get_site_info( $key ) {
372 if ( empty( $key ) ) {
373 return '';
374 }
375
376 // Removes prefix if present
377 $key = self::maybe_remove_prefix( $key, 'site_' );
378
379 $res = '';
380
381 switch ( $key ) {
382 case 'title':
383 $res = get_bloginfo();
384 break;
385 case 'tagline':
386 $res = get_bloginfo( 'description' );
387 break;
388 case 'arc_title':
389 $res = self::get_page_title();
390 break;
391 case 'arc_desc':
392 $res = wp_kses_post( get_the_archive_description() );
393 break;
394 }
395
396 return $res;
397 }
398
399 /**
400 * Gets the link
401 *
402 * @since 4.1.1
403 * @param string $post_id Post ID.
404 * @param string $key Key value.
405 *
406 * @return string
407 */
408 public static function get_link( $post_id, $key ) {
409 $res = '';
410
411 if ( empty( $post_id ) ) {
412 if ( in_the_loop() ) {
413 $post_id = get_the_ID();
414 } else {
415 global $post;
416 $post_id = isset( $post->ID ) ? $post->ID : '';
417 }
418 }
419
420 if ( self::is_field_key( $key ) ) {
421 $res = self::get_field_value( $post_id, $key );
422 } else {
423 $author_id = absint( get_post_field( 'post_author', $post_id ) );
424 $key = self::maybe_remove_prefix( $key, 'link_' );
425 switch ( $key ) {
426 case 'post_featured_image':
427 $res = wp_get_attachment_image_url( get_post_thumbnail_id( $post_id ), 'full' );
428 break;
429 case 'post_permalink':
430 $res = get_permalink( $post_id );
431 break;
432 case 'post_comments':
433 $res = get_comments_link( $post_id );
434 break;
435 case 'a_profile':
436 $res = get_avatar_url( $author_id );
437 break;
438 case 'a_arc':
439 $res = get_author_posts_url( $author_id );
440 break;
441 case 'a_page':
442 $res = get_the_author_meta( 'url', $author_id );
443 break;
444 }
445 }
446
447 return $res;
448 }
449
450 /**
451 * Replaces substring with a string
452 *
453 * @since 4.1.1
454 * @param string $old Post content string.
455 * @param string $new Value to insert.
456 *
457 * @return string
458 */
459 public static function replace( $old, $new ) {
460 $pattern = '/<span\s+class\s*=\s*["\']ultp-richtext-dynamic-content["\']\s*>(.*?)<\/span>/';
461
462 // Replaces only the first match, the rest matches are deleted.
463 $res = preg_replace_callback(
464 $pattern,
465 function ( $matches ) use ( &$firstMatch, $new ) {
466 if ( ! isset( $firstMatch ) ) {
467 $firstMatch = $matches[0];
468 return $new;
469 } else {
470 return '';
471 }
472 },
473 $old
474 );
475
476 return is_null( $res ) ? $old : $res;
477 }
478
479 /**
480 * Gets the post types
481 *
482 * @since 4.1.1
483 *
484 * @return array[string]
485 */
486 public static function get_post_types() {
487 $res = array();
488 $post_types = get_post_types(
489 array(
490 'public' => true,
491 ),
492 'objects'
493 );
494
495 foreach ( $post_types as $_ => $value ) {
496 if ( 'attachment' !== $value->name ) {
497 $res[] = array(
498 'value' => $value->name,
499 'label' => $value->label,
500 );
501 }
502 }
503
504 return $res;
505 }
506
507 /**
508 * Checks if Dynamic Content is active
509 *
510 * @since 4.1.1
511 *
512 * @param array $attr Block attributes.
513 *
514 * @return boolean
515 */
516 public static function is_dc_active( &$attr ) {
517 return ultimate_post()->get_setting( 'ultp_dynamic_content' ) == 'true' &&
518 isset( $attr['dcEnabled'] );
519 }
520
521 /**
522 * Get page title
523 *
524 * @since 4.1.1
525 *
526 * @return string
527 */
528 private static function get_page_title() {
529 $title = '';
530
531 if ( is_singular() ) {
532 $title = get_the_title();
533 } elseif ( is_search() ) {
534 /* translators: %s: Search query. */
535 $title = sprintf( esc_html__( 'Search Results for: %s', 'ultimate-post' ), get_search_query() );
536
537 if ( get_query_var( 'paged' ) ) {
538 /* translators: %s: Page number. */
539 $title .= sprintf( esc_html__( '&nbsp;&ndash; Page %s', 'ultimate-post' ), get_query_var( 'paged' ) );
540 }
541 } elseif ( is_category() ) {
542 $title = single_cat_title( '', false );
543 } elseif ( is_tag() ) {
544 $title = single_tag_title( '', false );
545 } elseif ( is_author() ) {
546 $title = get_the_author();
547 } elseif ( is_year() ) {
548 $title = get_the_date( __( 'Y', 'ultimate-post' ) );
549 } elseif ( is_month() ) {
550 $title = get_the_date( __( 'F Y', 'ultimate-post' ) );
551 } elseif ( is_day() ) {
552 $title = get_the_date( __( 'F j, Y', 'ultimate-post' ) );
553 } elseif ( is_post_type_archive() ) {
554 $title = post_type_archive_title( '', false );
555 } elseif ( is_tax() ) {
556 $title = single_term_title( '', false );
557 } elseif ( is_archive() ) {
558 $title = esc_html__( 'Archives', 'ultimate-post' );
559 } elseif ( is_404() ) {
560 $title = esc_html__( 'Page Not Found', 'ultimate-post' );
561 }
562
563 return $title;
564 }
565
566 /**
567 * Properly decodes HTML chars from string
568 *
569 * @param mixed $str
570 * @return mixed
571 */
572 public static function process( $str ) {
573 if ( is_string( $str ) ) {
574 $res = esc_html( html_entity_decode( $str ) );
575 return str_replace( '&amp;', '&', $res );
576 }
577 return $str;
578 }
579
580 /**
581 * Limits a string by word
582 *
583 * @param string $str
584 * @param int $limit
585 * @return string
586 */
587 public static function limit_str( $str, $limit ) {
588 return join( ' ', array_slice( explode( ' ', $str ), 0, $limit ) );
589 }
590
591 /**
592 * Whether a dynamic content key is a custom field
593 *
594 * Plain custom metas (`cmeta_`) are handled here; other field sources are answered by PostX Pro.
595 *
596 * @param string $key Key with its prefix.
597 * @return bool
598 */
599 private static function is_field_key( $key ) {
600 if ( ! is_string( $key ) || '' === $key ) {
601 return false;
602 }
603 if ( self::check_prefix( $key, 'cmeta_' ) ) {
604 return true;
605 }
606 return (bool) apply_filters( 'ultp_dc_is_field_key', false, $key );
607 }
608
609 private static function check_prefix( $key, $prefix ) {
610 return substr( $key, 0, strlen( $prefix ) ) === $prefix;
611 }
612
613 private static function maybe_remove_prefix( $key, $prefix ) {
614 if ( substr( $key, 0, strlen( $prefix ) ) === $prefix ) {
615 return substr( $key, strlen( $prefix ) );
616 }
617 return $key;
618 }
619 }
620