ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-kirki-settings-repeater-setting.php (2161B)


      1 <?php
      2 /**
      3  * Repeater Customizer Setting.
      4  *
      5  * @package     Kirki
      6  * @subpackage  Controls
      7  * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
      8  * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
      9  * @since       2.0
     10  */
     11 
     12 // Exit if accessed directly.
     13 if ( ! defined( 'ABSPATH' ) ) {
     14 	exit;
     15 }
     16 
     17 if ( ! class_exists( 'Kirki_Settings_Repeater_Setting' ) ) {
     18 
     19 	/**
     20 	 * Repeater Settings.
     21 	 */
     22 	class Kirki_Settings_Repeater_Setting extends WP_Customize_Setting {
     23 
     24 		/**
     25 		 * Constructor.
     26 		 *
     27 		 * Any supplied $args override class property defaults.
     28 		 *
     29 		 * @access public
     30 		 * @param WP_Customize_Manager $manager The WordPress WP_Customize_Manager object.
     31 		 * @param string               $id       A specific ID of the setting. Can be a theme mod or option name.
     32 		 * @param array                $args     Setting arguments.
     33 		 */
     34 		public function __construct( $manager, $id, $args = array() ) {
     35 			parent::__construct( $manager, $id, $args );
     36 
     37 			// Will onvert the setting from JSON to array. Must be triggered very soon.
     38 			add_filter( "customize_sanitize_{$this->id}", array( $this, 'sanitize_repeater_setting' ), 10, 1 );
     39 		}
     40 
     41 		/**
     42 		 * Fetch the value of the setting.
     43 		 *
     44 		 * @access public
     45 		 * @return mixed The value.
     46 		 */
     47 		public function value() {
     48 			$value = parent::value();
     49 			if ( ! is_array( $value ) ) {
     50 						$value = array();
     51 			}
     52 
     53 			return $value;
     54 		}
     55 
     56 		/**
     57 		 * Convert the JSON encoded setting coming from Customizer to an Array.
     58 		 *
     59 		 * @access public
     60 		 * @param string $value URL Encoded JSON Value.
     61 		 * @return array
     62 		 */
     63 		public function sanitize_repeater_setting( $value ) {
     64 
     65 			if ( ! is_array( $value ) ) {
     66 				$value = json_decode( urldecode( $value ) );
     67 			}
     68 			$sanitized = ( empty( $value ) || ! is_array( $value ) ) ? array() : $value;
     69 
     70 			// Make sure that every row is an array, not an object.
     71 			foreach ( $sanitized as $key => $_value ) {
     72 				if ( empty( $_value ) ) {
     73 					unset( $sanitized[ $key ] );
     74 				} else {
     75 					$sanitized[ $key ] = (array) $_value;
     76 				}
     77 			}
     78 
     79 			// Reindex array.
     80 			$sanitized = array_values( $sanitized );
     81 
     82 			return $sanitized;
     83 
     84 		}
     85 	}
     86 }