Last active
September 18, 2024 06:16
-
-
Save wpmudev-sls/c3bbe05b60f1c744bbb47b4dcadb6496 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Limit submission based on a field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator Pro] Limit maximum submissions based on the amount of a number field | |
* Description: Limit maximum submissions based on the amount of a number field. | |
* Author: Prashant @ WPMUDEV | |
* Task: SLS-6468 | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
add_filter( | |
'forminator_custom_form_submit_errors', | |
function ( $submit_errors, $form_id, $field_data_array ) { | |
// Add your form IDs and field IDs here. | |
$form_ids = array( 2960 => 'number-1', 2961 => 'number-2' ); | |
$submitted_data = wp_list_pluck( $field_data_array, 'value', 'name' ); | |
// Change this to the message that you want to show. | |
$message = 'Registrations closed now.'; | |
if ( in_array( intval( $form_id ), array_keys( $form_ids ), true ) ) { | |
$reg_count = wpmudev_get_reg_count_number( $form_id, $form_ids[ $form_id ] ); | |
if ( ! empty( $reg_count ) ) { | |
$reg_count = $reg_count + $submitted_data[ $form_ids[ $form_id ] ]; | |
if ( $reg_count >= 50 ) { // Limit to 50. | |
$submit_errors[]['submit'] = $message; | |
$GLOBALS['form_reg_error'] = true; | |
} else { | |
$GLOBALS['form_reg_error'] = false; | |
} | |
} | |
} | |
return $submit_errors; | |
}, | |
15, | |
3 | |
); | |
function wpmudev_get_reg_count_number( $form_id, $count_field ) { | |
global $wpdb; | |
$table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY_META ); | |
$entry_table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY ); | |
$sql = "SELECT sum(m.meta_value) FROM {$table_name} m LEFT JOIN {$entry_table_name} e ON(e.entry_id = m.entry_id) WHERE e.form_id = %d AND m.meta_key = %s"; | |
$entry_count = $wpdb->get_var( $wpdb->prepare( $sql, $form_id, $count_field ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
if ( $entry_count ) { | |
return $entry_count; | |
} | |
return false; | |
} | |
add_filter( 'forminator_custom_form_invalid_form_message', 'wpmudev_reg_count_number_msg', 10, 2 ); | |
function wpmudev_reg_count_number_msg( $invalid_form_message, $form_id ) { | |
$form_ids = array( 2960, 2961 ); // Please change the form IDs. | |
if ( ! in_array( intval( $form_id ), $form_ids, true ) ) { | |
return $invalid_form_message; | |
} | |
if ( $GLOBALS['form_reg_error'] ) { | |
$invalid_form_message = __( 'Registrations closed now.', 'forminator' ); | |
} | |
return $invalid_form_message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment