abstracts
2 years ago
admin
2 years ago
elementor
4 years ago
export
3 years ago
fields
2 years ago
interfaces
8 years ago
libraries
2 years ago
log-handlers
4 years ago
shortcodes
2 years ago
stats
3 years ago
templates
5 years ago
class-everest-forms.php
2 years ago
class-evf-ajax.php
2 years ago
class-evf-autoloader.php
7 years ago
class-evf-background-updater.php
7 years ago
class-evf-cache-helper.php
6 years ago
class-evf-cron.php
3 years ago
class-evf-deprecated-action-hooks.php
6 years ago
class-evf-deprecated-filter-hooks.php
5 years ago
class-evf-emails.php
2 years ago
class-evf-fields.php
2 years ago
class-evf-form-block.php
4 years ago
class-evf-form-handler.php
3 years ago
class-evf-form-task.php
2 years ago
class-evf-forms-features.php
2 years ago
class-evf-frontend-scripts.php
2 years ago
class-evf-install.php
2 years ago
class-evf-integrations.php
7 years ago
class-evf-log-levels.php
8 years ago
class-evf-logger.php
5 years ago
class-evf-post-types.php
5 years ago
class-evf-privacy.php
6 years ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
4 years ago
class-evf-smart-tags.php
2 years ago
class-evf-template-loader.php
2 years ago
class-evf-validation.php
6 years ago
evf-conditional-functions.php
6 years ago
evf-core-functions.php
2 years ago
evf-deprecated-functions.php
6 years ago
evf-entry-functions.php
3 years ago
evf-formatting-functions.php
4 years ago
evf-notice-functions.php
4 years ago
evf-template-functions.php
4 years ago
evf-template-hooks.php
7 years ago
evf-update-functions.php
5 years ago
evf-notice-functions.php
217 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Everest Forms Message Functions |
| 4 | * |
| 5 | * Functions for error/message handling and display. |
| 6 | * |
| 7 | * @package EverestForms/Functions |
| 8 | * @version 1.0.0 |
| 9 | */ |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * Get the count of notices added, either for all notices (default) or for one. |
| 15 | * particular notice type specified by $notice_type. |
| 16 | * |
| 17 | * @since 1.0.0 |
| 18 | * @param string $notice_type Optional. The name of the notice type - either error, success or notice. |
| 19 | * @return int |
| 20 | */ |
| 21 | function evf_notice_count( $notice_type = '' ) { |
| 22 | if ( ! did_action( 'everest_forms_init' ) ) { |
| 23 | evf_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before everest_forms_init.', 'everest-forms' ), '1.0' ); |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | $notice_count = 0; |
| 28 | $all_notices = evf()->session->get( 'evf_notices', array() ); |
| 29 | |
| 30 | if ( isset( $all_notices[ $notice_type ] ) ) { |
| 31 | |
| 32 | $notice_count = count( $all_notices[ $notice_type ] ); |
| 33 | |
| 34 | } elseif ( empty( $notice_type ) ) { |
| 35 | |
| 36 | foreach ( $all_notices as $notices ) { |
| 37 | $notice_count += count( $notices ); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return $notice_count; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Check if a notice has already been added. |
| 46 | * |
| 47 | * @since 1.0.0 |
| 48 | * @param string $message The text to display in the notice. |
| 49 | * @param string $notice_type Optional. The name of the notice type - either error, success or notice. |
| 50 | * @return bool |
| 51 | */ |
| 52 | function evf_has_notice( $message, $notice_type = 'success' ) { |
| 53 | if ( ! did_action( 'everest_forms_init' ) ) { |
| 54 | evf_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before everest_forms_init.', 'everest-forms' ), '1.0' ); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | $notices = evf()->session->get( 'evf_notices', array() ); |
| 59 | $notices = isset( $notices[ $notice_type ] ) ? $notices[ $notice_type ] : array(); |
| 60 | |
| 61 | return array_search( $message, $notices, true ) !== false; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Add and store a notice. |
| 66 | * |
| 67 | * @since 1.0.0 |
| 68 | * @param string $message The text to display in the notice. |
| 69 | * @param string $notice_type Optional. The name of the notice type - either error, success or notice. |
| 70 | */ |
| 71 | function evf_add_notice( $message, $notice_type = 'success' ) { |
| 72 | if ( ! did_action( 'everest_forms_init' ) ) { |
| 73 | evf_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before everest_forms_init.', 'everest-forms' ), '1.0' ); |
| 74 | return; |
| 75 | } |
| 76 | if ( evf_is_amp() ) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | $notices = evf()->session->get( 'evf_notices', array() ); |
| 81 | |
| 82 | // Backward compatibility. |
| 83 | if ( 'success' === $notice_type ) { |
| 84 | $message = apply_filters( 'everest_forms_add_message', $message ); |
| 85 | } |
| 86 | |
| 87 | $notices[ $notice_type ][] = apply_filters( 'everest_forms_add_' . $notice_type, $message ); |
| 88 | |
| 89 | evf()->session->set( 'evf_notices', $notices ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Set all notices at once. |
| 94 | * |
| 95 | * @since 1.0.0 |
| 96 | * @param mixed $notices Array of notices. |
| 97 | */ |
| 98 | function evf_set_notices( $notices ) { |
| 99 | if ( ! did_action( 'everest_forms_init' ) ) { |
| 100 | evf_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before everest_forms_init.', 'everest-forms' ), '1.0' ); |
| 101 | return; |
| 102 | } |
| 103 | evf()->session->set( 'evf_notices', $notices ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Unset all notices. |
| 108 | * |
| 109 | * @since 1.0.0 |
| 110 | */ |
| 111 | function evf_clear_notices() { |
| 112 | if ( ! did_action( 'everest_forms_init' ) ) { |
| 113 | evf_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before everest_forms_init.', 'everest-forms' ), '1.0' ); |
| 114 | return; |
| 115 | } |
| 116 | evf()->session->set( 'evf_notices', null ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Prints messages and errors which are stored in the session, then clears them. |
| 121 | * |
| 122 | * @since 1.0.0 |
| 123 | * |
| 124 | * @param array $form_data Prepared form settings. |
| 125 | */ |
| 126 | function evf_print_notices( $form_data = array() ) { |
| 127 | if ( ! did_action( 'everest_forms_init' ) ) { |
| 128 | evf_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before everest_forms_init.', 'everest-forms' ), '1.0' ); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | $form_id = isset( $form_data['id'] ) ? absint( $form_data['id'] ) : 0; |
| 133 | $all_notices = evf()->session->get( 'evf_notices', array() ); |
| 134 | $notice_types = apply_filters( 'everest_forms_notice_types', array( 'error', 'success', 'notice' ) ); |
| 135 | |
| 136 | // Skips notice print if it isn't the right form. |
| 137 | if ( isset( $_REQUEST['everest_forms']['id'] ) && ( (int) $form_id !== (int) $_REQUEST['everest_forms']['id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | foreach ( $notice_types as $notice_type ) { |
| 142 | if ( evf_notice_count( $notice_type ) > 0 ) { |
| 143 | foreach ( $all_notices[ $notice_type ] as $key => $message ) { |
| 144 | $all_notices[ $notice_type ][ $key ] = evf_string_translation( $form_id, 'notice_message_' . $notice_type, $message ); |
| 145 | } |
| 146 | evf_get_template( |
| 147 | "notices/{$notice_type}.php", |
| 148 | array( |
| 149 | 'messages' => array_filter( $all_notices[ $notice_type ] ), |
| 150 | ) |
| 151 | ); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | evf_clear_notices(); |
| 156 | } |
| 157 | add_action( 'everest_forms_display_fields_before', 'evf_print_notices', 10 ); |
| 158 | |
| 159 | /** |
| 160 | * Print a single notice immediately. |
| 161 | * |
| 162 | * @since 1.0.0 |
| 163 | * @param string $message The text to display in the notice. |
| 164 | * @param string $notice_type Optional. The name of the notice type - either error, success or notice. |
| 165 | */ |
| 166 | function evf_print_notice( $message, $notice_type = 'success' ) { |
| 167 | if ( 'success' === $notice_type ) { |
| 168 | $message = apply_filters( 'everest_forms_add_message', $message ); |
| 169 | } |
| 170 | |
| 171 | evf_get_template( |
| 172 | "notices/{$notice_type}.php", |
| 173 | array( |
| 174 | 'messages' => array( apply_filters( 'everest_forms_add_' . $notice_type, $message ) ), |
| 175 | ) |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Returns all queued notices, optionally filtered by a notice type. |
| 181 | * |
| 182 | * @since 1.0.0 |
| 183 | * @param string $notice_type Optional. The name of the notice type - either error, success or notice. |
| 184 | * @return array|mixed |
| 185 | */ |
| 186 | function evf_get_notices( $notice_type = '' ) { |
| 187 | if ( ! did_action( 'everest_forms_init' ) ) { |
| 188 | evf_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before everest_forms_init.', 'everest-forms' ), '1.0' ); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | $all_notices = evf()->session->get( 'evf_notices', array() ); |
| 193 | |
| 194 | if ( empty( $notice_type ) ) { |
| 195 | $notices = $all_notices; |
| 196 | } elseif ( isset( $all_notices[ $notice_type ] ) ) { |
| 197 | $notices = $all_notices[ $notice_type ]; |
| 198 | } else { |
| 199 | $notices = array(); |
| 200 | } |
| 201 | |
| 202 | return $notices; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Add notices for WP Errors. |
| 207 | * |
| 208 | * @param WP_Error $errors Errors. |
| 209 | */ |
| 210 | function evf_add_wp_error_notices( $errors ) { |
| 211 | if ( is_wp_error( $errors ) && $errors->get_error_messages() ) { |
| 212 | foreach ( $errors->get_error_messages() as $error ) { |
| 213 | evf_add_notice( $error, 'error' ); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 |