class-acf-internal-post-type.php
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly. |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'ACF_Internal_Post_Type' ) ) { |
| 8 | abstract class ACF_Internal_Post_Type { |
| 9 | |
| 10 | /** |
| 11 | * The ACF internal post type name. |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | public $post_type = ''; |
| 16 | |
| 17 | /** |
| 18 | * The prefix for the key used in the main post array. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | public $post_key_prefix = ''; |
| 23 | |
| 24 | /** |
| 25 | * The cache key for a singular post. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | public $cache_key = ''; |
| 30 | |
| 31 | /** |
| 32 | * The cache key for a collection of posts. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | public $cache_key_plural = ''; |
| 37 | |
| 38 | /** |
| 39 | * The hook name for a singular post. |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | public $hook_name = ''; |
| 44 | |
| 45 | /** |
| 46 | * The hook name for a collection of posts. |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | public $hook_name_plural = ''; |
| 51 | |
| 52 | /** |
| 53 | * The name of the store used for the post type. |
| 54 | * |
| 55 | * @var string |
| 56 | */ |
| 57 | public $store = ''; |
| 58 | |
| 59 | /** |
| 60 | * Constructs the class. |
| 61 | */ |
| 62 | public function __construct() { |
| 63 | acf_register_store( $this->store )->prop( 'multisite', true ); |
| 64 | |
| 65 | $internal_post_types_store = acf_get_store( 'internal-post-types' ); |
| 66 | |
| 67 | if ( ! $internal_post_types_store ) { |
| 68 | $internal_post_types_store = acf_register_store( 'internal-post-types' ); |
| 69 | } |
| 70 | |
| 71 | $internal_post_types_store->set( $this->post_type, get_class( $this ) ); |
| 72 | |
| 73 | $this->register_post_type(); |
| 74 | |
| 75 | add_action( "acf/validate_{$this->hook_name}", array( $this, 'translate_post' ) ); |
| 76 | |
| 77 | add_filter( 'wp_unique_post_slug', array( $this, 'apply_unique_post_slug' ), 999, 6 ); |
| 78 | add_action( 'wp_untrash_post_status', array( $this, 'untrash_post_status' ), 10, 3 ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Register post type to WordPress if required for this post type |
| 83 | * |
| 84 | * @since 6.1 |
| 85 | */ |
| 86 | public function register_post_type() {} |
| 87 | |
| 88 | /** |
| 89 | * Get an ACF CPT object as an array. |
| 90 | * |
| 91 | * @since 6.1 |
| 92 | * |
| 93 | * @param int|WP_Post $id The post ID being queried. |
| 94 | * @return array|bool The main ACF array for the post, or false on failure. |
| 95 | */ |
| 96 | public function get_post( $id = 0 ) { |
| 97 | // Allow WP_Post to be passed. |
| 98 | if ( is_object( $id ) ) { |
| 99 | $id = $id->ID; |
| 100 | } |
| 101 | |
| 102 | // Check store. |
| 103 | $store = acf_get_store( $this->store ); |
| 104 | if ( $store->has( $id ) ) { |
| 105 | return $store->get( $id ); |
| 106 | } |
| 107 | |
| 108 | if ( acf_is_local_internal_post_type( $id, $this->post_type ) ) { |
| 109 | $post = acf_get_local_internal_post_type( $id, $this->post_type ); |
| 110 | } else { |
| 111 | $post = $this->get_raw_post( $id ); |
| 112 | } |
| 113 | |
| 114 | // Bail early if no post. |
| 115 | if ( ! $post ) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | $post = $this->validate_post( $post ); |
| 120 | |
| 121 | /** |
| 122 | * Filters the post array after it has been loaded. |
| 123 | * |
| 124 | * @date 12/02/2014 |
| 125 | * @since 5.0.0 |
| 126 | * |
| 127 | * @param array $post The post array. |
| 128 | */ |
| 129 | $post = apply_filters( "acf/load_{$this->hook_name}", $post ); |
| 130 | |
| 131 | // Store field group using aliases to also find via key, ID and name. |
| 132 | $store->set( $post['key'], $post ); |
| 133 | $store->alias( $post['key'], $post['ID'] ); |
| 134 | |
| 135 | return $post; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Retrieves raw post data for the given identifier. |
| 140 | * |
| 141 | * @since 6.1 |
| 142 | * |
| 143 | * @param int|string $id The field ID, key or name. |
| 144 | * @return array|false The field group array, or false on failure. |
| 145 | */ |
| 146 | public function get_raw_post( $id = 0 ) { |
| 147 | // Get raw internal post from database. |
| 148 | $post = $this->get_post_object( $id ); |
| 149 | if ( ! $post ) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | // Bail early if incorrect post type. |
| 154 | if ( $post->post_type !== $this->post_type ) { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | // Unserialize post_content. |
| 159 | $raw_post = (array) acf_maybe_unserialize( $post->post_content ); |
| 160 | |
| 161 | // Update attributes. |
| 162 | $raw_post['ID'] = $post->ID; |
| 163 | $raw_post['title'] = $post->post_title; |
| 164 | $raw_post['key'] = $post->post_name; |
| 165 | $raw_post['menu_order'] = $post->menu_order; |
| 166 | $raw_post['active'] = in_array( $post->post_status, array( 'publish', 'auto-draft' ) ); |
| 167 | |
| 168 | return $raw_post; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Retrieves the WP_Post object for an ACF internal CPT. |
| 173 | * |
| 174 | * @since 6.1 |
| 175 | * |
| 176 | * @param int|string $id The post ID, key, or name. |
| 177 | * @return WP_Post|bool The post object, or false on failure. |
| 178 | */ |
| 179 | public function get_post_object( $id = 0 ) { |
| 180 | if ( is_numeric( $id ) ) { |
| 181 | $post_object = get_post( $id ); |
| 182 | |
| 183 | if ( $post_object && $post_object->post_type === $this->post_type ) { |
| 184 | return $post_object; |
| 185 | } |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | if ( is_string( $id ) ) { |
| 191 | // Try cache. |
| 192 | $cache_key = $this->cache_key . $id; |
| 193 | $post_id = wp_cache_get( $cache_key, 'acf' ); |
| 194 | |
| 195 | if ( $post_id === false ) { |
| 196 | $query_key = 'acf_' . $this->post_key_prefix . 'key'; |
| 197 | |
| 198 | // Query posts. |
| 199 | $posts = get_posts( |
| 200 | array( |
| 201 | 'posts_per_page' => 1, |
| 202 | 'post_type' => $this->post_type, |
| 203 | 'post_status' => array( 'publish', 'acf-disabled', 'trash' ), |
| 204 | 'orderby' => 'menu_order title', |
| 205 | 'order' => 'ASC', |
| 206 | 'suppress_filters' => false, |
| 207 | 'cache_results' => true, |
| 208 | 'update_post_meta_cache' => false, |
| 209 | 'update_post_term_cache' => false, |
| 210 | $query_key => $id, // Used to check post_name for field group/post type/taxonomy key. |
| 211 | ) |
| 212 | ); |
| 213 | |
| 214 | // Update $post_id with a non-false value. |
| 215 | $post_id = $posts ? $posts[0]->ID : 0; |
| 216 | |
| 217 | // Update cache. |
| 218 | wp_cache_set( $cache_key, $post_id, 'acf' ); |
| 219 | } |
| 220 | |
| 221 | // Check $post_id and return the post when possible. |
| 222 | if ( $post_id ) { |
| 223 | return get_post( $post_id ); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Returns true if the given identifier is an ACF post key. |
| 232 | * |
| 233 | * @since 6.1 |
| 234 | * |
| 235 | * @param string $id The identifier. |
| 236 | * @return bool |
| 237 | */ |
| 238 | public function is_post_key( $id = '' ) { |
| 239 | // Check if $id is a string starting with $this->post_key. |
| 240 | if ( is_string( $id ) && substr( $id, 0, strlen( $this->post_key_prefix ) ) === $this->post_key_prefix ) { |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Filters whether the $id is an ACF post key. |
| 246 | * |
| 247 | * @date 23/1/19 |
| 248 | * @since 5.7.10 |
| 249 | * |
| 250 | * @param bool $bool The result. |
| 251 | * @param string $id The identifier. |
| 252 | */ |
| 253 | return apply_filters( "acf/is_{$this->hook_name}_key", false, $id, $this->post_type ); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Validates an ACF internal post type. |
| 258 | * |
| 259 | * @since 6.1 |
| 260 | * |
| 261 | * @param array $post The main post array. |
| 262 | * @return array |
| 263 | */ |
| 264 | public function validate_post( $post = array() ) { |
| 265 | // Bail early if already valid. |
| 266 | if ( is_array( $post ) && ! empty( $post['_valid'] ) ) { |
| 267 | return $post; |
| 268 | } |
| 269 | |
| 270 | $post = wp_parse_args( |
| 271 | $post, |
| 272 | $this->get_settings_array() |
| 273 | ); |
| 274 | |
| 275 | // Convert types. |
| 276 | $post['ID'] = (int) $post['ID']; |
| 277 | $post['menu_order'] = (int) $post['menu_order']; |
| 278 | $post['active'] = (bool) $post['active']; |
| 279 | |
| 280 | // Post is now valid. |
| 281 | $post['_valid'] = true; |
| 282 | |
| 283 | /** |
| 284 | * Filters the ACF post array to validate settings. |
| 285 | * |
| 286 | * @date 12/02/2014 |
| 287 | * @since 5.0.0 |
| 288 | * |
| 289 | * @param array $post The post array. |
| 290 | */ |
| 291 | return apply_filters( "acf/validate_{$this->hook_name}", $post ); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Validates post type values before allowing save from the global $_POST object. |
| 296 | * Errors are added to the form using acf_add_internal_post_type_validation_error(). |
| 297 | * |
| 298 | * @since 6.1 |
| 299 | * |
| 300 | * @return bool |
| 301 | */ |
| 302 | public function ajax_validate_values() {} |
| 303 | |
| 304 | /** |
| 305 | * Ensures the given ACF post is valid. |
| 306 | * |
| 307 | * @since 6.1 |
| 308 | * |
| 309 | * @param array $post The main post array. |
| 310 | * @return array |
| 311 | */ |
| 312 | public function get_valid_post( $post = false ) { |
| 313 | return $this->validate_post( $post ); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Gets the default settings array for an ACF post type. |
| 318 | * |
| 319 | * @return array |
| 320 | */ |
| 321 | public function get_settings_array() { |
| 322 | return array(); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Translates an ACF post. |
| 327 | * |
| 328 | * @since 6.1 |
| 329 | * |
| 330 | * @param array $post The field group array. |
| 331 | * @return array |
| 332 | */ |
| 333 | public function translate_post( $post = array() ) { |
| 334 | // Get settings. |
| 335 | $l10n = acf_get_setting( 'l10n' ); |
| 336 | $l10n_textdomain = acf_get_setting( 'l10n_textdomain' ); |
| 337 | |
| 338 | // Translate field settings if textdomain is set. |
| 339 | if ( $l10n && $l10n_textdomain ) { |
| 340 | $post['title'] = acf_translate( $post['title'] ); |
| 341 | |
| 342 | /** |
| 343 | * Filters the post array to translate strings. |
| 344 | * |
| 345 | * @date 12/02/2014 |
| 346 | * @since 5.0.0 |
| 347 | * |
| 348 | * @param array $post The post array. |
| 349 | */ |
| 350 | $post = apply_filters( "acf/translate_{$this->hook_name}", $post ); |
| 351 | } |
| 352 | |
| 353 | return $post; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Returns an array of ACF posts for the given $filter. |
| 358 | * |
| 359 | * @since 6.1 |
| 360 | * |
| 361 | * @param array $filter An array of args to filter by. |
| 362 | * @return array |
| 363 | */ |
| 364 | public function get_posts( $filter = array() ) { |
| 365 | $posts = array(); |
| 366 | |
| 367 | // Check database. |
| 368 | $raw_posts = $this->get_raw_posts(); |
| 369 | if ( $raw_posts ) { |
| 370 | foreach ( $raw_posts as $raw_post ) { |
| 371 | $posts[] = $this->get_post( $raw_post['ID'] ); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Filters the posts array. |
| 377 | * |
| 378 | * @date 12/02/2014 |
| 379 | * @since 5.0.0 |
| 380 | * |
| 381 | * @param array $posts The array of ACF posts. |
| 382 | */ |
| 383 | $posts = apply_filters( "acf/load_{$this->hook_name_plural}", $posts, $this->post_type ); |
| 384 | |
| 385 | // Filter results. |
| 386 | if ( $filter ) { |
| 387 | return $this->filter_posts( $posts, $filter ); |
| 388 | } |
| 389 | |
| 390 | return $posts; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Returns an array of raw ACF post data. |
| 395 | * |
| 396 | * @since 6.1 |
| 397 | * |
| 398 | * @return array |
| 399 | */ |
| 400 | public function get_raw_posts() { |
| 401 | // Try cache. |
| 402 | $cache_key = acf_cache_key( $this->cache_key_plural ); |
| 403 | $post_ids = wp_cache_get( $cache_key, 'acf' ); // TODO: Do we need to change the group at all? |
| 404 | |
| 405 | if ( $post_ids === false ) { |
| 406 | |
| 407 | // Query posts. |
| 408 | $posts = get_posts( |
| 409 | array( |
| 410 | 'posts_per_page' => -1, |
| 411 | 'post_type' => $this->post_type, |
| 412 | 'orderby' => 'menu_order title', |
| 413 | 'order' => 'ASC', |
| 414 | 'suppress_filters' => false, // Allow WPML to modify the query. |
| 415 | 'cache_results' => true, |
| 416 | 'update_post_meta_cache' => false, |
| 417 | 'update_post_term_cache' => false, |
| 418 | 'post_status' => array( 'publish', 'acf-disabled' ), |
| 419 | ) |
| 420 | ); |
| 421 | |
| 422 | // Update $post_ids with a non-false value. |
| 423 | $post_ids = array(); |
| 424 | foreach ( $posts as $post ) { |
| 425 | $post_ids[] = $post->ID; |
| 426 | } |
| 427 | |
| 428 | // Update cache. |
| 429 | wp_cache_set( $cache_key, $post_ids, 'acf' ); |
| 430 | } |
| 431 | |
| 432 | // Loop over ids and populate array of ACF posts. |
| 433 | $return = array(); |
| 434 | foreach ( $post_ids as $post_id ) { |
| 435 | $return[] = $this->get_raw_post( $post_id ); |
| 436 | } |
| 437 | |
| 438 | return $return; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Filter the posts returned by $this->get_posts(). |
| 443 | * |
| 444 | * @since 6.1 |
| 445 | * |
| 446 | * @param array $posts An array of posts to filter. |
| 447 | * @param array $args An array of args to filter by. |
| 448 | * @return array |
| 449 | */ |
| 450 | public function filter_posts( $posts, $args = array() ) { |
| 451 | if ( ! empty( $args['active'] ) ) { |
| 452 | $posts = array_filter( |
| 453 | $posts, |
| 454 | function( $post ) { |
| 455 | return $post['active']; |
| 456 | } |
| 457 | ); |
| 458 | } |
| 459 | |
| 460 | return $posts; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Updates an ACF post. |
| 465 | * |
| 466 | * @since 6.1 |
| 467 | * |
| 468 | * @param array $post The ACF post to update. |
| 469 | * @return array |
| 470 | */ |
| 471 | public function update_post( $post ) { |
| 472 | // Validate internal post type. |
| 473 | $post = $this->validate_post( $post ); |
| 474 | |
| 475 | // May have been posted. Remove slashes. |
| 476 | $post = wp_unslash( $post ); |
| 477 | |
| 478 | // Parse types (converts string '0' to int 0). |
| 479 | $post = acf_parse_types( $post ); |
| 480 | |
| 481 | /** |
| 482 | * Fires before updating an ACF post in the database. |
| 483 | * |
| 484 | * @since 6.1 |
| 485 | * |
| 486 | * @param array $post The main ACF post array. |
| 487 | */ |
| 488 | $post = apply_filters( "acf/pre_update_{$this->hook_name}", $post ); |
| 489 | |
| 490 | // Make a backup of internal post type data and remove some args. |
| 491 | $_post = $post; |
| 492 | acf_extract_vars( $_post, array( 'ID', 'key', 'title', 'menu_order', 'fields', 'active', '_valid' ) ); |
| 493 | |
| 494 | // Create array of data to save. |
| 495 | $save = array( |
| 496 | 'ID' => $post['ID'], |
| 497 | 'post_status' => $post['active'] ? 'publish' : 'acf-disabled', |
| 498 | 'post_type' => $this->post_type, |
| 499 | 'post_title' => $post['title'], |
| 500 | 'post_name' => $post['key'], |
| 501 | 'post_excerpt' => sanitize_title( $post['title'] ), |
| 502 | 'post_content' => maybe_serialize( $_post ), |
| 503 | 'menu_order' => $post['menu_order'], |
| 504 | 'comment_status' => 'closed', |
| 505 | 'ping_status' => 'closed', |
| 506 | ); |
| 507 | |
| 508 | // Unhook wp_targeted_link_rel() filter from WP 5.1 corrupting serialized data. |
| 509 | remove_filter( 'content_save_pre', 'wp_targeted_link_rel' ); |
| 510 | |
| 511 | // Slash data. |
| 512 | // WP expects all data to be slashed and will unslash it (fixes '\' character issues). |
| 513 | $save = wp_slash( $save ); |
| 514 | |
| 515 | // Update or Insert. |
| 516 | if ( $post['ID'] ) { |
| 517 | wp_update_post( $save ); |
| 518 | } else { |
| 519 | $post['ID'] = wp_insert_post( $save ); |
| 520 | } |
| 521 | |
| 522 | $this->flush_post_cache( $post ); |
| 523 | |
| 524 | /** |
| 525 | * Fires immediately after an ACF post has been updated. |
| 526 | * |
| 527 | * @since 6.1 |
| 528 | * |
| 529 | * @param array $post The main ACF post array. |
| 530 | */ |
| 531 | do_action( "acf/update_{$this->hook_name}", $post ); |
| 532 | |
| 533 | return $post; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Allows full control over ACF post slugs. |
| 538 | * |
| 539 | * @since 6.1 |
| 540 | * |
| 541 | * @param string $slug The post slug. |
| 542 | * @param int $post_ID Post ID. |
| 543 | * @param string $post_status The post status. |
| 544 | * @param string $post_type Post type. |
| 545 | * @param int $post_parent Post parent ID. |
| 546 | * @param string $original_slug The original post slug. |
| 547 | * @return string |
| 548 | */ |
| 549 | public function apply_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) { |
| 550 | // Check post type and reset to original value. |
| 551 | if ( $post_type === $this->post_type ) { |
| 552 | return $original_slug; |
| 553 | } |
| 554 | |
| 555 | return $slug; |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * Deletes all caches for this ACF post. |
| 560 | * |
| 561 | * @since 6.1 |
| 562 | * |
| 563 | * @param array $post The ACF post array. |
| 564 | * @return void |
| 565 | */ |
| 566 | public function flush_post_cache( $post ) { |
| 567 | // Delete stored data. |
| 568 | acf_get_store( $this->store )->remove( $post['key'] ); |
| 569 | |
| 570 | // Flush cached post_id for this field group's key. |
| 571 | wp_cache_delete( acf_cache_key( $this->cache_key . $post['key'] ), 'acf' ); |
| 572 | |
| 573 | // Flush cached array of post_ids for collection of field groups. |
| 574 | wp_cache_delete( acf_cache_key( $this->cache_key_plural ), 'acf' ); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Deletes an ACF post. |
| 579 | * |
| 580 | * @since 6.1 |
| 581 | * |
| 582 | * @param int|string $id The ID of the ACF post to delete. |
| 583 | * @return bool |
| 584 | */ |
| 585 | public function delete_post( $id = 0 ) { |
| 586 | // Disable filters to ensure ACF loads data from DB. |
| 587 | acf_disable_filters(); |
| 588 | |
| 589 | // Get the post. |
| 590 | $post = $this->get_post( $id ); |
| 591 | |
| 592 | // Bail early if post was not found. |
| 593 | if ( ! $post || ! $post['ID'] ) { |
| 594 | return false; |
| 595 | } |
| 596 | |
| 597 | // Delete post and flush cache. |
| 598 | wp_delete_post( $post['ID'], true ); |
| 599 | $this->flush_post_cache( $post ); |
| 600 | |
| 601 | /** |
| 602 | * Fires immediately after an ACF post has been deleted. |
| 603 | * |
| 604 | * @date 12/02/2014 |
| 605 | * @since 5.0.0 |
| 606 | * |
| 607 | * @param array $post The ACF post array. |
| 608 | */ |
| 609 | do_action( "acf/delete_{$this->hook_name}", $post ); |
| 610 | |
| 611 | return true; |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Trashes an ACF post. |
| 616 | * |
| 617 | * @since 6.1 |
| 618 | * |
| 619 | * @param int|string $id The ID of the ACF post to trash. |
| 620 | * @return bool |
| 621 | */ |
| 622 | public function trash_post( $id = 0 ) { |
| 623 | // Disable filters to ensure ACF loads data from DB. |
| 624 | acf_disable_filters(); |
| 625 | |
| 626 | $post = $this->get_post( $id ); |
| 627 | if ( ! $post || ! $post['ID'] ) { |
| 628 | return false; |
| 629 | } |
| 630 | |
| 631 | wp_trash_post( $post['ID'] ); |
| 632 | $this->flush_post_cache( $post ); |
| 633 | |
| 634 | /** |
| 635 | * Fires immediately after a field_group has been trashed. |
| 636 | * |
| 637 | * @date 12/02/2014 |
| 638 | * @since 5.0.0 |
| 639 | * |
| 640 | * @param array $post The ACF post array. |
| 641 | */ |
| 642 | do_action( "acf/trash_{$this->hook_name}", $post ); |
| 643 | |
| 644 | return true; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Restores an ACF post from the trash. |
| 649 | * |
| 650 | * @since 6.1 |
| 651 | * |
| 652 | * @param int|string $id The ID of the ACF post to untrash. |
| 653 | * @return bool |
| 654 | */ |
| 655 | public function untrash_post( $id = 0 ) { |
| 656 | // Disable filters to ensure ACF loads data from DB. |
| 657 | acf_disable_filters(); |
| 658 | |
| 659 | $post = $this->get_post( $id ); |
| 660 | if ( ! $post || ! $post['ID'] ) { |
| 661 | return false; |
| 662 | } |
| 663 | |
| 664 | wp_untrash_post( $post['ID'] ); |
| 665 | $this->flush_post_cache( $post ); |
| 666 | |
| 667 | /** |
| 668 | * Fires immediately after an ACF post has been untrashed. |
| 669 | * |
| 670 | * @date 12/02/2014 |
| 671 | * @since 5.0.0 |
| 672 | * |
| 673 | * @param array $post The ACF post array. |
| 674 | */ |
| 675 | do_action( "acf/untrash_{$this->hook_name}", $post ); |
| 676 | |
| 677 | return true; |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Returns the previous post_status instead of "draft" for the ACF internal post types. |
| 682 | * Prior to WordPress 5.6.0, this filter was not needed as restored posts were always assigned their original status. |
| 683 | * |
| 684 | * @since 6.1 |
| 685 | * |
| 686 | * @param string $new_status The new status of the post being restored. |
| 687 | * @param int $post_id The ID of the post being restored. |
| 688 | * @param string $previous_status The status of the post at the point where it was trashed. |
| 689 | * @return string |
| 690 | */ |
| 691 | public function untrash_post_status( $new_status, $post_id, $previous_status ) { |
| 692 | return ( get_post_type( $post_id ) === $this->post_type ) ? $previous_status : $new_status; |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * Returns true if the given params match an ACF post. |
| 697 | * |
| 698 | * @since 6.1 |
| 699 | * |
| 700 | * @param array $post The post array to check. |
| 701 | * @return bool |
| 702 | */ |
| 703 | public function is_post( $post = false ) { |
| 704 | return ( |
| 705 | is_array( $post ) |
| 706 | && isset( $post['key'] ) |
| 707 | && isset( $post['title'] ) |
| 708 | ); |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Duplicates an ACF post. |
| 713 | * |
| 714 | * @since 6.1 |
| 715 | * |
| 716 | * @param int|string $id The ID of the post to duplicate. |
| 717 | * @param int $new_post_id Optional post ID to override. |
| 718 | * @return array The new ACF post array. |
| 719 | */ |
| 720 | public function duplicate_post( $id = 0, $new_post_id = 0 ) { |
| 721 | // Disable filters to ensure ACF loads data from DB. |
| 722 | acf_disable_filters(); |
| 723 | |
| 724 | $post = $this->get_post( $id ); |
| 725 | if ( ! $post || ! $post['ID'] ) { |
| 726 | return false; |
| 727 | } |
| 728 | |
| 729 | // Update attributes. |
| 730 | $post['ID'] = $new_post_id; |
| 731 | $post['key'] = uniqid( 'group_' ); |
| 732 | |
| 733 | // Add (copy) to title when appropriate. |
| 734 | if ( ! $new_post_id ) { |
| 735 | $post['title'] .= ' (' . __( 'copy', 'acf' ) . ')'; |
| 736 | } |
| 737 | |
| 738 | // When importing a new field group, insert a temporary post and set the field group's ID. |
| 739 | // This allows fields to be updated before the field group (field group ID is needed for field parent setting). |
| 740 | if ( ! $post['ID'] ) { |
| 741 | $post['ID'] = wp_insert_post( array( 'post_title' => $post['key'] ) ); |
| 742 | } |
| 743 | |
| 744 | $post = $this->update_post( $post ); |
| 745 | |
| 746 | /** |
| 747 | * Fires immediately after an ACF post has been duplicated. |
| 748 | * |
| 749 | * @date 12/02/2014 |
| 750 | * @since 5.0.0 |
| 751 | * |
| 752 | * @param array $post The ACF post array. |
| 753 | */ |
| 754 | do_action( "acf/duplicate_{$this->hook_name}", $post ); |
| 755 | |
| 756 | return $post; |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * Activates or deactivates an ACF post. |
| 761 | * |
| 762 | * @since 6.1 |
| 763 | * |
| 764 | * @param int|string $id The ID of the ACF post to activate/deactivate. |
| 765 | * @param bool $activate True if the post should be activated. |
| 766 | * @return bool |
| 767 | */ |
| 768 | public function update_post_active_status( $id, $activate = true ) { |
| 769 | // Disable filters to ensure ACF loads data from DB. |
| 770 | acf_disable_filters(); |
| 771 | |
| 772 | $post = $this->get_post( $id ); |
| 773 | if ( ! $post || ! $post['ID'] ) { |
| 774 | return false; |
| 775 | } |
| 776 | |
| 777 | $post['active'] = (bool) $activate; |
| 778 | $updated_post = $this->update_post( $post ); |
| 779 | |
| 780 | /** |
| 781 | * Fires immediately after an ACF post has been made active/inactive. |
| 782 | * |
| 783 | * @since 6.0.0 |
| 784 | * |
| 785 | * @param array $updated_post The updated ACF post array. |
| 786 | */ |
| 787 | do_action( "acf/update_{$this->hook_name}_active_status", $updated_post ); |
| 788 | |
| 789 | if ( ! isset( $updated_post['active'] ) || $activate !== $updated_post['active'] ) { |
| 790 | return false; |
| 791 | } |
| 792 | |
| 793 | return true; |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * Checks if the current user can edit ACF posts and returns the edit url. |
| 798 | * |
| 799 | * @since 6.1 |
| 800 | * |
| 801 | * @param int $post_id The ACF post ID. |
| 802 | * @return string |
| 803 | */ |
| 804 | public function get_post_edit_link( $post_id ) { |
| 805 | if ( $post_id && acf_current_user_can_admin() ) { |
| 806 | return admin_url( 'post.php?post=' . $post_id . '&action=edit' ); |
| 807 | } |
| 808 | |
| 809 | return ''; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * Returns a modified ACF post array ready for export. |
| 814 | * |
| 815 | * @since 6.1 |
| 816 | * |
| 817 | * @param array $post The ACF post array. |
| 818 | * @return array |
| 819 | */ |
| 820 | public function prepare_post_for_export( $post = array() ) { |
| 821 | // Remove args. |
| 822 | acf_extract_vars( $post, array( 'ID', 'local', '_valid' ) ); |
| 823 | |
| 824 | /** |
| 825 | * Filters the ACF post array before being returned to the export tool. |
| 826 | * |
| 827 | * @date 12/02/2014 |
| 828 | * @since 5.0.0 |
| 829 | * |
| 830 | * @param array $post The ACF post array. |
| 831 | */ |
| 832 | return apply_filters( "acf/prepare_{$this->hook_name}_for_export", $post ); |
| 833 | } |
| 834 | |
| 835 | /** |
| 836 | * Returns a string containing PHP code that can be used to create the post in ACF. |
| 837 | * |
| 838 | * @since 6.1 |
| 839 | * |
| 840 | * @param array $post The post being exported. |
| 841 | * @return string |
| 842 | */ |
| 843 | public function export_post_as_php( $post = array() ) { |
| 844 | return ''; |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * Formats code used for PHP exports. |
| 849 | * |
| 850 | * @since 6.1 |
| 851 | * |
| 852 | * @param string $code The code being formatted. |
| 853 | * @return string |
| 854 | */ |
| 855 | public function format_code_for_export( $code = '' ) { |
| 856 | if ( ! is_string( $code ) ) { |
| 857 | return ''; |
| 858 | } |
| 859 | |
| 860 | $str_replace = array( |
| 861 | ' ' => "\t", |
| 862 | "'!!__(!!\'" => "__('", |
| 863 | "!!\', !!\'" => "', '", |
| 864 | "!!\')!!'" => "')", |
| 865 | 'array (' => 'array(', |
| 866 | ); |
| 867 | $preg_replace = array( |
| 868 | '/([\t\r\n]+?)array/' => 'array', |
| 869 | '/[0-9]+ => array/' => 'array', |
| 870 | ); |
| 871 | |
| 872 | $code = str_replace( array_keys( $str_replace ), array_values( $str_replace ), $code ); |
| 873 | $code = preg_replace( array_keys( $preg_replace ), array_values( $preg_replace ), $code ); |
| 874 | |
| 875 | return $code; |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * Prepares an ACF post for import. |
| 880 | * |
| 881 | * @since 6.1 |
| 882 | * |
| 883 | * @param array $post The ACF post array. |
| 884 | * @return array |
| 885 | */ |
| 886 | public function prepare_post_for_import( $post ) { |
| 887 | /** |
| 888 | * Filters the ACF post array before being returned to the import tool. |
| 889 | * |
| 890 | * @date 21/11/19 |
| 891 | * @since 5.8.8 |
| 892 | * |
| 893 | * @param array $post The ACF post array. |
| 894 | */ |
| 895 | return apply_filters( "acf/prepare_{$this->hook_name}_for_import", $post ); |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * Imports an ACF post into the database. |
| 900 | * |
| 901 | * @since 6.1 |
| 902 | * |
| 903 | * @param array $post The ACF post array. |
| 904 | * @return array |
| 905 | */ |
| 906 | public function import_post( $post ) { |
| 907 | // Disable filters to ensure data is not modified by local, clone, etc. |
| 908 | $filters = acf_disable_filters(); |
| 909 | |
| 910 | // Validate the post (ensures all settings exist). |
| 911 | $post = $this->get_valid_post( $post ); |
| 912 | |
| 913 | // Prepare post for import (modifies settings). |
| 914 | $post = $this->prepare_post_for_import( $post ); |
| 915 | |
| 916 | // Save field group. |
| 917 | $post = $this->update_post( $post ); |
| 918 | |
| 919 | // Enable filters again. |
| 920 | acf_enable_filters( $filters ); |
| 921 | |
| 922 | /** |
| 923 | * Fires immediately after an ACF post has been imported. |
| 924 | * |
| 925 | * @date 12/02/2014 |
| 926 | * @since 5.0.0 |
| 927 | * |
| 928 | * @param array $post The ACF post array. |
| 929 | */ |
| 930 | do_action( "acf/import_{$this->hook_name}", $post ); |
| 931 | |
| 932 | return $post; |
| 933 | } |
| 934 | |
| 935 | } |
| 936 | |
| 937 | } |
| 938 |