__( 'Permission denied.', 'airy-frontend-forms' ) ) ); } $post_type = isset( $_POST['post_type'] ) ? sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) : ''; if ( empty( $post_type ) ) { wp_send_json_error( array( 'message' => __( 'Post type is required.', 'airy-frontend-forms' ) ) ); } $field_groups = $this->get_field_groups_for_post_type( $post_type ); wp_send_json_success( array( 'field_groups' => $field_groups, 'post_type' => $post_type, ) ); } /** * AJAX: Get fields for specific field group */ public function ajax_get_fields_for_group() { check_ajax_referer( 'affcf_admin_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', 'airy-frontend-forms' ) ) ); } $group_id = isset( $_POST['group_id'] ) ? sanitize_text_field( wp_unslash( $_POST['group_id'] ) ) : ''; if ( empty( $group_id ) ) { wp_send_json_error( array( 'message' => __( 'Group ID is required.', 'airy-frontend-forms' ) ) ); } $fields = $this->get_fields_for_group( $group_id ); wp_send_json_success( array( 'fields' => $fields, 'group_id' => $group_id, ) ); } /** * AJAX: Get taxonomies for post type */ public function ajax_get_taxonomies() { check_ajax_referer( 'affcf_admin_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Permission denied.', 'airy-frontend-forms' ) ) ); } $post_type = isset( $_POST['post_type'] ) ? sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) : ''; if ( empty( $post_type ) ) { wp_send_json_error( array( 'message' => __( 'Post type is required.', 'airy-frontend-forms' ) ) ); } $taxonomies = $this->get_taxonomies_for_post_type( $post_type ); wp_send_json_success( array( 'taxonomies' => $taxonomies, 'post_type' => $post_type, ) ); } /** * Get field groups for post type * * @param string $post_type Post type. * @return array Field groups. */ public function get_field_groups_for_post_type( $post_type ) { $field_groups = array(); // Check which field plugin is active. if ( AFFCF_Plugin_Checker::is_acf_active() ) { $field_groups = $this->get_acf_field_groups( $post_type ); } elseif ( AFFCF_Plugin_Checker::is_scf_active() ) { $field_groups = $this->get_scf_field_groups( $post_type ); } return $field_groups; } /** * Get ACF field groups * * @param string $post_type Post type. * @return array Field groups. */ private function get_acf_field_groups( $post_type ) { $acf_compat = AFFCF_ACF_Compat::get_instance(); return $acf_compat->get_field_groups_for_cpt( $post_type ); } /** * Get SCF field groups * * @param string $post_type Post type. * @return array Field groups. */ private function get_scf_field_groups( $post_type ) { $scf_compat = AFFCF_SCF_Compat::get_instance(); return $scf_compat->get_field_groups_for_cpt( $post_type ); } /** * Get fields for specific group * * @param string $group_id Group ID. * @return array Fields. */ public function get_fields_for_group( $group_id ) { $fields = array(); // Check which field plugin is active. if ( AFFCF_Plugin_Checker::is_acf_active() ) { $acf_compat = AFFCF_ACF_Compat::get_instance(); $fields = $acf_compat->get_fields_for_group( $group_id ); } elseif ( AFFCF_Plugin_Checker::is_scf_active() ) { // SCF fields are already included in field groups. // We'll need to implement get_fields_for_group in SCF compat. $scf_compat = AFFCF_SCF_Compat::get_instance(); $fields = $scf_compat->get_fields_for_group( $group_id ); } return $fields; } /** * Get taxonomies for post type * * @param string $post_type Post type. * @return array Taxonomies. */ public function get_taxonomies_for_post_type( $post_type ) { $taxonomies = get_object_taxonomies( $post_type, 'objects' ); $formatted_taxonomies = array(); foreach ( $taxonomies as $taxonomy ) { if ( ! $taxonomy->public || ! $taxonomy->show_ui ) { continue; } $formatted_taxonomies[] = array( 'name' => $taxonomy->name, 'label' => $taxonomy->labels->singular_name, 'labels' => array( 'singular' => $taxonomy->labels->singular_name, 'plural' => $taxonomy->labels->name, ), 'hierarchical' => $taxonomy->hierarchical, 'public' => $taxonomy->public, ); } return $formatted_taxonomies; } /** * Get available post types * * @return array Available post types. */ public function get_available_post_types() { $post_types = get_post_types( array( 'public' => true, 'show_ui' => true, ), 'objects' ); $formatted_post_types = array(); foreach ( $post_types as $post_type ) { $formatted_post_types[] = array( 'value' => $post_type->name, 'label' => $post_type->labels->singular_name, 'icon' => $post_type->menu_icon ?? 'dashicons-admin-post', ); } return $formatted_post_types; } /** * Check if ACF or SCF is available * * @return bool True if either ACF or SCF is active. */ public function is_available() { return AFFCF_Plugin_Checker::has_field_plugin(); } /** * Get field type icon * * @param string $field_type Field type. * @return string Dashicon class. */ public static function get_field_type_icon( $field_type ) { $icons = array( 'text' => 'dashicons-editor-textcolor', 'textarea' => 'dashicons-editor-alignleft', 'number' => 'dashicons-editor-ol', 'email' => 'dashicons-email', 'url' => 'dashicons-admin-links', 'select' => 'dashicons-menu-alt', 'checkbox' => 'dashicons-yes', 'radio' => 'dashicons-marker', 'true_false' => 'dashicons-editor-removeformatting', 'file' => 'dashicons-media-default', 'image' => 'dashicons-format-image', 'gallery' => 'dashicons-format-gallery', 'wysiwyg' => 'dashicons-editor-paste-word', 'date_picker' => 'dashicons-calendar-alt', 'color_picker' => 'dashicons-art', 'repeater' => 'dashicons-editor-ul', 'group' => 'dashicons-editor-table', 'relationship' => 'dashicons-admin-links', 'taxonomy' => 'dashicons-category', 'user' => 'dashicons-admin-users', ); return isset( $icons[ $field_type ] ) ? $icons[ $field_type ] : 'dashicons-admin-generic'; } /** * Get field type label * * @param string $field_type Field type. * @return string Readable label. */ public static function get_field_type_label( $field_type ) { $labels = array( 'text' => __( 'Text', 'airy-frontend-forms' ), 'textarea' => __( 'Textarea', 'airy-frontend-forms' ), 'number' => __( 'Number', 'airy-frontend-forms' ), 'email' => __( 'Email', 'airy-frontend-forms' ), 'url' => __( 'URL', 'airy-frontend-forms' ), 'select' => __( 'Select', 'airy-frontend-forms' ), 'checkbox' => __( 'Checkbox', 'airy-frontend-forms' ), 'radio' => __( 'Radio', 'airy-frontend-forms' ), 'true_false' => __( 'True/False', 'airy-frontend-forms' ), 'file' => __( 'File Upload', 'airy-frontend-forms' ), 'image' => __( 'Image', 'airy-frontend-forms' ), 'gallery' => __( 'Gallery', 'airy-frontend-forms' ), 'wysiwyg' => __( 'WYSIWYG Editor', 'airy-frontend-forms' ), 'date_picker' => __( 'Date Picker', 'airy-frontend-forms' ), 'color_picker' => __( 'Color Picker', 'airy-frontend-forms' ), 'repeater' => __( 'Repeater', 'airy-frontend-forms' ), 'group' => __( 'Group', 'airy-frontend-forms' ), 'relationship' => __( 'Relationship', 'airy-frontend-forms' ), 'taxonomy' => __( 'Taxonomy', 'airy-frontend-forms' ), 'user' => __( 'User', 'airy-frontend-forms' ), ); return isset( $labels[ $field_type ] ) ? $labels[ $field_type ] : ucfirst( str_replace( '_', ' ', $field_type ) ); } }