class-acf-field-group.php
1 month ago
class-acf-post-type.php
1 month ago
class-acf-taxonomy.php
1 month ago
class-acf-ui-options-page.php
1 year ago
class-scf-field-manager.php
6 months ago
index.php
1 year ago
class-scf-field-manager.php
213 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SCF Field Manager |
| 4 | * |
| 5 | * Manages field operations by wrapping ACF field functions. |
| 6 | * |
| 7 | * @package wordpress/secure-custom-fields |
| 8 | * @since 6.8.0 |
| 9 | */ |
| 10 | |
| 11 | // Exit if accessed directly. |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | if ( ! class_exists( 'SCF_Field_Manager' ) ) : |
| 17 | |
| 18 | /** |
| 19 | * SCF Field Manager class. |
| 20 | * |
| 21 | * @since 6.8.0 |
| 22 | */ |
| 23 | class SCF_Field_Manager { |
| 24 | |
| 25 | /** |
| 26 | * The post type for fields. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | public $post_type = 'acf-field'; |
| 31 | |
| 32 | /** |
| 33 | * The key prefix for fields. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public $post_key_prefix = 'field_'; |
| 38 | |
| 39 | /** |
| 40 | * Gets a field by ID or key. |
| 41 | * |
| 42 | * @since 6.8.0 |
| 43 | * |
| 44 | * @param int|string $id The field ID or key. |
| 45 | * @return array|false The field array or false if not found. |
| 46 | */ |
| 47 | public function get_post( $id = 0 ) { |
| 48 | return acf_get_field( $id ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Gets all fields across all field groups. |
| 53 | * |
| 54 | * Unlike internal post types which are top-level, fields are children |
| 55 | * of field groups. This method aggregates fields from all field groups. |
| 56 | * |
| 57 | * @since 6.8.0 |
| 58 | * |
| 59 | * @return array Array of all fields. |
| 60 | */ |
| 61 | public function get_posts() { |
| 62 | $all_fields = array(); |
| 63 | $field_groups = acf_get_field_groups(); |
| 64 | |
| 65 | foreach ( $field_groups as $field_group ) { |
| 66 | $fields = acf_get_fields( $field_group ); |
| 67 | if ( $fields ) { |
| 68 | $all_fields = array_merge( $all_fields, $fields ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return $all_fields; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Filters fields based on provided arguments. |
| 77 | * |
| 78 | * Supports filtering by: |
| 79 | * - parent: Field group ID or key |
| 80 | * - type: Field type (text, image, etc.) |
| 81 | * - name: Field name |
| 82 | * |
| 83 | * @since 6.8.0 |
| 84 | * |
| 85 | * @param array $posts Array of fields to filter. |
| 86 | * @param array $args Filter arguments. |
| 87 | * @return array Filtered fields. |
| 88 | */ |
| 89 | public function filter_posts( $posts, $args = array() ) { |
| 90 | if ( isset( $args['parent'] ) ) { |
| 91 | $parent_filter = $args['parent']; |
| 92 | |
| 93 | // Convert key to ID if not numeric (same pattern as acf_update_field). |
| 94 | if ( $parent_filter && ! is_numeric( $parent_filter ) ) { |
| 95 | $parent_post = acf_get_field_post( $parent_filter ); |
| 96 | $parent_filter = $parent_post ? $parent_post->ID : 0; |
| 97 | } |
| 98 | |
| 99 | $parent_filter = (int) $parent_filter; |
| 100 | $posts = array_filter( |
| 101 | $posts, |
| 102 | function ( $post ) use ( $parent_filter ) { |
| 103 | return (int) $post['parent'] === $parent_filter; |
| 104 | } |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | if ( isset( $args['type'] ) ) { |
| 109 | $type_filter = $args['type']; |
| 110 | $posts = array_filter( |
| 111 | $posts, |
| 112 | function ( $post ) use ( $type_filter ) { |
| 113 | return $post['type'] === $type_filter; |
| 114 | } |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | if ( isset( $args['name'] ) ) { |
| 119 | $name_filter = $args['name']; |
| 120 | $posts = array_filter( |
| 121 | $posts, |
| 122 | function ( $post ) use ( $name_filter ) { |
| 123 | return $post['name'] === $name_filter; |
| 124 | } |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | return array_values( $posts ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Updates or creates a field. |
| 133 | * |
| 134 | * @since 6.8.0 |
| 135 | * |
| 136 | * @param array $field The field data. |
| 137 | * @return array|false The updated field or false on failure. |
| 138 | */ |
| 139 | public function update_post( $field ) { |
| 140 | return acf_update_field( $field ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Permanently deletes a field. |
| 145 | * |
| 146 | * @since 6.8.0 |
| 147 | * |
| 148 | * @param int|string $id The field ID or key. |
| 149 | * @return bool True on success, false on failure. |
| 150 | */ |
| 151 | public function delete_post( $id = 0 ) { |
| 152 | return acf_delete_field( $id ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Duplicates a field. |
| 157 | * |
| 158 | * @since 6.8.0 |
| 159 | * |
| 160 | * @param int|string $id The field ID or key to duplicate. |
| 161 | * @param int $new_post_id Optional parent ID for the duplicate. |
| 162 | * @return array|false The duplicated field or false on failure. |
| 163 | */ |
| 164 | public function duplicate_post( $id = 0, $new_post_id = 0 ) { |
| 165 | $field = $this->get_post( $id ); |
| 166 | if ( ! $field ) { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | $parent_id = $new_post_id ? $new_post_id : $field['parent']; |
| 171 | return acf_duplicate_field( $field['ID'], $parent_id ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Prepares a field for export. |
| 176 | * |
| 177 | * Strips internal fields (ID, local, _valid) that shouldn't be exported. |
| 178 | * |
| 179 | * @since 6.8.0 |
| 180 | * |
| 181 | * @param array $field The field to prepare. |
| 182 | * @return array The prepared field. |
| 183 | */ |
| 184 | public function prepare_post_for_export( $field = array() ) { |
| 185 | acf_extract_vars( $field, array( 'ID', 'local', '_valid', '_name', 'prefix', 'value', 'id', 'class' ) ); |
| 186 | |
| 187 | /** This filter is documented in includes/acf-field-group-functions.php */ |
| 188 | return apply_filters( 'acf/prepare_field_for_export', $field ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Imports a field. |
| 193 | * |
| 194 | * @since 6.8.0 |
| 195 | * |
| 196 | * @param array $field The field data to import. |
| 197 | * @return array|false The imported field or false on failure. |
| 198 | */ |
| 199 | public function import_post( $field ) { |
| 200 | $filters = acf_disable_filters(); |
| 201 | $field = acf_validate_field( $field ); |
| 202 | $field = acf_update_field( $field ); |
| 203 | acf_enable_filters( $filters ); |
| 204 | |
| 205 | /** This action is documented in includes/acf-field-group-functions.php */ |
| 206 | do_action( 'acf/import_field', $field ); |
| 207 | |
| 208 | return $field; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | endif; |
| 213 |