class-redux-ajax-save.php (6010B)
1 <?php 2 /** 3 * Redux AJAX Save Class 4 * 5 * @class Redux_Core 6 * @version 4.0.0 7 * @package Redux Framework/Classes 8 */ 9 10 defined( 'ABSPATH' ) || exit; 11 12 if ( ! class_exists( 'Redux_AJAX_Save', false ) ) { 13 14 /** 15 * Class Redux_AJAX_Save 16 */ 17 class Redux_AJAX_Save extends Redux_Class { 18 19 /** 20 * Redux_AJAX_Save constructor. 21 * array_merge_recursive_distinct 22 * 23 * @param object $parent ReduxFrameword object. 24 */ 25 public function __construct( $parent ) { 26 parent::__construct( $parent ); 27 28 add_action( 'wp_ajax_' . $this->args['opt_name'] . '_ajax_save', array( $this, 'save' ) ); 29 } 30 31 /** 32 * AJAX callback to save the option panel values. 33 */ 34 public function save() { 35 $core = $this->core(); 36 37 if ( ! isset( $_REQUEST['nonce'] ) || ( isset( $_REQUEST['nonce'] ) && ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['nonce'] ) ), 'redux_ajax_nonce' . $this->args['opt_name'] ) ) ) { 38 echo wp_json_encode( 39 array( 40 'status' => esc_html__( 'Invalid security credential. Please reload the page and try again.', 'redux-framework' ), 41 'action' => '', 42 ) 43 ); 44 die(); 45 } 46 47 if ( ! Redux_Helpers::current_user_can( $core->args['page_permissions'] ) ) { 48 echo wp_json_encode( 49 array( 50 'status' => esc_html__( 'Invalid user capability. Please reload the page and try again.', 'redux-framework' ), 51 'action' => '', 52 ) 53 ); 54 die(); 55 } 56 57 if ( isset( $_POST['opt_name'] ) && ! empty( $_POST['opt_name'] ) && isset( $_POST['data'] ) && ! empty( $_POST['data'] ) ) { 58 $redux = Redux::instance( sanitize_text_field( wp_unslash( $_POST['opt_name'] ) ) ); 59 60 if ( ! empty( $redux->args['opt_name'] ) ) { 61 62 $post_data = wp_unslash( $_POST['data'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput 63 64 // New method to avoid input_var nonsense. Thanks, @harunbasic. 65 $values = Redux_Functions_Ex::parse_str( $post_data ); 66 $values = $values[ $redux->args['opt_name'] ]; 67 68 if ( ! empty( $values ) ) { 69 try { 70 if ( isset( $redux->validation_ran ) ) { 71 unset( $redux->validation_ran ); 72 } 73 74 $redux->options_class->set( $redux->options_class->validate_options( $values ) ); 75 76 $do_reload = false; 77 if ( isset( $core->required_class->reload_fields ) && ! empty( $core->required_class->reload_fields ) ) { 78 if ( ! empty( $core->transients['changed_values'] ) ) { 79 foreach ( $core->required_class->reload_fields as $idx => $val ) { 80 if ( array_key_exists( $val, $core->transients['changed_values'] ) ) { 81 $do_reload = true; 82 } 83 } 84 } 85 } 86 87 if ( $do_reload || ( isset( $values['defaults'] ) && ! empty( $values['defaults'] ) ) || ( isset( $values['defaults-section'] ) && ! empty( $values['defaults-section'] ) ) || ( isset( $values['import_code'] ) && ! empty( $values['import_code'] ) ) || ( isset( $values['import_link'] ) && ! empty( $values['import_link'] ) ) ) { 88 echo wp_json_encode( 89 array( 90 'status' => 'success', 91 'action' => 'reload', 92 ) 93 ); 94 die(); 95 } 96 97 $redux->enqueue_class->get_warnings_and_errors_array(); 98 99 $return_array = array( 100 'status' => 'success', 101 'options' => $redux->options, 102 'errors' => $redux->enqueue_class->localize_data['errors'] ?? null, 103 'warnings' => $redux->enqueue_class->localize_data['warnings'] ?? null, 104 'sanitize' => $redux->enqueue_class->localize_data['sanitize'] ?? null, 105 ); 106 } catch ( Exception $e ) { 107 $return_array = array( 'status' => $e->getMessage() ); 108 } 109 } else { 110 echo wp_json_encode( 111 array( 112 'status' => esc_html__( 'Your panel has no fields. Nothing to save.', 'redux-framework' ), 113 ) 114 ); 115 die(); 116 } 117 } 118 } 119 120 if ( isset( $core->transients['run_compiler'] ) && $core->transients['run_compiler'] ) { 121 $core->no_output = true; 122 $temp = $core->args['output_variables_prefix']; 123 124 // Allow the override of variable's prefix for use by SCSS or LESS. 125 if ( isset( $core->args['compiler_output_variables_prefix'] ) ) { 126 $core->args['output_variables_prefix'] = $core->args['compiler_output_variables_prefix']; 127 } 128 129 $core->output_class->enqueue(); 130 $core->args['output_variables_prefix'] = $temp; 131 132 try { 133 134 // phpcs:ignore WordPress.NamingConventions.ValidVariableName 135 $compiler_css = $core->compilerCSS; // Backward compatibility variable. 136 137 /** 138 * Action 'redux/options/{opt_name}/compiler' 139 * 140 * @param array options 141 * @param string CSS that get sent to the compiler hook 142 */ 143 144 // phpcs:ignore WordPress.NamingConventions.ValidHookName 145 do_action( 'redux/options/' . $core->args['opt_name'] . '/compiler', $core->options, $compiler_css, $core->transients['changed_values'], $core->output_variables ); 146 147 /** 148 * Action 'redux/options/{opt_name}/compiler/advanced' 149 * 150 * @param array options 151 * @param string CSS that get sent to the compiler hook, which sends the full Redux object 152 */ 153 154 // phpcs:ignore WordPress.NamingConventions.ValidHookName 155 do_action( 'redux/options/' . $core->args['opt_name'] . '/compiler/advanced', $core ); 156 } catch ( Exception $e ) { 157 $return_array = array( 'status' => $e->getMessage() ); 158 } 159 160 unset( $core->transients['run_compiler'] ); 161 $core->transient_class->set(); 162 } 163 164 if ( isset( $return_array ) ) { 165 if ( 'success' === $return_array['status'] ) { 166 $panel = new Redux_Panel( $redux ); 167 ob_start(); 168 $panel->notification_bar(); 169 $notification_bar = ob_get_contents(); 170 ob_end_clean(); 171 $return_array['notification_bar'] = $notification_bar; 172 } 173 174 // phpcs:ignore WordPress.NamingConventions.ValidHookName 175 echo wp_json_encode( apply_filters( 'redux/options/' . $core->args['opt_name'] . '/ajax_save/response', $return_array ) ); 176 } 177 178 die(); 179 } 180 } 181 }