| @@ -1,0 +1,278 @@ | ||
| 1 | +<?php | |
| 2 | +/* | |
| 3 | + * License: GPLv3 | |
| 4 | + * License URI: https://www.gnu.org/licenses/gpl.txt | |
| 5 | + * Copyright 2012-2026 Jean-Sebastien Morisset (https://surniaulula.com/) | |
| 6 | + */ | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + | |
| 10 | + die( 'These aren\'t the droids you\'re looking for.' ); | |
| 11 | +} | |
| 12 | + | |
| 13 | +if ( ! class_exists( 'SucomUtil' ) ) { | |
| 14 | + | |
| 15 | + require_once dirname( __FILE__ ) . '/util.php'; | |
| 16 | +} | |
| 17 | + | |
| 18 | +If ( ! class_exists( 'SucomUtilOptions' ) ) { | |
| 19 | + | |
| 20 | + class SucomUtilOptions extends SucomUtil { | |
| 21 | + | |
| 22 | + public function __construct() {} | |
| 23 | + | |
| 24 | + /* | |
| 25 | + * OPTIONS ARRAY METHODS: | |
| 26 | + * | |
| 27 | + * get_opts_begin() | |
| 28 | + * get_opts_hm_tz() | |
| 29 | + * get_opts_labels_transl() | |
| 30 | + * get_opts_values_transl() | |
| 31 | + * get_key_locale() | |
| 32 | + * get_key_value() | |
| 33 | + * get_key_values_multi() | |
| 34 | + * set_key_value() | |
| 35 | + * set_key_value_disabled() | |
| 36 | + * set_key_value_locale() | |
| 37 | + * set_key_value_locale_disabled() | |
| 38 | + * transl_key_values() | |
| 39 | + */ | |
| 40 | + public static function get_opts_begin( array $opts, $str ) { | |
| 41 | + | |
| 42 | + $found = array(); | |
| 43 | + | |
| 44 | + if ( ! is_string( $str ) ) return $found; | |
| 45 | + | |
| 46 | + foreach ( $opts as $key => $value ) { | |
| 47 | + | |
| 48 | + if ( 0 === strpos( $key, $str ) ) { | |
| 49 | + | |
| 50 | + $found[ $key ] = $value; | |
| 51 | + } | |
| 52 | + } | |
| 53 | + | |
| 54 | + return $found; | |
| 55 | + } | |
| 56 | + | |
| 57 | + public static function get_opts_hm_tz( array $opts, $key_hm, $key_tz = '' ) { | |
| 58 | + | |
| 59 | + if ( ! empty( $opts[ $key_hm ] ) ) { | |
| 60 | + | |
| 61 | + $timezone = empty( $key_tz ) || empty( $opts[ $key_tz ] ) ? self::get_default_timezone() : $opts[ $key_tz ]; | |
| 62 | + | |
| 63 | + $tz_offset = self::get_timezone_offset_hours( $timezone ); | |
| 64 | + | |
| 65 | + return $opts[ $key_hm ] . ':00' . $tz_offset; | |
| 66 | + } | |
| 67 | + | |
| 68 | + return false; | |
| 69 | + } | |
| 70 | + | |
| 71 | + public static function get_opts_labels_transl( array $opts, $text_domain ) { | |
| 72 | + | |
| 73 | + foreach ( $opts as $opt_key => &$opt_label ) { | |
| 74 | + | |
| 75 | + /* | |
| 76 | + * The static value of array option labels is pre-defined in a different PHP file, like | |
| 77 | + * gettext/gettext-lib-config.php for example. | |
| 78 | + */ | |
| 79 | + $opt_label = _x( $opt_label, 'option label', $text_domain ); | |
| 80 | + } | |
| 81 | + | |
| 82 | + self::natasort( $opts ); | |
| 83 | + | |
| 84 | + return $opts; | |
| 85 | + } | |
| 86 | + | |
| 87 | + public static function get_opts_values_transl( array $opts, $text_domain ) { | |
| 88 | + | |
| 89 | + foreach ( $opts as $opt_key => &$opt_label ) { | |
| 90 | + | |
| 91 | + /* | |
| 92 | + * The static value of array option labels is pre-defined in a different PHP file, like | |
| 93 | + * gettext/gettext-lib-config.php for example. | |
| 94 | + */ | |
| 95 | + $opt_label = _x( $opt_label, 'option value', $text_domain ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + return $opts; | |
| 99 | + } | |
| 100 | + | |
| 101 | + /* | |
| 102 | + * Localize an options array key. | |
| 103 | + * | |
| 104 | + * $opts = false | array | |
| 105 | + * | |
| 106 | + * $mixed = 'default' | 'current' | post ID | $mod array | |
| 107 | + */ | |
| 108 | + public static function get_key_locale( $key, $opts = false, $mixed = 'current' ) { | |
| 109 | + | |
| 110 | + if ( ! class_exists( 'SucomUtilWP' ) ) { | |
| 111 | + | |
| 112 | + require_once dirname( __FILE__ ) . '/util-wp.php'; | |
| 113 | + } | |
| 114 | + | |
| 115 | + if ( false !== ( $pos = strpos( $key, '#' ) ) ) { // Maybe remove pre-existing locale. | |
| 116 | + | |
| 117 | + $key = substr( $key, 0, $pos ); | |
| 118 | + } | |
| 119 | + | |
| 120 | + $locale = SucomUtilWP::get_locale( $mixed ); // Uses a local cache. | |
| 121 | + $def_locale = SucomUtilWP::get_locale( 'default' ); // Uses a local cache. | |
| 122 | + $key_locale = $key . '#' . $locale; // Maybe add the current or default locale. | |
| 123 | + | |
| 124 | + if ( $locale === $def_locale ) { | |
| 125 | + | |
| 126 | + /* | |
| 127 | + * The default language for the WordPress site may have changed in the past, so if we're using the | |
| 128 | + * default, check for a locale version of the default language. | |
| 129 | + */ | |
| 130 | + if ( isset( $opts[ $key_locale ] ) ) { | |
| 131 | + | |
| 132 | + return $key_locale; | |
| 133 | + } | |
| 134 | + | |
| 135 | + return $key; | |
| 136 | + } | |
| 137 | + | |
| 138 | + return $key_locale; | |
| 139 | + } | |
| 140 | + | |
| 141 | + /* | |
| 142 | + * Returns a localized option value or null. | |
| 143 | + * | |
| 144 | + * Note that for non-existing keys or empty value strings, this methods returns the default non-localized value. | |
| 145 | + * | |
| 146 | + * $mixed = 'default' | 'current' | post ID | $mod array | |
| 147 | + */ | |
| 148 | + public static function get_key_value( $key, array $opts, $mixed = 'current' ) { | |
| 149 | + | |
| 150 | + $key_locale = self::get_key_locale( $key, $opts, $mixed ); | |
| 151 | + | |
| 152 | + $value = isset( $opts[ $key_locale ] ) ? $opts[ $key_locale ] : null; // Null if key does not exist. | |
| 153 | + | |
| 154 | + if ( null === $value || '' === $value ) { // Maybe fallback to the default non-localized value. | |
| 155 | + | |
| 156 | + if ( false === strpos( $key_locale, '#' ) ) { // The option key not localized, return null or empty string. | |
| 157 | + | |
| 158 | + return $value; | |
| 159 | + } | |
| 160 | + | |
| 161 | + $key_default = self::get_key_locale( $key_locale, $opts, 'default' ); | |
| 162 | + | |
| 163 | + if ( $key_locale !== $key_default ) { // The option key is localized and it's not the default locale. | |
| 164 | + | |
| 165 | + /* | |
| 166 | + * If the $key_locale value is an empty string, and $key_default does not exist, then | |
| 167 | + * return the emty string. | |
| 168 | + */ | |
| 169 | + return isset( $opts[ $key_default ] ) ? $opts[ $key_default ] : $value; | |
| 170 | + } | |
| 171 | + } | |
| 172 | + | |
| 173 | + return $value; | |
| 174 | + } | |
| 175 | + | |
| 176 | + public static function get_key_values_multi( $prefix, array &$opts, $add_none = false ) { | |
| 177 | + | |
| 178 | + if ( ! class_exists( 'SucomUtilWP' ) ) { | |
| 179 | + | |
| 180 | + require_once dirname( __FILE__ ) . '/util-wp.php'; | |
| 181 | + } | |
| 182 | + | |
| 183 | + $current = SucomUtilWP::get_locale(); // Uses a local cache. | |
| 184 | + $def_locale = SucomUtilWP::get_locale( 'default' ); // Uses a local cache. | |
| 185 | + $matches = self::preg_grep_keys( '/^' . $prefix . '_([0-9]+)(#.*)?$/', $opts ); | |
| 186 | + $results = array(); | |
| 187 | + | |
| 188 | + foreach ( $matches as $key => $value ) { | |
| 189 | + | |
| 190 | + $num = preg_replace( '/^' . $prefix . '_([0-9]+)(#.*)?$/', '$1', $key ); | |
| 191 | + | |
| 192 | + if ( ! empty( $results[ $num ] ) ) { // Preserve the first non-blank value. | |
| 193 | + | |
| 194 | + continue; | |
| 195 | + | |
| 196 | + } elseif ( ! empty( $opts[ $prefix . '_' . $num . '#' . $current ] ) ) { // Current locale. | |
| 197 | + | |
| 198 | + $results[ $num ] = $opts[ $prefix . '_' . $num . '#' . $current ]; | |
| 199 | + | |
| 200 | + } elseif ( ! empty( $opts[ $prefix . '_' . $num . '#' . $def_locale ] ) ) { // Default locale. | |
| 201 | + | |
| 202 | + $results[ $num ] = $opts[ $prefix . '_' . $num . '#' . $def_locale ]; | |
| 203 | + | |
| 204 | + } elseif ( ! empty( $opts[ $prefix . '_' . $num ] ) ) { // No locale. | |
| 205 | + | |
| 206 | + $results[ $num ] = $opts[ $prefix . '_' . $num ]; | |
| 207 | + | |
| 208 | + } else { // Use value (could be empty). | |
| 209 | + | |
| 210 | + $results[ $num ] = $value; | |
| 211 | + } | |
| 212 | + } | |
| 213 | + | |
| 214 | + asort( $results ); // Sort values for display. | |
| 215 | + | |
| 216 | + if ( $add_none ) { | |
| 217 | + | |
| 218 | + /* | |
| 219 | + * The union operator (+) gives priority to values in the first array, while array_replace() gives | |
| 220 | + * priority to values in the the second array. | |
| 221 | + */ | |
| 222 | + $results = array( 'none' => 'none' ) + $results; // Maintains numeric index. | |
| 223 | + } | |
| 224 | + | |
| 225 | + return $results; | |
| 226 | + } | |
| 227 | + | |
| 228 | + public static function set_key_value( $key, $value, array &$opts ) { | |
| 229 | + | |
| 230 | + $opts[ $key ] = $value; | |
| 231 | + } | |
| 232 | + | |
| 233 | + public static function set_key_value_disabled( $key, $value, array &$opts ) { | |
| 234 | + | |
| 235 | + $opts[ $key ] = $value; | |
| 236 | + | |
| 237 | + $opts[ $key . ':disabled' ] = true; | |
| 238 | + } | |
| 239 | + | |
| 240 | + public static function set_key_value_locale( $key, $value, array &$opts, $mixed = 'current' ) { | |
| 241 | + | |
| 242 | + $key_locale = self::get_key_locale( $key, $opts, $mixed ); | |
| 243 | + | |
| 244 | + $opts[ $key_locale ] = $value; | |
| 245 | + } | |
| 246 | + | |
| 247 | + public static function set_key_value_locale_disabled( $key, $value, array &$opts, $mixed = 'current' ) { | |
| 248 | + | |
| 249 | + $key_locale = self::get_key_locale( $key, $opts, $mixed ); | |
| 250 | + | |
| 251 | + $opts[ $key_locale ] = $value; | |
| 252 | + | |
| 253 | + $opts[ $key_locale . ':disabled' ] = true; | |
| 254 | + } | |
| 255 | + | |
| 256 | + public static function transl_key_values( $pattern, array &$opts, $text_domain ) { | |
| 257 | + | |
| 258 | + foreach ( self::preg_grep_keys( $pattern, $opts ) as $key => $val ) { | |
| 259 | + | |
| 260 | + $locale_key = self::get_key_locale( $key ); | |
| 261 | + | |
| 262 | + if ( $locale_key !== $key && empty( $opts[ $locale_key ] ) ) { | |
| 263 | + | |
| 264 | + /* | |
| 265 | + * The static value of array option labels is pre-defined in a different PHP file, like | |
| 266 | + * gettext/gettext-lib-config.php for example. | |
| 267 | + */ | |
| 268 | + $val_transl = _x( $val, 'option value', $text_domain ); | |
| 269 | + | |
| 270 | + if ( $val_transl !== $val ) { | |
| 271 | + | |
| 272 | + $opts[ $locale_key ] = $val_transl; | |
| 273 | + } | |
| 274 | + } | |
| 275 | + } | |
| 276 | + } | |
| 277 | + } | |
| 278 | +} | |