diff --git a/README.md b/README.md index 072576a..1d3d8a2 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,24 @@ Please use which are appropriate for you. ### PHP Snippets -- `Email Validation` Limit Email Domains (Accept only particular Domains) [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/wiki/Limit-Email-Domains) -- `Email Validation` Ban Email Domains (Ban particular Domains) [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/wiki/Ban-Email-Domains) +- `Email Validation : Limit Domains` Limit Email Domains (Accept only particular Domains) [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/Limit-Email-Domains) +- `Email Validation : Ban Domains ` Ban Email Domains (Ban particular Domains) [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/ban-email-domains.php) - `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) - `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) +- `Ban Email Domains` Make the form submission failed if contains certain email domains. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/ban-email-domains.php) +- `Limit specific Email Domains` Limit the specific domain emails as valid. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/limit-email-domains.php) +- `Limit Checkbox/Radio/Select options based on previous submissions` Showing only less than max items from the previous responses. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/checkable-option-restriction.php) +- `Confirm email field` Validate email field with confirm email field. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/confirm_email_validation.php) +- `Custom Validation` Validate any field. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/any-input-custom-validation.php) +- `Input Data Validation` Validate form data. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/data-validation-example.php) +- `Delete Form Assets` Delete assets. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/delete-form-assets.php) +- `Delete Pending Payments` Pending Payment status entries automatically if 10 minutes exceeded. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/delete-pending-payments.php) +- `Delete existing submission on new submission` Imagine, You want to just keep only one entry from a user. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/delete-previous-user-entry.php) +- `Additional file types option to your file upload field ` Add supports for new file types. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/photoshop-illustrator-file-support.php) +- `Convert the email body in pure-text format ` Plain text email. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/plain-text-email.php) +- `Shuffle the Checkable items on render ` Randomize the order of options. [View Snippet](https://github.com/WPManageNinja/fluentform-snippets/blob/master/php-snippets/shuffle-checkable-items.php) +- `Input Data Attributes` Custom input attributes for input fields. [View Snippet](https://github.com/fluentform/fluentform-snippets/blob/49e4c5cb87333c1e9586c3b0823011b52f58a300/php-snippets/custom-input-attributes.php) +- `ACF Repeater Field on User Regristration` Format and update ACF repeater field meta for user registration. [View Snippet](https://github.com/fluentform/fluentform-snippets/blob/master/php-snippets/acf-repeater-field-user-meta.php) ### Contribution: diff --git a/js-snippets/date_field_customization.js b/js-snippets/date_field_customization.js index 790c5f1..3e38f5d 100644 --- a/js-snippets/date_field_customization.js +++ b/js-snippets/date_field_customization.js @@ -37,4 +37,23 @@ to: "2020-10-01" } ] +} + +/* +* Disable past dates and all future weekend dates: + */ +{ + "disable" : [ + function(date) { + const excludedDays = [0, 6]; // Exclude weekends (Saturday and Sunday) + const dateInfo = new Date(date); + const today = new Date(); + today.setHours(0, 0, 0, 0); + if (dateInfo >= today && !excludedDays.includes(dateInfo.getDay())) { + // Date is future and not a weekend + return false; + } + return true; + } + ] } \ No newline at end of file diff --git a/php-snippets/acf-repeater-field-user-meta.php b/php-snippets/acf-repeater-field-user-meta.php new file mode 100644 index 0000000..250203f --- /dev/null +++ b/php-snippets/acf-repeater-field-user-meta.php @@ -0,0 +1,215 @@ +id != $targetFormId) return; + + // Decode the form data + $formData = json_decode($entry->response, true); + $targetFormFieldName = 'repeater_field'; + + // Check if the form data contains the repeater field + if (isset($formData[$targetFormFieldName]) && function_exists('get_field_object')) { + // Get the repeater field value + $ffRepeaterValue = $formData[$targetFormFieldName]; + $targetAcfUserRepeaterFieldName = 'acf_repeater_field_name'; + + // Get the ACF field configuration using the current user ID + $fieldConfig = get_field_object($targetAcfUserRepeaterFieldName, 'user_' . $userId); + + // Ensure $fieldConfig is not falsy before proceeding + if ($fieldConfig && $fieldConfig['type'] == 'repeater' && isset($fieldConfig['sub_fields'])) { + $subFields = $fieldConfig['sub_fields']; + + // Prepare the repeater field value for ACF + $itemValues = []; + foreach ($ffRepeaterValue as $value) { + if (count($value) !== count($subFields)) { + continue; + } + $item = []; + foreach ($subFields as $subIndex => $subField) { + $item[$subField['name']] = $value[$subIndex]; + } + $itemValues[] = $item; + } + + // Update the ACF repeater field value + if ($itemValues) { + update_field($targetAcfUserRepeaterFieldName, $itemValues, 'user_' . $userId); + } + } + } +}, 10, 4); + + +/** + * Snipped 2 - For User Update + * + * Read more about this hook + * @link https://developers.fluentforms.com/hooks/actions/#fluentform_user_update_completed + * + */ +add_action('fluentform/user_update_completed', function($userId, $feed, $entry, $form) { + $targetFormId = 0; + // Return if the form ID is not equal to target FormId + if ($form->id != $targetFormId) return; + + // Decode the form data + $formData = json_decode($entry->response, true); + $targetFormFieldName = 'repeater_field'; + + // Check if the form data contains the repeater field + if (isset($formData[$targetFormFieldName]) && function_exists('get_field_object')) { + // Get the repeater field value + $ffRepeaterValue = $formData[$targetFormFieldName]; + $targetAcfUserRepeaterFieldName = 'acf_repeater_field_name'; + + // Get the ACF field configuration using the current user ID + $fieldConfig = get_field_object($targetAcfUserRepeaterFieldName, 'user_' . $userId); + + // Ensure $fieldConfig is not falsy before proceeding + if ($fieldConfig && $fieldConfig['type'] == 'repeater' && isset($fieldConfig['sub_fields'])) { + $subFields = $fieldConfig['sub_fields']; + + // Prepare the repeater field value for ACF + $itemValues = []; + foreach ($ffRepeaterValue as $value) { + if (count($value) !== count($subFields)) { + continue; + } + $item = []; + foreach ($subFields as $subIndex => $subField) { + $item[$subField['name']] = $value[$subIndex]; + } + $itemValues[] = $item; + } + + // Update the ACF repeater field value + if ($itemValues) { + update_field($targetAcfUserRepeaterFieldName, $itemValues, 'user_' . $userId); + } + } + } +}, 10, 4); + + +/** + * Snipped 3 - Default Repeater Value Mapping for User Update + * + * For some very old repeater filed use 'fluentform/render_item_input_repeat', + * Repeater filed created from FF version 3 use input_repeat for element type + * Repeater filed created after FF version 3 use repeater_field for element type + * + * Read more about this hook + * @link https://developers.fluentforms.com/hooks/actions/#fluentform_render_item___item_element_ + * + */ +add_action('fluentform/render_item_repeater_field', function($field, $form) { + // Return if the form ID is not equal to target FormId + $targetFormId = 0; + if ($form->id != $targetFormId) return; + + // Check acf field value get function exist + if (!function_exists('get_field')) return; + + // Form repeater field name + $targetFormFieldName = 'repeater_field'; + + // Return if the form field is not equal to target field + if ($field['attributes']['name'] != $targetFormFieldName) return; + + // Get the ACF meta value for the acf meta name + $targetAcfUserRepeaterFieldName = 'acf_repeater_field_name'; + $userRepeaterFieldValue = get_field($targetAcfUserRepeaterFieldName, 'user_' . get_current_user_id()); + + // Return if the meta value is empty and not an array + if (!$userRepeaterFieldValue || !is_array($userRepeaterFieldValue)) { + return; + } + + // Normalize the array structure to ff repeater format + foreach ($userRepeaterFieldValue as $index => &$subFieldValue) { + if (!is_array($subFieldValue) || count($subFieldValue) !== count($field['fields'])) { + unset($userRepeaterFieldValue[$index]); + continue; + } + $subFieldValue = array_values($subFieldValue); + } + + // If the array is empty after normalization, return + if (!$userRepeaterFieldValue) { + return; + } + + // Pass the ACF values from PHP to JavaScript + $acfValues = json_encode($userRepeaterFieldValue); + ?> + + id != $targetFormId) { return $targetFormId; diff --git a/php-snippets/custom-input-attributes.php b/php-snippets/custom-input-attributes.php new file mode 100644 index 0000000..4790892 --- /dev/null +++ b/php-snippets/custom-input-attributes.php @@ -0,0 +1,21 @@ + 'Test Value', + 'custom-attr-2' => 'Another Values' + ]; + // Update your target field name + $targetField = 'input_text'; + if ($data['attributes']['name'] != $targetField) { + return $data; + } + $data['attributes'] = array_merge($data['attributes'], $myAttributes); + + return $data; +}, 10, 2); diff --git a/php-snippets/custom-validation-example.php b/php-snippets/custom-validation-example.php index 3b4dbf9..b0a3c3c 100644 --- a/php-snippets/custom-validation-example.php +++ b/php-snippets/custom-validation-example.php @@ -12,7 +12,7 @@ * Snippet: 1 * This will apply for only form id: 12 */ -add_filter('fluentform_validate_input_item_input_email', function ($error, $field, $formData, $fields, $form) { +add_filter('fluentform/validate_input_item_input_email', function ($error, $field, $formData, $fields, $form) { /* * add your banned sub-strings and form it here * If the textarea has any match it will make the form submission failed @@ -48,7 +48,7 @@ * This will apply for all the forms in your site * */ -add_filter('fluentform_validate_input_item_textarea', function ($error, $field, $formData, $fields, $form) { +add_filter('fluentform/validate_input_item_textarea', function ($error, $field, $formData, $fields, $form) { $bannedStrings = ['http', 'www']; $errorMessage = 'No Url is allowed in textarea'; diff --git a/php-snippets/data-validation-example.php b/php-snippets/data-validation-example.php index b305d9a..0a4556f 100644 --- a/php-snippets/data-validation-example.php +++ b/php-snippets/data-validation-example.php @@ -5,7 +5,7 @@ * Use Case: Validate speci */ -add_filter('fluentform_validation_errors', function ($errors, $formData, $form) { +add_filter('fluentform/validation_errors', function ($errors, $formData, $form) { // use only for a form; eg: only form id: 1 if($form->id != 1) { return $errors; @@ -20,4 +20,4 @@ return $errors; -}, 10, 3); \ No newline at end of file +}, 10, 3); diff --git a/php-snippets/delete-pending-payments.php b/php-snippets/delete-pending-payments.php index 169b93d..8891c7b 100644 --- a/php-snippets/delete-pending-payments.php +++ b/php-snippets/delete-pending-payments.php @@ -3,7 +3,7 @@ /* * Delete Pending Payment status entries automatically if 10 minutes exceeded */ -add_action('fluentform_do_scheduled_tasks', function () { +add_action('fluentform/do_scheduled_tasks', function () { $formIds = [1, 2, 3]; // add your target form ids $thresholdSeconds = 10 * 60 * 60; // 10 minutes $thresholdDate = date('Y-m-d H:i:s', strtotime(current_time('mysql') - $thresholdSeconds)); @@ -26,7 +26,7 @@ /* * Move to trash for Pending Payment status entries automatically if 10 minutes exceeded */ -add_action('fluentform_do_scheduled_tasks', function () { +add_action('fluentform/do_scheduled_tasks', function () { $formIds = [1, 2, 3]; // add your target form ids $thresholdSeconds = 10 * 60 * 60; // 10 minutes $thresholdDate = date('Y-m-d H:i:s', strtotime(current_time('mysql') - $thresholdSeconds)); diff --git a/php-snippets/delete-previous-user-entry.php b/php-snippets/delete-previous-user-entry.php index 029e7cf..b5cdf35 100644 --- a/php-snippets/delete-previous-user-entry.php +++ b/php-snippets/delete-previous-user-entry.php @@ -11,7 +11,7 @@ * Snippet: 1 * This will apply for only form id: 12 */ -add_action('fluentform_before_submission_confirmation', function ($entryId, $formData, $form) { +add_action('fluentform/before_submission_confirmation', function ($entryId, $formData, $form) { if($form->id != 12) { // Say your target form is 12 return; } @@ -40,7 +40,7 @@ * This will apply for all the forms in your site * */ -add_action('fluentform_before_submission_confirmation', function ($entryId, $formData, $form) { +add_action('fluentform/before_submission_confirmation', function ($entryId, $formData, $form) { $currentUserId = get_current_user_id(); if(!$currentUserId) { return; // user is not logged in so no further action diff --git a/php-snippets/email-varification-with-truemail.php b/php-snippets/email-varification-with-truemail.php index 3e5a0a6..50a717d 100644 --- a/php-snippets/email-varification-with-truemail.php +++ b/php-snippets/email-varification-with-truemail.php @@ -10,7 +10,7 @@ * This will apply for all the forms in your site */ -add_filter('fluentform_validate_input_item_input_email', function ($default, $field, $formData, $fields, $form) { +add_filter('fluentform/validate_input_item_input_email', function ($default, $field, $formData, $fields, $form) { $errorMessage = 'Looks like email is not correct'; // You may change here $trueMailApiKey = 'INSERT_YOUR_TRUEMAIL.IO_API_KEY'; @@ -47,7 +47,7 @@ * Snippet: 2 * This will apply for only form id: 12 */ -add_filter('fluentform_validate_input_item_input_email', function ($default, $field, $formData, $fields, $form) { +add_filter('fluentform/validate_input_item_input_email', function ($default, $field, $formData, $fields, $form) { // You may change the following 3 lines $targetFormId = 12; diff --git a/php-snippets/limit-email-domains.php b/php-snippets/limit-email-domains.php index a66e061..6374dab 100644 --- a/php-snippets/limit-email-domains.php +++ b/php-snippets/limit-email-domains.php @@ -10,7 +10,7 @@ * Snippet: 1 * This will apply for all the forms in your site */ -add_filter('fluentform_validate_input_item_input_email', function ($error, $field, $formData, $fields, $form) { +add_filter('fluentform/validate_input_item_input_email', function ($error, $field, $formData, $fields, $form) { /* * add your accepted domains here * Other domains will be failed to submit the form @@ -37,7 +37,7 @@ * Snippet: 2 * This will apply for only form id: 12 */ -add_filter('fluentform_validate_input_item_input_email', function ($error, $field, $formData, $fields, $form) { +add_filter('fluentform/validate_input_item_input_email', function ($error, $field, $formData, $fields, $form) { /* * add your accepted domains here * Other domains will be failed to submit the form diff --git a/php-snippets/photoshop-illustrator-file-support.php b/php-snippets/photoshop-illustrator-file-support.php index f36157d..a00a91a 100644 --- a/php-snippets/photoshop-illustrator-file-support.php +++ b/php-snippets/photoshop-illustrator-file-support.php @@ -5,7 +5,7 @@ * Just add this snippet to your theme's functions.php file or relevant place. */ -add_filter('fluentform_file_type_options', function ($types) { +add_filter('fluentform/file_type_options', function ($types) { $types[] = [ 'label' => __('Graphics Files (psd, ai, eps)', 'fluentform'), 'value' => 'psd|ai|eps|bin', @@ -13,8 +13,8 @@ return $types; }); -add_action('fluentform_starting_file_upload', function () { - add_filter('fluentform_uploader_args', function ($args) { +add_action('fluentform/starting_file_upload', function () { + add_filter('fluentform/uploader_args', function ($args) { $args['test_type'] = false; return $args; }); diff --git a/php-snippets/plain-text-email.php b/php-snippets/plain-text-email.php index 8a181d5..b5ff8e5 100644 --- a/php-snippets/plain-text-email.php +++ b/php-snippets/plain-text-email.php @@ -5,7 +5,7 @@ * But your email body still may have HTML from the email body composer and smart codes. You can use this snippet to convert the body in pure-text format */ -add_filter('fluentform_email_body', function($body, $notification) { +add_filter('fluentform/email_body', function($body, $notification) { if(isset($notification['asPlainText']) && $notification['asPlainText'] == 'yes') { return wp_strip_all_tags($body); } diff --git a/php-snippets/shuffle-checkable-items.php b/php-snippets/shuffle-checkable-items.php index 9d7f5d1..2084b82 100644 --- a/php-snippets/shuffle-checkable-items.php +++ b/php-snippets/shuffle-checkable-items.php @@ -1,17 +1,17 @@ id != $targetFormId) { return $data;