$form_restriction The form restriction settings. * @since 1.10.1 * @return bool True if the entries limit is reached, false otherwise. */ public static function has_entries_limit_reached( $form_id, $form_restriction = [] ) { if ( ! isset( $form_restriction['maxEntries'] ) || ! is_int( $form_restriction['maxEntries'] ) ) { return false; // Invalid form ID or restriction settings. } $max_entries = $form_restriction['maxEntries']; $entries_count = Entries::get_total_entries_by_status( 'all', $form_id ); if ( ! is_int( $entries_count ) ) { $entries_count = 0; // Ensure entries count is a non-negative integer. } /** * Filter the count of entries used to evaluate the Maximum Number of Entries * cap. Allows extensions (e.g. SureForms Pro's Recurring Entry Limit) to * substitute a window-scoped count — entries since the start of today / * the current week / month / year — in place of the lifetime count. * * Returning a value greater than `$entries_count` will trip the cap sooner; * returning a smaller value will defer it. Non-integer return values are * coerced back to a non-negative integer. * * @param int $entries_count Lifetime entry count for the form. * @param int $form_id The ID of the form. * @param array $form_restriction The form restriction settings. * @since 2.8.2 */ $entries_count = apply_filters( 'srfm_form_restriction_entries_count', $entries_count, $form_id, $form_restriction ); // Always coerce to a non-negative integer — covers both non-int returns // from a misbehaving extension AND negative-int returns that would // otherwise silently disable the cap (e.g. -5 >= 100 is false). $entries_count = max( 0, (int) $entries_count ); return $entries_count >= $max_entries; } /** * Check if the form is outside its scheduled time period. * * @param array $form_restriction The form restriction settings. * @since 2.4.0 * @return bool True if form is outside schedule, false otherwise. */ public static function is_form_outside_schedule( $form_restriction ) { $scheduling_state = self::get_form_scheduling_state( $form_restriction ); return 'not_started' === $scheduling_state || 'ended' === $scheduling_state; } /** * Get the appropriate restriction message based on scheduling state. * * @param string $scheduling_state The scheduling state ('not_started', 'ended', 'active', or 'disabled'). * @param array $form_restriction The form restriction settings. * @since 2.4.0 * @return string The appropriate restriction message. */ public static function get_restriction_message_by_state( $scheduling_state, $form_restriction ) { if ( 'not_started' === $scheduling_state ) { $message = $form_restriction['schedulingNotStartedMessage'] ?? __( 'This form is not yet available. Check back after the scheduled start time.', 'sureforms' ); return is_string( $message ) ? $message : __( 'This form is not yet available. Check back after the scheduled start time.', 'sureforms' ); } if ( 'ended' === $scheduling_state ) { $message = $form_restriction['schedulingEndedMessage'] ?? __( 'This form is no longer accepting submissions. The submission period has ended.', 'sureforms' ); return is_string( $message ) ? $message : __( 'This form is no longer accepting submissions. The submission period has ended.', 'sureforms' ); } // Default to entry limit message. $message = $form_restriction['message'] ?? Translatable::get_default_form_restriction_message(); return is_string( $message ) ? $message : Translatable::get_default_form_restriction_message(); } /** * Get the scheduling state for a form. * * @param array $form_restriction The form restriction settings. * @since 2.4.0 * @return string 'not_started', 'ended', 'active', or 'disabled'. */ public static function get_form_scheduling_state( $form_restriction ) { $scheduling_status = $form_restriction['schedulingStatus'] ?? false; // Start date/time. $start_date = $form_restriction['startDate'] ?? ''; $start_hours = Helper::get_string_value( $form_restriction['startHours'] ?? '12' ); $start_minutes = Helper::get_string_value( $form_restriction['startMinutes'] ?? '00' ); $start_meridiem = Helper::get_string_value( $form_restriction['startMeridiem'] ?? 'AM' ); // End date/time. $end_date = $form_restriction['date'] ?? ''; $end_hours = Helper::get_string_value( $form_restriction['hours'] ?? '12' ); $end_minutes = Helper::get_string_value( $form_restriction['minutes'] ?? '00' ); $end_meridiem = Helper::get_string_value( $form_restriction['meridiem'] ?? 'PM' ); $dt = new \DateTime( 'now', wp_timezone() ); $current_timestamp = $dt->getTimestamp(); // Check if before start time (only if scheduling is enabled). if ( $scheduling_status && ! empty( $start_date ) && is_string( $start_date ) ) { $start_timestamp = Helper::get_timestamp_from_string( $start_date, $start_hours, $start_minutes, $start_meridiem ); if ( false !== $start_timestamp && is_int( $start_timestamp ) && $current_timestamp < $start_timestamp ) { return 'not_started'; } } // Check if after end time (works for both scheduling and simple time limit). if ( $scheduling_status && ! empty( $end_date ) && is_string( $end_date ) ) { $end_timestamp = Helper::get_timestamp_from_string( $end_date, $end_hours, $end_minutes, $end_meridiem ); if ( false !== $end_timestamp && is_int( $end_timestamp ) && $current_timestamp > $end_timestamp ) { return 'ended'; } } // If scheduling is disabled but we haven't passed the end date, return 'disabled'. // If scheduling is enabled and we're within the window, return 'active'. return $scheduling_status ? 'active' : 'disabled'; } /** * Display the form restriction message. * * @param int $form_id The ID of the form. * @since 1.10.1 * @return string|false The HTML markup for the restriction message or false if no restriction is set. */ public static function display_form_restriction_message( $form_id ) { // Get parsed restriction settings. $form_restriction = self::get_form_restriction_setting( $form_id ); // Get the scheduling state and appropriate message. $scheduling_state = self::get_form_scheduling_state( $form_restriction ); $form_restriction_message = self::get_restriction_message_by_state( $scheduling_state, $form_restriction ); // Translate the restriction message via the active multilingual provider // (no-op when no WPML/Polylang is installed). $form_restriction_message = String_Translator::get_instance()->translate_restriction_message( (int) $form_id, $form_restriction_message ); $form_restriction_message = apply_filters( 'srfm_form_restriction_message', $form_restriction_message, $form_id, $form_restriction ); ob_start(); ?>