ru-se.com

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

class-kirki-values.php (5516B)


      1 <?php
      2 /**
      3  * Hekoers to get the values of a field.
      4  * WARNING: PLEASE DO NOT USE THESE.
      5  * we only have these for backwards-compatibility purposes.
      6  * please use get_option() & get_theme_mod() instead.
      7  *
      8  * @package     Kirki
      9  * @category    Core
     10  * @author      Aristeides Stathopoulos
     11  * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
     12  * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
     13  * @since       1.0
     14  */
     15 
     16 if ( ! class_exists( 'Kirki_Values' ) ) {
     17 
     18 	/**
     19 	 * Wrapper class for static methods.
     20 	 */
     21 	class Kirki_Values {
     22 
     23 		/**
     24 		 * Get the value of a field.
     25 		 *
     26 		 * @static
     27 		 * @access public
     28 		 * @param string $config_id The configuration ID. @see Kirki_Config.
     29 		 * @param string $field_id  The field ID.
     30 		 * @return string|array
     31 		 */
     32 		public static function get_value( $config_id = '', $field_id = '' ) {
     33 
     34 			// Make sure value is defined.
     35 			$value = '';
     36 
     37 			// This allows us to skip the $config_id argument.
     38 			// If we skip adding a $config_id, use the 'global' configuration.
     39 			if ( ( '' === $field_id ) && '' !== $config_id ) {
     40 				$field_id  = $config_id;
     41 				$config_id = 'global';
     42 			}
     43 
     44 			// If $config_id is empty, set it to 'global'.
     45 			$config_id = ( '' === $config_id ) ? 'global' : $config_id;
     46 
     47 			// Fallback to 'global' if $config_id is not found.
     48 			if ( ! isset( Kirki::$config[ $config_id ] ) ) {
     49 				$config_id = 'global';
     50 			}
     51 
     52 			if ( 'theme_mod' === Kirki::$config[ $config_id ]['option_type'] ) {
     53 
     54 				// We're using theme_mods so just get the value using get_theme_mod.
     55 				$default_value = null;
     56 				if ( isset( Kirki::$fields[ $field_id ] ) && isset( Kirki::$fields[ $field_id ]['default'] ) ) {
     57 					$default_value = Kirki::$fields[ $field_id ]['default'];
     58 				}
     59 				$value = get_theme_mod( $field_id, $default_value );
     60 
     61 				// If the field is a background field, then get the sub-fields
     62 				// and return an array of the values.
     63 				if ( isset( Kirki::$fields[ $field_id ] ) && isset( Kirki::$fields[ $field_id ]['type'] ) && 'background' === Kirki::$fields[ $field_id ]['type'] ) {
     64 					$value = array();
     65 					if ( null === $default_value ) {
     66 						$default_value = array();
     67 					}
     68 					foreach ( $default_value as $property_key => $property_default ) {
     69 						$value[ $property_key ] = get_theme_mod( $field_id . '_' . $property_key, $property_default );
     70 					}
     71 				}
     72 			} elseif ( 'option' === Kirki::$config[ $config_id ]['option_type'] ) {
     73 
     74 				// We're using options.
     75 				if ( '' !== Kirki::$config[ $config_id ]['option_name'] ) {
     76 
     77 					// Options are serialized as a single option in the db.
     78 					// We'll have to get the option and then get the item from the array.
     79 					$options = get_option( Kirki::$config[ $config_id ]['option_name'] );
     80 
     81 					if ( ! isset( Kirki::$fields[ $field_id ] ) && isset( Kirki::$fields[ Kirki::$config[ $config_id ]['option_name'] . '[' . $field_id . ']' ] ) ) {
     82 						$field_id = Kirki::$config[ $config_id ]['option_name'] . '[' . $field_id . ']';
     83 					}
     84 					$setting_modified = str_replace( ']', '', str_replace( Kirki::$config[ $config_id ]['option_name'] . '[', '', $field_id ) );
     85 
     86 					// If this is a background field, get the individual sub-fields and return an array.
     87 					if ( 'background' === Kirki::$fields[ $field_id ]['type'] ) {
     88 						$value = array();
     89 
     90 						foreach ( Kirki::$fields[ $field_id ]['default'] as $property => $property_default ) {
     91 
     92 							if ( isset( $options[ $setting_modified . '_' . $property ] ) ) {
     93 								$value[ $property ] = $options[ $setting_modified . '_' . $property ];
     94 							} else {
     95 								$value[ $property ] = $property_default;
     96 							}
     97 						}
     98 					} else {
     99 
    100 						// This is not a background field so continue and get the value.
    101 						$value = ( isset( $options[ $setting_modified ] ) ) ? $options[ $setting_modified ] : Kirki::$fields[ $field_id ]['default'];
    102 						$value = maybe_unserialize( $value );
    103 					}
    104 				} else {
    105 
    106 					// Each option separately saved in the db.
    107 					$value = get_option( $field_id, Kirki::$fields[ $field_id ]['default'] );
    108 
    109 					// If the field is a background field, then get the sub-fields.
    110 					// and return an array of the values.
    111 					if ( 'background' === Kirki::$fields[ $field_id ]['type'] ) {
    112 						$value = array();
    113 						foreach ( Kirki::$fields[ $field_id ]['default'] as $property_key => $property_default ) {
    114 							$value[ $property_key ] = get_option( $field_id . '_' . $property_key, $property_default );
    115 						}
    116 					}
    117 				}
    118 			}
    119 
    120 			return apply_filters( 'kirki/values/get_value', $value, $field_id );
    121 
    122 		}
    123 
    124 		/**
    125 		 * Gets the value or fallsback to default.
    126 		 *
    127 		 * @static
    128 		 * @access public
    129 		 * @param array $field The field aruments.
    130 		 * @return string|array
    131 		 */
    132 		public static function get_sanitized_field_value( $field ) {
    133 			$value = $field['default'];
    134 			if ( isset( $field['option_type'] ) && 'theme_mod' === $field['option_type'] ) {
    135 				$value = get_theme_mod( $field['settings'], $field['default'] );
    136 			} else if ( isset( $field['option_type'] ) && 'option' === $field['option_type'] ) {
    137 				if ( isset( $field['option_name'] ) && '' !== $field['option_name'] ) {
    138 					$all_values = get_option( $field['option_name'], array() );
    139 					$sub_setting_id = str_replace( array( ']', $field['option_name'] . '[' ), '', $field['settings'] );
    140 					if ( isset( $all_values[ $sub_setting_id ] ) ) {
    141 						$value = $all_values[ $sub_setting_id ];
    142 					}
    143 				} else {
    144 					$value = get_option( $field['settings'], $field['default'] );
    145 				}
    146 			}
    147 
    148 			return $value;
    149 
    150 		}
    151 	}
    152 }