get_fields_for_group( $group ); $formatted_groups[] = array( 'id' => $group->get_id(), 'title' => $group->get_title(), 'fields' => $fields, ); } return $formatted_groups; } /** * Get fields for group * * @param object $group SCF group object. * @return array Fields. */ private function get_fields_for_group( $group ) { $fields = $group->get_fields(); if ( ! $fields ) { return array(); } $formatted_fields = array(); foreach ( $fields as $field ) { $formatted_fields[] = array( 'key' => $field->get( 'name' ), 'name' => $field->get( 'name' ), 'label' => $field->get( 'label' ), 'type' => $field->get( 'type' ), 'required' => false, // SCF doesn't have built-in required. 'instructions' => $field->get( 'notes' ) ?? '', 'default_value' => $field->get( 'default' ) ?? '', ); } return $formatted_fields; } /** * Get field by name * * Since SCF is an ACF fork, use ACF functions * * @param string $field_name Field name/key. * @return array|null Field data. */ public function get_field( $field_name ) { // SCF is an ACF fork - use ACF functions if available. if ( function_exists( 'acf_get_field' ) ) { return acf_get_field( $field_name ); } // Fallback to old SCF method. if ( ! class_exists( 'Smart_Custom_Fields_Settings' ) ) { return null; } // Search through all post types to find this field. $post_types = get_post_types( array( 'public' => true ) ); foreach ( $post_types as $post_type ) { $settings = SCF::get_settings( $post_type ); if ( ! $settings ) { continue; } foreach ( $settings as $group ) { $fields = $group->get_fields(); if ( ! $fields ) { continue; } foreach ( $fields as $field ) { if ( $field->get( 'name' ) === $field_name ) { // Found the field! Build array structure. $field_data = array( 'key' => $field->get( 'name' ), 'name' => $field->get( 'name' ), 'label' => $field->get( 'label' ), 'type' => $field->get( 'type' ), 'required' => false, 'instructions' => $field->get( 'notes' ) ?? '', 'default_value' => $field->get( 'default' ) ?? '', ); // For group/repeater fields, get sub-fields. if ( in_array( $field->get( 'type' ), array( 'group', 'repeater' ), true ) ) { $field_data['sub_fields'] = $this->get_group_sub_fields( $field ); } return $field_data; } } } } return null; } /** * Get sub-fields for a group or repeater field * * @param object $field SCF field object. * @return array Sub-fields. */ private function get_group_sub_fields( $field ) { // Debug checkpoint: Entering get_group_sub_fields. // For SCF, group fields have their structure in the field object. // Try to get the fields property. if ( method_exists( $field, 'get_fields' ) ) { $sub_fields_objects = $field->get_fields(); // Debug checkpoint: Got sub_fields_objects. if ( $sub_fields_objects ) { $formatted = array(); foreach ( $sub_fields_objects as $sub_field ) { $formatted[] = array( 'key' => $sub_field->get( 'name' ), 'name' => $sub_field->get( 'name' ), 'label' => $sub_field->get( 'label' ), 'type' => $sub_field->get( 'type' ), 'required' => false, 'instructions' => $sub_field->get( 'notes' ) ?? '', 'default_value' => $sub_field->get( 'default' ) ?? '', ); } // Debug checkpoint: Formatted sub-fields. return $formatted; } } // Try accessing the children property directly. if ( property_exists( $field, 'children' ) && ! empty( $field->children ) ) { // Debug checkpoint: Accessing children property. $formatted = array(); foreach ( $field->children as $sub_field ) { $formatted[] = array( 'key' => $sub_field->get( 'name' ), 'name' => $sub_field->get( 'name' ), 'label' => $sub_field->get( 'label' ), 'type' => $sub_field->get( 'type' ), 'required' => false, 'instructions' => $sub_field->get( 'notes' ) ?? '', 'default_value' => $sub_field->get( 'default' ) ?? '', ); } // Debug checkpoint: Formatted children. return $formatted; } return array(); } /** * Get sub-fields for a parent field (group or repeater) * * Since SCF is an ACF fork, use ACF functions * * @param string|array $parent_field Parent field name or field array. * @return array Sub-fields. */ public function get_sub_fields( $parent_field ) { // SCF is an ACF fork - use acf_get_fields() if available. if ( function_exists( 'acf_get_fields' ) ) { // If string, get the field first. if ( is_string( $parent_field ) ) { $parent_field = $this->get_field( $parent_field ); } if ( ! empty( $parent_field ) ) { $sub_fields = acf_get_fields( $parent_field ); // Debug checkpoint: Retrieved ACF sub-fields. return is_array( $sub_fields ) ? $sub_fields : array(); } } // Fallback to old method. if ( is_string( $parent_field ) ) { $parent_field_name = $parent_field; $parent_field = $this->get_field( $parent_field_name ); } if ( ! $parent_field || empty( $parent_field['sub_fields'] ) ) { return array(); // Debug checkpoint: No parent field or sub_fields. } // Debug checkpoint: Returning sub_fields array. return $parent_field['sub_fields']; } /** * Update field value * * @param string $field_name Field name. * @param mixed $value Field value. * @param int $post_id Post ID. * @return bool Success. */ public function update_field( $field_name, $value, $post_id ) { if ( ! function_exists( 'SCF' ) ) { return false; } return update_post_meta( $post_id, $field_name, $value ); } /** * Get field value * * @param string $field_name Field name. * @param int $post_id Post ID. * @return mixed Field value. */ public function get_field_value( $field_name, $post_id ) { if ( ! function_exists( 'SCF' ) ) { return null; } return SCF::get( $field_name, $post_id ); } /** * Get SCF version * * @return string|null. */ public function get_version() { if ( defined( 'SMART_CUSTOM_FIELDS_VERSION' ) ) { return SMART_CUSTOM_FIELDS_VERSION; } return null; } }