deprecated
1 month ago
Aliases.php
2 months ago
Config.php
5 months ago
Control.php
5 months ago
Deprecated.php
5 months ago
Field.php
2 months ago
Framework.php
5 months ago
Init.php
2 months ago
Kirki.php
2 months ago
Modules.php
2 months ago
Pro_Namespace_Compatibility.php
5 months ago
Sanitize_Values.php
5 months ago
Scripts.php
2 months ago
Settings.php
5 months ago
Values.php
5 months ago
Values.php
125 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Helpers to get the values of a field. |
| 4 | * |
| 5 | * ! WARNING: PLEASE DO NOT USE THESE. |
| 6 | * we only have these for backwards-compatibility purposes. |
| 7 | * please use get_option() & get_theme_mod() instead. |
| 8 | * |
| 9 | * @package Kirki |
| 10 | * @category Core |
| 11 | * @author Themeum |
| 12 | * @copyright Copyright (c) 2023, Themeum |
| 13 | * @license https://opensource.org/licenses/MIT |
| 14 | * @since 1.0 |
| 15 | */ |
| 16 | |
| 17 | namespace Kirki\Compatibility; |
| 18 | |
| 19 | /** |
| 20 | * Wrapper class for static methods. |
| 21 | */ |
| 22 | class Values { |
| 23 | |
| 24 | /** |
| 25 | * Get the value of a field. |
| 26 | * |
| 27 | * @static |
| 28 | * @access public |
| 29 | * @param string $config_id The configuration ID. @see Kirki\Compatibility\Config. |
| 30 | * @param string $field_id The field ID. |
| 31 | * @return string|array |
| 32 | */ |
| 33 | public static function get_value( $config_id = '', $field_id = '' ) { |
| 34 | |
| 35 | // Make sure value is defined. |
| 36 | $value = ''; |
| 37 | |
| 38 | // This allows us to skip the $config_id argument. |
| 39 | // If we skip adding a $config_id, use the 'global' configuration. |
| 40 | if ( ( '' === $field_id ) && '' !== $config_id ) { |
| 41 | $field_id = $config_id; |
| 42 | $config_id = 'global'; |
| 43 | } |
| 44 | |
| 45 | // If $config_id is empty, set it to 'global'. |
| 46 | $config_id = ( '' === $config_id ) ? 'global' : $config_id; |
| 47 | |
| 48 | // Fallback to 'global' if $config_id is not found. |
| 49 | if ( ! isset( Kirki::$config[ $config_id ] ) ) { |
| 50 | $config_id = 'global'; |
| 51 | } |
| 52 | |
| 53 | if ( 'theme_mod' === Kirki::$config[ $config_id ]['option_type'] ) { |
| 54 | |
| 55 | // We're using theme_mods so just get the value using get_theme_mod. |
| 56 | $default_value = null; |
| 57 | |
| 58 | if ( isset( Kirki::$all_fields[ $field_id ] ) && isset( Kirki::$all_fields[ $field_id ]['default'] ) ) { |
| 59 | $default_value = Kirki::$all_fields[ $field_id ]['default']; |
| 60 | } |
| 61 | |
| 62 | $value = get_theme_mod( $field_id, $default_value ); |
| 63 | |
| 64 | return apply_filters( 'kirki_values_get_value', $value, $field_id ); |
| 65 | } |
| 66 | |
| 67 | if ( 'option' === Kirki::$config[ $config_id ]['option_type'] ) { |
| 68 | |
| 69 | // We're using options. |
| 70 | if ( '' !== Kirki::$config[ $config_id ]['option_name'] ) { |
| 71 | // Options are serialized as a single option in the db. |
| 72 | // We'll have to get the option and then get the item from the array. |
| 73 | $options = get_option( Kirki::$config[ $config_id ]['option_name'] ); |
| 74 | |
| 75 | if ( ! isset( Kirki::$all_fields[ $field_id ] ) && isset( Kirki::$all_fields[ Kirki::$config[ $config_id ]['option_name'] . '[' . $field_id . ']' ] ) ) { |
| 76 | $field_id = Kirki::$config[ $config_id ]['option_name'] . '[' . $field_id . ']'; |
| 77 | } |
| 78 | |
| 79 | $setting_modified = str_replace( ']', '', str_replace( Kirki::$config[ $config_id ]['option_name'] . '[', '', $field_id ) ); |
| 80 | |
| 81 | $default_value = ( isset( Kirki::$all_fields[ $field_id ] ) && isset( Kirki::$all_fields[ $field_id ]['default'] ) ) ? Kirki::$all_fields[ $field_id ]['default'] : ''; |
| 82 | $value = ( isset( $options[ $setting_modified ] ) ) ? $options[ $setting_modified ] : $default_value; |
| 83 | $value = maybe_unserialize( $value ); |
| 84 | |
| 85 | return apply_filters( 'kirki_values_get_value', $value, $field_id ); |
| 86 | } |
| 87 | |
| 88 | // Each option separately saved in the db. |
| 89 | $value = get_option( $field_id, Kirki::$all_fields[ $field_id ]['default'] ); |
| 90 | |
| 91 | return apply_filters( 'kirki_values_get_value', $value, $field_id ); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | return apply_filters( 'kirki_values_get_value', $value, $field_id ); |
| 96 | |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Gets the value or fallsback to default. |
| 101 | * |
| 102 | * @static |
| 103 | * @access public |
| 104 | * @param array $field The field arguments. |
| 105 | * @return string|array |
| 106 | */ |
| 107 | public static function get_sanitized_field_value( $field ) { |
| 108 | $value = $field['default']; |
| 109 | if ( ! isset( $field['option_type'] ) || 'theme_mod' === $field['option_type'] ) { |
| 110 | $value = get_theme_mod( $field['settings'], $field['default'] ); |
| 111 | } elseif ( isset( $field['option_type'] ) && 'option' === $field['option_type'] ) { |
| 112 | if ( isset( $field['option_name'] ) && '' !== $field['option_name'] ) { |
| 113 | $all_values = get_option( $field['option_name'], [] ); |
| 114 | $sub_setting_id = str_replace( [ ']', $field['option_name'] . '[' ], '', $field['settings'] ); |
| 115 | if ( isset( $all_values[ $sub_setting_id ] ) ) { |
| 116 | $value = $all_values[ $sub_setting_id ]; |
| 117 | } |
| 118 | } else { |
| 119 | $value = get_option( $field['settings'], $field['default'] ); |
| 120 | } |
| 121 | } |
| 122 | return $value; |
| 123 | } |
| 124 | } |
| 125 |