| 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 ( |
| 98 |
str_starts_with( $content_src, 'acf_' ) || |
| 99 |
str_starts_with( $content_src, 'cmeta_' ) || |
| 100 |
str_starts_with( $content_src, 'pods_' ) || |
| 101 |
str_starts_with( $content_src, 'mb_' ) |
| 102 |
) { |
| 103 |
$res = self::get_field_value( $post_id, $content_src ); |
| 104 |
} elseif ( |
| 105 |
str_starts_with( $content_src, 'link_' ) |
| 106 |
) { |
| 107 |
$res = self::get_link( $post_id, $content_src ); |
| 108 |
} |
| 109 |
|
| 110 |
if ( empty( $res ) ) { |
| 111 |
$res = ULTP_URL . 'assets/img/ultp-placeholder.jpg'; |
| 112 |
} |
| 113 |
|
| 114 |
return $res; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Applies dynamic content to RichText |
| 119 |
* |
| 120 |
* @since 4.1.1 |
| 121 |
* |
| 122 |
* @param array $attr - block attributes. |
| 123 |
* |
| 124 |
* @return string |
| 125 |
*/ |
| 126 |
public static function get_dc_content_for_rich_text( &$attr ) { |
| 127 |
$text = ''; |
| 128 |
$url = ''; |
| 129 |
|
| 130 |
$data_src = $attr['dc']['dataSrc']; |
| 131 |
$content_src = $attr['dc']['contentSrc']; |
| 132 |
$link_src = $attr['dc']['linkSrc']; |
| 133 |
$link_enabled = $attr['dc']['linkEnabled']; |
| 134 |
$post_id = $attr['dc']['postId']; |
| 135 |
$fallback = $attr['dc']['fallback']; |
| 136 |
$max_length = $attr['dc']['maxCharLen']; |
| 137 |
|
| 138 |
if ( str_starts_with( $data_src, 'site_' ) ) { |
| 139 |
$text = self::get_site_info( $data_src ); |
| 140 |
} elseif ( str_starts_with( $content_src, 'post_' ) ) { |
| 141 |
$text = self::get_post_info( $post_id, $content_src ); |
| 142 |
} elseif ( str_starts_with( $content_src, 'a_' ) ) { |
| 143 |
$text = self::get_author_info( $post_id, $content_src ); |
| 144 |
} elseif ( |
| 145 |
str_starts_with( $content_src, 'acf_' ) || |
| 146 |
str_starts_with( $content_src, 'cmeta_' ) || |
| 147 |
str_starts_with( $content_src, 'pods_' ) || |
| 148 |
str_starts_with( $content_src, 'mb_' ) |
| 149 |
) { |
| 150 |
$text = self::get_field_value( $post_id, $content_src ); |
| 151 |
} |
| 152 |
|
| 153 |
if ( ! empty( $text ) && ! empty( $max_length ) && is_string( $max_length ) && intval( $max_length ) > 0 ) { |
| 154 |
$text = self::limit_str( $text, intval( $max_length ) ); |
| 155 |
} |
| 156 |
|
| 157 |
if ( empty( $text ) ) { |
| 158 |
$text = $fallback; |
| 159 |
} |
| 160 |
|
| 161 |
$text = $attr['dc']['beforeText'] . $text . $attr['dc']['afterText']; |
| 162 |
|
| 163 |
if ( ( $link_enabled && $link_src ) ) { |
| 164 |
$url = self::get_link( $post_id, $link_src ); |
| 165 |
} |
| 166 |
|
| 167 |
return array( $text, $url ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Get Html of Dynamic Content for Blocks |
| 172 |
* |
| 173 |
* @since 4.1.1 |
| 174 |
* |
| 175 |
* @param array $attr |
| 176 |
* |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
public static function get_dc_content_for_block( &$attr, $dcContent ) { |
| 180 |
for ( $i = 0; $i < count( $attr['dcFields'] ); $i++ ) { |
| 181 |
$dcGroup = $attr['dcFields'][ $i ]; |
| 182 |
|
| 183 |
if ( ! isset( $dcGroup['fields'] ) || ! isset( $dcGroup['id'] ) ) { |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
$dcContent[ $i ] .= '<div class="ultp-dynamic-content-group-common ultp-dynamic-content-group-' . $dcGroup['id'] . '">'; |
| 188 |
|
| 189 |
for ( $j = 0; $j < count( $dcGroup['fields'] ); $j++ ) { |
| 190 |
$dcField = $dcGroup['fields'][ $j ]; |
| 191 |
|
| 192 |
$url = ''; |
| 193 |
|
| 194 |
if ( ( $dcField['linkEnabled'] && $dcField['linkSrc'] ) ) { |
| 195 |
$url = self::get_link( $dcField['postId'], $dcField['linkSrc'] ); |
| 196 |
$url = esc_url( $url ); |
| 197 |
} |
| 198 |
|
| 199 |
if ( ! empty( $url ) ) { |
| 200 |
$dcContent[ $i ] .= '<a class="ultp-dynamic-content-field-anchor" href="' . $url . '">'; |
| 201 |
} |
| 202 |
|
| 203 |
$dcContent[ $i ] .= '<div class="ultp-dynamic-content-field-common ultp-dynamic-content-field-' . $dcField['id'] . '">'; |
| 204 |
if ( ! empty( $dcField['icon'] ) ) { |
| 205 |
$dcContent[ $i ] .= '<span class="ultp-dynamic-content-field-icon">' . ultimate_post()->get_svg_icon( $dcField['icon'] ) . '</span>'; |
| 206 |
} |
| 207 |
$dcContent[ $i ] .= $dcField['beforeText'] ? '<p class="ultp-dynamic-content-field-before">' . $dcField['beforeText'] . '</p>' : ''; |
| 208 |
|
| 209 |
$text = ''; |
| 210 |
|
| 211 |
if ( str_starts_with( $dcField['dataSrc'], 'site_' ) ) { |
| 212 |
$text = self::get_site_info( $dcField['dataSrc'] ); |
| 213 |
} elseif ( str_starts_with( $dcField['contentSrc'], 'post_' ) ) { |
| 214 |
$text = self::get_post_info( $dcField['postId'], $dcField['contentSrc'] ); |
| 215 |
} elseif ( str_starts_with( $dcField['contentSrc'], 'a_' ) ) { |
| 216 |
$text = self::get_author_info( $dcField['postId'], $dcField['contentSrc'] ); |
| 217 |
} elseif ( |
| 218 |
str_starts_with( $dcField['contentSrc'], 'acf_' ) || |
| 219 |
str_starts_with( $dcField['contentSrc'], 'cmeta_' ) || |
| 220 |
str_starts_with( $dcField['contentSrc'], 'pods_' ) || |
| 221 |
str_starts_with( $dcField['contentSrc'], 'mb_' ) |
| 222 |
) { |
| 223 |
$text = self::get_field_value( $dcField['postId'], $dcField['contentSrc'] ); |
| 224 |
} |
| 225 |
|
| 226 |
if ( ! empty( $text ) && ! empty( $dcField['maxCharLen'] ) && intval( $dcField['maxCharLen'] ) > 0 ) { |
| 227 |
$text = self::limit_str( $text, intval( $dcField['maxCharLen'] ) ); |
| 228 |
} |
| 229 |
|
| 230 |
if ( empty( $text ) && '0' !== $text && 0 !== $text ) { |
| 231 |
$text = $dcField['fallback']; |
| 232 |
} |
| 233 |
|
| 234 |
$dcContent[ $i ] .= '<p class="ultp-dynamic-content-field-dc">' . sanitize_text_field( $text ) . '</p>'; |
| 235 |
|
| 236 |
$dcContent[ $i ] .= $dcField['afterText'] ? '<p class="ultp-dynamic-content-field-after">' . $dcField['afterText'] . '</p>' : ''; |
| 237 |
$dcContent[ $i ] .= '</div>'; |
| 238 |
|
| 239 |
if ( ! empty( $url ) ) { |
| 240 |
$dcContent[ $i ] .= '</a>'; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
$dcContent[ $i ] .= '</div>'; |
| 245 |
} |
| 246 |
|
| 247 |
return $dcContent; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Get field value |
| 253 |
* |
| 254 |
* @since 4.1.1 |
| 255 |
* |
| 256 |
* @param int $post_id |
| 257 |
* @param string $meta_key |
| 258 |
* |
| 259 |
* @return string |
| 260 |
*/ |
| 261 |
public static function get_field_value( $post_id, $meta_key ) { |
| 262 |
if ( empty( $meta_key ) ) { |
| 263 |
return ''; |
| 264 |
} |
| 265 |
|
| 266 |
if ( empty( $post_id ) ) { |
| 267 |
if ( in_the_loop() ) { |
| 268 |
$post_id = get_the_ID(); |
| 269 |
} else { |
| 270 |
global $post; |
| 271 |
$post_id = isset( $post->ID ) ? $post->ID : ''; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
if ( self::check_prefix_and_remove( $meta_key, 'cmeta_' ) ) { |
| 276 |
if ( empty( $post_id ) ) { |
| 277 |
return ''; |
| 278 |
} |
| 279 |
return get_post_meta( $post_id, $meta_key, true ); |
| 280 |
} |
| 281 |
|
| 282 |
if ( self::check_prefix_and_remove( $meta_key, 'acf_' ) ) { |
| 283 |
if ( class_exists( 'ACF' ) ) { |
| 284 |
if ( ! empty( $post_id ) ) { |
| 285 |
$value = get_field( $meta_key, $post_id ); |
| 286 |
return self::extract_acf_value( $value ); |
| 287 |
} else { |
| 288 |
return get_field( $meta_key, false ); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if ( self::check_prefix_and_remove( $meta_key, 'mb_' ) ) { |
| 294 |
if ( function_exists( 'rwmb_get_value' ) ) { |
| 295 |
$value = rwmb_get_value( $meta_key, array(), $post_id ); |
| 296 |
return self::extract_mb_value( $value, $meta_key, $post_id ); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
if ( self::check_prefix_and_remove( $meta_key, 'pods_' ) ) { |
| 301 |
if ( function_exists( 'pods' ) ) { |
| 302 |
$pods = pods( get_post_type( $post_id ), $post_id ); |
| 303 |
if ( is_object( $pods ) && method_exists( $pods, 'exists' ) && |
| 304 |
$pods->exists() && method_exists( $pods, 'field' ) |
| 305 |
) { |
| 306 |
$value = $pods->field( $meta_key ); |
| 307 |
return self::extract_pods_value( $value ); |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
return ''; |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Post info |
| 317 |
* |
| 318 |
* @since 4.1.1 |
| 319 |
* |
| 320 |
* @param int $post_id Post ID. |
| 321 |
* @param string $key Key value. |
| 322 |
* |
| 323 |
* @return string |
| 324 |
*/ |
| 325 |
public static function get_post_info( $post_id, $key ) { |
| 326 |
if ( empty( $key ) ) { |
| 327 |
return ''; |
| 328 |
} |
| 329 |
|
| 330 |
$key = self::maybe_remove_prefix( $key, 'post_' ); |
| 331 |
|
| 332 |
$res = ''; |
| 333 |
|
| 334 |
if ( empty( $post_id ) ) { |
| 335 |
if ( in_the_loop() ) { |
| 336 |
$post_id = get_the_ID(); |
| 337 |
} else { |
| 338 |
global $post; |
| 339 |
$post_id = isset( $post->ID ) ? $post->ID : ''; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
switch ( $key ) { |
| 344 |
case 'id': |
| 345 |
$res = $post_id; |
| 346 |
break; |
| 347 |
case 'title': |
| 348 |
$res = get_the_title( $post_id ); |
| 349 |
break; |
| 350 |
case 'excerpt': |
| 351 |
if ( ! doing_filter( 'get_the_excerpt' ) ) { |
| 352 |
$res = get_the_excerpt( $post_id ); |
| 353 |
} |
| 354 |
break; |
| 355 |
case 'date': |
| 356 |
$res = get_the_date( '', $post_id ); |
| 357 |
break; |
| 358 |
case 'comments': |
| 359 |
$res = strval( get_comment_count( $post_id )['all'] ); |
| 360 |
break; |
| 361 |
} |
| 362 |
|
| 363 |
return $res; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Author info |
| 368 |
* |
| 369 |
* @since 4.1.1 |
| 370 |
* |
| 371 |
* @param int $post_id |
| 372 |
* @param string $key |
| 373 |
* |
| 374 |
* @return string |
| 375 |
*/ |
| 376 |
public static function get_author_info( $post_id, $key ) { |
| 377 |
if ( empty( $key ) ) { |
| 378 |
return ''; |
| 379 |
} |
| 380 |
|
| 381 |
// Removes prefix if present |
| 382 |
$key = self::maybe_remove_prefix( $key, 'a_' ); |
| 383 |
|
| 384 |
if ( empty( $post_id ) ) { |
| 385 |
if ( in_the_loop() ) { |
| 386 |
$post = get_post(); |
| 387 |
} else { |
| 388 |
global $post; |
| 389 |
} |
| 390 |
} else { |
| 391 |
$post = get_post( $post_id ); |
| 392 |
} |
| 393 |
|
| 394 |
if ( ! $post instanceof \WP_Post ) { |
| 395 |
return ''; |
| 396 |
} |
| 397 |
|
| 398 |
return get_the_author_meta( $key, intval( $post->post_author ) ); |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Site info |
| 403 |
* |
| 404 |
* @since 4.1.1 |
| 405 |
* |
| 406 |
* @param string $key |
| 407 |
* |
| 408 |
* @return string |
| 409 |
*/ |
| 410 |
public static function get_site_info( $key ) { |
| 411 |
if ( empty( $key ) ) { |
| 412 |
return ''; |
| 413 |
} |
| 414 |
|
| 415 |
// Removes prefix if present |
| 416 |
$key = self::maybe_remove_prefix( $key, 'site_' ); |
| 417 |
|
| 418 |
$res = ''; |
| 419 |
|
| 420 |
switch ( $key ) { |
| 421 |
case 'title': |
| 422 |
$res = get_bloginfo(); |
| 423 |
break; |
| 424 |
case 'tagline': |
| 425 |
$res = get_bloginfo( 'description' ); |
| 426 |
break; |
| 427 |
case 'arc_title': |
| 428 |
$res = self::get_page_title(); |
| 429 |
break; |
| 430 |
case 'arc_desc': |
| 431 |
$res = wp_kses_post( get_the_archive_description() ); |
| 432 |
break; |
| 433 |
} |
| 434 |
|
| 435 |
return $res; |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Gets the link |
| 440 |
* |
| 441 |
* @since 4.1.1 |
| 442 |
* @param string $post_id Post ID. |
| 443 |
* @param string $key Key value. |
| 444 |
* |
| 445 |
* @return string |
| 446 |
*/ |
| 447 |
public static function get_link( $post_id, $key ) { |
| 448 |
$res = ''; |
| 449 |
|
| 450 |
if ( empty( $post_id ) ) { |
| 451 |
if ( in_the_loop() ) { |
| 452 |
$post_id = get_the_ID(); |
| 453 |
} else { |
| 454 |
global $post; |
| 455 |
$post_id = isset( $post->ID ) ? $post->ID : ''; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
if ( self::check_prefix( $key, 'acf_' ) || |
| 460 |
self::check_prefix( $key, 'mb_' ) || |
| 461 |
self::check_prefix( $key, 'cmeta_' ) || |
| 462 |
self::check_prefix( $key, 'pods_' ) |
| 463 |
) { |
| 464 |
$res = self::get_field_value( $post_id, $key ); |
| 465 |
} else { |
| 466 |
$author_id = absint( get_post_field( 'post_author', $post_id ) ); |
| 467 |
$key = self::maybe_remove_prefix( $key, 'link_' ); |
| 468 |
switch ( $key ) { |
| 469 |
case 'post_featured_image': |
| 470 |
$res = wp_get_attachment_image_url( get_post_thumbnail_id( $post_id ), 'full' ); |
| 471 |
break; |
| 472 |
case 'post_permalink': |
| 473 |
$res = get_permalink( $post_id ); |
| 474 |
break; |
| 475 |
case 'post_comments': |
| 476 |
$res = get_comments_link( $post_id ); |
| 477 |
break; |
| 478 |
case 'a_profile': |
| 479 |
$res = get_avatar_url( $author_id ); |
| 480 |
break; |
| 481 |
case 'a_arc': |
| 482 |
$res = get_author_posts_url( $author_id ); |
| 483 |
break; |
| 484 |
case 'a_page': |
| 485 |
$res = get_the_author_meta( 'url', $author_id ); |
| 486 |
break; |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
return $res; |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* Replaces substring with a string |
| 495 |
* |
| 496 |
* @since 4.1.1 |
| 497 |
* @param string $old Post content string. |
| 498 |
* @param string $new Value to insert. |
| 499 |
* |
| 500 |
* @return string |
| 501 |
*/ |
| 502 |
public static function replace( $old, $new ) { |
| 503 |
$pattern = '/<span\s+class\s*=\s*["\']ultp-richtext-dynamic-content["\']\s*>(.*?)<\/span>/'; |
| 504 |
|
| 505 |
// Replaces only the first match, the rest matches are deleted. |
| 506 |
$res = preg_replace_callback( |
| 507 |
$pattern, |
| 508 |
function ( $matches ) use ( &$firstMatch, $new ) { |
| 509 |
if ( ! isset( $firstMatch ) ) { |
| 510 |
$firstMatch = $matches[0]; |
| 511 |
return $new; |
| 512 |
} else { |
| 513 |
return ''; |
| 514 |
} |
| 515 |
}, |
| 516 |
$old |
| 517 |
); |
| 518 |
|
| 519 |
return is_null( $res ) ? $old : $res; |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* Gets the post types |
| 524 |
* |
| 525 |
* @since 4.1.1 |
| 526 |
* |
| 527 |
* @return array[string] |
| 528 |
*/ |
| 529 |
public static function get_post_types() { |
| 530 |
$res = array(); |
| 531 |
$post_types = get_post_types( |
| 532 |
array( |
| 533 |
'public' => true, |
| 534 |
), |
| 535 |
'objects' |
| 536 |
); |
| 537 |
|
| 538 |
foreach ( $post_types as $_ => $value ) { |
| 539 |
if ( 'attachment' !== $value->name ) { |
| 540 |
$res[] = array( |
| 541 |
'value' => $value->name, |
| 542 |
'label' => $value->label, |
| 543 |
); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
return $res; |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* Checks if Dynamic Content is active |
| 552 |
* |
| 553 |
* @since 4.1.1 |
| 554 |
* |
| 555 |
* @param array $attr Block attributes. |
| 556 |
* |
| 557 |
* @return boolean |
| 558 |
*/ |
| 559 |
public static function is_dc_active( &$attr ) { |
| 560 |
return ultimate_post()->get_setting( 'ultp_dynamic_content' ) == 'true' && |
| 561 |
isset( $attr['dcEnabled'] ); |
| 562 |
} |
| 563 |
|
| 564 |
/** |
| 565 |
* Get page title |
| 566 |
* |
| 567 |
* @since 4.1.1 |
| 568 |
* |
| 569 |
* @return string |
| 570 |
*/ |
| 571 |
private static function get_page_title() { |
| 572 |
$title = ''; |
| 573 |
|
| 574 |
if ( is_singular() ) { |
| 575 |
$title = get_the_title(); |
| 576 |
} elseif ( is_search() ) { |
| 577 |
$title = sprintf( esc_html__( 'Search Results for: %s', 'ultimate-post' ), get_search_query() ); |
| 578 |
|
| 579 |
if ( get_query_var( 'paged' ) ) { |
| 580 |
$title .= sprintf( esc_html__( ' – Page %s', 'ultimate-post' ), get_query_var( 'paged' ) ); |
| 581 |
} |
| 582 |
} elseif ( is_category() ) { |
| 583 |
$title = single_cat_title( '', false ); |
| 584 |
} elseif ( is_tag() ) { |
| 585 |
$title = single_tag_title( '', false ); |
| 586 |
} elseif ( is_author() ) { |
| 587 |
$title = get_the_author(); |
| 588 |
} elseif ( is_year() ) { |
| 589 |
$title = get_the_date( __( 'Y', 'ultimate-post' ) ); |
| 590 |
} elseif ( is_month() ) { |
| 591 |
$title = get_the_date( __( 'F Y', 'ultimate-post' ) ); |
| 592 |
} elseif ( is_day() ) { |
| 593 |
$title = get_the_date( __( 'F j, Y', 'ultimate-post' ) ); |
| 594 |
} elseif ( is_post_type_archive() ) { |
| 595 |
$title = post_type_archive_title( '', false ); |
| 596 |
} elseif ( is_tax() ) { |
| 597 |
$title = single_term_title( '', false ); |
| 598 |
} elseif ( is_archive() ) { |
| 599 |
$title = esc_html__( 'Archives', 'ultimate-post' ); |
| 600 |
} elseif ( is_404() ) { |
| 601 |
$title = esc_html__( 'Page Not Found', 'ultimate-post' ); |
| 602 |
} |
| 603 |
|
| 604 |
return $title; |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* Formats ACF value based upon data type |
| 609 |
* |
| 610 |
* @since 4.1.1 |
| 611 |
* |
| 612 |
* @param array|string $value ACF Meta Value. |
| 613 |
* |
| 614 |
* @return string |
| 615 |
*/ |
| 616 |
private static function extract_acf_value( $value ) { |
| 617 |
if ( is_array( $value ) ) { |
| 618 |
$image_id = absint( $value['id'] ); |
| 619 |
$value = wp_get_attachment_image_src( $image_id ); |
| 620 |
} elseif ( is_numeric( $value ) ) { |
| 621 |
return $value; |
| 622 |
} |
| 623 |
|
| 624 |
return $value; |
| 625 |
} |
| 626 |
|
| 627 |
/** |
| 628 |
* Formats Pods value based upon data type |
| 629 |
* |
| 630 |
* @since 4.1.1 |
| 631 |
* |
| 632 |
* @param array|string $value Pods Meta Value. |
| 633 |
* |
| 634 |
* @return string |
| 635 |
*/ |
| 636 |
private static function extract_pods_value( $value ) { |
| 637 |
if ( is_array( $value ) ) { |
| 638 |
|
| 639 |
// Single image |
| 640 |
if ( isset( $value['guid'] ) ) { |
| 641 |
return $value['guid']; |
| 642 |
} |
| 643 |
|
| 644 |
// Multi image |
| 645 |
if ( isset( $value[0]['guid'] ) ) { |
| 646 |
return $value[0]['guid']; |
| 647 |
} |
| 648 |
|
| 649 |
// Repeater |
| 650 |
if ( isset( $value[0] ) && |
| 651 |
( is_string( $value[0] ) || is_numeric( $value[0] ) || is_bool( $value[0] ) ) |
| 652 |
) { |
| 653 |
return $value[0]; |
| 654 |
} |
| 655 |
|
| 656 |
return ''; |
| 657 |
} |
| 658 |
|
| 659 |
return $value; |
| 660 |
} |
| 661 |
|
| 662 |
/** |
| 663 |
* Formats Meta Box value based upon data type |
| 664 |
* |
| 665 |
* @since 4.1.1 |
| 666 |
* |
| 667 |
* @param array|string $value |
| 668 |
* |
| 669 |
* @return string |
| 670 |
*/ |
| 671 |
private static function extract_mb_value( $custom_fields, $meta_key, $post_id ) { |
| 672 |
|
| 673 |
if ( ! is_array( $custom_fields ) ) { |
| 674 |
return strval( $custom_fields ); |
| 675 |
} |
| 676 |
|
| 677 |
$field_data = rwmb_get_field_settings( $meta_key, array(), $post_id ); |
| 678 |
|
| 679 |
// Process the first element if its an array |
| 680 |
foreach ( $custom_fields as $field ) { |
| 681 |
$field_type = $field_data['type']; |
| 682 |
$field_value = ''; |
| 683 |
|
| 684 |
switch ( $field_type ) { |
| 685 |
case 'checkbox': |
| 686 |
case 'radio': |
| 687 |
$field_value = isset( $field['checked'] ) ? 'checked' : ''; |
| 688 |
break; |
| 689 |
case 'select': |
| 690 |
case 'radio_list': |
| 691 |
case 'checkbox_list': |
| 692 |
case 'taxonomy': |
| 693 |
case 'taxonomy_advanced': |
| 694 |
case 'post': |
| 695 |
case 'post_advanced': |
| 696 |
case 'post_checkbox_list': |
| 697 |
case 'post_select': |
| 698 |
$field_value = isset( $field['selected'] ) ? $field['selected'] : ''; |
| 699 |
break; |
| 700 |
case 'file': |
| 701 |
case 'file_input': |
| 702 |
case 'file_advanced': |
| 703 |
case 'file_upload': |
| 704 |
$field_value = isset( $field['url'] ) ? $field['url'] : $field['id']; |
| 705 |
break; |
| 706 |
case 'single_image': |
| 707 |
$field_value = wp_get_attachment_image_url( $custom_fields['ID'], 'full' ); |
| 708 |
break; |
| 709 |
case 'image': |
| 710 |
case 'image_advanced': |
| 711 |
case 'image_upload': |
| 712 |
case 'plupload_image': |
| 713 |
case 'thickbox_image': |
| 714 |
$field_value = wp_get_attachment_image_url( $field['ID'], 'full' ); |
| 715 |
break; |
| 716 |
case 'date': |
| 717 |
case 'datetime': |
| 718 |
case 'time': |
| 719 |
$field_value = $field['date']; |
| 720 |
break; |
| 721 |
default: |
| 722 |
$field_value = $field['value']; |
| 723 |
break; |
| 724 |
} |
| 725 |
|
| 726 |
return $field_value; |
| 727 |
} |
| 728 |
|
| 729 |
return ''; |
| 730 |
} |
| 731 |
|
| 732 |
/** |
| 733 |
* Properly decodes HTML chars from string |
| 734 |
* |
| 735 |
* @param mixed $str |
| 736 |
* @return mixed |
| 737 |
*/ |
| 738 |
public static function process( $str ) { |
| 739 |
if ( is_string( $str ) ) { |
| 740 |
$res = esc_html( html_entity_decode( $str ) ); |
| 741 |
return str_replace( '&', '&', $res ); |
| 742 |
} |
| 743 |
return $str; |
| 744 |
} |
| 745 |
|
| 746 |
/** |
| 747 |
* Limits a string by word |
| 748 |
* |
| 749 |
* @param string $str |
| 750 |
* @param int $limit |
| 751 |
* @return string |
| 752 |
*/ |
| 753 |
public static function limit_str( $str, $limit ) { |
| 754 |
return join( ' ', array_slice( explode( ' ', $str ), 0, $limit ) ); |
| 755 |
} |
| 756 |
|
| 757 |
private static function check_prefix( $key, $prefix ) { |
| 758 |
return substr( $key, 0, strlen( $prefix ) ) === $prefix; |
| 759 |
} |
| 760 |
|
| 761 |
private static function maybe_remove_prefix( $key, $prefix ) { |
| 762 |
if ( substr( $key, 0, strlen( $prefix ) ) === $prefix ) { |
| 763 |
return substr( $key, strlen( $prefix ) ); |
| 764 |
} |
| 765 |
return $key; |
| 766 |
} |
| 767 |
|
| 768 |
private static function check_prefix_and_remove( &$key, $prefix ) { |
| 769 |
if ( substr( $key, 0, strlen( $prefix ) ) === $prefix ) { |
| 770 |
$key = substr( $key, strlen( $prefix ) ); |
| 771 |
return true; |
| 772 |
} |
| 773 |
return false; |
| 774 |
} |
| 775 |
} |
| 776 |
|