partials
1 month ago
class-strong-testimonials-forms.php
1 month ago
class-strong-testimonials-general-settings-react.php
1 week ago
class-strong-testimonials-settings-form.php
1 month ago
class-strong-testimonials-settings-form.php
259 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Strong_Testimonials_Settings_Form |
| 4 | */ |
| 5 | class Strong_Testimonials_Settings_Form { |
| 6 | |
| 7 | const TAB_NAME = 'form'; |
| 8 | |
| 9 | const OPTION_NAME = 'wpmtst_form_options'; |
| 10 | |
| 11 | const GROUP_NAME = 'wpmtst-form-group'; |
| 12 | |
| 13 | /** |
| 14 | * Strong_Testimonials_Settings_Form constructor. |
| 15 | */ |
| 16 | public function __construct() {} |
| 17 | |
| 18 | /** |
| 19 | * Initialize. |
| 20 | */ |
| 21 | public static function init() { |
| 22 | self::add_actions(); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Add actions and filters. |
| 27 | */ |
| 28 | public static function add_actions() { |
| 29 | add_action( 'admin_init', array( __CLASS__, 'register_settings' ) ); |
| 30 | add_action( 'wpmtst_form_tabs', array( __CLASS__, 'register_tab' ), 2, 2 ); |
| 31 | add_filter( 'wpmtst_form_callbacks', array( __CLASS__, 'register_settings_page' ) ); |
| 32 | |
| 33 | add_action( 'wp_ajax_wpmtst_restore_default_messages', array( __CLASS__, 'restore_default_messages_function' ) ); |
| 34 | add_action( 'wp_ajax_wpmtst_restore_default_message', array( __CLASS__, 'restore_default_message_function' ) ); |
| 35 | |
| 36 | add_action( 'wp_ajax_wpmtst_add_recipient', array( __CLASS__, 'add_recipient' ) ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Register settings tab. |
| 41 | * |
| 42 | * @param $active_tab |
| 43 | * @param $url |
| 44 | */ |
| 45 | public static function register_tab( $active_tab, $url ) { |
| 46 | printf( |
| 47 | '<a href="%s" class="nav-tab %s">%s</a>', |
| 48 | esc_url( add_query_arg( 'tab', self::TAB_NAME, $url ) ), |
| 49 | esc_attr( self::TAB_NAME === $active_tab ? 'nav-tab-active' : '' ), |
| 50 | esc_html__( 'Settings', 'strong-testimonials' ) |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Register settings. |
| 56 | */ |
| 57 | public static function register_settings() { |
| 58 | register_setting( self::GROUP_NAME, self::OPTION_NAME, array( __CLASS__, 'sanitize_options' ) ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Register settings page. |
| 63 | * |
| 64 | * @param $pages |
| 65 | * |
| 66 | * @return mixed |
| 67 | */ |
| 68 | public static function register_settings_page( $pages ) { |
| 69 | $pages[ self::TAB_NAME ] = array( __CLASS__, 'settings_page' ); |
| 70 | return $pages; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Print settings page. |
| 75 | */ |
| 76 | public static function settings_page() { |
| 77 | settings_fields( self::GROUP_NAME ); |
| 78 | include WPMTST_ADMIN . 'settings/partials/form.php'; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Sanitize settings. |
| 83 | * |
| 84 | * @param $input |
| 85 | * |
| 86 | * @return array |
| 87 | */ |
| 88 | public static function sanitize_options( $input ) { |
| 89 | $input['post_status'] = sanitize_text_field( $input['post_status'] ); |
| 90 | $input['admin_notify'] = wpmtst_sanitize_checkbox( $input, 'admin_notify' ); |
| 91 | $input['mail_queue'] = wpmtst_sanitize_checkbox( $input, 'mail_queue' ); |
| 92 | $input['sender_name'] = sanitize_text_field( $input['sender_name'] ); |
| 93 | $input['sender_site_email'] = intval( $input['sender_site_email'] ); |
| 94 | $input['sender_email'] = sanitize_email( $input['sender_email'] ); |
| 95 | if ( ! $input['sender_email'] && ! $input['sender_site_email'] ) { |
| 96 | $input['sender_site_email'] = true; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Multiple recipients. |
| 101 | * |
| 102 | * @since 1.18 |
| 103 | */ |
| 104 | $new_recipients = array(); |
| 105 | foreach ( $input['recipients'] as $recipient ) { |
| 106 | |
| 107 | if ( isset( $recipient['primary'] ) ) { |
| 108 | $recipient['primary'] = true; |
| 109 | if ( isset( $recipient['admin_site_email'] ) && ! $recipient['admin_site_email'] ) { |
| 110 | if ( ! $recipient['admin_email'] ) { |
| 111 | $recipient['admin_site_email'] = true; |
| 112 | } |
| 113 | } |
| 114 | } else { |
| 115 | // Don't save if both fields are empty. |
| 116 | if ( ! isset( $recipient['admin_name'] ) && ! isset( $recipient['admin_email'] ) ) { |
| 117 | continue; |
| 118 | } |
| 119 | if ( ! $recipient['admin_name'] && ! $recipient['admin_email'] ) { |
| 120 | continue; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if ( isset( $recipient['admin_name'] ) ) { |
| 125 | $recipient['admin_name'] = sanitize_text_field( $recipient['admin_name'] ); |
| 126 | } |
| 127 | |
| 128 | if ( isset( $recipient['admin_email'] ) ) { |
| 129 | $recipient['admin_email'] = sanitize_email( $recipient['admin_email'] ); |
| 130 | } |
| 131 | |
| 132 | $new_recipients[] = $recipient; |
| 133 | |
| 134 | } |
| 135 | $input['recipients'] = $new_recipients; |
| 136 | |
| 137 | $input['default_recipient'] = maybe_unserialize( $input['default_recipient'] ); |
| 138 | $input['email_subject'] = isset( $input['email_subject'] ) ? wp_kses_post( trim( $input['email_subject'] ) ) : ''; |
| 139 | $input['email_message'] = isset( $input['email_message'] ) ? wp_kses_post( rtrim( $input['email_message'] ) ) : ''; |
| 140 | |
| 141 | foreach ( $input['messages'] as $key => $message ) { |
| 142 | if ( 'submission-success' === $key ) { |
| 143 | $input['messages'][ $key ]['text'] = $message['text']; |
| 144 | } else { |
| 145 | if ( 'required-field' === $key ) { |
| 146 | $input['messages'][ $key ]['enabled'] = wpmtst_sanitize_checkbox( $input['messages'][ $key ], 'enabled' ); |
| 147 | } |
| 148 | $input['messages'][ $key ]['text'] = wp_kses_data( $message['text'] ); |
| 149 | } |
| 150 | } |
| 151 | $input['scrolltop_error'] = wpmtst_sanitize_checkbox( $input, 'scrolltop_error' ); |
| 152 | $input['scrolltop_error_offset'] = intval( sanitize_text_field( $input['scrolltop_error_offset'] ) ); |
| 153 | $input['scrolltop_success'] = wpmtst_sanitize_checkbox( $input, 'scrolltop_success' ); |
| 154 | $input['scrolltop_success_offset'] = intval( sanitize_text_field( $input['scrolltop_success_offset'] ) ); |
| 155 | |
| 156 | /** |
| 157 | * Success redirect |
| 158 | * @since 2.18.0 |
| 159 | */ |
| 160 | |
| 161 | $input['success_action'] = sanitize_text_field( $input['success_action'] ); |
| 162 | |
| 163 | $input['success_redirect_url'] = esc_url_raw( $input['success_redirect_url'] ); |
| 164 | |
| 165 | // Check the "ID or slug" field next |
| 166 | if ( isset( $input['success_redirect_2'] ) && $input['success_redirect_2'] ) { |
| 167 | |
| 168 | // is post ID? |
| 169 | $id = sanitize_text_field( $input['success_redirect_2'] ); |
| 170 | if ( is_numeric( $id ) ) { |
| 171 | if ( ! get_posts( |
| 172 | array( |
| 173 | 'p' => $id, |
| 174 | 'post_type' => array( 'page' ), |
| 175 | 'post_status' => 'publish', |
| 176 | ) |
| 177 | ) ) { |
| 178 | $id = null; |
| 179 | } |
| 180 | } else { |
| 181 | // is post slug? |
| 182 | $target = get_posts( |
| 183 | array( |
| 184 | 'name' => $id, |
| 185 | 'post_type' => array( 'page' ), |
| 186 | 'post_status' => 'publish', |
| 187 | ) |
| 188 | ); |
| 189 | if ( $target ) { |
| 190 | $id = $target[0]->ID; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if ( $id ) { |
| 195 | $input['success_redirect_id'] = $id; |
| 196 | } |
| 197 | } else { |
| 198 | |
| 199 | if ( isset( $input['success_redirect_id'] ) ) { |
| 200 | $input['success_redirect_id'] = (int) sanitize_text_field( $input['success_redirect_id'] ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | unset( $input['success_redirect_2'] ); |
| 205 | //ksort( $input ); |
| 206 | |
| 207 | return apply_filters( 'wpmtst_sanitize_form_options', $input ); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * [Restore Default Messages] Ajax receiver. |
| 212 | * |
| 213 | * @since 1.13 |
| 214 | */ |
| 215 | public static function restore_default_messages_function() { |
| 216 | $default_form_options = Strong_Testimonials_Defaults::get_form_options(); |
| 217 | $messages = $default_form_options['messages']; |
| 218 | echo json_encode( $messages ); |
| 219 | wp_die(); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * [Restore Default] for single message Ajax receiver. |
| 224 | * |
| 225 | * @since 1.13 |
| 226 | */ |
| 227 | public static function restore_default_message_function() { |
| 228 | $input = isset( $_REQUEST['field'] ) ? str_replace( '_', '-', sanitize_text_field( wp_unslash( $_REQUEST['field'] ) ) ) : ''; |
| 229 | $default_form_options = Strong_Testimonials_Defaults::get_form_options(); |
| 230 | $message = $default_form_options['messages'][ $input ]; |
| 231 | echo json_encode( $message ); |
| 232 | wp_die(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * [Add Recipient] Ajax receiver |
| 237 | */ |
| 238 | public static function add_recipient() { |
| 239 | |
| 240 | if ( ! wp_verify_nonce( $_POST['nonce'], 'wpmtst-admin-form-script-nonce' ) ) { //phpcs:ignore |
| 241 | wp_send_json_error( array( 'message' => __( 'Nonce does not exist.', 'strong-testimonials' ) ) ); |
| 242 | die(); |
| 243 | } |
| 244 | |
| 245 | if ( ! current_user_can( 'manage_options' ) ) { |
| 246 | wp_send_json_error( array( 'message' => __( 'Insufficient capabilities.', 'strong-testimonials' ) ) ); |
| 247 | die(); |
| 248 | } |
| 249 | |
| 250 | $key = isset( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : ''; |
| 251 | $form_options = get_option( 'wpmtst_form_options' ); |
| 252 | $recipient = $form_options['default_recipient']; |
| 253 | include WPMTST_ADMIN . 'settings/partials/recipient.php'; |
| 254 | wp_die(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | Strong_Testimonials_Settings_Form::init(); |
| 259 |