PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.12.0
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.12.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
200 lines 6.1 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 false; // No limit set.
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 /**
90 * If the form has reached the entries limit or the time limit, return true.
91 *
92 * @since 1.10.1
93 */
94 return apply_filters(
95 'srfm_is_form_restricted',
96 $has_entries_limit_reached || $has_time_limit_reached,
97 $form_id,
98 $form_restriction,
99 $has_entries_limit_reached,
100 $has_time_limit_reached
101 );
102 }
103
104 /**
105 * Check if the entries limit has been reached for a given form.
106 *
107 * @param int $form_id The ID of the form.
108 * @param array<string, mixed> $form_restriction The form restriction settings.
109 * @since 1.10.1
110 * @return bool True if the entries limit is reached, false otherwise.
111 */
112 public static function has_entries_limit_reached( $form_id, $form_restriction = [] ) {
113
114 if ( ! isset( $form_restriction['maxEntries'] ) || ! is_int( $form_restriction['maxEntries'] ) ) {
115 return false; // Invalid form ID or restriction settings.
116 }
117
118 $max_entries = $form_restriction['maxEntries'];
119 $entries_count = Entries::get_total_entries_by_status( 'all', $form_id );
120
121 if ( ! is_int( $entries_count ) ) {
122 $entries_count = 0; // Ensure entries count is a non-negative integer.
123 }
124
125 return $entries_count >= $max_entries;
126 }
127
128 /**
129 * Check if the time limit has been reached based on the provided time period.
130 *
131 * @param array<string, mixed> $form_restriction The form restriction settings containing date, hours, minutes, and meridiem.
132 *
133 * @since 1.10.1
134 * @return bool True if the time limit has been reached, false otherwise.
135 */
136 public static function has_time_limit_reached( $form_restriction ) {
137
138 // Get date, hours, minutes, meridiem from the form restriction settings.
139 $date = $form_restriction['date'] ?? '';
140 $hours = Helper::get_string_value( $form_restriction['hours'] ?? '12' );
141 $minutes = Helper::get_string_value( $form_restriction['minutes'] ?? '00' );
142 $meridiem = Helper::get_string_value( $form_restriction['meridiem'] ?? 'AM' );
143
144 if ( empty( $date ) || ! is_string( $date ) ) {
145 return false; // No time limit set.
146 }
147
148 // Convert the time period to a timestamp.
149 $date_timestamp = Helper::get_timestamp_from_string( $date, $hours, $minutes, $meridiem );
150
151 $has_time_limit_reached = false;
152
153 // If the timestamp is valid, check if the current time is greater than the time period timestamp.
154 // Ensure the timestamp is a valid integer and not false.
155 if ( false !== $date_timestamp && is_int( $date_timestamp ) ) {
156 $current_time_timestamp = strtotime( current_time( 'mysql' ) );
157 // Check if the current time is greater than the time period timestamp.
158 $has_time_limit_reached = $current_time_timestamp > $date_timestamp;
159 }
160
161 return $has_time_limit_reached;
162 }
163
164 /**
165 * Display the form restriction message.
166 *
167 * @param int $form_id The ID of the form.
168 * @since 1.10.1
169 * @return string|false The HTML markup for the restriction message or false if no restriction is set.
170 */
171 public static function display_form_restriction_message( $form_id ) {
172 // Get parsed restriction settings.
173 $form_restriction = self::get_form_restriction_setting( $form_id );
174
175 // Get the description text.
176 $form_restriction_message = $form_restriction['message'] ?? Translatable::get_default_form_restriction_message();
177
178 ob_start();
179 ?>
180 <div class="srfm-form-container srfm-form-restriction-wrapper">
181 <div class="srfm-form-restriction-message" role="alert" aria-live="assertive">
182 <span class="srfm-form-restriction-icon" aria-hidden="true">
183 <?php
184 echo wp_kses(
185 Helper::fetch_svg( 'instant-form-warning', 'srfm-form-restriction-icon', 'aria-hidden="true"' ),
186 Helper::$allowed_tags_svg
187 );
188 ?>
189 </span>
190 <p class="srfm-form-restriction-text">
191 <?php echo esc_html( $form_restriction_message ); ?>
192 </p>
193 </div>
194 </div>
195 <?php
196 return ob_get_clean();
197 }
198
199 }
200