$value ) { if ( property_exists( $this, $key ) ) { $this->$key = $value; } } // Decode JSON fields. $this->decode_json_fields(); } /** * Decode JSON fields */ private function decode_json_fields() { $json_fields = array( 'field_groups', 'default_fields', 'taxonomies', 'settings' ); foreach ( $json_fields as $field ) { if ( is_string( $this->$field ) && ! empty( $this->$field ) ) { // Decode JSON string. $decoded = json_decode( $this->$field, true ); $this->$field = $decoded ?? array(); } elseif ( null === $this->$field || '' === $this->$field ) { // Initialize NULL or empty string to empty array. $this->$field = array(); } // If already an array, leave it as is. } } /** * Convert to array for database * * @return array Database-ready data. */ public function to_array() { $data = array( 'form_name' => $this->form_name, 'form_slug' => $this->form_slug, 'shortcode' => $this->shortcode, 'post_type' => $this->post_type, 'status' => $this->status, 'created_by' => $this->created_by, ); // Encode JSON fields. $json_fields = array( 'field_groups', 'default_fields', 'taxonomies', 'settings' ); foreach ( $json_fields as $field ) { $data[ $field ] = is_array( $this->$field ) ? wp_json_encode( $this->$field ) : $this->$field; } return $data; } /** * Validate model data * * @return bool|array True if valid, array of errors otherwise. */ public function validate() { $errors = array(); if ( empty( $this->form_name ) ) { $errors[] = __( 'Form name is required.', 'airy-frontend-forms' ); } if ( empty( $this->post_type ) ) { $errors[] = __( 'Post type is required.', 'airy-frontend-forms' ); } if ( empty( $this->shortcode ) ) { $errors[] = __( 'Shortcode is required.', 'airy-frontend-forms' ); } if ( ! in_array( $this->status, array( 'active', 'inactive' ), true ) ) { $this->status = 'active'; } return empty( $errors ) ? true : $errors; } /** * Get enabled field groups * * @return array Enabled field groups. */ public function get_enabled_field_groups() { if ( empty( $this->field_groups ) ) { return array(); } return array_filter( $this->field_groups, function ( $group ) { return ! empty( $group['enabled'] ); } ); } /** * Get enabled default fields * * @return array Enabled default fields. */ public function get_enabled_default_fields() { if ( empty( $this->default_fields ) ) { return array(); } return array_filter( $this->default_fields, function ( $field ) { return ! empty( $field['enabled'] ); } ); } /** * Get enabled taxonomies * * @return array Enabled taxonomies. */ public function get_enabled_taxonomies() { if ( empty( $this->taxonomies ) ) { return array(); } return array_filter( $this->taxonomies, function ( $tax ) { return ! empty( $tax['enabled'] ); } ); } /** * Check if form is active * * @return bool. */ public function is_active() { return 'active' === $this->status; } }