our type. Anything else is listed read-only. */ const WRITABLE = array( 'text' => 'text', 'textarea' => 'textarea', 'wysiwyg' => 'html', 'email' => 'email', 'url' => 'url', 'oembed' => 'url', 'number' => 'number', 'range' => 'number', 'true_false' => 'boolean', 'select' => 'select', 'radio' => 'select', 'button_group' => 'select', 'image' => 'image', 'link' => 'link', 'color_picker' => 'text', 'date_picker' => 'text', 'date_time_picker' => 'text', 'time_picker' => 'text', ); /** Storage names claimed by repeaters / flexible fields during a walk (their row count / layout list). */ private static $containers = array(); /** Field types that hold no value, or one ADI must never see. */ const SKIP = array( 'tab', 'message', 'accordion', 'password' ); public function id() { return 'acf'; } /** * The value exactly as stored, with no load filters: a repeater's row count, a flexible field's * list of layout names, an image's attachment id. (acf_get_value would turn repeaters and * flexible content into full row arrays.) */ private static function stored( $acf_id, array $def ) { if ( function_exists( 'acf_get_metadata_by_field' ) ) { return acf_get_metadata_by_field( $acf_id, $def ); } return acf_get_metadata( $acf_id, $def['name'] ); } public function name() { return 'Advanced Custom Fields'; } public function available() { return function_exists( 'acf_get_field_groups' ) && function_exists( 'acf_update_value' ) && ( function_exists( 'acf_get_metadata_by_field' ) || function_exists( 'acf_get_metadata' ) ); } public function detect( \WP_Post $post ) { return (bool) self::groups_for_post( $post ); } public function post_column( $locator ) { return null; // post meta / options } private static function groups_for_post( \WP_Post $post ) { $groups = acf_get_field_groups( array( 'post_id' => $post->ID, 'post_type' => $post->post_type, ) ); return array_values( array_filter( $groups, static function ( $g ) { return ! empty( $g['active'] ) || ! isset( $g['active'] ); } ) ); } public function list_fields( \WP_Post $post ) { return self::public_fields( self::walk_groups( $post->ID, self::groups_for_post( $post ) ) ); } public function read( \WP_Post $post, $locator ) { return self::find( self::walk_groups( $post->ID, self::groups_for_post( $post ) ), $locator ); } public function write( \WP_Post $post, $locator, $value ) { self::write_at( $post->ID, self::walk_groups( $post->ID, self::groups_for_post( $post ) ), $locator, $value ); } // ── Options pages ─────────────────────────────────────────────────────────────────── /** Registered options pages that have fields: slug => { title, post_id, capability }. */ public static function option_pages() { if ( ! function_exists( 'acf_get_options_pages' ) ) { return array(); } $out = array(); foreach ( (array) acf_get_options_pages() as $slug => $page ) { $store = isset( $page['post_id'] ) ? (string) $page['post_id'] : 'options'; // Only pages stored as options. ACF also lets a page store on a post, user or term (`42`, // `post_42`, `user_3`, `term_5`, `category_5`) — writing there would bypass that object's own // permission check, so those pages are not offered. ACF's own decoder decides. $decoded = function_exists( 'acf_decode_post_id' ) ? acf_decode_post_id( $store ) : null; if ( '' === $store || ( $decoded ? 'option' !== $decoded['type'] : ( ctype_digit( $store ) || preg_match( '/^(post|user|term|comment|block)_/', $store ) ) ) ) { continue; } $groups = acf_get_field_groups( array( 'options_page' => $slug ) ); if ( ! $groups ) { continue; } $out[ $slug ] = array( 'slug' => $slug, 'title' => isset( $page['page_title'] ) ? $page['page_title'] : $slug, 'post_id' => isset( $page['post_id'] ) ? $page['post_id'] : 'options', 'capability' => ! empty( $page['capability'] ) ? $page['capability'] : 'manage_options', ); } return $out; } public static function option_page( $slug ) { $pages = self::option_pages(); if ( ! isset( $pages[ $slug ] ) ) { // Name only the pages this user may open: the others are not theirs to know about. $visible = array_keys( array_filter( $pages, static function ( $p ) { return current_user_can( $p['capability'] ); } ) ); throw new Tool_Error( esc_html( "There is no options page \"$slug\" with fields. Options pages: " . ( $visible ? implode( ', ', $visible ) : 'none' ) . '.' ) ); } return $pages[ $slug ]; } private static function option_walk( $slug ) { $page = self::option_page( $slug ); return array( $page, self::walk_groups( $page['post_id'], acf_get_field_groups( array( 'options_page' => $slug ) ) ) ); } public function option_fields( $slug ) { list( , $walked ) = self::option_walk( $slug ); return self::public_fields( $walked ); } public function option_read( $slug, $locator ) { list( , $walked ) = self::option_walk( $slug ); return self::find( $walked, $locator ); } public function option_write( $slug, $locator, $value ) { list( $page, $walked ) = self::option_walk( $slug ); self::write_at( $page['post_id'], $walked, $locator, $value ); } // ── Walking ───────────────────────────────────────────────────────────────────────── /** @return array[] Each: public field + `_def` (the ACF field with its full stored name). */ private static function walk_groups( $acf_id, array $groups ) { self::$containers = array(); $items = array(); foreach ( $groups as $group ) { foreach ( (array) acf_get_fields( $group ) as $field ) { foreach ( self::walk( $acf_id, $field, $field['name'], $field['name'], array( $group['title'] ) ) as $item ) { $items[] = $item; } } } // Two definitions stored in the same place (the same name in two groups, a group's // contact/phone and a top-level contact_phone, or a text field named like a repeater whose // row count lives there) are one value: only the first stays editable — and a repeater's or // flexible field's own storage always wins, since overwriting it would orphan every row. $seen = array(); $used = array(); foreach ( self::$containers as $name => $label ) { $seen[ 'meta:' . $name ] = $label; } $out = array(); foreach ( $items as $item ) { $key = ! empty( $item['_def']['name'] ) ? 'meta:' . $item['_def']['name'] : 'path:' . $item['locator']; if ( isset( $seen[ $key ] ) ) { $item['writable'] = false; $item['_def'] = null; $item['note'] = 'Another field (' . $seen[ $key ] . ') is stored in the same place; edit it there.'; $base = $item['locator'] . '#duplicate'; $item['locator'] = $base; $n = 2; while ( isset( $used[ $item['locator'] ] ) ) { $item['locator'] = $base . $n++; } } else { $seen[ $key ] = $item['label']; } $used[ $item['locator'] ] = true; $out[] = $item; } return $out; } private static function walk( $acf_id, array $field, $stored, $locator, array $trail ) { $type = isset( $field['type'] ) ? $field['type'] : ''; $label = isset( $field['label'] ) && '' !== $field['label'] ? $field['label'] : $field['name']; if ( '' === (string) $field['name'] || in_array( $type, self::SKIP, true ) ) { return array(); } $def = $field; $def['name'] = $stored; // how ACF itself loads/saves a sub-field: by its full stored name if ( 'group' === $type ) { $out = array(); foreach ( (array) $field['sub_fields'] as $sub ) { $out = array_merge( $out, self::walk( $acf_id, $sub, "{$stored}_{$sub['name']}", "$locator/{$sub['name']}", array_merge( $trail, array( $label ) ) ) ); } return $out; } if ( 'repeater' === $type ) { self::$containers[ $stored ] = $label; $rows = (int) self::stored( $acf_id, $def ); $out = array(); for ( $i = 0; $i < $rows; $i++ ) { foreach ( (array) $field['sub_fields'] as $sub ) { $out = array_merge( $out, self::walk( $acf_id, $sub, "{$stored}_{$i}_{$sub['name']}", "$locator/$i/{$sub['name']}", array_merge( $trail, array( "$label " . ( $i + 1 ) ) ) ) ); } } return $out; } if ( 'flexible_content' === $type ) { self::$containers[ $stored ] = $label; $layouts = array(); foreach ( (array) $field['layouts'] as $layout ) { $layouts[ $layout['name'] ] = $layout; } $out = array(); $names = self::stored( $acf_id, $def ); foreach ( is_array( $names ) ? array_values( $names ) : array() as $i => $name ) { // '' = all sections removed $layout = isset( $layouts[ $name ] ) ? $layouts[ $name ] : null; $title = $layout && ! empty( $layout['label'] ) ? $layout['label'] : (string) $name; $crumb = array_merge( $trail, array( "$label › " . ( $i + 1 ) . ". $title" ) ); // The section itself, so the page's structure is visible even where it has no text. $out[] = array( 'locator' => "$locator/$i", 'label' => implode( ' › ', $crumb ), 'type' => 'readonly', 'value' => (string) $name, 'writable' => false, 'note' => 'A section of this page (layout "' . $name . '"). ADI edits its fields; adding, removing or reordering sections is done in wp-admin.', '_def' => null, ); if ( ! $layout ) { continue; } foreach ( (array) $layout['sub_fields'] as $sub ) { $out = array_merge( $out, self::walk( $acf_id, $sub, "{$stored}_{$i}_{$sub['name']}", "$locator/$i/{$sub['name']}", $crumb ) ); } } return $out; } $raw = self::stored( $acf_id, $def ); $ours = isset( self::WRITABLE[ $type ] ) ? self::WRITABLE[ $type ] : null; if ( 'number' === $ours && null !== $raw && '' !== $raw && ! is_numeric( $raw ) ) { $ours = null; // a number field holding something else (changed type, imported data): read-only } $multiple = in_array( $type, array( 'select' ), true ) && ! empty( $field['multiple'] ); $locked = ! empty( $field['readonly'] ) || ! empty( $field['disabled'] ); $public = array( 'locator' => $locator, 'label' => implode( ' › ', array_merge( $trail, array( $label ) ) ), 'type' => ( $ours && ! $multiple ) ? $ours : 'readonly', 'value' => self::normalise( $ours && ! $multiple ? $ours : 'readonly', $raw ), 'writable' => $ours && ! $multiple && ! $locked, 'acf_type' => $type, 'acf_key' => isset( $field['key'] ) ? (string) $field['key'] : '', // the SEO rule judges ACF fields by definition '_def' => $def, '_name' => $field['name'], ); if ( 'select' === $public['type'] ) { $public['choices'] = isset( $field['choices'] ) ? (array) $field['choices'] : array(); } if ( in_array( $type, array( 'date_picker', 'date_time_picker', 'time_picker' ), true ) ) { $public['note'] = 'Stored as ' . ( 'date_picker' === $type ? 'YYYYMMDD (e.g. 20261031)' : ( 'time_picker' === $type ? 'HH:MM:SS' : 'YYYY-MM-DD HH:MM:SS' ) ) . '.'; } if ( ! $public['writable'] && 'readonly' === $public['type'] ) { $public['note'] = in_array( $type, array( 'gallery', 'relationship', 'post_object', 'page_link', 'taxonomy', 'user', 'file' ), true ) ? 'IDs of linked items; change these in wp-admin.' : "ADI doesn't edit \"$type\" fields; change it in wp-admin."; } elseif ( $locked ) { $public['note'] = 'Marked read-only in the field settings.'; } if ( ! empty( $field['instructions'] ) && empty( $public['note'] ) ) { $public['note'] = wp_strip_all_tags( $field['instructions'] ); } return array( $public ); } private static function normalise( $type, $raw ) { switch ( $type ) { case 'boolean': return (bool) (int) $raw; case 'number': if ( null === $raw || '' === $raw ) { return null; } return is_numeric( $raw ) ? 0 + $raw : $raw; // non-numeric legacy data: shown as stored case 'image': return (int) $raw; case 'link': return is_array( $raw ) ? $raw : null; case 'readonly': return $raw; } return null === $raw ? '' : ( is_scalar( $raw ) ? (string) $raw : $raw ); } private static function public_fields( array $walked ) { return array_map( static function ( $f ) { unset( $f['_def'], $f['_name'] ); return $f; }, $walked ); } /** * Colour and date fields are text to WordPress but have a format their theme relies on — often * echoed straight into style="" — so only well-formed values are stored. */ private static function assert_format( array $field, $value ) { if ( ! is_string( $value ) || '' === $value || \AdiBridge\Content::is_restoring() ) { return; } $type = isset( $field['acf_type'] ) ? $field['acf_type'] : ''; $ok = true; if ( 'color_picker' === $type ) { $ok = (bool) preg_match( '/^#[0-9a-f]{3,8}$|^rgba?\(\s*[\d.]+%?\s*,\s*[\d.]+%?\s*,\s*[\d.]+%?\s*(,\s*[\d.]+\s*)?\)$/i', $value ); } elseif ( in_array( $type, array( 'date_picker', 'date_time_picker', 'time_picker' ), true ) ) { $format = array( 'date_picker' => 'Ymd', 'date_time_picker' => 'Y-m-d H:i:s', 'time_picker' => 'H:i:s' )[ $type ]; $parsed = \DateTime::createFromFormat( '!' . $format, $value ); $ok = $parsed && $parsed->format( $format ) === $value; } if ( ! $ok ) { throw new Tool_Error( esc_html( "{$field['label']} takes " . ( 'color_picker' === $type ? 'a colour like #5b2d8e' : 'a value like ' . ( isset( $field['note'] ) ? $field['note'] : 'the stored format' ) ) . '. Nothing was changed.' ) ); } } private static function find( array $walked, $locator ) { foreach ( $walked as $f ) { if ( $f['locator'] === $locator ) { unset( $f['_def'], $f['_name'] ); return $f; } } return null; } private static function write_at( $acf_id, array $walked, $locator, $value ) { $field = null; foreach ( $walked as $f ) { if ( $f['locator'] === $locator ) { $field = $f; break; // the same field find() returns } } if ( ! $field || ! $field['writable'] || empty( $field['_def'] ) ) { throw new Tool_Error( esc_html( "acf:$locator doesn't exist or can't be edited." ) ); } // ACF's own rules for this field (required, min/max, maxlength, the site's acf/validate_value // filters): what wp-admin would refuse, ADI refuses too. Not when putting back a value that was // already stored (a rule added since must not block undoing a refused change). if ( ! \AdiBridge\Content::is_restoring() && function_exists( 'acf_validate_value' ) && function_exists( 'acf_reset_validation_errors' ) ) { acf_reset_validation_errors(); $check = 'boolean' === $field['type'] ? ( $value ? 1 : 0 ) : $value; // Validate under the sub-field's own name, as wp-admin does for rows, so a site's // acf/validate_value/name=… rules fire; saving uses the full stored name. $validate = $field['_def']; $validate['name'] = isset( $field['_name'] ) ? $field['_name'] : $validate['name']; if ( ! acf_validate_value( $check, $validate, 'acf[adi]' ) ) { $errors = function_exists( 'acf_get_validation_errors' ) ? (array) acf_get_validation_errors() : array(); acf_reset_validation_errors(); $why = $errors && isset( $errors[0]['message'] ) ? wp_strip_all_tags( $errors[0]['message'] ) : 'the field\'s rules'; throw new Tool_Error( esc_html( "{$field['label']}: $why. Nothing was changed." ) ); } } self::assert_format( $field, $value ); if ( 'boolean' === $field['type'] ) { $value = $value ? 1 : 0; } elseif ( 'link' === $field['type'] ) { $value = is_array( $value ) && isset( $value['url'] ) ? array( 'title' => isset( $value['title'] ) ? $value['title'] : '', 'url' => $value['url'], 'target' => isset( $value['target'] ) ? $value['target'] : '' ) : ''; // an empty link (restoring a link that was never set) } // ACF (and update_metadata) strip one level of slashes, expecting slashed form data as // wp-admin sends it: slash here, or backslashes in the value would be lost. acf_update_value( wp_slash( $value ), $acf_id, $field['_def'] ); } }