save_form($select_id, isset($_GET['tab']) ? $_GET['tab'] : 0); } $this->metaboxes(); } function scripts() { \wp_enqueue_script("ajaxy-forms-admin-script", AJAXY_FORMS_PLUGIN_URL . '/admin/assets/js/builder.js', ['jquery', 'backbone', 'jquery-ui-draggable', 'jquery-ui-sortable'], AJAXY_FORMS_VERSION, true); // \wp_enqueue_script("ajaxy-forms" . "-admin-actions-script", AJAXY_FORMS_PLUGIN_URL . '/admin/assets/js/actions.js', ['jquery', 'backbone', 'jquery-ui-draggable'], AJAXY_FORMS_VERSION, true); wp_localize_script( "ajaxy-forms-admin-script", 'ajaxyFormsBuilder', array( 'fields' => \Ajaxy\Forms\Inc\Fields::getInstance()->get_all_properties(), "constraints" => \Ajaxy\Forms\Inc\Constraints::getInstance()->get_all_properties(), ) ); } function metaboxes() { add_meta_box( 'form-fields', __('Fields'), [$this, 'render_metabox'], 'form-fields', 'side' ); } function render_metabox() { $fields = \Ajaxy\Forms\Inc\Fields::getInstance()->get_fields(); // $fields = \array_unique($fields); $commonFields = \array_filter($fields, function ($field) { return $field['common']; }); ?> message = [ 'type' => 'error', 'message' => __('Nonce verification failed, Please try again.', "ajaxy-forms"), ]; } else { $fields = []; if (isset($post_vars['fields'])) { $fields = $post_vars['fields']; \usort($fields, function ($a, $b) { return $a['_sort'] <=> $b['_sort']; }); $names = []; $index = 1; $fields = \array_map(function ($field) use (&$names, &$index) { unset($field['_sort']); $name = $field['name'] ?? ''; if (\trim($name) === '') { $field['name'] = \sanitize_title($field['label']); } else { $field['name'] = \sanitize_title($field['name']); } if (\trim($field['name']) === '') { $field['name'] = 'field_' . ($index++); } if (\in_array($field['name'], $names)) { $field['name'] = 'field_' . \uniqid(); } $names[] = $field['name']; $properties = \Ajaxy\Forms\Inc\Fields::getInstance()->get_properties($field['type']); //remove default properties to reduce the settings size foreach ($properties as $key => $property) { $default = $property['default'] ?? ''; switch ($property['type']) { case 'radio': case 'checkbox': $default = intval($property['default'] ?? 0); break; default: $default = $property['default'] ?? ''; break; } if ($property['name'] != 'type' && isset($field[$property['name']]) && ($field[$property['name']] === $default || intval($field[$property['name']]) === $default)) { unset($field[$property['name']]); } } return $field; }, $fields); } $metadata = [ 'fields' => $fields, 'options' => $post_vars['options'] ?? [], 'initial_data' => [], 'theme' => $post_vars['theme'] ?? '', ]; $name = \sanitize_title($post_vars['name'] ?? ''); if (\trim($name) === '') { $name = 'form_' . \uniqid(); } else { $old_form = Data::get_form_by_name($name); if ($old_form && $old_form['id'] != $select_id) { $name = 'form_' . \uniqid(); } } if ($select_id) { Data::update_form($select_id, $name, $metadata); } else { $select_id = Data::add_form($name, $metadata); } if (\is_wp_error($select_id)) { $this->message = [ 'type' => 'error', 'message' => $select_id->get_error_message(), ]; } else if ($select_id) { \wp_safe_redirect( add_query_arg( array( 'action' => 'edit', 'tab' => $tab, 'form' => $select_id, 'message' => urlencode(__('Form saved successfully', "ajaxy-forms")), ), admin_url('admin.php?page=ajaxy-form') ) ); exit(); } else { $this->message = [ 'type' => 'error', 'message' => __('Failed to save the form', "ajaxy-forms") ]; } } } function form_page_handler() { if (!\current_user_can(self::CAPABILITY)) { \wp_die(\esc_html__('You are not allowed to manage forms.', 'ajaxy-forms'), 403); } $select_id = isset($_GET['form']) ? (int)$_GET['form'] : null; $message = isset($_GET['message']) ? ["type" => "success", "message" => $_GET['message']] : ''; $message = isset($_GET['error']) ? ["type" => "error", "message" => $_GET['error']] : $message; $action = isset($_GET['action']) ? $_GET['action'] : ''; if ($action == 'delete') { if (!wp_verify_nonce($_REQUEST['_wpnonce'] ?? '', 'delete-form-' . $select_id)) { wp_die('You already submitted this delete request, please reload and try again.'); } Data::delete_form($select_id); \wp_redirect(admin_url(sprintf('admin.php?page=ajaxy-form&message=%s', urlencode(__('Form deleted successfully', "ajaxy-forms"))))); exit(); } if (!\in_array($action, ['edit', 'actions', 'new', 'add'])) { $action = ''; } $tab = isset($_GET['tab']) ? $_GET['tab'] : 0; $form = null; $metadata = null; if ($select_id) { $form = Data::get_form($select_id); if (!$form) { wp_die('Form not found.'); } try { $metadata = \json_decode($form['metadata'], true); // echo ''; wp_add_inline_script('ajaxy-forms-admin-script', 'var form_metadata = ' . $form['metadata']); $form['metadata'] = $metadata; } catch (\Exception $e) { $metadata = null; } } else { $action = 'new'; } $nonce = 'save_form_' . ($select_id ? $select_id : 'new'); if ($this->message) { $message = $this->message; } ?>


'success', 'message' => __('Form imported successfully', "ajaxy-forms"), ]; } catch (\Exception $e) { $message = [ 'type' => 'error', 'message' => sprintf(__('Failed to import the form. Please make sure the JSON is valid: %s', "ajaxy-forms"), $e->getMessage()), ]; } if ($message) : ?>

create a new form. Do not forget to save your changes!', "ajaxy-forms"), esc_url( admin_url('admin.php?page=ajaxy-form&action=add') ) ), ['a' => ['href' => '']]); ?>
'error', 'message' => __('You are not allowed to edit form actions.', "ajaxy-forms"), ], 403); return; } $post_vars = array_map('stripslashes_deep', $_POST); $name = $post_vars['name'] ?? ''; if (!$name) { wp_send_json([ 'status' => 'error', 'message' => __('Invalid action name', "ajaxy-forms"), ], 200); return; } $form = Data::get_form($_GET['form_id'] ?? ''); if (!$form) { wp_send_json([ 'status' => 'error', 'message' => __('Form doesn\'t exist or haven\'t been saved yet', "ajaxy-forms"), ], 200); return; } if (!wp_verify_nonce($post_vars['_wpnonce'], 'ajaxy_forms_action_' . $name)) { wp_send_json([ 'status' => 'error', 'message' => __('Nonce verification failed, Please try again.', "ajaxy-forms"), ], 200); return; } $values = $post_vars['form_action'] ?? []; // The form never receives the saved credentials, so an empty one // means it was left alone rather than cleared. $existing = []; try { $existing = (array)\json_decode($form['actions'] ?? '', true); } catch (\Throwable $e) { $existing = []; } $previous = $existing[$name] ?? []; foreach (\Ajaxy\Forms\Inc\Actions::getInstance()->get_properties($name) as $property) { if (empty($property['secret'])) { continue; } $key = $property['name'] ?? ''; if ($key && empty($values[$key]) && !empty($previous[$key])) { $values[$key] = $previous[$key]; } } $saved = Data::update_form_action($form['id'], $name, $values); if ($saved === false) { wp_send_json([ 'status' => 'error', 'message' => __('Failed to save the action', "ajaxy-forms") ], 200); return; } $response = [ 'status' => 'success', 'message' => \sprintf(__('Action "%s" is saved!', "ajaxy-forms"), $name), ]; wp_send_json($response); } }