'; echo '
'; // Toggle — ACF saves this automatically as part of the field array. acf_render_field_setting( $field, [ 'label' => '', 'type' => 'true_false', 'name' => 'aibs_fcp_for_acf_plugin_enabled', 'ui' => 1, ] ); echo '
' . esc_html__( 'FLEXIBLE CONTENT PICKER', 'aibs-flexible-content-picker-for-acf' ) . '
'; echo '?' . esc_html__( 'Replaces the ACF Flexible Content layout dropdown with a visual picker — preview, search, multi-select, and reorder layouts before adding.', 'aibs-flexible-content-picker-for-acf' ) . ''; echo '
'; echo ''; echo ''; // .aibs-fcp-for-acf-plugin-wrapper } /** * Renders a single layout's metadata row. * * @param string $field_key ACF field key. * @param array $layout Layout definition array. * @param array $stored_meta Stored metadata keyed by layout name. */ private function render_layout_row( string $field_key, array $layout, array $stored_meta ): void { $layout_name = sanitize_key( $layout['name'] ?? '' ); $layout_label = $layout['label'] ?? $layout_name; $meta = $stored_meta[ $layout_name ] ?? []; $thumbnail = $meta['thumbnail'] ?? ''; echo '
'; echo '
'; echo '' . esc_html( $layout_label ) . ''; echo '
'; echo '
'; printf( '
', esc_attr( $field_key ), esc_attr( $layout_name ), esc_attr( $meta['title'] ?? '' ), esc_attr( $layout_label ) ); printf( '
', esc_attr( $field_key ), esc_attr( $layout_name ), esc_attr( $meta['description'] ?? '' ), esc_attr__( 'Short Description', 'aibs-flexible-content-picker-for-acf' ) ); printf( '
OR
', esc_url( $thumbnail ), ( $thumbnail ? '' : ' style="display:none;"' ), esc_attr( $field_key ), esc_attr( $layout_name ), esc_attr( $thumbnail ), esc_attr( $field_key ), esc_attr( $layout_name ), esc_html__( 'UPLOAD', 'aibs-flexible-content-picker-for-acf' ) ); echo '
'; echo '
'; } /* ----------------------------------------------------------------------- AJAX: autosave a single layout meta field --------------------------------------------------------------------- */ public function ajax_autosave_meta(): void { check_ajax_referer( 'aibs_fcp_for_acf_plugin_autosave', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( 'forbidden', 403 ); } // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $field_key = sanitize_key( wp_unslash( $_POST['field_key'] ?? '' ) ); $layout_name = sanitize_key( wp_unslash( $_POST['layout_name'] ?? '' ) ); $field_type = sanitize_key( wp_unslash( $_POST['field_type'] ?? '' ) ); $raw_value = wp_unslash( $_POST['value'] ?? '' ); // phpcs:enable $allowed_types = [ 'title', 'description', 'thumbnail' ]; if ( ! $field_key || ! $layout_name || ! in_array( $field_type, $allowed_types, true ) ) { wp_send_json_error( 'invalid_params' ); } if ( 'thumbnail' === $field_type ) { $value = esc_url_raw( $raw_value ); } elseif ( 'description' === $field_type ) { $value = sanitize_textarea_field( $raw_value ); } else { $value = sanitize_text_field( $raw_value ); } $meta = self::get_field_meta( $field_key ); if ( ! isset( $meta[ $layout_name ] ) ) { $meta[ $layout_name ] = [ 'title' => '', 'description' => '', 'thumbnail' => '' ]; } $meta[ $layout_name ][ $field_type ] = $value; update_option( 'aibs_fcp_for_acf_plugin_meta_' . $field_key, $meta, false ); wp_send_json_success(); } /* ----------------------------------------------------------------------- Field-group editor: save --------------------------------------------------------------------- */ /** * Persists per-layout metadata when ACF saves the field group. * * Fires via `acf/update_field/type=flexible_content`. * * The editor values are read from $_POST only after independently verifying * the ACF field-group save nonce (`_acf_nonce`, action `field_group`) — the * same nonce ACF checks before this filter fires — and confirming the user * can manage options. On import / field-group duplication there is no request * nonce, so the metadata embedded in the field array by acf/load_field is used * instead. (Editor values are also autosaved individually over AJAX with their * own nonce; this save is a consolidated snapshot on field-group save.) * * @param array $field ACF field array (already processed by ACF). * @return array Unchanged field array. */ public function save_layout_meta( array $field ): array { $meta_key = $field['key']; $nonce = isset( $_POST['_acf_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_acf_nonce'] ) ) : ''; if ( $nonce && wp_verify_nonce( $nonce, 'field_group' ) && current_user_can( 'manage_options' ) && isset( $_POST['aibs_fcp_for_acf_plugin_layout_meta'][ $meta_key ] ) ) { // Normal path: user saved the field group editor manually. Values are // unslashed here and every value is sanitized per-field in the loop below. // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $raw = wp_unslash( $_POST['aibs_fcp_for_acf_plugin_layout_meta'][ $meta_key ] ); } elseif ( ! empty( $field['aibs_fcp_for_acf_layout_meta'] ) && is_array( $field['aibs_fcp_for_acf_layout_meta'] ) ) { // Import / duplication path: metadata was embedded in the field array // by acf/load_field on the source field and is now available on the // new field key. Persist it under the new key. $raw = $field['aibs_fcp_for_acf_layout_meta']; } else { return $field; } if ( ! is_array( $raw ) ) { return $field; } $clean = []; foreach ( $raw as $layout_name => $values ) { if ( ! is_array( $values ) ) { continue; } $layout_name = sanitize_key( $layout_name ); if ( ! $layout_name ) { continue; } $clean[ $layout_name ] = [ 'title' => sanitize_text_field( $values['title'] ?? '' ), 'description' => sanitize_textarea_field( $values['description'] ?? '' ), 'thumbnail' => esc_url_raw( $values['thumbnail'] ?? '' ), ]; } update_option( 'aibs_fcp_for_acf_plugin_meta_' . $field['key'], $clean, false ); return $field; } /* ----------------------------------------------------------------------- Public API --------------------------------------------------------------------- */ /** * Returns stored per-layout metadata for a given flexible content field. * * @param string $field_key ACF field key. * @return array Keyed by layout name. */ public static function get_field_meta( string $field_key ): array { return (array) get_option( 'aibs_fcp_for_acf_plugin_meta_' . $field_key, [] ); } }