Revisions.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Pods\WP; |
| 4 | |
| 5 | use Pods\Whatsit\Pod; |
| 6 | use PodsForm; |
| 7 | |
| 8 | /** |
| 9 | * Revisions specific functionality. |
| 10 | * |
| 11 | * @since 3.2.0 |
| 12 | */ |
| 13 | class Revisions { |
| 14 | |
| 15 | /** |
| 16 | * Add the class hooks. |
| 17 | * |
| 18 | * @since 3.2.0 |
| 19 | */ |
| 20 | public function hook() { |
| 21 | add_filter( 'wp_post_revision_meta_keys', [ $this, 'wp_post_revision_meta_keys' ], 10, 2 ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Remove the class hooks. |
| 26 | * |
| 27 | * @since 3.2.0 |
| 28 | */ |
| 29 | public function unhook() { |
| 30 | remove_filter( 'wp_post_revision_meta_keys', [ $this, 'wp_post_revision_meta_keys' ] ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Filter the available post revision meta keys to include the ones registered by Pods. |
| 35 | * |
| 36 | * @since 3.2.0 |
| 37 | * |
| 38 | * @param array $revisioned_keys The list of revisioned meta keys. |
| 39 | * @param string $post_type The post type. |
| 40 | * |
| 41 | * @return array The list of revisioned meta keys. |
| 42 | */ |
| 43 | public function wp_post_revision_meta_keys( $revisioned_keys, $post_type ): array { |
| 44 | $meta = pods_container( Meta::class ); |
| 45 | |
| 46 | // Determine if we need to revision keys manually or if meta is already being registered. |
| 47 | if ( $meta->should_register_meta() ) { |
| 48 | return $revisioned_keys; |
| 49 | } |
| 50 | |
| 51 | $api = pods_api(); |
| 52 | |
| 53 | $pod = $api->load_pod( [ 'name' => $post_type ] ); |
| 54 | |
| 55 | if ( ! $pod instanceof Pod || 'post_type' !== $pod->get_type() || 'meta' !== $pod->get_storage() ) { |
| 56 | return $revisioned_keys; |
| 57 | } |
| 58 | |
| 59 | $revisionable_field_types = self::get_revisionable_field_types(); |
| 60 | |
| 61 | // Get the fields that are enabled for revisioning. |
| 62 | if ( 1 === (int) $pod->get_arg( 'revisions_revision_all_fields', 0 ) ) { |
| 63 | $revisionable_fields = $pod->get_fields( [ |
| 64 | 'type' => $revisionable_field_types, |
| 65 | 'names' => true, |
| 66 | ] ); |
| 67 | } else { |
| 68 | $revisionable_fields = $pod->get_fields( [ |
| 69 | 'type' => $revisionable_field_types, |
| 70 | 'args' => [ |
| 71 | 'revisions_revision_field' => 1, |
| 72 | ], |
| 73 | 'names' => true, |
| 74 | ] ); |
| 75 | } |
| 76 | |
| 77 | $revisioned_keys = array_merge( $revisioned_keys, array_values( $revisionable_fields ) ); |
| 78 | $revisioned_keys = array_unique( array_values( $revisioned_keys ) ); |
| 79 | |
| 80 | return $revisioned_keys; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get the list of revisionable field types. |
| 85 | * |
| 86 | * @since 3.2.0 |
| 87 | * |
| 88 | * @return array The list of revisionable field types. |
| 89 | */ |
| 90 | public static function get_revisionable_field_types(): array { |
| 91 | $field_types = PodsForm::field_types_list(); |
| 92 | $tableless_field_types = PodsForm::tableless_field_types(); |
| 93 | $layout_field_types = PodsForm::layout_field_types(); |
| 94 | |
| 95 | $revisionable_field_types = []; |
| 96 | |
| 97 | foreach ( $field_types as $field_type ) { |
| 98 | if ( |
| 99 | in_array( $field_type, $tableless_field_types, true ) |
| 100 | || in_array( $field_type, $layout_field_types, true ) |
| 101 | ) { |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | $revisionable_field_types[] = $field_type; |
| 106 | } |
| 107 | |
| 108 | return $revisionable_field_types; |
| 109 | } |
| 110 | |
| 111 | } |
| 112 |