PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.2.0
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.2.0
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / form-restriction.php

form-restriction.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 2.2.0, at inc/form-restriction.php

208 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Create new Form with Template and return the form ID.
4 *
5 * @package sureforms.
6 * @since 1.10.1
7 */
8
9 namespace SRFM\Inc;
10
11 use SRFM\Inc\Database\Tables\Entries;
12 use SRFM\Inc\Traits\Get_Instance;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // Exit if accessed directly.
16 }
17
18 /**
19 * Create New Form.
20 *
21 * @since 1.10.1
22 */
23 class Form_Restriction {
24 use Get_Instance;
25
26 /**
27 * Get the restriction settings for a given form.
28 *
29 * @param int $form_id The ID of the form.
30 * @since 1.10.1
31 * @return array Associative array of restriction settings or empty array if invalid.
32 */
33 public static function get_form_restriction_setting( $form_id ) {
34 // Validate the form ID. Must be numeric and non-empty.
35 if ( empty( $form_id ) || ! is_int( $form_id ) ) {
36 return [];
37 }
38
39 // Get the raw restriction meta.
40 $form_restriction_meta = get_post_meta( $form_id, '_srfm_form_restriction', true );
41
42 if ( empty( $form_restriction_meta ) || ! is_string( $form_restriction_meta ) ) {
43 return [];
44 }
45
46 // Decode the meta to an array.
47 $form_restriction = json_decode( $form_restriction_meta, true );
48
49 // Ensure it's a valid array.
50 return is_array( $form_restriction ) ? $form_restriction : [];
51 }
52
53 /**
54 * Check if the form has reached the entry limit.
55 *
56 * @param int $form_id The ID of the form.
57 * @since 1.10.1
58 * @return bool True if form is restricted, false otherwise.
59 */
60 public static function is_form_restricted( $form_id ) {
61
62 if ( empty( $form_id ) || ! is_int( $form_id ) ) {
63 return false; // Invalid form ID.
64 }
65
66 // Check for instant fom preview mode.
67 $srfm_live_mode_data = Helper::get_instant_form_live_data();
68
69 // Skip check in live mode.
70 if (
71 ! empty( $srfm_live_mode_data ) &&
72 is_array( $srfm_live_mode_data ) &&
73 isset( $srfm_live_mode_data['live_mode'] )
74 ) {
75 return false; // Skip check in live mode.
76 }
77
78 // Get parsed restriction settings.
79 $form_restriction = self::get_form_restriction_setting( $form_id );
80
81 // If the form restriction is empty or not an array, or if the status is not set, return false.
82 if ( empty( $form_restriction ) || ! is_array( $form_restriction ) || empty( $form_restriction['status'] ) ) {
83 return apply_filters( 'srfm_is_form_restricted', false, $form_id, $form_restriction, false, false );
84 }
85
86 $has_entries_limit_reached = self::has_entries_limit_reached( $form_id, $form_restriction );
87 $has_time_limit_reached = self::has_time_limit_reached( $form_restriction );
88
89 $conversational_form = get_post_meta( $form_id, '_srfm_conversational_form', true );
90 $is_conversational_form_enabled = is_array( $conversational_form ) && isset( $conversational_form['is_cf_enabled'] ) ? $conversational_form['is_cf_enabled'] : false;
91 if ( ( $has_entries_limit_reached || $has_time_limit_reached ) && $is_conversational_form_enabled ) {
92 add_filter( 'srfm_show_conversational_form_footer', '__return_false' );
93 }
94
95 /**
96 * If the form has reached the entries limit or the time limit, return true.
97 *
98 * @since 1.10.1
99 */
100 return apply_filters(
101 'srfm_is_form_restricted',
102 $has_entries_limit_reached || $has_time_limit_reached,
103 $form_id,
104 $form_restriction,
105 $has_entries_limit_reached,
106 $has_time_limit_reached
107 );
108 }
109
110 /**
111 * Check if the entries limit has been reached for a given form.
112 *
113 * @param int $form_id The ID of the form.
114 * @param array<string, mixed> $form_restriction The form restriction settings.
115 * @since 1.10.1
116 * @return bool True if the entries limit is reached, false otherwise.
117 */
118 public static function has_entries_limit_reached( $form_id, $form_restriction = [] ) {
119
120 if ( ! isset( $form_restriction['maxEntries'] ) || ! is_int( $form_restriction['maxEntries'] ) ) {
121 return false; // Invalid form ID or restriction settings.
122 }
123
124 $max_entries = $form_restriction['maxEntries'];
125 $entries_count = Entries::get_total_entries_by_status( 'all', $form_id );
126
127 if ( ! is_int( $entries_count ) ) {
128 $entries_count = 0; // Ensure entries count is a non-negative integer.
129 }
130
131 return $entries_count >= $max_entries;
132 }
133
134 /**
135 * Check if the time limit has been reached based on the provided time period.
136 *
137 * @param array<string, mixed> $form_restriction The form restriction settings containing date, hours, minutes, and meridiem.
138 *
139 * @since 1.10.1
140 * @return bool True if the time limit has been reached, false otherwise.
141 */
142 public static function has_time_limit_reached( $form_restriction ) {
143
144 // Get date, hours, minutes, meridiem from the form restriction settings.
145 $date = $form_restriction['date'] ?? '';
146 $hours = Helper::get_string_value( $form_restriction['hours'] ?? '12' );
147 $minutes = Helper::get_string_value( $form_restriction['minutes'] ?? '00' );
148 $meridiem = Helper::get_string_value( $form_restriction['meridiem'] ?? 'AM' );
149
150 if ( empty( $date ) || ! is_string( $date ) ) {
151 return false; // No time limit set.
152 }
153
154 // Convert the time period to a timestamp.
155 $date_timestamp = Helper::get_timestamp_from_string( $date, $hours, $minutes, $meridiem );
156
157 $has_time_limit_reached = false;
158
159 // If the timestamp is valid, check if the current time is greater than the time period timestamp.
160 // Ensure the timestamp is a valid integer and not false.
161 if ( false !== $date_timestamp && is_int( $date_timestamp ) ) {
162 $current_time_timestamp = strtotime( current_time( 'mysql' ) );
163 // Check if the current time is greater than the time period timestamp.
164 $has_time_limit_reached = $current_time_timestamp > $date_timestamp;
165 }
166
167 return $has_time_limit_reached;
168 }
169
170 /**
171 * Display the form restriction message.
172 *
173 * @param int $form_id The ID of the form.
174 * @since 1.10.1
175 * @return string|false The HTML markup for the restriction message or false if no restriction is set.
176 */
177 public static function display_form_restriction_message( $form_id ) {
178 // Get parsed restriction settings.
179 $form_restriction = self::get_form_restriction_setting( $form_id );
180
181 // Get the description text.
182 $form_restriction_message = $form_restriction['message'] ?? Translatable::get_default_form_restriction_message();
183
184 $form_restriction_message = apply_filters( 'srfm_form_restriction_message', $form_restriction_message, $form_id, $form_restriction );
185
186 ob_start();
187 ?>
188 <div class="srfm-form-container srfm-form-restriction-wrapper">
189 <div class="srfm-form-restriction-message" role="alert" aria-live="assertive">
190 <span class="srfm-form-restriction-icon" aria-hidden="true">
191 <?php
192 echo wp_kses(
193 Helper::fetch_svg( 'instant-form-warning', 'srfm-form-restriction-icon', 'aria-hidden="true"' ),
194 Helper::$allowed_tags_svg
195 );
196 ?>
197 </span>
198 <p class="srfm-form-restriction-text">
199 <?php echo esc_html( $form_restriction_message ); ?>
200 </p>
201 </div>
202 </div>
203 <?php
204 return ob_get_clean();
205 }
206
207 }
208