← All changes
|
addons/dynamic_content/includes/DCController.php
+47
-353
5.0.41
→
5.1.0
View file →
| @@ -23,8 +23,14 @@ | ||
| 23 | 23 | */ |
| 24 | 24 | public function __construct() { |
| 25 | 25 | require_once ULTP_PATH . '/addons/dynamic_content/includes/DCService.php'; |
| 26 | 26 | |
| 27 | + // PostX Pro before 1.9.0 does not answer the custom field filters below. | |
| 28 | + if ( \ULTP\Includes\Durbin\Xpo::is_pro_before_1_9_0() ) { | |
| 29 | + require_once ULTP_PATH . '/addons/dynamic_content/includes/DCLegacyFields.php'; | |
| 30 | + new DCLegacyFields(); | |
| 31 | + } | |
| 32 | + | |
| 27 | 33 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 28 | 34 | } |
| 29 | 35 | |
| 30 | 36 | public function register_routes() { |
| @@ -179,31 +185,46 @@ | ||
| 179 | 185 | |
| 180 | 186 | // All metas |
| 181 | 187 | $all_custom_metas = $this->get_custom_metas( $post_id, $post_type ); |
| 182 | 188 | |
| 183 | - // ACF | |
| 184 | - $acf_field_keys = $this->get_acf_fields( $post_id, $field_type, $post_type ); | |
| 189 | + // Field sources other than plain custom metas, added by PostX Pro. | |
| 190 | + $sources = apply_filters( | |
| 191 | + 'ultp_dc_custom_field_sources', | |
| 192 | + array(), | |
| 193 | + array( | |
| 194 | + 'post_id' => $post_id, | |
| 195 | + 'post_type' => $post_type, | |
| 196 | + 'field_type' => $field_type, | |
| 197 | + ) | |
| 198 | + ); | |
| 199 | + $sources = is_array( $sources ) ? $sources : array(); | |
| 185 | 200 | |
| 186 | - // Meta Box | |
| 187 | - $mb_field_keys = $this->get_mb_fields( $post_id, $field_type, $post_type ); | |
| 201 | + // Keys and key prefixes that belong to those sources are not plain custom metas. | |
| 202 | + $source_keys = array(); | |
| 203 | + $source_prefixes = array(); | |
| 204 | + foreach ( $sources as $source ) { | |
| 205 | + if ( ! empty( $source['_fields'] ) && is_array( $source['_fields'] ) ) { | |
| 206 | + $source_keys = array_merge( $source_keys, $source['_fields'] ); | |
| 207 | + } | |
| 208 | + if ( ! empty( $source['prefixes'] ) && is_array( $source['prefixes'] ) ) { | |
| 209 | + $source_prefixes = array_merge( $source_prefixes, $source['prefixes'] ); | |
| 210 | + } | |
| 211 | + } | |
| 188 | 212 | |
| 189 | - // Pods | |
| 190 | - $pods_field_keys = $this->get_pods_fields( $post_id, $field_type, $post_type ); | |
| 191 | - | |
| 192 | - $filtered_custom_metas = array_values( array_diff( $all_custom_metas, $acf_field_keys['_fields'] ) ); | |
| 193 | - $filtered_custom_metas = array_filter( | |
| 194 | - $filtered_custom_metas, | |
| 195 | - function ( $item ) use ( $acf_field_keys ) { | |
| 196 | - foreach ( $acf_field_keys['prefixes'] as $prefix ) { | |
| 197 | - if ( str_starts_with( $item, $prefix ) ) { | |
| 198 | - return false; | |
| 213 | + $filtered_custom_metas = array_values( array_diff( $all_custom_metas, $source_keys ) ); | |
| 214 | + $filtered_custom_metas = array_values( | |
| 215 | + array_filter( | |
| 216 | + $filtered_custom_metas, | |
| 217 | + function ( $item ) use ( $source_prefixes ) { | |
| 218 | + foreach ( $source_prefixes as $prefix ) { | |
| 219 | + if ( str_starts_with( $item, $prefix ) ) { | |
| 220 | + return false; | |
| 221 | + } | |
| 199 | 222 | } |
| 223 | + return true; | |
| 200 | 224 | } |
| 201 | - return true; | |
| 202 | - } | |
| 225 | + ) | |
| 203 | 226 | ); |
| 204 | - $filtered_custom_metas = array_values( array_diff( $filtered_custom_metas, $mb_field_keys['_fields'] ) ); | |
| 205 | - $filtered_custom_metas = array_values( array_diff( $filtered_custom_metas, $pods_field_keys['_fields'] ) ); | |
| 206 | 227 | |
| 207 | 228 | // Return value |
| 208 | 229 | $res['custom_metas'] = array( |
| 209 | 230 | 'fields' => array_map( |
| @@ -216,348 +237,21 @@ | ||
| 216 | 237 | $filtered_custom_metas |
| 217 | 238 | ), |
| 218 | 239 | ); |
| 219 | 240 | |
| 220 | - $res['acf'] = array( | |
| 221 | - 'fields' => $acf_field_keys['fields'], | |
| 222 | - ); | |
| 241 | + foreach ( $sources as $key => $source ) { | |
| 242 | + if ( 'custom_metas' === $key || ! is_array( $source ) ) { | |
| 243 | + continue; | |
| 244 | + } | |
| 245 | + $res[ $key ] = array( | |
| 246 | + 'fields' => isset( $source['fields'] ) && is_array( $source['fields'] ) ? $source['fields'] : array(), | |
| 247 | + ); | |
| 248 | + } | |
| 223 | 249 | |
| 224 | - $res['mb'] = array( | |
| 225 | - 'fields' => $mb_field_keys['fields'], | |
| 226 | - ); | |
| 227 | - | |
| 228 | - $res['pods'] = array( | |
| 229 | - 'fields' => $pods_field_keys['fields'], | |
| 230 | - ); | |
| 231 | - | |
| 232 | 250 | return rest_ensure_response( |
| 233 | 251 | array( |
| 234 | 252 | 'data' => $res, |
| 235 | 253 | ) |
| 236 | - ); | |
| 237 | - } | |
| 238 | - | |
| 239 | - /** | |
| 240 | - * Get ACF fields | |
| 241 | - * | |
| 242 | - * @param int $post_id | |
| 243 | - * @param string $field_type | |
| 244 | - * @param string $post_type | |
| 245 | - * | |
| 246 | - * @return array | |
| 247 | - * @since 4.1.1 | |
| 248 | - */ | |
| 249 | - public function get_acf_fields( $post_id, $field_type, $post_type ) { | |
| 250 | - $res = array( | |
| 251 | - 'fields' => array(), | |
| 252 | - '_fields' => array(), | |
| 253 | - 'prefixes' => array(), | |
| 254 | - ); | |
| 255 | - | |
| 256 | - if ( ! class_exists( 'ACF' ) ) { | |
| 257 | - return $res; | |
| 258 | - } | |
| 259 | - | |
| 260 | - if ( empty( $post_id ) && empty( $post_type ) ) { | |
| 261 | - global $post; | |
| 262 | - $post_id = isset( $post->ID ) ? $post->ID : ''; | |
| 263 | - } | |
| 264 | - | |
| 265 | - $allowed_types = $this->get_allowed_acf_field_types( $field_type ); | |
| 266 | - | |
| 267 | - // If post_id set set, return ACF fields for that post | |
| 268 | - if ( ! empty( $post_id ) ) { | |
| 269 | - $fields = get_field_objects( $post_id ); | |
| 270 | - if ( is_array( $fields ) ) { | |
| 271 | - foreach ( $fields as $field ) { | |
| 272 | - if ( in_array( $field['type'], $allowed_types, true ) ) { | |
| 273 | - $res['fields'][] = array( | |
| 274 | - 'value' => $field['name'], | |
| 275 | - 'label' => $field['label'], | |
| 276 | - ); | |
| 277 | - } | |
| 278 | - | |
| 279 | - $res['_fields'][] = $field['name']; | |
| 280 | - | |
| 281 | - // For filtering out pesky repeater field's sub fields | |
| 282 | - if ( $field['type'] === 'repeater' ) { | |
| 283 | - $res['prefixes'][] = $field['name']; | |
| 284 | - } | |
| 285 | - } | |
| 286 | - } | |
| 287 | - } elseif ( ! empty( $post_type ) ) { | |
| 288 | - $groups = acf_get_field_groups( array( 'post_type' => $post_type ) ); | |
| 289 | - foreach ( $groups as $group ) { | |
| 290 | - $fields = acf_get_fields( $group['ID'] ); | |
| 291 | - if ( is_array( $fields ) ) { | |
| 292 | - foreach ( $fields as $field ) { | |
| 293 | - if ( in_array( $field['type'], $allowed_types, true ) ) { | |
| 294 | - $res['fields'][] = array( | |
| 295 | - 'value' => $field['name'], | |
| 296 | - 'label' => $field['label'] . ' [' . $group['title'] . ']', | |
| 297 | - ); | |
| 298 | - } | |
| 299 | - $res['_fields'][] = $field['name']; | |
| 300 | - | |
| 301 | - if ( $field['type'] === 'repeater' ) { | |
| 302 | - $res['prefixes'][] = $field['name']; | |
| 303 | - } | |
| 304 | - } | |
| 305 | - } | |
| 306 | - } | |
| 307 | - } | |
| 308 | - | |
| 309 | - return $res; | |
| 310 | - } | |
| 311 | - | |
| 312 | - /** | |
| 313 | - * Get Meta Box fields | |
| 314 | - * | |
| 315 | - * @param int $post_id | |
| 316 | - * @param string $field_type | |
| 317 | - * @param string $post_type | |
| 318 | - * | |
| 319 | - * @return array | |
| 320 | - * @since 4.1.1 | |
| 321 | - */ | |
| 322 | - public function get_mb_fields( $post_id, $field_type, $post_type ) { | |
| 323 | - $res = array( | |
| 324 | - 'fields' => array(), | |
| 325 | - '_fields' => array(), | |
| 326 | - 'prefixes' => array(), | |
| 327 | - ); | |
| 328 | - | |
| 329 | - if ( ! function_exists( 'rwmb_get_field_settings' ) ) { | |
| 330 | - return $res; | |
| 331 | - } | |
| 332 | - | |
| 333 | - $value = null; | |
| 334 | - | |
| 335 | - if ( empty( $post_id ) && empty( $post_type ) ) { | |
| 336 | - global $post; | |
| 337 | - $value = isset( $post->ID ) ? $post->ID : ''; | |
| 338 | - } elseif ( ! empty( $post_id ) ) { | |
| 339 | - $value = $post_id; | |
| 340 | - } elseif ( ! empty( $post_type ) ) { | |
| 341 | - $value = $post_type; | |
| 342 | - } | |
| 343 | - | |
| 344 | - if ( empty( $value ) ) { | |
| 345 | - return $res; | |
| 346 | - } | |
| 347 | - | |
| 348 | - $allowed_types = self::get_allowed_mb_field_type( $field_type ); | |
| 349 | - | |
| 350 | - $fields = rwmb_get_object_fields( $value ); | |
| 351 | - | |
| 352 | - if ( is_array( $fields ) ) { | |
| 353 | - foreach ( $fields as $field ) { | |
| 354 | - if ( in_array( $field['type'], $allowed_types, true ) ) { | |
| 355 | - $res['fields'][] = array( | |
| 356 | - 'value' => $field['id'], | |
| 357 | - 'label' => $field['name'], | |
| 358 | - ); | |
| 359 | - } | |
| 360 | - | |
| 361 | - $res['_fields'][] = $field['id']; | |
| 362 | - } | |
| 363 | - } | |
| 364 | - | |
| 365 | - return $res; | |
| 366 | - } | |
| 367 | - | |
| 368 | - /** | |
| 369 | - * Get Pods fields | |
| 370 | - * | |
| 371 | - * @param int $post_id | |
| 372 | - * @param string $field_type | |
| 373 | - * @param string $post_type | |
| 374 | - * | |
| 375 | - * @return array | |
| 376 | - * @since 4.1.1 | |
| 377 | - */ | |
| 378 | - public function get_pods_fields( $post_id, $field_type, $post_type ) { | |
| 379 | - $res = array( | |
| 380 | - 'fields' => array(), | |
| 381 | - '_fields' => array(), | |
| 382 | - 'prefixes' => array(), | |
| 383 | - ); | |
| 384 | - | |
| 385 | - if ( ! function_exists( 'pods' ) ) { | |
| 386 | - return $res; | |
| 387 | - } | |
| 388 | - | |
| 389 | - if ( empty( $post_id ) ) { | |
| 390 | - global $post; | |
| 391 | - $post_id = isset( $post->ID ) ? $post->ID : ''; | |
| 392 | - } | |
| 393 | - | |
| 394 | - if ( empty( $post_type ) ) { | |
| 395 | - $post_type = get_post_type( $post_id ); | |
| 396 | - } | |
| 397 | - | |
| 398 | - if ( empty( $post_type ) ) { | |
| 399 | - return $res; | |
| 400 | - } | |
| 401 | - | |
| 402 | - $allowed_types = self::get_allowed_pods_field_types( $field_type ); | |
| 403 | - | |
| 404 | - $pods = pods( $post_type, $post_id ); | |
| 405 | - if ( is_object( $pods ) && method_exists( $pods, 'exists' ) && $pods->exists() && method_exists( $pods, 'fields' ) ) { | |
| 406 | - $fields = $pods->fields(); | |
| 407 | - if ( is_array( $fields ) ) { | |
| 408 | - foreach ( $fields as $field ) { | |
| 409 | - if ( in_array( $field['type'], $allowed_types, true ) && | |
| 410 | - 'field' === $field['object_type'] && | |
| 411 | - 'post_type' === $field['object_storage_type'] | |
| 412 | - ) { | |
| 413 | - $res['fields'][] = array( | |
| 414 | - 'value' => $field['name'], | |
| 415 | - 'label' => $field['label'], | |
| 416 | - ); | |
| 417 | - } | |
| 418 | - | |
| 419 | - $res['_fields'][] = $field['name']; | |
| 420 | - } | |
| 421 | - } | |
| 422 | - } | |
| 423 | - | |
| 424 | - return $res; | |
| 425 | - } | |
| 426 | - | |
| 427 | - /** | |
| 428 | - * Get ACF allowed field types | |
| 429 | - * | |
| 430 | - * @param string $type optional, possible values 'text'|'image'|'url'. | |
| 431 | - * | |
| 432 | - * @return array | |
| 433 | - * @since 4.1.1 | |
| 434 | - */ | |
| 435 | - public static function get_allowed_acf_field_types( $type = 'text' ) { | |
| 436 | - | |
| 437 | - if ( 'image' === $type ) { | |
| 438 | - return array( | |
| 439 | - 'image', | |
| 440 | - ); | |
| 441 | - } elseif ( 'url' === $type ) { | |
| 442 | - return array( | |
| 443 | - 'text', | |
| 444 | - 'email', | |
| 445 | - 'image', | |
| 446 | - 'file', | |
| 447 | - 'page_link', | |
| 448 | - 'url', | |
| 449 | - 'link', | |
| 450 | - ); | |
| 451 | - } | |
| 452 | - | |
| 453 | - return array( | |
| 454 | - 'text', | |
| 455 | - 'textarea', | |
| 456 | - 'number', | |
| 457 | - 'range', | |
| 458 | - 'email', | |
| 459 | - 'url', | |
| 460 | - 'password', | |
| 461 | - 'wysiwyg', | |
| 462 | - 'select', | |
| 463 | - 'checkbox', | |
| 464 | - 'radio', | |
| 465 | - 'true_false', | |
| 466 | - 'date_picker', | |
| 467 | - 'time_picker', | |
| 468 | - 'date_time_picker', | |
| 469 | - 'color_picker', | |
| 470 | - ); | |
| 471 | - } | |
| 472 | - | |
| 473 | - /** | |
| 474 | - * Get Meta Box allowed field types | |
| 475 | - * | |
| 476 | - * @param string $type optional, possible values 'text'|'image'|'url'. | |
| 477 | - * | |
| 478 | - * @return array | |
| 479 | - * @since 4.1.1 | |
| 480 | - */ | |
| 481 | - public static function get_allowed_mb_field_type( $type ) { | |
| 482 | - | |
| 483 | - if ( $type === 'image' ) { | |
| 484 | - return array( | |
| 485 | - 'image', | |
| 486 | - 'image_advanced', | |
| 487 | - 'image_upload', | |
| 488 | - 'single_image', | |
| 489 | - 'url', | |
| 490 | - 'file', | |
| 491 | - 'file_advanced', | |
| 492 | - 'file_input', | |
| 493 | - 'file_upload', | |
| 494 | - ); | |
| 495 | - } elseif ( $type === 'url' ) { | |
| 496 | - return array( | |
| 497 | - 'url', | |
| 498 | - 'file', | |
| 499 | - 'file_advanced', | |
| 500 | - 'file_input', | |
| 501 | - 'file_upload', | |
| 502 | - ); | |
| 503 | - } | |
| 504 | - | |
| 505 | - return array( | |
| 506 | - 'text', | |
| 507 | - 'email', | |
| 508 | - 'number', | |
| 509 | - 'textaraa', | |
| 510 | - 'select', | |
| 511 | - 'radio', | |
| 512 | - 'checkbox', | |
| 513 | - 'checkbox_list', | |
| 514 | - ); | |
| 515 | - } | |
| 516 | - | |
| 517 | - /** | |
| 518 | - * Get Pods allowed field types | |
| 519 | - * | |
| 520 | - * @param string $type optional, possible values 'text'|'image'|'url'. | |
| 521 | - * | |
| 522 | - * @return array | |
| 523 | - * @since 4.1.1 | |
| 524 | - */ | |
| 525 | - public static function get_allowed_pods_field_types( $type = 'text' ) { | |
| 526 | - | |
| 527 | - if ( 'image' === $type ) { | |
| 528 | - return array( | |
| 529 | - 'images', | |
| 530 | - 'file', | |
| 531 | - ); | |
| 532 | - } elseif ( 'url' === $type ) { | |
| 533 | - return array( | |
| 534 | - 'text', | |
| 535 | - 'email', | |
| 536 | - 'images', | |
| 537 | - 'file', | |
| 538 | - 'link', | |
| 539 | - 'website', | |
| 540 | - ); | |
| 541 | - } | |
| 542 | - | |
| 543 | - return array( | |
| 544 | - 'text', | |
| 545 | - 'paragraph', | |
| 546 | - 'password', | |
| 547 | - 'phone', | |
| 548 | - 'time', | |
| 549 | - 'website', | |
| 550 | - 'number', | |
| 551 | - 'wysiwyg', | |
| 552 | - 'email', | |
| 553 | - 'link', | |
| 554 | - 'boolean', | |
| 555 | - 'code', | |
| 556 | - 'number', | |
| 557 | - 'currency', | |
| 558 | - 'date', | |
| 559 | - 'datetime', | |
| 560 | 254 | ); |
| 561 | 255 | } |
| 562 | 256 | |
| 563 | 257 | /** |