├── .gitignore ├── README.md ├── api └── Form.php ├── js-snippets ├── confirm-email.js └── date_field_customization.js └── php-snippets ├── any-input-custom-validation.php ├── ban-email-domains.php ├── checkable-option-restriction.php ├── confirm_email_validation.php ├── custom-validation-example.php ├── data-validation-example.php ├── delete-form-assets.php ├── delete-pending-payments.php ├── delete-previous-user-entry.php ├── email-varification-with-truemail.php ├── limit-email-domains.php ├── photoshop-illustrator-file-support.php ├── plain-text-email.php └── shuffle-checkable-items.php /.gitignore: -------------------------------------------------------------------------------- 1 | wiki 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Snippets for Fluent Forms Plugin 2 | This repository contains useful fluent form snippets which will make your forms more powerful. 3 | Please use which are appropriate for you. 4 | 5 | 6 | ### PHP Snippets 7 | 8 | - `Email Validation` Limit Email Domains (Accept only particular Domains) [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/wiki/Limit-Email-Domains) 9 | - `Email Validation` Ban Email Domains (Ban particular Domains) [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/wiki/Ban-Email-Domains) 10 | - `Email Validation with TrueMail` Validate Email with TrueMail API [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/email-varification-with-truemail.php) 11 | - `TextArea Validation` Check if textarea contain url. if yes, make the form submission failed. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/custom-validation-example.php) 12 | 13 | 14 | ### Contribution: 15 | Please feel free to fork and contribute and do a pull request. 16 | 17 | ### Useful Links 18 | 19 | - [Download Fluent Forms](https://wordpress.org/plugins/fluentform) 20 | - [Facebook Community](https://www.facebook.com/groups/fluentforms/) 21 | - [Fluent Forms Public Roadmap](https://trello.com/b/FaDlAD9B/public-roadmap-wp-fluent-forms) 22 | -------------------------------------------------------------------------------- /api/Form.php: -------------------------------------------------------------------------------- 1 | table('fluentform_forms') 15 | ->select(['id', 'title']) // other columns status|appearance_settings|form_fields|has_payment|type|conditions|created_by|created_at|updated_at 16 | ->orderBy('id', 'DESC') 17 | ->get(); 18 | } 19 | 20 | /** 21 | * Get a singe form 22 | * @param int $formId 23 | * @return stdClass|null 24 | */ 25 | function getFluentForm($formId) 26 | { 27 | return wpFluent()->table('fluentform_forms') 28 | ->where('id', $formId) 29 | ->first(); 30 | } 31 | 32 | /** 33 | * Get Form Input fields 34 | * Possible with fields $with = ['admin_label', 'element', 'options', 'attributes', 'raw'] 35 | * @param int $formId 36 | * @param array $with 37 | * @return array 38 | */ 39 | function getFluentFormFields($formId, $with = ['admin_label']) 40 | { 41 | return \FluentForm\App\Modules\Form\FormFieldsParser::getInputs($formId, ['admin_label']); 42 | } 43 | 44 | -------------------------------------------------------------------------------- /js-snippets/confirm-email.js: -------------------------------------------------------------------------------- 1 | /* 2 | * JS snippet to match two fields value as same 3 | * Example: Email and Confirm Email need to be same 4 | * Placement: Place the following code in your form -> Settings & Integrations -> Custom CSS/JS -> Custom Javascript BOX 5 | */ 6 | 7 | var $email = $form.find('input[name=email]'); // Name of the first Email Address 8 | var $confirmEmail = $form.find('input[name=confirm_email]'); // Name of the Confirm Email Address 9 | 10 | $confirmEmail.on('blur', function() { 11 | if($(this).val() != $email.val()) { 12 | alert('Email not matching'); 13 | // Or you can show the custom message somewhere 14 | } 15 | }); 16 | 17 | // Additional code to check if user fixed the type in the primary email 18 | $email.on('blur', function() { 19 | if($confirmEmail.val() && $(this).val() != $confirmEmail.val()) { 20 | alert('Email not matching'); 21 | // Or you can show the custom message somewhere 22 | } 23 | }); 24 | -------------------------------------------------------------------------------- /js-snippets/date_field_customization.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Check how advanced customization works here 3 | * https://wpmanageninja.com/docs/fluent-form/field-types/time-date/#advanced_configaration 4 | * You can use the following snippets in the "Advanced Date Configuration" box of your date element settings 5 | * - You can use these settings as combined to 6 | * - you can use any flatpickr options - https://flatpickr.js.org/examples/ 7 | */ 8 | 9 | /* 10 | * Allow only future dates 11 | */ 12 | 13 | { 14 | minDate: "today" 15 | } 16 | 17 | /* 18 | * Allow only from today and 14 days from now 19 | */ 20 | { 21 | minDate: "today", 22 | maxDate: new Date().fp_incr(14) // 14 days from now 23 | } 24 | 25 | /* 26 | * Disabling range(s) of dates: 27 | */ 28 | { 29 | dateFormat: "Y-m-d", 30 | disable: [ 31 | { 32 | from: "2020-08-01", 33 | to: "2020-08-01" 34 | }, 35 | { 36 | from: "2020-09-01", 37 | to: "2020-10-01" 38 | } 39 | ] 40 | } -------------------------------------------------------------------------------- /php-snippets/any-input-custom-validation.php: -------------------------------------------------------------------------------- 1 | id != $targetFormId) { 76 | return $errorMessage; 77 | } 78 | 79 | $fieldName = $field['name']; 80 | if (empty($formData[$fieldName])) { 81 | return $errorMessage; 82 | } 83 | $value = $formData[$fieldName]; // This is the user input value 84 | 85 | /* 86 | * You can validate this value and return $errorMessage 87 | * If $error is empty then it's valid. Otherwise you can return the $errorMessage message as string 88 | */ 89 | 90 | return [$errorMessage]; 91 | 92 | }, 10, 5); 93 | -------------------------------------------------------------------------------- /php-snippets/ban-email-domains.php: -------------------------------------------------------------------------------- 1 | id != $targetFormId) { 50 | return $error; 51 | } 52 | 53 | $fieldName = $field['name']; 54 | if (empty($formData[$fieldName])) { 55 | return $error; 56 | } 57 | 58 | $valueArray = explode('@', $formData[$fieldName]); 59 | $inputDomain = array_pop($valueArray); 60 | 61 | if (in_array($inputDomain, $invalidDomains)) { 62 | return [$errorMessage]; 63 | } 64 | return $error; 65 | 66 | }, 10, 5); 67 | 68 | -------------------------------------------------------------------------------- /php-snippets/checkable-option-restriction.php: -------------------------------------------------------------------------------- 1 | id != $formId) { 19 | return $field; 20 | } 21 | 22 | $fieldName = $field['attributes']['name']; 23 | 24 | if($fieldName != $targetElementName) { 25 | return $field; 26 | } 27 | 28 | $options = $field['settings']['advanced_options']; 29 | 30 | 31 | $keyedOptions = []; 32 | foreach ($options as $option) { 33 | $keyedOptions[$option['value']] = $option; 34 | } 35 | 36 | $submittedItems = wpFluent()->table('fluentform_entry_details') 37 | ->select(wpFluent()->raw('field_value, COUNT(*) as count')) 38 | ->where('form_id', $form->id) 39 | ->where('field_name', $fieldName) 40 | ->groupBy('field_value') 41 | ->having('count', '>=', $maxValueSubmit) 42 | ->get(); 43 | 44 | foreach ($submittedItems as $item) { 45 | unset($keyedOptions[$item->field_value]); 46 | } 47 | 48 | $field['settings']['advanced_options'] = array_values($keyedOptions); 49 | return $field; 50 | }, 10, 2); 51 | -------------------------------------------------------------------------------- /php-snippets/confirm_email_validation.php: -------------------------------------------------------------------------------- 1 | id != $targetFormId) { 10 | return $targetFormId; 11 | } 12 | 13 | $primaryInputName = 'email'; // Your primary email input name 14 | $conformInputName = 'confirm_email'; // Your confirm email input name 15 | 16 | if($data[$primaryInputName] != $data[$conformInputName]) { 17 | if(!isset( $errors[$conformInputName])) { 18 | $errors[$conformInputName] = []; 19 | } 20 | $errors[$conformInputName][] = 'Confirm Email needs to be matched with primary email'; 21 | } 22 | return $errors; 23 | }, 10, 3); 24 | -------------------------------------------------------------------------------- /php-snippets/custom-validation-example.php: -------------------------------------------------------------------------------- 1 | id != $targetFormId) { 26 | return $error; 27 | } 28 | 29 | $fieldName = $field['name']; 30 | if (empty($formData[$fieldName])) { 31 | return $error; 32 | } 33 | 34 | foreach ($bannedStrings as $string) { 35 | if(strpos($formData[$fieldName], $string) !== false) { 36 | return [$errorMessage]; 37 | } 38 | } 39 | 40 | return $error; 41 | 42 | }, 10, 5); 43 | 44 | 45 | 46 | /* 47 | * Snippet: 2 48 | * This will apply for all the forms in your site 49 | * 50 | */ 51 | add_filter('fluentform_validate_input_item_textarea', function ($error, $field, $formData, $fields, $form) { 52 | $bannedStrings = ['http', 'www']; 53 | $errorMessage = 'No Url is allowed in textarea'; 54 | 55 | $fieldName = $field['name']; 56 | if (empty($formData[$fieldName])) { 57 | return $error; 58 | } 59 | 60 | foreach ($bannedStrings as $string) { 61 | if(strpos($formData[$fieldName], $string) !== false) { 62 | return [$errorMessage]; 63 | } 64 | } 65 | 66 | return $error; 67 | 68 | }, 10, 5); 69 | 70 | -------------------------------------------------------------------------------- /php-snippets/data-validation-example.php: -------------------------------------------------------------------------------- 1 | id != 1) { 11 | return $errors; 12 | } 13 | 14 | $targetInputName = 'target_input_name'; // your target input name 15 | $inputValue = $formData[$targetInputName]; // target input name data value from user 16 | 17 | if($inputValue != 'expected_value') { 18 | $errors['target_input_name']['required'] = 'Please provide the right value'; 19 | } 20 | 21 | return $errors; 22 | 23 | }, 10, 3); -------------------------------------------------------------------------------- /php-snippets/delete-form-assets.php: -------------------------------------------------------------------------------- 1 | table('fluentform_submissions') 7 | ->where('form_id', $formId) 8 | ->delete(); 9 | 10 | wpFluent()->table('fluentform_submission_meta') 11 | ->where('form_id', $formId) 12 | ->delete(); 13 | 14 | wpFluent()->table('fluentform_entry_details') 15 | ->where('form_id', $formId) 16 | ->delete(); 17 | 18 | wpFluent()->table('fluentform_form_analytics') 19 | ->where('form_id', $formId) 20 | ->delete(); 21 | 22 | wpFluent()->table('fluentform_logs') 23 | ->where('parent_source_id', $formId) 24 | ->whereIn('source_type', ['submission_item', 'form_item']) 25 | ->delete(); 26 | 27 | ob_start(); 28 | if (defined('FLUENTFORMPRO')) { 29 | try { 30 | wpFluent()->table('fluentform_order_items') 31 | ->where('form_id', $formId) 32 | ->delete(); 33 | wpFluent()->table('fluentform_subscriptions') 34 | ->where('form_id', $formId) 35 | ->delete(); 36 | wpFluent()->table('fluentform_transactions') 37 | ->where('form_id', $formId) 38 | ->delete(); 39 | } catch (\Exception $exception) { 40 | 41 | } 42 | } 43 | $errors = ob_get_clean(); 44 | 45 | return true; 46 | } 47 | -------------------------------------------------------------------------------- /php-snippets/delete-pending-payments.php: -------------------------------------------------------------------------------- 1 | table('fluentform_submissions') 11 | ->whereNotNull('payment_total') 12 | ->where('payment_status', 'pending') 13 | // ->whereIn('form_id', $formIds) // if you want to target specific forms. Uncomment this line 14 | ->where('created_at', '<', $thresholdDate) 15 | ->get(); 16 | 17 | if($pendingEntries) { 18 | $entryClass = new FluentForm\App\Modules\Entries\Entries(); 19 | foreach ($pendingEntries as $entry) { 20 | $entryClass->deleteEntryById($entry->id, $entry->form_id); 21 | } 22 | } 23 | }); 24 | 25 | 26 | /* 27 | * Move to trash for Pending Payment status entries automatically if 10 minutes exceeded 28 | */ 29 | add_action('fluentform_do_scheduled_tasks', function () { 30 | $formIds = [1, 2, 3]; // add your target form ids 31 | $thresholdSeconds = 10 * 60 * 60; // 10 minutes 32 | $thresholdDate = date('Y-m-d H:i:s', strtotime(current_time('mysql') - $thresholdSeconds)); 33 | $pendingEntries = wpFluent()->table('fluentform_submissions') 34 | ->whereNotNull('payment_total') 35 | ->where('payment_status', 'pending') 36 | // ->whereIn('form_id', $formIds) // if you want to target specific forms. Uncomment this line 37 | ->where('created_at', '<', $thresholdDate) 38 | ->update([ 39 | 'status' => 'trashed' 40 | ]); 41 | }); 42 | -------------------------------------------------------------------------------- /php-snippets/delete-previous-user-entry.php: -------------------------------------------------------------------------------- 1 | id != 12) { // Say your target form is 12 16 | return; 17 | } 18 | $currentUserId = get_current_user_id(); 19 | if(!$currentUserId) { 20 | return; // user is not logged in so no further action 21 | } 22 | 23 | // Let's get existing entries by this user 24 | $submissions = wpFluent()->table('fluentform_submissions') 25 | ->where('user_id', $currentUserId) 26 | ->where('form_id', $form->id) 27 | ->get(); 28 | if(!$submissions) { 29 | return; // This is the first submission 30 | } 31 | 32 | $entryClass = new \FluentForm\App\Modules\Entries\Entries(); 33 | foreach ($submissions as $submission) { 34 | $entryClass->deleteEntryById($submission->id, $form->id); 35 | } 36 | }, 10, 3); 37 | 38 | /* 39 | * Snippet: 2 40 | * This will apply for all the forms in your site 41 | * 42 | */ 43 | add_action('fluentform_before_submission_confirmation', function ($entryId, $formData, $form) { 44 | $currentUserId = get_current_user_id(); 45 | if(!$currentUserId) { 46 | return; // user is not logged in so no further action 47 | } 48 | 49 | // Let's get existing entries by this user 50 | $submissions = wpFluent()->table('fluentform_submissions') 51 | ->where('user_id', $currentUserId) 52 | ->where('form_id', $form->id) 53 | ->get(); 54 | if(!$submissions) { 55 | return; // This is the first submission 56 | } 57 | 58 | $entryClass = new \FluentForm\App\Modules\Entries\Entries(); 59 | foreach ($submissions as $submission) { 60 | $entryClass->deleteEntryById($submission->id, $form->id); 61 | } 62 | }, 10, 3); 63 | -------------------------------------------------------------------------------- /php-snippets/email-varification-with-truemail.php: -------------------------------------------------------------------------------- 1 | id != $targetFormId) { 58 | return $default; 59 | } 60 | 61 | $fieldName = $field['name']; 62 | if (empty($formData[$fieldName])) { 63 | return $default; 64 | } 65 | $email = $formData[$fieldName]; 66 | 67 | 68 | $request = wp_remote_get('https://truemail.io/api/v1/verify/single?address_info=1&timeout=5&access_token='.$trueMailApiKey.'&email='.$email); 69 | 70 | if(is_wp_error($request)) { 71 | return $default; // request failed so we are skipping validation 72 | } 73 | 74 | $response = wp_remote_retrieve_body($request); 75 | 76 | $response = json_decode($response, true); 77 | 78 | if($response['result'] == 'valid') { 79 | return $default; 80 | } 81 | 82 | return $errorMessage; 83 | 84 | }, 10, 5); 85 | 86 | -------------------------------------------------------------------------------- /php-snippets/limit-email-domains.php: -------------------------------------------------------------------------------- 1 | id != $targetFormId) { 51 | return $error; 52 | } 53 | 54 | $fieldName = $field['name']; 55 | if (empty($formData[$fieldName])) { 56 | return $error; 57 | } 58 | 59 | $valueArray = explode('@', $formData[$fieldName]); 60 | $inputDomain = array_pop($valueArray); 61 | 62 | if (!in_array($inputDomain, $acceptedDomains)) { 63 | return [$errorMessage]; 64 | } 65 | return $error; 66 | 67 | }, 10, 5); 68 | 69 | -------------------------------------------------------------------------------- /php-snippets/photoshop-illustrator-file-support.php: -------------------------------------------------------------------------------- 1 | __('Graphics Files (psd, ai, eps)', 'fluentform'), 11 | 'value' => 'psd|ai|eps|bin', 12 | ]; 13 | return $types; 14 | }); 15 | 16 | add_action('fluentform_starting_file_upload', function () { 17 | add_filter('fluentform_uploader_args', function ($args) { 18 | $args['test_type'] = false; 19 | return $args; 20 | }); 21 | add_filter('upload_mimes', function ($mime_types) { 22 | $mime_types['psd'] = 'image/vnd.adobe.photoshop'; //Adding photoshop files 23 | $mime_types['eps'] = 'application/postscript'; //Adding ai files 24 | $mime_types['ai'] = 'application/postscript'; //Adding ai files 25 | return $mime_types; 26 | }, 1, 1); 27 | }); 28 | -------------------------------------------------------------------------------- /php-snippets/plain-text-email.php: -------------------------------------------------------------------------------- 1 | id != $targetFormId) { 30 | return $data; 31 | } 32 | $options = $data['settings']['advanced_options']; 33 | shuffle($options); 34 | $data['settings']['advanced_options'] = $options; 35 | return $data; 36 | }, 10, 2); 37 | 38 | --------------------------------------------------------------------------------