add_action('admin_menu', 'register_form_folders_submenu', 15); add_action('init', 'register_form_folders_taxonomy'); add_action('wp_ajax_create_folder', 'handle_create_folder'); add_action('wp_ajax_assign_form_to_folder', 'handle_assign_form_to_folder'); add_action('wp_ajax_remove_form_from_folder', 'handle_remove_form_from_folder'); add_action('wp_ajax_rename_folder', 'handle_folder_renaming'); add_action('wp_ajax_delete_folder', 'handle_folder_deletion'); /** * Registers a submenu page under the Gravity Forms menu for form folders. * * @return void */ function register_form_folders_submenu(): void { add_submenu_page( 'gf_edit_forms', 'Form Folders', 'Form Folders', 'gform_full_access', 'gf-form-folders', 'render_form_folders_page' ); } /** * Registers a custom taxonomy for organizing forms into folders. * * The taxonomy 'gf_form_folders' is associated with the 'gf_form' post type. It is not publicly queryable, * does not have URL rewrites, and supports a non-hierarchical structure. It includes an admin column for easier management in the admin interface. * * @return void */ function register_form_folders_taxonomy(): void { register_taxonomy('gf_form_folders', 'gf_form', [ 'label' => 'Form Folders', 'rewrite' => false, 'public' => false, 'show_admin_column' => true, 'hierarchical' => false, ]); } /** * Handles the creation of a new folder for forms. * * Validates the current user's permission and the provided folder name. * Inserts a new term into the 'gf_form_folders' taxonomy. Returns a success or error message depending on the outcome. * * @return void Sends a JSON response indicating success or failure. */ function handle_create_folder(): void { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'create_folder')) { wp_send_json_error(['message' => 'Invalid nonce. Request rejected.'], 403); wp_die(); } if (!current_user_can('gform_full_access')) { wp_send_json_error(['message' => 'Unauthorized'], 403); wp_die(); } if (empty($_POST['folder_name'])) { wp_send_json_error(['message' => 'Folder name is required'], 403); wp_die(); } $folder_name = sanitize_text_field($_POST['folder_name']); $inserted = wp_insert_term($folder_name, 'gf_form_folders'); if (is_wp_error($inserted)) { wp_send_json_error(['message' => $inserted->get_error_message()], 403); wp_die(); } wp_send_json_success(['message' => 'Folder created successfully!']); wp_die(); } /** * Handles the process of assigning a form to a folder. * * Ensures the current user has the necessary permissions to perform the action. * Validates required input data, assigns the form to the specified folder, * and returns the appropriate success or error messages. * * @return void Outputs a JSON response indicating success or failure. */ function handle_assign_form_to_folder(): void { if (! isset($_POST['nonce']) || ! wp_verify_nonce($_POST['nonce'], 'assign_form')) { wp_send_json_error(['message' => 'Invalid nonce. Request rejected.'], 403); wp_die(); } if (!current_user_can('gform_full_access')) { wp_send_json_error(['message' => 'Unauthorized']); wp_die(); } if (empty($_POST['form_id']) || empty($_POST['folder_id'])) { wp_send_json_error(['message' => 'Form and Folder are required']); wp_die(); } $form_id = absint($_POST['form_id']); $folder_id = absint($_POST['folder_id']); $result = wp_set_object_terms($form_id, [$folder_id], 'gf_form_folders'); if (is_wp_error($result)) { wp_send_json_error(['message' => $result->get_error_message()]); wp_die(); } wp_send_json_success(['message' => 'Form assigned successfully!']); wp_die(); } /** * Handles the removal of a form from a folder. * * This function validates the nonce, checks user permissions, and removes the specified form * from its associated folder. It sends a JSON response indicating success or failure. * * @return void Outputs a JSON response and terminates execution. */ function handle_remove_form_from_folder(): void { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'remove_form')) { wp_send_json_error(['message' => 'Invalid nonce. Request rejected.'], 403); wp_die(); } if (!current_user_can('gform_full_access')) { wp_send_json_error(['message' => 'Unauthorized'], 403); wp_die(); } if (empty($_POST['form_id'])) { wp_send_json_error(['message' => 'Form ID is required'], 403); wp_die(); } $form_id = absint($_POST['form_id']); $result = wp_set_object_terms($form_id, [], 'gf_form_folders'); if (is_wp_error($result)) { wp_send_json_error(['message' => $result->get_error_message()], 403); wp_die(); } wp_send_json_success(['message' => 'Form removed from the folder successfully!']); wp_die(); } /** * Handles the renaming of a folder via an AJAX request. * * This function validates the provided nonce, ensures required parameters * are present, and updates the folder name in the taxonomy. Errors are returned * in JSON format, and a success response is sent upon successful renaming. * * @return void This function exits with a JSON response and does not return. */ function handle_folder_renaming(): void { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'rename_folder')) { wp_send_json_error(['message' => 'Invalid nonce. Request rejected.'], 403); wp_die(); } if (empty($_POST['folder_id']) || empty($_POST['folder_name'])) { wp_send_json_error(['message' => __('Missing required parameters.', 'my-textdomain')], 400); wp_die(); } $folder_id = absint($_POST['folder_id']); $folder_name = sanitize_text_field($_POST['folder_name']); $folder = get_term($folder_id, 'gf_form_folders'); if (is_wp_error($folder) || !$folder) { wp_send_json_error(['message' => __('The specified folder does not exist.', 'my-textdomain')], 404); } // Update the folder name $updated_folder = wp_update_term($folder_id, 'gf_form_folders', ['name' => $folder_name]); if (is_wp_error($updated_folder)) { wp_send_json_error(['message' => __('Failed to rename the folder. Please try again.', 'my-textdomain')]); } wp_send_json_success(['message' => __('Folder renamed successfully.', 'my-textdomain')]); wp_die(); } function handle_folder_deletion(): void { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'delete_folder')) { wp_send_json_error(['message' => 'Invalid nonce. Request rejected.'], 403); wp_die(); } if (empty($_POST['folder_id'])) { wp_send_json_error(['message' => 'Missing required parameters.'], 400); } $folder_id = absint($_POST['folder_id']); $folder = get_term($folder_id, 'gf_form_folders'); if (is_wp_error($folder) || !$folder) { wp_send_json_error(['message' => 'The specified folder does not exist.'], 404); } $result = wp_delete_term($folder_id, 'gf_form_folders'); if (is_wp_error($result)) { wp_send_json_error(['message' => 'Failed to delete the folder. Please try again.'], 403); } else { wp_send_json_success(['message' => 'Folder deleted successfully.']); } } /** * Renders the Form Folders admin page for the Gravity Forms plugin. * * This method displays the main "Form Folders" page or a detailed view of a specific folder * with its assigned forms. Includes functionality for viewing forms within a folder, creating * new folders, and assigning forms to folders. Access is restricted to users with full Gravity Forms access. * * @return void */ function render_form_folders_page(): void { if (!current_user_can('gform_full_access')) { wp_die(__('You do not have sufficient permissions to access this page.')); } $folder_id = isset($_GET['folder_id']) ? absint($_GET['folder_id']) : 0; if ($folder_id) { $folder = get_term($folder_id, 'gf_form_folders'); if (is_wp_error($folder) || !$folder) { echo '

Invalid folder.

'; return; } ?>

Forms in Folder: name); ?>

Back to All Folders 'ids']); if (in_array($folder_id, $form_terms)) { $found = true; $settings_info = GFForms::get_form_settings_sub_menu_items($form['id']); ?> '; } $rename_folder_nonce = wp_create_nonce('rename_folder'); ?>
Form Name Shortcode Settings Actions
[gravityform id="" title="false" description="false"] Edit | |
No forms found in this folder.


Rename Folder

'; return; } ?>

Form Folders


Create a New Folder

Assign a Form to a Folder