@@ -16,791 +16,791 @@ |
||
| 16 | 16 | // Don't duplicate me! |
| 17 | 17 | if ( ! class_exists( 'Redux_Extension_Color_Scheme' ) ) { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Class Redux_Extension_Color_Scheme |
|
| 21 | - */ |
|
| 22 | - class Redux_Extension_Color_Scheme extends Redux_Extension_Abstract { |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Extension version. |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - public static $version = '4.5.10'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Extension friendly name. |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - public string $extension_name = 'Color Schemes'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Field ID. |
|
| 40 | - * |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - public $field_id = ''; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Transparent output bit. |
|
| 47 | - * |
|
| 48 | - * @var bool |
|
| 49 | - */ |
|
| 50 | - public bool $output_transparent = false; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Extension field name. |
|
| 54 | - * |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - public string $field_name = ''; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Class Constructor. Defines the args for the extensions class |
|
| 61 | - * |
|
| 62 | - * @since 1.0.0 |
|
| 63 | - * @access public |
|
| 64 | - * |
|
| 65 | - * @param object $redux Parent settings. |
|
| 66 | - * |
|
| 67 | - * @return void |
|
| 68 | - */ |
|
| 69 | - public function __construct( $redux ) { |
|
| 70 | - parent::__construct( $redux, __FILE__ ); |
|
| 19 | + /** |
|
| 20 | + * Class Redux_Extension_Color_Scheme |
|
| 21 | + */ |
|
| 22 | + class Redux_Extension_Color_Scheme extends Redux_Extension_Abstract { |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Extension version. |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + public static $version = '4.5.10'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Extension friendly name. |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + public string $extension_name = 'Color Schemes'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Field ID. |
|
| 40 | + * |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + public $field_id = ''; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Transparent output bit. |
|
| 47 | + * |
|
| 48 | + * @var bool |
|
| 49 | + */ |
|
| 50 | + public bool $output_transparent = false; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Extension field name. |
|
| 54 | + * |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + public string $field_name = ''; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Class Constructor. Defines the args for the extensions class |
|
| 61 | + * |
|
| 62 | + * @since 1.0.0 |
|
| 63 | + * @access public |
|
| 64 | + * |
|
| 65 | + * @param object $redux Parent settings. |
|
| 66 | + * |
|
| 67 | + * @return void |
|
| 68 | + */ |
|
| 69 | + public function __construct( $redux ) { |
|
| 70 | + parent::__construct( $redux, __FILE__ ); |
|
| 71 | 71 | |
| 72 | - $this->add_field( 'color_scheme' ); |
|
| 73 | - $this->field_name = 'color_scheme'; |
|
| 72 | + $this->add_field( 'color_scheme' ); |
|
| 73 | + $this->field_name = 'color_scheme'; |
|
| 74 | 74 | |
| 75 | - add_filter( "redux/options/{$this->parent->args['opt_name']}/defaults", array( $this, 'set_defaults' ) ); |
|
| 75 | + add_filter( "redux/options/{$this->parent->args['opt_name']}/defaults", array( $this, 'set_defaults' ) ); |
|
| 76 | 76 | |
| 77 | - // Ajax hooks. |
|
| 78 | - add_action( 'wp_ajax_redux_color_schemes', array( $this, 'parse_ajax' ) ); |
|
| 79 | - |
|
| 80 | - // Reset hooks. |
|
| 81 | - add_action( 'redux/validate/' . $this->parent->args['opt_name'] . '/defaults', array( $this, 'reset_defaults' ), 0, 3 ); |
|
| 82 | - add_action( 'redux/validate/' . $this->parent->args['opt_name'] . '/defaults_section', array( $this, 'reset_defaults_section' ), 0, 3 ); |
|
| 83 | - |
|
| 84 | - // Save filter. |
|
| 85 | - add_action( 'redux/validate/' . $this->parent->args['opt_name'] . '/before_validation', array( $this, 'save_hook' ), 0, 3 ); |
|
| 86 | - |
|
| 87 | - // Register hook - to get field id and prep helper. |
|
| 88 | - add_action( 'redux/options/' . $this->parent->args['opt_name'] . '/field/' . $this->field_name . '/register', array( $this, 'register_field' ) ); |
|
| 89 | - |
|
| 90 | - include_once $this->extension_dir . 'color_scheme/inc/class-redux-color-scheme-functions.php'; |
|
| 91 | - Redux_Color_Scheme_Functions::init( $redux ); |
|
| 92 | - |
|
| 93 | - $field = Redux_Color_Scheme_Functions::get_field( $redux ); |
|
| 94 | - |
|
| 95 | - if ( ! is_array( $field ) ) { |
|
| 96 | - return; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $this->field_id = $field['id']; |
|
| 100 | - |
|
| 101 | - // Prep storage. |
|
| 102 | - $upload_dir = Redux_Color_Scheme_Functions::$upload_dir; |
|
| 103 | - |
|
| 104 | - // Create uploads/redux_scheme_colors/ folder. |
|
| 105 | - if ( ! is_dir( $upload_dir ) ) { |
|
| 106 | - Redux_Core::$filesystem->execute( 'mkdir', $upload_dir ); |
|
| 107 | - } |
|
| 108 | - } |
|
| 77 | + // Ajax hooks. |
|
| 78 | + add_action( 'wp_ajax_redux_color_schemes', array( $this, 'parse_ajax' ) ); |
|
| 79 | + |
|
| 80 | + // Reset hooks. |
|
| 81 | + add_action( 'redux/validate/' . $this->parent->args['opt_name'] . '/defaults', array( $this, 'reset_defaults' ), 0, 3 ); |
|
| 82 | + add_action( 'redux/validate/' . $this->parent->args['opt_name'] . '/defaults_section', array( $this, 'reset_defaults_section' ), 0, 3 ); |
|
| 83 | + |
|
| 84 | + // Save filter. |
|
| 85 | + add_action( 'redux/validate/' . $this->parent->args['opt_name'] . '/before_validation', array( $this, 'save_hook' ), 0, 3 ); |
|
| 86 | + |
|
| 87 | + // Register hook - to get field id and prep helper. |
|
| 88 | + add_action( 'redux/options/' . $this->parent->args['opt_name'] . '/field/' . $this->field_name . '/register', array( $this, 'register_field' ) ); |
|
| 89 | + |
|
| 90 | + include_once $this->extension_dir . 'color_scheme/inc/class-redux-color-scheme-functions.php'; |
|
| 91 | + Redux_Color_Scheme_Functions::init( $redux ); |
|
| 92 | + |
|
| 93 | + $field = Redux_Color_Scheme_Functions::get_field( $redux ); |
|
| 94 | + |
|
| 95 | + if ( ! is_array( $field ) ) { |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $this->field_id = $field['id']; |
|
| 100 | + |
|
| 101 | + // Prep storage. |
|
| 102 | + $upload_dir = Redux_Color_Scheme_Functions::$upload_dir; |
|
| 103 | + |
|
| 104 | + // Create uploads/redux_scheme_colors/ folder. |
|
| 105 | + if ( ! is_dir( $upload_dir ) ) { |
|
| 106 | + Redux_Core::$filesystem->execute( 'mkdir', $upload_dir ); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Set default values after reset. |
|
| 112 | - * |
|
| 113 | - * @param array $defaults Default values. |
|
| 114 | - * |
|
| 115 | - * @return array |
|
| 116 | - */ |
|
| 117 | - public function set_defaults( array $defaults = array() ): array { |
|
| 118 | - if ( ! Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
|
| 119 | - return $defaults; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - if ( empty( $this->field_id ) ) { |
|
| 123 | - return $defaults; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $x = get_option( $this->parent->args['opt_name'] ); |
|
| 127 | - $color_opts = $x[ $this->field_id ] ?? array(); |
|
| 128 | - $wrong_format = false; |
|
| 129 | - |
|
| 130 | - if ( ! isset( $color_opts['color_scheme_name'] ) ) { |
|
| 131 | - $wrong_format = true; |
|
| 132 | - |
|
| 133 | - $data = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 134 | - |
|
| 135 | - if ( ! empty( $data ) && isset( $x[ $this->field_id ] ) ) { |
|
| 136 | - $x[ $this->field_id ] = $data; |
|
| 137 | - |
|
| 138 | - update_option( $this->parent->args['opt_name'], $x ); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 143 | - |
|
| 144 | - $ot_val = Redux_Color_Scheme_Functions::get_output_transparent_val(); |
|
| 145 | - $this->output_transparent = $ot_val; |
|
| 146 | - |
|
| 147 | - Redux_Color_Scheme_Functions::convert_to_db(); |
|
| 148 | - |
|
| 149 | - $scheme_key = Redux_Color_Scheme_Functions::get_scheme_key(); |
|
| 150 | - $scheme_data = get_option( $scheme_key ); |
|
| 151 | - |
|
| 152 | - $scheme_data_exists = ! empty( $scheme_data ); |
|
| 153 | - |
|
| 154 | - $default_exists = in_array( 'default', array_map( 'strtolower', Redux_Color_Scheme_Functions::get_scheme_names() ), true ); |
|
| 155 | - |
|
| 156 | - if ( ! $scheme_data_exists || ! $default_exists || $wrong_format ) { |
|
| 157 | - $data = $this->get_default_data(); |
|
| 158 | - |
|
| 159 | - // Add to (and/or create) JSON scheme file. |
|
| 160 | - Redux_Color_Scheme_Functions::set_scheme_data( 'Default', $data ); |
|
| 161 | - |
|
| 162 | - // Set default scheme. |
|
| 163 | - Redux_Color_Scheme_Functions::set_current_scheme_id( 'Default' ); |
|
| 164 | - |
|
| 165 | - $data = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 166 | - |
|
| 167 | - $this->parent->options[ $this->field_id ] = $data; |
|
| 168 | - |
|
| 169 | - $defaults[ $this->field_id ] = $data; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - return $defaults; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Field Register. Sets the whole smash up. |
|
| 177 | - * |
|
| 178 | - * @param array $data Field data. |
|
| 179 | - * |
|
| 180 | - * @return array |
|
| 181 | - * @since 1.0.0 |
|
| 182 | - * @access public |
|
| 183 | - */ |
|
| 184 | - public function register_field( array $data ): array { |
|
| 185 | - |
|
| 186 | - // Include color_scheme helper. |
|
| 187 | - include_once $this->extension_dir . 'color_scheme/inc/class-redux-color-scheme-functions.php'; |
|
| 188 | - |
|
| 189 | - if ( isset( $data['output_transparent'] ) ) { |
|
| 190 | - $this->output_transparent = $data['output_transparent']; |
|
| 191 | - } |
|
| 110 | + /** |
|
| 111 | + * Set default values after reset. |
|
| 112 | + * |
|
| 113 | + * @param array $defaults Default values. |
|
| 114 | + * |
|
| 115 | + * @return array |
|
| 116 | + */ |
|
| 117 | + public function set_defaults( array $defaults = array() ): array { |
|
| 118 | + if ( ! Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
|
| 119 | + return $defaults; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + if ( empty( $this->field_id ) ) { |
|
| 123 | + return $defaults; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $x = get_option( $this->parent->args['opt_name'] ); |
|
| 127 | + $color_opts = $x[ $this->field_id ] ?? array(); |
|
| 128 | + $wrong_format = false; |
|
| 129 | + |
|
| 130 | + if ( ! isset( $color_opts['color_scheme_name'] ) ) { |
|
| 131 | + $wrong_format = true; |
|
| 132 | + |
|
| 133 | + $data = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 134 | + |
|
| 135 | + if ( ! empty( $data ) && isset( $x[ $this->field_id ] ) ) { |
|
| 136 | + $x[ $this->field_id ] = $data; |
|
| 137 | + |
|
| 138 | + update_option( $this->parent->args['opt_name'], $x ); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 143 | + |
|
| 144 | + $ot_val = Redux_Color_Scheme_Functions::get_output_transparent_val(); |
|
| 145 | + $this->output_transparent = $ot_val; |
|
| 146 | + |
|
| 147 | + Redux_Color_Scheme_Functions::convert_to_db(); |
|
| 148 | + |
|
| 149 | + $scheme_key = Redux_Color_Scheme_Functions::get_scheme_key(); |
|
| 150 | + $scheme_data = get_option( $scheme_key ); |
|
| 151 | + |
|
| 152 | + $scheme_data_exists = ! empty( $scheme_data ); |
|
| 153 | + |
|
| 154 | + $default_exists = in_array( 'default', array_map( 'strtolower', Redux_Color_Scheme_Functions::get_scheme_names() ), true ); |
|
| 155 | + |
|
| 156 | + if ( ! $scheme_data_exists || ! $default_exists || $wrong_format ) { |
|
| 157 | + $data = $this->get_default_data(); |
|
| 158 | + |
|
| 159 | + // Add to (and/or create) JSON scheme file. |
|
| 160 | + Redux_Color_Scheme_Functions::set_scheme_data( 'Default', $data ); |
|
| 161 | + |
|
| 162 | + // Set default scheme. |
|
| 163 | + Redux_Color_Scheme_Functions::set_current_scheme_id( 'Default' ); |
|
| 164 | + |
|
| 165 | + $data = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 166 | + |
|
| 167 | + $this->parent->options[ $this->field_id ] = $data; |
|
| 168 | + |
|
| 169 | + $defaults[ $this->field_id ] = $data; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + return $defaults; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Field Register. Sets the whole smash up. |
|
| 177 | + * |
|
| 178 | + * @param array $data Field data. |
|
| 179 | + * |
|
| 180 | + * @return array |
|
| 181 | + * @since 1.0.0 |
|
| 182 | + * @access public |
|
| 183 | + */ |
|
| 184 | + public function register_field( array $data ): array { |
|
| 185 | + |
|
| 186 | + // Include color_scheme helper. |
|
| 187 | + include_once $this->extension_dir . 'color_scheme/inc/class-redux-color-scheme-functions.php'; |
|
| 188 | + |
|
| 189 | + if ( isset( $data['output_transparent'] ) ) { |
|
| 190 | + $this->output_transparent = $data['output_transparent']; |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - $this->field_id = $data['id']; |
|
| 194 | - Redux_Color_Scheme_Functions::$field_id = $data['id']; |
|
| 195 | - |
|
| 196 | - // Set helper parent object. |
|
| 197 | - Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 198 | - |
|
| 199 | - // Prep storage. |
|
| 200 | - $upload_dir = Redux_Color_Scheme_Functions::$upload_dir; |
|
| 201 | - |
|
| 202 | - // Set upload_dir cookie. |
|
| 203 | - setcookie( 'redux_color_scheme_upload_dir', $upload_dir, 0, '/' ); |
|
| 204 | - |
|
| 205 | - return $data; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Reset defaults. |
|
| 210 | - * |
|
| 211 | - * @param array $defaults Default values. |
|
| 212 | - * |
|
| 213 | - * @return array |
|
| 214 | - */ |
|
| 215 | - public function reset_defaults( array $defaults = array() ): array { |
|
| 216 | - if ( Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
|
| 217 | - // Check if reset_all was fired. |
|
| 218 | - $this->reset_all(); |
|
| 219 | - $defaults[ $this->field_id ] = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 220 | - } |
|
| 193 | + $this->field_id = $data['id']; |
|
| 194 | + Redux_Color_Scheme_Functions::$field_id = $data['id']; |
|
| 195 | + |
|
| 196 | + // Set helper parent object. |
|
| 197 | + Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 198 | + |
|
| 199 | + // Prep storage. |
|
| 200 | + $upload_dir = Redux_Color_Scheme_Functions::$upload_dir; |
|
| 201 | + |
|
| 202 | + // Set upload_dir cookie. |
|
| 203 | + setcookie( 'redux_color_scheme_upload_dir', $upload_dir, 0, '/' ); |
|
| 204 | + |
|
| 205 | + return $data; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Reset defaults. |
|
| 210 | + * |
|
| 211 | + * @param array $defaults Default values. |
|
| 212 | + * |
|
| 213 | + * @return array |
|
| 214 | + */ |
|
| 215 | + public function reset_defaults( array $defaults = array() ): array { |
|
| 216 | + if ( Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
|
| 217 | + // Check if reset_all was fired. |
|
| 218 | + $this->reset_all(); |
|
| 219 | + $defaults[ $this->field_id ] = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | - return $defaults; |
|
| 223 | - } |
|
| 222 | + return $defaults; |
|
| 223 | + } |
|
| 224 | 224 | |
| 225 | - /** |
|
| 226 | - * Reset section defaults. |
|
| 227 | - * |
|
| 228 | - * @param array $defaults Default values. |
|
| 229 | - * |
|
| 230 | - * @return array |
|
| 231 | - */ |
|
| 232 | - public function reset_defaults_section( array $defaults = array() ): array { |
|
| 233 | - if ( Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
|
| 234 | - // Get the current tab/section number. |
|
| 235 | - if ( isset( $_COOKIE['redux_current_tab'] ) ) { |
|
| 236 | - $cur_tab = sanitize_text_field( wp_unslash( $_COOKIE['redux_current_tab'] ) ); |
|
| 237 | - |
|
| 238 | - // Get the tab/section number field is used on. |
|
| 239 | - $tab_num = $this->parent->field_sections['color_scheme'][ $this->field_id ]; |
|
| 240 | - |
|
| 241 | - // Match... |
|
| 242 | - if ( $cur_tab === $tab_num ) { |
|
| 243 | - |
|
| 244 | - // Reset data. |
|
| 245 | - $this->reset_all(); |
|
| 246 | - } |
|
| 247 | - $defaults[ $this->field_id ] = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - return $defaults; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Save Changes Hook. What to do when changes are saved |
|
| 256 | - * |
|
| 257 | - * @param array $saved_options Saved data. |
|
| 258 | - * @param array $old_options Previous data. |
|
| 259 | - * |
|
| 260 | - * @return array |
|
| 261 | - * @since 1.0.0 |
|
| 262 | - * @access public |
|
| 263 | - */ |
|
| 264 | - public function save_hook( array $saved_options = array(), array $old_options = array() ): array { |
|
| 265 | - if ( ! isset( $saved_options[ $this->field_id ] ) || empty( $saved_options[ $this->field_id ] ) || ( is_array( $saved_options[ $this->field_id ] ) && $old_options === $saved_options ) || ! array_key_exists( $this->field_id, $saved_options ) ) { |
|
| 266 | - return $saved_options; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - // We'll use the reset hook instead. |
|
| 270 | - if ( ! empty( $saved_options['defaults'] ) || ! empty( $saved_options['defaults-section'] ) ) { |
|
| 271 | - return $saved_options; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - $first_value = reset( $saved_options[ $this->field_id ] ); // First Element's Value. |
|
| 275 | - |
|
| 276 | - // Parse the JSON to an array. |
|
| 277 | - if ( isset( $first_value['data'] ) ) { |
|
| 278 | - |
|
| 279 | - Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 280 | - Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 281 | - |
|
| 282 | - Redux_Color_Scheme_Functions::set_current_scheme_id( $saved_options['redux-scheme-select'] ); |
|
| 283 | - |
|
| 284 | - // Get the current field ID. |
|
| 285 | - $raw_data = $saved_options[ $this->field_id ]; |
|
| 286 | - |
|
| 287 | - // Create a new array. |
|
| 288 | - $save_data = array(); |
|
| 289 | - |
|
| 290 | - // Enum through saved data. |
|
| 291 | - foreach ( $raw_data as $id => $val ) { |
|
| 292 | - |
|
| 293 | - if ( 'color_scheme_name' !== $id ) { |
|
| 294 | - if ( is_array( $val ) ) { |
|
| 295 | - |
|
| 296 | - if ( ! isset( $val['data'] ) ) { |
|
| 297 | - continue; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - $data = json_decode( rawurldecode( $val['data'] ), true ); |
|
| 301 | - |
|
| 302 | - // Sanitize everything. |
|
| 303 | - $color = $data['color'] ?? ''; |
|
| 304 | - $alpha = $data['alpha'] ?? 1; |
|
| 305 | - |
|
| 306 | - $id = $data['id'] ?? $id; |
|
| 307 | - $title = $data['title'] ?? $id; |
|
| 308 | - |
|
| 309 | - $grp = $data['group'] ?? ''; |
|
| 310 | - |
|
| 311 | - if ( '' === $color || 'transparent' === $color ) { |
|
| 312 | - $rgba = $this->output_transparent ? 'transparent' : ''; |
|
| 313 | - } else { |
|
| 314 | - $rgba = Redux_Helpers::hex2rgba( $color, $alpha ); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - // Create an array of saved data. |
|
| 318 | - $save_data[] = array( |
|
| 319 | - 'id' => $id, |
|
| 320 | - 'title' => $title, |
|
| 321 | - 'color' => $color, |
|
| 322 | - 'alpha' => $alpha, |
|
| 323 | - 'group' => $grp, |
|
| 324 | - 'rgba' => $rgba, |
|
| 325 | - ); |
|
| 326 | - } else { |
|
| 327 | - $save_data[] = array( |
|
| 328 | - 'id' => $id, |
|
| 329 | - 'value' => $val, |
|
| 330 | - 'type' => 'select', |
|
| 331 | - ); |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - $new_scheme = array(); |
|
| 337 | - |
|
| 338 | - $new_scheme['color_scheme_name'] = Redux_Color_Scheme_Functions::get_current_scheme_id(); |
|
| 339 | - |
|
| 340 | - // Enum through values and assign them to a new array. |
|
| 341 | - foreach ( $save_data as $val ) { |
|
| 342 | - if ( isset( $val['id'] ) ) { |
|
| 343 | - $new_scheme[ $val['id'] ] = $val; |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - // Filter for DB save |
|
| 348 | - // Doesn't need to save select arrays to a database, |
|
| 349 | - // just the id => value. |
|
| 350 | - $database_data = $new_scheme; |
|
| 351 | - |
|
| 352 | - foreach ( $database_data as $k => $v ) { |
|
| 353 | - if ( isset( $v['type'] ) ) { |
|
| 354 | - $val = $v['value']; |
|
| 355 | - |
|
| 356 | - unset( $database_data[ $k ] ); |
|
| 357 | - |
|
| 358 | - $database_data[ $k ] = $val; |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - $saved_options[ $this->field_id ] = $database_data; |
|
| 363 | - |
|
| 364 | - // Check if we should save this compared to the old data. |
|
| 365 | - $save_scheme = false; |
|
| 366 | - |
|
| 367 | - // Doesn't exist or is empty. |
|
| 368 | - if ( ! isset( $old_options[ $this->field_id ] ) || ( isset( $old_options[ $this->field_id ] ) && ! empty( $old_options[ $this->field_id ] ) ) ) { |
|
| 369 | - $save_scheme = true; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - // Isn't empty and isn't the same as the new array. |
|
| 373 | - if ( ! empty( $old_options[ $this->field_id ] ) && $saved_options[ $this->field_id ] !== $old_options[ $this->field_id ] ) { |
|
| 374 | - $save_scheme = true; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - if ( $save_scheme ) { |
|
| 378 | - $scheme = Redux_Color_Scheme_Functions::get_current_scheme_id(); |
|
| 379 | - Redux_Color_Scheme_Functions::set_scheme_data( $scheme, $save_data ); |
|
| 380 | - } |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - return $saved_options; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Reset data. Restores colour picker to default values |
|
| 388 | - * |
|
| 389 | - * @since 1.0.0 |
|
| 390 | - * @access private |
|
| 391 | - * @return void |
|
| 392 | - */ |
|
| 393 | - private function reset_data() { |
|
| 394 | - Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 395 | - Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 396 | - |
|
| 397 | - // Get default data. |
|
| 398 | - $data = $this->get_default_data(); |
|
| 399 | - |
|
| 400 | - // Add to (and/or create) JSON scheme file. |
|
| 401 | - Redux_Color_Scheme_Functions::set_scheme_data( 'Default', $data ); |
|
| 402 | - |
|
| 403 | - // Set default scheme. |
|
| 404 | - Redux_Color_Scheme_Functions::set_current_scheme_id( 'Default' ); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Reset All Hook. Todo list when all data is reset |
|
| 409 | - * |
|
| 410 | - * @return void |
|
| 411 | - * @since 1.0.0 |
|
| 412 | - * @access public |
|
| 413 | - */ |
|
| 414 | - public function reset_all() { |
|
| 415 | - if ( ! empty( $this->field_id ) && isset( $this->parent->options_defaults[ $this->field_id ] ) && ! empty( $this->parent->options_defaults[ $this->field_id ] ) ) { |
|
| 416 | - Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 417 | - Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 418 | - |
|
| 419 | - $this->reset_data(); |
|
| 420 | - } |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * AJAX evaluator. Determine course of action based on AJAX callback |
|
| 425 | - * |
|
| 426 | - * @since 1.0.0 |
|
| 427 | - * @access public |
|
| 428 | - * @return void |
|
| 429 | - */ |
|
| 430 | - public function parse_ajax() { |
|
| 431 | - if ( ! is_user_logged_in() && ! is_admin() && ! current_user_can( $this->parent->args['page_permissions'] ) ) { |
|
| 432 | - wp_die( esc_html__( 'You do not have permission to perform this action.', 'redux-framework' ) ); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - $opt_name = $this->parent->args['opt_name']; |
|
| 436 | - $parent = $this->parent; |
|
| 437 | - |
|
| 438 | - $nonce = isset( $_REQUEST['nonce'] ) ? sanitize_key( wp_unslash( $_REQUEST['nonce'] ) ) : ''; |
|
| 439 | - if ( ! wp_verify_nonce( $nonce, 'redux_' . $opt_name . '_color_schemes' ) ) { |
|
| 440 | - wp_die( esc_html__( 'Invalid Security Credentials. Please reload the page and try again.', 'redux-framework' ) ); |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - $type = isset( $_REQUEST['type'] ) ? sanitize_key( wp_unslash( $_REQUEST['type'] ) ) : ''; |
|
| 444 | - if ( ! in_array( $type, array( 'save', 'delete', 'update', 'export', 'import' ), true ) ) { |
|
| 445 | - wp_die( esc_html__( 'Invalid request.', 'redux-framework' ) ); |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - if ( 'save' === $type ) { |
|
| 449 | - $this->save_scheme( $parent ); |
|
| 450 | - } elseif ( 'delete' === $type ) { |
|
| 451 | - $this->delete_scheme( $parent ); |
|
| 452 | - } elseif ( 'update' === $type ) { |
|
| 453 | - $this->get_scheme_html( $parent ); |
|
| 454 | - } elseif ( 'export' === $type ) { |
|
| 455 | - $this->download_schemes(); |
|
| 456 | - } elseif ( 'import' === $type ) { |
|
| 457 | - $this->import_schemes(); |
|
| 458 | - } |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * Download Scheme File. |
|
| 463 | - * |
|
| 464 | - * @since 4.4.18 |
|
| 465 | - * @access private |
|
| 466 | - * @return void |
|
| 467 | - */ |
|
| 468 | - private function import_schemes() { |
|
| 469 | - if ( isset( $_REQUEST['content'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 470 | - $opt_name = Redux_Color_Scheme_Functions::$parent->args['opt_name']; |
|
| 471 | - if ( ! preg_match( '/^[a-zA-Z0-9_-]+$/', $opt_name ) ) { |
|
| 472 | - $result = array( |
|
| 473 | - 'result' => false, |
|
| 474 | - 'data' => esc_html__( 'Invalid opt_name', 'redux-framework' ), |
|
| 475 | - ); |
|
| 476 | - echo wp_json_encode( $result ); |
|
| 477 | - die(); |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - $field_id = Redux_Color_Scheme_Functions::$field_id; |
|
| 481 | - if ( ! preg_match( '/^[a-zA-Z0-9_-]+$/', $field_id ) ) { |
|
| 482 | - $result = array( |
|
| 483 | - 'result' => false, |
|
| 484 | - 'data' => esc_html__( 'Invalid field_id', 'redux-framework' ), |
|
| 485 | - ); |
|
| 486 | - echo wp_json_encode( $result ); |
|
| 487 | - die(); |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - $content = wp_unslash( $_REQUEST['content'] ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
|
| 491 | - $content = is_array( $content ) ? array_map( 'stripslashes_deep', $content ) : stripslashes( $content ); |
|
| 492 | - $content = json_decode( $content, true ); |
|
| 493 | - |
|
| 494 | - if ( is_null( $content ) ) { |
|
| 495 | - $result = array( |
|
| 496 | - 'result' => false, |
|
| 497 | - 'data' => esc_html__( 'Import unsuccessful! Malformed JSON data detected.', 'redux-framework' ), |
|
| 498 | - ); |
|
| 499 | - |
|
| 500 | - $result = wp_json_encode( $result ); |
|
| 501 | - |
|
| 502 | - echo $result; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 503 | - |
|
| 504 | - die; |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - if ( isset( $content['Default']['color_scheme_name'] ) ) { |
|
| 508 | - $content = wp_json_encode( $content ); |
|
| 509 | - |
|
| 510 | - $param_array = array( |
|
| 511 | - 'content' => $content, |
|
| 512 | - 'overwrite' => true, |
|
| 513 | - 'chmod' => FS_CHMOD_FILE, |
|
| 514 | - ); |
|
| 515 | - |
|
| 516 | - $import_file = Redux_Color_Scheme_Functions::$upload_dir . sanitize_file_name( $opt_name ) . '_' . sanitize_file_name( $field_id ) . '.json'; |
|
| 517 | - |
|
| 518 | - if ( true === Redux_Core::$filesystem->execute( 'put_contents', $import_file, $param_array ) ) { |
|
| 519 | - $result = array( |
|
| 520 | - 'result' => true, |
|
| 521 | - // translators: %s = HTML content. |
|
| 522 | - 'data' => sprintf( esc_html__( 'Import successful! Click %s to refresh.', 'redux-framework' ), '<strong>' . esc_html__( 'OK', 'redux-framework' ) . '</strong>' ), |
|
| 523 | - ); |
|
| 524 | - } else { |
|
| 525 | - $result = array( |
|
| 526 | - 'result' => false, |
|
| 527 | - 'data' => esc_html__( 'Import unsuccessful! File permission error: Could not write import data to server.', 'redux-framework' ), |
|
| 528 | - ); |
|
| 529 | - } |
|
| 530 | - } else { |
|
| 531 | - $result = array( |
|
| 532 | - 'result' => false, |
|
| 533 | - 'data' => esc_html__( 'Import unsuccessful! The selected file is not a valid color scheme file.', 'redux-framework' ), |
|
| 534 | - ); |
|
| 535 | - } |
|
| 536 | - } else { |
|
| 537 | - $result = array( |
|
| 538 | - 'result' => false, |
|
| 539 | - 'data' => esc_html__( 'Import unsuccessful! No data detected in the import file.', 'redux-framework' ), |
|
| 540 | - ); |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - $result = wp_json_encode( $result ); |
|
| 544 | - |
|
| 545 | - echo $result; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 546 | - |
|
| 547 | - die; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * Download Scheme File. |
|
| 552 | - * |
|
| 553 | - * @since 1.0.0 |
|
| 554 | - * @access private |
|
| 555 | - * @return void |
|
| 556 | - */ |
|
| 557 | - private function download_schemes() { |
|
| 558 | - Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 559 | - Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 560 | - |
|
| 561 | - // Read contents of scheme file. |
|
| 562 | - $content = Redux_Color_Scheme_Functions::read_scheme_file(); |
|
| 563 | - $content = wp_json_encode( $content ); |
|
| 564 | - |
|
| 565 | - // Set header info. |
|
| 566 | - header( 'Content-Description: File Transfer' ); |
|
| 567 | - header( 'Content-type: application/txt' ); |
|
| 568 | - header( 'Content-Disposition: attachment; filename="redux_schemes_' . $this->parent->args['opt_name'] . '_' . $this->field_id . '_' . gmdate( 'm-d-Y' ) . '.json"' ); |
|
| 569 | - header( 'Content-Transfer-Encoding: binary' ); |
|
| 570 | - header( 'Expires: 0' ); |
|
| 571 | - header( 'Cache-Control: must-revalidate' ); |
|
| 572 | - header( 'Pragma: public' ); |
|
| 573 | - |
|
| 574 | - // File download. |
|
| 575 | - echo $content; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 576 | - |
|
| 577 | - // 2B ~! 2B |
|
| 578 | - die; |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * Save Scheme. Saved an individual scheme to JSON scheme file. |
|
| 583 | - * |
|
| 584 | - * @param ReduxFramework $redux ReduxFramework object. |
|
| 585 | - * |
|
| 586 | - * @return void |
|
| 587 | - * @since 1.0.0 |
|
| 588 | - * @access private |
|
| 589 | - */ |
|
| 590 | - private function save_scheme( ReduxFramework $redux ) { |
|
| 591 | - Redux_Color_Scheme_Functions::$parent = $redux; |
|
| 592 | - Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 593 | - |
|
| 594 | - // Get the scheme name. |
|
| 595 | - if ( isset( $_REQUEST['scheme_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 596 | - $scheme_name = sanitize_text_field( wp_unslash( $_REQUEST['scheme_name'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 597 | - |
|
| 598 | - // Check for duplicates. |
|
| 599 | - $names = Redux_Color_Scheme_Functions::get_scheme_names(); |
|
| 600 | - foreach ( $names as $name ) { |
|
| 601 | - $name = strtolower( $name ); |
|
| 602 | - $tmp_name = strtolower( $scheme_name ); |
|
| 603 | - |
|
| 604 | - if ( $name === $tmp_name ) { |
|
| 605 | - echo 'fail'; |
|
| 606 | - die(); |
|
| 607 | - } |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - // Get scheme data. |
|
| 611 | - if ( isset( $_REQUEST['scheme_data'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 612 | - $scheme_data = wp_unslash( $_REQUEST['scheme_data'] ); // phpcs:ignore WordPress.Security |
|
| 613 | - |
|
| 614 | - // Get field ID. |
|
| 615 | - if ( isset( $_REQUEST['field_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 616 | - $scheme_data = rawurldecode( $scheme_data ); |
|
| 617 | - $scheme_data = json_decode( $scheme_data, true ); |
|
| 618 | - |
|
| 619 | - // Save scheme to file. If successful... |
|
| 620 | - if ( true === Redux_Color_Scheme_Functions::set_scheme_data( $scheme_name, $scheme_data ) ) { |
|
| 621 | - |
|
| 622 | - // Update scheme selector. |
|
| 623 | - echo Redux_Color_Scheme_Functions::get_scheme_select_html( $scheme_name ); // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 624 | - } |
|
| 625 | - } |
|
| 626 | - } |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - die(); // a horrible death! |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * Delete Scheme. Delete individual scheme from JSON scheme file. |
|
| 634 | - * |
|
| 635 | - * @param ReduxFramework $redux ReduxFramework object. |
|
| 636 | - * |
|
| 637 | - * @return void |
|
| 638 | - * @since 1.0.0 |
|
| 639 | - * @access private |
|
| 640 | - */ |
|
| 641 | - private function delete_scheme( ReduxFramework $redux ) { |
|
| 642 | - |
|
| 643 | - // Get deleted scheme ID. |
|
| 644 | - if ( isset( $_REQUEST['scheme_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 645 | - $scheme_id = sanitize_text_field( wp_unslash( $_REQUEST['scheme_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 646 | - |
|
| 647 | - // Get field ID. |
|
| 648 | - if ( isset( $_REQUEST['field_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 649 | - $field_id = sanitize_text_field( wp_unslash( $_REQUEST['field_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 650 | - |
|
| 651 | - // If scheme ID was passed (and why wouldn't it be?? Hmm??). |
|
| 652 | - if ( $scheme_id ) { |
|
| 653 | - Redux_Color_Scheme_Functions::$field_id = $field_id; |
|
| 654 | - Redux_Color_Scheme_Functions::$parent = $redux; |
|
| 655 | - |
|
| 656 | - // Get the entire scheme file. |
|
| 657 | - $schemes = Redux_Color_Scheme_Functions::read_scheme_file(); |
|
| 658 | - |
|
| 659 | - // If we got a good read... |
|
| 660 | - if ( false !== $schemes ) { |
|
| 661 | - |
|
| 662 | - // If scheme name exists... |
|
| 663 | - if ( isset( $schemes[ $scheme_id ] ) ) { |
|
| 664 | - |
|
| 665 | - // Unset it. |
|
| 666 | - unset( $schemes[ $scheme_id ] ); |
|
| 667 | - |
|
| 668 | - // Save the scheme data, minus the deleted scheme. Upon success... |
|
| 669 | - if ( true === Redux_Color_Scheme_Functions::write_scheme_file( $schemes ) ) { |
|
| 670 | - |
|
| 671 | - // Set default scheme. |
|
| 672 | - Redux_Color_Scheme_Functions::set_current_scheme_id( 'Default' ); |
|
| 673 | - |
|
| 674 | - // Update field ID. |
|
| 675 | - Redux_Color_Scheme_Functions::$field_id = $field_id; |
|
| 676 | - |
|
| 677 | - // Meh TODO. |
|
| 678 | - Redux_Color_Scheme_Functions::set_database_data(); |
|
| 679 | - |
|
| 680 | - echo 'success'; |
|
| 681 | - } else { |
|
| 682 | - echo 'Failed to write JSON file to server.'; |
|
| 683 | - } |
|
| 684 | - } else { |
|
| 685 | - echo 'Scheme name does not exist in JSON string. Aborting.'; |
|
| 686 | - } |
|
| 687 | - } else { |
|
| 688 | - echo 'Failed to read JSON scheme file, or file is empty.'; |
|
| 689 | - } |
|
| 690 | - } else { |
|
| 691 | - echo 'No scheme ID passed. Aborting.'; |
|
| 692 | - } |
|
| 693 | - } |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - die(); // rolled a two. |
|
| 697 | - } |
|
| 698 | - |
|
| 699 | - /** |
|
| 700 | - * Gets the new scheme based on selection. |
|
| 701 | - * |
|
| 702 | - * @param ReduxFramework $redux ReduxFramework object. |
|
| 703 | - * |
|
| 704 | - * @return void |
|
| 705 | - * @since 1.0.0 |
|
| 706 | - * @access private |
|
| 707 | - */ |
|
| 708 | - private function get_scheme_html( ReduxFramework $redux ) { |
|
| 709 | - if ( isset( $_POST['scheme_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 710 | - |
|
| 711 | - // Get the selected scheme name. |
|
| 712 | - $scheme_id = sanitize_text_field( wp_unslash( $_POST['scheme_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 713 | - |
|
| 714 | - if ( isset( $_POST['field_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 715 | - |
|
| 716 | - // Get the field ID. |
|
| 717 | - $field_id = sanitize_text_field( wp_unslash( $_POST['field_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 718 | - |
|
| 719 | - // Get the field class. |
|
| 720 | - $field_class = isset( $_POST['field_class'] ) ? sanitize_text_field( wp_unslash( $_POST['field_class'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
|
| 721 | - |
|
| 722 | - Redux_Color_Scheme_Functions::$parent = $redux; |
|
| 723 | - |
|
| 724 | - // Set the updated field ID. |
|
| 725 | - Redux_Color_Scheme_Functions::$field_id = $field_id; |
|
| 726 | - |
|
| 727 | - // Set the updated field class. |
|
| 728 | - Redux_Color_Scheme_Functions::$field_class = $field_class; |
|
| 729 | - |
|
| 730 | - // Get the color picket layout HTML. |
|
| 731 | - $html = Redux_Color_Scheme_Functions::get_current_color_scheme_html( $scheme_id ); |
|
| 732 | - |
|
| 733 | - // Print! |
|
| 734 | - echo $html; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 735 | - } |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - die(); // another day. |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - |
|
| 742 | - /** |
|
| 743 | - * Retrieves an array of default data for color picker. |
|
| 744 | - * |
|
| 745 | - * @since 1.0.0 |
|
| 746 | - * @access private |
|
| 747 | - * @return array Default values from config. |
|
| 748 | - */ |
|
| 749 | - private function get_default_data(): array { |
|
| 750 | - $def_opts = $this->parent->options_defaults[ $this->field_id ]; |
|
| 751 | - |
|
| 752 | - if ( isset( $def_opts['color_scheme_name'] ) ) { |
|
| 753 | - return array(); |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - $sections = $this->parent->sections; |
|
| 757 | - $data = array(); |
|
| 758 | - |
|
| 759 | - foreach ( $sections as $arr ) { |
|
| 760 | - if ( isset( $arr['fields'] ) ) { |
|
| 761 | - foreach ( $arr['fields'] as $arr2 ) { |
|
| 762 | - if ( $arr2['id'] === $this->field_id ) { |
|
| 763 | - |
|
| 764 | - // Select fields. |
|
| 765 | - if ( isset( $arr2['select'] ) ) { |
|
| 766 | - foreach ( $arr2['select'] as $v ) { |
|
| 767 | - $data[] = array( |
|
| 768 | - 'id' => $v['id'], |
|
| 769 | - 'value' => $v['default'], |
|
| 770 | - 'type' => 'select', |
|
| 771 | - ); |
|
| 772 | - } |
|
| 773 | - } |
|
| 774 | - } |
|
| 775 | - } |
|
| 776 | - } |
|
| 777 | - } |
|
| 778 | - |
|
| 779 | - foreach ( $def_opts as $v ) { |
|
| 780 | - $title = $v['title'] ?? $v['id']; |
|
| 781 | - $color = $v['color'] ?? ''; |
|
| 782 | - $alpha = $v['alpha'] ?? 1; |
|
| 783 | - $grp = $v['group'] ?? ''; |
|
| 784 | - |
|
| 785 | - if ( '' === $color || 'transparent' === $color ) { |
|
| 786 | - $rgba = $this->output_transparent ? 'transparent' : ''; |
|
| 787 | - } else { |
|
| 788 | - $rgba = Redux_Helpers::hex2rgba( $color, $alpha ); |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - $data[] = array( |
|
| 792 | - 'id' => $v['id'], |
|
| 793 | - 'title' => $title, |
|
| 794 | - 'color' => $color, |
|
| 795 | - 'alpha' => $alpha, |
|
| 796 | - 'group' => $grp, |
|
| 797 | - 'rgba' => $rgba, |
|
| 798 | - ); |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - return $data; |
|
| 802 | - } |
|
| 803 | - } |
|
| 804 | - |
|
| 805 | - class_alias( Redux_Extension_Color_Scheme::class, 'ReduxFramework_Extension_Color_Scheme' ); |
|
| 225 | + /** |
|
| 226 | + * Reset section defaults. |
|
| 227 | + * |
|
| 228 | + * @param array $defaults Default values. |
|
| 229 | + * |
|
| 230 | + * @return array |
|
| 231 | + */ |
|
| 232 | + public function reset_defaults_section( array $defaults = array() ): array { |
|
| 233 | + if ( Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
|
| 234 | + // Get the current tab/section number. |
|
| 235 | + if ( isset( $_COOKIE['redux_current_tab'] ) ) { |
|
| 236 | + $cur_tab = sanitize_text_field( wp_unslash( $_COOKIE['redux_current_tab'] ) ); |
|
| 237 | + |
|
| 238 | + // Get the tab/section number field is used on. |
|
| 239 | + $tab_num = $this->parent->field_sections['color_scheme'][ $this->field_id ]; |
|
| 240 | + |
|
| 241 | + // Match... |
|
| 242 | + if ( $cur_tab === $tab_num ) { |
|
| 243 | + |
|
| 244 | + // Reset data. |
|
| 245 | + $this->reset_all(); |
|
| 246 | + } |
|
| 247 | + $defaults[ $this->field_id ] = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + return $defaults; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Save Changes Hook. What to do when changes are saved |
|
| 256 | + * |
|
| 257 | + * @param array $saved_options Saved data. |
|
| 258 | + * @param array $old_options Previous data. |
|
| 259 | + * |
|
| 260 | + * @return array |
|
| 261 | + * @since 1.0.0 |
|
| 262 | + * @access public |
|
| 263 | + */ |
|
| 264 | + public function save_hook( array $saved_options = array(), array $old_options = array() ): array { |
|
| 265 | + if ( ! isset( $saved_options[ $this->field_id ] ) || empty( $saved_options[ $this->field_id ] ) || ( is_array( $saved_options[ $this->field_id ] ) && $old_options === $saved_options ) || ! array_key_exists( $this->field_id, $saved_options ) ) { |
|
| 266 | + return $saved_options; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + // We'll use the reset hook instead. |
|
| 270 | + if ( ! empty( $saved_options['defaults'] ) || ! empty( $saved_options['defaults-section'] ) ) { |
|
| 271 | + return $saved_options; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + $first_value = reset( $saved_options[ $this->field_id ] ); // First Element's Value. |
|
| 275 | + |
|
| 276 | + // Parse the JSON to an array. |
|
| 277 | + if ( isset( $first_value['data'] ) ) { |
|
| 278 | + |
|
| 279 | + Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 280 | + Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 281 | + |
|
| 282 | + Redux_Color_Scheme_Functions::set_current_scheme_id( $saved_options['redux-scheme-select'] ); |
|
| 283 | + |
|
| 284 | + // Get the current field ID. |
|
| 285 | + $raw_data = $saved_options[ $this->field_id ]; |
|
| 286 | + |
|
| 287 | + // Create a new array. |
|
| 288 | + $save_data = array(); |
|
| 289 | + |
|
| 290 | + // Enum through saved data. |
|
| 291 | + foreach ( $raw_data as $id => $val ) { |
|
| 292 | + |
|
| 293 | + if ( 'color_scheme_name' !== $id ) { |
|
| 294 | + if ( is_array( $val ) ) { |
|
| 295 | + |
|
| 296 | + if ( ! isset( $val['data'] ) ) { |
|
| 297 | + continue; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + $data = json_decode( rawurldecode( $val['data'] ), true ); |
|
| 301 | + |
|
| 302 | + // Sanitize everything. |
|
| 303 | + $color = $data['color'] ?? ''; |
|
| 304 | + $alpha = $data['alpha'] ?? 1; |
|
| 305 | + |
|
| 306 | + $id = $data['id'] ?? $id; |
|
| 307 | + $title = $data['title'] ?? $id; |
|
| 308 | + |
|
| 309 | + $grp = $data['group'] ?? ''; |
|
| 310 | + |
|
| 311 | + if ( '' === $color || 'transparent' === $color ) { |
|
| 312 | + $rgba = $this->output_transparent ? 'transparent' : ''; |
|
| 313 | + } else { |
|
| 314 | + $rgba = Redux_Helpers::hex2rgba( $color, $alpha ); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + // Create an array of saved data. |
|
| 318 | + $save_data[] = array( |
|
| 319 | + 'id' => $id, |
|
| 320 | + 'title' => $title, |
|
| 321 | + 'color' => $color, |
|
| 322 | + 'alpha' => $alpha, |
|
| 323 | + 'group' => $grp, |
|
| 324 | + 'rgba' => $rgba, |
|
| 325 | + ); |
|
| 326 | + } else { |
|
| 327 | + $save_data[] = array( |
|
| 328 | + 'id' => $id, |
|
| 329 | + 'value' => $val, |
|
| 330 | + 'type' => 'select', |
|
| 331 | + ); |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + $new_scheme = array(); |
|
| 337 | + |
|
| 338 | + $new_scheme['color_scheme_name'] = Redux_Color_Scheme_Functions::get_current_scheme_id(); |
|
| 339 | + |
|
| 340 | + // Enum through values and assign them to a new array. |
|
| 341 | + foreach ( $save_data as $val ) { |
|
| 342 | + if ( isset( $val['id'] ) ) { |
|
| 343 | + $new_scheme[ $val['id'] ] = $val; |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + // Filter for DB save |
|
| 348 | + // Doesn't need to save select arrays to a database, |
|
| 349 | + // just the id => value. |
|
| 350 | + $database_data = $new_scheme; |
|
| 351 | + |
|
| 352 | + foreach ( $database_data as $k => $v ) { |
|
| 353 | + if ( isset( $v['type'] ) ) { |
|
| 354 | + $val = $v['value']; |
|
| 355 | + |
|
| 356 | + unset( $database_data[ $k ] ); |
|
| 357 | + |
|
| 358 | + $database_data[ $k ] = $val; |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + $saved_options[ $this->field_id ] = $database_data; |
|
| 363 | + |
|
| 364 | + // Check if we should save this compared to the old data. |
|
| 365 | + $save_scheme = false; |
|
| 366 | + |
|
| 367 | + // Doesn't exist or is empty. |
|
| 368 | + if ( ! isset( $old_options[ $this->field_id ] ) || ( isset( $old_options[ $this->field_id ] ) && ! empty( $old_options[ $this->field_id ] ) ) ) { |
|
| 369 | + $save_scheme = true; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + // Isn't empty and isn't the same as the new array. |
|
| 373 | + if ( ! empty( $old_options[ $this->field_id ] ) && $saved_options[ $this->field_id ] !== $old_options[ $this->field_id ] ) { |
|
| 374 | + $save_scheme = true; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + if ( $save_scheme ) { |
|
| 378 | + $scheme = Redux_Color_Scheme_Functions::get_current_scheme_id(); |
|
| 379 | + Redux_Color_Scheme_Functions::set_scheme_data( $scheme, $save_data ); |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + return $saved_options; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Reset data. Restores colour picker to default values |
|
| 388 | + * |
|
| 389 | + * @since 1.0.0 |
|
| 390 | + * @access private |
|
| 391 | + * @return void |
|
| 392 | + */ |
|
| 393 | + private function reset_data() { |
|
| 394 | + Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 395 | + Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 396 | + |
|
| 397 | + // Get default data. |
|
| 398 | + $data = $this->get_default_data(); |
|
| 399 | + |
|
| 400 | + // Add to (and/or create) JSON scheme file. |
|
| 401 | + Redux_Color_Scheme_Functions::set_scheme_data( 'Default', $data ); |
|
| 402 | + |
|
| 403 | + // Set default scheme. |
|
| 404 | + Redux_Color_Scheme_Functions::set_current_scheme_id( 'Default' ); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Reset All Hook. Todo list when all data is reset |
|
| 409 | + * |
|
| 410 | + * @return void |
|
| 411 | + * @since 1.0.0 |
|
| 412 | + * @access public |
|
| 413 | + */ |
|
| 414 | + public function reset_all() { |
|
| 415 | + if ( ! empty( $this->field_id ) && isset( $this->parent->options_defaults[ $this->field_id ] ) && ! empty( $this->parent->options_defaults[ $this->field_id ] ) ) { |
|
| 416 | + Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 417 | + Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 418 | + |
|
| 419 | + $this->reset_data(); |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * AJAX evaluator. Determine course of action based on AJAX callback |
|
| 425 | + * |
|
| 426 | + * @since 1.0.0 |
|
| 427 | + * @access public |
|
| 428 | + * @return void |
|
| 429 | + */ |
|
| 430 | + public function parse_ajax() { |
|
| 431 | + if ( ! is_user_logged_in() && ! is_admin() && ! current_user_can( $this->parent->args['page_permissions'] ) ) { |
|
| 432 | + wp_die( esc_html__( 'You do not have permission to perform this action.', 'redux-framework' ) ); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + $opt_name = $this->parent->args['opt_name']; |
|
| 436 | + $parent = $this->parent; |
|
| 437 | + |
|
| 438 | + $nonce = isset( $_REQUEST['nonce'] ) ? sanitize_key( wp_unslash( $_REQUEST['nonce'] ) ) : ''; |
|
| 439 | + if ( ! wp_verify_nonce( $nonce, 'redux_' . $opt_name . '_color_schemes' ) ) { |
|
| 440 | + wp_die( esc_html__( 'Invalid Security Credentials. Please reload the page and try again.', 'redux-framework' ) ); |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + $type = isset( $_REQUEST['type'] ) ? sanitize_key( wp_unslash( $_REQUEST['type'] ) ) : ''; |
|
| 444 | + if ( ! in_array( $type, array( 'save', 'delete', 'update', 'export', 'import' ), true ) ) { |
|
| 445 | + wp_die( esc_html__( 'Invalid request.', 'redux-framework' ) ); |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + if ( 'save' === $type ) { |
|
| 449 | + $this->save_scheme( $parent ); |
|
| 450 | + } elseif ( 'delete' === $type ) { |
|
| 451 | + $this->delete_scheme( $parent ); |
|
| 452 | + } elseif ( 'update' === $type ) { |
|
| 453 | + $this->get_scheme_html( $parent ); |
|
| 454 | + } elseif ( 'export' === $type ) { |
|
| 455 | + $this->download_schemes(); |
|
| 456 | + } elseif ( 'import' === $type ) { |
|
| 457 | + $this->import_schemes(); |
|
| 458 | + } |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * Download Scheme File. |
|
| 463 | + * |
|
| 464 | + * @since 4.4.18 |
|
| 465 | + * @access private |
|
| 466 | + * @return void |
|
| 467 | + */ |
|
| 468 | + private function import_schemes() { |
|
| 469 | + if ( isset( $_REQUEST['content'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 470 | + $opt_name = Redux_Color_Scheme_Functions::$parent->args['opt_name']; |
|
| 471 | + if ( ! preg_match( '/^[a-zA-Z0-9_-]+$/', $opt_name ) ) { |
|
| 472 | + $result = array( |
|
| 473 | + 'result' => false, |
|
| 474 | + 'data' => esc_html__( 'Invalid opt_name', 'redux-framework' ), |
|
| 475 | + ); |
|
| 476 | + echo wp_json_encode( $result ); |
|
| 477 | + die(); |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + $field_id = Redux_Color_Scheme_Functions::$field_id; |
|
| 481 | + if ( ! preg_match( '/^[a-zA-Z0-9_-]+$/', $field_id ) ) { |
|
| 482 | + $result = array( |
|
| 483 | + 'result' => false, |
|
| 484 | + 'data' => esc_html__( 'Invalid field_id', 'redux-framework' ), |
|
| 485 | + ); |
|
| 486 | + echo wp_json_encode( $result ); |
|
| 487 | + die(); |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + $content = wp_unslash( $_REQUEST['content'] ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
|
| 491 | + $content = is_array( $content ) ? array_map( 'stripslashes_deep', $content ) : stripslashes( $content ); |
|
| 492 | + $content = json_decode( $content, true ); |
|
| 493 | + |
|
| 494 | + if ( is_null( $content ) ) { |
|
| 495 | + $result = array( |
|
| 496 | + 'result' => false, |
|
| 497 | + 'data' => esc_html__( 'Import unsuccessful! Malformed JSON data detected.', 'redux-framework' ), |
|
| 498 | + ); |
|
| 499 | + |
|
| 500 | + $result = wp_json_encode( $result ); |
|
| 501 | + |
|
| 502 | + echo $result; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 503 | + |
|
| 504 | + die; |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + if ( isset( $content['Default']['color_scheme_name'] ) ) { |
|
| 508 | + $content = wp_json_encode( $content ); |
|
| 509 | + |
|
| 510 | + $param_array = array( |
|
| 511 | + 'content' => $content, |
|
| 512 | + 'overwrite' => true, |
|
| 513 | + 'chmod' => FS_CHMOD_FILE, |
|
| 514 | + ); |
|
| 515 | + |
|
| 516 | + $import_file = Redux_Color_Scheme_Functions::$upload_dir . sanitize_file_name( $opt_name ) . '_' . sanitize_file_name( $field_id ) . '.json'; |
|
| 517 | + |
|
| 518 | + if ( true === Redux_Core::$filesystem->execute( 'put_contents', $import_file, $param_array ) ) { |
|
| 519 | + $result = array( |
|
| 520 | + 'result' => true, |
|
| 521 | + // translators: %s = HTML content. |
|
| 522 | + 'data' => sprintf( esc_html__( 'Import successful! Click %s to refresh.', 'redux-framework' ), '<strong>' . esc_html__( 'OK', 'redux-framework' ) . '</strong>' ), |
|
| 523 | + ); |
|
| 524 | + } else { |
|
| 525 | + $result = array( |
|
| 526 | + 'result' => false, |
|
| 527 | + 'data' => esc_html__( 'Import unsuccessful! File permission error: Could not write import data to server.', 'redux-framework' ), |
|
| 528 | + ); |
|
| 529 | + } |
|
| 530 | + } else { |
|
| 531 | + $result = array( |
|
| 532 | + 'result' => false, |
|
| 533 | + 'data' => esc_html__( 'Import unsuccessful! The selected file is not a valid color scheme file.', 'redux-framework' ), |
|
| 534 | + ); |
|
| 535 | + } |
|
| 536 | + } else { |
|
| 537 | + $result = array( |
|
| 538 | + 'result' => false, |
|
| 539 | + 'data' => esc_html__( 'Import unsuccessful! No data detected in the import file.', 'redux-framework' ), |
|
| 540 | + ); |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + $result = wp_json_encode( $result ); |
|
| 544 | + |
|
| 545 | + echo $result; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 546 | + |
|
| 547 | + die; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * Download Scheme File. |
|
| 552 | + * |
|
| 553 | + * @since 1.0.0 |
|
| 554 | + * @access private |
|
| 555 | + * @return void |
|
| 556 | + */ |
|
| 557 | + private function download_schemes() { |
|
| 558 | + Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 559 | + Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 560 | + |
|
| 561 | + // Read contents of scheme file. |
|
| 562 | + $content = Redux_Color_Scheme_Functions::read_scheme_file(); |
|
| 563 | + $content = wp_json_encode( $content ); |
|
| 564 | + |
|
| 565 | + // Set header info. |
|
| 566 | + header( 'Content-Description: File Transfer' ); |
|
| 567 | + header( 'Content-type: application/txt' ); |
|
| 568 | + header( 'Content-Disposition: attachment; filename="redux_schemes_' . $this->parent->args['opt_name'] . '_' . $this->field_id . '_' . gmdate( 'm-d-Y' ) . '.json"' ); |
|
| 569 | + header( 'Content-Transfer-Encoding: binary' ); |
|
| 570 | + header( 'Expires: 0' ); |
|
| 571 | + header( 'Cache-Control: must-revalidate' ); |
|
| 572 | + header( 'Pragma: public' ); |
|
| 573 | + |
|
| 574 | + // File download. |
|
| 575 | + echo $content; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 576 | + |
|
| 577 | + // 2B ~! 2B |
|
| 578 | + die; |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * Save Scheme. Saved an individual scheme to JSON scheme file. |
|
| 583 | + * |
|
| 584 | + * @param ReduxFramework $redux ReduxFramework object. |
|
| 585 | + * |
|
| 586 | + * @return void |
|
| 587 | + * @since 1.0.0 |
|
| 588 | + * @access private |
|
| 589 | + */ |
|
| 590 | + private function save_scheme( ReduxFramework $redux ) { |
|
| 591 | + Redux_Color_Scheme_Functions::$parent = $redux; |
|
| 592 | + Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 593 | + |
|
| 594 | + // Get the scheme name. |
|
| 595 | + if ( isset( $_REQUEST['scheme_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 596 | + $scheme_name = sanitize_text_field( wp_unslash( $_REQUEST['scheme_name'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 597 | + |
|
| 598 | + // Check for duplicates. |
|
| 599 | + $names = Redux_Color_Scheme_Functions::get_scheme_names(); |
|
| 600 | + foreach ( $names as $name ) { |
|
| 601 | + $name = strtolower( $name ); |
|
| 602 | + $tmp_name = strtolower( $scheme_name ); |
|
| 603 | + |
|
| 604 | + if ( $name === $tmp_name ) { |
|
| 605 | + echo 'fail'; |
|
| 606 | + die(); |
|
| 607 | + } |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + // Get scheme data. |
|
| 611 | + if ( isset( $_REQUEST['scheme_data'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 612 | + $scheme_data = wp_unslash( $_REQUEST['scheme_data'] ); // phpcs:ignore WordPress.Security |
|
| 613 | + |
|
| 614 | + // Get field ID. |
|
| 615 | + if ( isset( $_REQUEST['field_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 616 | + $scheme_data = rawurldecode( $scheme_data ); |
|
| 617 | + $scheme_data = json_decode( $scheme_data, true ); |
|
| 618 | + |
|
| 619 | + // Save scheme to file. If successful... |
|
| 620 | + if ( true === Redux_Color_Scheme_Functions::set_scheme_data( $scheme_name, $scheme_data ) ) { |
|
| 621 | + |
|
| 622 | + // Update scheme selector. |
|
| 623 | + echo Redux_Color_Scheme_Functions::get_scheme_select_html( $scheme_name ); // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 624 | + } |
|
| 625 | + } |
|
| 626 | + } |
|
| 627 | + } |
|
| 628 | + |
|
| 629 | + die(); // a horrible death! |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * Delete Scheme. Delete individual scheme from JSON scheme file. |
|
| 634 | + * |
|
| 635 | + * @param ReduxFramework $redux ReduxFramework object. |
|
| 636 | + * |
|
| 637 | + * @return void |
|
| 638 | + * @since 1.0.0 |
|
| 639 | + * @access private |
|
| 640 | + */ |
|
| 641 | + private function delete_scheme( ReduxFramework $redux ) { |
|
| 642 | + |
|
| 643 | + // Get deleted scheme ID. |
|
| 644 | + if ( isset( $_REQUEST['scheme_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 645 | + $scheme_id = sanitize_text_field( wp_unslash( $_REQUEST['scheme_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 646 | + |
|
| 647 | + // Get field ID. |
|
| 648 | + if ( isset( $_REQUEST['field_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 649 | + $field_id = sanitize_text_field( wp_unslash( $_REQUEST['field_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 650 | + |
|
| 651 | + // If scheme ID was passed (and why wouldn't it be?? Hmm??). |
|
| 652 | + if ( $scheme_id ) { |
|
| 653 | + Redux_Color_Scheme_Functions::$field_id = $field_id; |
|
| 654 | + Redux_Color_Scheme_Functions::$parent = $redux; |
|
| 655 | + |
|
| 656 | + // Get the entire scheme file. |
|
| 657 | + $schemes = Redux_Color_Scheme_Functions::read_scheme_file(); |
|
| 658 | + |
|
| 659 | + // If we got a good read... |
|
| 660 | + if ( false !== $schemes ) { |
|
| 661 | + |
|
| 662 | + // If scheme name exists... |
|
| 663 | + if ( isset( $schemes[ $scheme_id ] ) ) { |
|
| 664 | + |
|
| 665 | + // Unset it. |
|
| 666 | + unset( $schemes[ $scheme_id ] ); |
|
| 667 | + |
|
| 668 | + // Save the scheme data, minus the deleted scheme. Upon success... |
|
| 669 | + if ( true === Redux_Color_Scheme_Functions::write_scheme_file( $schemes ) ) { |
|
| 670 | + |
|
| 671 | + // Set default scheme. |
|
| 672 | + Redux_Color_Scheme_Functions::set_current_scheme_id( 'Default' ); |
|
| 673 | + |
|
| 674 | + // Update field ID. |
|
| 675 | + Redux_Color_Scheme_Functions::$field_id = $field_id; |
|
| 676 | + |
|
| 677 | + // Meh TODO. |
|
| 678 | + Redux_Color_Scheme_Functions::set_database_data(); |
|
| 679 | + |
|
| 680 | + echo 'success'; |
|
| 681 | + } else { |
|
| 682 | + echo 'Failed to write JSON file to server.'; |
|
| 683 | + } |
|
| 684 | + } else { |
|
| 685 | + echo 'Scheme name does not exist in JSON string. Aborting.'; |
|
| 686 | + } |
|
| 687 | + } else { |
|
| 688 | + echo 'Failed to read JSON scheme file, or file is empty.'; |
|
| 689 | + } |
|
| 690 | + } else { |
|
| 691 | + echo 'No scheme ID passed. Aborting.'; |
|
| 692 | + } |
|
| 693 | + } |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + die(); // rolled a two. |
|
| 697 | + } |
|
| 698 | + |
|
| 699 | + /** |
|
| 700 | + * Gets the new scheme based on selection. |
|
| 701 | + * |
|
| 702 | + * @param ReduxFramework $redux ReduxFramework object. |
|
| 703 | + * |
|
| 704 | + * @return void |
|
| 705 | + * @since 1.0.0 |
|
| 706 | + * @access private |
|
| 707 | + */ |
|
| 708 | + private function get_scheme_html( ReduxFramework $redux ) { |
|
| 709 | + if ( isset( $_POST['scheme_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 710 | + |
|
| 711 | + // Get the selected scheme name. |
|
| 712 | + $scheme_id = sanitize_text_field( wp_unslash( $_POST['scheme_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 713 | + |
|
| 714 | + if ( isset( $_POST['field_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 715 | + |
|
| 716 | + // Get the field ID. |
|
| 717 | + $field_id = sanitize_text_field( wp_unslash( $_POST['field_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 718 | + |
|
| 719 | + // Get the field class. |
|
| 720 | + $field_class = isset( $_POST['field_class'] ) ? sanitize_text_field( wp_unslash( $_POST['field_class'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
|
| 721 | + |
|
| 722 | + Redux_Color_Scheme_Functions::$parent = $redux; |
|
| 723 | + |
|
| 724 | + // Set the updated field ID. |
|
| 725 | + Redux_Color_Scheme_Functions::$field_id = $field_id; |
|
| 726 | + |
|
| 727 | + // Set the updated field class. |
|
| 728 | + Redux_Color_Scheme_Functions::$field_class = $field_class; |
|
| 729 | + |
|
| 730 | + // Get the color picket layout HTML. |
|
| 731 | + $html = Redux_Color_Scheme_Functions::get_current_color_scheme_html( $scheme_id ); |
|
| 732 | + |
|
| 733 | + // Print! |
|
| 734 | + echo $html; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 735 | + } |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + die(); // another day. |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + |
|
| 742 | + /** |
|
| 743 | + * Retrieves an array of default data for color picker. |
|
| 744 | + * |
|
| 745 | + * @since 1.0.0 |
|
| 746 | + * @access private |
|
| 747 | + * @return array Default values from config. |
|
| 748 | + */ |
|
| 749 | + private function get_default_data(): array { |
|
| 750 | + $def_opts = $this->parent->options_defaults[ $this->field_id ]; |
|
| 751 | + |
|
| 752 | + if ( isset( $def_opts['color_scheme_name'] ) ) { |
|
| 753 | + return array(); |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + $sections = $this->parent->sections; |
|
| 757 | + $data = array(); |
|
| 758 | + |
|
| 759 | + foreach ( $sections as $arr ) { |
|
| 760 | + if ( isset( $arr['fields'] ) ) { |
|
| 761 | + foreach ( $arr['fields'] as $arr2 ) { |
|
| 762 | + if ( $arr2['id'] === $this->field_id ) { |
|
| 763 | + |
|
| 764 | + // Select fields. |
|
| 765 | + if ( isset( $arr2['select'] ) ) { |
|
| 766 | + foreach ( $arr2['select'] as $v ) { |
|
| 767 | + $data[] = array( |
|
| 768 | + 'id' => $v['id'], |
|
| 769 | + 'value' => $v['default'], |
|
| 770 | + 'type' => 'select', |
|
| 771 | + ); |
|
| 772 | + } |
|
| 773 | + } |
|
| 774 | + } |
|
| 775 | + } |
|
| 776 | + } |
|
| 777 | + } |
|
| 778 | + |
|
| 779 | + foreach ( $def_opts as $v ) { |
|
| 780 | + $title = $v['title'] ?? $v['id']; |
|
| 781 | + $color = $v['color'] ?? ''; |
|
| 782 | + $alpha = $v['alpha'] ?? 1; |
|
| 783 | + $grp = $v['group'] ?? ''; |
|
| 784 | + |
|
| 785 | + if ( '' === $color || 'transparent' === $color ) { |
|
| 786 | + $rgba = $this->output_transparent ? 'transparent' : ''; |
|
| 787 | + } else { |
|
| 788 | + $rgba = Redux_Helpers::hex2rgba( $color, $alpha ); |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + $data[] = array( |
|
| 792 | + 'id' => $v['id'], |
|
| 793 | + 'title' => $title, |
|
| 794 | + 'color' => $color, |
|
| 795 | + 'alpha' => $alpha, |
|
| 796 | + 'group' => $grp, |
|
| 797 | + 'rgba' => $rgba, |
|
| 798 | + ); |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + return $data; |
|
| 802 | + } |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + class_alias( Redux_Extension_Color_Scheme::class, 'ReduxFramework_Extension_Color_Scheme' ); |
|
| 806 | 806 | } |
@@ -12,70 +12,70 @@ |
||
| 12 | 12 | |
| 13 | 13 | if ( ! class_exists( 'Redux_Secure_Token', false ) ) { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Redux_Secure_Token. |
|
| 17 | - * |
|
| 18 | - * @since 4.5.10 |
|
| 19 | - */ |
|
| 20 | - class Redux_Secure_Token { |
|
| 21 | - /** |
|
| 22 | - * Generate a secure, single-use token. |
|
| 23 | - * |
|
| 24 | - * @param string $action Nonce action. |
|
| 25 | - * @param int|string|null $user_id User ID. |
|
| 26 | - * |
|
| 27 | - * @return false|mixed|null |
|
| 28 | - */ |
|
| 29 | - public static function generate( string $action, $user_id = null ) { |
|
| 30 | - $user_id = $user_id ?? get_current_user_id(); |
|
| 15 | + /** |
|
| 16 | + * Redux_Secure_Token. |
|
| 17 | + * |
|
| 18 | + * @since 4.5.10 |
|
| 19 | + */ |
|
| 20 | + class Redux_Secure_Token { |
|
| 21 | + /** |
|
| 22 | + * Generate a secure, single-use token. |
|
| 23 | + * |
|
| 24 | + * @param string $action Nonce action. |
|
| 25 | + * @param int|string|null $user_id User ID. |
|
| 26 | + * |
|
| 27 | + * @return false|mixed|null |
|
| 28 | + */ |
|
| 29 | + public static function generate( string $action, $user_id = null ) { |
|
| 30 | + $user_id = $user_id ?? get_current_user_id(); |
|
| 31 | 31 | |
| 32 | - if ( ! $user_id ) { |
|
| 33 | - return false; // No tokens for unauthenticated users. |
|
| 34 | - } |
|
| 32 | + if ( ! $user_id ) { |
|
| 33 | + return false; // No tokens for unauthenticated users. |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - $token = wp_generate_password( 32, false ); |
|
| 37 | - $hash = wp_hash( $token . $action . $user_id ); |
|
| 36 | + $token = wp_generate_password( 32, false ); |
|
| 37 | + $hash = wp_hash( $token . $action . $user_id ); |
|
| 38 | 38 | |
| 39 | - // Store with expiration (1 hour). |
|
| 40 | - set_transient( |
|
| 41 | - 'redux_token_' . $hash, |
|
| 42 | - array( |
|
| 43 | - 'user_id' => $user_id, |
|
| 44 | - 'action' => $action, |
|
| 45 | - 'created' => time(), |
|
| 46 | - ), |
|
| 47 | - HOUR_IN_SECONDS |
|
| 48 | - ); |
|
| 39 | + // Store with expiration (1 hour). |
|
| 40 | + set_transient( |
|
| 41 | + 'redux_token_' . $hash, |
|
| 42 | + array( |
|
| 43 | + 'user_id' => $user_id, |
|
| 44 | + 'action' => $action, |
|
| 45 | + 'created' => time(), |
|
| 46 | + ), |
|
| 47 | + HOUR_IN_SECONDS |
|
| 48 | + ); |
|
| 49 | 49 | |
| 50 | - return $token; |
|
| 51 | - } |
|
| 50 | + return $token; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Verify and consume a token. |
|
| 55 | - * |
|
| 56 | - * @param string $token Security token. |
|
| 57 | - * @param string $action Nonce action. |
|
| 58 | - * |
|
| 59 | - * @return bool |
|
| 60 | - */ |
|
| 61 | - public static function verify( string $token, string $action ): bool { |
|
| 62 | - $user_id = get_current_user_id(); |
|
| 53 | + /** |
|
| 54 | + * Verify and consume a token. |
|
| 55 | + * |
|
| 56 | + * @param string $token Security token. |
|
| 57 | + * @param string $action Nonce action. |
|
| 58 | + * |
|
| 59 | + * @return bool |
|
| 60 | + */ |
|
| 61 | + public static function verify( string $token, string $action ): bool { |
|
| 62 | + $user_id = get_current_user_id(); |
|
| 63 | 63 | |
| 64 | - if ( ! $user_id ) { |
|
| 65 | - return false; |
|
| 66 | - } |
|
| 64 | + if ( ! $user_id ) { |
|
| 65 | + return false; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - $hash = wp_hash( $token . $action . $user_id ); |
|
| 69 | - $data = get_transient( 'redux_token_' . $hash ); |
|
| 68 | + $hash = wp_hash( $token . $action . $user_id ); |
|
| 69 | + $data = get_transient( 'redux_token_' . $hash ); |
|
| 70 | 70 | |
| 71 | - if ( ! $data || $data['user_id'] !== $user_id || $data['action'] !== $action ) { |
|
| 72 | - return false; |
|
| 73 | - } |
|
| 71 | + if ( ! $data || $data['user_id'] !== $user_id || $data['action'] !== $action ) { |
|
| 72 | + return false; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - // Delete token after use (single-use). |
|
| 76 | - delete_transient( 'redux_token_' . $hash ); |
|
| 75 | + // Delete token after use (single-use). |
|
| 76 | + delete_transient( 'redux_token_' . $hash ); |
|
| 77 | 77 | |
| 78 | - return true; |
|
| 79 | - } |
|
| 80 | - } |
|
| 78 | + return true; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | 81 | } |