Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new snippets #6

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions js-snippets/date_field_customization.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
]
}
215 changes: 215 additions & 0 deletions php-snippets/acf-repeater-field-user-meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<?php

/*
* Update ACF repeater field using action hook for user meta on user registration.
* Action Hook: fluentform/user_registration_completed
*
*/

/*
* Note:
* - Replace $targetFormId with your form ID.
* - Replace $targetFormFieldName with the responsible form repeater field name.
* - Replace $targetAcfUserRepeaterFieldName with the responsible ACF repeater field name.
*/


/**
* Snipped 1 - For User Registration
*
* Read more about this hook
* @link https://developers.fluentforms.com/hooks/actions/#fluentform_user_registration_completed
*
*/
add_action('fluentform/user_registration_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 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);
?>
<script>
// Pass the ACF values from PHP to JavaScript
var acfUserMetaRepeaterValues = <?php echo $acfValues; ?>;

// jQuery document ready function
jQuery(document).ready(function($) {
// Find the repeater table within the form
var $repeaterTable = $('form[data-form_id="<?php echo $targetFormId?>"] table[data-root_name="<?php echo $targetFormFieldName?>"]');

// Find the rows of the repeater table
var $repeaterTableTr = $repeaterTable.find('tbody tr');

// Check if the repeater table contains rows
if (!$repeaterTableTr.length) {
return;
}

// Loop through each ACF repeater row value
acfUserMetaRepeaterValues.forEach(function(acfUserMetaRepeaterValue, acfUserMetaRepeaterValueIndex) {
// Clone the first row of the repeater table
var $repeaterTableTrFresh = $repeaterTableTr.eq(0).clone();

// Update the cloned elements
$repeaterTableTrFresh.find('td').each(function(tdIndex) {
var $el = $(this).find('.ff-el-form-control:last-child');
var newId = 'ffrpt-' + (new Date()).getTime() + tdIndex;
var itemProp = {
value: acfUserMetaRepeaterValue[tdIndex] || '', // Set value from ACF values or empty string
id: newId
};
$el.prop(itemProp);
if ($el.attr('data-mask')) {
$el.mask($el.attr('data-mask'));
}
});

// Insert the fresh copy after the original <tr>
$repeaterTableTrFresh.insertAfter($repeaterTable.find('tbody tr').eq(acfUserMetaRepeaterValueIndex));
});

// Remove the first empty row
$repeaterTable.find('tbody tr').eq(0).remove();
});
</script>
<?php
}, 10, 2);
60 changes: 30 additions & 30 deletions php-snippets/any-input-custom-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@

/*
* Common Filter hook names
* text/Mask: fluentform_validate_input_item_input_text
* email: fluentform_validate_input_item_input_email
* textarea: fluentform_validate_input_item_textarea
* numeric: fluentform_validate_input_item_input_number
* numeric: fluentform_validate_input_item_input_number
* Range Slider: fluentform_validate_input_item_rangeslider
* Range Slider: fluentform_validate_input_item_rangeslider
* Address: fluentform_validate_input_item_address
* Address: fluentform_validate_input_item_address
* Country Select: fluentform_validate_input_item_select_country
* Country Select: fluentform_validate_input_item_select_country
* Select: fluentform_validate_input_item_select
* Radio: fluentform_validate_input_item_input_radio
* Checkbox: fluentform_validate_input_item_input_checkbox
* Checkbox: fluentform_validate_input_item_input_checkbox
* Website URL: fluentform_validate_input_item_input_input_url
* Website URL: fluentform_validate_input_item_input_input_url
* Date: fluentform_validate_input_item_input_input_date
* Image Upload: fluentform_validate_input_item_input_image
* File Upload: fluentform_validate_input_item_input_file
* Phone Filed: fluentform_validate_input_item_phone
* Phone Filed: fluentform_validate_input_item_phone
* Color Picker: fluentform_validate_input_item_color_picker
* Net Promoter Score: fluentform_validate_input_item_net_promoter_score
* Password: fluentform_validate_input_item_input_password
* Ratings: fluentform_validate_input_item_ratings
* Ratings: fluentform_validate_input_item_ratings
* text/Mask: fluentform/validate_input_item_input_text
* email: fluentform/validate_input_item_input_email
* textarea: fluentform/validate_input_item_textarea
* numeric: fluentform/validate_input_item_input_number
* numeric: fluentform/validate_input_item_input_number
* Range Slider: fluentform/validate_input_item_rangeslider
* Range Slider: fluentform/validate_input_item_rangeslider
* Address: fluentform/validate_input_item_address
* Address: fluentform/validate_input_item_address
* Country Select: fluentform/validate_input_item_select_country
* Country Select: fluentform/validate_input_item_select_country
* Select: fluentform/validate_input_item_select
* Radio: fluentform/validate_input_item_input_radio
* Checkbox: fluentform/validate_input_item_input_checkbox
* Checkbox: fluentform/validate_input_item_input_checkbox
* Website URL: fluentform/validate_input_item_input_input_url
* Website URL: fluentform/validate_input_item_input_input_url
* Date: fluentform/validate_input_item_input_input_date
* Image Upload: fluentform/validate_input_item_input_image
* File Upload: fluentform/validate_input_item_input_file
* Phone Filed: fluentform/validate_input_item_phone
* Phone Filed: fluentform/validate_input_item_phone
* Color Picker: fluentform/validate_input_item_color_picker
* Net Promoter Score: fluentform/validate_input_item_net_promoter_score
* Password: fluentform/validate_input_item_input_password
* Ratings: fluentform/validate_input_item_ratings
* Ratings: fluentform/validate_input_item_ratings
*/

/*
Expand All @@ -45,7 +45,7 @@
* $fields: Array - All fields of the form
* $form: Object - The Form Object
*/
add_filter('fluentform_validate_input_item_input_text', function ($errorMessage, $field, $formData, $fields, $form) {
add_filter('fluentform/validate_input_item_input_text', function ($errorMessage, $field, $formData, $fields, $form) {
$fieldName = $field['name'];
if (empty($formData[$fieldName])) {
return $errorMessage;
Expand All @@ -66,7 +66,7 @@
* Snippet: 2
* This will apply for all the forms in your site
*/
add_filter('fluentform_validate_input_item_input_text', function ($errorMessage, $field, $formData, $fields, $form) {
add_filter('fluentform/validate_input_item_input_text', function ($errorMessage, $field, $formData, $fields, $form) {

/*
* Validate only for form id 12
Expand All @@ -84,7 +84,7 @@

/*
* You can validate this value and return $errorMessage
* If $error is empty then it's valid. Otherwise you can return the $errorMessage message as string
* If $error is empty then it's valid. Otherwise, you can return the $errorMessage message as string
*/

return [$errorMessage];
Expand Down
4 changes: 2 additions & 2 deletions php-snippets/ban-email-domains.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 not accepted domains here
* The mentioned domains will be failed to submit the form
Expand All @@ -36,7 +36,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 not accepted domains here
* The mentioned domains will be failed to submit the form
Expand Down
6 changes: 3 additions & 3 deletions php-snippets/checkable-option-restriction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* selected more than 2 times in the previous responses. This is the snippet for you.
*
* This example is for checkbox. For other inputs just replace the filter name
* radio: fluentform_rendering_field_data_input_radio
* select/dropdown: fluentform_rendering_field_data_select
* radio: fluentform/rendering_field_data_input_radio
* select/dropdown: fluentform/rendering_field_data_select
*/
add_filter('fluentform_rendering_field_data_input_checkbox', function ($field, $form) {
add_filter('fluentform/rendering_field_data_input_checkbox', function ($field, $form) {

$formId = 10; // change your form ID
$targetElementName = 'checkbox'; // check with your input name
Expand Down
Loading