Option.php
185 lines
| 1 | <?php // phpcs:disable PHPCompatibility.FunctionDeclarations.NewClosure |
| 2 | /** |
| 3 | * Option tweaks. |
| 4 | * |
| 5 | * @package kirki-framework/data-option |
| 6 | * @copyright Copyright (c) 2023, Themeum |
| 7 | * @license https://opensource.org/licenses/MIT |
| 8 | * @since 1.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Kirki\Data; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Hooks and tweaks to allow Kirki saving Options instead of theme-mods. |
| 19 | * |
| 20 | * @since 1.0 |
| 21 | */ |
| 22 | class Option { |
| 23 | |
| 24 | /** |
| 25 | * Constructor. |
| 26 | * |
| 27 | * @access public |
| 28 | * @since 1.0 |
| 29 | */ |
| 30 | public function __construct() { |
| 31 | add_filter( 'kirki_field_add_setting_args', [ $this, 'add_setting_args' ], 20, 2 ); |
| 32 | add_filter( 'kirki_field_add_control_args', [ $this, 'add_control_args' ], 20, 2 ); |
| 33 | add_filter( 'kirki_get_value', [ $this, 'kirki_get_value' ], 10, 4 ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Filters the value for an option. |
| 38 | * |
| 39 | * @access public |
| 40 | * @since 4.0 |
| 41 | * @param mixed $value The value. |
| 42 | * @param string $option The field-name. |
| 43 | * @param mixed $default The default value. |
| 44 | * @param string $type The option-type (theme_mod, option etc). |
| 45 | * @return mixed Returns the field value. |
| 46 | */ |
| 47 | public function kirki_get_value( $value = '', $option = '', $default = '', $type = 'theme_mod' ) { |
| 48 | |
| 49 | if ( 'option' === $type ) { |
| 50 | |
| 51 | /** |
| 52 | * If the option doesn't contain a '[', then it's not a sub-item |
| 53 | * of another option. Get the option value and return it. |
| 54 | */ |
| 55 | if ( false === strpos( $option, '[' ) ) { |
| 56 | return get_option( $option, $default ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * If we got here then this is part of an option array. |
| 61 | * We need to get the 1st level, and then find the item inside that array. |
| 62 | */ |
| 63 | $parts = \explode( '[', $option ); |
| 64 | $value = get_option( $parts[0], [] ); |
| 65 | |
| 66 | // If there's no value, return the default. |
| 67 | if ( empty( $value ) ) { |
| 68 | return $default; |
| 69 | } |
| 70 | |
| 71 | foreach ( $parts as $key => $part ) { |
| 72 | /** |
| 73 | * Skip the 1st item, it's already been dealt with |
| 74 | * when we got the value initially right before this loop. |
| 75 | */ |
| 76 | if ( 0 === $key ) { |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | $part = str_replace( ']', '', $part ); |
| 81 | |
| 82 | /** |
| 83 | * If the item exists in the value, then change $value to the item. |
| 84 | * This runs recursively for all parts until we get to the end. |
| 85 | */ |
| 86 | if ( is_array( $value ) && isset( $value[ $part ] ) ) { |
| 87 | $value = $value[ $part ]; |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * If we got here, the item was not found in the value. |
| 93 | * We need to change the value accordingly depending on whether |
| 94 | * this is the last item in the loop or not. |
| 95 | */ |
| 96 | $value = ( isset( $parts[ $key + 1 ] ) ) ? [] : ''; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return $value; |
| 101 | |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Allow filtering the arguments. |
| 106 | * |
| 107 | * @since 0.1 |
| 108 | * @param array $args The arguments. |
| 109 | * @param WP_Customize_Manager $customizer The customizer instance. |
| 110 | * @return array Return the arguments. |
| 111 | */ |
| 112 | public function add_setting_args( $args, $customizer ) { |
| 113 | |
| 114 | // If this is not an option, early exit. |
| 115 | if ( ! isset( $args['option_type'] ) || 'option' !== $args['option_type'] ) { |
| 116 | return $args; |
| 117 | } |
| 118 | |
| 119 | // Set "type" argument to option. |
| 120 | $args['type'] = 'option'; |
| 121 | return $this->maybe_change_settings( $args ); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Allow filtering the arguments. |
| 127 | * |
| 128 | * @since 0.1 |
| 129 | * @param array $args The arguments. |
| 130 | * @param WP_Customize_Manager $customizer The customizer instance. |
| 131 | * @return array Return the arguments. |
| 132 | */ |
| 133 | public function add_control_args( $args, $customizer ) { |
| 134 | |
| 135 | // If this is not an option, early exit. |
| 136 | if ( ! isset( $args['option_type'] ) || 'option' !== $args['option_type'] ) { |
| 137 | return $args; |
| 138 | } |
| 139 | |
| 140 | return $this->maybe_change_settings( $args ); |
| 141 | |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Change the settings argument. |
| 146 | * |
| 147 | * @access private |
| 148 | * @since 1.0 |
| 149 | * @param array $args The arguments. |
| 150 | * @return array Returns modified array with tweaks to the [settings] argument if needed. |
| 151 | */ |
| 152 | private function maybe_change_settings( $args ) { |
| 153 | |
| 154 | // Check if we have an option-name defined. |
| 155 | if ( isset( $args['option_name'] ) ) { |
| 156 | if ( empty( $args['option_name'] ) ) { |
| 157 | return $args; |
| 158 | } |
| 159 | |
| 160 | if ( isset( $args['settings'] ) && $args['settings'] && false !== strpos( $args['settings'], $args['option_name'] . '[' ) ) { |
| 161 | return $args; |
| 162 | } |
| 163 | |
| 164 | if ( false === strpos( $args['settings'], '[' ) ) { |
| 165 | // ? Bagus: in line above, it's obvious that '[' is not found in $args['settings']. But why do we explode it using '[' here? |
| 166 | $parts = explode( '[', $args['settings'] ); |
| 167 | $final_parts = [ $args['option_name'] ]; |
| 168 | |
| 169 | foreach ( $parts as $part ) { |
| 170 | $final_parts[] = $part; |
| 171 | } |
| 172 | |
| 173 | $args['settings'] = \implode( '][', $final_parts ) . ']'; |
| 174 | $args['settings'] = str_replace( |
| 175 | $args['option_name'] . '][', |
| 176 | $args['option_name'] . '[', |
| 177 | $args['settings'] |
| 178 | ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return $args; |
| 183 | |
| 184 | } |
| 185 | } |