@@ -542,7 +542,7 @@ discard block |
||
| 542 | 542 | } |
| 543 | 543 | } |
| 544 | 544 | |
| 545 | - return (bool) $return; |
|
| 545 | + return ( bool ) $return; |
|
| 546 | 546 | } |
| 547 | 547 | |
| 548 | 548 | /** |
@@ -942,7 +942,7 @@ discard block |
||
| 942 | 942 | } |
| 943 | 943 | } |
| 944 | 944 | |
| 945 | - $ret[ $struc['name'] ] = $struc; |
|
| 945 | + $ret[$struc['name']] = $struc; |
|
| 946 | 946 | } |
| 947 | 947 | |
| 948 | 948 | $dir->close(); |
@@ -13,1072 +13,1072 @@ |
||
| 13 | 13 | |
| 14 | 14 | if ( ! class_exists( 'Redux_Filesystem', false ) ) { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Class Redux_Filesystem |
|
| 18 | - */ |
|
| 19 | - class Redux_Filesystem { |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * Instance of this class. |
|
| 23 | - * |
|
| 24 | - * @since 1.0.0 |
|
| 25 | - * @var null|Redux_Filesystem |
|
| 26 | - */ |
|
| 27 | - protected static ?Redux_Filesystem $instance = null; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * WP Filesystem object. |
|
| 31 | - * |
|
| 32 | - * @var null|WP_Filesystem_Direct |
|
| 33 | - */ |
|
| 34 | - protected static ?WP_Filesystem_Direct $direct = null; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * File system credentials. |
|
| 38 | - * |
|
| 39 | - * @var array|bool|null |
|
| 40 | - */ |
|
| 41 | - private $creds; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * ReduxFramework object pointer. |
|
| 45 | - * |
|
| 46 | - * @var null|ReduxFramework |
|
| 47 | - */ |
|
| 48 | - public ?ReduxFramework $parent = null; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Instance of WP_Filesystem |
|
| 52 | - * |
|
| 53 | - * @var WP_Filesystem_Base|null |
|
| 54 | - */ |
|
| 55 | - private ?WP_Filesystem_Base $wp_filesystem = null; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * If DBI_Filesystem should attempt to use the WP_Filesystem class. |
|
| 59 | - * |
|
| 60 | - * @var bool |
|
| 61 | - */ |
|
| 62 | - private bool $use_filesystem = false; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Default chmod octal value for directories. |
|
| 66 | - * |
|
| 67 | - * @var int |
|
| 68 | - */ |
|
| 69 | - private int $chmod_dir; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Default chmod octal value for files. |
|
| 73 | - * |
|
| 74 | - * @var int |
|
| 75 | - */ |
|
| 76 | - private int $chmod_file; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Default cache folder. |
|
| 80 | - * |
|
| 81 | - * @var string |
|
| 82 | - */ |
|
| 83 | - public string $cache_folder; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Kill switch. |
|
| 87 | - * |
|
| 88 | - * @var bool |
|
| 89 | - */ |
|
| 90 | - public bool $killswitch = false; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * FTP Form HTML. |
|
| 94 | - * |
|
| 95 | - * @var string |
|
| 96 | - */ |
|
| 97 | - public string $ftp_form; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Pass `true` when instantiating to skip using WP_Filesystem. |
|
| 101 | - * |
|
| 102 | - * @param bool $force_no_fs Force no use of the filesystem. |
|
| 103 | - */ |
|
| 104 | - public function __construct( bool $force_no_fs = false ) { |
|
| 105 | - |
|
| 106 | - // This little number fixes some issues with certain filesystem setups. |
|
| 107 | - |
|
| 108 | - if ( ! function_exists( 'request_filesystem_credentials' ) ) { |
|
| 109 | - require_once ABSPATH . '/wp-admin/includes/template.php'; |
|
| 110 | - require_once ABSPATH . '/wp-includes/pluggable.php'; |
|
| 111 | - require_once ABSPATH . '/wp-admin/includes/file.php'; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - // Set default permissions. |
|
| 115 | - if ( defined( 'FS_CHMOD_DIR' ) ) { |
|
| 116 | - $this->chmod_dir = FS_CHMOD_DIR; |
|
| 117 | - } else { |
|
| 118 | - $this->chmod_dir = ( fileperms( ABSPATH ) & 0777 | 0755 ); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - if ( defined( 'FS_CHMOD_FILE' ) ) { |
|
| 122 | - $this->chmod_file = FS_CHMOD_FILE; |
|
| 123 | - } else { |
|
| 124 | - $this->chmod_file = ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if ( ! $force_no_fs && function_exists( 'request_filesystem_credentials' ) ) { |
|
| 128 | - if ( ( defined( 'WPMDB_WP_FILESYSTEM' ) && WPMDB_WP_FILESYSTEM ) || ! defined( 'WPMDB_WP_FILESYSTEM' ) ) { |
|
| 129 | - $this->maybe_init_wp_filesystem(); |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $uploads_dir = wp_upload_dir(); |
|
| 134 | - $this->cache_folder = trailingslashit( $uploads_dir['basedir'] ) . 'redux/'; |
|
| 135 | - if ( ! $this->file_exists( $this->cache_folder ) ) { |
|
| 136 | - $this->mkdir( $this->cache_folder ); |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Return an instance of this class. |
|
| 142 | - * |
|
| 143 | - * @param ReduxFramework|null $me ReduxFramework pointer. |
|
| 144 | - * |
|
| 145 | - * @return object A single instance of this class. |
|
| 146 | - * @since 1.0.0 |
|
| 147 | - */ |
|
| 148 | - public static function get_instance( ?ReduxFramework $me = null ): ?object { |
|
| 149 | - |
|
| 150 | - // If the single instance hasn't been set, set it now. |
|
| 151 | - if ( null === self::$instance ) { |
|
| 152 | - self::$instance = new self(); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - if ( null !== $me ) { |
|
| 156 | - self::$instance->parent = $me; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return self::$instance; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Build an FTP form. |
|
| 164 | - */ |
|
| 165 | - public function ftp_form() { |
|
| 166 | - if ( isset( $this->ftp_form ) && ! empty( $this->ftp_form ) ) { |
|
| 167 | - echo '<div class="wrap">'; |
|
| 168 | - echo '<div class="error">'; |
|
| 169 | - echo '<p>'; |
|
| 170 | - // translators: %1$s: Upload URL. %2$s: Codex URL. |
|
| 171 | - echo '<strong>' . esc_html__( 'File Permission Issues', 'redux-framework' ) . '</strong><br/>' . sprintf( esc_html__( 'We were unable to modify required files. Please ensure that %1$s has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as %2$s.', 'redux-framework' ), '<code>' . esc_url( Redux_Functions_Ex::wp_normalize_path( trailingslashit( WP_CONTENT_DIR ) ) . '/uploads/' ) . '</code>', ' <a href="https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants" target="_blank">' . esc_html__( 'outlined here', 'redux-framework' ) . '</a>' ); |
|
| 172 | - echo '</p>'; |
|
| 173 | - echo '</div>'; |
|
| 174 | - echo '<h2></h2>'; |
|
| 175 | - echo '</div>'; |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Attempt to initiate WP_Filesystem |
|
| 181 | - * If this fails, $use_filesystem is set to false and all methods in this class should use native php fallbacks |
|
| 182 | - * Thwarts `request_filesystem_credentials()` attempt to display a form for obtaining creds from users |
|
| 183 | - * TODO: provide notice and input in wp-admin for users when this fails |
|
| 184 | - */ |
|
| 185 | - public function maybe_init_wp_filesystem() { |
|
| 186 | - // Set up the filesystem with creds. |
|
| 187 | - require_once ABSPATH . '/wp-admin/includes/template.php'; |
|
| 188 | - require_once ABSPATH . '/wp-includes/pluggable.php'; |
|
| 189 | - require_once ABSPATH . '/wp-admin/includes/file.php'; |
|
| 190 | - |
|
| 191 | - ob_start(); |
|
| 192 | - $credentials = request_filesystem_credentials( '', '', false, false ); |
|
| 193 | - $ob_contents = ob_get_contents(); |
|
| 194 | - ob_end_clean(); |
|
| 195 | - |
|
| 196 | - if ( @wp_filesystem( $credentials ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors |
|
| 197 | - global $wp_filesystem; |
|
| 198 | - $this->wp_filesystem = $wp_filesystem; |
|
| 199 | - $this->use_filesystem = true; |
|
| 200 | - $this->generate_default_files(); |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Init WO Filesystem. |
|
| 206 | - * |
|
| 207 | - * @param string $form_url Form URL. |
|
| 208 | - * @param string $method Connect method. |
|
| 209 | - * @param bool $context Context. |
|
| 210 | - * |
|
| 211 | - * @return bool |
|
| 212 | - */ |
|
| 213 | - public function advanced_filesystem_init( string $form_url, string $method = '', bool $context = false ): bool { |
|
| 214 | - if ( ! empty( $this->wp_filesystem ) && $this->use_filesystem ) { |
|
| 215 | - return true; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - if ( ! empty( $this->creds ) ) { |
|
| 219 | - return true; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - ob_start(); |
|
| 223 | - |
|
| 224 | - $this->creds = request_filesystem_credentials( $form_url, $method, false, $context ); |
|
| 225 | - |
|
| 226 | - /* first attempt to get credentials */ |
|
| 227 | - if ( false === $this->creds ) { |
|
| 228 | - $this->creds = array(); |
|
| 229 | - $this->ftp_form = ob_get_contents(); |
|
| 230 | - ob_end_clean(); |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * If we come here, we don't have credentials |
|
| 234 | - * so the request for them is displaying |
|
| 235 | - * no need for further processing |
|
| 236 | - * */ |
|
| 237 | - return false; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /* now we got some credentials - try to use them */ |
|
| 241 | - if ( ! @wp_filesystem( $this->creds ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors |
|
| 242 | - $this->creds = array(); |
|
| 243 | - /* incorrect connection data - ask for credentials again, now with an error message */ |
|
| 244 | - request_filesystem_credentials( $form_url, '', true, $context ); |
|
| 245 | - $this->ftp_form = ob_get_contents(); |
|
| 246 | - ob_end_clean(); |
|
| 247 | - |
|
| 248 | - return false; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - global $wp_filesystem; |
|
| 252 | - $this->wp_filesystem = $wp_filesystem; |
|
| 253 | - $this->use_filesystem = true; |
|
| 254 | - $this->generate_default_files(); |
|
| 255 | - |
|
| 256 | - return true; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Load WP filesystem directly. |
|
| 261 | - */ |
|
| 262 | - public static function load_direct() { |
|
| 263 | - if ( null === self::$direct ) { |
|
| 264 | - require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php'; |
|
| 265 | - require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php'; |
|
| 266 | - |
|
| 267 | - self::$direct = new WP_Filesystem_Direct( array() ); |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Execute filesystem request. |
|
| 273 | - * |
|
| 274 | - * @param string $action Action to perform. |
|
| 275 | - * @param string $file File to perform upon. |
|
| 276 | - * @param array $params Argument for action. |
|
| 277 | - * |
|
| 278 | - * @return bool|void |
|
| 279 | - */ |
|
| 280 | - public function execute( string $action, string $file = '', array $params = array() ) { |
|
| 281 | - if ( empty( $this->parent->args ) ) { |
|
| 282 | - return; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - if ( ! empty( $params ) ) { |
|
| 286 | - // phpcs:ignore WordPress.PHP.DontExtract |
|
| 287 | - extract( $params ); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - if ( empty( $this->wp_filesystem ) ) { |
|
| 291 | - if ( 'submenu' === $this->parent->args['menu_type'] ) { |
|
| 292 | - $page_parent = $this->parent->args['page_parent']; |
|
| 293 | - $base = $page_parent . '?page=' . $this->parent->args['page_slug']; |
|
| 294 | - } else { |
|
| 295 | - $base = 'admin.php?page=' . $this->parent->args['page_slug']; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - $url = wp_nonce_url( $base, 'redux-options' ); |
|
| 299 | - $this->advanced_filesystem_init( $url, 'direct', dirname( $file ) ); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - return $this->do_action( $action, $file, $params ); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Generates the default Redux cache folder. |
|
| 308 | - * |
|
| 309 | - * @return void |
|
| 310 | - */ |
|
| 311 | - private function generate_default_files() { |
|
| 312 | - if ( ! $this->is_dir( Redux_Core::$upload_dir ) ) { |
|
| 313 | - $this->mkdir( Redux_Core::$upload_dir ); |
|
| 314 | - } |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Do request filesystem action. |
|
| 319 | - * |
|
| 320 | - * @param string $action Requested action. |
|
| 321 | - * @param string $file File to perform action upon. |
|
| 322 | - * @param array $params Action arguments. |
|
| 323 | - * |
|
| 324 | - * @return bool|void |
|
| 325 | - */ |
|
| 326 | - public function do_action( string $action, string $file = '', array $params = array() ) { |
|
| 327 | - $destination = ''; |
|
| 328 | - $overwrite = ''; |
|
| 329 | - $content = ''; |
|
| 330 | - |
|
| 331 | - if ( ! empty( $params ) ) { |
|
| 332 | - |
|
| 333 | - // phpcs:ignore WordPress.PHP.DontExtract |
|
| 334 | - extract( $params ); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - global $wp_filesystem; |
|
| 338 | - |
|
| 339 | - if ( defined( 'FS_CHMOD_FILE' ) ) { |
|
| 340 | - $chmod = FS_CHMOD_FILE; |
|
| 341 | - } else { |
|
| 342 | - $chmod = 0644; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - if ( isset( $params['chmod'] ) && ! empty( $params['chmod'] ) ) { |
|
| 346 | - $chmod = $params['chmod']; |
|
| 347 | - } |
|
| 348 | - $res = false; |
|
| 349 | - if ( ! isset( $recursive ) ) { |
|
| 350 | - $recursive = false; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - // Do unique stuff. |
|
| 354 | - if ( 'mkdir' === $action ) { |
|
| 355 | - $chmod = null; |
|
| 356 | - if ( isset( $params['chmod'] ) && ! empty( $params['chmod'] ) ) { |
|
| 357 | - $chmod = $params['chmod']; |
|
| 358 | - } |
|
| 359 | - $res = $this->mkdir( $file, $chmod ); |
|
| 360 | - } elseif ( 'rmdir' === $action ) { |
|
| 361 | - $res = $this->rmdir( $file, $recursive ); |
|
| 362 | - } elseif ( 'copy' === $action && false === $this->killswitch ) { |
|
| 363 | - $res = $this->copy( $file, $destination, $overwrite, $chmod ); |
|
| 364 | - } elseif ( 'move' === $action && false === $this->killswitch ) { |
|
| 365 | - $res = $this->move( $file, $destination, $overwrite ); |
|
| 366 | - } elseif ( 'delete' === $action ) { |
|
| 367 | - if ( $this->is_dir( $file ) ) { |
|
| 368 | - $res = $this->rmdir( $file, $recursive ); |
|
| 369 | - } else { |
|
| 370 | - $res = $this->unlink( $file ); |
|
| 371 | - } |
|
| 372 | - } elseif ( 'dirlist' === $action ) { |
|
| 373 | - if ( ! isset( $include_hidden ) ) { |
|
| 374 | - $include_hidden = true; |
|
| 375 | - } |
|
| 376 | - $res = $this->scandir( $file, $include_hidden, $recursive ); |
|
| 377 | - } elseif ( 'put_contents' === $action && false === $this->killswitch ) { |
|
| 378 | - // Write a string to a file. |
|
| 379 | - if ( isset( $this->ftp_form ) && ! empty( $this->ftp_form ) ) { |
|
| 380 | - self::load_direct(); |
|
| 381 | - $res = self::$direct->put_contents( $file, $content, $chmod ); |
|
| 382 | - } else { |
|
| 383 | - $res = $this->put_contents( $file, $content, $chmod ); |
|
| 384 | - } |
|
| 385 | - } elseif ( 'chown' === $action ) { |
|
| 386 | - // Changes the file owner. |
|
| 387 | - if ( isset( $owner ) && ! empty( $owner ) ) { |
|
| 388 | - $res = $wp_filesystem->chmod( $file, $chmod, $recursive ); |
|
| 389 | - } |
|
| 390 | - } elseif ( 'owner' === $action ) { |
|
| 391 | - // Gets the file owner. |
|
| 392 | - $res = $this->wp_filesystem->owner( $file ); |
|
| 393 | - } elseif ( 'chmod' === $action ) { |
|
| 394 | - if ( ! isset( $params['chmod'] ) || ( empty( $params['chmod'] ) ) ) { |
|
| 395 | - $chmod = false; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - $res = $this->chmod( $file, $chmod ); |
|
| 399 | - } elseif ( 'get_contents' === $action ) { |
|
| 400 | - // Reads entire file into a string. |
|
| 401 | - if ( isset( $this->ftp_form ) && ! empty( $this->ftp_form ) ) { |
|
| 402 | - self::load_direct(); |
|
| 403 | - $res = self::$direct->get_contents( $file ); |
|
| 404 | - } else { |
|
| 405 | - $res = $this->get_contents( $file ); |
|
| 406 | - } |
|
| 407 | - } elseif ( 'get_contents_array' === $action ) { |
|
| 408 | - // Reads entire file into an array. |
|
| 409 | - $res = $this->wp_filesystem->get_contents_array( $file ); |
|
| 410 | - } elseif ( 'object' === $action ) { |
|
| 411 | - $res = $this->wp_filesystem; |
|
| 412 | - } elseif ( 'unzip' === $action ) { |
|
| 413 | - $unzipfile = unzip_file( $file, $destination ); |
|
| 414 | - if ( $unzipfile ) { |
|
| 415 | - $res = true; |
|
| 416 | - } |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - if ( ! $res ) { |
|
| 420 | - if ( 'dirlist' === $action ) { |
|
| 421 | - if ( empty( $res ) ) { |
|
| 422 | - return; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - if ( ! is_array( $res ) ) { |
|
| 426 | - if ( count( glob( "$file*" ) ) === 0 ) { |
|
| 427 | - return; |
|
| 428 | - } |
|
| 429 | - } |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - $this->killswitch = true; |
|
| 433 | - |
|
| 434 | - // translators: %1$s: Upload URL. %2$s: Codex URL. |
|
| 435 | - $msg = '<strong>' . esc_html__( 'File Permission Issues', 'redux-framework' ) . '</strong><br/>' . sprintf( esc_html__( 'We were unable to modify required files. Please ensure that %1$s has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as %2$s.', 'redux-framework' ), '<code>' . esc_url( Redux_Functions_Ex::wp_normalize_path( trailingslashit( WP_CONTENT_DIR ) ) ) . '/uploads/</code>', '<a href="https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants" target="_blank">' . esc_html__( 'outlined here', 'redux-framework' ) . '</a>' ); |
|
| 436 | - |
|
| 437 | - $data = array( |
|
| 438 | - 'parent' => self::$instance->parent, |
|
| 439 | - 'type' => 'error', |
|
| 440 | - 'msg' => $msg, |
|
| 441 | - 'id' => 'redux-wp-login', |
|
| 442 | - 'dismiss' => false, |
|
| 443 | - ); |
|
| 444 | - |
|
| 445 | - Redux_Admin_Notices::set_notice( $data ); |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - return $res; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Getter for the instantiated WP_Filesystem. This should be used carefully since $wp_filesystem won't always have a value. |
|
| 454 | - * |
|
| 455 | - * @return WP_Filesystem_Base|false |
|
| 456 | - */ |
|
| 457 | - public function get_wp_filesystem() { |
|
| 458 | - if ( $this->use_filesystem ) { |
|
| 459 | - return $this->wp_filesystem; |
|
| 460 | - } else { |
|
| 461 | - return false; |
|
| 462 | - } |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Check if WP_Filesystem being used. |
|
| 467 | - * |
|
| 468 | - * @return bool |
|
| 469 | - */ |
|
| 470 | - public function using_wp_filesystem(): bool { |
|
| 471 | - return $this->use_filesystem; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * Attempts to use the correct path for the FS method being used. |
|
| 476 | - * |
|
| 477 | - * @param string $abs_path Absolute path. |
|
| 478 | - * |
|
| 479 | - * @return string |
|
| 480 | - */ |
|
| 481 | - public function get_sanitized_path( string $abs_path ): string { |
|
| 482 | - if ( $this->using_wp_filesystem() ) { |
|
| 483 | - return str_replace( ABSPATH, $this->wp_filesystem->abspath(), $abs_path ); |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - return $abs_path; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * Create file if not exists then set mtime and atime on file |
|
| 491 | - * |
|
| 492 | - * @param string $abs_path Absolute path. |
|
| 493 | - * @param int $time Time. |
|
| 494 | - * @param int $atime Altered time. |
|
| 495 | - * |
|
| 496 | - * @return bool |
|
| 497 | - */ |
|
| 498 | - public function touch( string $abs_path, int $time = 0, int $atime = 0 ): bool { |
|
| 499 | - if ( 0 === $time ) { |
|
| 500 | - $time = time(); |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - if ( 0 === $atime ) { |
|
| 504 | - $atime = time(); |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP |
|
| 508 | - $return = @touch( $abs_path, $time, $atime ); |
|
| 509 | - |
|
| 510 | - if ( ! $return && $this->use_filesystem ) { |
|
| 511 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 512 | - $return = $this->wp_filesystem->touch( $abs_path, $time, $atime ); |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - return $return; |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * Calls file_put_contents with chmod. |
|
| 520 | - * |
|
| 521 | - * @param string $abs_path Absolute path. |
|
| 522 | - * @param string $contents Content to write to the file. |
|
| 523 | - * @param string|null $perms Default permissions value. |
|
| 524 | - * |
|
| 525 | - * @return bool |
|
| 526 | - */ |
|
| 527 | - public function put_contents( string $abs_path, string $contents, ?string $perms = null ): bool { |
|
| 528 | - $return = false; |
|
| 529 | - |
|
| 530 | - if ( ! $this->is_dir( dirname( $abs_path ) ) ) { |
|
| 531 | - $this->mkdir( dirname( $abs_path ) ); |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - if ( $this->is_writable( dirname( $abs_path ) ) ) { |
|
| 535 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents, WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 536 | - $return = @file_put_contents( $abs_path, $contents ); |
|
| 537 | - $this->chmod( $abs_path ); |
|
| 538 | - |
|
| 539 | - if ( null === $perms ) { |
|
| 540 | - $perms = $this->chmod_file; |
|
| 541 | - } |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - if ( ! $return && $this->use_filesystem ) { |
|
| 545 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 546 | - // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_is_writable, WordPress.WP.AlternativeFunctions.file_system_operations_is_writable |
|
| 547 | - if ( $this->is_writable( dirname( $abs_path ) ) ) { |
|
| 548 | - $return = $this->wp_filesystem->put_contents( $abs_path, $contents, $perms ); |
|
| 549 | - } |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - return (bool) $return; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - /** |
|
| 556 | - * Does the specified file or dir exist? |
|
| 557 | - * |
|
| 558 | - * @param string $abs_path Absolute path. |
|
| 559 | - * @return bool |
|
| 560 | - */ |
|
| 561 | - public function file_exists( string $abs_path ): bool { |
|
| 562 | - $return = file_exists( $abs_path ); |
|
| 563 | - |
|
| 564 | - if ( ! $return && $this->use_filesystem ) { |
|
| 565 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 566 | - $return = $this->wp_filesystem->exists( $abs_path ); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - return $return; |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - /** |
|
| 573 | - * Get a file's size. |
|
| 574 | - * |
|
| 575 | - * @param string $abs_path Absolute path. |
|
| 576 | - * |
|
| 577 | - * @return int |
|
| 578 | - */ |
|
| 579 | - public function filesize( string $abs_path ): int { |
|
| 580 | - $return = filesize( $abs_path ); |
|
| 581 | - |
|
| 582 | - if ( ! $return && $this->use_filesystem ) { |
|
| 583 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 584 | - $return = $this->wp_filesystem->size( $abs_path ); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - return $return; |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - /** |
|
| 591 | - * Get the contents of a file as a string. |
|
| 592 | - * |
|
| 593 | - * @param string $abs_path Absolute path. |
|
| 594 | - * |
|
| 595 | - * @return string |
|
| 596 | - */ |
|
| 597 | - public function get_local_file_contents( string $abs_path ): string { |
|
| 598 | - |
|
| 599 | - try { |
|
| 600 | - $contents = ''; |
|
| 601 | - |
|
| 602 | - if ( $this->file_exists( $abs_path ) && is_file( $abs_path ) ) { |
|
| 603 | - if ( $this->is_writable( $abs_path ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions |
|
| 604 | - ob_start(); |
|
| 605 | - |
|
| 606 | - include_once $abs_path; |
|
| 607 | - |
|
| 608 | - $contents = ob_get_clean(); |
|
| 609 | - } |
|
| 610 | - } |
|
| 611 | - } catch ( Exception $e ) { |
|
| 612 | - // This means that ob_start has been disabled on the system. Lets fallback to good old file_get_contents. |
|
| 613 | - $contents = file_get_contents( $abs_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - return $contents; |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - /** |
|
| 620 | - * Get the contents of a file as a string. |
|
| 621 | - * |
|
| 622 | - * @param string $abs_path Absolute path. |
|
| 623 | - * |
|
| 624 | - * @return string |
|
| 625 | - */ |
|
| 626 | - public function get_contents( string $abs_path ): string { |
|
| 627 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 628 | - $return = ''; |
|
| 629 | - if ( $this->use_filesystem ) { |
|
| 630 | - $return = $this->wp_filesystem->get_contents( $abs_path ); |
|
| 631 | - } |
|
| 632 | - if ( empty( $return ) ) { |
|
| 633 | - $return = $this->get_local_file_contents( $abs_path ); |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - return $return; |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Delete a file. |
|
| 641 | - * |
|
| 642 | - * @param string $abs_path Absolute path. |
|
| 643 | - * |
|
| 644 | - * @return bool |
|
| 645 | - */ |
|
| 646 | - public function unlink( string $abs_path ): bool { |
|
| 647 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions |
|
| 648 | - $return = @unlink( $abs_path ); |
|
| 649 | - |
|
| 650 | - if ( ! $return && $this->use_filesystem ) { |
|
| 651 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 652 | - $return = $this->wp_filesystem->delete( $abs_path ); |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - return $return; |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * Chmod a file. |
|
| 660 | - * |
|
| 661 | - * @param string $abs_path Absolute path. |
|
| 662 | - * @param int|null $perms Permission value, if not provided, defaults to WP standards. |
|
| 663 | - * |
|
| 664 | - * @return bool |
|
| 665 | - */ |
|
| 666 | - public function chmod( string $abs_path, ?int $perms = null ): bool { |
|
| 667 | - if ( ! $this->file_exists( $abs_path ) ) { |
|
| 668 | - return false; |
|
| 669 | - } |
|
| 670 | - if ( is_null( $perms ) ) { |
|
| 671 | - $perms = $this->is_file( $abs_path ) ? $this->chmod_file : $this->chmod_dir; |
|
| 672 | - } |
|
| 673 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions |
|
| 674 | - $return = @chmod( $abs_path, $perms ); |
|
| 675 | - |
|
| 676 | - if ( ! $return && $this->use_filesystem ) { |
|
| 677 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 678 | - $return = $this->wp_filesystem->chmod( $abs_path, $perms ); |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - return $return; |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - /** |
|
| 685 | - * Check if this path is a directory. |
|
| 686 | - * |
|
| 687 | - * @param string $abs_path Absolute path. |
|
| 688 | - * |
|
| 689 | - * @return bool |
|
| 690 | - */ |
|
| 691 | - public function is_dir( string $abs_path ): bool { |
|
| 692 | - $return = is_dir( $abs_path ); |
|
| 693 | - |
|
| 694 | - if ( ! $return && $this->use_filesystem ) { |
|
| 695 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 696 | - $return = $this->wp_filesystem->is_dir( $abs_path ); |
|
| 697 | - } |
|
| 698 | - |
|
| 699 | - return $return; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - /** |
|
| 703 | - * Check if the specified path is a file. |
|
| 704 | - * |
|
| 705 | - * @param string $abs_path Absolute path. |
|
| 706 | - * |
|
| 707 | - * @return bool |
|
| 708 | - */ |
|
| 709 | - public function is_file( string $abs_path ): bool { |
|
| 710 | - $return = is_file( $abs_path ); |
|
| 711 | - |
|
| 712 | - if ( ! $return && $this->use_filesystem ) { |
|
| 713 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 714 | - $return = $this->wp_filesystem->is_file( $abs_path ); |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - return $return; |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - /** |
|
| 721 | - * Is the specified path readable? |
|
| 722 | - * |
|
| 723 | - * @param string $abs_path Absolute path. |
|
| 724 | - * |
|
| 725 | - * @return bool |
|
| 726 | - */ |
|
| 727 | - public function is_readable( string $abs_path ): bool { |
|
| 728 | - $return = is_readable( $abs_path ); |
|
| 729 | - |
|
| 730 | - if ( ! $return && $this->use_filesystem ) { |
|
| 731 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 732 | - $return = $this->wp_filesystem->is_readable( $abs_path ); |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - return $return; |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - /** |
|
| 739 | - * Is the specified path writable? |
|
| 740 | - * |
|
| 741 | - * @param string $abs_path Absolute path. |
|
| 742 | - * |
|
| 743 | - * @return bool |
|
| 744 | - */ |
|
| 745 | - public function is_writable( string $abs_path ): bool { |
|
| 746 | - $return = is_writable( $abs_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions |
|
| 747 | - |
|
| 748 | - if ( ! $return && $this->use_filesystem ) { |
|
| 749 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 750 | - $return = $this->wp_filesystem->is_writable( $abs_path ); |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - return $return; |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - /** |
|
| 757 | - * Create an index file at the given path. |
|
| 758 | - * |
|
| 759 | - * @param string $path Directory to add the index to. |
|
| 760 | - */ |
|
| 761 | - private function create_index( string $path ) { |
|
| 762 | - $index_path = trailingslashit( $path ) . 'index.php'; |
|
| 763 | - if ( ! $this->file_exists( $index_path ) ) { |
|
| 764 | - $this->put_contents( $index_path, "<?php\n//Silence is golden" ); |
|
| 765 | - } |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - /** |
|
| 769 | - * Recursive mkdir. |
|
| 770 | - * |
|
| 771 | - * @param string $abs_path Absolute path. |
|
| 772 | - * @param int|null $perms Permissions, if default not required. |
|
| 773 | - * |
|
| 774 | - * @return bool |
|
| 775 | - */ |
|
| 776 | - public function mkdir( string $abs_path, ?int $perms = null ): bool { |
|
| 777 | - if ( is_null( $perms ) ) { |
|
| 778 | - $perms = $this->chmod_dir; |
|
| 779 | - } |
|
| 780 | - |
|
| 781 | - if ( $this->is_dir( $abs_path ) ) { |
|
| 782 | - $this->chmod( $abs_path, $perms ); |
|
| 783 | - $this->create_index( $abs_path ); |
|
| 784 | - |
|
| 785 | - return true; |
|
| 786 | - } |
|
| 787 | - |
|
| 788 | - try { |
|
| 789 | - $mkdirp = wp_mkdir_p( $abs_path ); |
|
| 790 | - } catch ( Exception $e ) { |
|
| 791 | - $mkdirp = false; |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - if ( $mkdirp ) { |
|
| 795 | - $this->chmod( $abs_path, $perms ); |
|
| 796 | - $this->create_index( $abs_path ); |
|
| 797 | - |
|
| 798 | - return true; |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - $return = false; |
|
| 802 | - |
|
| 803 | - if ( $this->is_writable( dirname( $abs_path ) ) ) { |
|
| 804 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions, WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_is_writable |
|
| 805 | - $return = @mkdir( $abs_path, $perms, true ); |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - if ( ! $return && $this->use_filesystem ) { |
|
| 809 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 810 | - |
|
| 811 | - if ( $this->is_dir( $abs_path ) ) { |
|
| 812 | - $this->create_index( $abs_path ); |
|
| 813 | - |
|
| 814 | - return true; |
|
| 815 | - } |
|
| 816 | - |
|
| 817 | - // WP_Filesystem doesn't offer a recursive mkdir(). |
|
| 818 | - $abs_path = str_replace( '//', '/', $abs_path ); |
|
| 819 | - $abs_path = rtrim( $abs_path, '/' ); |
|
| 820 | - if ( empty( $abs_path ) ) { |
|
| 821 | - $abs_path = '/'; |
|
| 822 | - } |
|
| 823 | - |
|
| 824 | - $dirs = explode( '/', ltrim( $abs_path, '/' ) ); |
|
| 825 | - $current_dir = ''; |
|
| 826 | - |
|
| 827 | - foreach ( $dirs as $dir ) { |
|
| 828 | - $current_dir .= '/' . $dir; |
|
| 829 | - |
|
| 830 | - // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_is_writable, WordPress.WP.AlternativeFunctions.file_system_operations_is_writable |
|
| 831 | - if ( ! $this->is_dir( $current_dir ) && $this->is_writable( dirname( $current_dir ) ) ) { |
|
| 832 | - $this->wp_filesystem->mkdir( $current_dir, $perms ); |
|
| 833 | - } |
|
| 834 | - } |
|
| 835 | - |
|
| 836 | - $return = $this->is_dir( $abs_path ); |
|
| 837 | - } |
|
| 838 | - |
|
| 839 | - return $return; |
|
| 840 | - } |
|
| 841 | - |
|
| 842 | - /** |
|
| 843 | - * Delete a directory. |
|
| 844 | - * |
|
| 845 | - * @param string $abs_path Absolute path. |
|
| 846 | - * @param bool $recursive Set to recursively create. |
|
| 847 | - * |
|
| 848 | - * @return bool |
|
| 849 | - */ |
|
| 850 | - public function rmdir( string $abs_path, bool $recursive = false ): bool { |
|
| 851 | - if ( ! $this->is_dir( $abs_path ) ) { |
|
| 852 | - return false; |
|
| 853 | - } |
|
| 854 | - |
|
| 855 | - // Taken from WP_Filesystem_Direct. |
|
| 856 | - if ( ! $recursive ) { |
|
| 857 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions |
|
| 858 | - $return = @rmdir( $abs_path ); |
|
| 859 | - } else { |
|
| 860 | - |
|
| 861 | - // At this point, it's a folder, and we're in recursive mode. |
|
| 862 | - $abs_path = trailingslashit( $abs_path ); |
|
| 863 | - $filelist = $this->scandir( $abs_path ); |
|
| 864 | - |
|
| 865 | - $return = true; |
|
| 866 | - if ( is_array( $filelist ) ) { |
|
| 867 | - foreach ( $filelist as $filename => $fileinfo ) { |
|
| 868 | - |
|
| 869 | - if ( 'd' === $fileinfo['type'] ) { |
|
| 870 | - $return = $this->rmdir( $abs_path . $filename, $recursive ); |
|
| 871 | - } else { |
|
| 872 | - $return = $this->unlink( $abs_path . $filename ); |
|
| 873 | - } |
|
| 874 | - } |
|
| 875 | - } |
|
| 876 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions |
|
| 877 | - if ( file_exists( $abs_path ) && ! @rmdir( $abs_path ) ) { |
|
| 878 | - $return = false; |
|
| 879 | - } |
|
| 880 | - } |
|
| 881 | - |
|
| 882 | - if ( ! $return && $this->use_filesystem ) { |
|
| 883 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 884 | - |
|
| 885 | - return $this->wp_filesystem->rmdir( $abs_path, $recursive ); |
|
| 886 | - } |
|
| 887 | - |
|
| 888 | - return $return; |
|
| 889 | - } |
|
| 890 | - |
|
| 891 | - /** |
|
| 892 | - * Get a list of files/folders under the specified directory. |
|
| 893 | - * |
|
| 894 | - * @param string $abs_path Absolute path. |
|
| 895 | - * @param bool $include_hidden Include hidden files, defaults to true. |
|
| 896 | - * @param bool $recursive Recursive search, defaults to false. |
|
| 897 | - * |
|
| 898 | - * @return array|bool |
|
| 899 | - */ |
|
| 900 | - public function scandir( string $abs_path, bool $include_hidden = true, bool $recursive = false ) { |
|
| 901 | - if ( $this->is_file( $abs_path ) ) { |
|
| 902 | - $limit_file = basename( $abs_path ); |
|
| 903 | - $abs_path = dirname( $abs_path ); |
|
| 904 | - } else { |
|
| 905 | - $limit_file = false; |
|
| 906 | - } |
|
| 907 | - |
|
| 908 | - if ( ! $this->is_dir( $abs_path ) || ! $this->is_readable( $abs_path ) ) { |
|
| 909 | - return false; |
|
| 910 | - } |
|
| 911 | - |
|
| 912 | - $dir = dir( $abs_path ); |
|
| 913 | - |
|
| 914 | - if ( false === $dir ) { |
|
| 915 | - if ( $this->use_filesystem ) { |
|
| 916 | - $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 917 | - |
|
| 918 | - return $this->wp_filesystem->dirlist( $abs_path, $include_hidden, $recursive ); |
|
| 919 | - } |
|
| 920 | - |
|
| 921 | - return false; |
|
| 922 | - } |
|
| 923 | - |
|
| 924 | - $ret = array(); |
|
| 925 | - |
|
| 926 | - while ( false !== ( $entry = $dir->read() ) ) { |
|
| 927 | - $struc = array(); |
|
| 928 | - $struc['name'] = $entry; |
|
| 929 | - |
|
| 930 | - if ( '.' === $struc['name'] || '..' === $struc['name'] ) { |
|
| 931 | - continue; |
|
| 932 | - } |
|
| 933 | - |
|
| 934 | - if ( ! $include_hidden && '.' === $struc['name'][0] ) { |
|
| 935 | - continue; |
|
| 936 | - } |
|
| 937 | - |
|
| 938 | - if ( $limit_file && $struc['name'] !== $limit_file ) { |
|
| 939 | - continue; |
|
| 940 | - } |
|
| 941 | - |
|
| 942 | - $struc['type'] = $this->is_dir( $abs_path . '/' . $entry ) ? 'd' : 'f'; |
|
| 943 | - |
|
| 944 | - if ( 'd' === $struc['type'] ) { |
|
| 945 | - if ( $recursive ) { |
|
| 946 | - $struc['files'] = $this->scandir( $abs_path . '/' . $struc['name'], $include_hidden, $recursive ); |
|
| 947 | - } else { |
|
| 948 | - $struc['files'] = array(); |
|
| 949 | - } |
|
| 950 | - } |
|
| 951 | - |
|
| 952 | - $ret[ $struc['name'] ] = $struc; |
|
| 953 | - } |
|
| 954 | - |
|
| 955 | - $dir->close(); |
|
| 956 | - |
|
| 957 | - unset( $dir ); |
|
| 958 | - |
|
| 959 | - return $ret; |
|
| 960 | - } |
|
| 961 | - |
|
| 962 | - /** |
|
| 963 | - * Light wrapper for move_uploaded_file with chmod. |
|
| 964 | - * |
|
| 965 | - * @param string $file Source file. |
|
| 966 | - * @param string $destination File destination. |
|
| 967 | - * @param int|null $perms Permission value. |
|
| 968 | - * |
|
| 969 | - * @return bool |
|
| 970 | - */ |
|
| 971 | - public function move_uploaded_file( string $file, string $destination, ?int $perms = null ): bool { |
|
| 972 | - // TODO: look into replicating more functionality from wp_handle_upload(). |
|
| 973 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors |
|
| 974 | - $return = @move_uploaded_file( $file, $destination ); |
|
| 975 | - |
|
| 976 | - if ( $return ) { |
|
| 977 | - $this->chmod( $destination, $perms ); |
|
| 978 | - } |
|
| 979 | - |
|
| 980 | - return $return; |
|
| 981 | - } |
|
| 982 | - |
|
| 983 | - /** |
|
| 984 | - * Copy a file. |
|
| 985 | - * |
|
| 986 | - * @param string $source_abs_path Source path. |
|
| 987 | - * @param string $destination_abs_path Destination path. |
|
| 988 | - * @param bool $overwrite Overwrite file. |
|
| 989 | - * @param mixed $perms Permission value. |
|
| 990 | - * @return bool |
|
| 991 | - * Taken from WP_Filesystem_Direct |
|
| 992 | - */ |
|
| 993 | - public function copy( string $source_abs_path, string $destination_abs_path, bool $overwrite = true, $perms = false ): bool { |
|
| 994 | - |
|
| 995 | - // Error if source file doesn't exist. |
|
| 996 | - if ( ! $this->file_exists( $source_abs_path ) ) { |
|
| 997 | - return false; |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - if ( ! $overwrite && $this->file_exists( $destination_abs_path ) ) { |
|
| 1001 | - return false; |
|
| 1002 | - } |
|
| 1003 | - if ( ! $this->is_dir( dirname( $destination_abs_path ) ) ) { |
|
| 1004 | - $this->mkdir( dirname( $destination_abs_path ) ); |
|
| 1005 | - } |
|
| 1006 | - |
|
| 1007 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors |
|
| 1008 | - $return = @copy( $source_abs_path, $destination_abs_path ); |
|
| 1009 | - if ( $perms && $return ) { |
|
| 1010 | - $this->chmod( $destination_abs_path, $perms ); |
|
| 1011 | - } |
|
| 1012 | - |
|
| 1013 | - if ( ! $return && $this->use_filesystem ) { |
|
| 1014 | - $source_abs_path = $this->get_sanitized_path( $source_abs_path ); |
|
| 1015 | - $destination_abs_path = $this->get_sanitized_path( $destination_abs_path ); |
|
| 1016 | - $return = $this->wp_filesystem->copy( |
|
| 1017 | - $source_abs_path, |
|
| 1018 | - $destination_abs_path, |
|
| 1019 | - $overwrite, |
|
| 1020 | - $perms |
|
| 1021 | - ); |
|
| 1022 | - } |
|
| 1023 | - |
|
| 1024 | - return $return; |
|
| 1025 | - } |
|
| 1026 | - |
|
| 1027 | - /** |
|
| 1028 | - * Move a file. |
|
| 1029 | - * |
|
| 1030 | - * @param string $source_abs_path Source absolute path. |
|
| 1031 | - * @param string $destination_abs_path Destination absolute path. |
|
| 1032 | - * @param bool $overwrite Overwrite if file exists. |
|
| 1033 | - * @return bool |
|
| 1034 | - */ |
|
| 1035 | - public function move( string $source_abs_path, string $destination_abs_path, bool $overwrite = true ): bool { |
|
| 1036 | - |
|
| 1037 | - // Error if source file doesn't exist. |
|
| 1038 | - if ( ! $this->file_exists( $source_abs_path ) ) { |
|
| 1039 | - return false; |
|
| 1040 | - } |
|
| 1041 | - |
|
| 1042 | - // Try using rename first. |
|
| 1043 | - // If that fails (for example, the source is read only) try copy. |
|
| 1044 | - // Taken in part from WP_Filesystem_Direct. |
|
| 1045 | - if ( ! $overwrite && $this->file_exists( $destination_abs_path ) ) { |
|
| 1046 | - return false; |
|
| 1047 | - } elseif ( @rename( $source_abs_path, $destination_abs_path ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions |
|
| 1048 | - return true; |
|
| 1049 | - } elseif ( $this->copy( $source_abs_path, $destination_abs_path, $overwrite ) && $this->file_exists( |
|
| 1050 | - $destination_abs_path |
|
| 1051 | - ) ) { |
|
| 1052 | - |
|
| 1053 | - $this->unlink( $source_abs_path ); |
|
| 1054 | - |
|
| 1055 | - return true; |
|
| 1056 | - } else { |
|
| 1057 | - $return = false; |
|
| 1058 | - } |
|
| 1059 | - |
|
| 1060 | - if ( $this->use_filesystem ) { |
|
| 1061 | - $source_abs_path = $this->get_sanitized_path( $source_abs_path ); |
|
| 1062 | - $destination_abs_path = $this->get_sanitized_path( $destination_abs_path ); |
|
| 1063 | - |
|
| 1064 | - $return = $this->wp_filesystem->move( $source_abs_path, $destination_abs_path, $overwrite ); |
|
| 1065 | - } |
|
| 1066 | - |
|
| 1067 | - return $return; |
|
| 1068 | - } |
|
| 1069 | - |
|
| 1070 | - /** |
|
| 1071 | - * Shim: get_template. |
|
| 1072 | - * |
|
| 1073 | - * @param string $file Template name. |
|
| 1074 | - * |
|
| 1075 | - * @return void Path to template file. |
|
| 1076 | - */ |
|
| 1077 | - public function get_template( string $file ) { |
|
| 1078 | - $panel = new Redux_Panel( $this ); |
|
| 1079 | - $panel->get_template( $file ); |
|
| 1080 | - } |
|
| 1081 | - } |
|
| 1082 | - |
|
| 1083 | - Redux_Filesystem::get_instance(); |
|
| 16 | + /** |
|
| 17 | + * Class Redux_Filesystem |
|
| 18 | + */ |
|
| 19 | + class Redux_Filesystem { |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Instance of this class. |
|
| 23 | + * |
|
| 24 | + * @since 1.0.0 |
|
| 25 | + * @var null|Redux_Filesystem |
|
| 26 | + */ |
|
| 27 | + protected static ?Redux_Filesystem $instance = null; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * WP Filesystem object. |
|
| 31 | + * |
|
| 32 | + * @var null|WP_Filesystem_Direct |
|
| 33 | + */ |
|
| 34 | + protected static ?WP_Filesystem_Direct $direct = null; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * File system credentials. |
|
| 38 | + * |
|
| 39 | + * @var array|bool|null |
|
| 40 | + */ |
|
| 41 | + private $creds; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * ReduxFramework object pointer. |
|
| 45 | + * |
|
| 46 | + * @var null|ReduxFramework |
|
| 47 | + */ |
|
| 48 | + public ?ReduxFramework $parent = null; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Instance of WP_Filesystem |
|
| 52 | + * |
|
| 53 | + * @var WP_Filesystem_Base|null |
|
| 54 | + */ |
|
| 55 | + private ?WP_Filesystem_Base $wp_filesystem = null; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * If DBI_Filesystem should attempt to use the WP_Filesystem class. |
|
| 59 | + * |
|
| 60 | + * @var bool |
|
| 61 | + */ |
|
| 62 | + private bool $use_filesystem = false; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Default chmod octal value for directories. |
|
| 66 | + * |
|
| 67 | + * @var int |
|
| 68 | + */ |
|
| 69 | + private int $chmod_dir; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Default chmod octal value for files. |
|
| 73 | + * |
|
| 74 | + * @var int |
|
| 75 | + */ |
|
| 76 | + private int $chmod_file; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Default cache folder. |
|
| 80 | + * |
|
| 81 | + * @var string |
|
| 82 | + */ |
|
| 83 | + public string $cache_folder; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Kill switch. |
|
| 87 | + * |
|
| 88 | + * @var bool |
|
| 89 | + */ |
|
| 90 | + public bool $killswitch = false; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * FTP Form HTML. |
|
| 94 | + * |
|
| 95 | + * @var string |
|
| 96 | + */ |
|
| 97 | + public string $ftp_form; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Pass `true` when instantiating to skip using WP_Filesystem. |
|
| 101 | + * |
|
| 102 | + * @param bool $force_no_fs Force no use of the filesystem. |
|
| 103 | + */ |
|
| 104 | + public function __construct( bool $force_no_fs = false ) { |
|
| 105 | + |
|
| 106 | + // This little number fixes some issues with certain filesystem setups. |
|
| 107 | + |
|
| 108 | + if ( ! function_exists( 'request_filesystem_credentials' ) ) { |
|
| 109 | + require_once ABSPATH . '/wp-admin/includes/template.php'; |
|
| 110 | + require_once ABSPATH . '/wp-includes/pluggable.php'; |
|
| 111 | + require_once ABSPATH . '/wp-admin/includes/file.php'; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + // Set default permissions. |
|
| 115 | + if ( defined( 'FS_CHMOD_DIR' ) ) { |
|
| 116 | + $this->chmod_dir = FS_CHMOD_DIR; |
|
| 117 | + } else { |
|
| 118 | + $this->chmod_dir = ( fileperms( ABSPATH ) & 0777 | 0755 ); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + if ( defined( 'FS_CHMOD_FILE' ) ) { |
|
| 122 | + $this->chmod_file = FS_CHMOD_FILE; |
|
| 123 | + } else { |
|
| 124 | + $this->chmod_file = ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if ( ! $force_no_fs && function_exists( 'request_filesystem_credentials' ) ) { |
|
| 128 | + if ( ( defined( 'WPMDB_WP_FILESYSTEM' ) && WPMDB_WP_FILESYSTEM ) || ! defined( 'WPMDB_WP_FILESYSTEM' ) ) { |
|
| 129 | + $this->maybe_init_wp_filesystem(); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $uploads_dir = wp_upload_dir(); |
|
| 134 | + $this->cache_folder = trailingslashit( $uploads_dir['basedir'] ) . 'redux/'; |
|
| 135 | + if ( ! $this->file_exists( $this->cache_folder ) ) { |
|
| 136 | + $this->mkdir( $this->cache_folder ); |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Return an instance of this class. |
|
| 142 | + * |
|
| 143 | + * @param ReduxFramework|null $me ReduxFramework pointer. |
|
| 144 | + * |
|
| 145 | + * @return object A single instance of this class. |
|
| 146 | + * @since 1.0.0 |
|
| 147 | + */ |
|
| 148 | + public static function get_instance( ?ReduxFramework $me = null ): ?object { |
|
| 149 | + |
|
| 150 | + // If the single instance hasn't been set, set it now. |
|
| 151 | + if ( null === self::$instance ) { |
|
| 152 | + self::$instance = new self(); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + if ( null !== $me ) { |
|
| 156 | + self::$instance->parent = $me; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return self::$instance; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Build an FTP form. |
|
| 164 | + */ |
|
| 165 | + public function ftp_form() { |
|
| 166 | + if ( isset( $this->ftp_form ) && ! empty( $this->ftp_form ) ) { |
|
| 167 | + echo '<div class="wrap">'; |
|
| 168 | + echo '<div class="error">'; |
|
| 169 | + echo '<p>'; |
|
| 170 | + // translators: %1$s: Upload URL. %2$s: Codex URL. |
|
| 171 | + echo '<strong>' . esc_html__( 'File Permission Issues', 'redux-framework' ) . '</strong><br/>' . sprintf( esc_html__( 'We were unable to modify required files. Please ensure that %1$s has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as %2$s.', 'redux-framework' ), '<code>' . esc_url( Redux_Functions_Ex::wp_normalize_path( trailingslashit( WP_CONTENT_DIR ) ) . '/uploads/' ) . '</code>', ' <a href="https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants" target="_blank">' . esc_html__( 'outlined here', 'redux-framework' ) . '</a>' ); |
|
| 172 | + echo '</p>'; |
|
| 173 | + echo '</div>'; |
|
| 174 | + echo '<h2></h2>'; |
|
| 175 | + echo '</div>'; |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Attempt to initiate WP_Filesystem |
|
| 181 | + * If this fails, $use_filesystem is set to false and all methods in this class should use native php fallbacks |
|
| 182 | + * Thwarts `request_filesystem_credentials()` attempt to display a form for obtaining creds from users |
|
| 183 | + * TODO: provide notice and input in wp-admin for users when this fails |
|
| 184 | + */ |
|
| 185 | + public function maybe_init_wp_filesystem() { |
|
| 186 | + // Set up the filesystem with creds. |
|
| 187 | + require_once ABSPATH . '/wp-admin/includes/template.php'; |
|
| 188 | + require_once ABSPATH . '/wp-includes/pluggable.php'; |
|
| 189 | + require_once ABSPATH . '/wp-admin/includes/file.php'; |
|
| 190 | + |
|
| 191 | + ob_start(); |
|
| 192 | + $credentials = request_filesystem_credentials( '', '', false, false ); |
|
| 193 | + $ob_contents = ob_get_contents(); |
|
| 194 | + ob_end_clean(); |
|
| 195 | + |
|
| 196 | + if ( @wp_filesystem( $credentials ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors |
|
| 197 | + global $wp_filesystem; |
|
| 198 | + $this->wp_filesystem = $wp_filesystem; |
|
| 199 | + $this->use_filesystem = true; |
|
| 200 | + $this->generate_default_files(); |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Init WO Filesystem. |
|
| 206 | + * |
|
| 207 | + * @param string $form_url Form URL. |
|
| 208 | + * @param string $method Connect method. |
|
| 209 | + * @param bool $context Context. |
|
| 210 | + * |
|
| 211 | + * @return bool |
|
| 212 | + */ |
|
| 213 | + public function advanced_filesystem_init( string $form_url, string $method = '', bool $context = false ): bool { |
|
| 214 | + if ( ! empty( $this->wp_filesystem ) && $this->use_filesystem ) { |
|
| 215 | + return true; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + if ( ! empty( $this->creds ) ) { |
|
| 219 | + return true; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + ob_start(); |
|
| 223 | + |
|
| 224 | + $this->creds = request_filesystem_credentials( $form_url, $method, false, $context ); |
|
| 225 | + |
|
| 226 | + /* first attempt to get credentials */ |
|
| 227 | + if ( false === $this->creds ) { |
|
| 228 | + $this->creds = array(); |
|
| 229 | + $this->ftp_form = ob_get_contents(); |
|
| 230 | + ob_end_clean(); |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * If we come here, we don't have credentials |
|
| 234 | + * so the request for them is displaying |
|
| 235 | + * no need for further processing |
|
| 236 | + * */ |
|
| 237 | + return false; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /* now we got some credentials - try to use them */ |
|
| 241 | + if ( ! @wp_filesystem( $this->creds ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors |
|
| 242 | + $this->creds = array(); |
|
| 243 | + /* incorrect connection data - ask for credentials again, now with an error message */ |
|
| 244 | + request_filesystem_credentials( $form_url, '', true, $context ); |
|
| 245 | + $this->ftp_form = ob_get_contents(); |
|
| 246 | + ob_end_clean(); |
|
| 247 | + |
|
| 248 | + return false; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + global $wp_filesystem; |
|
| 252 | + $this->wp_filesystem = $wp_filesystem; |
|
| 253 | + $this->use_filesystem = true; |
|
| 254 | + $this->generate_default_files(); |
|
| 255 | + |
|
| 256 | + return true; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Load WP filesystem directly. |
|
| 261 | + */ |
|
| 262 | + public static function load_direct() { |
|
| 263 | + if ( null === self::$direct ) { |
|
| 264 | + require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php'; |
|
| 265 | + require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php'; |
|
| 266 | + |
|
| 267 | + self::$direct = new WP_Filesystem_Direct( array() ); |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Execute filesystem request. |
|
| 273 | + * |
|
| 274 | + * @param string $action Action to perform. |
|
| 275 | + * @param string $file File to perform upon. |
|
| 276 | + * @param array $params Argument for action. |
|
| 277 | + * |
|
| 278 | + * @return bool|void |
|
| 279 | + */ |
|
| 280 | + public function execute( string $action, string $file = '', array $params = array() ) { |
|
| 281 | + if ( empty( $this->parent->args ) ) { |
|
| 282 | + return; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + if ( ! empty( $params ) ) { |
|
| 286 | + // phpcs:ignore WordPress.PHP.DontExtract |
|
| 287 | + extract( $params ); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + if ( empty( $this->wp_filesystem ) ) { |
|
| 291 | + if ( 'submenu' === $this->parent->args['menu_type'] ) { |
|
| 292 | + $page_parent = $this->parent->args['page_parent']; |
|
| 293 | + $base = $page_parent . '?page=' . $this->parent->args['page_slug']; |
|
| 294 | + } else { |
|
| 295 | + $base = 'admin.php?page=' . $this->parent->args['page_slug']; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + $url = wp_nonce_url( $base, 'redux-options' ); |
|
| 299 | + $this->advanced_filesystem_init( $url, 'direct', dirname( $file ) ); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + return $this->do_action( $action, $file, $params ); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Generates the default Redux cache folder. |
|
| 308 | + * |
|
| 309 | + * @return void |
|
| 310 | + */ |
|
| 311 | + private function generate_default_files() { |
|
| 312 | + if ( ! $this->is_dir( Redux_Core::$upload_dir ) ) { |
|
| 313 | + $this->mkdir( Redux_Core::$upload_dir ); |
|
| 314 | + } |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Do request filesystem action. |
|
| 319 | + * |
|
| 320 | + * @param string $action Requested action. |
|
| 321 | + * @param string $file File to perform action upon. |
|
| 322 | + * @param array $params Action arguments. |
|
| 323 | + * |
|
| 324 | + * @return bool|void |
|
| 325 | + */ |
|
| 326 | + public function do_action( string $action, string $file = '', array $params = array() ) { |
|
| 327 | + $destination = ''; |
|
| 328 | + $overwrite = ''; |
|
| 329 | + $content = ''; |
|
| 330 | + |
|
| 331 | + if ( ! empty( $params ) ) { |
|
| 332 | + |
|
| 333 | + // phpcs:ignore WordPress.PHP.DontExtract |
|
| 334 | + extract( $params ); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + global $wp_filesystem; |
|
| 338 | + |
|
| 339 | + if ( defined( 'FS_CHMOD_FILE' ) ) { |
|
| 340 | + $chmod = FS_CHMOD_FILE; |
|
| 341 | + } else { |
|
| 342 | + $chmod = 0644; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + if ( isset( $params['chmod'] ) && ! empty( $params['chmod'] ) ) { |
|
| 346 | + $chmod = $params['chmod']; |
|
| 347 | + } |
|
| 348 | + $res = false; |
|
| 349 | + if ( ! isset( $recursive ) ) { |
|
| 350 | + $recursive = false; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + // Do unique stuff. |
|
| 354 | + if ( 'mkdir' === $action ) { |
|
| 355 | + $chmod = null; |
|
| 356 | + if ( isset( $params['chmod'] ) && ! empty( $params['chmod'] ) ) { |
|
| 357 | + $chmod = $params['chmod']; |
|
| 358 | + } |
|
| 359 | + $res = $this->mkdir( $file, $chmod ); |
|
| 360 | + } elseif ( 'rmdir' === $action ) { |
|
| 361 | + $res = $this->rmdir( $file, $recursive ); |
|
| 362 | + } elseif ( 'copy' === $action && false === $this->killswitch ) { |
|
| 363 | + $res = $this->copy( $file, $destination, $overwrite, $chmod ); |
|
| 364 | + } elseif ( 'move' === $action && false === $this->killswitch ) { |
|
| 365 | + $res = $this->move( $file, $destination, $overwrite ); |
|
| 366 | + } elseif ( 'delete' === $action ) { |
|
| 367 | + if ( $this->is_dir( $file ) ) { |
|
| 368 | + $res = $this->rmdir( $file, $recursive ); |
|
| 369 | + } else { |
|
| 370 | + $res = $this->unlink( $file ); |
|
| 371 | + } |
|
| 372 | + } elseif ( 'dirlist' === $action ) { |
|
| 373 | + if ( ! isset( $include_hidden ) ) { |
|
| 374 | + $include_hidden = true; |
|
| 375 | + } |
|
| 376 | + $res = $this->scandir( $file, $include_hidden, $recursive ); |
|
| 377 | + } elseif ( 'put_contents' === $action && false === $this->killswitch ) { |
|
| 378 | + // Write a string to a file. |
|
| 379 | + if ( isset( $this->ftp_form ) && ! empty( $this->ftp_form ) ) { |
|
| 380 | + self::load_direct(); |
|
| 381 | + $res = self::$direct->put_contents( $file, $content, $chmod ); |
|
| 382 | + } else { |
|
| 383 | + $res = $this->put_contents( $file, $content, $chmod ); |
|
| 384 | + } |
|
| 385 | + } elseif ( 'chown' === $action ) { |
|
| 386 | + // Changes the file owner. |
|
| 387 | + if ( isset( $owner ) && ! empty( $owner ) ) { |
|
| 388 | + $res = $wp_filesystem->chmod( $file, $chmod, $recursive ); |
|
| 389 | + } |
|
| 390 | + } elseif ( 'owner' === $action ) { |
|
| 391 | + // Gets the file owner. |
|
| 392 | + $res = $this->wp_filesystem->owner( $file ); |
|
| 393 | + } elseif ( 'chmod' === $action ) { |
|
| 394 | + if ( ! isset( $params['chmod'] ) || ( empty( $params['chmod'] ) ) ) { |
|
| 395 | + $chmod = false; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + $res = $this->chmod( $file, $chmod ); |
|
| 399 | + } elseif ( 'get_contents' === $action ) { |
|
| 400 | + // Reads entire file into a string. |
|
| 401 | + if ( isset( $this->ftp_form ) && ! empty( $this->ftp_form ) ) { |
|
| 402 | + self::load_direct(); |
|
| 403 | + $res = self::$direct->get_contents( $file ); |
|
| 404 | + } else { |
|
| 405 | + $res = $this->get_contents( $file ); |
|
| 406 | + } |
|
| 407 | + } elseif ( 'get_contents_array' === $action ) { |
|
| 408 | + // Reads entire file into an array. |
|
| 409 | + $res = $this->wp_filesystem->get_contents_array( $file ); |
|
| 410 | + } elseif ( 'object' === $action ) { |
|
| 411 | + $res = $this->wp_filesystem; |
|
| 412 | + } elseif ( 'unzip' === $action ) { |
|
| 413 | + $unzipfile = unzip_file( $file, $destination ); |
|
| 414 | + if ( $unzipfile ) { |
|
| 415 | + $res = true; |
|
| 416 | + } |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + if ( ! $res ) { |
|
| 420 | + if ( 'dirlist' === $action ) { |
|
| 421 | + if ( empty( $res ) ) { |
|
| 422 | + return; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + if ( ! is_array( $res ) ) { |
|
| 426 | + if ( count( glob( "$file*" ) ) === 0 ) { |
|
| 427 | + return; |
|
| 428 | + } |
|
| 429 | + } |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + $this->killswitch = true; |
|
| 433 | + |
|
| 434 | + // translators: %1$s: Upload URL. %2$s: Codex URL. |
|
| 435 | + $msg = '<strong>' . esc_html__( 'File Permission Issues', 'redux-framework' ) . '</strong><br/>' . sprintf( esc_html__( 'We were unable to modify required files. Please ensure that %1$s has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as %2$s.', 'redux-framework' ), '<code>' . esc_url( Redux_Functions_Ex::wp_normalize_path( trailingslashit( WP_CONTENT_DIR ) ) ) . '/uploads/</code>', '<a href="https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants" target="_blank">' . esc_html__( 'outlined here', 'redux-framework' ) . '</a>' ); |
|
| 436 | + |
|
| 437 | + $data = array( |
|
| 438 | + 'parent' => self::$instance->parent, |
|
| 439 | + 'type' => 'error', |
|
| 440 | + 'msg' => $msg, |
|
| 441 | + 'id' => 'redux-wp-login', |
|
| 442 | + 'dismiss' => false, |
|
| 443 | + ); |
|
| 444 | + |
|
| 445 | + Redux_Admin_Notices::set_notice( $data ); |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + return $res; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Getter for the instantiated WP_Filesystem. This should be used carefully since $wp_filesystem won't always have a value. |
|
| 454 | + * |
|
| 455 | + * @return WP_Filesystem_Base|false |
|
| 456 | + */ |
|
| 457 | + public function get_wp_filesystem() { |
|
| 458 | + if ( $this->use_filesystem ) { |
|
| 459 | + return $this->wp_filesystem; |
|
| 460 | + } else { |
|
| 461 | + return false; |
|
| 462 | + } |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Check if WP_Filesystem being used. |
|
| 467 | + * |
|
| 468 | + * @return bool |
|
| 469 | + */ |
|
| 470 | + public function using_wp_filesystem(): bool { |
|
| 471 | + return $this->use_filesystem; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * Attempts to use the correct path for the FS method being used. |
|
| 476 | + * |
|
| 477 | + * @param string $abs_path Absolute path. |
|
| 478 | + * |
|
| 479 | + * @return string |
|
| 480 | + */ |
|
| 481 | + public function get_sanitized_path( string $abs_path ): string { |
|
| 482 | + if ( $this->using_wp_filesystem() ) { |
|
| 483 | + return str_replace( ABSPATH, $this->wp_filesystem->abspath(), $abs_path ); |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + return $abs_path; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * Create file if not exists then set mtime and atime on file |
|
| 491 | + * |
|
| 492 | + * @param string $abs_path Absolute path. |
|
| 493 | + * @param int $time Time. |
|
| 494 | + * @param int $atime Altered time. |
|
| 495 | + * |
|
| 496 | + * @return bool |
|
| 497 | + */ |
|
| 498 | + public function touch( string $abs_path, int $time = 0, int $atime = 0 ): bool { |
|
| 499 | + if ( 0 === $time ) { |
|
| 500 | + $time = time(); |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + if ( 0 === $atime ) { |
|
| 504 | + $atime = time(); |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP |
|
| 508 | + $return = @touch( $abs_path, $time, $atime ); |
|
| 509 | + |
|
| 510 | + if ( ! $return && $this->use_filesystem ) { |
|
| 511 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 512 | + $return = $this->wp_filesystem->touch( $abs_path, $time, $atime ); |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + return $return; |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * Calls file_put_contents with chmod. |
|
| 520 | + * |
|
| 521 | + * @param string $abs_path Absolute path. |
|
| 522 | + * @param string $contents Content to write to the file. |
|
| 523 | + * @param string|null $perms Default permissions value. |
|
| 524 | + * |
|
| 525 | + * @return bool |
|
| 526 | + */ |
|
| 527 | + public function put_contents( string $abs_path, string $contents, ?string $perms = null ): bool { |
|
| 528 | + $return = false; |
|
| 529 | + |
|
| 530 | + if ( ! $this->is_dir( dirname( $abs_path ) ) ) { |
|
| 531 | + $this->mkdir( dirname( $abs_path ) ); |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + if ( $this->is_writable( dirname( $abs_path ) ) ) { |
|
| 535 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents, WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 536 | + $return = @file_put_contents( $abs_path, $contents ); |
|
| 537 | + $this->chmod( $abs_path ); |
|
| 538 | + |
|
| 539 | + if ( null === $perms ) { |
|
| 540 | + $perms = $this->chmod_file; |
|
| 541 | + } |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + if ( ! $return && $this->use_filesystem ) { |
|
| 545 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 546 | + // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_is_writable, WordPress.WP.AlternativeFunctions.file_system_operations_is_writable |
|
| 547 | + if ( $this->is_writable( dirname( $abs_path ) ) ) { |
|
| 548 | + $return = $this->wp_filesystem->put_contents( $abs_path, $contents, $perms ); |
|
| 549 | + } |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + return (bool) $return; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + /** |
|
| 556 | + * Does the specified file or dir exist? |
|
| 557 | + * |
|
| 558 | + * @param string $abs_path Absolute path. |
|
| 559 | + * @return bool |
|
| 560 | + */ |
|
| 561 | + public function file_exists( string $abs_path ): bool { |
|
| 562 | + $return = file_exists( $abs_path ); |
|
| 563 | + |
|
| 564 | + if ( ! $return && $this->use_filesystem ) { |
|
| 565 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 566 | + $return = $this->wp_filesystem->exists( $abs_path ); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + return $return; |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + /** |
|
| 573 | + * Get a file's size. |
|
| 574 | + * |
|
| 575 | + * @param string $abs_path Absolute path. |
|
| 576 | + * |
|
| 577 | + * @return int |
|
| 578 | + */ |
|
| 579 | + public function filesize( string $abs_path ): int { |
|
| 580 | + $return = filesize( $abs_path ); |
|
| 581 | + |
|
| 582 | + if ( ! $return && $this->use_filesystem ) { |
|
| 583 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 584 | + $return = $this->wp_filesystem->size( $abs_path ); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + return $return; |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * Get the contents of a file as a string. |
|
| 592 | + * |
|
| 593 | + * @param string $abs_path Absolute path. |
|
| 594 | + * |
|
| 595 | + * @return string |
|
| 596 | + */ |
|
| 597 | + public function get_local_file_contents( string $abs_path ): string { |
|
| 598 | + |
|
| 599 | + try { |
|
| 600 | + $contents = ''; |
|
| 601 | + |
|
| 602 | + if ( $this->file_exists( $abs_path ) && is_file( $abs_path ) ) { |
|
| 603 | + if ( $this->is_writable( $abs_path ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions |
|
| 604 | + ob_start(); |
|
| 605 | + |
|
| 606 | + include_once $abs_path; |
|
| 607 | + |
|
| 608 | + $contents = ob_get_clean(); |
|
| 609 | + } |
|
| 610 | + } |
|
| 611 | + } catch ( Exception $e ) { |
|
| 612 | + // This means that ob_start has been disabled on the system. Lets fallback to good old file_get_contents. |
|
| 613 | + $contents = file_get_contents( $abs_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + return $contents; |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + /** |
|
| 620 | + * Get the contents of a file as a string. |
|
| 621 | + * |
|
| 622 | + * @param string $abs_path Absolute path. |
|
| 623 | + * |
|
| 624 | + * @return string |
|
| 625 | + */ |
|
| 626 | + public function get_contents( string $abs_path ): string { |
|
| 627 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 628 | + $return = ''; |
|
| 629 | + if ( $this->use_filesystem ) { |
|
| 630 | + $return = $this->wp_filesystem->get_contents( $abs_path ); |
|
| 631 | + } |
|
| 632 | + if ( empty( $return ) ) { |
|
| 633 | + $return = $this->get_local_file_contents( $abs_path ); |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + return $return; |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Delete a file. |
|
| 641 | + * |
|
| 642 | + * @param string $abs_path Absolute path. |
|
| 643 | + * |
|
| 644 | + * @return bool |
|
| 645 | + */ |
|
| 646 | + public function unlink( string $abs_path ): bool { |
|
| 647 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions |
|
| 648 | + $return = @unlink( $abs_path ); |
|
| 649 | + |
|
| 650 | + if ( ! $return && $this->use_filesystem ) { |
|
| 651 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 652 | + $return = $this->wp_filesystem->delete( $abs_path ); |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + return $return; |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * Chmod a file. |
|
| 660 | + * |
|
| 661 | + * @param string $abs_path Absolute path. |
|
| 662 | + * @param int|null $perms Permission value, if not provided, defaults to WP standards. |
|
| 663 | + * |
|
| 664 | + * @return bool |
|
| 665 | + */ |
|
| 666 | + public function chmod( string $abs_path, ?int $perms = null ): bool { |
|
| 667 | + if ( ! $this->file_exists( $abs_path ) ) { |
|
| 668 | + return false; |
|
| 669 | + } |
|
| 670 | + if ( is_null( $perms ) ) { |
|
| 671 | + $perms = $this->is_file( $abs_path ) ? $this->chmod_file : $this->chmod_dir; |
|
| 672 | + } |
|
| 673 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions |
|
| 674 | + $return = @chmod( $abs_path, $perms ); |
|
| 675 | + |
|
| 676 | + if ( ! $return && $this->use_filesystem ) { |
|
| 677 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 678 | + $return = $this->wp_filesystem->chmod( $abs_path, $perms ); |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + return $return; |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + /** |
|
| 685 | + * Check if this path is a directory. |
|
| 686 | + * |
|
| 687 | + * @param string $abs_path Absolute path. |
|
| 688 | + * |
|
| 689 | + * @return bool |
|
| 690 | + */ |
|
| 691 | + public function is_dir( string $abs_path ): bool { |
|
| 692 | + $return = is_dir( $abs_path ); |
|
| 693 | + |
|
| 694 | + if ( ! $return && $this->use_filesystem ) { |
|
| 695 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 696 | + $return = $this->wp_filesystem->is_dir( $abs_path ); |
|
| 697 | + } |
|
| 698 | + |
|
| 699 | + return $return; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + /** |
|
| 703 | + * Check if the specified path is a file. |
|
| 704 | + * |
|
| 705 | + * @param string $abs_path Absolute path. |
|
| 706 | + * |
|
| 707 | + * @return bool |
|
| 708 | + */ |
|
| 709 | + public function is_file( string $abs_path ): bool { |
|
| 710 | + $return = is_file( $abs_path ); |
|
| 711 | + |
|
| 712 | + if ( ! $return && $this->use_filesystem ) { |
|
| 713 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 714 | + $return = $this->wp_filesystem->is_file( $abs_path ); |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + return $return; |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + /** |
|
| 721 | + * Is the specified path readable? |
|
| 722 | + * |
|
| 723 | + * @param string $abs_path Absolute path. |
|
| 724 | + * |
|
| 725 | + * @return bool |
|
| 726 | + */ |
|
| 727 | + public function is_readable( string $abs_path ): bool { |
|
| 728 | + $return = is_readable( $abs_path ); |
|
| 729 | + |
|
| 730 | + if ( ! $return && $this->use_filesystem ) { |
|
| 731 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 732 | + $return = $this->wp_filesystem->is_readable( $abs_path ); |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + return $return; |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + /** |
|
| 739 | + * Is the specified path writable? |
|
| 740 | + * |
|
| 741 | + * @param string $abs_path Absolute path. |
|
| 742 | + * |
|
| 743 | + * @return bool |
|
| 744 | + */ |
|
| 745 | + public function is_writable( string $abs_path ): bool { |
|
| 746 | + $return = is_writable( $abs_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions |
|
| 747 | + |
|
| 748 | + if ( ! $return && $this->use_filesystem ) { |
|
| 749 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 750 | + $return = $this->wp_filesystem->is_writable( $abs_path ); |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + return $return; |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + /** |
|
| 757 | + * Create an index file at the given path. |
|
| 758 | + * |
|
| 759 | + * @param string $path Directory to add the index to. |
|
| 760 | + */ |
|
| 761 | + private function create_index( string $path ) { |
|
| 762 | + $index_path = trailingslashit( $path ) . 'index.php'; |
|
| 763 | + if ( ! $this->file_exists( $index_path ) ) { |
|
| 764 | + $this->put_contents( $index_path, "<?php\n//Silence is golden" ); |
|
| 765 | + } |
|
| 766 | + } |
|
| 767 | + |
|
| 768 | + /** |
|
| 769 | + * Recursive mkdir. |
|
| 770 | + * |
|
| 771 | + * @param string $abs_path Absolute path. |
|
| 772 | + * @param int|null $perms Permissions, if default not required. |
|
| 773 | + * |
|
| 774 | + * @return bool |
|
| 775 | + */ |
|
| 776 | + public function mkdir( string $abs_path, ?int $perms = null ): bool { |
|
| 777 | + if ( is_null( $perms ) ) { |
|
| 778 | + $perms = $this->chmod_dir; |
|
| 779 | + } |
|
| 780 | + |
|
| 781 | + if ( $this->is_dir( $abs_path ) ) { |
|
| 782 | + $this->chmod( $abs_path, $perms ); |
|
| 783 | + $this->create_index( $abs_path ); |
|
| 784 | + |
|
| 785 | + return true; |
|
| 786 | + } |
|
| 787 | + |
|
| 788 | + try { |
|
| 789 | + $mkdirp = wp_mkdir_p( $abs_path ); |
|
| 790 | + } catch ( Exception $e ) { |
|
| 791 | + $mkdirp = false; |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + if ( $mkdirp ) { |
|
| 795 | + $this->chmod( $abs_path, $perms ); |
|
| 796 | + $this->create_index( $abs_path ); |
|
| 797 | + |
|
| 798 | + return true; |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + $return = false; |
|
| 802 | + |
|
| 803 | + if ( $this->is_writable( dirname( $abs_path ) ) ) { |
|
| 804 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions, WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_is_writable |
|
| 805 | + $return = @mkdir( $abs_path, $perms, true ); |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + if ( ! $return && $this->use_filesystem ) { |
|
| 809 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 810 | + |
|
| 811 | + if ( $this->is_dir( $abs_path ) ) { |
|
| 812 | + $this->create_index( $abs_path ); |
|
| 813 | + |
|
| 814 | + return true; |
|
| 815 | + } |
|
| 816 | + |
|
| 817 | + // WP_Filesystem doesn't offer a recursive mkdir(). |
|
| 818 | + $abs_path = str_replace( '//', '/', $abs_path ); |
|
| 819 | + $abs_path = rtrim( $abs_path, '/' ); |
|
| 820 | + if ( empty( $abs_path ) ) { |
|
| 821 | + $abs_path = '/'; |
|
| 822 | + } |
|
| 823 | + |
|
| 824 | + $dirs = explode( '/', ltrim( $abs_path, '/' ) ); |
|
| 825 | + $current_dir = ''; |
|
| 826 | + |
|
| 827 | + foreach ( $dirs as $dir ) { |
|
| 828 | + $current_dir .= '/' . $dir; |
|
| 829 | + |
|
| 830 | + // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_is_writable, WordPress.WP.AlternativeFunctions.file_system_operations_is_writable |
|
| 831 | + if ( ! $this->is_dir( $current_dir ) && $this->is_writable( dirname( $current_dir ) ) ) { |
|
| 832 | + $this->wp_filesystem->mkdir( $current_dir, $perms ); |
|
| 833 | + } |
|
| 834 | + } |
|
| 835 | + |
|
| 836 | + $return = $this->is_dir( $abs_path ); |
|
| 837 | + } |
|
| 838 | + |
|
| 839 | + return $return; |
|
| 840 | + } |
|
| 841 | + |
|
| 842 | + /** |
|
| 843 | + * Delete a directory. |
|
| 844 | + * |
|
| 845 | + * @param string $abs_path Absolute path. |
|
| 846 | + * @param bool $recursive Set to recursively create. |
|
| 847 | + * |
|
| 848 | + * @return bool |
|
| 849 | + */ |
|
| 850 | + public function rmdir( string $abs_path, bool $recursive = false ): bool { |
|
| 851 | + if ( ! $this->is_dir( $abs_path ) ) { |
|
| 852 | + return false; |
|
| 853 | + } |
|
| 854 | + |
|
| 855 | + // Taken from WP_Filesystem_Direct. |
|
| 856 | + if ( ! $recursive ) { |
|
| 857 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions |
|
| 858 | + $return = @rmdir( $abs_path ); |
|
| 859 | + } else { |
|
| 860 | + |
|
| 861 | + // At this point, it's a folder, and we're in recursive mode. |
|
| 862 | + $abs_path = trailingslashit( $abs_path ); |
|
| 863 | + $filelist = $this->scandir( $abs_path ); |
|
| 864 | + |
|
| 865 | + $return = true; |
|
| 866 | + if ( is_array( $filelist ) ) { |
|
| 867 | + foreach ( $filelist as $filename => $fileinfo ) { |
|
| 868 | + |
|
| 869 | + if ( 'd' === $fileinfo['type'] ) { |
|
| 870 | + $return = $this->rmdir( $abs_path . $filename, $recursive ); |
|
| 871 | + } else { |
|
| 872 | + $return = $this->unlink( $abs_path . $filename ); |
|
| 873 | + } |
|
| 874 | + } |
|
| 875 | + } |
|
| 876 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions |
|
| 877 | + if ( file_exists( $abs_path ) && ! @rmdir( $abs_path ) ) { |
|
| 878 | + $return = false; |
|
| 879 | + } |
|
| 880 | + } |
|
| 881 | + |
|
| 882 | + if ( ! $return && $this->use_filesystem ) { |
|
| 883 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 884 | + |
|
| 885 | + return $this->wp_filesystem->rmdir( $abs_path, $recursive ); |
|
| 886 | + } |
|
| 887 | + |
|
| 888 | + return $return; |
|
| 889 | + } |
|
| 890 | + |
|
| 891 | + /** |
|
| 892 | + * Get a list of files/folders under the specified directory. |
|
| 893 | + * |
|
| 894 | + * @param string $abs_path Absolute path. |
|
| 895 | + * @param bool $include_hidden Include hidden files, defaults to true. |
|
| 896 | + * @param bool $recursive Recursive search, defaults to false. |
|
| 897 | + * |
|
| 898 | + * @return array|bool |
|
| 899 | + */ |
|
| 900 | + public function scandir( string $abs_path, bool $include_hidden = true, bool $recursive = false ) { |
|
| 901 | + if ( $this->is_file( $abs_path ) ) { |
|
| 902 | + $limit_file = basename( $abs_path ); |
|
| 903 | + $abs_path = dirname( $abs_path ); |
|
| 904 | + } else { |
|
| 905 | + $limit_file = false; |
|
| 906 | + } |
|
| 907 | + |
|
| 908 | + if ( ! $this->is_dir( $abs_path ) || ! $this->is_readable( $abs_path ) ) { |
|
| 909 | + return false; |
|
| 910 | + } |
|
| 911 | + |
|
| 912 | + $dir = dir( $abs_path ); |
|
| 913 | + |
|
| 914 | + if ( false === $dir ) { |
|
| 915 | + if ( $this->use_filesystem ) { |
|
| 916 | + $abs_path = $this->get_sanitized_path( $abs_path ); |
|
| 917 | + |
|
| 918 | + return $this->wp_filesystem->dirlist( $abs_path, $include_hidden, $recursive ); |
|
| 919 | + } |
|
| 920 | + |
|
| 921 | + return false; |
|
| 922 | + } |
|
| 923 | + |
|
| 924 | + $ret = array(); |
|
| 925 | + |
|
| 926 | + while ( false !== ( $entry = $dir->read() ) ) { |
|
| 927 | + $struc = array(); |
|
| 928 | + $struc['name'] = $entry; |
|
| 929 | + |
|
| 930 | + if ( '.' === $struc['name'] || '..' === $struc['name'] ) { |
|
| 931 | + continue; |
|
| 932 | + } |
|
| 933 | + |
|
| 934 | + if ( ! $include_hidden && '.' === $struc['name'][0] ) { |
|
| 935 | + continue; |
|
| 936 | + } |
|
| 937 | + |
|
| 938 | + if ( $limit_file && $struc['name'] !== $limit_file ) { |
|
| 939 | + continue; |
|
| 940 | + } |
|
| 941 | + |
|
| 942 | + $struc['type'] = $this->is_dir( $abs_path . '/' . $entry ) ? 'd' : 'f'; |
|
| 943 | + |
|
| 944 | + if ( 'd' === $struc['type'] ) { |
|
| 945 | + if ( $recursive ) { |
|
| 946 | + $struc['files'] = $this->scandir( $abs_path . '/' . $struc['name'], $include_hidden, $recursive ); |
|
| 947 | + } else { |
|
| 948 | + $struc['files'] = array(); |
|
| 949 | + } |
|
| 950 | + } |
|
| 951 | + |
|
| 952 | + $ret[ $struc['name'] ] = $struc; |
|
| 953 | + } |
|
| 954 | + |
|
| 955 | + $dir->close(); |
|
| 956 | + |
|
| 957 | + unset( $dir ); |
|
| 958 | + |
|
| 959 | + return $ret; |
|
| 960 | + } |
|
| 961 | + |
|
| 962 | + /** |
|
| 963 | + * Light wrapper for move_uploaded_file with chmod. |
|
| 964 | + * |
|
| 965 | + * @param string $file Source file. |
|
| 966 | + * @param string $destination File destination. |
|
| 967 | + * @param int|null $perms Permission value. |
|
| 968 | + * |
|
| 969 | + * @return bool |
|
| 970 | + */ |
|
| 971 | + public function move_uploaded_file( string $file, string $destination, ?int $perms = null ): bool { |
|
| 972 | + // TODO: look into replicating more functionality from wp_handle_upload(). |
|
| 973 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors |
|
| 974 | + $return = @move_uploaded_file( $file, $destination ); |
|
| 975 | + |
|
| 976 | + if ( $return ) { |
|
| 977 | + $this->chmod( $destination, $perms ); |
|
| 978 | + } |
|
| 979 | + |
|
| 980 | + return $return; |
|
| 981 | + } |
|
| 982 | + |
|
| 983 | + /** |
|
| 984 | + * Copy a file. |
|
| 985 | + * |
|
| 986 | + * @param string $source_abs_path Source path. |
|
| 987 | + * @param string $destination_abs_path Destination path. |
|
| 988 | + * @param bool $overwrite Overwrite file. |
|
| 989 | + * @param mixed $perms Permission value. |
|
| 990 | + * @return bool |
|
| 991 | + * Taken from WP_Filesystem_Direct |
|
| 992 | + */ |
|
| 993 | + public function copy( string $source_abs_path, string $destination_abs_path, bool $overwrite = true, $perms = false ): bool { |
|
| 994 | + |
|
| 995 | + // Error if source file doesn't exist. |
|
| 996 | + if ( ! $this->file_exists( $source_abs_path ) ) { |
|
| 997 | + return false; |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + if ( ! $overwrite && $this->file_exists( $destination_abs_path ) ) { |
|
| 1001 | + return false; |
|
| 1002 | + } |
|
| 1003 | + if ( ! $this->is_dir( dirname( $destination_abs_path ) ) ) { |
|
| 1004 | + $this->mkdir( dirname( $destination_abs_path ) ); |
|
| 1005 | + } |
|
| 1006 | + |
|
| 1007 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors |
|
| 1008 | + $return = @copy( $source_abs_path, $destination_abs_path ); |
|
| 1009 | + if ( $perms && $return ) { |
|
| 1010 | + $this->chmod( $destination_abs_path, $perms ); |
|
| 1011 | + } |
|
| 1012 | + |
|
| 1013 | + if ( ! $return && $this->use_filesystem ) { |
|
| 1014 | + $source_abs_path = $this->get_sanitized_path( $source_abs_path ); |
|
| 1015 | + $destination_abs_path = $this->get_sanitized_path( $destination_abs_path ); |
|
| 1016 | + $return = $this->wp_filesystem->copy( |
|
| 1017 | + $source_abs_path, |
|
| 1018 | + $destination_abs_path, |
|
| 1019 | + $overwrite, |
|
| 1020 | + $perms |
|
| 1021 | + ); |
|
| 1022 | + } |
|
| 1023 | + |
|
| 1024 | + return $return; |
|
| 1025 | + } |
|
| 1026 | + |
|
| 1027 | + /** |
|
| 1028 | + * Move a file. |
|
| 1029 | + * |
|
| 1030 | + * @param string $source_abs_path Source absolute path. |
|
| 1031 | + * @param string $destination_abs_path Destination absolute path. |
|
| 1032 | + * @param bool $overwrite Overwrite if file exists. |
|
| 1033 | + * @return bool |
|
| 1034 | + */ |
|
| 1035 | + public function move( string $source_abs_path, string $destination_abs_path, bool $overwrite = true ): bool { |
|
| 1036 | + |
|
| 1037 | + // Error if source file doesn't exist. |
|
| 1038 | + if ( ! $this->file_exists( $source_abs_path ) ) { |
|
| 1039 | + return false; |
|
| 1040 | + } |
|
| 1041 | + |
|
| 1042 | + // Try using rename first. |
|
| 1043 | + // If that fails (for example, the source is read only) try copy. |
|
| 1044 | + // Taken in part from WP_Filesystem_Direct. |
|
| 1045 | + if ( ! $overwrite && $this->file_exists( $destination_abs_path ) ) { |
|
| 1046 | + return false; |
|
| 1047 | + } elseif ( @rename( $source_abs_path, $destination_abs_path ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions |
|
| 1048 | + return true; |
|
| 1049 | + } elseif ( $this->copy( $source_abs_path, $destination_abs_path, $overwrite ) && $this->file_exists( |
|
| 1050 | + $destination_abs_path |
|
| 1051 | + ) ) { |
|
| 1052 | + |
|
| 1053 | + $this->unlink( $source_abs_path ); |
|
| 1054 | + |
|
| 1055 | + return true; |
|
| 1056 | + } else { |
|
| 1057 | + $return = false; |
|
| 1058 | + } |
|
| 1059 | + |
|
| 1060 | + if ( $this->use_filesystem ) { |
|
| 1061 | + $source_abs_path = $this->get_sanitized_path( $source_abs_path ); |
|
| 1062 | + $destination_abs_path = $this->get_sanitized_path( $destination_abs_path ); |
|
| 1063 | + |
|
| 1064 | + $return = $this->wp_filesystem->move( $source_abs_path, $destination_abs_path, $overwrite ); |
|
| 1065 | + } |
|
| 1066 | + |
|
| 1067 | + return $return; |
|
| 1068 | + } |
|
| 1069 | + |
|
| 1070 | + /** |
|
| 1071 | + * Shim: get_template. |
|
| 1072 | + * |
|
| 1073 | + * @param string $file Template name. |
|
| 1074 | + * |
|
| 1075 | + * @return void Path to template file. |
|
| 1076 | + */ |
|
| 1077 | + public function get_template( string $file ) { |
|
| 1078 | + $panel = new Redux_Panel( $this ); |
|
| 1079 | + $panel->get_template( $file ); |
|
| 1080 | + } |
|
| 1081 | + } |
|
| 1082 | + |
|
| 1083 | + Redux_Filesystem::get_instance(); |
|
| 1084 | 1084 | } |
@@ -11,210 +11,210 @@ |
||
| 11 | 11 | |
| 12 | 12 | if ( ! class_exists( 'Redux_Tabbed', false ) ) { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Class Redux_Tabbed |
|
| 16 | - */ |
|
| 17 | - class Redux_Tabbed extends Redux_Field { |
|
| 14 | + /** |
|
| 15 | + * Class Redux_Tabbed |
|
| 16 | + */ |
|
| 17 | + class Redux_Tabbed extends Redux_Field { |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Set field defaults. |
|
| 21 | - */ |
|
| 22 | - public function set_defaults() { |
|
| 23 | - $defaults = array( |
|
| 24 | - 'icon' => '', |
|
| 25 | - ); |
|
| 19 | + /** |
|
| 20 | + * Set field defaults. |
|
| 21 | + */ |
|
| 22 | + public function set_defaults() { |
|
| 23 | + $defaults = array( |
|
| 24 | + 'icon' => '', |
|
| 25 | + ); |
|
| 26 | 26 | |
| 27 | - $this->field = wp_parse_args( $this->field, $defaults ); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Field Render Function. |
|
| 32 | - * Takes the vars and outputs the HTML for the field in the settings |
|
| 33 | - * |
|
| 34 | - * @since ReduxFramework 0.0.4 |
|
| 35 | - */ |
|
| 36 | - public function render() { |
|
| 37 | - $unallowed = array( 'tabbed', 'social_profiles', 'color_schemes', 'repeater' ); |
|
| 27 | + $this->field = wp_parse_args( $this->field, $defaults ); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Field Render Function. |
|
| 32 | + * Takes the vars and outputs the HTML for the field in the settings |
|
| 33 | + * |
|
| 34 | + * @since ReduxFramework 0.0.4 |
|
| 35 | + */ |
|
| 36 | + public function render() { |
|
| 37 | + $unallowed = array( 'tabbed', 'social_profiles', 'color_schemes', 'repeater' ); |
|
| 38 | 38 | |
| 39 | - echo '<div id="' . esc_attr( $this->field['id'] ) . '-tabbed" class="redux-tabbed" rel="' . esc_attr( $this->field['id'] ) . '">'; |
|
| 40 | - echo '<div class="redux-tabbed-nav" data-id="' . esc_attr( $this->field['id'] ) . '">'; |
|
| 41 | - foreach ( $this->field['tabs'] as $key => $tab ) { |
|
| 42 | - |
|
| 43 | - $tabbed_icon = ( ! empty( $tab['icon'] ) ) ? '<i class="redux-tab-icon ' . esc_attr( $tab['icon'] ) . '"></i>' : ''; |
|
| 44 | - $tabbed_active = ( empty( $key ) ) ? 'redux-tabbed-active' : ''; |
|
| 45 | - |
|
| 46 | - // Output HTML escaped above. |
|
| 47 | - // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 48 | - echo '<a href="#" class="' . esc_attr( $tabbed_active ) . '"">' . $tabbed_icon . esc_attr( $tab['title'] ) . '</a>'; |
|
| 49 | - |
|
| 50 | - } |
|
| 51 | - echo '</div>'; |
|
| 52 | - |
|
| 53 | - echo '<div class="redux-tabbed-contents">'; |
|
| 54 | - foreach ( $this->field['tabs'] as $key => $tab ) { |
|
| 55 | - |
|
| 56 | - $tabbed_hidden = ( ! empty( $key ) ) ? ' hidden' : ''; |
|
| 57 | - |
|
| 58 | - echo '<div class="redux-tabbed-content' . esc_attr( $tabbed_hidden ) . '">'; |
|
| 59 | - |
|
| 60 | - foreach ( $tab['fields'] as $field ) { |
|
| 61 | - |
|
| 62 | - if ( in_array( $field['type'], $unallowed, true ) ) { |
|
| 63 | - echo esc_html__( 'The', 'redux-framework' ) . ' <code>' . esc_html( $field['type'] ) . '</code> ' . esc_html__( 'field is not supported within the Tabbed field.', 'redux-framework' ); |
|
| 64 | - } else { |
|
| 65 | - $this->output_field( $field ); |
|
| 66 | - } |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - echo '</div>'; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - echo '</div>'; |
|
| 73 | - echo '</div>'; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Output field. |
|
| 78 | - * |
|
| 79 | - * @param array $field Field array. |
|
| 80 | - */ |
|
| 81 | - public function output_field( array $field ) { |
|
| 82 | - $this->enqueue_dependencies( $field ); |
|
| 83 | - |
|
| 84 | - if ( ! isset( $field['class'] ) ) { |
|
| 85 | - $field['class'] = ''; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - $field['class'] .= ' tabbed'; |
|
| 89 | - |
|
| 90 | - echo '<div class="redux-tab-field">'; |
|
| 91 | - |
|
| 92 | - if ( ! empty( $field['title'] ) ) { |
|
| 93 | - echo '<div class="redux-field-title">'; |
|
| 94 | - echo '<h4>' . wp_kses_post( $field['title'] ) . '</h4>'; |
|
| 95 | - |
|
| 96 | - if ( ! empty( $field['subtitle'] ) ) { |
|
| 97 | - echo '<div class="redux-field-subtitle">' . wp_kses_post( $field['subtitle'] ) . '</div>'; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - echo '</div>'; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $orig_field_id = $field['id']; |
|
| 104 | - |
|
| 105 | - $field['name'] = $this->parent->args['opt_name'] . '[' . $orig_field_id . ']'; |
|
| 106 | - $field['class'] .= ' in-tabbed'; |
|
| 107 | - |
|
| 108 | - if ( isset( $field['options'] ) ) { |
|
| 109 | - |
|
| 110 | - // Sorter data filter. |
|
| 111 | - if ( 'sorter' === $field['type'] && ! empty( $field['data'] ) && is_array( $field['data'] ) ) { |
|
| 112 | - if ( ! isset( $field['args'] ) ) { |
|
| 113 | - $field['args'] = array(); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - foreach ( $field['data'] as $key => $data ) { |
|
| 117 | - if ( ! isset( $field['args'][ $key ] ) ) { |
|
| 118 | - $field['args'][ $key ] = array(); |
|
| 119 | - } |
|
| 120 | - $field['options'][ $key ] = $this->parent->get_wordpress_data( $data, $field['args'][ $key ] ); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $default = $field['default'] ?? null; |
|
| 126 | - |
|
| 127 | - // phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
|
| 128 | - // $value = isset( $this->parent->options[ $orig_field_id ] ) && 0 !== (int) $this->parent->options[ $orig_field_id ] ? $this->parent->options[ $orig_field_id ] : $default; |
|
| 129 | - $value = $this->parent->options[ $orig_field_id ] ?? $default; |
|
| 130 | - |
|
| 131 | - $this->parent->render_class->field_input( $field, $value ); |
|
| 132 | - |
|
| 133 | - echo '<div class="clear"></div>'; |
|
| 134 | - echo '</div>'; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Localize. |
|
| 139 | - * |
|
| 140 | - * @param array $field Field. |
|
| 141 | - * @param string $value Value. |
|
| 142 | - * |
|
| 143 | - * @return void |
|
| 144 | - */ |
|
| 145 | - public function localize( array $field, string $value = '' ) { |
|
| 146 | - ob_start(); |
|
| 147 | - |
|
| 148 | - foreach ( $field['tabs'] as $f ) { |
|
| 149 | - foreach ( $f['fields'] as $field ) { |
|
| 150 | - $this->output_field( $field ); |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - ob_end_clean(); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Enqueue Deps. |
|
| 159 | - * |
|
| 160 | - * @param array $field Field. |
|
| 161 | - */ |
|
| 162 | - private function enqueue_dependencies( array $field ) { |
|
| 163 | - $field_type = $field['type']; |
|
| 164 | - |
|
| 165 | - $field_class = 'Redux_' . $field_type; |
|
| 166 | - |
|
| 167 | - if ( ! class_exists( $field_class ) ) { |
|
| 168 | - $field_type = str_replace( '_', '-', $field_type ); |
|
| 169 | - |
|
| 170 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 171 | - $class_file = apply_filters( 'redux-typeclass-load', ReduxFramework::$_dir . 'inc/fields/' . $field_type . '/class-redux-' . $field_type . '.php', $field_class ); |
|
| 172 | - |
|
| 173 | - if ( file_exists( $class_file ) ) { |
|
| 174 | - require_once $class_file; |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if ( class_exists( $field_class ) && method_exists( $field_class, 'enqueue' ) ) { |
|
| 179 | - $enqueue = new $field_class( '', '', $this->parent ); |
|
| 180 | - $enqueue->enqueue(); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - if ( class_exists( $field_class ) && method_exists( $field_class, 'localize' ) ) { |
|
| 184 | - |
|
| 185 | - $enqueue = new $field_class( '', '', $this->parent ); |
|
| 186 | - |
|
| 187 | - $data = $enqueue->localize( $field ); |
|
| 188 | - |
|
| 189 | - $this->parent->enqueue_class->localize_data[ $field['type'] ][ $field['id'] ] = $data; |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Enqueue Function. |
|
| 195 | - * If this field requires any scripts, or css define this function and register/enqueue the scripts/css |
|
| 196 | - * |
|
| 197 | - * @since ReduxFramework 0.0.4 |
|
| 198 | - */ |
|
| 199 | - public function enqueue() { |
|
| 200 | - wp_print_styles( 'editor-buttons' ); |
|
| 201 | - |
|
| 202 | - wp_enqueue_script( |
|
| 203 | - 'redux-field-tabbed', |
|
| 204 | - Redux_Core::$url . 'inc/extensions/tabbed/tabbed/redux-tabbed' . Redux_Functions::is_min() . '.js', |
|
| 205 | - array( 'jquery', 'redux-js' ), |
|
| 206 | - Redux_Extension_Tabbed::$version, |
|
| 207 | - true |
|
| 208 | - ); |
|
| 209 | - |
|
| 210 | - if ( $this->parent->args['dev_mode'] ) { |
|
| 211 | - wp_enqueue_style( |
|
| 212 | - 'redux-field-tabbed', |
|
| 213 | - Redux_Core::$url . 'inc/extensions/tabbed/tabbed/redux-tabbed.css', |
|
| 214 | - array(), |
|
| 215 | - Redux_Extension_Tabbed::$version |
|
| 216 | - ); |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - } |
|
| 39 | + echo '<div id="' . esc_attr( $this->field['id'] ) . '-tabbed" class="redux-tabbed" rel="' . esc_attr( $this->field['id'] ) . '">'; |
|
| 40 | + echo '<div class="redux-tabbed-nav" data-id="' . esc_attr( $this->field['id'] ) . '">'; |
|
| 41 | + foreach ( $this->field['tabs'] as $key => $tab ) { |
|
| 42 | + |
|
| 43 | + $tabbed_icon = ( ! empty( $tab['icon'] ) ) ? '<i class="redux-tab-icon ' . esc_attr( $tab['icon'] ) . '"></i>' : ''; |
|
| 44 | + $tabbed_active = ( empty( $key ) ) ? 'redux-tabbed-active' : ''; |
|
| 45 | + |
|
| 46 | + // Output HTML escaped above. |
|
| 47 | + // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 48 | + echo '<a href="#" class="' . esc_attr( $tabbed_active ) . '"">' . $tabbed_icon . esc_attr( $tab['title'] ) . '</a>'; |
|
| 49 | + |
|
| 50 | + } |
|
| 51 | + echo '</div>'; |
|
| 52 | + |
|
| 53 | + echo '<div class="redux-tabbed-contents">'; |
|
| 54 | + foreach ( $this->field['tabs'] as $key => $tab ) { |
|
| 55 | + |
|
| 56 | + $tabbed_hidden = ( ! empty( $key ) ) ? ' hidden' : ''; |
|
| 57 | + |
|
| 58 | + echo '<div class="redux-tabbed-content' . esc_attr( $tabbed_hidden ) . '">'; |
|
| 59 | + |
|
| 60 | + foreach ( $tab['fields'] as $field ) { |
|
| 61 | + |
|
| 62 | + if ( in_array( $field['type'], $unallowed, true ) ) { |
|
| 63 | + echo esc_html__( 'The', 'redux-framework' ) . ' <code>' . esc_html( $field['type'] ) . '</code> ' . esc_html__( 'field is not supported within the Tabbed field.', 'redux-framework' ); |
|
| 64 | + } else { |
|
| 65 | + $this->output_field( $field ); |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + echo '</div>'; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + echo '</div>'; |
|
| 73 | + echo '</div>'; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Output field. |
|
| 78 | + * |
|
| 79 | + * @param array $field Field array. |
|
| 80 | + */ |
|
| 81 | + public function output_field( array $field ) { |
|
| 82 | + $this->enqueue_dependencies( $field ); |
|
| 83 | + |
|
| 84 | + if ( ! isset( $field['class'] ) ) { |
|
| 85 | + $field['class'] = ''; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + $field['class'] .= ' tabbed'; |
|
| 89 | + |
|
| 90 | + echo '<div class="redux-tab-field">'; |
|
| 91 | + |
|
| 92 | + if ( ! empty( $field['title'] ) ) { |
|
| 93 | + echo '<div class="redux-field-title">'; |
|
| 94 | + echo '<h4>' . wp_kses_post( $field['title'] ) . '</h4>'; |
|
| 95 | + |
|
| 96 | + if ( ! empty( $field['subtitle'] ) ) { |
|
| 97 | + echo '<div class="redux-field-subtitle">' . wp_kses_post( $field['subtitle'] ) . '</div>'; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + echo '</div>'; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $orig_field_id = $field['id']; |
|
| 104 | + |
|
| 105 | + $field['name'] = $this->parent->args['opt_name'] . '[' . $orig_field_id . ']'; |
|
| 106 | + $field['class'] .= ' in-tabbed'; |
|
| 107 | + |
|
| 108 | + if ( isset( $field['options'] ) ) { |
|
| 109 | + |
|
| 110 | + // Sorter data filter. |
|
| 111 | + if ( 'sorter' === $field['type'] && ! empty( $field['data'] ) && is_array( $field['data'] ) ) { |
|
| 112 | + if ( ! isset( $field['args'] ) ) { |
|
| 113 | + $field['args'] = array(); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + foreach ( $field['data'] as $key => $data ) { |
|
| 117 | + if ( ! isset( $field['args'][ $key ] ) ) { |
|
| 118 | + $field['args'][ $key ] = array(); |
|
| 119 | + } |
|
| 120 | + $field['options'][ $key ] = $this->parent->get_wordpress_data( $data, $field['args'][ $key ] ); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $default = $field['default'] ?? null; |
|
| 126 | + |
|
| 127 | + // phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
|
| 128 | + // $value = isset( $this->parent->options[ $orig_field_id ] ) && 0 !== (int) $this->parent->options[ $orig_field_id ] ? $this->parent->options[ $orig_field_id ] : $default; |
|
| 129 | + $value = $this->parent->options[ $orig_field_id ] ?? $default; |
|
| 130 | + |
|
| 131 | + $this->parent->render_class->field_input( $field, $value ); |
|
| 132 | + |
|
| 133 | + echo '<div class="clear"></div>'; |
|
| 134 | + echo '</div>'; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Localize. |
|
| 139 | + * |
|
| 140 | + * @param array $field Field. |
|
| 141 | + * @param string $value Value. |
|
| 142 | + * |
|
| 143 | + * @return void |
|
| 144 | + */ |
|
| 145 | + public function localize( array $field, string $value = '' ) { |
|
| 146 | + ob_start(); |
|
| 147 | + |
|
| 148 | + foreach ( $field['tabs'] as $f ) { |
|
| 149 | + foreach ( $f['fields'] as $field ) { |
|
| 150 | + $this->output_field( $field ); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + ob_end_clean(); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Enqueue Deps. |
|
| 159 | + * |
|
| 160 | + * @param array $field Field. |
|
| 161 | + */ |
|
| 162 | + private function enqueue_dependencies( array $field ) { |
|
| 163 | + $field_type = $field['type']; |
|
| 164 | + |
|
| 165 | + $field_class = 'Redux_' . $field_type; |
|
| 166 | + |
|
| 167 | + if ( ! class_exists( $field_class ) ) { |
|
| 168 | + $field_type = str_replace( '_', '-', $field_type ); |
|
| 169 | + |
|
| 170 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 171 | + $class_file = apply_filters( 'redux-typeclass-load', ReduxFramework::$_dir . 'inc/fields/' . $field_type . '/class-redux-' . $field_type . '.php', $field_class ); |
|
| 172 | + |
|
| 173 | + if ( file_exists( $class_file ) ) { |
|
| 174 | + require_once $class_file; |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if ( class_exists( $field_class ) && method_exists( $field_class, 'enqueue' ) ) { |
|
| 179 | + $enqueue = new $field_class( '', '', $this->parent ); |
|
| 180 | + $enqueue->enqueue(); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + if ( class_exists( $field_class ) && method_exists( $field_class, 'localize' ) ) { |
|
| 184 | + |
|
| 185 | + $enqueue = new $field_class( '', '', $this->parent ); |
|
| 186 | + |
|
| 187 | + $data = $enqueue->localize( $field ); |
|
| 188 | + |
|
| 189 | + $this->parent->enqueue_class->localize_data[ $field['type'] ][ $field['id'] ] = $data; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Enqueue Function. |
|
| 195 | + * If this field requires any scripts, or css define this function and register/enqueue the scripts/css |
|
| 196 | + * |
|
| 197 | + * @since ReduxFramework 0.0.4 |
|
| 198 | + */ |
|
| 199 | + public function enqueue() { |
|
| 200 | + wp_print_styles( 'editor-buttons' ); |
|
| 201 | + |
|
| 202 | + wp_enqueue_script( |
|
| 203 | + 'redux-field-tabbed', |
|
| 204 | + Redux_Core::$url . 'inc/extensions/tabbed/tabbed/redux-tabbed' . Redux_Functions::is_min() . '.js', |
|
| 205 | + array( 'jquery', 'redux-js' ), |
|
| 206 | + Redux_Extension_Tabbed::$version, |
|
| 207 | + true |
|
| 208 | + ); |
|
| 209 | + |
|
| 210 | + if ( $this->parent->args['dev_mode'] ) { |
|
| 211 | + wp_enqueue_style( |
|
| 212 | + 'redux-field-tabbed', |
|
| 213 | + Redux_Core::$url . 'inc/extensions/tabbed/tabbed/redux-tabbed.css', |
|
| 214 | + array(), |
|
| 215 | + Redux_Extension_Tabbed::$version |
|
| 216 | + ); |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | 220 | } |
@@ -114,10 +114,10 @@ discard block |
||
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | foreach ( $field['data'] as $key => $data ) { |
| 117 | - if ( ! isset( $field['args'][ $key ] ) ) { |
|
| 118 | - $field['args'][ $key ] = array(); |
|
| 117 | + if ( ! isset( $field['args'][$key] ) ) { |
|
| 118 | + $field['args'][$key] = array(); |
|
| 119 | 119 | } |
| 120 | - $field['options'][ $key ] = $this->parent->get_wordpress_data( $data, $field['args'][ $key ] ); |
|
| 120 | + $field['options'][$key] = $this->parent->get_wordpress_data( $data, $field['args'][$key] ); |
|
| 121 | 121 | } |
| 122 | 122 | } |
| 123 | 123 | } |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | |
| 127 | 127 | // phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
| 128 | 128 | // $value = isset( $this->parent->options[ $orig_field_id ] ) && 0 !== (int) $this->parent->options[ $orig_field_id ] ? $this->parent->options[ $orig_field_id ] : $default; |
| 129 | - $value = $this->parent->options[ $orig_field_id ] ?? $default; |
|
| 129 | + $value = $this->parent->options[$orig_field_id] ?? $default; |
|
| 130 | 130 | |
| 131 | 131 | $this->parent->render_class->field_input( $field, $value ); |
| 132 | 132 | |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | |
| 187 | 187 | $data = $enqueue->localize( $field ); |
| 188 | 188 | |
| 189 | - $this->parent->enqueue_class->localize_data[ $field['type'] ][ $field['id'] ] = $data; |
|
| 189 | + $this->parent->enqueue_class->localize_data[$field['type']][$field['id']] = $data; |
|
| 190 | 190 | } |
| 191 | 191 | } |
| 192 | 192 | |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | $current_data_args_key = md5( maybe_serialize( $current_data_args ) ); |
| 94 | 94 | |
| 95 | 95 | // Check to make sure we haven't already run this call before. |
| 96 | - $current_data = $this->wp_data[ $type . $current_data_args_key ] ?? $this->get_data( $type, $current_data_args, $current_value ); |
|
| 96 | + $current_data = $this->wp_data[$type . $current_data_args_key] ?? $this->get_data( $type, $current_data_args, $current_value ); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | // If ajax is enabled AND $current_data is empty, set a dummy value for the init. |
@@ -112,8 +112,8 @@ discard block |
||
| 112 | 112 | $args_key = md5( maybe_serialize( $args ) ); |
| 113 | 113 | |
| 114 | 114 | // Data caching. |
| 115 | - if ( isset( $this->wp_data[ $type . $args_key ] ) ) { |
|
| 116 | - $data = $this->wp_data[ $type . $args_key ]; |
|
| 115 | + if ( isset( $this->wp_data[$type . $args_key] ) ) { |
|
| 116 | + $data = $this->wp_data[$type . $args_key]; |
|
| 117 | 117 | } else { |
| 118 | 118 | /** |
| 119 | 119 | * Use data from WordPress to populate an option array. |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | |
| 128 | 128 | if ( ! empty( $data ) ) { |
| 129 | 129 | $data = $this->order_data( $data, $data_sort, $data_order ); |
| 130 | - $this->wp_data[ $type . $args_key ] = $data; |
|
| 130 | + $this->wp_data[$type . $args_key] = $data; |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | /** |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | if ( is_object( $v ) ) { |
| 163 | 163 | $key = $v->$id_key; |
| 164 | 164 | } elseif ( is_array( $v ) ) { |
| 165 | - $key = $v[ $id_key ]; |
|
| 165 | + $key = $v[$id_key]; |
|
| 166 | 166 | } else { |
| 167 | 167 | $key = $k; |
| 168 | 168 | } |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | if ( is_object( $v ) ) { |
| 175 | 175 | $value = $v->$name_key; |
| 176 | 176 | } elseif ( is_array( $v ) ) { |
| 177 | - $value = $v[ $name_key ]; |
|
| 177 | + $value = $v[$name_key]; |
|
| 178 | 178 | } else { |
| 179 | 179 | $value = $v; |
| 180 | 180 | } |
@@ -184,12 +184,12 @@ discard block |
||
| 184 | 184 | |
| 185 | 185 | if ( is_object( $v ) && isset( $v->$secondary_key ) ) { |
| 186 | 186 | $display_key = $v->$secondary_key; |
| 187 | - } elseif ( ! is_object( $v ) && isset( $v[ $secondary_key ] ) ) { |
|
| 188 | - $display_key = $v[ $secondary_key ]; |
|
| 187 | + } elseif ( ! is_object( $v ) && isset( $v[$secondary_key] ) ) { |
|
| 188 | + $display_key = $v[$secondary_key]; |
|
| 189 | 189 | } |
| 190 | - $data[ $key ] = $value; |
|
| 190 | + $data[$key] = $value; |
|
| 191 | 191 | if ( $display_key !== $value && $add_key ) { |
| 192 | - $data[ $key ] = $data[ $key ] . ' [' . $display_key . ']'; |
|
| 192 | + $data[$key] = $data[$key] . ' [' . $display_key . ']'; |
|
| 193 | 193 | } |
| 194 | 194 | } |
| 195 | 195 | } |
@@ -308,14 +308,14 @@ discard block |
||
| 308 | 308 | if ( isset( $sites ) ) { |
| 309 | 309 | $results = array(); |
| 310 | 310 | foreach ( $sites as $site ) { |
| 311 | - $site = (array) $site; |
|
| 311 | + $site = ( array ) $site; |
|
| 312 | 312 | $k = $site['blog_id']; |
| 313 | 313 | $v = $site['domain'] . $site['path']; |
| 314 | 314 | $name = get_blog_option( $k, 'blogname' ); |
| 315 | 315 | if ( ! empty( $name ) ) { |
| 316 | 316 | $v .= ' - [' . $name . ']'; |
| 317 | 317 | } |
| 318 | - $results[ $k ] = $v; |
|
| 318 | + $results[$k] = $v; |
|
| 319 | 319 | } |
| 320 | 320 | $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
| 321 | 321 | } |
@@ -341,10 +341,10 @@ discard block |
||
| 341 | 341 | $post_types = get_post_types( $args, $output, $operator ); |
| 342 | 342 | |
| 343 | 343 | foreach ( $post_types as $name => $title ) { |
| 344 | - if ( isset( $wp_post_types[ $name ]->labels->menu_name ) ) { |
|
| 345 | - $data[ $name ] = $wp_post_types[ $name ]->labels->menu_name; |
|
| 344 | + if ( isset( $wp_post_types[$name]->labels->menu_name ) ) { |
|
| 345 | + $data[$name] = $wp_post_types[$name]->labels->menu_name; |
|
| 346 | 346 | } else { |
| 347 | - $data[ $name ] = ucfirst( $name ); |
|
| 347 | + $data[$name] = ucfirst( $name ); |
|
| 348 | 348 | } |
| 349 | 349 | } |
| 350 | 350 | break; |
@@ -353,9 +353,9 @@ discard block |
||
| 353 | 353 | case 'menu_location': |
| 354 | 354 | global $_wp_registered_nav_menus; |
| 355 | 355 | foreach ( $_wp_registered_nav_menus as $k => $v ) { |
| 356 | - $data[ $k ] = $v; |
|
| 356 | + $data[$k] = $v; |
|
| 357 | 357 | if ( ! has_nav_menu( $k ) ) { |
| 358 | - $data[ $k ] .= ' ' . __( '[unassigned]', 'redux-framework' ); |
|
| 358 | + $data[$k] .= ' ' . __( '[unassigned]', 'redux-framework' ); |
|
| 359 | 359 | } |
| 360 | 360 | } |
| 361 | 361 | break; |
@@ -365,7 +365,7 @@ discard block |
||
| 365 | 365 | global $_wp_additional_image_sizes; |
| 366 | 366 | $results = array(); |
| 367 | 367 | foreach ( $_wp_additional_image_sizes as $size_name => $size_attrs ) { |
| 368 | - $results[ $size_name ] = $size_name . ' - ' . $size_attrs['width'] . ' x ' . $size_attrs['height']; |
|
| 368 | + $results[$size_name] = $size_name . ' - ' . $size_attrs['width'] . ' x ' . $size_attrs['height']; |
|
| 369 | 369 | } |
| 370 | 370 | $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
| 371 | 371 | |
@@ -385,7 +385,7 @@ discard block |
||
| 385 | 385 | if ( 'before' === $item ) { |
| 386 | 386 | continue; |
| 387 | 387 | } |
| 388 | - $data[ 'el el-' . $item ] = $item; |
|
| 388 | + $data['el el-' . $item] = $item; |
|
| 389 | 389 | } |
| 390 | 390 | } |
| 391 | 391 | |
@@ -422,7 +422,7 @@ discard block |
||
| 422 | 422 | if ( 'before' === $item ) { |
| 423 | 423 | continue; |
| 424 | 424 | } |
| 425 | - $data[ 'dashicons dashicons-' . $item ] = $item; |
|
| 425 | + $data['dashicons dashicons-' . $item] = $item; |
|
| 426 | 426 | } |
| 427 | 427 | } |
| 428 | 428 | break; |
@@ -446,7 +446,7 @@ discard block |
||
| 446 | 446 | $results = array(); |
| 447 | 447 | foreach ( $wp_roles->roles as $role ) { |
| 448 | 448 | foreach ( $role['capabilities'] as $key => $cap ) { |
| 449 | - $results[ $key ] = ucwords( str_replace( '_', ' ', $key ) ); |
|
| 449 | + $results[$key] = ucwords( str_replace( '_', ' ', $key ) ); |
|
| 450 | 450 | } |
| 451 | 451 | } |
| 452 | 452 | $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
@@ -462,10 +462,10 @@ discard block |
||
| 462 | 462 | foreach ( $wp_roles->roles as $role ) { |
| 463 | 463 | $caps = array(); |
| 464 | 464 | foreach ( $role['capabilities'] as $key => $cap ) { |
| 465 | - $caps[ $key ] = ucwords( str_replace( '_', ' ', $key ) ); |
|
| 465 | + $caps[$key] = ucwords( str_replace( '_', ' ', $key ) ); |
|
| 466 | 466 | } |
| 467 | 467 | asort( $caps ); |
| 468 | - $data[ $role['name'] ] = $caps; |
|
| 468 | + $data[$role['name']] = $caps; |
|
| 469 | 469 | } |
| 470 | 470 | |
| 471 | 471 | break; |
@@ -543,7 +543,7 @@ discard block |
||
| 543 | 543 | $value = apply_filters( 'wpml_object_id', $value, $post_type, true ); |
| 544 | 544 | } elseif ( is_array( $value ) ) { |
| 545 | 545 | $value = array_map( |
| 546 | - function ( $val ) use ( $post_type ) { |
|
| 546 | + function( $val ) use ( $post_type ) { |
|
| 547 | 547 | return apply_filters( 'wpml_object_id', $val, $post_type, true ); |
| 548 | 548 | }, |
| 549 | 549 | $value |
@@ -11,677 +11,677 @@ |
||
| 11 | 11 | |
| 12 | 12 | if ( ! class_exists( 'Redux_WordPress_Data', false ) ) { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Class Redux_WordPress_Data |
|
| 16 | - */ |
|
| 17 | - class Redux_WordPress_Data extends Redux_Class { |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Holds WordPress data. |
|
| 21 | - * |
|
| 22 | - * @var null |
|
| 23 | - */ |
|
| 24 | - private $wp_data = null; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Redux_WordPress_Data constructor. |
|
| 28 | - * |
|
| 29 | - * @param mixed $redux ReduxFramework pointer or opt_name. |
|
| 30 | - */ |
|
| 31 | - public function __construct( $redux = null ) { |
|
| 32 | - if ( is_string( $redux ) ) { |
|
| 33 | - $this->opt_name = $redux; |
|
| 34 | - } else { |
|
| 35 | - parent::__construct( $redux ); |
|
| 36 | - } |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Get the data. |
|
| 41 | - * |
|
| 42 | - * @param string|array $type Type. |
|
| 43 | - * @param array|string $args Args. |
|
| 44 | - * @param string $opt_name Opt name. |
|
| 45 | - * @param string|int $current_value Current value. |
|
| 46 | - * @param bool $ajax Tells if this is an AJAX call. |
|
| 47 | - * |
|
| 48 | - * @return array|mixed|string |
|
| 49 | - */ |
|
| 50 | - public function get( $type, $args = array(), string $opt_name = '', $current_value = '', bool $ajax = false ) { |
|
| 51 | - if ( '' === $opt_name ) { |
|
| 52 | - $opt_name = $this->opt_name; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - // We don't want to run this, it's not a string value. Send it back! |
|
| 56 | - if ( is_array( $type ) ) { |
|
| 57 | - return $type; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Filter 'redux/options/{opt_name}/pre_data/{type}' |
|
| 62 | - * |
|
| 63 | - * @param string $data |
|
| 64 | - */ |
|
| 65 | - $pre_data = apply_filters( "redux/options/$opt_name/pre_data/$type", null ); // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 66 | - if ( null !== $pre_data || empty( $type ) ) { |
|
| 67 | - return $pre_data; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - if ( isset( $args['data_sortby'] ) && in_array( $args['data_sortby'], array( 'value', 'key' ), true ) ) { |
|
| 71 | - $data_sort = $args['data_sortby']; |
|
| 72 | - unset( $args['data_sortby'] ); |
|
| 73 | - } else { |
|
| 74 | - $data_sort = 'value'; |
|
| 75 | - } |
|
| 76 | - if ( isset( $args['data_order'] ) && in_array( $args['data_order'], array( 'asc', 'desc' ), true ) ) { |
|
| 77 | - $data_order = $args['data_order']; |
|
| 78 | - unset( $args['data_order'] ); |
|
| 79 | - } else { |
|
| 80 | - $data_order = 'asc'; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - $this->maybe_get_translation( $type, $current_value, $args ); |
|
| 84 | - |
|
| 85 | - $current_data = array(); |
|
| 86 | - if ( empty( $current_value ) && ! Redux_Helpers::is_integer( $current_value ) ) { |
|
| 87 | - $current_value = null; |
|
| 88 | - } else { |
|
| 89 | - // Get the args to grab the current data. |
|
| 90 | - $current_data_args = $this->get_current_data_args( $type, $args, $current_value ); |
|
| 91 | - |
|
| 92 | - // Let's make a unique key for this arg array. |
|
| 93 | - $current_data_args_key = md5( maybe_serialize( $current_data_args ) ); |
|
| 94 | - |
|
| 95 | - // Check to make sure we haven't already run this call before. |
|
| 96 | - $current_data = $this->wp_data[ $type . $current_data_args_key ] ?? $this->get_data( $type, $current_data_args, $current_value ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // If ajax is enabled AND $current_data is empty, set a dummy value for the init. |
|
| 100 | - if ( $ajax && ! wp_doing_ajax() ) { |
|
| 101 | - // Dummy is necessary otherwise empty. |
|
| 102 | - if ( empty( $current_data ) ) { |
|
| 103 | - $current_data = array( |
|
| 104 | - 'dummy' => '', |
|
| 105 | - ); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - return $current_data; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - // phpcs:ignore Squiz.PHP.CommentedOutCode |
|
| 112 | - $args_key = md5( maybe_serialize( $args ) ); |
|
| 113 | - |
|
| 114 | - // Data caching. |
|
| 115 | - if ( isset( $this->wp_data[ $type . $args_key ] ) ) { |
|
| 116 | - $data = $this->wp_data[ $type . $args_key ]; |
|
| 117 | - } else { |
|
| 118 | - /** |
|
| 119 | - * Use data from WordPress to populate an option array. |
|
| 120 | - * */ |
|
| 121 | - $data = $this->get_data( $type, $args, $current_value ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - if ( ! empty( $current_data ) ) { |
|
| 125 | - $data += $current_data; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - if ( ! empty( $data ) ) { |
|
| 129 | - $data = $this->order_data( $data, $data_sort, $data_order ); |
|
| 130 | - $this->wp_data[ $type . $args_key ] = $data; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Filter 'redux/options/{opt_name}/data/{type}' |
|
| 135 | - * |
|
| 136 | - * @param string $data |
|
| 137 | - */ |
|
| 138 | - |
|
| 139 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 140 | - return apply_filters( "redux/options/$opt_name/data/$type", $data ); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Process the results into a proper array, fetching the data elements needed for each data type. |
|
| 146 | - * |
|
| 147 | - * @param array|WP_Error $results Results to process in the data array. |
|
| 148 | - * @param string|bool $id_key Key on object/array that represents the ID. |
|
| 149 | - * @param string|bool $name_key Key on object/array that represents the name/text. |
|
| 150 | - * @param bool $add_key If true, the display key will appear in the text. |
|
| 151 | - * @param string|bool $secondary_key If a data type, you'd rather display a different ID as the display key. |
|
| 152 | - * |
|
| 153 | - * @return array |
|
| 154 | - */ |
|
| 155 | - private function process_results( $results = array(), $id_key = '', $name_key = '', bool $add_key = true, $secondary_key = 'slug' ): array { |
|
| 156 | - $data = array(); |
|
| 157 | - if ( ! empty( $results ) && ! is_a( $results, 'WP_Error' ) ) { |
|
| 158 | - foreach ( $results as $k => $v ) { |
|
| 159 | - if ( empty( $id_key ) ) { |
|
| 160 | - $key = $k; |
|
| 161 | - } else { |
|
| 162 | - if ( is_object( $v ) ) { |
|
| 163 | - $key = $v->$id_key; |
|
| 164 | - } elseif ( is_array( $v ) ) { |
|
| 165 | - $key = $v[ $id_key ]; |
|
| 166 | - } else { |
|
| 167 | - $key = $k; |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - if ( empty( $name_key ) ) { |
|
| 172 | - $value = $v; |
|
| 173 | - } else { |
|
| 174 | - if ( is_object( $v ) ) { |
|
| 175 | - $value = $v->$name_key; |
|
| 176 | - } elseif ( is_array( $v ) ) { |
|
| 177 | - $value = $v[ $name_key ]; |
|
| 178 | - } else { |
|
| 179 | - $value = $v; |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - $display_key = $key; |
|
| 184 | - |
|
| 185 | - if ( is_object( $v ) && isset( $v->$secondary_key ) ) { |
|
| 186 | - $display_key = $v->$secondary_key; |
|
| 187 | - } elseif ( ! is_object( $v ) && isset( $v[ $secondary_key ] ) ) { |
|
| 188 | - $display_key = $v[ $secondary_key ]; |
|
| 189 | - } |
|
| 190 | - $data[ $key ] = $value; |
|
| 191 | - if ( $display_key !== $value && $add_key ) { |
|
| 192 | - $data[ $key ] = $data[ $key ] . ' [' . $display_key . ']'; |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - return $data; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Order / Sort the data. |
|
| 202 | - * |
|
| 203 | - * @param array $data Data to sort. |
|
| 204 | - * @param string $sort Way to sort. Accepts: key|value. |
|
| 205 | - * @param string $order Order of the sort. Accepts: asc|desc. |
|
| 206 | - * |
|
| 207 | - * @return array |
|
| 208 | - */ |
|
| 209 | - private function order_data( array $data = array(), string $sort = 'value', string $order = 'asc' ): array { |
|
| 210 | - if ( 'key' === $sort ) { |
|
| 211 | - if ( 'asc' === $order ) { |
|
| 212 | - ksort( $data ); |
|
| 213 | - } else { |
|
| 214 | - krsort( $data ); |
|
| 215 | - } |
|
| 216 | - } elseif ( 'value' === $sort ) { |
|
| 217 | - if ( 'asc' === $order ) { |
|
| 218 | - asort( $data ); |
|
| 219 | - } else { |
|
| 220 | - arsort( $data ); |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - return $data; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Fetch the data for a given type. |
|
| 229 | - * |
|
| 230 | - * @param string $type The data type we're fetching. |
|
| 231 | - * @param array|string $args Arguments to pass. |
|
| 232 | - * @param mixed|array $current_value If a current value already set in the database. |
|
| 233 | - * |
|
| 234 | - * @return array|null|string |
|
| 235 | - */ |
|
| 236 | - private function get_data( string $type, $args, $current_value ) { |
|
| 237 | - $args = $this->get_arg_defaults( $type, $args ); |
|
| 238 | - |
|
| 239 | - $opt_name = $this->opt_name; |
|
| 240 | - if ( empty( $args ) ) { |
|
| 241 | - $args = array(); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - $data = array(); |
|
| 245 | - if ( isset( $args['args'] ) && empty( $args['args'] ) ) { |
|
| 246 | - unset( $args['args'] ); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - $display_keys = false; |
|
| 250 | - if ( isset( $args['display_keys'] ) ) { |
|
| 251 | - $display_keys = true; |
|
| 252 | - unset( $args['display_keys'] ); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - $secondary_key = 'slug'; |
|
| 256 | - if ( isset( $args['secondary_key'] ) ) { |
|
| 257 | - $secondary_key = $args['secondary_key']; |
|
| 258 | - unset( $args['secondary_key'] ); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - switch ( $type ) { |
|
| 262 | - case 'categories': |
|
| 263 | - case 'category': |
|
| 264 | - case 'terms': |
|
| 265 | - case 'term': |
|
| 266 | - if ( isset( $args['taxonomies'] ) ) { |
|
| 267 | - $args['taxonomy'] = $args['taxonomies']; |
|
| 268 | - unset( $args['taxonomies'] ); |
|
| 269 | - } |
|
| 270 | - $results = get_terms( $args ); |
|
| 271 | - $data = $this->process_results( $results, 'term_id', 'name', $display_keys, $secondary_key ); |
|
| 272 | - break; |
|
| 273 | - |
|
| 274 | - case 'pages': |
|
| 275 | - case 'page': |
|
| 276 | - $results = get_pages( $args ); |
|
| 277 | - $data = $this->process_results( $results, 'ID', 'post_title', $display_keys, $secondary_key ); |
|
| 278 | - break; |
|
| 279 | - |
|
| 280 | - case 'tags': |
|
| 281 | - case 'tag': |
|
| 282 | - $results = get_tags( $args ); |
|
| 283 | - $data = $this->process_results( $results, 'term_id', 'name', $display_keys, $secondary_key ); |
|
| 284 | - break; |
|
| 285 | - |
|
| 286 | - case 'menus': |
|
| 287 | - case 'menu': |
|
| 288 | - $results = wp_get_nav_menus( $args ); |
|
| 289 | - $data = $this->process_results( $results, 'term_id', 'name', $display_keys, $secondary_key ); |
|
| 290 | - break; |
|
| 291 | - |
|
| 292 | - case 'posts': |
|
| 293 | - case 'post': |
|
| 294 | - $results = get_posts( $args ); |
|
| 295 | - $data = $this->process_results( $results, 'ID', 'post_title', $display_keys, $secondary_key ); |
|
| 296 | - break; |
|
| 297 | - |
|
| 298 | - case 'users': |
|
| 299 | - case 'user': |
|
| 300 | - $results = get_users( $args ); |
|
| 301 | - $data = $this->process_results( $results, 'ID', 'display_name', $display_keys, $secondary_key ); |
|
| 302 | - break; |
|
| 303 | - |
|
| 304 | - case 'sites': |
|
| 305 | - case 'site': |
|
| 306 | - $sites = get_sites(); |
|
| 307 | - |
|
| 308 | - if ( isset( $sites ) ) { |
|
| 309 | - $results = array(); |
|
| 310 | - foreach ( $sites as $site ) { |
|
| 311 | - $site = (array) $site; |
|
| 312 | - $k = $site['blog_id']; |
|
| 313 | - $v = $site['domain'] . $site['path']; |
|
| 314 | - $name = get_blog_option( $k, 'blogname' ); |
|
| 315 | - if ( ! empty( $name ) ) { |
|
| 316 | - $v .= ' - [' . $name . ']'; |
|
| 317 | - } |
|
| 318 | - $results[ $k ] = $v; |
|
| 319 | - } |
|
| 320 | - $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - break; |
|
| 324 | - |
|
| 325 | - case 'taxonomies': |
|
| 326 | - case 'taxonomy': |
|
| 327 | - case 'tax': |
|
| 328 | - $results = get_taxonomies( $args ); |
|
| 329 | - $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
|
| 330 | - break; |
|
| 331 | - |
|
| 332 | - case 'post_types': |
|
| 333 | - case 'post_type': |
|
| 334 | - global $wp_post_types; |
|
| 335 | - |
|
| 336 | - $output = $args['output']; |
|
| 337 | - unset( $args['output'] ); |
|
| 338 | - $operator = $args['operator']; |
|
| 339 | - unset( $args['operator'] ); |
|
| 340 | - |
|
| 341 | - $post_types = get_post_types( $args, $output, $operator ); |
|
| 342 | - |
|
| 343 | - foreach ( $post_types as $name => $title ) { |
|
| 344 | - if ( isset( $wp_post_types[ $name ]->labels->menu_name ) ) { |
|
| 345 | - $data[ $name ] = $wp_post_types[ $name ]->labels->menu_name; |
|
| 346 | - } else { |
|
| 347 | - $data[ $name ] = ucfirst( $name ); |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - break; |
|
| 351 | - |
|
| 352 | - case 'menu_locations': |
|
| 353 | - case 'menu_location': |
|
| 354 | - global $_wp_registered_nav_menus; |
|
| 355 | - foreach ( $_wp_registered_nav_menus as $k => $v ) { |
|
| 356 | - $data[ $k ] = $v; |
|
| 357 | - if ( ! has_nav_menu( $k ) ) { |
|
| 358 | - $data[ $k ] .= ' ' . __( '[unassigned]', 'redux-framework' ); |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - break; |
|
| 362 | - |
|
| 363 | - case 'image_sizes': |
|
| 364 | - case 'image_size': |
|
| 365 | - global $_wp_additional_image_sizes; |
|
| 366 | - $results = array(); |
|
| 367 | - foreach ( $_wp_additional_image_sizes as $size_name => $size_attrs ) { |
|
| 368 | - $results[ $size_name ] = $size_name . ' - ' . $size_attrs['width'] . ' x ' . $size_attrs['height']; |
|
| 369 | - } |
|
| 370 | - $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
|
| 371 | - |
|
| 372 | - break; |
|
| 373 | - |
|
| 374 | - case 'elusive-icons': |
|
| 375 | - case 'elusive-icon': |
|
| 376 | - case 'elusive': |
|
| 377 | - case 'icons': |
|
| 378 | - case 'font-icon': |
|
| 379 | - case 'font-icons': |
|
| 380 | - $fs = Redux_Filesystem::get_instance(); |
|
| 381 | - $fonts = $fs->get_contents( Redux_Core::$dir . 'assets/css/vendor/elusive-icons.css' ); |
|
| 382 | - if ( ! empty( $fonts ) ) { |
|
| 383 | - preg_match_all( '@\.el-(\w+)::before@', $fonts, $matches ); |
|
| 384 | - foreach ( $matches[1] as $item ) { |
|
| 385 | - if ( 'before' === $item ) { |
|
| 386 | - continue; |
|
| 387 | - } |
|
| 388 | - $data[ 'el el-' . $item ] = $item; |
|
| 389 | - } |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * Filter 'redux/font-icons' |
|
| 394 | - * |
|
| 395 | - * @param array $font_icons array of elusive icon classes |
|
| 396 | - * |
|
| 397 | - * @deprecated |
|
| 398 | - */ |
|
| 399 | - |
|
| 400 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 401 | - $font_icons = apply_filters_deprecated( 'redux/font-icons', array( $data ), '4.3', 'redux/$opt_name/field/font/icons' ); |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Filter 'redux/{opt_name}/field/font/icons' |
|
| 405 | - * |
|
| 406 | - * @param array $font_icons array of elusive icon classes |
|
| 407 | - */ |
|
| 408 | - |
|
| 409 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 410 | - $data = apply_filters( "redux/$opt_name/field/font/icons", $font_icons ); |
|
| 411 | - |
|
| 412 | - break; |
|
| 413 | - |
|
| 414 | - case 'dashicons': |
|
| 415 | - case 'dashicon': |
|
| 416 | - case 'dash': |
|
| 417 | - $fs = Redux_Filesystem::get_instance(); |
|
| 418 | - $fonts = $fs->get_contents( ABSPATH . WPINC . '/css/dashicons.css' ); |
|
| 419 | - if ( ! empty( $fonts ) ) { |
|
| 420 | - preg_match_all( '@\.dashicons-(\w+):before@', $fonts, $matches ); |
|
| 421 | - foreach ( $matches[1] as $item ) { |
|
| 422 | - if ( 'before' === $item ) { |
|
| 423 | - continue; |
|
| 424 | - } |
|
| 425 | - $data[ 'dashicons dashicons-' . $item ] = $item; |
|
| 426 | - } |
|
| 427 | - } |
|
| 428 | - break; |
|
| 429 | - |
|
| 430 | - case 'roles': |
|
| 431 | - case 'role': |
|
| 432 | - global $wp_roles; |
|
| 433 | - $results = $wp_roles->get_names(); |
|
| 434 | - $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
|
| 435 | - |
|
| 436 | - break; |
|
| 437 | - |
|
| 438 | - case 'sidebars': |
|
| 439 | - case 'sidebar': |
|
| 440 | - global $wp_registered_sidebars; |
|
| 441 | - $data = $this->process_results( $wp_registered_sidebars, '', 'name', $display_keys, $secondary_key ); |
|
| 442 | - break; |
|
| 443 | - case 'capabilities': |
|
| 444 | - case 'capability': |
|
| 445 | - global $wp_roles; |
|
| 446 | - $results = array(); |
|
| 447 | - foreach ( $wp_roles->roles as $role ) { |
|
| 448 | - foreach ( $role['capabilities'] as $key => $cap ) { |
|
| 449 | - $results[ $key ] = ucwords( str_replace( '_', ' ', $key ) ); |
|
| 450 | - } |
|
| 451 | - } |
|
| 452 | - $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
|
| 453 | - |
|
| 454 | - break; |
|
| 455 | - |
|
| 456 | - case 'capabilities_grouped': |
|
| 457 | - case 'capability_grouped': |
|
| 458 | - case 'capabilities_group': |
|
| 459 | - case 'capability_group': |
|
| 460 | - global $wp_roles; |
|
| 461 | - |
|
| 462 | - foreach ( $wp_roles->roles as $role ) { |
|
| 463 | - $caps = array(); |
|
| 464 | - foreach ( $role['capabilities'] as $key => $cap ) { |
|
| 465 | - $caps[ $key ] = ucwords( str_replace( '_', ' ', $key ) ); |
|
| 466 | - } |
|
| 467 | - asort( $caps ); |
|
| 468 | - $data[ $role['name'] ] = $caps; |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - break; |
|
| 472 | - |
|
| 473 | - case 'callback': |
|
| 474 | - if ( ! empty( $args ) && is_callable( $args ) ) { |
|
| 475 | - $data = call_user_func( $args, $current_value ); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - break; |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - return $data; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * Router for translation based on the given post-type. |
|
| 487 | - * |
|
| 488 | - * @param string $type Type of data request. |
|
| 489 | - * @param mixed|array $current_value Current value stored in DB. |
|
| 490 | - * @param array|string $args Arguments for the call. |
|
| 491 | - */ |
|
| 492 | - private function maybe_get_translation( string $type, &$current_value = '', $args = array() ) { |
|
| 493 | - switch ( $type ) { |
|
| 494 | - case 'categories': |
|
| 495 | - case 'category': |
|
| 496 | - $this->maybe_translate( $current_value, 'category' ); |
|
| 497 | - break; |
|
| 498 | - |
|
| 499 | - case 'pages': |
|
| 500 | - case 'page': |
|
| 501 | - $this->maybe_translate( $current_value, 'page' ); |
|
| 502 | - break; |
|
| 503 | - |
|
| 504 | - case 'terms': |
|
| 505 | - case 'term': |
|
| 506 | - $this->maybe_translate( $current_value, $args['taxonomy'] ?? '' ); |
|
| 507 | - break; |
|
| 508 | - |
|
| 509 | - case 'tags': |
|
| 510 | - case 'tag': |
|
| 511 | - $this->maybe_translate( $current_value, 'post_tag' ); |
|
| 512 | - break; |
|
| 513 | - |
|
| 514 | - case 'menus': |
|
| 515 | - case 'menu': |
|
| 516 | - $this->maybe_translate( $current_value, 'nav_menu' ); |
|
| 517 | - break; |
|
| 518 | - |
|
| 519 | - case 'post': |
|
| 520 | - case 'posts': |
|
| 521 | - $this->maybe_translate( $current_value, 'post' ); |
|
| 522 | - break; |
|
| 523 | - default: |
|
| 524 | - $this->maybe_translate( $current_value, '' ); |
|
| 525 | - } |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - /** |
|
| 529 | - * Maybe translate the values. |
|
| 530 | - * |
|
| 531 | - * @param mixed|array $value Value. |
|
| 532 | - * @param mixed|array $post_type Post type. |
|
| 533 | - */ |
|
| 534 | - private function maybe_translate( &$value, $post_type ) { |
|
| 535 | - |
|
| 536 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 537 | - $value = apply_filters( "redux/options/$this->opt_name/wordpress_data/translate/post_type_value", $value, $post_type ); |
|
| 538 | - |
|
| 539 | - // WPML Integration, see https://wpml.org/documentation/support/creating-multilingual-wordpress-themes/language-dependent-ids/. |
|
| 540 | - if ( function_exists( 'icl_object_id' ) ) { |
|
| 541 | - if ( has_filter( 'wpml_object_id' ) ) { |
|
| 542 | - if ( Redux_Helpers::is_integer( $value ) ) { |
|
| 543 | - $value = apply_filters( 'wpml_object_id', $value, $post_type, true ); |
|
| 544 | - } elseif ( is_array( $value ) ) { |
|
| 545 | - $value = array_map( |
|
| 546 | - function ( $val ) use ( $post_type ) { |
|
| 547 | - return apply_filters( 'wpml_object_id', $val, $post_type, true ); |
|
| 548 | - }, |
|
| 549 | - $value |
|
| 550 | - ); |
|
| 551 | - } |
|
| 552 | - } |
|
| 553 | - } |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * Set the default arguments for a current query (existing data). |
|
| 558 | - * |
|
| 559 | - * @param string $type Type of data request. |
|
| 560 | - * @param array|string $args Arguments for the call. |
|
| 561 | - * @param mixed|array $current_value Current value stored in DB. |
|
| 562 | - * |
|
| 563 | - * @return array |
|
| 564 | - */ |
|
| 565 | - private function get_current_data_args( string $type, $args, $current_value ): array { |
|
| 566 | - // In this section, we set the default arguments for each data type. |
|
| 567 | - switch ( $type ) { |
|
| 568 | - case 'categories': |
|
| 569 | - case 'category': |
|
| 570 | - case 'pages': |
|
| 571 | - case 'page': |
|
| 572 | - case 'terms': |
|
| 573 | - case 'term': |
|
| 574 | - case 'users': |
|
| 575 | - case 'user': |
|
| 576 | - $args['include'] = $current_value; |
|
| 577 | - break; |
|
| 578 | - case 'tags': |
|
| 579 | - case 'tag': |
|
| 580 | - $args['get'] = 'all'; |
|
| 581 | - break; |
|
| 582 | - case 'menus': |
|
| 583 | - case 'menu': |
|
| 584 | - $args['object_ids'] = $current_value; |
|
| 585 | - break; |
|
| 586 | - case 'post': |
|
| 587 | - case 'posts': |
|
| 588 | - if ( ! empty( $current_value ) ) { |
|
| 589 | - $args['post__in'] = is_array( $current_value ) ? $current_value : array( $current_value ); |
|
| 590 | - } |
|
| 591 | - break; |
|
| 592 | - |
|
| 593 | - default: |
|
| 594 | - $args = array(); |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - return $args; |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * Get default arguments for a given data type. |
|
| 603 | - * |
|
| 604 | - * @param string $type Type of data request. |
|
| 605 | - * @param array|string $args Arguments for the call. |
|
| 606 | - * |
|
| 607 | - * @return array|string |
|
| 608 | - */ |
|
| 609 | - private function get_arg_defaults( string $type, $args = array() ) { |
|
| 610 | - // In this section, we set the default arguments for each data type. |
|
| 611 | - switch ( $type ) { |
|
| 612 | - case 'categories': |
|
| 613 | - case 'category': |
|
| 614 | - $args = wp_parse_args( |
|
| 615 | - $args, |
|
| 616 | - array( |
|
| 617 | - 'taxonomy' => 'category', |
|
| 618 | - ) |
|
| 619 | - ); |
|
| 620 | - break; |
|
| 621 | - |
|
| 622 | - case 'pages': |
|
| 623 | - case 'page': |
|
| 624 | - $args = wp_parse_args( |
|
| 625 | - $args, |
|
| 626 | - array( |
|
| 627 | - 'display_keys' => true, |
|
| 628 | - 'posts_per_page' => 20, |
|
| 629 | - ) |
|
| 630 | - ); |
|
| 631 | - break; |
|
| 632 | - |
|
| 633 | - case 'post_type': |
|
| 634 | - case 'post_types': |
|
| 635 | - $args = wp_parse_args( |
|
| 636 | - $args, |
|
| 637 | - array( |
|
| 638 | - 'public' => true, |
|
| 639 | - 'exclude_from_search' => false, |
|
| 640 | - 'output' => 'names', |
|
| 641 | - 'operator' => 'and', |
|
| 642 | - ) |
|
| 643 | - ); |
|
| 644 | - |
|
| 645 | - break; |
|
| 646 | - |
|
| 647 | - case 'tag': |
|
| 648 | - case 'tags': |
|
| 649 | - $args = wp_parse_args( |
|
| 650 | - $args, |
|
| 651 | - array( |
|
| 652 | - 'get' => 'all', |
|
| 653 | - 'display_keys' => true, |
|
| 654 | - ) |
|
| 655 | - ); |
|
| 656 | - break; |
|
| 657 | - |
|
| 658 | - case 'sidebars': |
|
| 659 | - case 'sidebar': |
|
| 660 | - case 'capabilities': |
|
| 661 | - case 'capability': |
|
| 662 | - $args = wp_parse_args( |
|
| 663 | - $args, |
|
| 664 | - array( |
|
| 665 | - 'display_keys' => true, |
|
| 666 | - ) |
|
| 667 | - ); |
|
| 668 | - break; |
|
| 669 | - |
|
| 670 | - case 'capabilities_grouped': |
|
| 671 | - case 'capability_grouped': |
|
| 672 | - case 'capabilities_group': |
|
| 673 | - case 'capability_group': |
|
| 674 | - $args = wp_parse_args( |
|
| 675 | - $args, |
|
| 676 | - array( |
|
| 677 | - 'data_sortby' => '', |
|
| 678 | - ) |
|
| 679 | - ); |
|
| 680 | - break; |
|
| 681 | - |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - return $args; |
|
| 685 | - } |
|
| 686 | - } |
|
| 14 | + /** |
|
| 15 | + * Class Redux_WordPress_Data |
|
| 16 | + */ |
|
| 17 | + class Redux_WordPress_Data extends Redux_Class { |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Holds WordPress data. |
|
| 21 | + * |
|
| 22 | + * @var null |
|
| 23 | + */ |
|
| 24 | + private $wp_data = null; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Redux_WordPress_Data constructor. |
|
| 28 | + * |
|
| 29 | + * @param mixed $redux ReduxFramework pointer or opt_name. |
|
| 30 | + */ |
|
| 31 | + public function __construct( $redux = null ) { |
|
| 32 | + if ( is_string( $redux ) ) { |
|
| 33 | + $this->opt_name = $redux; |
|
| 34 | + } else { |
|
| 35 | + parent::__construct( $redux ); |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Get the data. |
|
| 41 | + * |
|
| 42 | + * @param string|array $type Type. |
|
| 43 | + * @param array|string $args Args. |
|
| 44 | + * @param string $opt_name Opt name. |
|
| 45 | + * @param string|int $current_value Current value. |
|
| 46 | + * @param bool $ajax Tells if this is an AJAX call. |
|
| 47 | + * |
|
| 48 | + * @return array|mixed|string |
|
| 49 | + */ |
|
| 50 | + public function get( $type, $args = array(), string $opt_name = '', $current_value = '', bool $ajax = false ) { |
|
| 51 | + if ( '' === $opt_name ) { |
|
| 52 | + $opt_name = $this->opt_name; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + // We don't want to run this, it's not a string value. Send it back! |
|
| 56 | + if ( is_array( $type ) ) { |
|
| 57 | + return $type; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Filter 'redux/options/{opt_name}/pre_data/{type}' |
|
| 62 | + * |
|
| 63 | + * @param string $data |
|
| 64 | + */ |
|
| 65 | + $pre_data = apply_filters( "redux/options/$opt_name/pre_data/$type", null ); // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 66 | + if ( null !== $pre_data || empty( $type ) ) { |
|
| 67 | + return $pre_data; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + if ( isset( $args['data_sortby'] ) && in_array( $args['data_sortby'], array( 'value', 'key' ), true ) ) { |
|
| 71 | + $data_sort = $args['data_sortby']; |
|
| 72 | + unset( $args['data_sortby'] ); |
|
| 73 | + } else { |
|
| 74 | + $data_sort = 'value'; |
|
| 75 | + } |
|
| 76 | + if ( isset( $args['data_order'] ) && in_array( $args['data_order'], array( 'asc', 'desc' ), true ) ) { |
|
| 77 | + $data_order = $args['data_order']; |
|
| 78 | + unset( $args['data_order'] ); |
|
| 79 | + } else { |
|
| 80 | + $data_order = 'asc'; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + $this->maybe_get_translation( $type, $current_value, $args ); |
|
| 84 | + |
|
| 85 | + $current_data = array(); |
|
| 86 | + if ( empty( $current_value ) && ! Redux_Helpers::is_integer( $current_value ) ) { |
|
| 87 | + $current_value = null; |
|
| 88 | + } else { |
|
| 89 | + // Get the args to grab the current data. |
|
| 90 | + $current_data_args = $this->get_current_data_args( $type, $args, $current_value ); |
|
| 91 | + |
|
| 92 | + // Let's make a unique key for this arg array. |
|
| 93 | + $current_data_args_key = md5( maybe_serialize( $current_data_args ) ); |
|
| 94 | + |
|
| 95 | + // Check to make sure we haven't already run this call before. |
|
| 96 | + $current_data = $this->wp_data[ $type . $current_data_args_key ] ?? $this->get_data( $type, $current_data_args, $current_value ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // If ajax is enabled AND $current_data is empty, set a dummy value for the init. |
|
| 100 | + if ( $ajax && ! wp_doing_ajax() ) { |
|
| 101 | + // Dummy is necessary otherwise empty. |
|
| 102 | + if ( empty( $current_data ) ) { |
|
| 103 | + $current_data = array( |
|
| 104 | + 'dummy' => '', |
|
| 105 | + ); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + return $current_data; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + // phpcs:ignore Squiz.PHP.CommentedOutCode |
|
| 112 | + $args_key = md5( maybe_serialize( $args ) ); |
|
| 113 | + |
|
| 114 | + // Data caching. |
|
| 115 | + if ( isset( $this->wp_data[ $type . $args_key ] ) ) { |
|
| 116 | + $data = $this->wp_data[ $type . $args_key ]; |
|
| 117 | + } else { |
|
| 118 | + /** |
|
| 119 | + * Use data from WordPress to populate an option array. |
|
| 120 | + * */ |
|
| 121 | + $data = $this->get_data( $type, $args, $current_value ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + if ( ! empty( $current_data ) ) { |
|
| 125 | + $data += $current_data; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + if ( ! empty( $data ) ) { |
|
| 129 | + $data = $this->order_data( $data, $data_sort, $data_order ); |
|
| 130 | + $this->wp_data[ $type . $args_key ] = $data; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Filter 'redux/options/{opt_name}/data/{type}' |
|
| 135 | + * |
|
| 136 | + * @param string $data |
|
| 137 | + */ |
|
| 138 | + |
|
| 139 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 140 | + return apply_filters( "redux/options/$opt_name/data/$type", $data ); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Process the results into a proper array, fetching the data elements needed for each data type. |
|
| 146 | + * |
|
| 147 | + * @param array|WP_Error $results Results to process in the data array. |
|
| 148 | + * @param string|bool $id_key Key on object/array that represents the ID. |
|
| 149 | + * @param string|bool $name_key Key on object/array that represents the name/text. |
|
| 150 | + * @param bool $add_key If true, the display key will appear in the text. |
|
| 151 | + * @param string|bool $secondary_key If a data type, you'd rather display a different ID as the display key. |
|
| 152 | + * |
|
| 153 | + * @return array |
|
| 154 | + */ |
|
| 155 | + private function process_results( $results = array(), $id_key = '', $name_key = '', bool $add_key = true, $secondary_key = 'slug' ): array { |
|
| 156 | + $data = array(); |
|
| 157 | + if ( ! empty( $results ) && ! is_a( $results, 'WP_Error' ) ) { |
|
| 158 | + foreach ( $results as $k => $v ) { |
|
| 159 | + if ( empty( $id_key ) ) { |
|
| 160 | + $key = $k; |
|
| 161 | + } else { |
|
| 162 | + if ( is_object( $v ) ) { |
|
| 163 | + $key = $v->$id_key; |
|
| 164 | + } elseif ( is_array( $v ) ) { |
|
| 165 | + $key = $v[ $id_key ]; |
|
| 166 | + } else { |
|
| 167 | + $key = $k; |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + if ( empty( $name_key ) ) { |
|
| 172 | + $value = $v; |
|
| 173 | + } else { |
|
| 174 | + if ( is_object( $v ) ) { |
|
| 175 | + $value = $v->$name_key; |
|
| 176 | + } elseif ( is_array( $v ) ) { |
|
| 177 | + $value = $v[ $name_key ]; |
|
| 178 | + } else { |
|
| 179 | + $value = $v; |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + $display_key = $key; |
|
| 184 | + |
|
| 185 | + if ( is_object( $v ) && isset( $v->$secondary_key ) ) { |
|
| 186 | + $display_key = $v->$secondary_key; |
|
| 187 | + } elseif ( ! is_object( $v ) && isset( $v[ $secondary_key ] ) ) { |
|
| 188 | + $display_key = $v[ $secondary_key ]; |
|
| 189 | + } |
|
| 190 | + $data[ $key ] = $value; |
|
| 191 | + if ( $display_key !== $value && $add_key ) { |
|
| 192 | + $data[ $key ] = $data[ $key ] . ' [' . $display_key . ']'; |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + return $data; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Order / Sort the data. |
|
| 202 | + * |
|
| 203 | + * @param array $data Data to sort. |
|
| 204 | + * @param string $sort Way to sort. Accepts: key|value. |
|
| 205 | + * @param string $order Order of the sort. Accepts: asc|desc. |
|
| 206 | + * |
|
| 207 | + * @return array |
|
| 208 | + */ |
|
| 209 | + private function order_data( array $data = array(), string $sort = 'value', string $order = 'asc' ): array { |
|
| 210 | + if ( 'key' === $sort ) { |
|
| 211 | + if ( 'asc' === $order ) { |
|
| 212 | + ksort( $data ); |
|
| 213 | + } else { |
|
| 214 | + krsort( $data ); |
|
| 215 | + } |
|
| 216 | + } elseif ( 'value' === $sort ) { |
|
| 217 | + if ( 'asc' === $order ) { |
|
| 218 | + asort( $data ); |
|
| 219 | + } else { |
|
| 220 | + arsort( $data ); |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + return $data; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Fetch the data for a given type. |
|
| 229 | + * |
|
| 230 | + * @param string $type The data type we're fetching. |
|
| 231 | + * @param array|string $args Arguments to pass. |
|
| 232 | + * @param mixed|array $current_value If a current value already set in the database. |
|
| 233 | + * |
|
| 234 | + * @return array|null|string |
|
| 235 | + */ |
|
| 236 | + private function get_data( string $type, $args, $current_value ) { |
|
| 237 | + $args = $this->get_arg_defaults( $type, $args ); |
|
| 238 | + |
|
| 239 | + $opt_name = $this->opt_name; |
|
| 240 | + if ( empty( $args ) ) { |
|
| 241 | + $args = array(); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + $data = array(); |
|
| 245 | + if ( isset( $args['args'] ) && empty( $args['args'] ) ) { |
|
| 246 | + unset( $args['args'] ); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + $display_keys = false; |
|
| 250 | + if ( isset( $args['display_keys'] ) ) { |
|
| 251 | + $display_keys = true; |
|
| 252 | + unset( $args['display_keys'] ); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + $secondary_key = 'slug'; |
|
| 256 | + if ( isset( $args['secondary_key'] ) ) { |
|
| 257 | + $secondary_key = $args['secondary_key']; |
|
| 258 | + unset( $args['secondary_key'] ); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + switch ( $type ) { |
|
| 262 | + case 'categories': |
|
| 263 | + case 'category': |
|
| 264 | + case 'terms': |
|
| 265 | + case 'term': |
|
| 266 | + if ( isset( $args['taxonomies'] ) ) { |
|
| 267 | + $args['taxonomy'] = $args['taxonomies']; |
|
| 268 | + unset( $args['taxonomies'] ); |
|
| 269 | + } |
|
| 270 | + $results = get_terms( $args ); |
|
| 271 | + $data = $this->process_results( $results, 'term_id', 'name', $display_keys, $secondary_key ); |
|
| 272 | + break; |
|
| 273 | + |
|
| 274 | + case 'pages': |
|
| 275 | + case 'page': |
|
| 276 | + $results = get_pages( $args ); |
|
| 277 | + $data = $this->process_results( $results, 'ID', 'post_title', $display_keys, $secondary_key ); |
|
| 278 | + break; |
|
| 279 | + |
|
| 280 | + case 'tags': |
|
| 281 | + case 'tag': |
|
| 282 | + $results = get_tags( $args ); |
|
| 283 | + $data = $this->process_results( $results, 'term_id', 'name', $display_keys, $secondary_key ); |
|
| 284 | + break; |
|
| 285 | + |
|
| 286 | + case 'menus': |
|
| 287 | + case 'menu': |
|
| 288 | + $results = wp_get_nav_menus( $args ); |
|
| 289 | + $data = $this->process_results( $results, 'term_id', 'name', $display_keys, $secondary_key ); |
|
| 290 | + break; |
|
| 291 | + |
|
| 292 | + case 'posts': |
|
| 293 | + case 'post': |
|
| 294 | + $results = get_posts( $args ); |
|
| 295 | + $data = $this->process_results( $results, 'ID', 'post_title', $display_keys, $secondary_key ); |
|
| 296 | + break; |
|
| 297 | + |
|
| 298 | + case 'users': |
|
| 299 | + case 'user': |
|
| 300 | + $results = get_users( $args ); |
|
| 301 | + $data = $this->process_results( $results, 'ID', 'display_name', $display_keys, $secondary_key ); |
|
| 302 | + break; |
|
| 303 | + |
|
| 304 | + case 'sites': |
|
| 305 | + case 'site': |
|
| 306 | + $sites = get_sites(); |
|
| 307 | + |
|
| 308 | + if ( isset( $sites ) ) { |
|
| 309 | + $results = array(); |
|
| 310 | + foreach ( $sites as $site ) { |
|
| 311 | + $site = (array) $site; |
|
| 312 | + $k = $site['blog_id']; |
|
| 313 | + $v = $site['domain'] . $site['path']; |
|
| 314 | + $name = get_blog_option( $k, 'blogname' ); |
|
| 315 | + if ( ! empty( $name ) ) { |
|
| 316 | + $v .= ' - [' . $name . ']'; |
|
| 317 | + } |
|
| 318 | + $results[ $k ] = $v; |
|
| 319 | + } |
|
| 320 | + $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + break; |
|
| 324 | + |
|
| 325 | + case 'taxonomies': |
|
| 326 | + case 'taxonomy': |
|
| 327 | + case 'tax': |
|
| 328 | + $results = get_taxonomies( $args ); |
|
| 329 | + $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
|
| 330 | + break; |
|
| 331 | + |
|
| 332 | + case 'post_types': |
|
| 333 | + case 'post_type': |
|
| 334 | + global $wp_post_types; |
|
| 335 | + |
|
| 336 | + $output = $args['output']; |
|
| 337 | + unset( $args['output'] ); |
|
| 338 | + $operator = $args['operator']; |
|
| 339 | + unset( $args['operator'] ); |
|
| 340 | + |
|
| 341 | + $post_types = get_post_types( $args, $output, $operator ); |
|
| 342 | + |
|
| 343 | + foreach ( $post_types as $name => $title ) { |
|
| 344 | + if ( isset( $wp_post_types[ $name ]->labels->menu_name ) ) { |
|
| 345 | + $data[ $name ] = $wp_post_types[ $name ]->labels->menu_name; |
|
| 346 | + } else { |
|
| 347 | + $data[ $name ] = ucfirst( $name ); |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + break; |
|
| 351 | + |
|
| 352 | + case 'menu_locations': |
|
| 353 | + case 'menu_location': |
|
| 354 | + global $_wp_registered_nav_menus; |
|
| 355 | + foreach ( $_wp_registered_nav_menus as $k => $v ) { |
|
| 356 | + $data[ $k ] = $v; |
|
| 357 | + if ( ! has_nav_menu( $k ) ) { |
|
| 358 | + $data[ $k ] .= ' ' . __( '[unassigned]', 'redux-framework' ); |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + break; |
|
| 362 | + |
|
| 363 | + case 'image_sizes': |
|
| 364 | + case 'image_size': |
|
| 365 | + global $_wp_additional_image_sizes; |
|
| 366 | + $results = array(); |
|
| 367 | + foreach ( $_wp_additional_image_sizes as $size_name => $size_attrs ) { |
|
| 368 | + $results[ $size_name ] = $size_name . ' - ' . $size_attrs['width'] . ' x ' . $size_attrs['height']; |
|
| 369 | + } |
|
| 370 | + $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
|
| 371 | + |
|
| 372 | + break; |
|
| 373 | + |
|
| 374 | + case 'elusive-icons': |
|
| 375 | + case 'elusive-icon': |
|
| 376 | + case 'elusive': |
|
| 377 | + case 'icons': |
|
| 378 | + case 'font-icon': |
|
| 379 | + case 'font-icons': |
|
| 380 | + $fs = Redux_Filesystem::get_instance(); |
|
| 381 | + $fonts = $fs->get_contents( Redux_Core::$dir . 'assets/css/vendor/elusive-icons.css' ); |
|
| 382 | + if ( ! empty( $fonts ) ) { |
|
| 383 | + preg_match_all( '@\.el-(\w+)::before@', $fonts, $matches ); |
|
| 384 | + foreach ( $matches[1] as $item ) { |
|
| 385 | + if ( 'before' === $item ) { |
|
| 386 | + continue; |
|
| 387 | + } |
|
| 388 | + $data[ 'el el-' . $item ] = $item; |
|
| 389 | + } |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * Filter 'redux/font-icons' |
|
| 394 | + * |
|
| 395 | + * @param array $font_icons array of elusive icon classes |
|
| 396 | + * |
|
| 397 | + * @deprecated |
|
| 398 | + */ |
|
| 399 | + |
|
| 400 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 401 | + $font_icons = apply_filters_deprecated( 'redux/font-icons', array( $data ), '4.3', 'redux/$opt_name/field/font/icons' ); |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Filter 'redux/{opt_name}/field/font/icons' |
|
| 405 | + * |
|
| 406 | + * @param array $font_icons array of elusive icon classes |
|
| 407 | + */ |
|
| 408 | + |
|
| 409 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 410 | + $data = apply_filters( "redux/$opt_name/field/font/icons", $font_icons ); |
|
| 411 | + |
|
| 412 | + break; |
|
| 413 | + |
|
| 414 | + case 'dashicons': |
|
| 415 | + case 'dashicon': |
|
| 416 | + case 'dash': |
|
| 417 | + $fs = Redux_Filesystem::get_instance(); |
|
| 418 | + $fonts = $fs->get_contents( ABSPATH . WPINC . '/css/dashicons.css' ); |
|
| 419 | + if ( ! empty( $fonts ) ) { |
|
| 420 | + preg_match_all( '@\.dashicons-(\w+):before@', $fonts, $matches ); |
|
| 421 | + foreach ( $matches[1] as $item ) { |
|
| 422 | + if ( 'before' === $item ) { |
|
| 423 | + continue; |
|
| 424 | + } |
|
| 425 | + $data[ 'dashicons dashicons-' . $item ] = $item; |
|
| 426 | + } |
|
| 427 | + } |
|
| 428 | + break; |
|
| 429 | + |
|
| 430 | + case 'roles': |
|
| 431 | + case 'role': |
|
| 432 | + global $wp_roles; |
|
| 433 | + $results = $wp_roles->get_names(); |
|
| 434 | + $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
|
| 435 | + |
|
| 436 | + break; |
|
| 437 | + |
|
| 438 | + case 'sidebars': |
|
| 439 | + case 'sidebar': |
|
| 440 | + global $wp_registered_sidebars; |
|
| 441 | + $data = $this->process_results( $wp_registered_sidebars, '', 'name', $display_keys, $secondary_key ); |
|
| 442 | + break; |
|
| 443 | + case 'capabilities': |
|
| 444 | + case 'capability': |
|
| 445 | + global $wp_roles; |
|
| 446 | + $results = array(); |
|
| 447 | + foreach ( $wp_roles->roles as $role ) { |
|
| 448 | + foreach ( $role['capabilities'] as $key => $cap ) { |
|
| 449 | + $results[ $key ] = ucwords( str_replace( '_', ' ', $key ) ); |
|
| 450 | + } |
|
| 451 | + } |
|
| 452 | + $data = $this->process_results( $results, '', '', $display_keys, $secondary_key ); |
|
| 453 | + |
|
| 454 | + break; |
|
| 455 | + |
|
| 456 | + case 'capabilities_grouped': |
|
| 457 | + case 'capability_grouped': |
|
| 458 | + case 'capabilities_group': |
|
| 459 | + case 'capability_group': |
|
| 460 | + global $wp_roles; |
|
| 461 | + |
|
| 462 | + foreach ( $wp_roles->roles as $role ) { |
|
| 463 | + $caps = array(); |
|
| 464 | + foreach ( $role['capabilities'] as $key => $cap ) { |
|
| 465 | + $caps[ $key ] = ucwords( str_replace( '_', ' ', $key ) ); |
|
| 466 | + } |
|
| 467 | + asort( $caps ); |
|
| 468 | + $data[ $role['name'] ] = $caps; |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + break; |
|
| 472 | + |
|
| 473 | + case 'callback': |
|
| 474 | + if ( ! empty( $args ) && is_callable( $args ) ) { |
|
| 475 | + $data = call_user_func( $args, $current_value ); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + break; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + return $data; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * Router for translation based on the given post-type. |
|
| 487 | + * |
|
| 488 | + * @param string $type Type of data request. |
|
| 489 | + * @param mixed|array $current_value Current value stored in DB. |
|
| 490 | + * @param array|string $args Arguments for the call. |
|
| 491 | + */ |
|
| 492 | + private function maybe_get_translation( string $type, &$current_value = '', $args = array() ) { |
|
| 493 | + switch ( $type ) { |
|
| 494 | + case 'categories': |
|
| 495 | + case 'category': |
|
| 496 | + $this->maybe_translate( $current_value, 'category' ); |
|
| 497 | + break; |
|
| 498 | + |
|
| 499 | + case 'pages': |
|
| 500 | + case 'page': |
|
| 501 | + $this->maybe_translate( $current_value, 'page' ); |
|
| 502 | + break; |
|
| 503 | + |
|
| 504 | + case 'terms': |
|
| 505 | + case 'term': |
|
| 506 | + $this->maybe_translate( $current_value, $args['taxonomy'] ?? '' ); |
|
| 507 | + break; |
|
| 508 | + |
|
| 509 | + case 'tags': |
|
| 510 | + case 'tag': |
|
| 511 | + $this->maybe_translate( $current_value, 'post_tag' ); |
|
| 512 | + break; |
|
| 513 | + |
|
| 514 | + case 'menus': |
|
| 515 | + case 'menu': |
|
| 516 | + $this->maybe_translate( $current_value, 'nav_menu' ); |
|
| 517 | + break; |
|
| 518 | + |
|
| 519 | + case 'post': |
|
| 520 | + case 'posts': |
|
| 521 | + $this->maybe_translate( $current_value, 'post' ); |
|
| 522 | + break; |
|
| 523 | + default: |
|
| 524 | + $this->maybe_translate( $current_value, '' ); |
|
| 525 | + } |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + /** |
|
| 529 | + * Maybe translate the values. |
|
| 530 | + * |
|
| 531 | + * @param mixed|array $value Value. |
|
| 532 | + * @param mixed|array $post_type Post type. |
|
| 533 | + */ |
|
| 534 | + private function maybe_translate( &$value, $post_type ) { |
|
| 535 | + |
|
| 536 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 537 | + $value = apply_filters( "redux/options/$this->opt_name/wordpress_data/translate/post_type_value", $value, $post_type ); |
|
| 538 | + |
|
| 539 | + // WPML Integration, see https://wpml.org/documentation/support/creating-multilingual-wordpress-themes/language-dependent-ids/. |
|
| 540 | + if ( function_exists( 'icl_object_id' ) ) { |
|
| 541 | + if ( has_filter( 'wpml_object_id' ) ) { |
|
| 542 | + if ( Redux_Helpers::is_integer( $value ) ) { |
|
| 543 | + $value = apply_filters( 'wpml_object_id', $value, $post_type, true ); |
|
| 544 | + } elseif ( is_array( $value ) ) { |
|
| 545 | + $value = array_map( |
|
| 546 | + function ( $val ) use ( $post_type ) { |
|
| 547 | + return apply_filters( 'wpml_object_id', $val, $post_type, true ); |
|
| 548 | + }, |
|
| 549 | + $value |
|
| 550 | + ); |
|
| 551 | + } |
|
| 552 | + } |
|
| 553 | + } |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * Set the default arguments for a current query (existing data). |
|
| 558 | + * |
|
| 559 | + * @param string $type Type of data request. |
|
| 560 | + * @param array|string $args Arguments for the call. |
|
| 561 | + * @param mixed|array $current_value Current value stored in DB. |
|
| 562 | + * |
|
| 563 | + * @return array |
|
| 564 | + */ |
|
| 565 | + private function get_current_data_args( string $type, $args, $current_value ): array { |
|
| 566 | + // In this section, we set the default arguments for each data type. |
|
| 567 | + switch ( $type ) { |
|
| 568 | + case 'categories': |
|
| 569 | + case 'category': |
|
| 570 | + case 'pages': |
|
| 571 | + case 'page': |
|
| 572 | + case 'terms': |
|
| 573 | + case 'term': |
|
| 574 | + case 'users': |
|
| 575 | + case 'user': |
|
| 576 | + $args['include'] = $current_value; |
|
| 577 | + break; |
|
| 578 | + case 'tags': |
|
| 579 | + case 'tag': |
|
| 580 | + $args['get'] = 'all'; |
|
| 581 | + break; |
|
| 582 | + case 'menus': |
|
| 583 | + case 'menu': |
|
| 584 | + $args['object_ids'] = $current_value; |
|
| 585 | + break; |
|
| 586 | + case 'post': |
|
| 587 | + case 'posts': |
|
| 588 | + if ( ! empty( $current_value ) ) { |
|
| 589 | + $args['post__in'] = is_array( $current_value ) ? $current_value : array( $current_value ); |
|
| 590 | + } |
|
| 591 | + break; |
|
| 592 | + |
|
| 593 | + default: |
|
| 594 | + $args = array(); |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + return $args; |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * Get default arguments for a given data type. |
|
| 603 | + * |
|
| 604 | + * @param string $type Type of data request. |
|
| 605 | + * @param array|string $args Arguments for the call. |
|
| 606 | + * |
|
| 607 | + * @return array|string |
|
| 608 | + */ |
|
| 609 | + private function get_arg_defaults( string $type, $args = array() ) { |
|
| 610 | + // In this section, we set the default arguments for each data type. |
|
| 611 | + switch ( $type ) { |
|
| 612 | + case 'categories': |
|
| 613 | + case 'category': |
|
| 614 | + $args = wp_parse_args( |
|
| 615 | + $args, |
|
| 616 | + array( |
|
| 617 | + 'taxonomy' => 'category', |
|
| 618 | + ) |
|
| 619 | + ); |
|
| 620 | + break; |
|
| 621 | + |
|
| 622 | + case 'pages': |
|
| 623 | + case 'page': |
|
| 624 | + $args = wp_parse_args( |
|
| 625 | + $args, |
|
| 626 | + array( |
|
| 627 | + 'display_keys' => true, |
|
| 628 | + 'posts_per_page' => 20, |
|
| 629 | + ) |
|
| 630 | + ); |
|
| 631 | + break; |
|
| 632 | + |
|
| 633 | + case 'post_type': |
|
| 634 | + case 'post_types': |
|
| 635 | + $args = wp_parse_args( |
|
| 636 | + $args, |
|
| 637 | + array( |
|
| 638 | + 'public' => true, |
|
| 639 | + 'exclude_from_search' => false, |
|
| 640 | + 'output' => 'names', |
|
| 641 | + 'operator' => 'and', |
|
| 642 | + ) |
|
| 643 | + ); |
|
| 644 | + |
|
| 645 | + break; |
|
| 646 | + |
|
| 647 | + case 'tag': |
|
| 648 | + case 'tags': |
|
| 649 | + $args = wp_parse_args( |
|
| 650 | + $args, |
|
| 651 | + array( |
|
| 652 | + 'get' => 'all', |
|
| 653 | + 'display_keys' => true, |
|
| 654 | + ) |
|
| 655 | + ); |
|
| 656 | + break; |
|
| 657 | + |
|
| 658 | + case 'sidebars': |
|
| 659 | + case 'sidebar': |
|
| 660 | + case 'capabilities': |
|
| 661 | + case 'capability': |
|
| 662 | + $args = wp_parse_args( |
|
| 663 | + $args, |
|
| 664 | + array( |
|
| 665 | + 'display_keys' => true, |
|
| 666 | + ) |
|
| 667 | + ); |
|
| 668 | + break; |
|
| 669 | + |
|
| 670 | + case 'capabilities_grouped': |
|
| 671 | + case 'capability_grouped': |
|
| 672 | + case 'capabilities_group': |
|
| 673 | + case 'capability_group': |
|
| 674 | + $args = wp_parse_args( |
|
| 675 | + $args, |
|
| 676 | + array( |
|
| 677 | + 'data_sortby' => '', |
|
| 678 | + ) |
|
| 679 | + ); |
|
| 680 | + break; |
|
| 681 | + |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + return $args; |
|
| 685 | + } |
|
| 686 | + } |
|
| 687 | 687 | } |
@@ -13,233 +13,233 @@ |
||
| 13 | 13 | |
| 14 | 14 | if ( ! class_exists( 'Redux_Select', false ) ) { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Class Redux_Select |
|
| 18 | - */ |
|
| 19 | - class Redux_Select extends Redux_Field { |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * Set field defaults. |
|
| 23 | - */ |
|
| 24 | - public function set_defaults() { |
|
| 25 | - $defaults = array( |
|
| 26 | - 'options' => array(), |
|
| 27 | - 'width' => '40%', |
|
| 28 | - 'multi' => false, |
|
| 29 | - 'sortable' => false, |
|
| 30 | - 'ajax' => false, |
|
| 31 | - 'min-input-length' => 1, |
|
| 32 | - 'placeholder' => '', |
|
| 33 | - ); |
|
| 34 | - |
|
| 35 | - $this->field = wp_parse_args( $this->field, $defaults ); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Field Render Function. |
|
| 40 | - * Takes the vars and outputs the HTML for the field in the settings |
|
| 41 | - * |
|
| 42 | - * @since ReduxFramework 1.0.0 |
|
| 43 | - */ |
|
| 44 | - public function render() { |
|
| 45 | - $sortable = ( isset( $this->field['sortable'] ) && true === (bool) $this->field['sortable'] ) ? ' select2-sortable' : ''; |
|
| 46 | - |
|
| 47 | - if ( ! empty( $sortable ) ) { // Dummy proofing :P. |
|
| 48 | - $this->field['multi'] = true; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - if ( ! empty( $this->field['data'] ) && empty( $this->field['options'] ) ) { |
|
| 52 | - if ( empty( $this->field['args'] ) ) { |
|
| 53 | - $this->field['args'] = array(); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - if ( 'elusive-icons' === $this->field['data'] || 'elusive-icon' === $this->field['data'] || 'elusive' === $this->field['data'] ) { |
|
| 57 | - $icons_file = Redux_Core::$dir . 'lib/elusive-icons.php'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Filter 'redux-font-icons-file}' |
|
| 61 | - * |
|
| 62 | - * @param array $icon_file The File for the icons |
|
| 63 | - */ |
|
| 64 | - |
|
| 65 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 66 | - $icons_file = apply_filters( 'redux-font-icons-file', $icons_file ); |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Filter 'redux/{opt_name}/field/font/icons/file' |
|
| 70 | - * |
|
| 71 | - * @param array $icon_file The file for the icons |
|
| 72 | - */ |
|
| 73 | - |
|
| 74 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 75 | - $icons_file = apply_filters( "redux/{$this->parent->args['opt_name']}/field/font/icons/file", $icons_file ); |
|
| 76 | - |
|
| 77 | - if ( file_exists( $icons_file ) ) { |
|
| 78 | - include_once $icons_file; |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - // The First one obtained with AJAX. |
|
| 83 | - $ajax = false; |
|
| 84 | - if ( isset( $this->field['ajax'] ) && $this->field['ajax'] ) { |
|
| 85 | - $ajax = true; |
|
| 86 | - } |
|
| 87 | - $this->field['options'] = $this->parent->wordpress_data->get( $this->field['data'], $this->field['args'], $this->parent->args['opt_name'], $this->value, $ajax ); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - if ( ! empty( $this->field['data'] ) && in_array( $this->field['data'], array( 'elusive-icons', 'elusive-icon', 'elusive', 'dashicons', 'dashicon', 'dash' ), true ) ) { |
|
| 91 | - $this->field['class'] .= ' font-icons'; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - if ( ! empty( $this->field['options'] ) ) { |
|
| 95 | - $multi = ( isset( $this->field['multi'] ) && $this->field['multi'] ) ? ' multiple="multiple"' : ''; |
|
| 96 | - |
|
| 97 | - if ( ! empty( $this->field['width'] ) ) { |
|
| 98 | - $width = ' style="width:' . esc_attr( $this->field['width'] ) . '"'; |
|
| 99 | - } else { |
|
| 100 | - $width = ' style="width:40%;"'; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $name_brackets = ''; |
|
| 104 | - if ( ! empty( $multi ) ) { |
|
| 105 | - $name_brackets = '[]'; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $placeholder = ( isset( $this->field['placeholder'] ) ) ? esc_attr( $this->field['placeholder'] ) : esc_html__( 'Select an item', 'redux-framework' ); |
|
| 109 | - |
|
| 110 | - $select2_width = 'resolve'; |
|
| 111 | - if ( '' !== $multi ) { |
|
| 112 | - $select2_width = '100%'; |
|
| 113 | - } |
|
| 114 | - $this->select2_config['width'] = $select2_width; |
|
| 115 | - $this->select2_config['allowClear'] = true; |
|
| 116 | - |
|
| 117 | - if ( isset( $this->field['ajax'] ) && $this->field['ajax'] && isset( $this->field['data'] ) && '' !== $this->field['data'] ) { |
|
| 118 | - $this->select2_config['ajax'] = true; |
|
| 119 | - $this->select2_config['min-input-length'] = $this->field['min_input_length'] ?? 1; |
|
| 120 | - $this->select2_config['action'] = "redux_{$this->parent->args['opt_name']}_select2"; |
|
| 121 | - if ( isset( $this->field['args'] ) ) { |
|
| 122 | - $this->select2_config['args'] = wp_json_encode( $this->field['args'] ); |
|
| 123 | - } |
|
| 124 | - $this->select2_config['nonce'] = wp_create_nonce( "redux_{$this->parent->args['opt_name']}_select2" ); |
|
| 125 | - $this->select2_config['wp-data'] = $this->field['data']; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - if ( isset( $this->field['select2'] ) ) { |
|
| 129 | - $this->field['select2'] = wp_parse_args( $this->field['select2'], $this->select2_config ); |
|
| 130 | - } else { |
|
| 131 | - $this->field['select2'] = $this->select2_config; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - $this->field['select2'] = Redux_Functions::sanitize_camel_case_array_keys( $this->field['select2'] ); |
|
| 135 | - |
|
| 136 | - $select2_data = Redux_Functions::create_data_string( $this->field['select2'] ); |
|
| 137 | - |
|
| 138 | - if ( isset( $this->field['multi'] ) && $this->field['multi'] && isset( $this->field['sortable'] ) && $this->field['sortable'] && ! empty( $this->value ) && is_array( $this->value ) ) { |
|
| 139 | - $orig_option = $this->field['options']; |
|
| 140 | - $this->field['options'] = array(); |
|
| 141 | - |
|
| 142 | - foreach ( $this->value as $value ) { |
|
| 143 | - $this->field['options'][ $value ] = $orig_option[ $value ]; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - if ( count( $this->field['options'] ) < count( $orig_option ) ) { |
|
| 147 | - foreach ( $orig_option as $key => $value ) { |
|
| 148 | - if ( ! in_array( $key, $this->field['options'], true ) ) { |
|
| 149 | - $this->field['options'][ $key ] = $value; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $sortable = ( isset( $this->field['sortable'] ) && $this->field['sortable'] ) ? ' select2-sortable' : ''; |
|
| 156 | - |
|
| 157 | - echo '<select ' . |
|
| 158 | - esc_html( $multi ) . ' |
|
| 16 | + /** |
|
| 17 | + * Class Redux_Select |
|
| 18 | + */ |
|
| 19 | + class Redux_Select extends Redux_Field { |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Set field defaults. |
|
| 23 | + */ |
|
| 24 | + public function set_defaults() { |
|
| 25 | + $defaults = array( |
|
| 26 | + 'options' => array(), |
|
| 27 | + 'width' => '40%', |
|
| 28 | + 'multi' => false, |
|
| 29 | + 'sortable' => false, |
|
| 30 | + 'ajax' => false, |
|
| 31 | + 'min-input-length' => 1, |
|
| 32 | + 'placeholder' => '', |
|
| 33 | + ); |
|
| 34 | + |
|
| 35 | + $this->field = wp_parse_args( $this->field, $defaults ); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Field Render Function. |
|
| 40 | + * Takes the vars and outputs the HTML for the field in the settings |
|
| 41 | + * |
|
| 42 | + * @since ReduxFramework 1.0.0 |
|
| 43 | + */ |
|
| 44 | + public function render() { |
|
| 45 | + $sortable = ( isset( $this->field['sortable'] ) && true === (bool) $this->field['sortable'] ) ? ' select2-sortable' : ''; |
|
| 46 | + |
|
| 47 | + if ( ! empty( $sortable ) ) { // Dummy proofing :P. |
|
| 48 | + $this->field['multi'] = true; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + if ( ! empty( $this->field['data'] ) && empty( $this->field['options'] ) ) { |
|
| 52 | + if ( empty( $this->field['args'] ) ) { |
|
| 53 | + $this->field['args'] = array(); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + if ( 'elusive-icons' === $this->field['data'] || 'elusive-icon' === $this->field['data'] || 'elusive' === $this->field['data'] ) { |
|
| 57 | + $icons_file = Redux_Core::$dir . 'lib/elusive-icons.php'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Filter 'redux-font-icons-file}' |
|
| 61 | + * |
|
| 62 | + * @param array $icon_file The File for the icons |
|
| 63 | + */ |
|
| 64 | + |
|
| 65 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 66 | + $icons_file = apply_filters( 'redux-font-icons-file', $icons_file ); |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Filter 'redux/{opt_name}/field/font/icons/file' |
|
| 70 | + * |
|
| 71 | + * @param array $icon_file The file for the icons |
|
| 72 | + */ |
|
| 73 | + |
|
| 74 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 75 | + $icons_file = apply_filters( "redux/{$this->parent->args['opt_name']}/field/font/icons/file", $icons_file ); |
|
| 76 | + |
|
| 77 | + if ( file_exists( $icons_file ) ) { |
|
| 78 | + include_once $icons_file; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + // The First one obtained with AJAX. |
|
| 83 | + $ajax = false; |
|
| 84 | + if ( isset( $this->field['ajax'] ) && $this->field['ajax'] ) { |
|
| 85 | + $ajax = true; |
|
| 86 | + } |
|
| 87 | + $this->field['options'] = $this->parent->wordpress_data->get( $this->field['data'], $this->field['args'], $this->parent->args['opt_name'], $this->value, $ajax ); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + if ( ! empty( $this->field['data'] ) && in_array( $this->field['data'], array( 'elusive-icons', 'elusive-icon', 'elusive', 'dashicons', 'dashicon', 'dash' ), true ) ) { |
|
| 91 | + $this->field['class'] .= ' font-icons'; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + if ( ! empty( $this->field['options'] ) ) { |
|
| 95 | + $multi = ( isset( $this->field['multi'] ) && $this->field['multi'] ) ? ' multiple="multiple"' : ''; |
|
| 96 | + |
|
| 97 | + if ( ! empty( $this->field['width'] ) ) { |
|
| 98 | + $width = ' style="width:' . esc_attr( $this->field['width'] ) . '"'; |
|
| 99 | + } else { |
|
| 100 | + $width = ' style="width:40%;"'; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $name_brackets = ''; |
|
| 104 | + if ( ! empty( $multi ) ) { |
|
| 105 | + $name_brackets = '[]'; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $placeholder = ( isset( $this->field['placeholder'] ) ) ? esc_attr( $this->field['placeholder'] ) : esc_html__( 'Select an item', 'redux-framework' ); |
|
| 109 | + |
|
| 110 | + $select2_width = 'resolve'; |
|
| 111 | + if ( '' !== $multi ) { |
|
| 112 | + $select2_width = '100%'; |
|
| 113 | + } |
|
| 114 | + $this->select2_config['width'] = $select2_width; |
|
| 115 | + $this->select2_config['allowClear'] = true; |
|
| 116 | + |
|
| 117 | + if ( isset( $this->field['ajax'] ) && $this->field['ajax'] && isset( $this->field['data'] ) && '' !== $this->field['data'] ) { |
|
| 118 | + $this->select2_config['ajax'] = true; |
|
| 119 | + $this->select2_config['min-input-length'] = $this->field['min_input_length'] ?? 1; |
|
| 120 | + $this->select2_config['action'] = "redux_{$this->parent->args['opt_name']}_select2"; |
|
| 121 | + if ( isset( $this->field['args'] ) ) { |
|
| 122 | + $this->select2_config['args'] = wp_json_encode( $this->field['args'] ); |
|
| 123 | + } |
|
| 124 | + $this->select2_config['nonce'] = wp_create_nonce( "redux_{$this->parent->args['opt_name']}_select2" ); |
|
| 125 | + $this->select2_config['wp-data'] = $this->field['data']; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + if ( isset( $this->field['select2'] ) ) { |
|
| 129 | + $this->field['select2'] = wp_parse_args( $this->field['select2'], $this->select2_config ); |
|
| 130 | + } else { |
|
| 131 | + $this->field['select2'] = $this->select2_config; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + $this->field['select2'] = Redux_Functions::sanitize_camel_case_array_keys( $this->field['select2'] ); |
|
| 135 | + |
|
| 136 | + $select2_data = Redux_Functions::create_data_string( $this->field['select2'] ); |
|
| 137 | + |
|
| 138 | + if ( isset( $this->field['multi'] ) && $this->field['multi'] && isset( $this->field['sortable'] ) && $this->field['sortable'] && ! empty( $this->value ) && is_array( $this->value ) ) { |
|
| 139 | + $orig_option = $this->field['options']; |
|
| 140 | + $this->field['options'] = array(); |
|
| 141 | + |
|
| 142 | + foreach ( $this->value as $value ) { |
|
| 143 | + $this->field['options'][ $value ] = $orig_option[ $value ]; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + if ( count( $this->field['options'] ) < count( $orig_option ) ) { |
|
| 147 | + foreach ( $orig_option as $key => $value ) { |
|
| 148 | + if ( ! in_array( $key, $this->field['options'], true ) ) { |
|
| 149 | + $this->field['options'][ $key ] = $value; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $sortable = ( isset( $this->field['sortable'] ) && $this->field['sortable'] ) ? ' select2-sortable' : ''; |
|
| 156 | + |
|
| 157 | + echo '<select ' . |
|
| 158 | + esc_html( $multi ) . ' |
|
| 159 | 159 | id="' . esc_attr( $this->field['id'] ) . '-select" |
| 160 | 160 | data-placeholder="' . esc_attr( $placeholder ) . '" |
| 161 | 161 | name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] ) . esc_attr( $name_brackets ) . '" |
| 162 | 162 | class="redux-select-item ' . esc_attr( $this->field['class'] ) . esc_attr( $sortable ) . '"' . |
| 163 | - $width . ' rows="6"' . esc_attr( $select2_data ) . '>'; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 164 | - |
|
| 165 | - echo '<option></option>'; |
|
| 166 | - |
|
| 167 | - foreach ( $this->field['options'] as $k => $v ) { |
|
| 168 | - if ( is_array( $v ) ) { |
|
| 169 | - echo '<optgroup label="' . esc_attr( $k ) . '">'; |
|
| 170 | - |
|
| 171 | - foreach ( $v as $opt => $val ) { |
|
| 172 | - $this->make_option( (string) $opt, $val ); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - echo '</optgroup>'; |
|
| 176 | - |
|
| 177 | - continue; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - $this->make_option( (string) $k, $v ); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - echo '</select>'; |
|
| 184 | - } else { |
|
| 185 | - echo '<strong>' . esc_html__( 'No items of this type were found.', 'redux-framework' ) . '</strong>'; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Compile option HTML. |
|
| 191 | - * |
|
| 192 | - * @param string $id HTML ID. |
|
| 193 | - * @param mixed $value Value array. |
|
| 194 | - */ |
|
| 195 | - private function make_option( string $id, $value ) { |
|
| 196 | - if ( is_array( $this->value ) ) { |
|
| 197 | - $selected = ( in_array( $id, $this->value, true ) ) ? ' selected="selected"' : ''; |
|
| 198 | - } else { |
|
| 199 | - $selected = selected( $this->value, $id, false ); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - echo '<option value="' . esc_attr( $id ) . '" ' . esc_html( $selected ) . '>' . esc_attr( $value ) . '</option>'; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Do enqueue for each field instance. |
|
| 207 | - * |
|
| 208 | - * @return void |
|
| 209 | - */ |
|
| 210 | - public function always_enqueue() { |
|
| 211 | - if ( isset( $this->field['sortable'] ) && $this->field['sortable'] ) { |
|
| 212 | - wp_enqueue_script( 'jquery-ui-sortable' ); |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Enqueue Function. |
|
| 218 | - * If this field requires any scripts, or css define this function and register/enqueue the scripts/css |
|
| 219 | - * |
|
| 220 | - * @since ReduxFramework 1.0.0 |
|
| 221 | - */ |
|
| 222 | - public function enqueue() { |
|
| 223 | - wp_enqueue_style( 'select2-css' ); |
|
| 224 | - |
|
| 225 | - wp_enqueue_script( |
|
| 226 | - 'redux-field-select', |
|
| 227 | - Redux_Core::$url . 'inc/fields/select/redux-select' . Redux_Functions::is_min() . '.js', |
|
| 228 | - array( 'jquery', 'select2-js', 'redux-js' ), |
|
| 229 | - $this->timestamp, |
|
| 230 | - true |
|
| 231 | - ); |
|
| 232 | - |
|
| 233 | - if ( $this->parent->args['dev_mode'] ) { |
|
| 234 | - wp_enqueue_style( |
|
| 235 | - 'redux-field-select', |
|
| 236 | - Redux_Core::$url . 'inc/fields/select/redux-select.css', |
|
| 237 | - array(), |
|
| 238 | - $this->timestamp |
|
| 239 | - ); |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - } |
|
| 163 | + $width . ' rows="6"' . esc_attr( $select2_data ) . '>'; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 164 | + |
|
| 165 | + echo '<option></option>'; |
|
| 166 | + |
|
| 167 | + foreach ( $this->field['options'] as $k => $v ) { |
|
| 168 | + if ( is_array( $v ) ) { |
|
| 169 | + echo '<optgroup label="' . esc_attr( $k ) . '">'; |
|
| 170 | + |
|
| 171 | + foreach ( $v as $opt => $val ) { |
|
| 172 | + $this->make_option( (string) $opt, $val ); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + echo '</optgroup>'; |
|
| 176 | + |
|
| 177 | + continue; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + $this->make_option( (string) $k, $v ); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + echo '</select>'; |
|
| 184 | + } else { |
|
| 185 | + echo '<strong>' . esc_html__( 'No items of this type were found.', 'redux-framework' ) . '</strong>'; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Compile option HTML. |
|
| 191 | + * |
|
| 192 | + * @param string $id HTML ID. |
|
| 193 | + * @param mixed $value Value array. |
|
| 194 | + */ |
|
| 195 | + private function make_option( string $id, $value ) { |
|
| 196 | + if ( is_array( $this->value ) ) { |
|
| 197 | + $selected = ( in_array( $id, $this->value, true ) ) ? ' selected="selected"' : ''; |
|
| 198 | + } else { |
|
| 199 | + $selected = selected( $this->value, $id, false ); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + echo '<option value="' . esc_attr( $id ) . '" ' . esc_html( $selected ) . '>' . esc_attr( $value ) . '</option>'; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Do enqueue for each field instance. |
|
| 207 | + * |
|
| 208 | + * @return void |
|
| 209 | + */ |
|
| 210 | + public function always_enqueue() { |
|
| 211 | + if ( isset( $this->field['sortable'] ) && $this->field['sortable'] ) { |
|
| 212 | + wp_enqueue_script( 'jquery-ui-sortable' ); |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Enqueue Function. |
|
| 218 | + * If this field requires any scripts, or css define this function and register/enqueue the scripts/css |
|
| 219 | + * |
|
| 220 | + * @since ReduxFramework 1.0.0 |
|
| 221 | + */ |
|
| 222 | + public function enqueue() { |
|
| 223 | + wp_enqueue_style( 'select2-css' ); |
|
| 224 | + |
|
| 225 | + wp_enqueue_script( |
|
| 226 | + 'redux-field-select', |
|
| 227 | + Redux_Core::$url . 'inc/fields/select/redux-select' . Redux_Functions::is_min() . '.js', |
|
| 228 | + array( 'jquery', 'select2-js', 'redux-js' ), |
|
| 229 | + $this->timestamp, |
|
| 230 | + true |
|
| 231 | + ); |
|
| 232 | + |
|
| 233 | + if ( $this->parent->args['dev_mode'] ) { |
|
| 234 | + wp_enqueue_style( |
|
| 235 | + 'redux-field-select', |
|
| 236 | + Redux_Core::$url . 'inc/fields/select/redux-select.css', |
|
| 237 | + array(), |
|
| 238 | + $this->timestamp |
|
| 239 | + ); |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | class_alias( 'Redux_Select', 'ReduxFramework_Select' ); |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | * @since ReduxFramework 1.0.0 |
| 43 | 43 | */ |
| 44 | 44 | public function render() { |
| 45 | - $sortable = ( isset( $this->field['sortable'] ) && true === (bool) $this->field['sortable'] ) ? ' select2-sortable' : ''; |
|
| 45 | + $sortable = ( isset( $this->field['sortable'] ) && true === ( bool ) $this->field['sortable'] ) ? ' select2-sortable' : ''; |
|
| 46 | 46 | |
| 47 | 47 | if ( ! empty( $sortable ) ) { // Dummy proofing :P. |
| 48 | 48 | $this->field['multi'] = true; |
@@ -140,13 +140,13 @@ discard block |
||
| 140 | 140 | $this->field['options'] = array(); |
| 141 | 141 | |
| 142 | 142 | foreach ( $this->value as $value ) { |
| 143 | - $this->field['options'][ $value ] = $orig_option[ $value ]; |
|
| 143 | + $this->field['options'][$value] = $orig_option[$value]; |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | if ( count( $this->field['options'] ) < count( $orig_option ) ) { |
| 147 | 147 | foreach ( $orig_option as $key => $value ) { |
| 148 | 148 | if ( ! in_array( $key, $this->field['options'], true ) ) { |
| 149 | - $this->field['options'][ $key ] = $value; |
|
| 149 | + $this->field['options'][$key] = $value; |
|
| 150 | 150 | } |
| 151 | 151 | } |
| 152 | 152 | } |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | echo '<optgroup label="' . esc_attr( $k ) . '">'; |
| 170 | 170 | |
| 171 | 171 | foreach ( $v as $opt => $val ) { |
| 172 | - $this->make_option( (string) $opt, $val ); |
|
| 172 | + $this->make_option( ( string ) $opt, $val ); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | echo '</optgroup>'; |
@@ -177,7 +177,7 @@ discard block |
||
| 177 | 177 | continue; |
| 178 | 178 | } |
| 179 | 179 | |
| 180 | - $this->make_option( (string) $k, $v ); |
|
| 180 | + $this->make_option( ( string ) $k, $v ); |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | echo '</select>'; |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | $instances = Redux::all_instances(); |
| 93 | 93 | |
| 94 | 94 | foreach ( self::$fields as $opt_name => $fields ) { |
| 95 | - if ( ! isset( $instances[ $opt_name ] ) ) { |
|
| 95 | + if ( ! isset( $instances[$opt_name] ) ) { |
|
| 96 | 96 | Redux::set_args( $opt_name, array( 'menu_type' => 'hidden' ) ); |
| 97 | 97 | |
| 98 | 98 | Redux::set_sections( |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | |
| 114 | 114 | self::check_opt_name( $opt_name ); |
| 115 | 115 | |
| 116 | - Redux::set_args( $opt_name, self::$args[ $opt_name ] ); |
|
| 116 | + Redux::set_args( $opt_name, self::$args[$opt_name] ); |
|
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | 119 | } |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | * @return mixed |
| 127 | 127 | */ |
| 128 | 128 | public static function construct_args( string $opt_name ) { |
| 129 | - $args = self::$args[ $opt_name ]; |
|
| 129 | + $args = self::$args[$opt_name]; |
|
| 130 | 130 | $args['opt_name'] = $opt_name; |
| 131 | 131 | |
| 132 | 132 | if ( ! isset( $args['menu_title'] ) ) { |
@@ -154,11 +154,11 @@ discard block |
||
| 154 | 154 | public static function construct_profiles( string $opt_name ): array { |
| 155 | 155 | $profiles = array(); |
| 156 | 156 | |
| 157 | - if ( ! isset( self::$profiles[ $opt_name ] ) ) { |
|
| 157 | + if ( ! isset( self::$profiles[$opt_name] ) ) { |
|
| 158 | 158 | return $profiles; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - foreach ( self::$profiles[ $opt_name ] as $profile ) { |
|
| 161 | + foreach ( self::$profiles[$opt_name] as $profile ) { |
|
| 162 | 162 | $profile['sections'] = self::construct_sections( $opt_name, $profile['id'] ); |
| 163 | 163 | $profiles[] = $profile; |
| 164 | 164 | } |
@@ -179,22 +179,22 @@ discard block |
||
| 179 | 179 | public static function construct_sections( string $opt_name, $profile_id ): array { |
| 180 | 180 | $sections = array(); |
| 181 | 181 | |
| 182 | - if ( ! isset( self::$sections[ $opt_name ] ) ) { |
|
| 182 | + if ( ! isset( self::$sections[$opt_name] ) ) { |
|
| 183 | 183 | return $sections; |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | - foreach ( self::$sections[ $opt_name ] as $section_id => $section ) { |
|
| 186 | + foreach ( self::$sections[$opt_name] as $section_id => $section ) { |
|
| 187 | 187 | if ( $section['profile_id'] === $profile_id ) { |
| 188 | - self::$sections[ $opt_name ][ $section_id ]['roles'] = $section; |
|
| 188 | + self::$sections[$opt_name][$section_id]['roles'] = $section; |
|
| 189 | 189 | |
| 190 | 190 | $p = $section['priority']; |
| 191 | 191 | |
| 192 | - while ( isset( $sections[ $p ] ) ) { |
|
| 192 | + while ( isset( $sections[$p] ) ) { |
|
| 193 | 193 | echo esc_html( $p++ ); |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | $section['fields'] = self::construct_fields( $opt_name, $section_id ); |
| 197 | - $sections[ $p ] = $section; |
|
| 197 | + $sections[$p] = $section; |
|
| 198 | 198 | } |
| 199 | 199 | } |
| 200 | 200 | |
@@ -214,29 +214,29 @@ discard block |
||
| 214 | 214 | public static function construct_fields( string $opt_name = '', string $section_id = '', bool $permissions = false, bool $roles = false ): array { |
| 215 | 215 | $fields = array(); |
| 216 | 216 | |
| 217 | - if ( ! isset( self::$fields[ $opt_name ] ) ) { |
|
| 217 | + if ( ! isset( self::$fields[$opt_name] ) ) { |
|
| 218 | 218 | return $fields; |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - foreach ( self::$fields[ $opt_name ] as $key => $field ) { |
|
| 221 | + foreach ( self::$fields[$opt_name] as $key => $field ) { |
|
| 222 | 222 | // Nested permissions. |
| 223 | 223 | $field['permissions'] = $field['permissions'] ?? $permissions; |
| 224 | 224 | |
| 225 | - self::$fields[ $opt_name ][ $key ]['permissions'] = $field['permissions']; |
|
| 225 | + self::$fields[$opt_name][$key]['permissions'] = $field['permissions']; |
|
| 226 | 226 | |
| 227 | 227 | // Nested roles permissions. |
| 228 | 228 | $field['roles'] = $field['roles'] ?? $roles; |
| 229 | 229 | |
| 230 | - self::$fields[ $opt_name ][ $key ]['roles'] = $field['roles']; |
|
| 230 | + self::$fields[$opt_name][$key]['roles'] = $field['roles']; |
|
| 231 | 231 | |
| 232 | 232 | if ( $field['section_id'] === $section_id ) { |
| 233 | 233 | $p = $field['priority']; |
| 234 | 234 | |
| 235 | - while ( isset( $fields[ $p ] ) ) { |
|
| 235 | + while ( isset( $fields[$p] ) ) { |
|
| 236 | 236 | echo esc_html( $p++ ); |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | - $fields[ $p ] = $field; |
|
| 239 | + $fields[$p] = $field; |
|
| 240 | 240 | } |
| 241 | 241 | } |
| 242 | 242 | |
@@ -257,11 +257,11 @@ discard block |
||
| 257 | 257 | self::check_opt_name( $opt_name ); |
| 258 | 258 | |
| 259 | 259 | if ( ! empty( $opt_name ) && ! empty( $id ) ) { |
| 260 | - if ( ! isset( self::$sections[ $opt_name ][ $id ] ) ) { |
|
| 260 | + if ( ! isset( self::$sections[$opt_name][$id] ) ) { |
|
| 261 | 261 | $id = strtolower( sanitize_html_class( $id ) ); |
| 262 | 262 | } |
| 263 | 263 | |
| 264 | - return self::$sections[ $opt_name ][ $id ] ?? false; |
|
| 264 | + return self::$sections[$opt_name][$id] ?? false; |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | return false; |
@@ -277,8 +277,8 @@ discard block |
||
| 277 | 277 | self::check_opt_name( $opt_name ); |
| 278 | 278 | |
| 279 | 279 | if ( ! empty( $opt_name ) && is_array( $args ) && ! empty( $args ) ) { |
| 280 | - self::$args[ $opt_name ] = self::$args[ $opt_name ] ?? array(); |
|
| 281 | - self::$args[ $opt_name ] = wp_parse_args( $args, self::$args[ $opt_name ] ); |
|
| 280 | + self::$args[$opt_name] = self::$args[$opt_name] ?? array(); |
|
| 281 | + self::$args[$opt_name] = wp_parse_args( $args, self::$args[$opt_name] ); |
|
| 282 | 282 | } |
| 283 | 283 | } |
| 284 | 284 | |
@@ -299,11 +299,11 @@ discard block |
||
| 299 | 299 | $section['id'] = 'section'; |
| 300 | 300 | } |
| 301 | 301 | |
| 302 | - if ( isset( self::$sections[ $opt_name ][ $section['id'] ] ) ) { |
|
| 302 | + if ( isset( self::$sections[$opt_name][$section['id']] ) ) { |
|
| 303 | 303 | $orig = $section['id']; |
| 304 | 304 | $i = 0; |
| 305 | 305 | |
| 306 | - while ( isset( self::$sections[ $opt_name ][ $section['id'] ] ) ) { |
|
| 306 | + while ( isset( self::$sections[$opt_name][$section['id']] ) ) { |
|
| 307 | 307 | $section['id'] = $orig . '_' . $i; |
| 308 | 308 | } |
| 309 | 309 | } |
@@ -318,11 +318,11 @@ discard block |
||
| 318 | 318 | if ( isset( $section['permissions'] ) || isset( $section['roles'] ) ) { |
| 319 | 319 | foreach ( $section['fields'] as $key => $field ) { |
| 320 | 320 | if ( ! isset( $field['permissions'] ) && isset( $section['permissions'] ) ) { |
| 321 | - $section['fields'][ $key ]['permissions'] = $section['permissions']; |
|
| 321 | + $section['fields'][$key]['permissions'] = $section['permissions']; |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | if ( ! isset( $field['roles'] ) && isset( $section['roles'] ) ) { |
| 325 | - $section['fields'][ $key ]['roles'] = $section['roles']; |
|
| 325 | + $section['fields'][$key]['roles'] = $section['roles']; |
|
| 326 | 326 | } |
| 327 | 327 | } |
| 328 | 328 | } |
@@ -333,9 +333,9 @@ discard block |
||
| 333 | 333 | unset( $section['fields'] ); |
| 334 | 334 | } |
| 335 | 335 | |
| 336 | - self::$sections[ $opt_name ][ $section['id'] ] = $section; |
|
| 336 | + self::$sections[$opt_name][$section['id']] = $section; |
|
| 337 | 337 | } else { |
| 338 | - self::$errors[ $opt_name ]['section']['empty'] = esc_html__( 'Unable to create a section due an empty section array or the section variable passed was not an array.', 'redux-framework' ); |
|
| 338 | + self::$errors[$opt_name]['section']['empty'] = esc_html__( 'Unable to create a section due an empty section array or the section variable passed was not an array.', 'redux-framework' ); |
|
| 339 | 339 | } |
| 340 | 340 | } |
| 341 | 341 | |
@@ -397,7 +397,7 @@ discard block |
||
| 397 | 397 | self::check_opt_name( $opt_name ); |
| 398 | 398 | |
| 399 | 399 | if ( ! empty( $opt_name ) && ! empty( $id ) ) { |
| 400 | - return self::$fields[ $opt_name ][ $id ] ?? false; |
|
| 400 | + return self::$fields[$opt_name][$id] ?? false; |
|
| 401 | 401 | } |
| 402 | 402 | |
| 403 | 403 | return false; |
@@ -417,7 +417,7 @@ discard block |
||
| 417 | 417 | $field['priority'] = self::get_priority( $opt_name, 'fields' ); |
| 418 | 418 | } |
| 419 | 419 | |
| 420 | - self::$fields[ $opt_name ][ $field['id'] ] = $field; |
|
| 420 | + self::$fields[$opt_name][$field['id']] = $field; |
|
| 421 | 421 | } |
| 422 | 422 | } |
| 423 | 423 | |
@@ -438,11 +438,11 @@ discard block |
||
| 438 | 438 | $profile['id'] = 'profile'; |
| 439 | 439 | } |
| 440 | 440 | |
| 441 | - if ( isset( self::$profiles[ $opt_name ][ $profile['id'] ] ) ) { |
|
| 441 | + if ( isset( self::$profiles[$opt_name][$profile['id']] ) ) { |
|
| 442 | 442 | $orig = $profile['id']; |
| 443 | 443 | $i = 0; |
| 444 | 444 | |
| 445 | - while ( isset( self::$profiles[ $opt_name ][ $profile['id'] ] ) ) { |
|
| 445 | + while ( isset( self::$profiles[$opt_name][$profile['id']] ) ) { |
|
| 446 | 446 | $profile['id'] = $orig . '_' . $i; |
| 447 | 447 | } |
| 448 | 448 | } |
@@ -453,11 +453,11 @@ discard block |
||
| 453 | 453 | if ( isset( $profile['permissions'] ) || isset( $profile['roles'] ) ) { |
| 454 | 454 | foreach ( $profile['sections'] as $key => $section ) { |
| 455 | 455 | if ( ! isset( $section['permissions'] ) && isset( $profile['permissions'] ) ) { |
| 456 | - $profile['sections'][ $key ]['permissions'] = $profile['permissions']; |
|
| 456 | + $profile['sections'][$key]['permissions'] = $profile['permissions']; |
|
| 457 | 457 | } |
| 458 | 458 | |
| 459 | 459 | if ( ! isset( $section['roles'] ) && isset( $profile['roles'] ) ) { |
| 460 | - $profile['sections'][ $key ]['roles'] = $profile['roles']; |
|
| 460 | + $profile['sections'][$key]['roles'] = $profile['roles']; |
|
| 461 | 461 | } |
| 462 | 462 | } |
| 463 | 463 | } |
@@ -468,9 +468,9 @@ discard block |
||
| 468 | 468 | unset( $profile['sections'] ); |
| 469 | 469 | } |
| 470 | 470 | |
| 471 | - self::$profiles[ $opt_name ][ $profile['id'] ] = $profile; |
|
| 471 | + self::$profiles[$opt_name][$profile['id']] = $profile; |
|
| 472 | 472 | } else { |
| 473 | - self::$errors[ $opt_name ]['profile']['empty'] = esc_html__( 'Unable to create a profile due an empty profile array or the profile variable passed was not an array.', 'redux-framework' ); |
|
| 473 | + self::$errors[$opt_name]['profile']['empty'] = esc_html__( 'Unable to create a profile due an empty profile array or the profile variable passed was not an array.', 'redux-framework' ); |
|
| 474 | 474 | } |
| 475 | 475 | } |
| 476 | 476 | |
@@ -483,8 +483,8 @@ discard block |
||
| 483 | 483 | * @return mixed |
| 484 | 484 | */ |
| 485 | 485 | public static function get_priority( string $opt_name, $type ) { |
| 486 | - $priority = self::$priority[ $opt_name ][ $type ]; |
|
| 487 | - self::$priority[ $opt_name ][ $type ] += 1; |
|
| 486 | + $priority = self::$priority[$opt_name][$type]; |
|
| 487 | + self::$priority[$opt_name][$type] += 1; |
|
| 488 | 488 | |
| 489 | 489 | return $priority; |
| 490 | 490 | } |
@@ -499,34 +499,34 @@ discard block |
||
| 499 | 499 | return; |
| 500 | 500 | } |
| 501 | 501 | |
| 502 | - if ( ! isset( self::$profiles[ $opt_name ] ) ) { |
|
| 503 | - self::$profiles[ $opt_name ] = array(); |
|
| 502 | + if ( ! isset( self::$profiles[$opt_name] ) ) { |
|
| 503 | + self::$profiles[$opt_name] = array(); |
|
| 504 | 504 | } |
| 505 | 505 | |
| 506 | - if ( ! isset( self::$priority[ $opt_name ] ) ) { |
|
| 507 | - self::$priority[ $opt_name ]['args'] = 1; |
|
| 506 | + if ( ! isset( self::$priority[$opt_name] ) ) { |
|
| 507 | + self::$priority[$opt_name]['args'] = 1; |
|
| 508 | 508 | } |
| 509 | 509 | |
| 510 | - if ( ! isset( self::$sections[ $opt_name ] ) ) { |
|
| 511 | - self::$sections[ $opt_name ] = array(); |
|
| 512 | - self::$priority[ $opt_name ]['sections'] = 1; |
|
| 510 | + if ( ! isset( self::$sections[$opt_name] ) ) { |
|
| 511 | + self::$sections[$opt_name] = array(); |
|
| 512 | + self::$priority[$opt_name]['sections'] = 1; |
|
| 513 | 513 | } |
| 514 | 514 | |
| 515 | - if ( ! isset( self::$fields[ $opt_name ] ) ) { |
|
| 516 | - self::$fields[ $opt_name ] = array(); |
|
| 517 | - self::$priority[ $opt_name ]['fields'] = 1; |
|
| 515 | + if ( ! isset( self::$fields[$opt_name] ) ) { |
|
| 516 | + self::$fields[$opt_name] = array(); |
|
| 517 | + self::$priority[$opt_name]['fields'] = 1; |
|
| 518 | 518 | } |
| 519 | 519 | |
| 520 | - if ( ! isset( self::$errors[ $opt_name ] ) ) { |
|
| 521 | - self::$errors[ $opt_name ] = array(); |
|
| 520 | + if ( ! isset( self::$errors[$opt_name] ) ) { |
|
| 521 | + self::$errors[$opt_name] = array(); |
|
| 522 | 522 | } |
| 523 | 523 | |
| 524 | - if ( ! isset( self::$init[ $opt_name ] ) ) { |
|
| 525 | - self::$init[ $opt_name ] = false; |
|
| 524 | + if ( ! isset( self::$init[$opt_name] ) ) { |
|
| 525 | + self::$init[$opt_name] = false; |
|
| 526 | 526 | } |
| 527 | 527 | |
| 528 | - if ( ! isset( self::$args[ $opt_name ] ) ) { |
|
| 529 | - self::$args[ $opt_name ] = false; |
|
| 528 | + if ( ! isset( self::$args[$opt_name] ) ) { |
|
| 529 | + self::$args[$opt_name] = false; |
|
| 530 | 530 | } |
| 531 | 531 | } |
| 532 | 532 | |
@@ -565,7 +565,7 @@ discard block |
||
| 565 | 565 | $value = $value[0]; |
| 566 | 566 | } |
| 567 | 567 | |
| 568 | - $meta[ $key ] = maybe_unserialize( $value ); |
|
| 568 | + $meta[$key] = maybe_unserialize( $value ); |
|
| 569 | 569 | } |
| 570 | 570 | } |
| 571 | 571 | |
@@ -13,565 +13,565 @@ |
||
| 13 | 13 | // Don't duplicate me! |
| 14 | 14 | if ( ! class_exists( 'Redux_Users' ) ) { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Redux Users API Class |
|
| 18 | - * Simple API for Redux Framework |
|
| 19 | - * |
|
| 20 | - * @since 1.0.0 |
|
| 21 | - */ |
|
| 22 | - class Redux_Users { |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Profile array. |
|
| 26 | - * |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - public static array $profiles = array(); |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Sections array. |
|
| 33 | - * |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - public static array $sections = array(); |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Fields array. |
|
| 40 | - * |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - public static array $fields = array(); |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Priority array. |
|
| 47 | - * |
|
| 48 | - * @var array |
|
| 49 | - */ |
|
| 50 | - public static array $priority = array(); |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Errors array. |
|
| 54 | - * |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - public static array $errors = array(); |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Init array. |
|
| 61 | - * |
|
| 62 | - * @var array |
|
| 63 | - */ |
|
| 64 | - public static array $init = array(); |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Args array. |
|
| 68 | - * |
|
| 69 | - * @var array |
|
| 70 | - */ |
|
| 71 | - public static array $args = array(); |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Load. |
|
| 75 | - */ |
|
| 76 | - public static function load() { |
|
| 77 | - add_action( 'init', array( 'Redux_Users', 'enqueue' ), 99 ); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Enqueue support files and fields. |
|
| 82 | - * |
|
| 83 | - * @throws ReflectionException Exception. |
|
| 84 | - */ |
|
| 85 | - public static function enqueue() { |
|
| 86 | - global $pagenow; |
|
| 87 | - |
|
| 88 | - // Check and run instances of Redux where the opt_name hasn't been run. |
|
| 89 | - $pagenows = array( 'user-new.php', 'profile.php', 'user-edit.php' ); |
|
| 90 | - |
|
| 91 | - if ( ! empty( self::$sections ) && in_array( $pagenow, $pagenows, true ) ) { |
|
| 92 | - $instances = Redux::all_instances(); |
|
| 93 | - |
|
| 94 | - foreach ( self::$fields as $opt_name => $fields ) { |
|
| 95 | - if ( ! isset( $instances[ $opt_name ] ) ) { |
|
| 96 | - Redux::set_args( $opt_name, array( 'menu_type' => 'hidden' ) ); |
|
| 97 | - |
|
| 98 | - Redux::set_sections( |
|
| 99 | - $opt_name, |
|
| 100 | - array( |
|
| 101 | - array( |
|
| 102 | - 'id' => 'EXTENSION_USERS_FAKE_ID' . $opt_name, |
|
| 103 | - 'fields' => $fields, |
|
| 104 | - 'title' => 'N/A', |
|
| 105 | - ), |
|
| 106 | - ) |
|
| 107 | - ); |
|
| 108 | - |
|
| 109 | - Redux::init( $opt_name ); |
|
| 110 | - |
|
| 111 | - $instances = ReduxFrameworkInstances::get_all_instances(); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - self::check_opt_name( $opt_name ); |
|
| 115 | - |
|
| 116 | - Redux::set_args( $opt_name, self::$args[ $opt_name ] ); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Construct Args. |
|
| 123 | - * |
|
| 124 | - * @param string $opt_name Panel Opt Name. |
|
| 125 | - * |
|
| 126 | - * @return mixed |
|
| 127 | - */ |
|
| 128 | - public static function construct_args( string $opt_name ) { |
|
| 129 | - $args = self::$args[ $opt_name ]; |
|
| 130 | - $args['opt_name'] = $opt_name; |
|
| 131 | - |
|
| 132 | - if ( ! isset( $args['menu_title'] ) ) { |
|
| 133 | - $args['menu_title'] = ucfirst( $opt_name ) . ' Options'; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - if ( ! isset( $args['page_title'] ) ) { |
|
| 137 | - $args['page_title'] = ucfirst( $opt_name ) . ' Options'; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - if ( ! isset( $args['page_slug'] ) ) { |
|
| 141 | - $args['page_slug'] = $opt_name . '_options'; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - return $args; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Construct Profiles. |
|
| 149 | - * |
|
| 150 | - * @param string $opt_name Panel opt_name. |
|
| 151 | - * |
|
| 152 | - * @return array |
|
| 153 | - */ |
|
| 154 | - public static function construct_profiles( string $opt_name ): array { |
|
| 155 | - $profiles = array(); |
|
| 156 | - |
|
| 157 | - if ( ! isset( self::$profiles[ $opt_name ] ) ) { |
|
| 158 | - return $profiles; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - foreach ( self::$profiles[ $opt_name ] as $profile ) { |
|
| 162 | - $profile['sections'] = self::construct_sections( $opt_name, $profile['id'] ); |
|
| 163 | - $profiles[] = $profile; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - ksort( $profiles ); |
|
| 167 | - |
|
| 168 | - return $profiles; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Construct Sections. |
|
| 173 | - * |
|
| 174 | - * @param string $opt_name Panel opt_name. |
|
| 175 | - * @param int|string $profile_id Profile ID. |
|
| 176 | - * |
|
| 177 | - * @return array |
|
| 178 | - */ |
|
| 179 | - public static function construct_sections( string $opt_name, $profile_id ): array { |
|
| 180 | - $sections = array(); |
|
| 181 | - |
|
| 182 | - if ( ! isset( self::$sections[ $opt_name ] ) ) { |
|
| 183 | - return $sections; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - foreach ( self::$sections[ $opt_name ] as $section_id => $section ) { |
|
| 187 | - if ( $section['profile_id'] === $profile_id ) { |
|
| 188 | - self::$sections[ $opt_name ][ $section_id ]['roles'] = $section; |
|
| 189 | - |
|
| 190 | - $p = $section['priority']; |
|
| 191 | - |
|
| 192 | - while ( isset( $sections[ $p ] ) ) { |
|
| 193 | - echo esc_html( $p++ ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - $section['fields'] = self::construct_fields( $opt_name, $section_id ); |
|
| 197 | - $sections[ $p ] = $section; |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - return $sections; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Construct Fields. |
|
| 206 | - * |
|
| 207 | - * @param string $opt_name Panel opt_name. |
|
| 208 | - * @param string $section_id Section ID. |
|
| 209 | - * @param bool $permissions Permissions. |
|
| 210 | - * @param bool $roles Roles. |
|
| 211 | - * |
|
| 212 | - * @return array |
|
| 213 | - */ |
|
| 214 | - public static function construct_fields( string $opt_name = '', string $section_id = '', bool $permissions = false, bool $roles = false ): array { |
|
| 215 | - $fields = array(); |
|
| 216 | - |
|
| 217 | - if ( ! isset( self::$fields[ $opt_name ] ) ) { |
|
| 218 | - return $fields; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - foreach ( self::$fields[ $opt_name ] as $key => $field ) { |
|
| 222 | - // Nested permissions. |
|
| 223 | - $field['permissions'] = $field['permissions'] ?? $permissions; |
|
| 224 | - |
|
| 225 | - self::$fields[ $opt_name ][ $key ]['permissions'] = $field['permissions']; |
|
| 226 | - |
|
| 227 | - // Nested roles permissions. |
|
| 228 | - $field['roles'] = $field['roles'] ?? $roles; |
|
| 229 | - |
|
| 230 | - self::$fields[ $opt_name ][ $key ]['roles'] = $field['roles']; |
|
| 231 | - |
|
| 232 | - if ( $field['section_id'] === $section_id ) { |
|
| 233 | - $p = $field['priority']; |
|
| 234 | - |
|
| 235 | - while ( isset( $fields[ $p ] ) ) { |
|
| 236 | - echo esc_html( $p++ ); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - $fields[ $p ] = $field; |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - ksort( $fields ); |
|
| 244 | - |
|
| 245 | - return $fields; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Get Section. |
|
| 250 | - * |
|
| 251 | - * @param string $opt_name Panel opt_name. |
|
| 252 | - * @param string $id ID. |
|
| 253 | - * |
|
| 254 | - * @return bool |
|
| 255 | - */ |
|
| 256 | - public static function get_section( string $opt_name = '', string $id = '' ): bool { |
|
| 257 | - self::check_opt_name( $opt_name ); |
|
| 258 | - |
|
| 259 | - if ( ! empty( $opt_name ) && ! empty( $id ) ) { |
|
| 260 | - if ( ! isset( self::$sections[ $opt_name ][ $id ] ) ) { |
|
| 261 | - $id = strtolower( sanitize_html_class( $id ) ); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - return self::$sections[ $opt_name ][ $id ] ?? false; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - return false; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Set args. |
|
| 272 | - * |
|
| 273 | - * @param string $opt_name Panel opt_name. |
|
| 274 | - * @param array $args Args array. |
|
| 275 | - */ |
|
| 276 | - public static function set_args( string $opt_name = '', array $args = array() ) { |
|
| 277 | - self::check_opt_name( $opt_name ); |
|
| 278 | - |
|
| 279 | - if ( ! empty( $opt_name ) && is_array( $args ) && ! empty( $args ) ) { |
|
| 280 | - self::$args[ $opt_name ] = self::$args[ $opt_name ] ?? array(); |
|
| 281 | - self::$args[ $opt_name ] = wp_parse_args( $args, self::$args[ $opt_name ] ); |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Set Section. |
|
| 287 | - * |
|
| 288 | - * @param string $opt_name Panel opt_name. |
|
| 289 | - * @param array $section Section array. |
|
| 290 | - */ |
|
| 291 | - public static function set_section( string $opt_name = '', array $section = array() ) { |
|
| 292 | - self::check_opt_name( $opt_name ); |
|
| 293 | - |
|
| 294 | - if ( ! empty( $opt_name ) && is_array( $section ) && ! empty( $section ) ) { |
|
| 295 | - if ( ! isset( $section['id'] ) ) { |
|
| 296 | - if ( isset( $section['title'] ) ) { |
|
| 297 | - $section['id'] = strtolower( sanitize_html_class( $section['title'] ) ); |
|
| 298 | - } else { |
|
| 299 | - $section['id'] = 'section'; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - if ( isset( self::$sections[ $opt_name ][ $section['id'] ] ) ) { |
|
| 303 | - $orig = $section['id']; |
|
| 304 | - $i = 0; |
|
| 305 | - |
|
| 306 | - while ( isset( self::$sections[ $opt_name ][ $section['id'] ] ) ) { |
|
| 307 | - $section['id'] = $orig . '_' . $i; |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - if ( ! isset( $section['priority'] ) ) { |
|
| 313 | - $section['priority'] = self::get_priority( $opt_name, 'sections' ); |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - if ( isset( $section['fields'] ) ) { |
|
| 317 | - if ( ! empty( $section['fields'] ) && is_array( $section['fields'] ) ) { |
|
| 318 | - if ( isset( $section['permissions'] ) || isset( $section['roles'] ) ) { |
|
| 319 | - foreach ( $section['fields'] as $key => $field ) { |
|
| 320 | - if ( ! isset( $field['permissions'] ) && isset( $section['permissions'] ) ) { |
|
| 321 | - $section['fields'][ $key ]['permissions'] = $section['permissions']; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - if ( ! isset( $field['roles'] ) && isset( $section['roles'] ) ) { |
|
| 325 | - $section['fields'][ $key ]['roles'] = $section['roles']; |
|
| 326 | - } |
|
| 327 | - } |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - self::process_fields_array( $opt_name, $section['id'], $section['fields'] ); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - unset( $section['fields'] ); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - self::$sections[ $opt_name ][ $section['id'] ] = $section; |
|
| 337 | - } else { |
|
| 338 | - self::$errors[ $opt_name ]['section']['empty'] = esc_html__( 'Unable to create a section due an empty section array or the section variable passed was not an array.', 'redux-framework' ); |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Process Section Array. |
|
| 344 | - * |
|
| 345 | - * @param string $opt_name Panel opt_name. |
|
| 346 | - * @param string $profile_id Profile ID. |
|
| 347 | - * @param array $sections Sections array. |
|
| 348 | - */ |
|
| 349 | - public static function process_sections_array( string $opt_name = '', string $profile_id = '', array $sections = array() ) { |
|
| 350 | - if ( ! empty( $opt_name ) && ! empty( $profile_id ) && is_array( $sections ) && ! empty( $sections ) ) { |
|
| 351 | - foreach ( $sections as $section ) { |
|
| 352 | - if ( ! is_array( $section ) ) { |
|
| 353 | - continue; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - $section['profile_id'] = $profile_id; |
|
| 357 | - |
|
| 358 | - if ( ! isset( $section['fields'] ) || ! is_array( $section['fields'] ) ) { |
|
| 359 | - $section['fields'] = array(); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - self::set_section( $opt_name, $section ); |
|
| 363 | - } |
|
| 364 | - } |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Process Fields Array. |
|
| 369 | - * |
|
| 370 | - * @param string $opt_name Panel opt_name. |
|
| 371 | - * @param string $section_id Section ID. |
|
| 372 | - * @param array $fields Fields array. |
|
| 373 | - */ |
|
| 374 | - public static function process_fields_array( string $opt_name = '', string $section_id = '', array $fields = array() ) { |
|
| 375 | - if ( ! empty( $opt_name ) && ! empty( $section_id ) && is_array( $fields ) && ! empty( $fields ) ) { |
|
| 376 | - foreach ( $fields as $field ) { |
|
| 377 | - if ( ! is_array( $field ) ) { |
|
| 378 | - continue; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - $field['section_id'] = $section_id; |
|
| 382 | - |
|
| 383 | - self::set_field( $opt_name, $field ); |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Get field. |
|
| 390 | - * |
|
| 391 | - * @param string $opt_name Panel opt_name. |
|
| 392 | - * @param string $id Field ID. |
|
| 393 | - * |
|
| 394 | - * @return bool |
|
| 395 | - */ |
|
| 396 | - public static function get_field( string $opt_name = '', string $id = '' ): bool { |
|
| 397 | - self::check_opt_name( $opt_name ); |
|
| 398 | - |
|
| 399 | - if ( ! empty( $opt_name ) && ! empty( $id ) ) { |
|
| 400 | - return self::$fields[ $opt_name ][ $id ] ?? false; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - return false; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Set field. |
|
| 408 | - * |
|
| 409 | - * @param string $opt_name Panel opt_name. |
|
| 410 | - * @param array $field Field array. |
|
| 411 | - */ |
|
| 412 | - public static function set_field( string $opt_name = '', array $field = array() ) { |
|
| 413 | - self::check_opt_name( $opt_name ); |
|
| 414 | - |
|
| 415 | - if ( ! empty( $opt_name ) && is_array( $field ) && ! empty( $field ) ) { |
|
| 416 | - if ( ! isset( $field['priority'] ) ) { |
|
| 417 | - $field['priority'] = self::get_priority( $opt_name, 'fields' ); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - self::$fields[ $opt_name ][ $field['id'] ] = $field; |
|
| 421 | - } |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * Set Profile. |
|
| 426 | - * |
|
| 427 | - * @param string $opt_name Panel opt_name. |
|
| 428 | - * @param array $profile Profile array. |
|
| 429 | - */ |
|
| 430 | - public static function set_profile( string $opt_name = '', array $profile = array() ) { |
|
| 431 | - self::check_opt_name( $opt_name ); |
|
| 432 | - |
|
| 433 | - if ( ! empty( $opt_name ) && is_array( $profile ) && ! empty( $profile ) ) { |
|
| 434 | - if ( ! isset( $profile['id'] ) ) { |
|
| 435 | - if ( isset( $profile['title'] ) ) { |
|
| 436 | - $profile['id'] = strtolower( sanitize_html_class( $profile['title'] ) ); |
|
| 437 | - } else { |
|
| 438 | - $profile['id'] = 'profile'; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - if ( isset( self::$profiles[ $opt_name ][ $profile['id'] ] ) ) { |
|
| 442 | - $orig = $profile['id']; |
|
| 443 | - $i = 0; |
|
| 444 | - |
|
| 445 | - while ( isset( self::$profiles[ $opt_name ][ $profile['id'] ] ) ) { |
|
| 446 | - $profile['id'] = $orig . '_' . $i; |
|
| 447 | - } |
|
| 448 | - } |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - if ( isset( $profile['sections'] ) ) { |
|
| 452 | - if ( ! empty( $profile['sections'] ) && is_array( $profile['sections'] ) ) { |
|
| 453 | - if ( isset( $profile['permissions'] ) || isset( $profile['roles'] ) ) { |
|
| 454 | - foreach ( $profile['sections'] as $key => $section ) { |
|
| 455 | - if ( ! isset( $section['permissions'] ) && isset( $profile['permissions'] ) ) { |
|
| 456 | - $profile['sections'][ $key ]['permissions'] = $profile['permissions']; |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - if ( ! isset( $section['roles'] ) && isset( $profile['roles'] ) ) { |
|
| 460 | - $profile['sections'][ $key ]['roles'] = $profile['roles']; |
|
| 461 | - } |
|
| 462 | - } |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - self::process_sections_array( $opt_name, $profile['id'], $profile['sections'] ); |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - unset( $profile['sections'] ); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - self::$profiles[ $opt_name ][ $profile['id'] ] = $profile; |
|
| 472 | - } else { |
|
| 473 | - self::$errors[ $opt_name ]['profile']['empty'] = esc_html__( 'Unable to create a profile due an empty profile array or the profile variable passed was not an array.', 'redux-framework' ); |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - /** |
|
| 478 | - * Get Priority. |
|
| 479 | - * |
|
| 480 | - * @param string $opt_name Panel opt_name. |
|
| 481 | - * @param mixed $type Type. |
|
| 482 | - * |
|
| 483 | - * @return mixed |
|
| 484 | - */ |
|
| 485 | - public static function get_priority( string $opt_name, $type ) { |
|
| 486 | - $priority = self::$priority[ $opt_name ][ $type ]; |
|
| 487 | - self::$priority[ $opt_name ][ $type ] += 1; |
|
| 488 | - |
|
| 489 | - return $priority; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * Check opt name. |
|
| 494 | - * |
|
| 495 | - * @param string $opt_name Panel opt_name. |
|
| 496 | - */ |
|
| 497 | - public static function check_opt_name( string $opt_name = '' ) { |
|
| 498 | - if ( empty( $opt_name ) || is_array( $opt_name ) ) { |
|
| 499 | - return; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - if ( ! isset( self::$profiles[ $opt_name ] ) ) { |
|
| 503 | - self::$profiles[ $opt_name ] = array(); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - if ( ! isset( self::$priority[ $opt_name ] ) ) { |
|
| 507 | - self::$priority[ $opt_name ]['args'] = 1; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - if ( ! isset( self::$sections[ $opt_name ] ) ) { |
|
| 511 | - self::$sections[ $opt_name ] = array(); |
|
| 512 | - self::$priority[ $opt_name ]['sections'] = 1; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - if ( ! isset( self::$fields[ $opt_name ] ) ) { |
|
| 516 | - self::$fields[ $opt_name ] = array(); |
|
| 517 | - self::$priority[ $opt_name ]['fields'] = 1; |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - if ( ! isset( self::$errors[ $opt_name ] ) ) { |
|
| 521 | - self::$errors[ $opt_name ] = array(); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - if ( ! isset( self::$init[ $opt_name ] ) ) { |
|
| 525 | - self::$init[ $opt_name ] = false; |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - if ( ! isset( self::$args[ $opt_name ] ) ) { |
|
| 529 | - self::$args[ $opt_name ] = false; |
|
| 530 | - } |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * Get USer Meta. |
|
| 535 | - * |
|
| 536 | - * @param array $args Args array. |
|
| 537 | - * |
|
| 538 | - * @return mixed |
|
| 539 | - */ |
|
| 540 | - public static function get_user_meta( array $args = array() ) { |
|
| 541 | - $default = array( |
|
| 542 | - 'key' => '', |
|
| 543 | - 'opt_name' => '', |
|
| 544 | - 'user' => '', |
|
| 545 | - ); |
|
| 546 | - |
|
| 547 | - $args = wp_parse_args( $args, $default ); |
|
| 548 | - |
|
| 549 | - if ( empty( $args['user'] ) ) { |
|
| 550 | - $args['user'] = get_current_user_id(); |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - // phpcs:ignore WordPress.PHP.DontExtract |
|
| 554 | - extract( $args ); |
|
| 555 | - |
|
| 556 | - $single = ! empty( $key ); |
|
| 557 | - |
|
| 558 | - $meta = get_user_meta( $user, $key, $single ); |
|
| 559 | - |
|
| 560 | - if ( $single ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement |
|
| 561 | - // Do nothing. |
|
| 562 | - } elseif ( ! empty( $meta ) ) { |
|
| 563 | - foreach ( $meta as $key => $value ) { |
|
| 564 | - if ( is_array( $value ) ) { |
|
| 565 | - $value = $value[0]; |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - $meta[ $key ] = maybe_unserialize( $value ); |
|
| 569 | - } |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - return $meta; |
|
| 573 | - } |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - Redux_Users::load(); |
|
| 16 | + /** |
|
| 17 | + * Redux Users API Class |
|
| 18 | + * Simple API for Redux Framework |
|
| 19 | + * |
|
| 20 | + * @since 1.0.0 |
|
| 21 | + */ |
|
| 22 | + class Redux_Users { |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Profile array. |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + public static array $profiles = array(); |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Sections array. |
|
| 33 | + * |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + public static array $sections = array(); |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Fields array. |
|
| 40 | + * |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + public static array $fields = array(); |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Priority array. |
|
| 47 | + * |
|
| 48 | + * @var array |
|
| 49 | + */ |
|
| 50 | + public static array $priority = array(); |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Errors array. |
|
| 54 | + * |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + public static array $errors = array(); |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Init array. |
|
| 61 | + * |
|
| 62 | + * @var array |
|
| 63 | + */ |
|
| 64 | + public static array $init = array(); |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Args array. |
|
| 68 | + * |
|
| 69 | + * @var array |
|
| 70 | + */ |
|
| 71 | + public static array $args = array(); |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Load. |
|
| 75 | + */ |
|
| 76 | + public static function load() { |
|
| 77 | + add_action( 'init', array( 'Redux_Users', 'enqueue' ), 99 ); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Enqueue support files and fields. |
|
| 82 | + * |
|
| 83 | + * @throws ReflectionException Exception. |
|
| 84 | + */ |
|
| 85 | + public static function enqueue() { |
|
| 86 | + global $pagenow; |
|
| 87 | + |
|
| 88 | + // Check and run instances of Redux where the opt_name hasn't been run. |
|
| 89 | + $pagenows = array( 'user-new.php', 'profile.php', 'user-edit.php' ); |
|
| 90 | + |
|
| 91 | + if ( ! empty( self::$sections ) && in_array( $pagenow, $pagenows, true ) ) { |
|
| 92 | + $instances = Redux::all_instances(); |
|
| 93 | + |
|
| 94 | + foreach ( self::$fields as $opt_name => $fields ) { |
|
| 95 | + if ( ! isset( $instances[ $opt_name ] ) ) { |
|
| 96 | + Redux::set_args( $opt_name, array( 'menu_type' => 'hidden' ) ); |
|
| 97 | + |
|
| 98 | + Redux::set_sections( |
|
| 99 | + $opt_name, |
|
| 100 | + array( |
|
| 101 | + array( |
|
| 102 | + 'id' => 'EXTENSION_USERS_FAKE_ID' . $opt_name, |
|
| 103 | + 'fields' => $fields, |
|
| 104 | + 'title' => 'N/A', |
|
| 105 | + ), |
|
| 106 | + ) |
|
| 107 | + ); |
|
| 108 | + |
|
| 109 | + Redux::init( $opt_name ); |
|
| 110 | + |
|
| 111 | + $instances = ReduxFrameworkInstances::get_all_instances(); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + self::check_opt_name( $opt_name ); |
|
| 115 | + |
|
| 116 | + Redux::set_args( $opt_name, self::$args[ $opt_name ] ); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Construct Args. |
|
| 123 | + * |
|
| 124 | + * @param string $opt_name Panel Opt Name. |
|
| 125 | + * |
|
| 126 | + * @return mixed |
|
| 127 | + */ |
|
| 128 | + public static function construct_args( string $opt_name ) { |
|
| 129 | + $args = self::$args[ $opt_name ]; |
|
| 130 | + $args['opt_name'] = $opt_name; |
|
| 131 | + |
|
| 132 | + if ( ! isset( $args['menu_title'] ) ) { |
|
| 133 | + $args['menu_title'] = ucfirst( $opt_name ) . ' Options'; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + if ( ! isset( $args['page_title'] ) ) { |
|
| 137 | + $args['page_title'] = ucfirst( $opt_name ) . ' Options'; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + if ( ! isset( $args['page_slug'] ) ) { |
|
| 141 | + $args['page_slug'] = $opt_name . '_options'; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + return $args; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Construct Profiles. |
|
| 149 | + * |
|
| 150 | + * @param string $opt_name Panel opt_name. |
|
| 151 | + * |
|
| 152 | + * @return array |
|
| 153 | + */ |
|
| 154 | + public static function construct_profiles( string $opt_name ): array { |
|
| 155 | + $profiles = array(); |
|
| 156 | + |
|
| 157 | + if ( ! isset( self::$profiles[ $opt_name ] ) ) { |
|
| 158 | + return $profiles; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + foreach ( self::$profiles[ $opt_name ] as $profile ) { |
|
| 162 | + $profile['sections'] = self::construct_sections( $opt_name, $profile['id'] ); |
|
| 163 | + $profiles[] = $profile; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + ksort( $profiles ); |
|
| 167 | + |
|
| 168 | + return $profiles; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Construct Sections. |
|
| 173 | + * |
|
| 174 | + * @param string $opt_name Panel opt_name. |
|
| 175 | + * @param int|string $profile_id Profile ID. |
|
| 176 | + * |
|
| 177 | + * @return array |
|
| 178 | + */ |
|
| 179 | + public static function construct_sections( string $opt_name, $profile_id ): array { |
|
| 180 | + $sections = array(); |
|
| 181 | + |
|
| 182 | + if ( ! isset( self::$sections[ $opt_name ] ) ) { |
|
| 183 | + return $sections; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + foreach ( self::$sections[ $opt_name ] as $section_id => $section ) { |
|
| 187 | + if ( $section['profile_id'] === $profile_id ) { |
|
| 188 | + self::$sections[ $opt_name ][ $section_id ]['roles'] = $section; |
|
| 189 | + |
|
| 190 | + $p = $section['priority']; |
|
| 191 | + |
|
| 192 | + while ( isset( $sections[ $p ] ) ) { |
|
| 193 | + echo esc_html( $p++ ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + $section['fields'] = self::construct_fields( $opt_name, $section_id ); |
|
| 197 | + $sections[ $p ] = $section; |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + return $sections; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Construct Fields. |
|
| 206 | + * |
|
| 207 | + * @param string $opt_name Panel opt_name. |
|
| 208 | + * @param string $section_id Section ID. |
|
| 209 | + * @param bool $permissions Permissions. |
|
| 210 | + * @param bool $roles Roles. |
|
| 211 | + * |
|
| 212 | + * @return array |
|
| 213 | + */ |
|
| 214 | + public static function construct_fields( string $opt_name = '', string $section_id = '', bool $permissions = false, bool $roles = false ): array { |
|
| 215 | + $fields = array(); |
|
| 216 | + |
|
| 217 | + if ( ! isset( self::$fields[ $opt_name ] ) ) { |
|
| 218 | + return $fields; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + foreach ( self::$fields[ $opt_name ] as $key => $field ) { |
|
| 222 | + // Nested permissions. |
|
| 223 | + $field['permissions'] = $field['permissions'] ?? $permissions; |
|
| 224 | + |
|
| 225 | + self::$fields[ $opt_name ][ $key ]['permissions'] = $field['permissions']; |
|
| 226 | + |
|
| 227 | + // Nested roles permissions. |
|
| 228 | + $field['roles'] = $field['roles'] ?? $roles; |
|
| 229 | + |
|
| 230 | + self::$fields[ $opt_name ][ $key ]['roles'] = $field['roles']; |
|
| 231 | + |
|
| 232 | + if ( $field['section_id'] === $section_id ) { |
|
| 233 | + $p = $field['priority']; |
|
| 234 | + |
|
| 235 | + while ( isset( $fields[ $p ] ) ) { |
|
| 236 | + echo esc_html( $p++ ); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + $fields[ $p ] = $field; |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + ksort( $fields ); |
|
| 244 | + |
|
| 245 | + return $fields; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Get Section. |
|
| 250 | + * |
|
| 251 | + * @param string $opt_name Panel opt_name. |
|
| 252 | + * @param string $id ID. |
|
| 253 | + * |
|
| 254 | + * @return bool |
|
| 255 | + */ |
|
| 256 | + public static function get_section( string $opt_name = '', string $id = '' ): bool { |
|
| 257 | + self::check_opt_name( $opt_name ); |
|
| 258 | + |
|
| 259 | + if ( ! empty( $opt_name ) && ! empty( $id ) ) { |
|
| 260 | + if ( ! isset( self::$sections[ $opt_name ][ $id ] ) ) { |
|
| 261 | + $id = strtolower( sanitize_html_class( $id ) ); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + return self::$sections[ $opt_name ][ $id ] ?? false; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + return false; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Set args. |
|
| 272 | + * |
|
| 273 | + * @param string $opt_name Panel opt_name. |
|
| 274 | + * @param array $args Args array. |
|
| 275 | + */ |
|
| 276 | + public static function set_args( string $opt_name = '', array $args = array() ) { |
|
| 277 | + self::check_opt_name( $opt_name ); |
|
| 278 | + |
|
| 279 | + if ( ! empty( $opt_name ) && is_array( $args ) && ! empty( $args ) ) { |
|
| 280 | + self::$args[ $opt_name ] = self::$args[ $opt_name ] ?? array(); |
|
| 281 | + self::$args[ $opt_name ] = wp_parse_args( $args, self::$args[ $opt_name ] ); |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Set Section. |
|
| 287 | + * |
|
| 288 | + * @param string $opt_name Panel opt_name. |
|
| 289 | + * @param array $section Section array. |
|
| 290 | + */ |
|
| 291 | + public static function set_section( string $opt_name = '', array $section = array() ) { |
|
| 292 | + self::check_opt_name( $opt_name ); |
|
| 293 | + |
|
| 294 | + if ( ! empty( $opt_name ) && is_array( $section ) && ! empty( $section ) ) { |
|
| 295 | + if ( ! isset( $section['id'] ) ) { |
|
| 296 | + if ( isset( $section['title'] ) ) { |
|
| 297 | + $section['id'] = strtolower( sanitize_html_class( $section['title'] ) ); |
|
| 298 | + } else { |
|
| 299 | + $section['id'] = 'section'; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + if ( isset( self::$sections[ $opt_name ][ $section['id'] ] ) ) { |
|
| 303 | + $orig = $section['id']; |
|
| 304 | + $i = 0; |
|
| 305 | + |
|
| 306 | + while ( isset( self::$sections[ $opt_name ][ $section['id'] ] ) ) { |
|
| 307 | + $section['id'] = $orig . '_' . $i; |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + if ( ! isset( $section['priority'] ) ) { |
|
| 313 | + $section['priority'] = self::get_priority( $opt_name, 'sections' ); |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + if ( isset( $section['fields'] ) ) { |
|
| 317 | + if ( ! empty( $section['fields'] ) && is_array( $section['fields'] ) ) { |
|
| 318 | + if ( isset( $section['permissions'] ) || isset( $section['roles'] ) ) { |
|
| 319 | + foreach ( $section['fields'] as $key => $field ) { |
|
| 320 | + if ( ! isset( $field['permissions'] ) && isset( $section['permissions'] ) ) { |
|
| 321 | + $section['fields'][ $key ]['permissions'] = $section['permissions']; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + if ( ! isset( $field['roles'] ) && isset( $section['roles'] ) ) { |
|
| 325 | + $section['fields'][ $key ]['roles'] = $section['roles']; |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + self::process_fields_array( $opt_name, $section['id'], $section['fields'] ); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + unset( $section['fields'] ); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + self::$sections[ $opt_name ][ $section['id'] ] = $section; |
|
| 337 | + } else { |
|
| 338 | + self::$errors[ $opt_name ]['section']['empty'] = esc_html__( 'Unable to create a section due an empty section array or the section variable passed was not an array.', 'redux-framework' ); |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Process Section Array. |
|
| 344 | + * |
|
| 345 | + * @param string $opt_name Panel opt_name. |
|
| 346 | + * @param string $profile_id Profile ID. |
|
| 347 | + * @param array $sections Sections array. |
|
| 348 | + */ |
|
| 349 | + public static function process_sections_array( string $opt_name = '', string $profile_id = '', array $sections = array() ) { |
|
| 350 | + if ( ! empty( $opt_name ) && ! empty( $profile_id ) && is_array( $sections ) && ! empty( $sections ) ) { |
|
| 351 | + foreach ( $sections as $section ) { |
|
| 352 | + if ( ! is_array( $section ) ) { |
|
| 353 | + continue; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + $section['profile_id'] = $profile_id; |
|
| 357 | + |
|
| 358 | + if ( ! isset( $section['fields'] ) || ! is_array( $section['fields'] ) ) { |
|
| 359 | + $section['fields'] = array(); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + self::set_section( $opt_name, $section ); |
|
| 363 | + } |
|
| 364 | + } |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Process Fields Array. |
|
| 369 | + * |
|
| 370 | + * @param string $opt_name Panel opt_name. |
|
| 371 | + * @param string $section_id Section ID. |
|
| 372 | + * @param array $fields Fields array. |
|
| 373 | + */ |
|
| 374 | + public static function process_fields_array( string $opt_name = '', string $section_id = '', array $fields = array() ) { |
|
| 375 | + if ( ! empty( $opt_name ) && ! empty( $section_id ) && is_array( $fields ) && ! empty( $fields ) ) { |
|
| 376 | + foreach ( $fields as $field ) { |
|
| 377 | + if ( ! is_array( $field ) ) { |
|
| 378 | + continue; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + $field['section_id'] = $section_id; |
|
| 382 | + |
|
| 383 | + self::set_field( $opt_name, $field ); |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Get field. |
|
| 390 | + * |
|
| 391 | + * @param string $opt_name Panel opt_name. |
|
| 392 | + * @param string $id Field ID. |
|
| 393 | + * |
|
| 394 | + * @return bool |
|
| 395 | + */ |
|
| 396 | + public static function get_field( string $opt_name = '', string $id = '' ): bool { |
|
| 397 | + self::check_opt_name( $opt_name ); |
|
| 398 | + |
|
| 399 | + if ( ! empty( $opt_name ) && ! empty( $id ) ) { |
|
| 400 | + return self::$fields[ $opt_name ][ $id ] ?? false; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + return false; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Set field. |
|
| 408 | + * |
|
| 409 | + * @param string $opt_name Panel opt_name. |
|
| 410 | + * @param array $field Field array. |
|
| 411 | + */ |
|
| 412 | + public static function set_field( string $opt_name = '', array $field = array() ) { |
|
| 413 | + self::check_opt_name( $opt_name ); |
|
| 414 | + |
|
| 415 | + if ( ! empty( $opt_name ) && is_array( $field ) && ! empty( $field ) ) { |
|
| 416 | + if ( ! isset( $field['priority'] ) ) { |
|
| 417 | + $field['priority'] = self::get_priority( $opt_name, 'fields' ); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + self::$fields[ $opt_name ][ $field['id'] ] = $field; |
|
| 421 | + } |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * Set Profile. |
|
| 426 | + * |
|
| 427 | + * @param string $opt_name Panel opt_name. |
|
| 428 | + * @param array $profile Profile array. |
|
| 429 | + */ |
|
| 430 | + public static function set_profile( string $opt_name = '', array $profile = array() ) { |
|
| 431 | + self::check_opt_name( $opt_name ); |
|
| 432 | + |
|
| 433 | + if ( ! empty( $opt_name ) && is_array( $profile ) && ! empty( $profile ) ) { |
|
| 434 | + if ( ! isset( $profile['id'] ) ) { |
|
| 435 | + if ( isset( $profile['title'] ) ) { |
|
| 436 | + $profile['id'] = strtolower( sanitize_html_class( $profile['title'] ) ); |
|
| 437 | + } else { |
|
| 438 | + $profile['id'] = 'profile'; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + if ( isset( self::$profiles[ $opt_name ][ $profile['id'] ] ) ) { |
|
| 442 | + $orig = $profile['id']; |
|
| 443 | + $i = 0; |
|
| 444 | + |
|
| 445 | + while ( isset( self::$profiles[ $opt_name ][ $profile['id'] ] ) ) { |
|
| 446 | + $profile['id'] = $orig . '_' . $i; |
|
| 447 | + } |
|
| 448 | + } |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + if ( isset( $profile['sections'] ) ) { |
|
| 452 | + if ( ! empty( $profile['sections'] ) && is_array( $profile['sections'] ) ) { |
|
| 453 | + if ( isset( $profile['permissions'] ) || isset( $profile['roles'] ) ) { |
|
| 454 | + foreach ( $profile['sections'] as $key => $section ) { |
|
| 455 | + if ( ! isset( $section['permissions'] ) && isset( $profile['permissions'] ) ) { |
|
| 456 | + $profile['sections'][ $key ]['permissions'] = $profile['permissions']; |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + if ( ! isset( $section['roles'] ) && isset( $profile['roles'] ) ) { |
|
| 460 | + $profile['sections'][ $key ]['roles'] = $profile['roles']; |
|
| 461 | + } |
|
| 462 | + } |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + self::process_sections_array( $opt_name, $profile['id'], $profile['sections'] ); |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + unset( $profile['sections'] ); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + self::$profiles[ $opt_name ][ $profile['id'] ] = $profile; |
|
| 472 | + } else { |
|
| 473 | + self::$errors[ $opt_name ]['profile']['empty'] = esc_html__( 'Unable to create a profile due an empty profile array or the profile variable passed was not an array.', 'redux-framework' ); |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + /** |
|
| 478 | + * Get Priority. |
|
| 479 | + * |
|
| 480 | + * @param string $opt_name Panel opt_name. |
|
| 481 | + * @param mixed $type Type. |
|
| 482 | + * |
|
| 483 | + * @return mixed |
|
| 484 | + */ |
|
| 485 | + public static function get_priority( string $opt_name, $type ) { |
|
| 486 | + $priority = self::$priority[ $opt_name ][ $type ]; |
|
| 487 | + self::$priority[ $opt_name ][ $type ] += 1; |
|
| 488 | + |
|
| 489 | + return $priority; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * Check opt name. |
|
| 494 | + * |
|
| 495 | + * @param string $opt_name Panel opt_name. |
|
| 496 | + */ |
|
| 497 | + public static function check_opt_name( string $opt_name = '' ) { |
|
| 498 | + if ( empty( $opt_name ) || is_array( $opt_name ) ) { |
|
| 499 | + return; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + if ( ! isset( self::$profiles[ $opt_name ] ) ) { |
|
| 503 | + self::$profiles[ $opt_name ] = array(); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + if ( ! isset( self::$priority[ $opt_name ] ) ) { |
|
| 507 | + self::$priority[ $opt_name ]['args'] = 1; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + if ( ! isset( self::$sections[ $opt_name ] ) ) { |
|
| 511 | + self::$sections[ $opt_name ] = array(); |
|
| 512 | + self::$priority[ $opt_name ]['sections'] = 1; |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + if ( ! isset( self::$fields[ $opt_name ] ) ) { |
|
| 516 | + self::$fields[ $opt_name ] = array(); |
|
| 517 | + self::$priority[ $opt_name ]['fields'] = 1; |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + if ( ! isset( self::$errors[ $opt_name ] ) ) { |
|
| 521 | + self::$errors[ $opt_name ] = array(); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + if ( ! isset( self::$init[ $opt_name ] ) ) { |
|
| 525 | + self::$init[ $opt_name ] = false; |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + if ( ! isset( self::$args[ $opt_name ] ) ) { |
|
| 529 | + self::$args[ $opt_name ] = false; |
|
| 530 | + } |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * Get USer Meta. |
|
| 535 | + * |
|
| 536 | + * @param array $args Args array. |
|
| 537 | + * |
|
| 538 | + * @return mixed |
|
| 539 | + */ |
|
| 540 | + public static function get_user_meta( array $args = array() ) { |
|
| 541 | + $default = array( |
|
| 542 | + 'key' => '', |
|
| 543 | + 'opt_name' => '', |
|
| 544 | + 'user' => '', |
|
| 545 | + ); |
|
| 546 | + |
|
| 547 | + $args = wp_parse_args( $args, $default ); |
|
| 548 | + |
|
| 549 | + if ( empty( $args['user'] ) ) { |
|
| 550 | + $args['user'] = get_current_user_id(); |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + // phpcs:ignore WordPress.PHP.DontExtract |
|
| 554 | + extract( $args ); |
|
| 555 | + |
|
| 556 | + $single = ! empty( $key ); |
|
| 557 | + |
|
| 558 | + $meta = get_user_meta( $user, $key, $single ); |
|
| 559 | + |
|
| 560 | + if ( $single ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement |
|
| 561 | + // Do nothing. |
|
| 562 | + } elseif ( ! empty( $meta ) ) { |
|
| 563 | + foreach ( $meta as $key => $value ) { |
|
| 564 | + if ( is_array( $value ) ) { |
|
| 565 | + $value = $value[0]; |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + $meta[ $key ] = maybe_unserialize( $value ); |
|
| 569 | + } |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + return $meta; |
|
| 573 | + } |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + Redux_Users::load(); |
|
| 577 | 577 | } |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | $x = get_option( $this->parent->args['opt_name'] ); |
| 128 | - $color_opts = $x[ $this->field_id ] ?? array(); |
|
| 128 | + $color_opts = $x[$this->field_id] ?? array(); |
|
| 129 | 129 | $wrong_format = false; |
| 130 | 130 | |
| 131 | 131 | if ( ! isset( $color_opts['color_scheme_name'] ) ) { |
@@ -133,8 +133,8 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | $data = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
| 135 | 135 | |
| 136 | - if ( ! empty( $data ) && isset( $x[ $this->field_id ] ) ) { |
|
| 137 | - $x[ $this->field_id ] = $data; |
|
| 136 | + if ( ! empty( $data ) && isset( $x[$this->field_id] ) ) { |
|
| 137 | + $x[$this->field_id] = $data; |
|
| 138 | 138 | |
| 139 | 139 | update_option( $this->parent->args['opt_name'], $x ); |
| 140 | 140 | } |
@@ -165,9 +165,9 @@ discard block |
||
| 165 | 165 | |
| 166 | 166 | $data = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
| 167 | 167 | |
| 168 | - $this->parent->options[ $this->field_id ] = $data; |
|
| 168 | + $this->parent->options[$this->field_id] = $data; |
|
| 169 | 169 | |
| 170 | - $defaults[ $this->field_id ] = $data; |
|
| 170 | + $defaults[$this->field_id] = $data; |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | return $defaults; |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | if ( Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
| 218 | 218 | // Check if reset_all was fired. |
| 219 | 219 | $this->reset_all(); |
| 220 | - $defaults[ $this->field_id ] = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 220 | + $defaults[$this->field_id] = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | return $defaults; |
@@ -237,7 +237,7 @@ discard block |
||
| 237 | 237 | $cur_tab = sanitize_text_field( wp_unslash( $_COOKIE['redux_current_tab'] ) ); |
| 238 | 238 | |
| 239 | 239 | // Get the tab/section number field is used on. |
| 240 | - $tab_num = $this->parent->field_sections['color_scheme'][ $this->field_id ]; |
|
| 240 | + $tab_num = $this->parent->field_sections['color_scheme'][$this->field_id]; |
|
| 241 | 241 | |
| 242 | 242 | // Match... |
| 243 | 243 | if ( $cur_tab === $tab_num ) { |
@@ -245,7 +245,7 @@ discard block |
||
| 245 | 245 | // Reset data. |
| 246 | 246 | $this->reset_all(); |
| 247 | 247 | } |
| 248 | - $defaults[ $this->field_id ] = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 248 | + $defaults[$this->field_id] = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 249 | 249 | } |
| 250 | 250 | } |
| 251 | 251 | |
@@ -263,7 +263,7 @@ discard block |
||
| 263 | 263 | * @access public |
| 264 | 264 | */ |
| 265 | 265 | public function save_hook( array $saved_options = array(), array $old_options = array() ): array { |
| 266 | - if ( ! isset( $saved_options[ $this->field_id ] ) || empty( $saved_options[ $this->field_id ] ) || ( is_array( $saved_options[ $this->field_id ] ) && $old_options === $saved_options ) || ! array_key_exists( $this->field_id, $saved_options ) ) { |
|
| 266 | + if ( ! isset( $saved_options[$this->field_id] ) || empty( $saved_options[$this->field_id] ) || ( is_array( $saved_options[$this->field_id] ) && $old_options === $saved_options ) || ! array_key_exists( $this->field_id, $saved_options ) ) { |
|
| 267 | 267 | return $saved_options; |
| 268 | 268 | } |
| 269 | 269 | |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | return $saved_options; |
| 273 | 273 | } |
| 274 | 274 | |
| 275 | - $first_value = reset( $saved_options[ $this->field_id ] ); // First Element's Value. |
|
| 275 | + $first_value = reset( $saved_options[$this->field_id] ); // First Element's Value. |
|
| 276 | 276 | |
| 277 | 277 | // Parse the JSON to an array. |
| 278 | 278 | if ( isset( $first_value['data'] ) ) { |
@@ -283,7 +283,7 @@ discard block |
||
| 283 | 283 | Redux_Color_Scheme_Functions::set_current_scheme_id( $saved_options['redux-scheme-select'] ); |
| 284 | 284 | |
| 285 | 285 | // Get the current field ID. |
| 286 | - $raw_data = $saved_options[ $this->field_id ]; |
|
| 286 | + $raw_data = $saved_options[$this->field_id]; |
|
| 287 | 287 | |
| 288 | 288 | // Create a new array. |
| 289 | 289 | $save_data = array(); |
@@ -341,7 +341,7 @@ discard block |
||
| 341 | 341 | // Enum through values and assign them to a new array. |
| 342 | 342 | foreach ( $save_data as $val ) { |
| 343 | 343 | if ( isset( $val['id'] ) ) { |
| 344 | - $new_scheme[ $val['id'] ] = $val; |
|
| 344 | + $new_scheme[$val['id']] = $val; |
|
| 345 | 345 | } |
| 346 | 346 | } |
| 347 | 347 | |
@@ -354,24 +354,24 @@ discard block |
||
| 354 | 354 | if ( isset( $v['type'] ) ) { |
| 355 | 355 | $val = $v['value']; |
| 356 | 356 | |
| 357 | - unset( $database_data[ $k ] ); |
|
| 357 | + unset( $database_data[$k] ); |
|
| 358 | 358 | |
| 359 | - $database_data[ $k ] = $val; |
|
| 359 | + $database_data[$k] = $val; |
|
| 360 | 360 | } |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | - $saved_options[ $this->field_id ] = $database_data; |
|
| 363 | + $saved_options[$this->field_id] = $database_data; |
|
| 364 | 364 | |
| 365 | 365 | // Check if we should save this compared to the old data. |
| 366 | 366 | $save_scheme = false; |
| 367 | 367 | |
| 368 | 368 | // Doesn't exist or is empty. |
| 369 | - if ( ! isset( $old_options[ $this->field_id ] ) || ( isset( $old_options[ $this->field_id ] ) && ! empty( $old_options[ $this->field_id ] ) ) ) { |
|
| 369 | + if ( ! isset( $old_options[$this->field_id] ) || ( isset( $old_options[$this->field_id] ) && ! empty( $old_options[$this->field_id] ) ) ) { |
|
| 370 | 370 | $save_scheme = true; |
| 371 | 371 | } |
| 372 | 372 | |
| 373 | 373 | // Isn't empty and isn't the same as the new array. |
| 374 | - if ( ! empty( $old_options[ $this->field_id ] ) && $saved_options[ $this->field_id ] !== $old_options[ $this->field_id ] ) { |
|
| 374 | + if ( ! empty( $old_options[$this->field_id] ) && $saved_options[$this->field_id] !== $old_options[$this->field_id] ) { |
|
| 375 | 375 | $save_scheme = true; |
| 376 | 376 | } |
| 377 | 377 | |
@@ -413,7 +413,7 @@ discard block |
||
| 413 | 413 | * @access public |
| 414 | 414 | */ |
| 415 | 415 | public function reset_all() { |
| 416 | - if ( ! empty( $this->field_id ) && isset( $this->parent->options_defaults[ $this->field_id ] ) && ! empty( $this->parent->options_defaults[ $this->field_id ] ) ) { |
|
| 416 | + if ( ! empty( $this->field_id ) && isset( $this->parent->options_defaults[$this->field_id] ) && ! empty( $this->parent->options_defaults[$this->field_id] ) ) { |
|
| 417 | 417 | Redux_Color_Scheme_Functions::$parent = $this->parent; |
| 418 | 418 | Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
| 419 | 419 | |
@@ -643,10 +643,10 @@ discard block |
||
| 643 | 643 | if ( false !== $schemes ) { |
| 644 | 644 | |
| 645 | 645 | // If scheme name exists... |
| 646 | - if ( isset( $schemes[ $scheme_id ] ) ) { |
|
| 646 | + if ( isset( $schemes[$scheme_id] ) ) { |
|
| 647 | 647 | |
| 648 | 648 | // Unset it. |
| 649 | - unset( $schemes[ $scheme_id ] ); |
|
| 649 | + unset( $schemes[$scheme_id] ); |
|
| 650 | 650 | |
| 651 | 651 | // Save the scheme data, minus the deleted scheme. Upon success... |
| 652 | 652 | if ( true === Redux_Color_Scheme_Functions::write_scheme_file( $schemes ) ) { |
@@ -730,7 +730,7 @@ discard block |
||
| 730 | 730 | * @return array Default values from config. |
| 731 | 731 | */ |
| 732 | 732 | private function get_default_data(): array { |
| 733 | - $def_opts = $this->parent->options_defaults[ $this->field_id ]; |
|
| 733 | + $def_opts = $this->parent->options_defaults[$this->field_id]; |
|
| 734 | 734 | |
| 735 | 735 | if ( isset( $def_opts['color_scheme_name'] ) ) { |
| 736 | 736 | return array(); |
@@ -16,774 +16,774 @@ |
||
| 16 | 16 | // Don't duplicate me! |
| 17 | 17 | if ( ! class_exists( 'Redux_Extension_Color_Scheme' ) ) { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Class Redux_Extension_Color_Scheme |
|
| 21 | - */ |
|
| 22 | - class Redux_Extension_Color_Scheme extends Redux_Extension_Abstract { |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Extension version. |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - public static $version = '4.4.10'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Extension friendly name. |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - public string $extension_name = 'Color Schemes'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Field ID. |
|
| 40 | - * |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - public $field_id = ''; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Transparent output bit. |
|
| 47 | - * |
|
| 48 | - * @var bool |
|
| 49 | - */ |
|
| 50 | - public bool $output_transparent = false; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Extension field name. |
|
| 54 | - * |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - public string $field_name = ''; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Class Constructor. Defines the args for the extensions class |
|
| 61 | - * |
|
| 62 | - * @since 1.0.0 |
|
| 63 | - * @access public |
|
| 64 | - * |
|
| 65 | - * @param object $redux Parent settings. |
|
| 66 | - * |
|
| 67 | - * @return void |
|
| 68 | - */ |
|
| 69 | - public function __construct( $redux ) { |
|
| 70 | - parent::__construct( $redux, __FILE__ ); |
|
| 71 | - |
|
| 72 | - $this->add_field( 'color_scheme' ); |
|
| 73 | - $this->field_name = 'color_scheme'; |
|
| 74 | - |
|
| 75 | - add_filter( "redux/options/{$this->parent->args['opt_name']}/defaults", array( $this, 'set_defaults' ) ); |
|
| 19 | + /** |
|
| 20 | + * Class Redux_Extension_Color_Scheme |
|
| 21 | + */ |
|
| 22 | + class Redux_Extension_Color_Scheme extends Redux_Extension_Abstract { |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Extension version. |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + public static $version = '4.4.10'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Extension friendly name. |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + public string $extension_name = 'Color Schemes'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Field ID. |
|
| 40 | + * |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + public $field_id = ''; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Transparent output bit. |
|
| 47 | + * |
|
| 48 | + * @var bool |
|
| 49 | + */ |
|
| 50 | + public bool $output_transparent = false; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Extension field name. |
|
| 54 | + * |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + public string $field_name = ''; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Class Constructor. Defines the args for the extensions class |
|
| 61 | + * |
|
| 62 | + * @since 1.0.0 |
|
| 63 | + * @access public |
|
| 64 | + * |
|
| 65 | + * @param object $redux Parent settings. |
|
| 66 | + * |
|
| 67 | + * @return void |
|
| 68 | + */ |
|
| 69 | + public function __construct( $redux ) { |
|
| 70 | + parent::__construct( $redux, __FILE__ ); |
|
| 71 | + |
|
| 72 | + $this->add_field( 'color_scheme' ); |
|
| 73 | + $this->field_name = 'color_scheme'; |
|
| 74 | + |
|
| 75 | + add_filter( "redux/options/{$this->parent->args['opt_name']}/defaults", array( $this, 'set_defaults' ) ); |
|
| 76 | 76 | |
| 77 | - // Ajax hooks. |
|
| 78 | - add_action( 'wp_ajax_redux_color_schemes', array( $this, 'parse_ajax' ) ); |
|
| 79 | - add_action( 'wp_ajax_nopriv_redux_color_schemes', array( $this, 'parse_ajax' ) ); |
|
| 80 | - |
|
| 81 | - // Reset hooks. |
|
| 82 | - add_action( 'redux/validate/' . $this->parent->args['opt_name'] . '/defaults', array( $this, 'reset_defaults' ), 0, 3 ); |
|
| 83 | - add_action( 'redux/validate/' . $this->parent->args['opt_name'] . '/defaults_section', array( $this, 'reset_defaults_section' ), 0, 3 ); |
|
| 84 | - |
|
| 85 | - // Save filter. |
|
| 86 | - add_action( 'redux/validate/' . $this->parent->args['opt_name'] . '/before_validation', array( $this, 'save_hook' ), 0, 3 ); |
|
| 87 | - |
|
| 88 | - // Register hook - to get field id and prep helper. |
|
| 89 | - add_action( 'redux/options/' . $this->parent->args['opt_name'] . '/field/' . $this->field_name . '/register', array( $this, 'register_field' ) ); |
|
| 90 | - |
|
| 91 | - include_once $this->extension_dir . 'color_scheme/inc/class-redux-color-scheme-functions.php'; |
|
| 92 | - Redux_Color_Scheme_Functions::init( $redux ); |
|
| 93 | - |
|
| 94 | - $field = Redux_Color_Scheme_Functions::get_field( $redux ); |
|
| 95 | - |
|
| 96 | - if ( ! is_array( $field ) ) { |
|
| 97 | - return; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $this->field_id = $field['id']; |
|
| 101 | - |
|
| 102 | - // Prep storage. |
|
| 103 | - $upload_dir = Redux_Color_Scheme_Functions::$upload_dir; |
|
| 104 | - |
|
| 105 | - // Create uploads/redux_scheme_colors/ folder. |
|
| 106 | - if ( ! is_dir( $upload_dir ) ) { |
|
| 107 | - Redux_Core::$filesystem->execute( 'mkdir', $upload_dir ); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Set default values after reset. |
|
| 113 | - * |
|
| 114 | - * @param array $defaults Default values. |
|
| 115 | - * |
|
| 116 | - * @return array |
|
| 117 | - */ |
|
| 118 | - public function set_defaults( array $defaults = array() ): array { |
|
| 119 | - if ( ! Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
|
| 120 | - return $defaults; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - if ( empty( $this->field_id ) ) { |
|
| 124 | - return $defaults; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $x = get_option( $this->parent->args['opt_name'] ); |
|
| 128 | - $color_opts = $x[ $this->field_id ] ?? array(); |
|
| 129 | - $wrong_format = false; |
|
| 130 | - |
|
| 131 | - if ( ! isset( $color_opts['color_scheme_name'] ) ) { |
|
| 132 | - $wrong_format = true; |
|
| 133 | - |
|
| 134 | - $data = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 135 | - |
|
| 136 | - if ( ! empty( $data ) && isset( $x[ $this->field_id ] ) ) { |
|
| 137 | - $x[ $this->field_id ] = $data; |
|
| 138 | - |
|
| 139 | - update_option( $this->parent->args['opt_name'], $x ); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 144 | - |
|
| 145 | - $ot_val = Redux_Color_Scheme_Functions::get_output_transparent_val(); |
|
| 146 | - $this->output_transparent = $ot_val; |
|
| 147 | - |
|
| 148 | - Redux_Color_Scheme_Functions::convert_to_db(); |
|
| 149 | - |
|
| 150 | - $scheme_key = Redux_Color_Scheme_Functions::get_scheme_key(); |
|
| 151 | - $scheme_data = get_option( $scheme_key ); |
|
| 152 | - |
|
| 153 | - $scheme_data_exists = ! empty( $scheme_data ); |
|
| 154 | - |
|
| 155 | - $default_exists = in_array( 'default', array_map( 'strtolower', Redux_Color_Scheme_Functions::get_scheme_names() ), true ); |
|
| 156 | - |
|
| 157 | - if ( ! $scheme_data_exists || ! $default_exists || $wrong_format ) { |
|
| 158 | - $data = $this->get_default_data(); |
|
| 159 | - |
|
| 160 | - // Add to (and/or create) JSON scheme file. |
|
| 161 | - Redux_Color_Scheme_Functions::set_scheme_data( 'Default', $data ); |
|
| 162 | - |
|
| 163 | - // Set default scheme. |
|
| 164 | - Redux_Color_Scheme_Functions::set_current_scheme_id( 'Default' ); |
|
| 165 | - |
|
| 166 | - $data = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 167 | - |
|
| 168 | - $this->parent->options[ $this->field_id ] = $data; |
|
| 169 | - |
|
| 170 | - $defaults[ $this->field_id ] = $data; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - return $defaults; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Field Register. Sets the whole smash up. |
|
| 178 | - * |
|
| 179 | - * @param array $data Field data. |
|
| 180 | - * |
|
| 181 | - * @return array |
|
| 182 | - * @since 1.0.0 |
|
| 183 | - * @access public |
|
| 184 | - */ |
|
| 185 | - public function register_field( array $data ): array { |
|
| 186 | - |
|
| 187 | - // Include color_scheme helper. |
|
| 188 | - include_once $this->extension_dir . 'color_scheme/inc/class-redux-color-scheme-functions.php'; |
|
| 189 | - |
|
| 190 | - if ( isset( $data['output_transparent'] ) ) { |
|
| 191 | - $this->output_transparent = $data['output_transparent']; |
|
| 192 | - } |
|
| 77 | + // Ajax hooks. |
|
| 78 | + add_action( 'wp_ajax_redux_color_schemes', array( $this, 'parse_ajax' ) ); |
|
| 79 | + add_action( 'wp_ajax_nopriv_redux_color_schemes', array( $this, 'parse_ajax' ) ); |
|
| 80 | + |
|
| 81 | + // Reset hooks. |
|
| 82 | + add_action( 'redux/validate/' . $this->parent->args['opt_name'] . '/defaults', array( $this, 'reset_defaults' ), 0, 3 ); |
|
| 83 | + add_action( 'redux/validate/' . $this->parent->args['opt_name'] . '/defaults_section', array( $this, 'reset_defaults_section' ), 0, 3 ); |
|
| 84 | + |
|
| 85 | + // Save filter. |
|
| 86 | + add_action( 'redux/validate/' . $this->parent->args['opt_name'] . '/before_validation', array( $this, 'save_hook' ), 0, 3 ); |
|
| 87 | + |
|
| 88 | + // Register hook - to get field id and prep helper. |
|
| 89 | + add_action( 'redux/options/' . $this->parent->args['opt_name'] . '/field/' . $this->field_name . '/register', array( $this, 'register_field' ) ); |
|
| 90 | + |
|
| 91 | + include_once $this->extension_dir . 'color_scheme/inc/class-redux-color-scheme-functions.php'; |
|
| 92 | + Redux_Color_Scheme_Functions::init( $redux ); |
|
| 93 | + |
|
| 94 | + $field = Redux_Color_Scheme_Functions::get_field( $redux ); |
|
| 95 | + |
|
| 96 | + if ( ! is_array( $field ) ) { |
|
| 97 | + return; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $this->field_id = $field['id']; |
|
| 101 | + |
|
| 102 | + // Prep storage. |
|
| 103 | + $upload_dir = Redux_Color_Scheme_Functions::$upload_dir; |
|
| 104 | + |
|
| 105 | + // Create uploads/redux_scheme_colors/ folder. |
|
| 106 | + if ( ! is_dir( $upload_dir ) ) { |
|
| 107 | + Redux_Core::$filesystem->execute( 'mkdir', $upload_dir ); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Set default values after reset. |
|
| 113 | + * |
|
| 114 | + * @param array $defaults Default values. |
|
| 115 | + * |
|
| 116 | + * @return array |
|
| 117 | + */ |
|
| 118 | + public function set_defaults( array $defaults = array() ): array { |
|
| 119 | + if ( ! Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
|
| 120 | + return $defaults; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + if ( empty( $this->field_id ) ) { |
|
| 124 | + return $defaults; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $x = get_option( $this->parent->args['opt_name'] ); |
|
| 128 | + $color_opts = $x[ $this->field_id ] ?? array(); |
|
| 129 | + $wrong_format = false; |
|
| 130 | + |
|
| 131 | + if ( ! isset( $color_opts['color_scheme_name'] ) ) { |
|
| 132 | + $wrong_format = true; |
|
| 133 | + |
|
| 134 | + $data = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 135 | + |
|
| 136 | + if ( ! empty( $data ) && isset( $x[ $this->field_id ] ) ) { |
|
| 137 | + $x[ $this->field_id ] = $data; |
|
| 138 | + |
|
| 139 | + update_option( $this->parent->args['opt_name'], $x ); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 144 | + |
|
| 145 | + $ot_val = Redux_Color_Scheme_Functions::get_output_transparent_val(); |
|
| 146 | + $this->output_transparent = $ot_val; |
|
| 147 | + |
|
| 148 | + Redux_Color_Scheme_Functions::convert_to_db(); |
|
| 149 | + |
|
| 150 | + $scheme_key = Redux_Color_Scheme_Functions::get_scheme_key(); |
|
| 151 | + $scheme_data = get_option( $scheme_key ); |
|
| 152 | + |
|
| 153 | + $scheme_data_exists = ! empty( $scheme_data ); |
|
| 154 | + |
|
| 155 | + $default_exists = in_array( 'default', array_map( 'strtolower', Redux_Color_Scheme_Functions::get_scheme_names() ), true ); |
|
| 156 | + |
|
| 157 | + if ( ! $scheme_data_exists || ! $default_exists || $wrong_format ) { |
|
| 158 | + $data = $this->get_default_data(); |
|
| 159 | + |
|
| 160 | + // Add to (and/or create) JSON scheme file. |
|
| 161 | + Redux_Color_Scheme_Functions::set_scheme_data( 'Default', $data ); |
|
| 162 | + |
|
| 163 | + // Set default scheme. |
|
| 164 | + Redux_Color_Scheme_Functions::set_current_scheme_id( 'Default' ); |
|
| 165 | + |
|
| 166 | + $data = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 167 | + |
|
| 168 | + $this->parent->options[ $this->field_id ] = $data; |
|
| 169 | + |
|
| 170 | + $defaults[ $this->field_id ] = $data; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + return $defaults; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Field Register. Sets the whole smash up. |
|
| 178 | + * |
|
| 179 | + * @param array $data Field data. |
|
| 180 | + * |
|
| 181 | + * @return array |
|
| 182 | + * @since 1.0.0 |
|
| 183 | + * @access public |
|
| 184 | + */ |
|
| 185 | + public function register_field( array $data ): array { |
|
| 186 | + |
|
| 187 | + // Include color_scheme helper. |
|
| 188 | + include_once $this->extension_dir . 'color_scheme/inc/class-redux-color-scheme-functions.php'; |
|
| 189 | + |
|
| 190 | + if ( isset( $data['output_transparent'] ) ) { |
|
| 191 | + $this->output_transparent = $data['output_transparent']; |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - $this->field_id = $data['id']; |
|
| 195 | - Redux_Color_Scheme_Functions::$field_id = $data['id']; |
|
| 196 | - |
|
| 197 | - // Set helper parent object. |
|
| 198 | - Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 199 | - |
|
| 200 | - // Prep storage. |
|
| 201 | - $upload_dir = Redux_Color_Scheme_Functions::$upload_dir; |
|
| 202 | - |
|
| 203 | - // Set upload_dir cookie. |
|
| 204 | - setcookie( 'redux_color_scheme_upload_dir', $upload_dir, 0, '/' ); |
|
| 205 | - |
|
| 206 | - return $data; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Reset defaults. |
|
| 211 | - * |
|
| 212 | - * @param array $defaults Default values. |
|
| 213 | - * |
|
| 214 | - * @return array |
|
| 215 | - */ |
|
| 216 | - public function reset_defaults( array $defaults = array() ): array { |
|
| 217 | - if ( Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
|
| 218 | - // Check if reset_all was fired. |
|
| 219 | - $this->reset_all(); |
|
| 220 | - $defaults[ $this->field_id ] = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 221 | - } |
|
| 194 | + $this->field_id = $data['id']; |
|
| 195 | + Redux_Color_Scheme_Functions::$field_id = $data['id']; |
|
| 196 | + |
|
| 197 | + // Set helper parent object. |
|
| 198 | + Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 199 | + |
|
| 200 | + // Prep storage. |
|
| 201 | + $upload_dir = Redux_Color_Scheme_Functions::$upload_dir; |
|
| 202 | + |
|
| 203 | + // Set upload_dir cookie. |
|
| 204 | + setcookie( 'redux_color_scheme_upload_dir', $upload_dir, 0, '/' ); |
|
| 205 | + |
|
| 206 | + return $data; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Reset defaults. |
|
| 211 | + * |
|
| 212 | + * @param array $defaults Default values. |
|
| 213 | + * |
|
| 214 | + * @return array |
|
| 215 | + */ |
|
| 216 | + public function reset_defaults( array $defaults = array() ): array { |
|
| 217 | + if ( Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
|
| 218 | + // Check if reset_all was fired. |
|
| 219 | + $this->reset_all(); |
|
| 220 | + $defaults[ $this->field_id ] = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - return $defaults; |
|
| 224 | - } |
|
| 223 | + return $defaults; |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - /** |
|
| 227 | - * Reset section defaults. |
|
| 228 | - * |
|
| 229 | - * @param array $defaults Default values. |
|
| 230 | - * |
|
| 231 | - * @return array |
|
| 232 | - */ |
|
| 233 | - public function reset_defaults_section( array $defaults = array() ): array { |
|
| 234 | - if ( Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
|
| 235 | - // Get the current tab/section number. |
|
| 236 | - if ( isset( $_COOKIE['redux_current_tab'] ) ) { |
|
| 237 | - $cur_tab = sanitize_text_field( wp_unslash( $_COOKIE['redux_current_tab'] ) ); |
|
| 238 | - |
|
| 239 | - // Get the tab/section number field is used on. |
|
| 240 | - $tab_num = $this->parent->field_sections['color_scheme'][ $this->field_id ]; |
|
| 241 | - |
|
| 242 | - // Match... |
|
| 243 | - if ( $cur_tab === $tab_num ) { |
|
| 244 | - |
|
| 245 | - // Reset data. |
|
| 246 | - $this->reset_all(); |
|
| 247 | - } |
|
| 248 | - $defaults[ $this->field_id ] = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - return $defaults; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Save Changes Hook. What to do when changes are saved |
|
| 257 | - * |
|
| 258 | - * @param array $saved_options Saved data. |
|
| 259 | - * @param array $old_options Previous data. |
|
| 260 | - * |
|
| 261 | - * @return array |
|
| 262 | - * @since 1.0.0 |
|
| 263 | - * @access public |
|
| 264 | - */ |
|
| 265 | - public function save_hook( array $saved_options = array(), array $old_options = array() ): array { |
|
| 266 | - if ( ! isset( $saved_options[ $this->field_id ] ) || empty( $saved_options[ $this->field_id ] ) || ( is_array( $saved_options[ $this->field_id ] ) && $old_options === $saved_options ) || ! array_key_exists( $this->field_id, $saved_options ) ) { |
|
| 267 | - return $saved_options; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - // We'll use the reset hook instead. |
|
| 271 | - if ( ! empty( $saved_options['defaults'] ) || ! empty( $saved_options['defaults-section'] ) ) { |
|
| 272 | - return $saved_options; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - $first_value = reset( $saved_options[ $this->field_id ] ); // First Element's Value. |
|
| 276 | - |
|
| 277 | - // Parse the JSON to an array. |
|
| 278 | - if ( isset( $first_value['data'] ) ) { |
|
| 279 | - |
|
| 280 | - Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 281 | - Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 282 | - |
|
| 283 | - Redux_Color_Scheme_Functions::set_current_scheme_id( $saved_options['redux-scheme-select'] ); |
|
| 284 | - |
|
| 285 | - // Get the current field ID. |
|
| 286 | - $raw_data = $saved_options[ $this->field_id ]; |
|
| 287 | - |
|
| 288 | - // Create a new array. |
|
| 289 | - $save_data = array(); |
|
| 290 | - |
|
| 291 | - // Enum through saved data. |
|
| 292 | - foreach ( $raw_data as $id => $val ) { |
|
| 293 | - |
|
| 294 | - if ( 'color_scheme_name' !== $id ) { |
|
| 295 | - if ( is_array( $val ) ) { |
|
| 296 | - |
|
| 297 | - if ( ! isset( $val['data'] ) ) { |
|
| 298 | - continue; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - $data = json_decode( rawurldecode( $val['data'] ), true ); |
|
| 302 | - |
|
| 303 | - // Sanitize everything. |
|
| 304 | - $color = $data['color'] ?? ''; |
|
| 305 | - $alpha = $data['alpha'] ?? 1; |
|
| 306 | - |
|
| 307 | - $id = $data['id'] ?? $id; |
|
| 308 | - $title = $data['title'] ?? $id; |
|
| 309 | - |
|
| 310 | - $grp = $data['group'] ?? ''; |
|
| 311 | - |
|
| 312 | - if ( '' === $color || 'transparent' === $color ) { |
|
| 313 | - $rgba = $this->output_transparent ? 'transparent' : ''; |
|
| 314 | - } else { |
|
| 315 | - $rgba = Redux_Helpers::hex2rgba( $color, $alpha ); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - // Create an array of saved data. |
|
| 319 | - $save_data[] = array( |
|
| 320 | - 'id' => $id, |
|
| 321 | - 'title' => $title, |
|
| 322 | - 'color' => $color, |
|
| 323 | - 'alpha' => $alpha, |
|
| 324 | - 'group' => $grp, |
|
| 325 | - 'rgba' => $rgba, |
|
| 326 | - ); |
|
| 327 | - } else { |
|
| 328 | - $save_data[] = array( |
|
| 329 | - 'id' => $id, |
|
| 330 | - 'value' => $val, |
|
| 331 | - 'type' => 'select', |
|
| 332 | - ); |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - $new_scheme = array(); |
|
| 338 | - |
|
| 339 | - $new_scheme['color_scheme_name'] = Redux_Color_Scheme_Functions::get_current_scheme_id(); |
|
| 340 | - |
|
| 341 | - // Enum through values and assign them to a new array. |
|
| 342 | - foreach ( $save_data as $val ) { |
|
| 343 | - if ( isset( $val['id'] ) ) { |
|
| 344 | - $new_scheme[ $val['id'] ] = $val; |
|
| 345 | - } |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - // Filter for DB save |
|
| 349 | - // Doesn't need to save select arrays to a database, |
|
| 350 | - // just the id => value. |
|
| 351 | - $database_data = $new_scheme; |
|
| 352 | - |
|
| 353 | - foreach ( $database_data as $k => $v ) { |
|
| 354 | - if ( isset( $v['type'] ) ) { |
|
| 355 | - $val = $v['value']; |
|
| 356 | - |
|
| 357 | - unset( $database_data[ $k ] ); |
|
| 358 | - |
|
| 359 | - $database_data[ $k ] = $val; |
|
| 360 | - } |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - $saved_options[ $this->field_id ] = $database_data; |
|
| 364 | - |
|
| 365 | - // Check if we should save this compared to the old data. |
|
| 366 | - $save_scheme = false; |
|
| 367 | - |
|
| 368 | - // Doesn't exist or is empty. |
|
| 369 | - if ( ! isset( $old_options[ $this->field_id ] ) || ( isset( $old_options[ $this->field_id ] ) && ! empty( $old_options[ $this->field_id ] ) ) ) { |
|
| 370 | - $save_scheme = true; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - // Isn't empty and isn't the same as the new array. |
|
| 374 | - if ( ! empty( $old_options[ $this->field_id ] ) && $saved_options[ $this->field_id ] !== $old_options[ $this->field_id ] ) { |
|
| 375 | - $save_scheme = true; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - if ( $save_scheme ) { |
|
| 379 | - $scheme = Redux_Color_Scheme_Functions::get_current_scheme_id(); |
|
| 380 | - Redux_Color_Scheme_Functions::set_scheme_data( $scheme, $save_data ); |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - return $saved_options; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Reset data. Restores colour picker to default values |
|
| 389 | - * |
|
| 390 | - * @since 1.0.0 |
|
| 391 | - * @access private |
|
| 392 | - * @return void |
|
| 393 | - */ |
|
| 394 | - private function reset_data() { |
|
| 395 | - Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 396 | - Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 397 | - |
|
| 398 | - // Get default data. |
|
| 399 | - $data = $this->get_default_data(); |
|
| 400 | - |
|
| 401 | - // Add to (and/or create) JSON scheme file. |
|
| 402 | - Redux_Color_Scheme_Functions::set_scheme_data( 'Default', $data ); |
|
| 403 | - |
|
| 404 | - // Set default scheme. |
|
| 405 | - Redux_Color_Scheme_Functions::set_current_scheme_id( 'Default' ); |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Reset All Hook. Todo list when all data is reset |
|
| 410 | - * |
|
| 411 | - * @return void |
|
| 412 | - * @since 1.0.0 |
|
| 413 | - * @access public |
|
| 414 | - */ |
|
| 415 | - public function reset_all() { |
|
| 416 | - if ( ! empty( $this->field_id ) && isset( $this->parent->options_defaults[ $this->field_id ] ) && ! empty( $this->parent->options_defaults[ $this->field_id ] ) ) { |
|
| 417 | - Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 418 | - Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 419 | - |
|
| 420 | - $this->reset_data(); |
|
| 421 | - } |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * AJAX evaluator. Determine course of action based on AJAX callback |
|
| 426 | - * |
|
| 427 | - * @since 1.0.0 |
|
| 428 | - * @access public |
|
| 429 | - * @return void |
|
| 430 | - */ |
|
| 431 | - public function parse_ajax() { |
|
| 432 | - if ( isset( $_REQUEST['nonce'] ) && isset( $_REQUEST['opt_name'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['nonce'] ) ), 'redux_' . sanitize_text_field( wp_unslash( $_REQUEST['opt_name'] ) ) . '_color_schemes' ) ) { |
|
| 433 | - $parent = $this->parent; |
|
| 434 | - |
|
| 435 | - // Do action. |
|
| 436 | - if ( isset( $_REQUEST['type'] ) ) { |
|
| 437 | - |
|
| 438 | - // Save scheme. |
|
| 439 | - if ( 'save' === $_REQUEST['type'] ) { |
|
| 440 | - $this->save_scheme( $parent ); |
|
| 441 | - |
|
| 442 | - // Delete scheme. |
|
| 443 | - } elseif ( 'delete' === $_REQUEST['type'] ) { |
|
| 444 | - $this->delete_scheme( $parent ); |
|
| 445 | - |
|
| 446 | - // Scheme change. |
|
| 447 | - } elseif ( 'update' === $_REQUEST['type'] ) { |
|
| 448 | - $this->get_scheme_html( $parent ); |
|
| 449 | - |
|
| 450 | - // Export scheme file. |
|
| 451 | - } elseif ( 'export' === $_REQUEST['type'] ) { |
|
| 452 | - $this->download_schemes(); |
|
| 453 | - |
|
| 454 | - // Import scheme file. |
|
| 455 | - } elseif ( 'import' === $_REQUEST['type'] ) { |
|
| 456 | - $this->import_schemes(); |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - } else { |
|
| 460 | - wp_die( esc_html__( 'Invalid Security Credentials. Please reload the page and try again.', 'redux-framework' ) ); |
|
| 461 | - } |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Download Scheme File. |
|
| 466 | - * |
|
| 467 | - * @since 4.4.18 |
|
| 468 | - * @access private |
|
| 469 | - * @return void |
|
| 470 | - */ |
|
| 471 | - private function import_schemes() { |
|
| 472 | - if ( isset( $_REQUEST['content'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 473 | - $content = wp_unslash( $_REQUEST['content'] ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
|
| 474 | - $content = is_array( $content ) ? array_map( 'stripslashes_deep', $content ) : stripslashes( $content ); |
|
| 475 | - $content = json_decode( $content, true ); |
|
| 476 | - |
|
| 477 | - if ( is_null( $content ) ) { |
|
| 478 | - $result = array( |
|
| 479 | - 'result' => false, |
|
| 480 | - 'data' => esc_html__( 'Import unsuccessful! Malformed JSON data detected.', 'redux-framework' ), |
|
| 481 | - ); |
|
| 482 | - |
|
| 483 | - $result = wp_json_encode( $result ); |
|
| 484 | - |
|
| 485 | - echo $result; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 486 | - |
|
| 487 | - die; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - if ( isset( $content['Default']['color_scheme_name'] ) ) { |
|
| 491 | - $content = wp_json_encode( $content ); |
|
| 492 | - |
|
| 493 | - $param_array = array( |
|
| 494 | - 'content' => $content, |
|
| 495 | - 'overwrite' => true, |
|
| 496 | - 'chmod' => FS_CHMOD_FILE, |
|
| 497 | - ); |
|
| 498 | - |
|
| 499 | - $import_file = Redux_Color_Scheme_Functions::$upload_dir . Redux_Color_Scheme_Functions::$parent->args['opt_name'] . '_' . Redux_Color_Scheme_Functions::$field_id . '.json'; |
|
| 500 | - |
|
| 501 | - if ( true === Redux_Core::$filesystem->execute( 'put_contents', $import_file, $param_array ) ) { |
|
| 502 | - $result = array( |
|
| 503 | - 'result' => true, |
|
| 504 | - // translators: %s = HTML content. |
|
| 505 | - 'data' => sprintf( esc_html__( 'Import successful! Click %s to refresh.', 'redux-framework' ), '<strong>' . esc_html__( 'OK', 'redux-framework' ) . '</strong>' ), |
|
| 506 | - ); |
|
| 507 | - } else { |
|
| 508 | - $result = array( |
|
| 509 | - 'result' => false, |
|
| 510 | - 'data' => esc_html__( 'Import unsuccessful! File permission error: Could not write import data to server.', 'redux-framework' ), |
|
| 511 | - ); |
|
| 512 | - } |
|
| 513 | - } else { |
|
| 514 | - $result = array( |
|
| 515 | - 'result' => false, |
|
| 516 | - 'data' => esc_html__( 'Import unsuccessful! The selected file is not a valid color scheme file.', 'redux-framework' ), |
|
| 517 | - ); |
|
| 518 | - } |
|
| 519 | - } else { |
|
| 520 | - $result = array( |
|
| 521 | - 'result' => false, |
|
| 522 | - 'data' => esc_html__( 'Import unsuccessful! No data detected in the import file.', 'redux-framework' ), |
|
| 523 | - ); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - $result = wp_json_encode( $result ); |
|
| 527 | - |
|
| 528 | - echo $result; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 529 | - |
|
| 530 | - die; |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * Download Scheme File. |
|
| 535 | - * |
|
| 536 | - * @since 1.0.0 |
|
| 537 | - * @access private |
|
| 538 | - * @return void |
|
| 539 | - */ |
|
| 540 | - private function download_schemes() { |
|
| 541 | - Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 542 | - Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 543 | - |
|
| 544 | - // Read contents of scheme file. |
|
| 545 | - $content = Redux_Color_Scheme_Functions::read_scheme_file(); |
|
| 546 | - $content = wp_json_encode( $content ); |
|
| 547 | - |
|
| 548 | - // Set header info. |
|
| 549 | - header( 'Content-Description: File Transfer' ); |
|
| 550 | - header( 'Content-type: application/txt' ); |
|
| 551 | - header( 'Content-Disposition: attachment; filename="redux_schemes_' . $this->parent->args['opt_name'] . '_' . $this->field_id . '_' . gmdate( 'm-d-Y' ) . '.json"' ); |
|
| 552 | - header( 'Content-Transfer-Encoding: binary' ); |
|
| 553 | - header( 'Expires: 0' ); |
|
| 554 | - header( 'Cache-Control: must-revalidate' ); |
|
| 555 | - header( 'Pragma: public' ); |
|
| 556 | - |
|
| 557 | - // File download. |
|
| 558 | - echo $content; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 559 | - |
|
| 560 | - // 2B ~! 2B |
|
| 561 | - die; |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - /** |
|
| 565 | - * Save Scheme. Saved an individual scheme to JSON scheme file. |
|
| 566 | - * |
|
| 567 | - * @param ReduxFramework $redux ReduxFramework object. |
|
| 568 | - * |
|
| 569 | - * @return void |
|
| 570 | - * @since 1.0.0 |
|
| 571 | - * @access private |
|
| 572 | - */ |
|
| 573 | - private function save_scheme( ReduxFramework $redux ) { |
|
| 574 | - Redux_Color_Scheme_Functions::$parent = $redux; |
|
| 575 | - Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 576 | - |
|
| 577 | - // Get the scheme name. |
|
| 578 | - if ( isset( $_REQUEST['scheme_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 579 | - $scheme_name = sanitize_text_field( wp_unslash( $_REQUEST['scheme_name'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 580 | - |
|
| 581 | - // Check for duplicates. |
|
| 582 | - $names = Redux_Color_Scheme_Functions::get_scheme_names(); |
|
| 583 | - foreach ( $names as $name ) { |
|
| 584 | - $name = strtolower( $name ); |
|
| 585 | - $tmp_name = strtolower( $scheme_name ); |
|
| 586 | - |
|
| 587 | - if ( $name === $tmp_name ) { |
|
| 588 | - echo 'fail'; |
|
| 589 | - die(); |
|
| 590 | - } |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - // Get scheme data. |
|
| 594 | - if ( isset( $_REQUEST['scheme_data'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 595 | - $scheme_data = wp_unslash( $_REQUEST['scheme_data'] ); // phpcs:ignore WordPress.Security |
|
| 596 | - |
|
| 597 | - // Get field ID. |
|
| 598 | - if ( isset( $_REQUEST['field_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 599 | - $scheme_data = rawurldecode( $scheme_data ); |
|
| 600 | - $scheme_data = json_decode( $scheme_data, true ); |
|
| 601 | - |
|
| 602 | - // Save scheme to file. If successful... |
|
| 603 | - if ( true === Redux_Color_Scheme_Functions::set_scheme_data( $scheme_name, $scheme_data ) ) { |
|
| 604 | - |
|
| 605 | - // Update scheme selector. |
|
| 606 | - echo Redux_Color_Scheme_Functions::get_scheme_select_html( $scheme_name ); // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 607 | - } |
|
| 608 | - } |
|
| 609 | - } |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - die(); // a horrible death! |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Delete Scheme. Delete individual scheme from JSON scheme file. |
|
| 617 | - * |
|
| 618 | - * @param ReduxFramework $redux ReduxFramework object. |
|
| 619 | - * |
|
| 620 | - * @return void |
|
| 621 | - * @since 1.0.0 |
|
| 622 | - * @access private |
|
| 623 | - */ |
|
| 624 | - private function delete_scheme( ReduxFramework $redux ) { |
|
| 625 | - |
|
| 626 | - // Get deleted scheme ID. |
|
| 627 | - if ( isset( $_REQUEST['scheme_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 628 | - $scheme_id = sanitize_text_field( wp_unslash( $_REQUEST['scheme_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 629 | - |
|
| 630 | - // Get field ID. |
|
| 631 | - if ( isset( $_REQUEST['field_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 632 | - $field_id = sanitize_text_field( wp_unslash( $_REQUEST['field_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 633 | - |
|
| 634 | - // If scheme ID was passed (and why wouldn't it be?? Hmm??). |
|
| 635 | - if ( $scheme_id ) { |
|
| 636 | - Redux_Color_Scheme_Functions::$field_id = $field_id; |
|
| 637 | - Redux_Color_Scheme_Functions::$parent = $redux; |
|
| 638 | - |
|
| 639 | - // Get the entire scheme file. |
|
| 640 | - $schemes = Redux_Color_Scheme_Functions::read_scheme_file(); |
|
| 641 | - |
|
| 642 | - // If we got a good read... |
|
| 643 | - if ( false !== $schemes ) { |
|
| 644 | - |
|
| 645 | - // If scheme name exists... |
|
| 646 | - if ( isset( $schemes[ $scheme_id ] ) ) { |
|
| 647 | - |
|
| 648 | - // Unset it. |
|
| 649 | - unset( $schemes[ $scheme_id ] ); |
|
| 650 | - |
|
| 651 | - // Save the scheme data, minus the deleted scheme. Upon success... |
|
| 652 | - if ( true === Redux_Color_Scheme_Functions::write_scheme_file( $schemes ) ) { |
|
| 653 | - |
|
| 654 | - // Set default scheme. |
|
| 655 | - Redux_Color_Scheme_Functions::set_current_scheme_id( 'Default' ); |
|
| 656 | - |
|
| 657 | - // Update field ID. |
|
| 658 | - Redux_Color_Scheme_Functions::$field_id = $field_id; |
|
| 659 | - |
|
| 660 | - // Meh TODO. |
|
| 661 | - Redux_Color_Scheme_Functions::set_database_data(); |
|
| 662 | - |
|
| 663 | - echo 'success'; |
|
| 664 | - } else { |
|
| 665 | - echo 'Failed to write JSON file to server.'; |
|
| 666 | - } |
|
| 667 | - } else { |
|
| 668 | - echo 'Scheme name does not exist in JSON string. Aborting.'; |
|
| 669 | - } |
|
| 670 | - } else { |
|
| 671 | - echo 'Failed to read JSON scheme file, or file is empty.'; |
|
| 672 | - } |
|
| 673 | - } else { |
|
| 674 | - echo 'No scheme ID passed. Aborting.'; |
|
| 675 | - } |
|
| 676 | - } |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - die(); // rolled a two. |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - /** |
|
| 683 | - * Gets the new scheme based on selection. |
|
| 684 | - * |
|
| 685 | - * @param ReduxFramework $redux ReduxFramework object. |
|
| 686 | - * |
|
| 687 | - * @return void |
|
| 688 | - * @since 1.0.0 |
|
| 689 | - * @access private |
|
| 690 | - */ |
|
| 691 | - private function get_scheme_html( ReduxFramework $redux ) { |
|
| 692 | - if ( isset( $_POST['scheme_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 693 | - |
|
| 694 | - // Get the selected scheme name. |
|
| 695 | - $scheme_id = sanitize_text_field( wp_unslash( $_POST['scheme_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 696 | - |
|
| 697 | - if ( isset( $_POST['field_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 698 | - |
|
| 699 | - // Get the field ID. |
|
| 700 | - $field_id = sanitize_text_field( wp_unslash( $_POST['field_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 701 | - |
|
| 702 | - // Get the field class. |
|
| 703 | - $field_class = isset( $_POST['field_class'] ) ? sanitize_text_field( wp_unslash( $_POST['field_class'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
|
| 704 | - |
|
| 705 | - Redux_Color_Scheme_Functions::$parent = $redux; |
|
| 706 | - |
|
| 707 | - // Set the updated field ID. |
|
| 708 | - Redux_Color_Scheme_Functions::$field_id = $field_id; |
|
| 709 | - |
|
| 710 | - // Set the updated field class. |
|
| 711 | - Redux_Color_Scheme_Functions::$field_class = $field_class; |
|
| 712 | - |
|
| 713 | - // Get the color picket layout HTML. |
|
| 714 | - $html = Redux_Color_Scheme_Functions::get_current_color_scheme_html( $scheme_id ); |
|
| 715 | - |
|
| 716 | - // Print! |
|
| 717 | - echo $html; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 718 | - } |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - die(); // another day. |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - |
|
| 725 | - /** |
|
| 726 | - * Retrieves an array of default data for color picker. |
|
| 727 | - * |
|
| 728 | - * @since 1.0.0 |
|
| 729 | - * @access private |
|
| 730 | - * @return array Default values from config. |
|
| 731 | - */ |
|
| 732 | - private function get_default_data(): array { |
|
| 733 | - $def_opts = $this->parent->options_defaults[ $this->field_id ]; |
|
| 734 | - |
|
| 735 | - if ( isset( $def_opts['color_scheme_name'] ) ) { |
|
| 736 | - return array(); |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - $sections = $this->parent->sections; |
|
| 740 | - $data = array(); |
|
| 741 | - |
|
| 742 | - foreach ( $sections as $arr ) { |
|
| 743 | - if ( isset( $arr['fields'] ) ) { |
|
| 744 | - foreach ( $arr['fields'] as $arr2 ) { |
|
| 745 | - if ( $arr2['id'] === $this->field_id ) { |
|
| 746 | - |
|
| 747 | - // Select fields. |
|
| 748 | - if ( isset( $arr2['select'] ) ) { |
|
| 749 | - foreach ( $arr2['select'] as $v ) { |
|
| 750 | - $data[] = array( |
|
| 751 | - 'id' => $v['id'], |
|
| 752 | - 'value' => $v['default'], |
|
| 753 | - 'type' => 'select', |
|
| 754 | - ); |
|
| 755 | - } |
|
| 756 | - } |
|
| 757 | - } |
|
| 758 | - } |
|
| 759 | - } |
|
| 760 | - } |
|
| 761 | - |
|
| 762 | - foreach ( $def_opts as $v ) { |
|
| 763 | - $title = $v['title'] ?? $v['id']; |
|
| 764 | - $color = $v['color'] ?? ''; |
|
| 765 | - $alpha = $v['alpha'] ?? 1; |
|
| 766 | - $grp = $v['group'] ?? ''; |
|
| 767 | - |
|
| 768 | - if ( '' === $color || 'transparent' === $color ) { |
|
| 769 | - $rgba = $this->output_transparent ? 'transparent' : ''; |
|
| 770 | - } else { |
|
| 771 | - $rgba = Redux_Helpers::hex2rgba( $color, $alpha ); |
|
| 772 | - } |
|
| 773 | - |
|
| 774 | - $data[] = array( |
|
| 775 | - 'id' => $v['id'], |
|
| 776 | - 'title' => $title, |
|
| 777 | - 'color' => $color, |
|
| 778 | - 'alpha' => $alpha, |
|
| 779 | - 'group' => $grp, |
|
| 780 | - 'rgba' => $rgba, |
|
| 781 | - ); |
|
| 782 | - } |
|
| 783 | - |
|
| 784 | - return $data; |
|
| 785 | - } |
|
| 786 | - } |
|
| 787 | - |
|
| 788 | - class_alias( Redux_Extension_Color_Scheme::class, 'ReduxFramework_Extension_Color_Scheme' ); |
|
| 226 | + /** |
|
| 227 | + * Reset section defaults. |
|
| 228 | + * |
|
| 229 | + * @param array $defaults Default values. |
|
| 230 | + * |
|
| 231 | + * @return array |
|
| 232 | + */ |
|
| 233 | + public function reset_defaults_section( array $defaults = array() ): array { |
|
| 234 | + if ( Redux_Helpers::is_field_in_use( $this->parent, 'color_scheme' ) ) { |
|
| 235 | + // Get the current tab/section number. |
|
| 236 | + if ( isset( $_COOKIE['redux_current_tab'] ) ) { |
|
| 237 | + $cur_tab = sanitize_text_field( wp_unslash( $_COOKIE['redux_current_tab'] ) ); |
|
| 238 | + |
|
| 239 | + // Get the tab/section number field is used on. |
|
| 240 | + $tab_num = $this->parent->field_sections['color_scheme'][ $this->field_id ]; |
|
| 241 | + |
|
| 242 | + // Match... |
|
| 243 | + if ( $cur_tab === $tab_num ) { |
|
| 244 | + |
|
| 245 | + // Reset data. |
|
| 246 | + $this->reset_all(); |
|
| 247 | + } |
|
| 248 | + $defaults[ $this->field_id ] = Redux_Color_Scheme_Functions::data_array_from_scheme( 'Default' ); |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + return $defaults; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Save Changes Hook. What to do when changes are saved |
|
| 257 | + * |
|
| 258 | + * @param array $saved_options Saved data. |
|
| 259 | + * @param array $old_options Previous data. |
|
| 260 | + * |
|
| 261 | + * @return array |
|
| 262 | + * @since 1.0.0 |
|
| 263 | + * @access public |
|
| 264 | + */ |
|
| 265 | + public function save_hook( array $saved_options = array(), array $old_options = array() ): array { |
|
| 266 | + if ( ! isset( $saved_options[ $this->field_id ] ) || empty( $saved_options[ $this->field_id ] ) || ( is_array( $saved_options[ $this->field_id ] ) && $old_options === $saved_options ) || ! array_key_exists( $this->field_id, $saved_options ) ) { |
|
| 267 | + return $saved_options; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + // We'll use the reset hook instead. |
|
| 271 | + if ( ! empty( $saved_options['defaults'] ) || ! empty( $saved_options['defaults-section'] ) ) { |
|
| 272 | + return $saved_options; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + $first_value = reset( $saved_options[ $this->field_id ] ); // First Element's Value. |
|
| 276 | + |
|
| 277 | + // Parse the JSON to an array. |
|
| 278 | + if ( isset( $first_value['data'] ) ) { |
|
| 279 | + |
|
| 280 | + Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 281 | + Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 282 | + |
|
| 283 | + Redux_Color_Scheme_Functions::set_current_scheme_id( $saved_options['redux-scheme-select'] ); |
|
| 284 | + |
|
| 285 | + // Get the current field ID. |
|
| 286 | + $raw_data = $saved_options[ $this->field_id ]; |
|
| 287 | + |
|
| 288 | + // Create a new array. |
|
| 289 | + $save_data = array(); |
|
| 290 | + |
|
| 291 | + // Enum through saved data. |
|
| 292 | + foreach ( $raw_data as $id => $val ) { |
|
| 293 | + |
|
| 294 | + if ( 'color_scheme_name' !== $id ) { |
|
| 295 | + if ( is_array( $val ) ) { |
|
| 296 | + |
|
| 297 | + if ( ! isset( $val['data'] ) ) { |
|
| 298 | + continue; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + $data = json_decode( rawurldecode( $val['data'] ), true ); |
|
| 302 | + |
|
| 303 | + // Sanitize everything. |
|
| 304 | + $color = $data['color'] ?? ''; |
|
| 305 | + $alpha = $data['alpha'] ?? 1; |
|
| 306 | + |
|
| 307 | + $id = $data['id'] ?? $id; |
|
| 308 | + $title = $data['title'] ?? $id; |
|
| 309 | + |
|
| 310 | + $grp = $data['group'] ?? ''; |
|
| 311 | + |
|
| 312 | + if ( '' === $color || 'transparent' === $color ) { |
|
| 313 | + $rgba = $this->output_transparent ? 'transparent' : ''; |
|
| 314 | + } else { |
|
| 315 | + $rgba = Redux_Helpers::hex2rgba( $color, $alpha ); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + // Create an array of saved data. |
|
| 319 | + $save_data[] = array( |
|
| 320 | + 'id' => $id, |
|
| 321 | + 'title' => $title, |
|
| 322 | + 'color' => $color, |
|
| 323 | + 'alpha' => $alpha, |
|
| 324 | + 'group' => $grp, |
|
| 325 | + 'rgba' => $rgba, |
|
| 326 | + ); |
|
| 327 | + } else { |
|
| 328 | + $save_data[] = array( |
|
| 329 | + 'id' => $id, |
|
| 330 | + 'value' => $val, |
|
| 331 | + 'type' => 'select', |
|
| 332 | + ); |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + $new_scheme = array(); |
|
| 338 | + |
|
| 339 | + $new_scheme['color_scheme_name'] = Redux_Color_Scheme_Functions::get_current_scheme_id(); |
|
| 340 | + |
|
| 341 | + // Enum through values and assign them to a new array. |
|
| 342 | + foreach ( $save_data as $val ) { |
|
| 343 | + if ( isset( $val['id'] ) ) { |
|
| 344 | + $new_scheme[ $val['id'] ] = $val; |
|
| 345 | + } |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + // Filter for DB save |
|
| 349 | + // Doesn't need to save select arrays to a database, |
|
| 350 | + // just the id => value. |
|
| 351 | + $database_data = $new_scheme; |
|
| 352 | + |
|
| 353 | + foreach ( $database_data as $k => $v ) { |
|
| 354 | + if ( isset( $v['type'] ) ) { |
|
| 355 | + $val = $v['value']; |
|
| 356 | + |
|
| 357 | + unset( $database_data[ $k ] ); |
|
| 358 | + |
|
| 359 | + $database_data[ $k ] = $val; |
|
| 360 | + } |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + $saved_options[ $this->field_id ] = $database_data; |
|
| 364 | + |
|
| 365 | + // Check if we should save this compared to the old data. |
|
| 366 | + $save_scheme = false; |
|
| 367 | + |
|
| 368 | + // Doesn't exist or is empty. |
|
| 369 | + if ( ! isset( $old_options[ $this->field_id ] ) || ( isset( $old_options[ $this->field_id ] ) && ! empty( $old_options[ $this->field_id ] ) ) ) { |
|
| 370 | + $save_scheme = true; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + // Isn't empty and isn't the same as the new array. |
|
| 374 | + if ( ! empty( $old_options[ $this->field_id ] ) && $saved_options[ $this->field_id ] !== $old_options[ $this->field_id ] ) { |
|
| 375 | + $save_scheme = true; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + if ( $save_scheme ) { |
|
| 379 | + $scheme = Redux_Color_Scheme_Functions::get_current_scheme_id(); |
|
| 380 | + Redux_Color_Scheme_Functions::set_scheme_data( $scheme, $save_data ); |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + return $saved_options; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Reset data. Restores colour picker to default values |
|
| 389 | + * |
|
| 390 | + * @since 1.0.0 |
|
| 391 | + * @access private |
|
| 392 | + * @return void |
|
| 393 | + */ |
|
| 394 | + private function reset_data() { |
|
| 395 | + Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 396 | + Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 397 | + |
|
| 398 | + // Get default data. |
|
| 399 | + $data = $this->get_default_data(); |
|
| 400 | + |
|
| 401 | + // Add to (and/or create) JSON scheme file. |
|
| 402 | + Redux_Color_Scheme_Functions::set_scheme_data( 'Default', $data ); |
|
| 403 | + |
|
| 404 | + // Set default scheme. |
|
| 405 | + Redux_Color_Scheme_Functions::set_current_scheme_id( 'Default' ); |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Reset All Hook. Todo list when all data is reset |
|
| 410 | + * |
|
| 411 | + * @return void |
|
| 412 | + * @since 1.0.0 |
|
| 413 | + * @access public |
|
| 414 | + */ |
|
| 415 | + public function reset_all() { |
|
| 416 | + if ( ! empty( $this->field_id ) && isset( $this->parent->options_defaults[ $this->field_id ] ) && ! empty( $this->parent->options_defaults[ $this->field_id ] ) ) { |
|
| 417 | + Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 418 | + Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 419 | + |
|
| 420 | + $this->reset_data(); |
|
| 421 | + } |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * AJAX evaluator. Determine course of action based on AJAX callback |
|
| 426 | + * |
|
| 427 | + * @since 1.0.0 |
|
| 428 | + * @access public |
|
| 429 | + * @return void |
|
| 430 | + */ |
|
| 431 | + public function parse_ajax() { |
|
| 432 | + if ( isset( $_REQUEST['nonce'] ) && isset( $_REQUEST['opt_name'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['nonce'] ) ), 'redux_' . sanitize_text_field( wp_unslash( $_REQUEST['opt_name'] ) ) . '_color_schemes' ) ) { |
|
| 433 | + $parent = $this->parent; |
|
| 434 | + |
|
| 435 | + // Do action. |
|
| 436 | + if ( isset( $_REQUEST['type'] ) ) { |
|
| 437 | + |
|
| 438 | + // Save scheme. |
|
| 439 | + if ( 'save' === $_REQUEST['type'] ) { |
|
| 440 | + $this->save_scheme( $parent ); |
|
| 441 | + |
|
| 442 | + // Delete scheme. |
|
| 443 | + } elseif ( 'delete' === $_REQUEST['type'] ) { |
|
| 444 | + $this->delete_scheme( $parent ); |
|
| 445 | + |
|
| 446 | + // Scheme change. |
|
| 447 | + } elseif ( 'update' === $_REQUEST['type'] ) { |
|
| 448 | + $this->get_scheme_html( $parent ); |
|
| 449 | + |
|
| 450 | + // Export scheme file. |
|
| 451 | + } elseif ( 'export' === $_REQUEST['type'] ) { |
|
| 452 | + $this->download_schemes(); |
|
| 453 | + |
|
| 454 | + // Import scheme file. |
|
| 455 | + } elseif ( 'import' === $_REQUEST['type'] ) { |
|
| 456 | + $this->import_schemes(); |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + } else { |
|
| 460 | + wp_die( esc_html__( 'Invalid Security Credentials. Please reload the page and try again.', 'redux-framework' ) ); |
|
| 461 | + } |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Download Scheme File. |
|
| 466 | + * |
|
| 467 | + * @since 4.4.18 |
|
| 468 | + * @access private |
|
| 469 | + * @return void |
|
| 470 | + */ |
|
| 471 | + private function import_schemes() { |
|
| 472 | + if ( isset( $_REQUEST['content'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 473 | + $content = wp_unslash( $_REQUEST['content'] ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
|
| 474 | + $content = is_array( $content ) ? array_map( 'stripslashes_deep', $content ) : stripslashes( $content ); |
|
| 475 | + $content = json_decode( $content, true ); |
|
| 476 | + |
|
| 477 | + if ( is_null( $content ) ) { |
|
| 478 | + $result = array( |
|
| 479 | + 'result' => false, |
|
| 480 | + 'data' => esc_html__( 'Import unsuccessful! Malformed JSON data detected.', 'redux-framework' ), |
|
| 481 | + ); |
|
| 482 | + |
|
| 483 | + $result = wp_json_encode( $result ); |
|
| 484 | + |
|
| 485 | + echo $result; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 486 | + |
|
| 487 | + die; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + if ( isset( $content['Default']['color_scheme_name'] ) ) { |
|
| 491 | + $content = wp_json_encode( $content ); |
|
| 492 | + |
|
| 493 | + $param_array = array( |
|
| 494 | + 'content' => $content, |
|
| 495 | + 'overwrite' => true, |
|
| 496 | + 'chmod' => FS_CHMOD_FILE, |
|
| 497 | + ); |
|
| 498 | + |
|
| 499 | + $import_file = Redux_Color_Scheme_Functions::$upload_dir . Redux_Color_Scheme_Functions::$parent->args['opt_name'] . '_' . Redux_Color_Scheme_Functions::$field_id . '.json'; |
|
| 500 | + |
|
| 501 | + if ( true === Redux_Core::$filesystem->execute( 'put_contents', $import_file, $param_array ) ) { |
|
| 502 | + $result = array( |
|
| 503 | + 'result' => true, |
|
| 504 | + // translators: %s = HTML content. |
|
| 505 | + 'data' => sprintf( esc_html__( 'Import successful! Click %s to refresh.', 'redux-framework' ), '<strong>' . esc_html__( 'OK', 'redux-framework' ) . '</strong>' ), |
|
| 506 | + ); |
|
| 507 | + } else { |
|
| 508 | + $result = array( |
|
| 509 | + 'result' => false, |
|
| 510 | + 'data' => esc_html__( 'Import unsuccessful! File permission error: Could not write import data to server.', 'redux-framework' ), |
|
| 511 | + ); |
|
| 512 | + } |
|
| 513 | + } else { |
|
| 514 | + $result = array( |
|
| 515 | + 'result' => false, |
|
| 516 | + 'data' => esc_html__( 'Import unsuccessful! The selected file is not a valid color scheme file.', 'redux-framework' ), |
|
| 517 | + ); |
|
| 518 | + } |
|
| 519 | + } else { |
|
| 520 | + $result = array( |
|
| 521 | + 'result' => false, |
|
| 522 | + 'data' => esc_html__( 'Import unsuccessful! No data detected in the import file.', 'redux-framework' ), |
|
| 523 | + ); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + $result = wp_json_encode( $result ); |
|
| 527 | + |
|
| 528 | + echo $result; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
| 529 | + |
|
| 530 | + die; |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * Download Scheme File. |
|
| 535 | + * |
|
| 536 | + * @since 1.0.0 |
|
| 537 | + * @access private |
|
| 538 | + * @return void |
|
| 539 | + */ |
|
| 540 | + private function download_schemes() { |
|
| 541 | + Redux_Color_Scheme_Functions::$parent = $this->parent; |
|
| 542 | + Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 543 | + |
|
| 544 | + // Read contents of scheme file. |
|
| 545 | + $content = Redux_Color_Scheme_Functions::read_scheme_file(); |
|
| 546 | + $content = wp_json_encode( $content ); |
|
| 547 | + |
|
| 548 | + // Set header info. |
|
| 549 | + header( 'Content-Description: File Transfer' ); |
|
| 550 | + header( 'Content-type: application/txt' ); |
|
| 551 | + header( 'Content-Disposition: attachment; filename="redux_schemes_' . $this->parent->args['opt_name'] . '_' . $this->field_id . '_' . gmdate( 'm-d-Y' ) . '.json"' ); |
|
| 552 | + header( 'Content-Transfer-Encoding: binary' ); |
|
| 553 | + header( 'Expires: 0' ); |
|
| 554 | + header( 'Cache-Control: must-revalidate' ); |
|
| 555 | + header( 'Pragma: public' ); |
|
| 556 | + |
|
| 557 | + // File download. |
|
| 558 | + echo $content; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 559 | + |
|
| 560 | + // 2B ~! 2B |
|
| 561 | + die; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + /** |
|
| 565 | + * Save Scheme. Saved an individual scheme to JSON scheme file. |
|
| 566 | + * |
|
| 567 | + * @param ReduxFramework $redux ReduxFramework object. |
|
| 568 | + * |
|
| 569 | + * @return void |
|
| 570 | + * @since 1.0.0 |
|
| 571 | + * @access private |
|
| 572 | + */ |
|
| 573 | + private function save_scheme( ReduxFramework $redux ) { |
|
| 574 | + Redux_Color_Scheme_Functions::$parent = $redux; |
|
| 575 | + Redux_Color_Scheme_Functions::$field_id = $this->field_id; |
|
| 576 | + |
|
| 577 | + // Get the scheme name. |
|
| 578 | + if ( isset( $_REQUEST['scheme_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 579 | + $scheme_name = sanitize_text_field( wp_unslash( $_REQUEST['scheme_name'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 580 | + |
|
| 581 | + // Check for duplicates. |
|
| 582 | + $names = Redux_Color_Scheme_Functions::get_scheme_names(); |
|
| 583 | + foreach ( $names as $name ) { |
|
| 584 | + $name = strtolower( $name ); |
|
| 585 | + $tmp_name = strtolower( $scheme_name ); |
|
| 586 | + |
|
| 587 | + if ( $name === $tmp_name ) { |
|
| 588 | + echo 'fail'; |
|
| 589 | + die(); |
|
| 590 | + } |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + // Get scheme data. |
|
| 594 | + if ( isset( $_REQUEST['scheme_data'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 595 | + $scheme_data = wp_unslash( $_REQUEST['scheme_data'] ); // phpcs:ignore WordPress.Security |
|
| 596 | + |
|
| 597 | + // Get field ID. |
|
| 598 | + if ( isset( $_REQUEST['field_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 599 | + $scheme_data = rawurldecode( $scheme_data ); |
|
| 600 | + $scheme_data = json_decode( $scheme_data, true ); |
|
| 601 | + |
|
| 602 | + // Save scheme to file. If successful... |
|
| 603 | + if ( true === Redux_Color_Scheme_Functions::set_scheme_data( $scheme_name, $scheme_data ) ) { |
|
| 604 | + |
|
| 605 | + // Update scheme selector. |
|
| 606 | + echo Redux_Color_Scheme_Functions::get_scheme_select_html( $scheme_name ); // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 607 | + } |
|
| 608 | + } |
|
| 609 | + } |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + die(); // a horrible death! |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Delete Scheme. Delete individual scheme from JSON scheme file. |
|
| 617 | + * |
|
| 618 | + * @param ReduxFramework $redux ReduxFramework object. |
|
| 619 | + * |
|
| 620 | + * @return void |
|
| 621 | + * @since 1.0.0 |
|
| 622 | + * @access private |
|
| 623 | + */ |
|
| 624 | + private function delete_scheme( ReduxFramework $redux ) { |
|
| 625 | + |
|
| 626 | + // Get deleted scheme ID. |
|
| 627 | + if ( isset( $_REQUEST['scheme_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 628 | + $scheme_id = sanitize_text_field( wp_unslash( $_REQUEST['scheme_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 629 | + |
|
| 630 | + // Get field ID. |
|
| 631 | + if ( isset( $_REQUEST['field_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 632 | + $field_id = sanitize_text_field( wp_unslash( $_REQUEST['field_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 633 | + |
|
| 634 | + // If scheme ID was passed (and why wouldn't it be?? Hmm??). |
|
| 635 | + if ( $scheme_id ) { |
|
| 636 | + Redux_Color_Scheme_Functions::$field_id = $field_id; |
|
| 637 | + Redux_Color_Scheme_Functions::$parent = $redux; |
|
| 638 | + |
|
| 639 | + // Get the entire scheme file. |
|
| 640 | + $schemes = Redux_Color_Scheme_Functions::read_scheme_file(); |
|
| 641 | + |
|
| 642 | + // If we got a good read... |
|
| 643 | + if ( false !== $schemes ) { |
|
| 644 | + |
|
| 645 | + // If scheme name exists... |
|
| 646 | + if ( isset( $schemes[ $scheme_id ] ) ) { |
|
| 647 | + |
|
| 648 | + // Unset it. |
|
| 649 | + unset( $schemes[ $scheme_id ] ); |
|
| 650 | + |
|
| 651 | + // Save the scheme data, minus the deleted scheme. Upon success... |
|
| 652 | + if ( true === Redux_Color_Scheme_Functions::write_scheme_file( $schemes ) ) { |
|
| 653 | + |
|
| 654 | + // Set default scheme. |
|
| 655 | + Redux_Color_Scheme_Functions::set_current_scheme_id( 'Default' ); |
|
| 656 | + |
|
| 657 | + // Update field ID. |
|
| 658 | + Redux_Color_Scheme_Functions::$field_id = $field_id; |
|
| 659 | + |
|
| 660 | + // Meh TODO. |
|
| 661 | + Redux_Color_Scheme_Functions::set_database_data(); |
|
| 662 | + |
|
| 663 | + echo 'success'; |
|
| 664 | + } else { |
|
| 665 | + echo 'Failed to write JSON file to server.'; |
|
| 666 | + } |
|
| 667 | + } else { |
|
| 668 | + echo 'Scheme name does not exist in JSON string. Aborting.'; |
|
| 669 | + } |
|
| 670 | + } else { |
|
| 671 | + echo 'Failed to read JSON scheme file, or file is empty.'; |
|
| 672 | + } |
|
| 673 | + } else { |
|
| 674 | + echo 'No scheme ID passed. Aborting.'; |
|
| 675 | + } |
|
| 676 | + } |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + die(); // rolled a two. |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + /** |
|
| 683 | + * Gets the new scheme based on selection. |
|
| 684 | + * |
|
| 685 | + * @param ReduxFramework $redux ReduxFramework object. |
|
| 686 | + * |
|
| 687 | + * @return void |
|
| 688 | + * @since 1.0.0 |
|
| 689 | + * @access private |
|
| 690 | + */ |
|
| 691 | + private function get_scheme_html( ReduxFramework $redux ) { |
|
| 692 | + if ( isset( $_POST['scheme_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 693 | + |
|
| 694 | + // Get the selected scheme name. |
|
| 695 | + $scheme_id = sanitize_text_field( wp_unslash( $_POST['scheme_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 696 | + |
|
| 697 | + if ( isset( $_POST['field_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 698 | + |
|
| 699 | + // Get the field ID. |
|
| 700 | + $field_id = sanitize_text_field( wp_unslash( $_POST['field_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 701 | + |
|
| 702 | + // Get the field class. |
|
| 703 | + $field_class = isset( $_POST['field_class'] ) ? sanitize_text_field( wp_unslash( $_POST['field_class'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
|
| 704 | + |
|
| 705 | + Redux_Color_Scheme_Functions::$parent = $redux; |
|
| 706 | + |
|
| 707 | + // Set the updated field ID. |
|
| 708 | + Redux_Color_Scheme_Functions::$field_id = $field_id; |
|
| 709 | + |
|
| 710 | + // Set the updated field class. |
|
| 711 | + Redux_Color_Scheme_Functions::$field_class = $field_class; |
|
| 712 | + |
|
| 713 | + // Get the color picket layout HTML. |
|
| 714 | + $html = Redux_Color_Scheme_Functions::get_current_color_scheme_html( $scheme_id ); |
|
| 715 | + |
|
| 716 | + // Print! |
|
| 717 | + echo $html; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 718 | + } |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + die(); // another day. |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + |
|
| 725 | + /** |
|
| 726 | + * Retrieves an array of default data for color picker. |
|
| 727 | + * |
|
| 728 | + * @since 1.0.0 |
|
| 729 | + * @access private |
|
| 730 | + * @return array Default values from config. |
|
| 731 | + */ |
|
| 732 | + private function get_default_data(): array { |
|
| 733 | + $def_opts = $this->parent->options_defaults[ $this->field_id ]; |
|
| 734 | + |
|
| 735 | + if ( isset( $def_opts['color_scheme_name'] ) ) { |
|
| 736 | + return array(); |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + $sections = $this->parent->sections; |
|
| 740 | + $data = array(); |
|
| 741 | + |
|
| 742 | + foreach ( $sections as $arr ) { |
|
| 743 | + if ( isset( $arr['fields'] ) ) { |
|
| 744 | + foreach ( $arr['fields'] as $arr2 ) { |
|
| 745 | + if ( $arr2['id'] === $this->field_id ) { |
|
| 746 | + |
|
| 747 | + // Select fields. |
|
| 748 | + if ( isset( $arr2['select'] ) ) { |
|
| 749 | + foreach ( $arr2['select'] as $v ) { |
|
| 750 | + $data[] = array( |
|
| 751 | + 'id' => $v['id'], |
|
| 752 | + 'value' => $v['default'], |
|
| 753 | + 'type' => 'select', |
|
| 754 | + ); |
|
| 755 | + } |
|
| 756 | + } |
|
| 757 | + } |
|
| 758 | + } |
|
| 759 | + } |
|
| 760 | + } |
|
| 761 | + |
|
| 762 | + foreach ( $def_opts as $v ) { |
|
| 763 | + $title = $v['title'] ?? $v['id']; |
|
| 764 | + $color = $v['color'] ?? ''; |
|
| 765 | + $alpha = $v['alpha'] ?? 1; |
|
| 766 | + $grp = $v['group'] ?? ''; |
|
| 767 | + |
|
| 768 | + if ( '' === $color || 'transparent' === $color ) { |
|
| 769 | + $rgba = $this->output_transparent ? 'transparent' : ''; |
|
| 770 | + } else { |
|
| 771 | + $rgba = Redux_Helpers::hex2rgba( $color, $alpha ); |
|
| 772 | + } |
|
| 773 | + |
|
| 774 | + $data[] = array( |
|
| 775 | + 'id' => $v['id'], |
|
| 776 | + 'title' => $title, |
|
| 777 | + 'color' => $color, |
|
| 778 | + 'alpha' => $alpha, |
|
| 779 | + 'group' => $grp, |
|
| 780 | + 'rgba' => $rgba, |
|
| 781 | + ); |
|
| 782 | + } |
|
| 783 | + |
|
| 784 | + return $data; |
|
| 785 | + } |
|
| 786 | + } |
|
| 787 | + |
|
| 788 | + class_alias( Redux_Extension_Color_Scheme::class, 'ReduxFramework_Extension_Color_Scheme' ); |
|
| 789 | 789 | } |
@@ -271,8 +271,8 @@ discard block |
||
| 271 | 271 | if ( $add_field || ( ( is_admin() && in_array( $pagenow, $this->pagenows, true ) ) || ( ! is_admin() ) ) ) { |
| 272 | 272 | $run_hooks = true; |
| 273 | 273 | |
| 274 | - $this->meta[ $this->tag_id ] = Redux_Taxonomy::get_term_meta( array( 'taxonomy' => $this->tag_id ) ); |
|
| 275 | - $this->parent->options = array_merge( $this->parent->options, $this->meta[ $this->tag_id ] ); |
|
| 274 | + $this->meta[$this->tag_id] = Redux_Taxonomy::get_term_meta( array( 'taxonomy' => $this->tag_id ) ); |
|
| 275 | + $this->parent->options = array_merge( $this->parent->options, $this->meta[$this->tag_id] ); |
|
| 276 | 276 | |
| 277 | 277 | foreach ( $term['sections'] as $sk => $section ) { |
| 278 | 278 | if ( ! empty( $section['fields'] ) ) { |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | if ( ! isset( $field['class'] ) ) { |
| 281 | 281 | $field['class'] = ''; |
| 282 | 282 | |
| 283 | - $this->terms[ $bk ]['sections'][ $sk ]['fields'][ $fk ] = $field; |
|
| 283 | + $this->terms[$bk]['sections'][$sk]['fields'][$fk] = $field; |
|
| 284 | 284 | } |
| 285 | 285 | |
| 286 | 286 | $this->parent->required_class->check_dependencies( $field ); |
@@ -291,37 +291,37 @@ discard block |
||
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | if ( isset( $field['default'] ) ) { |
| 294 | - $this->options_defaults[ $field['id'] ] = $field['default']; |
|
| 294 | + $this->options_defaults[$field['id']] = $field['default']; |
|
| 295 | 295 | } else { |
| 296 | - $this->options_defaults[ $field['id'] ] = $this->field_default( $field ); |
|
| 296 | + $this->options_defaults[$field['id']] = $this->field_default( $field ); |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | foreach ( $term['taxonomy_types'] as $type ) { |
| 300 | - $this->taxonomy_type_fields[ $type ][ $field['id'] ] = 1; |
|
| 300 | + $this->taxonomy_type_fields[$type][$field['id']] = 1; |
|
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | if ( ! empty( $field['output'] ) ) { |
| 304 | - $this->output[ $field['id'] ] = isset( $this->output[ $field['id'] ] ) ? array_merge( $field['output'], $this->output[ $field['id'] ] ) : $field['output']; |
|
| 304 | + $this->output[$field['id']] = isset( $this->output[$field['id']] ) ? array_merge( $field['output'], $this->output[$field['id']] ) : $field['output']; |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | // Detect what field types are being used. |
| 308 | - if ( ! isset( $this->parent->fields[ $field['type'] ][ $field['id'] ] ) ) { |
|
| 309 | - $this->parent->fields[ $field['type'] ][ $field['id'] ] = 1; |
|
| 308 | + if ( ! isset( $this->parent->fields[$field['type']][$field['id']] ) ) { |
|
| 309 | + $this->parent->fields[$field['type']][$field['id']] = 1; |
|
| 310 | 310 | } else { |
| 311 | - $this->parent->fields[ $field['type'] ] = array( $field['id'] => 1 ); |
|
| 311 | + $this->parent->fields[$field['type']] = array( $field['id'] => 1 ); |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | - if ( isset( $this->options_defaults[ $field['id'] ] ) ) { |
|
| 315 | - $this->to_replace[ $field['id'] ] = $field; |
|
| 314 | + if ( isset( $this->options_defaults[$field['id']] ) ) { |
|
| 315 | + $this->to_replace[$field['id']] = $field; |
|
| 316 | 316 | } |
| 317 | 317 | } |
| 318 | 318 | |
| 319 | - if ( ! isset( $this->parent->options[ $field['id'] ] ) ) { |
|
| 320 | - $this->parent->sections[ ( count( $this->parent->sections ) - 1 ) ]['fields'][] = $field; |
|
| 319 | + if ( ! isset( $this->parent->options[$field['id']] ) ) { |
|
| 320 | + $this->parent->sections[( count( $this->parent->sections ) - 1 )]['fields'][] = $field; |
|
| 321 | 321 | } |
| 322 | 322 | |
| 323 | - if ( ! isset( $this->meta[ $this->tag_id ][ $field['id'] ] ) ) { |
|
| 324 | - $this->meta[ $this->tag_id ][ $field['id'] ] = $this->options_defaults[ $field['id'] ]; |
|
| 323 | + if ( ! isset( $this->meta[$this->tag_id][$field['id']] ) ) { |
|
| 324 | + $this->meta[$this->tag_id][$field['id']] = $this->options_defaults[$field['id']]; |
|
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | // Only override if it exists, and it's not the default. |
@@ -369,8 +369,8 @@ discard block |
||
| 369 | 369 | * @return mixed |
| 370 | 370 | */ |
| 371 | 371 | public function replace_field( array $field ) { |
| 372 | - if ( isset( $this->to_replace[ $field['id'] ] ) ) { |
|
| 373 | - $field = $this->to_replace[ $field['id'] ]; |
|
| 372 | + if ( isset( $this->to_replace[$field['id']] ) ) { |
|
| 373 | + $field = $this->to_replace[$field['id']]; |
|
| 374 | 374 | } |
| 375 | 375 | |
| 376 | 376 | return $field; |
@@ -384,7 +384,7 @@ discard block |
||
| 384 | 384 | * @return array |
| 385 | 385 | */ |
| 386 | 386 | public function override_can_output_css( array $field ): array { |
| 387 | - if ( isset( $this->output[ $field['id'] ] ) ) { |
|
| 387 | + if ( isset( $this->output[$field['id']] ) ) { |
|
| 388 | 388 | $field['force_output'] = true; |
| 389 | 389 | } |
| 390 | 390 | |
@@ -399,8 +399,8 @@ discard block |
||
| 399 | 399 | * @return array |
| 400 | 400 | */ |
| 401 | 401 | public function output_css( array $field ): array { |
| 402 | - if ( isset( $this->output[ $field['id'] ] ) ) { |
|
| 403 | - $field['output'] = $this->output[ $field['id'] ]; |
|
| 402 | + if ( isset( $this->output[$field['id']] ) ) { |
|
| 403 | + $field['output'] = $this->output[$field['id']]; |
|
| 404 | 404 | } |
| 405 | 405 | |
| 406 | 406 | return $field; |
@@ -421,13 +421,13 @@ discard block |
||
| 421 | 421 | $data = wp_parse_args( $meta, $this->options_defaults ); |
| 422 | 422 | |
| 423 | 423 | foreach ( $data as $key => $value ) { |
| 424 | - if ( isset( $meta[ $key ] ) && '' !== $meta[ $key ] ) { |
|
| 425 | - $data[ $key ] = $meta[ $key ]; |
|
| 424 | + if ( isset( $meta[$key] ) && '' !== $meta[$key] ) { |
|
| 425 | + $data[$key] = $meta[$key]; |
|
| 426 | 426 | continue; |
| 427 | 427 | } |
| 428 | 428 | |
| 429 | - if ( isset( $options[ $key ] ) ) { |
|
| 430 | - $data[ $key ] = $options[ $key ]; |
|
| 429 | + if ( isset( $options[$key] ) ) { |
|
| 430 | + $data[$key] = $options[$key]; |
|
| 431 | 431 | } |
| 432 | 432 | } |
| 433 | 433 | |
@@ -460,7 +460,7 @@ discard block |
||
| 460 | 460 | if ( ! empty( $term['taxonomy_types'] ) ) { |
| 461 | 461 | if ( ! is_array( $term['taxonomy_types'] ) ) { |
| 462 | 462 | $term['taxonomy_types'] = array( $term['taxonomy_types'] ); |
| 463 | - $this->terms[ $key ]['taxonomy_types'] = $term['taxonomy_types']; |
|
| 463 | + $this->terms[$key]['taxonomy_types'] = $term['taxonomy_types']; |
|
| 464 | 464 | } |
| 465 | 465 | } |
| 466 | 466 | } |
@@ -532,7 +532,7 @@ discard block |
||
| 532 | 532 | } else { |
| 533 | 533 | $section['id'] = sanitize_title( $section['title'], $sk ); |
| 534 | 534 | } |
| 535 | - $this->terms[ $key ]['sections'][ $sk ] = $section; |
|
| 535 | + $this->terms[$key]['sections'][$sk] = $section; |
|
| 536 | 536 | } |
| 537 | 537 | if ( isset( $section['fields'] ) ) { |
| 538 | 538 | foreach ( $section['fields'] as $k => $field ) { |
@@ -541,14 +541,14 @@ discard block |
||
| 541 | 541 | } |
| 542 | 542 | |
| 543 | 543 | if ( 'ace_editor' === $field['type'] && isset( $field['options'] ) ) { |
| 544 | - $this->terms[ $key ]['sections'][ $sk ]['fields'][ $k ]['args'] = $field['options']; |
|
| 545 | - unset( $this->terms[ $key ]['sections'][ $sk ]['fields'][ $k ]['options'] ); |
|
| 544 | + $this->terms[$key]['sections'][$sk]['fields'][$k]['args'] = $field['options']; |
|
| 545 | + unset( $this->terms[$key]['sections'][$sk]['fields'][$k]['options'] ); |
|
| 546 | 546 | } |
| 547 | 547 | |
| 548 | 548 | if ( 'section' === $field['type'] && isset( $field['indent'] ) && true === $field['indent'] ) { |
| 549 | 549 | $field['class'] = $field['class'] ?? ''; |
| 550 | 550 | $field['class'] .= 'redux-section-indent-start'; |
| 551 | - $this->terms[ $key ]['sections'][ $sk ]['fields'][ $k ] = $field; |
|
| 551 | + $this->terms[$key]['sections'][$sk]['fields'][$k] = $field; |
|
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | if ( ! isset( $this->parent->options_defaults_class ) ) { |
@@ -570,8 +570,8 @@ discard block |
||
| 570 | 570 | } |
| 571 | 571 | } |
| 572 | 572 | |
| 573 | - if ( empty( $this->meta[ $this->tag_id ] ) ) { |
|
| 574 | - $this->meta[ $this->tag_id ] = $this->get_meta( $this->tag_id ); |
|
| 573 | + if ( empty( $this->meta[$this->tag_id] ) ) { |
|
| 574 | + $this->meta[$this->tag_id] = $this->get_meta( $this->tag_id ); |
|
| 575 | 575 | } |
| 576 | 576 | } |
| 577 | 577 | |
@@ -662,8 +662,8 @@ discard block |
||
| 662 | 662 | $this->parent->options_class->get(); |
| 663 | 663 | } |
| 664 | 664 | |
| 665 | - if ( isset( $this->parent->options[ $field_id['id'] ] ) && isset( $this->parent->options_defaults[ $field_id['id'] ] ) && $this->parent->options[ $field_id['id'] ] !== $this->parent->options_defaults[ $field_id['id'] ] ) { |
|
| 666 | - return $this->parent->options[ $field_id['id'] ]; |
|
| 665 | + if ( isset( $this->parent->options[$field_id['id']] ) && isset( $this->parent->options_defaults[$field_id['id']] ) && $this->parent->options[$field_id['id']] !== $this->parent->options_defaults[$field_id['id']] ) { |
|
| 666 | + return $this->parent->options[$field_id['id']]; |
|
| 667 | 667 | } else { |
| 668 | 668 | if ( empty( $this->options_defaults ) ) { |
| 669 | 669 | $this->default_values(); // fill cache. |
@@ -671,11 +671,11 @@ discard block |
||
| 671 | 671 | |
| 672 | 672 | $data = ''; |
| 673 | 673 | if ( ! empty( $this->options_defaults ) ) { |
| 674 | - $data = $this->options_defaults[ $field_id['id'] ] ?? ''; |
|
| 674 | + $data = $this->options_defaults[$field_id['id']] ?? ''; |
|
| 675 | 675 | } |
| 676 | 676 | |
| 677 | - if ( empty( $data ) && isset( $this->parent->options_defaults[ $field_id['id'] ] ) ) { |
|
| 678 | - $data = $this->parent->options_defaults[ $field_id['id'] ] ?? ''; |
|
| 677 | + if ( empty( $data ) && isset( $this->parent->options_defaults[$field_id['id']] ) ) { |
|
| 678 | + $data = $this->parent->options_defaults[$field_id['id']] ?? ''; |
|
| 679 | 679 | } |
| 680 | 680 | |
| 681 | 681 | return $data; |
@@ -690,8 +690,8 @@ discard block |
||
| 690 | 690 | * @return array |
| 691 | 691 | */ |
| 692 | 692 | private function get_meta( string $id ): array { |
| 693 | - if ( ! isset( $this->meta[ $id ] ) ) { |
|
| 694 | - $this->meta[ $id ] = array(); |
|
| 693 | + if ( ! isset( $this->meta[$id] ) ) { |
|
| 694 | + $this->meta[$id] = array(); |
|
| 695 | 695 | $o_data = get_post_meta( $id ); |
| 696 | 696 | |
| 697 | 697 | // phpcs:ignore WordPress.NamingConventions.ValidHookName |
@@ -700,30 +700,30 @@ discard block |
||
| 700 | 700 | if ( ! empty( $o_data ) ) { |
| 701 | 701 | foreach ( $o_data as $key => $value ) { |
| 702 | 702 | if ( count( $value ) === 1 ) { |
| 703 | - $this->meta[ $id ][ $key ] = maybe_unserialize( $value[0] ); |
|
| 703 | + $this->meta[$id][$key] = maybe_unserialize( $value[0] ); |
|
| 704 | 704 | } else { |
| 705 | 705 | $new_value = array_map( 'maybe_unserialize', $value ); |
| 706 | 706 | |
| 707 | - $this->meta[ $id ][ $key ] = $new_value[0]; |
|
| 707 | + $this->meta[$id][$key] = $new_value[0]; |
|
| 708 | 708 | } |
| 709 | 709 | } |
| 710 | 710 | } |
| 711 | 711 | |
| 712 | - if ( isset( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ) ) { |
|
| 713 | - $data = maybe_unserialize( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ); |
|
| 712 | + if ( isset( $this->meta[$id][$this->parent->args['opt_name']] ) ) { |
|
| 713 | + $data = maybe_unserialize( $this->meta[$id][$this->parent->args['opt_name']] ); |
|
| 714 | 714 | |
| 715 | 715 | foreach ( $data as $key => $value ) { |
| 716 | - $this->meta[ $id ][ $key ] = $value; |
|
| 716 | + $this->meta[$id][$key] = $value; |
|
| 717 | 717 | update_post_meta( $id, $key, $value ); |
| 718 | 718 | } |
| 719 | 719 | |
| 720 | - unset( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ); |
|
| 720 | + unset( $this->meta[$id][$this->parent->args['opt_name']] ); |
|
| 721 | 721 | |
| 722 | 722 | delete_post_meta( $id, $this->parent->args['opt_name'] ); |
| 723 | 723 | } |
| 724 | 724 | } |
| 725 | 725 | |
| 726 | - return $this->meta[ $id ]; |
|
| 726 | + return $this->meta[$id]; |
|
| 727 | 727 | } |
| 728 | 728 | |
| 729 | 729 | /** |
@@ -739,29 +739,29 @@ discard block |
||
| 739 | 739 | |
| 740 | 740 | // Override these values if they differ from the admin panel defaults. |
| 741 | 741 | if ( isset( $the_post->taxonomy_type ) && in_array( $the_post->taxonomy_type, $this->taxonomy_types, true ) ) { |
| 742 | - if ( isset( $this->taxonomy_type_values[ $the_post->taxonomy_type ] ) ) { |
|
| 743 | - $meta = $this->taxonomy_type_fields[ $the_post->taxonomy_type ]; |
|
| 742 | + if ( isset( $this->taxonomy_type_values[$the_post->taxonomy_type] ) ) { |
|
| 743 | + $meta = $this->taxonomy_type_fields[$the_post->taxonomy_type]; |
|
| 744 | 744 | } else { |
| 745 | 745 | $defaults = array(); |
| 746 | - if ( ! empty( $this->taxonomy_type_fields[ $the_post->taxonomy_type ] ) ) { |
|
| 747 | - foreach ( $this->taxonomy_type_fields[ $the_post->taxonomy_type ] as $key => $null ) { |
|
| 748 | - if ( isset( $this->options_defaults[ $key ] ) ) { |
|
| 749 | - $defaults[ $key ] = $this->options_defaults[ $key ]; |
|
| 746 | + if ( ! empty( $this->taxonomy_type_fields[$the_post->taxonomy_type] ) ) { |
|
| 747 | + foreach ( $this->taxonomy_type_fields[$the_post->taxonomy_type] as $key => $null ) { |
|
| 748 | + if ( isset( $this->options_defaults[$key] ) ) { |
|
| 749 | + $defaults[$key] = $this->options_defaults[$key]; |
|
| 750 | 750 | } |
| 751 | 751 | } |
| 752 | 752 | } |
| 753 | 753 | |
| 754 | 754 | $meta = wp_parse_args( $this->get_meta( $the_post->ID ), $defaults ); |
| 755 | 755 | |
| 756 | - $this->taxonomy_type_fields[ $the_post->taxonomy_type ] = $meta; |
|
| 756 | + $this->taxonomy_type_fields[$the_post->taxonomy_type] = $meta; |
|
| 757 | 757 | } |
| 758 | 758 | |
| 759 | 759 | if ( ! empty( $meta_key ) ) { |
| 760 | - if ( ! isset( $meta[ $meta_key ] ) ) { |
|
| 761 | - $meta[ $meta_key ] = $def_val; |
|
| 760 | + if ( ! isset( $meta[$meta_key] ) ) { |
|
| 761 | + $meta[$meta_key] = $def_val; |
|
| 762 | 762 | } |
| 763 | 763 | |
| 764 | - return $meta[ $meta_key ]; |
|
| 764 | + return $meta[$meta_key]; |
|
| 765 | 765 | } else { |
| 766 | 766 | return $meta; |
| 767 | 767 | } |
@@ -960,11 +960,11 @@ discard block |
||
| 960 | 960 | $field['class'] .= 'redux-section-indent-start'; |
| 961 | 961 | } |
| 962 | 962 | |
| 963 | - if ( ! isset( $this->meta[ $this->tag_id ][ $field['id'] ] ) ) { |
|
| 964 | - $this->meta[ $this->tag_id ][ $field['id'] ] = ''; |
|
| 963 | + if ( ! isset( $this->meta[$this->tag_id][$field['id']] ) ) { |
|
| 964 | + $this->meta[$this->tag_id][$field['id']] = ''; |
|
| 965 | 965 | } |
| 966 | 966 | |
| 967 | - $this->parent->render_class->field_input( $field, $this->meta[ $this->tag_id ][ $field['id'] ], true ); |
|
| 967 | + $this->parent->render_class->field_input( $field, $this->meta[$this->tag_id][$field['id']], true ); |
|
| 968 | 968 | echo '</td></tr>'; |
| 969 | 969 | } |
| 970 | 970 | echo '</tbody></table>'; |
@@ -992,7 +992,7 @@ discard block |
||
| 992 | 992 | public function meta_terms_save( $tag_id ): bool { |
| 993 | 993 | |
| 994 | 994 | // Check if our nonce is set. |
| 995 | - if ( ! isset( $_POST['redux_taxonomy_meta_nonce'] ) || ! isset( $_POST[ $this->parent->args['opt_name'] ] ) ) { |
|
| 995 | + if ( ! isset( $_POST['redux_taxonomy_meta_nonce'] ) || ! isset( $_POST[$this->parent->args['opt_name']] ) ) { |
|
| 996 | 996 | return false; |
| 997 | 997 | } |
| 998 | 998 | |
@@ -1009,16 +1009,16 @@ discard block |
||
| 1009 | 1009 | $to_delete = array(); |
| 1010 | 1010 | $dont_save = true; |
| 1011 | 1011 | |
| 1012 | - $field_args = Redux_Taxonomy::$fields[ $this->parent->args['opt_name'] ]; |
|
| 1012 | + $field_args = Redux_Taxonomy::$fields[$this->parent->args['opt_name']]; |
|
| 1013 | 1013 | |
| 1014 | - foreach ( Redux_Helpers::sanitize_array( wp_unslash( $_POST[ $this->parent->args['opt_name'] ] ) ) as $key => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
|
| 1014 | + foreach ( Redux_Helpers::sanitize_array( wp_unslash( $_POST[$this->parent->args['opt_name']] ) ) as $key => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
|
| 1015 | 1015 | |
| 1016 | 1016 | // Do not save anything the user doesn't have permissions for. |
| 1017 | - if ( ! empty( $field_args[ $key ]['permissions'] ) ) { |
|
| 1018 | - foreach ( (array) $field_args[ $key ]['permissions'] as $pv ) { |
|
| 1017 | + if ( ! empty( $field_args[$key]['permissions'] ) ) { |
|
| 1018 | + foreach ( ( array ) $field_args[$key]['permissions'] as $pv ) { |
|
| 1019 | 1019 | |
| 1020 | 1020 | // Do not save anything the user doesn't have permissions for. |
| 1021 | - if ( isset( $field_args[ $key ] ) ) { |
|
| 1021 | + if ( isset( $field_args[$key] ) ) { |
|
| 1022 | 1022 | if ( user_can( get_current_user_id(), $pv ) ) { |
| 1023 | 1023 | break; |
| 1024 | 1024 | } |
@@ -1030,7 +1030,7 @@ discard block |
||
| 1030 | 1030 | if ( is_array( $value ) ) { |
| 1031 | 1031 | foreach ( $value as $k => $v ) { |
| 1032 | 1032 | if ( ! is_array( $v ) ) { |
| 1033 | - $value[ $k ] = stripslashes( $v ); |
|
| 1033 | + $value[$k] = stripslashes( $v ); |
|
| 1034 | 1034 | } |
| 1035 | 1035 | } |
| 1036 | 1036 | } |
@@ -1038,19 +1038,19 @@ discard block |
||
| 1038 | 1038 | $save = true; |
| 1039 | 1039 | |
| 1040 | 1040 | // parent_options. |
| 1041 | - if ( ! $dont_save && isset( $this->options_defaults[ $key ] ) && $value === $this->options_defaults[ $key ] ) { |
|
| 1041 | + if ( ! $dont_save && isset( $this->options_defaults[$key] ) && $value === $this->options_defaults[$key] ) { |
|
| 1042 | 1042 | $save = false; |
| 1043 | 1043 | } |
| 1044 | 1044 | |
| 1045 | - if ( $save && isset( $this->parent_options[ $key ] ) && $this->parent_options[ $key ] !== $value ) { |
|
| 1045 | + if ( $save && isset( $this->parent_options[$key] ) && $this->parent_options[$key] !== $value ) { |
|
| 1046 | 1046 | $save = false; |
| 1047 | 1047 | } |
| 1048 | 1048 | |
| 1049 | 1049 | if ( $save ) { |
| 1050 | - $to_save[ $key ] = $value; |
|
| 1051 | - $to_compare[ $key ] = $this->parent->options[ $key ] ?? ''; |
|
| 1050 | + $to_save[$key] = $value; |
|
| 1051 | + $to_compare[$key] = $this->parent->options[$key] ?? ''; |
|
| 1052 | 1052 | } else { |
| 1053 | - $to_delete[ $key ] = $value; |
|
| 1053 | + $to_delete[$key] = $value; |
|
| 1054 | 1054 | } |
| 1055 | 1055 | } |
| 1056 | 1056 | |
@@ -1061,11 +1061,11 @@ discard block |
||
| 1061 | 1061 | |
| 1062 | 1062 | // Validate fields (if needed). |
| 1063 | 1063 | foreach ( $to_save as $key => $value ) { |
| 1064 | - if ( isset( $validate[ $key ] ) && $validate[ $key ] !== $value ) { |
|
| 1065 | - if ( isset( $meta[ $key ] ) && $validate[ $key ] === $meta[ $key ] ) { |
|
| 1066 | - unset( $to_save[ $key ] ); |
|
| 1064 | + if ( isset( $validate[$key] ) && $validate[$key] !== $value ) { |
|
| 1065 | + if ( isset( $meta[$key] ) && $validate[$key] === $meta[$key] ) { |
|
| 1066 | + unset( $to_save[$key] ); |
|
| 1067 | 1067 | } else { |
| 1068 | - $to_save[ $key ] = $validate[ $key ]; |
|
| 1068 | + $to_save[$key] = $validate[$key]; |
|
| 1069 | 1069 | } |
| 1070 | 1070 | } |
| 1071 | 1071 | } |
@@ -1091,27 +1091,27 @@ discard block |
||
| 1091 | 1091 | |
| 1092 | 1092 | $post_tax = isset( $_POST['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ) : ''; |
| 1093 | 1093 | |
| 1094 | - $check = $this->taxonomy_type_fields[ $post_tax ] ?? array(); |
|
| 1094 | + $check = $this->taxonomy_type_fields[$post_tax] ?? array(); |
|
| 1095 | 1095 | |
| 1096 | 1096 | // phpcs:ignore WordPress.NamingConventions.ValidHookName |
| 1097 | 1097 | $to_save = apply_filters( 'redux/taxonomy/save', $to_save, $to_compare, $this->sections ); |
| 1098 | 1098 | |
| 1099 | 1099 | foreach ( $to_save as $key => $value ) { |
| 1100 | - $prev_value = $this->meta[ $tag_id ][ $key ] ?? ''; |
|
| 1100 | + $prev_value = $this->meta[$tag_id][$key] ?? ''; |
|
| 1101 | 1101 | |
| 1102 | - if ( isset( $check[ $key ] ) ) { |
|
| 1103 | - unset( $check[ $key ] ); |
|
| 1102 | + if ( isset( $check[$key] ) ) { |
|
| 1103 | + unset( $check[$key] ); |
|
| 1104 | 1104 | } |
| 1105 | 1105 | |
| 1106 | 1106 | update_term_meta( $tag_id, $key, $value, $prev_value ); |
| 1107 | 1107 | } |
| 1108 | 1108 | |
| 1109 | 1109 | foreach ( $to_delete as $key => $value ) { |
| 1110 | - if ( isset( $check[ $key ] ) ) { |
|
| 1111 | - unset( $check[ $key ] ); |
|
| 1110 | + if ( isset( $check[$key] ) ) { |
|
| 1111 | + unset( $check[$key] ); |
|
| 1112 | 1112 | } |
| 1113 | 1113 | |
| 1114 | - $prev_value = $this->meta[ $tag_id ][ $key ] ?? ''; |
|
| 1114 | + $prev_value = $this->meta[$tag_id][$key] ?? ''; |
|
| 1115 | 1115 | delete_term_meta( $tag_id, $key, $prev_value ); |
| 1116 | 1116 | } |
| 1117 | 1117 | |
@@ -14,834 +14,834 @@ discard block |
||
| 14 | 14 | // Don't duplicate me! |
| 15 | 15 | if ( ! class_exists( 'Redux_Extension_Taxonomy' ) ) { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Main ReduxFramework customizer extension class |
|
| 19 | - * |
|
| 20 | - * @since 1.0.0 |
|
| 21 | - */ |
|
| 22 | - class Redux_Extension_Taxonomy extends Redux_Extension_Abstract { |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Extension version. |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - public static $version = '4.4.19'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Extension friendly name. |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - public string $extension_name = 'Taxonomy'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Terms array. |
|
| 40 | - * |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - public array $terms = array(); |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Taxonomy types array. |
|
| 47 | - * |
|
| 48 | - * @var array |
|
| 49 | - */ |
|
| 50 | - public array $taxonomy_types = array(); |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Taxonomy type. |
|
| 54 | - * |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - public string $taxonomy_type = ''; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Sections array. |
|
| 61 | - * |
|
| 62 | - * @var array |
|
| 63 | - */ |
|
| 64 | - public array $sections = array(); |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Original args array. |
|
| 68 | - * |
|
| 69 | - * @var array |
|
| 70 | - */ |
|
| 71 | - public array $orig_args = array(); |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Output array. |
|
| 75 | - * |
|
| 76 | - * @var array |
|
| 77 | - */ |
|
| 78 | - public array $output = array(); |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Parent options. |
|
| 82 | - * |
|
| 83 | - * @var array |
|
| 84 | - */ |
|
| 85 | - public array $parent_options = array(); |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Parent defaults. |
|
| 89 | - * |
|
| 90 | - * @var array |
|
| 91 | - */ |
|
| 92 | - public array $parent_defaults = array(); |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Taxonomy field types array. |
|
| 96 | - * |
|
| 97 | - * @var array |
|
| 98 | - */ |
|
| 99 | - public array $taxonomy_type_fields = array(); |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Option defaults. |
|
| 103 | - * |
|
| 104 | - * @var array |
|
| 105 | - */ |
|
| 106 | - public array $options_defaults = array(); |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * To replace array. |
|
| 110 | - * |
|
| 111 | - * @var array |
|
| 112 | - */ |
|
| 113 | - public array $to_replace = array(); |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Meta array. |
|
| 117 | - * |
|
| 118 | - * @var array |
|
| 119 | - */ |
|
| 120 | - public array $meta = array(); |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Tag ID. |
|
| 124 | - * |
|
| 125 | - * @var int |
|
| 126 | - */ |
|
| 127 | - public int $tag_id = 0; |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Accepted WordPress screens. |
|
| 131 | - * |
|
| 132 | - * @var array |
|
| 133 | - */ |
|
| 134 | - private array $pagenows; |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Notices array. |
|
| 138 | - * |
|
| 139 | - * @var array |
|
| 140 | - */ |
|
| 141 | - public array $notices; |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Redux_Extension_Taxonomy constructor. |
|
| 145 | - * |
|
| 146 | - * @param object $redux ReduxFramework object. |
|
| 147 | - */ |
|
| 148 | - public function __construct( $redux ) { |
|
| 149 | - global $pagenow; |
|
| 150 | - |
|
| 151 | - parent::__construct( $redux, __FILE__ ); |
|
| 152 | - |
|
| 153 | - $this->parent = $redux; |
|
| 154 | - |
|
| 155 | - $this->add_field( 'taxonomy' ); |
|
| 156 | - $this->parent->extensions['taxonomy'] = $this; |
|
| 157 | - |
|
| 158 | - $this->pagenows = array( 'edit-tags.php', 'term.php' ); |
|
| 159 | - |
|
| 160 | - include_once __DIR__ . '/redux-taxonomy-helpers.php'; |
|
| 161 | - |
|
| 162 | - add_action( 'admin_notices', array( $this, 'meta_terms_show_errors' ), 0 ); |
|
| 163 | - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 20 ); |
|
| 164 | - |
|
| 165 | - if ( is_admin() && in_array( $pagenow, $this->pagenows, true ) ) { |
|
| 166 | - $this->init(); |
|
| 167 | - |
|
| 168 | - // phpcs:ignore WordPress.Security.NonceVerification |
|
| 169 | - if ( isset( $_POST['taxonomy'] ) ) { |
|
| 170 | - |
|
| 171 | - // phpcs:ignore WordPress.Security.NonceVerification |
|
| 172 | - add_action( 'edit_' . sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ), array( $this, 'meta_terms_save' ), 10, 3 ); |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Add term classes. |
|
| 179 | - * |
|
| 180 | - * @param array $classes Classes. |
|
| 181 | - * |
|
| 182 | - * @return array |
|
| 183 | - */ |
|
| 184 | - public function add_term_classes( array $classes ): array { |
|
| 185 | - $classes[] = 'redux-taxonomy'; |
|
| 186 | - $classes[] = 'redux-' . $this->parent->args['opt_name']; |
|
| 187 | - |
|
| 188 | - if ( '' !== $this->parent->args['class'] ) { |
|
| 189 | - $classes[] = $this->parent->args['class']; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - return $classes; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Add term hide class. |
|
| 197 | - * |
|
| 198 | - * @param array $classes Classes. |
|
| 199 | - * |
|
| 200 | - * @return array |
|
| 201 | - */ |
|
| 202 | - public function add_term_hide_class( array $classes ): array { |
|
| 203 | - $classes[] = 'hide'; |
|
| 204 | - |
|
| 205 | - return $classes; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Init. |
|
| 210 | - */ |
|
| 211 | - public function init() { |
|
| 212 | - global $pagenow; |
|
| 213 | - |
|
| 214 | - if ( isset( $_POST['action'] ) && 'redux_demo_customizer_save' === $_POST['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 215 | - return; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $this->parent->transients['run_compiler'] = false; |
|
| 219 | - |
|
| 220 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 221 | - $this->terms = apply_filters( 'redux/taxonomy/' . $this->parent->args['opt_name'] . '/terms', $this->terms, $this->parent->args['opt_name'] ); |
|
| 222 | - |
|
| 223 | - if ( empty( $this->terms ) && class_exists( 'Redux_Taxonomy' ) ) { |
|
| 224 | - $this->terms = Redux_Taxonomy::construct_terms( $this->parent->args['opt_name'] ); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - if ( empty( $this->terms ) || ! is_array( $this->terms ) ) { |
|
| 228 | - return; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // phpcs:disable WordPress.Security.NonceVerification |
|
| 232 | - if ( ! isset( $_GET['tag_ID'] ) ) { |
|
| 233 | - $_GET['tag_ID'] = 0; |
|
| 234 | - } |
|
| 235 | - $this->tag_id = isset( $_GET['tag_ID'] ) ? sanitize_text_field( wp_unslash( $_GET['tag_ID'] ) ) : 0; |
|
| 236 | - $this->taxonomy_type = isset( $_GET['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) : ''; |
|
| 237 | - // phpcs:enable WordPress.Security.NonceVerification |
|
| 238 | - |
|
| 239 | - foreach ( $this->terms as $bk => $term ) { |
|
| 240 | - |
|
| 241 | - // If the tag_ids for this term are set, we're limiting to the current tag id. |
|
| 242 | - if ( ! empty( $term['tag_ids'] ) ) { |
|
| 243 | - if ( ! is_array( $term['tag_ids'] ) ) { |
|
| 244 | - $term['tag_ids'] = array( $term['tag_ids'] ); |
|
| 245 | - } |
|
| 246 | - if ( ! in_array( $this->tag_id, $term['tag_ids'], true ) ) { |
|
| 247 | - continue; |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - if ( ! isset( $term['taxonomy_types'] ) ) { |
|
| 252 | - return; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - if ( ! empty( $term['sections'] ) ) { |
|
| 256 | - $this->sections = $term['sections']; |
|
| 257 | - $this->parent->sections = array_merge( $this->parent->sections, $term['sections'] ); |
|
| 258 | - |
|
| 259 | - $this->taxonomy_types = wp_parse_args( $this->taxonomy_types, $term['taxonomy_types'] ); |
|
| 260 | - |
|
| 261 | - // Checking to override the parent variables. |
|
| 262 | - $add_field = false; |
|
| 263 | - |
|
| 264 | - foreach ( $term['taxonomy_types'] as $type ) { |
|
| 265 | - if ( $this->taxonomy_type === $type ) { |
|
| 266 | - $add_field = true; |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - // Replacing all the fields. |
|
| 271 | - if ( $add_field || ( ( is_admin() && in_array( $pagenow, $this->pagenows, true ) ) || ( ! is_admin() ) ) ) { |
|
| 272 | - $run_hooks = true; |
|
| 273 | - |
|
| 274 | - $this->meta[ $this->tag_id ] = Redux_Taxonomy::get_term_meta( array( 'taxonomy' => $this->tag_id ) ); |
|
| 275 | - $this->parent->options = array_merge( $this->parent->options, $this->meta[ $this->tag_id ] ); |
|
| 276 | - |
|
| 277 | - foreach ( $term['sections'] as $sk => $section ) { |
|
| 278 | - if ( ! empty( $section['fields'] ) ) { |
|
| 279 | - foreach ( $section['fields'] as $fk => $field ) { |
|
| 280 | - if ( ! isset( $field['class'] ) ) { |
|
| 281 | - $field['class'] = ''; |
|
| 282 | - |
|
| 283 | - $this->terms[ $bk ]['sections'][ $sk ]['fields'][ $fk ] = $field; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - $this->parent->required_class->check_dependencies( $field ); |
|
| 287 | - |
|
| 288 | - if ( $add_field || ( ( is_admin() && in_array( $pagenow, $this->pagenows, true ) ) || ( ! is_admin() ) ) ) { |
|
| 289 | - if ( empty( $field['id'] ) ) { |
|
| 290 | - continue; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - if ( isset( $field['default'] ) ) { |
|
| 294 | - $this->options_defaults[ $field['id'] ] = $field['default']; |
|
| 295 | - } else { |
|
| 296 | - $this->options_defaults[ $field['id'] ] = $this->field_default( $field ); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - foreach ( $term['taxonomy_types'] as $type ) { |
|
| 300 | - $this->taxonomy_type_fields[ $type ][ $field['id'] ] = 1; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - if ( ! empty( $field['output'] ) ) { |
|
| 304 | - $this->output[ $field['id'] ] = isset( $this->output[ $field['id'] ] ) ? array_merge( $field['output'], $this->output[ $field['id'] ] ) : $field['output']; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - // Detect what field types are being used. |
|
| 308 | - if ( ! isset( $this->parent->fields[ $field['type'] ][ $field['id'] ] ) ) { |
|
| 309 | - $this->parent->fields[ $field['type'] ][ $field['id'] ] = 1; |
|
| 310 | - } else { |
|
| 311 | - $this->parent->fields[ $field['type'] ] = array( $field['id'] => 1 ); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - if ( isset( $this->options_defaults[ $field['id'] ] ) ) { |
|
| 315 | - $this->to_replace[ $field['id'] ] = $field; |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - if ( ! isset( $this->parent->options[ $field['id'] ] ) ) { |
|
| 320 | - $this->parent->sections[ ( count( $this->parent->sections ) - 1 ) ]['fields'][] = $field; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - if ( ! isset( $this->meta[ $this->tag_id ][ $field['id'] ] ) ) { |
|
| 324 | - $this->meta[ $this->tag_id ][ $field['id'] ] = $this->options_defaults[ $field['id'] ]; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - // Only override if it exists, and it's not the default. |
|
| 328 | - // phpcs:ignore Generic.CodeAnalysis.EmptyStatement |
|
| 329 | - /** If ( isset( $this->meta[ $this->tag_id ][ $field['id'] ] ) && isset( $field['default'] ) && $this->meta[ $this->tag_id ][ $field['id'] ] === $field['default'] ) { |
|
| 330 | - * // unset($this->meta[$this->tag_id][$field['id']]); . |
|
| 331 | - * } |
|
| 332 | - */ |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - if ( isset( $run_hooks ) && true === $run_hooks ) { |
|
| 341 | - $this->parent_options = array(); |
|
| 342 | - |
|
| 343 | - if ( ! empty( $this->to_replace ) ) { |
|
| 344 | - foreach ( $this->to_replace as $id => $field ) { |
|
| 345 | - add_filter( "redux/options/{$this->parent->args['opt_name']}/field/$id/register", array( $this, 'replace_field' ) ); |
|
| 346 | - } |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - add_filter( "redux/options/{$this->parent->args['opt_name']}/options", array( $this, 'override_options' ) ); |
|
| 350 | - add_filter( "redux/field/{$this->parent->args['opt_name']}/_can_output_css", array( $this, 'override_can_output_css' ) ); |
|
| 351 | - add_filter( "redux/field/{$this->parent->args['opt_name']}/output_css", array( $this, 'output_css' ) ); |
|
| 352 | - |
|
| 353 | - // phpcs:disable WordPress.Security.NonceVerification |
|
| 354 | - if ( is_admin() && in_array( $pagenow, $this->pagenows, true ) && isset( $_GET['taxonomy'] ) ) { |
|
| 355 | - $priority = $this->parent->args['taxonomy_priority'] ?? 3; |
|
| 356 | - |
|
| 357 | - add_action( sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) . '_edit_form', array( $this, 'add_meta_terms' ), $priority ); |
|
| 358 | - add_action( sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) . '_add_form_fields', array( $this, 'add_meta_terms' ), $priority ); |
|
| 359 | - } |
|
| 360 | - // phpcs:enable WordPress.Security.NonceVerification |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * Replace field. |
|
| 366 | - * |
|
| 367 | - * @param array $field Field array. |
|
| 368 | - * |
|
| 369 | - * @return mixed |
|
| 370 | - */ |
|
| 371 | - public function replace_field( array $field ) { |
|
| 372 | - if ( isset( $this->to_replace[ $field['id'] ] ) ) { |
|
| 373 | - $field = $this->to_replace[ $field['id'] ]; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - return $field; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * CSS can output override. |
|
| 381 | - * |
|
| 382 | - * @param array $field Field array. |
|
| 383 | - * |
|
| 384 | - * @return array |
|
| 385 | - */ |
|
| 386 | - public function override_can_output_css( array $field ): array { |
|
| 387 | - if ( isset( $this->output[ $field['id'] ] ) ) { |
|
| 388 | - $field['force_output'] = true; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - return $field; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * Output CSS> |
|
| 396 | - * |
|
| 397 | - * @param array $field Field array. |
|
| 398 | - * |
|
| 399 | - * @return array |
|
| 400 | - */ |
|
| 401 | - public function output_css( array $field ): array { |
|
| 402 | - if ( isset( $this->output[ $field['id'] ] ) ) { |
|
| 403 | - $field['output'] = $this->output[ $field['id'] ]; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - return $field; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Make sure the defaults are the defaults |
|
| 411 | - * |
|
| 412 | - * @param array $options Options array. |
|
| 413 | - * |
|
| 414 | - * @return array |
|
| 415 | - */ |
|
| 416 | - public function override_options( array $options ): array { |
|
| 417 | - $this->parent->options_class->default_values(); |
|
| 418 | - $this->parent_defaults = $this->parent->options_defaults; |
|
| 419 | - |
|
| 420 | - $meta = $this->get_meta( $this->tag_id ); |
|
| 421 | - $data = wp_parse_args( $meta, $this->options_defaults ); |
|
| 422 | - |
|
| 423 | - foreach ( $data as $key => $value ) { |
|
| 424 | - if ( isset( $meta[ $key ] ) && '' !== $meta[ $key ] ) { |
|
| 425 | - $data[ $key ] = $meta[ $key ]; |
|
| 426 | - continue; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - if ( isset( $options[ $key ] ) ) { |
|
| 430 | - $data[ $key ] = $options[ $key ]; |
|
| 431 | - } |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - $this->parent->options_defaults = wp_parse_args( $this->options_defaults, $this->parent->options_defaults ); |
|
| 435 | - |
|
| 436 | - return wp_parse_args( $data, $options ); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * Enqueue support files and fields. |
|
| 441 | - */ |
|
| 442 | - public function enqueue() { |
|
| 443 | - global $pagenow; |
|
| 444 | - |
|
| 445 | - $types = array(); |
|
| 446 | - $sections = array(); |
|
| 447 | - |
|
| 448 | - // Enqueue css. |
|
| 449 | - foreach ( $this->terms as $key => $term ) { |
|
| 450 | - if ( empty( $term['sections'] ) ) { |
|
| 451 | - continue; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - // phpcs:ignore WordPress.Security.NonceVerification |
|
| 455 | - if ( isset( $_GET['taxonomy'] ) && isset( $term['taxonomy_types'] ) && in_array( sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ), $term['taxonomy_types'], true ) ) { |
|
| 456 | - $sections[] = $term['sections']; |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - $types = isset( $term['taxonomy_types'] ) ? array_merge( $term['taxonomy_types'], $types ) : $types; |
|
| 460 | - if ( ! empty( $term['taxonomy_types'] ) ) { |
|
| 461 | - if ( ! is_array( $term['taxonomy_types'] ) ) { |
|
| 462 | - $term['taxonomy_types'] = array( $term['taxonomy_types'] ); |
|
| 463 | - $this->terms[ $key ]['taxonomy_types'] = $term['taxonomy_types']; |
|
| 464 | - } |
|
| 465 | - } |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - // phpcs:ignore WordPress.Security.NonceVerification |
|
| 469 | - if ( in_array( $pagenow, $this->pagenows, true ) && isset( $_GET['taxonomy'] ) ) { |
|
| 470 | - if ( in_array( wp_unslash( $_GET['taxonomy'] ), $types, true ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 471 | - $this->parent->transients = get_transient( $this->parent->args['opt_name'] . '-transients-taxonomy' ); |
|
| 472 | - $this->parent->transients_check = $this->parent->transients; |
|
| 473 | - |
|
| 474 | - if ( isset( $this->parent->transients['notices'] ) ) { |
|
| 475 | - $this->notices = $this->parent->transients['notices']; |
|
| 476 | - $this->parent->transients['last_save_mode'] = 'taxonomy'; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - delete_transient( $this->parent->args['opt_name'] . '-transients-taxonomy' ); |
|
| 480 | - $this->parent->enqueue_class->init(); |
|
| 481 | - |
|
| 482 | - $this->parent->sections = $sections; |
|
| 483 | - |
|
| 484 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 485 | - do_action( "redux/taxonomy/{$this->parent->args['opt_name']}/enqueue" ); |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * Redux taxonomy CSS |
|
| 489 | - * filter 'redux/page/{opt_name}/enqueue/redux-extension-taxonomy-css' |
|
| 490 | - */ |
|
| 491 | - if ( true === $this->parent->args['dev_mode'] ) { |
|
| 492 | - wp_enqueue_style( |
|
| 493 | - 'redux-extension-taxonomy', |
|
| 494 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 495 | - apply_filters( "redux/taxonomy/{$this->parent->args['opt_name']}/enqueue/redux-extension-taxonomy-css", $this->extension_url . 'redux-extension-taxonomy.css' ), |
|
| 496 | - array( 'redux-admin-css' ), |
|
| 497 | - self::$version |
|
| 498 | - ); |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - /** |
|
| 502 | - * Redux taxonomy JS |
|
| 503 | - * filter 'redux/page/{opt_name}/enqueue/redux-extension-taxonomy-js |
|
| 504 | - */ |
|
| 505 | - wp_enqueue_script( |
|
| 506 | - 'redux-extension-taxonomy', |
|
| 507 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 508 | - apply_filters( "redux/taxonomy/{$this->parent->args['opt_name']}/enqueue/redux-extension-taxonomy-js", $this->extension_url . 'redux-extension-taxonomy' . Redux_Functions::is_min() . '.js' ), |
|
| 509 | - array( 'jquery', 'redux-js' ), |
|
| 510 | - self::$version, |
|
| 511 | - true |
|
| 512 | - ); |
|
| 513 | - } |
|
| 514 | - } |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - /** |
|
| 518 | - * DEPRECATED |
|
| 519 | - */ |
|
| 520 | - public function default_values() { |
|
| 521 | - if ( ! empty( $this->terms ) && empty( $this->options_defaults ) ) { |
|
| 522 | - foreach ( $this->terms as $key => $term ) { |
|
| 523 | - if ( empty( $term['sections'] ) ) { |
|
| 524 | - continue; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - // fill the cache. |
|
| 528 | - foreach ( $term['sections'] as $sk => $section ) { |
|
| 529 | - if ( ! isset( $section['id'] ) ) { |
|
| 530 | - if ( ! is_numeric( $sk ) || ! isset( $section['title'] ) ) { |
|
| 531 | - $section['id'] = $sk; |
|
| 532 | - } else { |
|
| 533 | - $section['id'] = sanitize_title( $section['title'], $sk ); |
|
| 534 | - } |
|
| 535 | - $this->terms[ $key ]['sections'][ $sk ] = $section; |
|
| 536 | - } |
|
| 537 | - if ( isset( $section['fields'] ) ) { |
|
| 538 | - foreach ( $section['fields'] as $k => $field ) { |
|
| 539 | - if ( empty( $field['id'] ) && empty( $field['type'] ) ) { |
|
| 540 | - continue; |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - if ( 'ace_editor' === $field['type'] && isset( $field['options'] ) ) { |
|
| 544 | - $this->terms[ $key ]['sections'][ $sk ]['fields'][ $k ]['args'] = $field['options']; |
|
| 545 | - unset( $this->terms[ $key ]['sections'][ $sk ]['fields'][ $k ]['options'] ); |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - if ( 'section' === $field['type'] && isset( $field['indent'] ) && true === $field['indent'] ) { |
|
| 549 | - $field['class'] = $field['class'] ?? ''; |
|
| 550 | - $field['class'] .= 'redux-section-indent-start'; |
|
| 551 | - $this->terms[ $key ]['sections'][ $sk ]['fields'][ $k ] = $field; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - if ( ! isset( $this->parent->options_defaults_class ) ) { |
|
| 555 | - $this->parent->options_defaults_class = new Redux_Options_Defaults(); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - $this->parent->options_defaults_class->field_default_values( $this->parent->args['opt_name'], $field ); |
|
| 559 | - |
|
| 560 | - if ( 'repeater' === $field['type'] ) { |
|
| 561 | - foreach ( $field['fields'] as $f ) { |
|
| 562 | - $this->parent->options_defaults_class->field_default_values( $this->parent->args['opt_name'], $f, null, true ); |
|
| 563 | - } |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - $this->parent->options_defaults = $this->parent->options_defaults_class->options_defaults; |
|
| 567 | - } |
|
| 568 | - } |
|
| 569 | - } |
|
| 570 | - } |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - if ( empty( $this->meta[ $this->tag_id ] ) ) { |
|
| 574 | - $this->meta[ $this->tag_id ] = $this->get_meta( $this->tag_id ); |
|
| 575 | - } |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * Add Meta Terms. |
|
| 580 | - */ |
|
| 581 | - public function add_meta_terms() { |
|
| 582 | - if ( empty( $this->terms ) || ! is_array( $this->terms ) ) { |
|
| 583 | - return; |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - foreach ( $this->terms as $key => $term ) { |
|
| 587 | - if ( empty( $term['sections'] ) ) { |
|
| 588 | - continue; |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - $defaults = array( |
|
| 592 | - 'id' => "$key", |
|
| 593 | - 'section_id' => $key, |
|
| 594 | - 'terms' => array(), |
|
| 595 | - ); |
|
| 596 | - |
|
| 597 | - $term = wp_parse_args( $term, $defaults ); |
|
| 598 | - if ( ! empty( $term['taxonomy_types'] ) && isset( $_GET['taxonomy'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 599 | - foreach ( $term['taxonomy_types'] as $termtype ) { |
|
| 600 | - if ( $_GET['taxonomy'] !== $termtype ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 601 | - continue; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - // Override the parent args on a metaterm level. |
|
| 605 | - if ( empty( $this->orig_args ) ) { |
|
| 606 | - $this->orig_args = $this->parent->args; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - if ( isset( $term['args'] ) ) { |
|
| 610 | - $this->parent->args = wp_parse_args( $term['args'], $this->orig_args ); |
|
| 611 | - } elseif ( $this->parent->args !== $this->orig_args ) { |
|
| 612 | - $this->parent->args = $this->orig_args; |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - if ( ! isset( $term['class'] ) ) { |
|
| 616 | - $term['class'] = array(); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - if ( ! empty( $term['class'] ) ) { |
|
| 620 | - if ( ! is_array( $term['class'] ) ) { |
|
| 621 | - $term['class'] = array( $term['class'] ); |
|
| 622 | - } |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - $term['class'] = $this->add_term_classes( $term['class'] ); |
|
| 626 | - |
|
| 627 | - if ( isset( $term['post_format'] ) ) { |
|
| 628 | - $term['class'] = $this->add_term_hide_class( $term['class'] ); |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - global $pagenow; |
|
| 632 | - if ( strpos( $pagenow, 'edit-' ) !== false ) { |
|
| 633 | - |
|
| 634 | - $term['style'] = 'wp'; |
|
| 635 | - $term['class'][] = ' edit-page'; |
|
| 636 | - $term['class'][] = ' redux-wp-style'; |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - $this->generate_terms( array( 'args' => $term ) ); |
|
| 640 | - } |
|
| 641 | - } |
|
| 642 | - } |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - /** |
|
| 646 | - * Field Default. |
|
| 647 | - * |
|
| 648 | - * @param array $field_id ID. |
|
| 649 | - * |
|
| 650 | - * @return mixed|string |
|
| 651 | - */ |
|
| 652 | - private function field_default( array $field_id ) { |
|
| 653 | - if ( ! isset( $this->parent->options_defaults ) ) { |
|
| 654 | - $this->parent->options_defaults = $this->parent->default_values(); |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - if ( ! isset( $this->parent->options ) || empty( $this->parent->options ) ) { |
|
| 658 | - if ( ! isset( $this->parent->options_class ) ) { |
|
| 659 | - $this->parent->options_class = new Redux_Options_Constructor( $this->parent ); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - $this->parent->options_class->get(); |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - if ( isset( $this->parent->options[ $field_id['id'] ] ) && isset( $this->parent->options_defaults[ $field_id['id'] ] ) && $this->parent->options[ $field_id['id'] ] !== $this->parent->options_defaults[ $field_id['id'] ] ) { |
|
| 666 | - return $this->parent->options[ $field_id['id'] ]; |
|
| 667 | - } else { |
|
| 668 | - if ( empty( $this->options_defaults ) ) { |
|
| 669 | - $this->default_values(); // fill cache. |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - $data = ''; |
|
| 673 | - if ( ! empty( $this->options_defaults ) ) { |
|
| 674 | - $data = $this->options_defaults[ $field_id['id'] ] ?? ''; |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - if ( empty( $data ) && isset( $this->parent->options_defaults[ $field_id['id'] ] ) ) { |
|
| 678 | - $data = $this->parent->options_defaults[ $field_id['id'] ] ?? ''; |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - return $data; |
|
| 682 | - } |
|
| 683 | - } |
|
| 684 | - |
|
| 685 | - /** |
|
| 686 | - * Function to get and cache the post-meta. |
|
| 687 | - * |
|
| 688 | - * @param string $id ID. |
|
| 689 | - * |
|
| 690 | - * @return array |
|
| 691 | - */ |
|
| 692 | - private function get_meta( string $id ): array { |
|
| 693 | - if ( ! isset( $this->meta[ $id ] ) ) { |
|
| 694 | - $this->meta[ $id ] = array(); |
|
| 695 | - $o_data = get_post_meta( $id ); |
|
| 696 | - |
|
| 697 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 698 | - $o_data = apply_filters( "redux/taxonomy/{$this->parent->args['opt_name']}/get_meta", $o_data ); |
|
| 699 | - |
|
| 700 | - if ( ! empty( $o_data ) ) { |
|
| 701 | - foreach ( $o_data as $key => $value ) { |
|
| 702 | - if ( count( $value ) === 1 ) { |
|
| 703 | - $this->meta[ $id ][ $key ] = maybe_unserialize( $value[0] ); |
|
| 704 | - } else { |
|
| 705 | - $new_value = array_map( 'maybe_unserialize', $value ); |
|
| 706 | - |
|
| 707 | - $this->meta[ $id ][ $key ] = $new_value[0]; |
|
| 708 | - } |
|
| 709 | - } |
|
| 710 | - } |
|
| 711 | - |
|
| 712 | - if ( isset( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ) ) { |
|
| 713 | - $data = maybe_unserialize( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ); |
|
| 714 | - |
|
| 715 | - foreach ( $data as $key => $value ) { |
|
| 716 | - $this->meta[ $id ][ $key ] = $value; |
|
| 717 | - update_post_meta( $id, $key, $value ); |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - unset( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ); |
|
| 721 | - |
|
| 722 | - delete_post_meta( $id, $this->parent->args['opt_name'] ); |
|
| 723 | - } |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - return $this->meta[ $id ]; |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - /** |
|
| 730 | - * Get values. |
|
| 731 | - * |
|
| 732 | - * @param mixed $the_post WP_Post. |
|
| 733 | - * @param string $meta_key Meta key. |
|
| 734 | - * @param string $def_val Default value. |
|
| 735 | - * |
|
| 736 | - * @return mixed|string |
|
| 737 | - */ |
|
| 738 | - public function get_values( $the_post, string $meta_key = '', string $def_val = '' ) { |
|
| 739 | - |
|
| 740 | - // Override these values if they differ from the admin panel defaults. |
|
| 741 | - if ( isset( $the_post->taxonomy_type ) && in_array( $the_post->taxonomy_type, $this->taxonomy_types, true ) ) { |
|
| 742 | - if ( isset( $this->taxonomy_type_values[ $the_post->taxonomy_type ] ) ) { |
|
| 743 | - $meta = $this->taxonomy_type_fields[ $the_post->taxonomy_type ]; |
|
| 744 | - } else { |
|
| 745 | - $defaults = array(); |
|
| 746 | - if ( ! empty( $this->taxonomy_type_fields[ $the_post->taxonomy_type ] ) ) { |
|
| 747 | - foreach ( $this->taxonomy_type_fields[ $the_post->taxonomy_type ] as $key => $null ) { |
|
| 748 | - if ( isset( $this->options_defaults[ $key ] ) ) { |
|
| 749 | - $defaults[ $key ] = $this->options_defaults[ $key ]; |
|
| 750 | - } |
|
| 751 | - } |
|
| 752 | - } |
|
| 753 | - |
|
| 754 | - $meta = wp_parse_args( $this->get_meta( $the_post->ID ), $defaults ); |
|
| 755 | - |
|
| 756 | - $this->taxonomy_type_fields[ $the_post->taxonomy_type ] = $meta; |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - if ( ! empty( $meta_key ) ) { |
|
| 760 | - if ( ! isset( $meta[ $meta_key ] ) ) { |
|
| 761 | - $meta[ $meta_key ] = $def_val; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - return $meta[ $meta_key ]; |
|
| 765 | - } else { |
|
| 766 | - return $meta; |
|
| 767 | - } |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - return $def_val; |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * Check edit visibility. |
|
| 775 | - * |
|
| 776 | - * @param array $params Array. |
|
| 777 | - * @param bool $subscan Flag to perform scan for fields of fields. |
|
| 778 | - * |
|
| 779 | - * @return bool |
|
| 780 | - */ |
|
| 781 | - private function check_edit_visibility( array $params = array(), bool $subscan = true ): bool { |
|
| 782 | - global $pagenow; |
|
| 783 | - |
|
| 784 | - // Edit page visibility. |
|
| 785 | - if ( strpos( $pagenow, 'edit-' ) !== false ) { |
|
| 786 | - if ( true === $subscan ) { |
|
| 787 | - if ( isset( $params['fields'] ) ) { |
|
| 788 | - |
|
| 789 | - foreach ( $params['fields'] as $field ) { |
|
| 790 | - if ( in_array( $field['id'], Redux_Core::$fields_hidden, true ) ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement |
|
| 791 | - // Not visible. |
|
| 792 | - } elseif ( isset( $field['add_visibility'] ) && $field['add_visibility'] ) { |
|
| 793 | - return true; |
|
| 794 | - } |
|
| 795 | - } |
|
| 796 | - |
|
| 797 | - return false; |
|
| 798 | - } |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - if ( isset( $params['add_visibility'] ) && true === $params['add_visibility'] ) { |
|
| 802 | - return true; |
|
| 803 | - } |
|
| 804 | - |
|
| 805 | - return false; |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - return true; |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - /** |
|
| 812 | - * Generate Terms. |
|
| 813 | - * |
|
| 814 | - * @param array $metaterm Term. |
|
| 815 | - */ |
|
| 816 | - private function generate_terms( array $metaterm ) { |
|
| 817 | - if ( ! empty( $metaterm['args']['permissions'] ) && ! Redux_Helpers::current_user_can( $metaterm['args']['permissions'] ) ) { |
|
| 818 | - return; |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - if ( isset( $metaterm['args']['style'] ) && in_array( $metaterm['args']['style'], array( 'wp', 'wordpress' ), true ) ) { |
|
| 822 | - $container_class = 'redux-wp-style'; |
|
| 823 | - $metaterm['args']['sidebar'] = false; |
|
| 824 | - } elseif ( isset( $metaterm['args']['sidebar'] ) && ! $metaterm['args']['sidebar'] ) { |
|
| 825 | - $container_class = 'redux-no-sections'; |
|
| 826 | - } else { |
|
| 827 | - $container_class = 'redux-has-sections'; |
|
| 828 | - } |
|
| 829 | - |
|
| 830 | - $class = implode( ' ', $metaterm['args']['class'] ); |
|
| 831 | - echo '<div class="' . esc_attr( $class ) . '">'; |
|
| 832 | - |
|
| 833 | - $sections = $metaterm['args']['sections']; |
|
| 834 | - |
|
| 835 | - wp_nonce_field( 'redux_taxonomy_meta_nonce', 'redux_taxonomy_meta_nonce' ); |
|
| 836 | - |
|
| 837 | - wp_dequeue_script( 'json-view-js' ); |
|
| 838 | - |
|
| 839 | - $sidebar = true; |
|
| 840 | - if ( count( $sections ) === 1 || ( isset( $metaterm['args']['sidebar'] ) && false === $metaterm['args']['sidebar'] ) ) { |
|
| 841 | - $sidebar = false; // Show the section dividers or not. |
|
| 842 | - } |
|
| 843 | - |
|
| 844 | - ?> |
|
| 17 | + /** |
|
| 18 | + * Main ReduxFramework customizer extension class |
|
| 19 | + * |
|
| 20 | + * @since 1.0.0 |
|
| 21 | + */ |
|
| 22 | + class Redux_Extension_Taxonomy extends Redux_Extension_Abstract { |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Extension version. |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + public static $version = '4.4.19'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Extension friendly name. |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + public string $extension_name = 'Taxonomy'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Terms array. |
|
| 40 | + * |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + public array $terms = array(); |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Taxonomy types array. |
|
| 47 | + * |
|
| 48 | + * @var array |
|
| 49 | + */ |
|
| 50 | + public array $taxonomy_types = array(); |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Taxonomy type. |
|
| 54 | + * |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + public string $taxonomy_type = ''; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Sections array. |
|
| 61 | + * |
|
| 62 | + * @var array |
|
| 63 | + */ |
|
| 64 | + public array $sections = array(); |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Original args array. |
|
| 68 | + * |
|
| 69 | + * @var array |
|
| 70 | + */ |
|
| 71 | + public array $orig_args = array(); |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Output array. |
|
| 75 | + * |
|
| 76 | + * @var array |
|
| 77 | + */ |
|
| 78 | + public array $output = array(); |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Parent options. |
|
| 82 | + * |
|
| 83 | + * @var array |
|
| 84 | + */ |
|
| 85 | + public array $parent_options = array(); |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Parent defaults. |
|
| 89 | + * |
|
| 90 | + * @var array |
|
| 91 | + */ |
|
| 92 | + public array $parent_defaults = array(); |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Taxonomy field types array. |
|
| 96 | + * |
|
| 97 | + * @var array |
|
| 98 | + */ |
|
| 99 | + public array $taxonomy_type_fields = array(); |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Option defaults. |
|
| 103 | + * |
|
| 104 | + * @var array |
|
| 105 | + */ |
|
| 106 | + public array $options_defaults = array(); |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * To replace array. |
|
| 110 | + * |
|
| 111 | + * @var array |
|
| 112 | + */ |
|
| 113 | + public array $to_replace = array(); |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Meta array. |
|
| 117 | + * |
|
| 118 | + * @var array |
|
| 119 | + */ |
|
| 120 | + public array $meta = array(); |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Tag ID. |
|
| 124 | + * |
|
| 125 | + * @var int |
|
| 126 | + */ |
|
| 127 | + public int $tag_id = 0; |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Accepted WordPress screens. |
|
| 131 | + * |
|
| 132 | + * @var array |
|
| 133 | + */ |
|
| 134 | + private array $pagenows; |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Notices array. |
|
| 138 | + * |
|
| 139 | + * @var array |
|
| 140 | + */ |
|
| 141 | + public array $notices; |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Redux_Extension_Taxonomy constructor. |
|
| 145 | + * |
|
| 146 | + * @param object $redux ReduxFramework object. |
|
| 147 | + */ |
|
| 148 | + public function __construct( $redux ) { |
|
| 149 | + global $pagenow; |
|
| 150 | + |
|
| 151 | + parent::__construct( $redux, __FILE__ ); |
|
| 152 | + |
|
| 153 | + $this->parent = $redux; |
|
| 154 | + |
|
| 155 | + $this->add_field( 'taxonomy' ); |
|
| 156 | + $this->parent->extensions['taxonomy'] = $this; |
|
| 157 | + |
|
| 158 | + $this->pagenows = array( 'edit-tags.php', 'term.php' ); |
|
| 159 | + |
|
| 160 | + include_once __DIR__ . '/redux-taxonomy-helpers.php'; |
|
| 161 | + |
|
| 162 | + add_action( 'admin_notices', array( $this, 'meta_terms_show_errors' ), 0 ); |
|
| 163 | + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 20 ); |
|
| 164 | + |
|
| 165 | + if ( is_admin() && in_array( $pagenow, $this->pagenows, true ) ) { |
|
| 166 | + $this->init(); |
|
| 167 | + |
|
| 168 | + // phpcs:ignore WordPress.Security.NonceVerification |
|
| 169 | + if ( isset( $_POST['taxonomy'] ) ) { |
|
| 170 | + |
|
| 171 | + // phpcs:ignore WordPress.Security.NonceVerification |
|
| 172 | + add_action( 'edit_' . sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ), array( $this, 'meta_terms_save' ), 10, 3 ); |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Add term classes. |
|
| 179 | + * |
|
| 180 | + * @param array $classes Classes. |
|
| 181 | + * |
|
| 182 | + * @return array |
|
| 183 | + */ |
|
| 184 | + public function add_term_classes( array $classes ): array { |
|
| 185 | + $classes[] = 'redux-taxonomy'; |
|
| 186 | + $classes[] = 'redux-' . $this->parent->args['opt_name']; |
|
| 187 | + |
|
| 188 | + if ( '' !== $this->parent->args['class'] ) { |
|
| 189 | + $classes[] = $this->parent->args['class']; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + return $classes; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Add term hide class. |
|
| 197 | + * |
|
| 198 | + * @param array $classes Classes. |
|
| 199 | + * |
|
| 200 | + * @return array |
|
| 201 | + */ |
|
| 202 | + public function add_term_hide_class( array $classes ): array { |
|
| 203 | + $classes[] = 'hide'; |
|
| 204 | + |
|
| 205 | + return $classes; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Init. |
|
| 210 | + */ |
|
| 211 | + public function init() { |
|
| 212 | + global $pagenow; |
|
| 213 | + |
|
| 214 | + if ( isset( $_POST['action'] ) && 'redux_demo_customizer_save' === $_POST['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 215 | + return; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $this->parent->transients['run_compiler'] = false; |
|
| 219 | + |
|
| 220 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 221 | + $this->terms = apply_filters( 'redux/taxonomy/' . $this->parent->args['opt_name'] . '/terms', $this->terms, $this->parent->args['opt_name'] ); |
|
| 222 | + |
|
| 223 | + if ( empty( $this->terms ) && class_exists( 'Redux_Taxonomy' ) ) { |
|
| 224 | + $this->terms = Redux_Taxonomy::construct_terms( $this->parent->args['opt_name'] ); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + if ( empty( $this->terms ) || ! is_array( $this->terms ) ) { |
|
| 228 | + return; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // phpcs:disable WordPress.Security.NonceVerification |
|
| 232 | + if ( ! isset( $_GET['tag_ID'] ) ) { |
|
| 233 | + $_GET['tag_ID'] = 0; |
|
| 234 | + } |
|
| 235 | + $this->tag_id = isset( $_GET['tag_ID'] ) ? sanitize_text_field( wp_unslash( $_GET['tag_ID'] ) ) : 0; |
|
| 236 | + $this->taxonomy_type = isset( $_GET['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) : ''; |
|
| 237 | + // phpcs:enable WordPress.Security.NonceVerification |
|
| 238 | + |
|
| 239 | + foreach ( $this->terms as $bk => $term ) { |
|
| 240 | + |
|
| 241 | + // If the tag_ids for this term are set, we're limiting to the current tag id. |
|
| 242 | + if ( ! empty( $term['tag_ids'] ) ) { |
|
| 243 | + if ( ! is_array( $term['tag_ids'] ) ) { |
|
| 244 | + $term['tag_ids'] = array( $term['tag_ids'] ); |
|
| 245 | + } |
|
| 246 | + if ( ! in_array( $this->tag_id, $term['tag_ids'], true ) ) { |
|
| 247 | + continue; |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + if ( ! isset( $term['taxonomy_types'] ) ) { |
|
| 252 | + return; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + if ( ! empty( $term['sections'] ) ) { |
|
| 256 | + $this->sections = $term['sections']; |
|
| 257 | + $this->parent->sections = array_merge( $this->parent->sections, $term['sections'] ); |
|
| 258 | + |
|
| 259 | + $this->taxonomy_types = wp_parse_args( $this->taxonomy_types, $term['taxonomy_types'] ); |
|
| 260 | + |
|
| 261 | + // Checking to override the parent variables. |
|
| 262 | + $add_field = false; |
|
| 263 | + |
|
| 264 | + foreach ( $term['taxonomy_types'] as $type ) { |
|
| 265 | + if ( $this->taxonomy_type === $type ) { |
|
| 266 | + $add_field = true; |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + // Replacing all the fields. |
|
| 271 | + if ( $add_field || ( ( is_admin() && in_array( $pagenow, $this->pagenows, true ) ) || ( ! is_admin() ) ) ) { |
|
| 272 | + $run_hooks = true; |
|
| 273 | + |
|
| 274 | + $this->meta[ $this->tag_id ] = Redux_Taxonomy::get_term_meta( array( 'taxonomy' => $this->tag_id ) ); |
|
| 275 | + $this->parent->options = array_merge( $this->parent->options, $this->meta[ $this->tag_id ] ); |
|
| 276 | + |
|
| 277 | + foreach ( $term['sections'] as $sk => $section ) { |
|
| 278 | + if ( ! empty( $section['fields'] ) ) { |
|
| 279 | + foreach ( $section['fields'] as $fk => $field ) { |
|
| 280 | + if ( ! isset( $field['class'] ) ) { |
|
| 281 | + $field['class'] = ''; |
|
| 282 | + |
|
| 283 | + $this->terms[ $bk ]['sections'][ $sk ]['fields'][ $fk ] = $field; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + $this->parent->required_class->check_dependencies( $field ); |
|
| 287 | + |
|
| 288 | + if ( $add_field || ( ( is_admin() && in_array( $pagenow, $this->pagenows, true ) ) || ( ! is_admin() ) ) ) { |
|
| 289 | + if ( empty( $field['id'] ) ) { |
|
| 290 | + continue; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + if ( isset( $field['default'] ) ) { |
|
| 294 | + $this->options_defaults[ $field['id'] ] = $field['default']; |
|
| 295 | + } else { |
|
| 296 | + $this->options_defaults[ $field['id'] ] = $this->field_default( $field ); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + foreach ( $term['taxonomy_types'] as $type ) { |
|
| 300 | + $this->taxonomy_type_fields[ $type ][ $field['id'] ] = 1; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + if ( ! empty( $field['output'] ) ) { |
|
| 304 | + $this->output[ $field['id'] ] = isset( $this->output[ $field['id'] ] ) ? array_merge( $field['output'], $this->output[ $field['id'] ] ) : $field['output']; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + // Detect what field types are being used. |
|
| 308 | + if ( ! isset( $this->parent->fields[ $field['type'] ][ $field['id'] ] ) ) { |
|
| 309 | + $this->parent->fields[ $field['type'] ][ $field['id'] ] = 1; |
|
| 310 | + } else { |
|
| 311 | + $this->parent->fields[ $field['type'] ] = array( $field['id'] => 1 ); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + if ( isset( $this->options_defaults[ $field['id'] ] ) ) { |
|
| 315 | + $this->to_replace[ $field['id'] ] = $field; |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + if ( ! isset( $this->parent->options[ $field['id'] ] ) ) { |
|
| 320 | + $this->parent->sections[ ( count( $this->parent->sections ) - 1 ) ]['fields'][] = $field; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + if ( ! isset( $this->meta[ $this->tag_id ][ $field['id'] ] ) ) { |
|
| 324 | + $this->meta[ $this->tag_id ][ $field['id'] ] = $this->options_defaults[ $field['id'] ]; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + // Only override if it exists, and it's not the default. |
|
| 328 | + // phpcs:ignore Generic.CodeAnalysis.EmptyStatement |
|
| 329 | + /** If ( isset( $this->meta[ $this->tag_id ][ $field['id'] ] ) && isset( $field['default'] ) && $this->meta[ $this->tag_id ][ $field['id'] ] === $field['default'] ) { |
|
| 330 | + * // unset($this->meta[$this->tag_id][$field['id']]); . |
|
| 331 | + * } |
|
| 332 | + */ |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + if ( isset( $run_hooks ) && true === $run_hooks ) { |
|
| 341 | + $this->parent_options = array(); |
|
| 342 | + |
|
| 343 | + if ( ! empty( $this->to_replace ) ) { |
|
| 344 | + foreach ( $this->to_replace as $id => $field ) { |
|
| 345 | + add_filter( "redux/options/{$this->parent->args['opt_name']}/field/$id/register", array( $this, 'replace_field' ) ); |
|
| 346 | + } |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + add_filter( "redux/options/{$this->parent->args['opt_name']}/options", array( $this, 'override_options' ) ); |
|
| 350 | + add_filter( "redux/field/{$this->parent->args['opt_name']}/_can_output_css", array( $this, 'override_can_output_css' ) ); |
|
| 351 | + add_filter( "redux/field/{$this->parent->args['opt_name']}/output_css", array( $this, 'output_css' ) ); |
|
| 352 | + |
|
| 353 | + // phpcs:disable WordPress.Security.NonceVerification |
|
| 354 | + if ( is_admin() && in_array( $pagenow, $this->pagenows, true ) && isset( $_GET['taxonomy'] ) ) { |
|
| 355 | + $priority = $this->parent->args['taxonomy_priority'] ?? 3; |
|
| 356 | + |
|
| 357 | + add_action( sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) . '_edit_form', array( $this, 'add_meta_terms' ), $priority ); |
|
| 358 | + add_action( sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) . '_add_form_fields', array( $this, 'add_meta_terms' ), $priority ); |
|
| 359 | + } |
|
| 360 | + // phpcs:enable WordPress.Security.NonceVerification |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * Replace field. |
|
| 366 | + * |
|
| 367 | + * @param array $field Field array. |
|
| 368 | + * |
|
| 369 | + * @return mixed |
|
| 370 | + */ |
|
| 371 | + public function replace_field( array $field ) { |
|
| 372 | + if ( isset( $this->to_replace[ $field['id'] ] ) ) { |
|
| 373 | + $field = $this->to_replace[ $field['id'] ]; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + return $field; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * CSS can output override. |
|
| 381 | + * |
|
| 382 | + * @param array $field Field array. |
|
| 383 | + * |
|
| 384 | + * @return array |
|
| 385 | + */ |
|
| 386 | + public function override_can_output_css( array $field ): array { |
|
| 387 | + if ( isset( $this->output[ $field['id'] ] ) ) { |
|
| 388 | + $field['force_output'] = true; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + return $field; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * Output CSS> |
|
| 396 | + * |
|
| 397 | + * @param array $field Field array. |
|
| 398 | + * |
|
| 399 | + * @return array |
|
| 400 | + */ |
|
| 401 | + public function output_css( array $field ): array { |
|
| 402 | + if ( isset( $this->output[ $field['id'] ] ) ) { |
|
| 403 | + $field['output'] = $this->output[ $field['id'] ]; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + return $field; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Make sure the defaults are the defaults |
|
| 411 | + * |
|
| 412 | + * @param array $options Options array. |
|
| 413 | + * |
|
| 414 | + * @return array |
|
| 415 | + */ |
|
| 416 | + public function override_options( array $options ): array { |
|
| 417 | + $this->parent->options_class->default_values(); |
|
| 418 | + $this->parent_defaults = $this->parent->options_defaults; |
|
| 419 | + |
|
| 420 | + $meta = $this->get_meta( $this->tag_id ); |
|
| 421 | + $data = wp_parse_args( $meta, $this->options_defaults ); |
|
| 422 | + |
|
| 423 | + foreach ( $data as $key => $value ) { |
|
| 424 | + if ( isset( $meta[ $key ] ) && '' !== $meta[ $key ] ) { |
|
| 425 | + $data[ $key ] = $meta[ $key ]; |
|
| 426 | + continue; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + if ( isset( $options[ $key ] ) ) { |
|
| 430 | + $data[ $key ] = $options[ $key ]; |
|
| 431 | + } |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + $this->parent->options_defaults = wp_parse_args( $this->options_defaults, $this->parent->options_defaults ); |
|
| 435 | + |
|
| 436 | + return wp_parse_args( $data, $options ); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * Enqueue support files and fields. |
|
| 441 | + */ |
|
| 442 | + public function enqueue() { |
|
| 443 | + global $pagenow; |
|
| 444 | + |
|
| 445 | + $types = array(); |
|
| 446 | + $sections = array(); |
|
| 447 | + |
|
| 448 | + // Enqueue css. |
|
| 449 | + foreach ( $this->terms as $key => $term ) { |
|
| 450 | + if ( empty( $term['sections'] ) ) { |
|
| 451 | + continue; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + // phpcs:ignore WordPress.Security.NonceVerification |
|
| 455 | + if ( isset( $_GET['taxonomy'] ) && isset( $term['taxonomy_types'] ) && in_array( sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ), $term['taxonomy_types'], true ) ) { |
|
| 456 | + $sections[] = $term['sections']; |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + $types = isset( $term['taxonomy_types'] ) ? array_merge( $term['taxonomy_types'], $types ) : $types; |
|
| 460 | + if ( ! empty( $term['taxonomy_types'] ) ) { |
|
| 461 | + if ( ! is_array( $term['taxonomy_types'] ) ) { |
|
| 462 | + $term['taxonomy_types'] = array( $term['taxonomy_types'] ); |
|
| 463 | + $this->terms[ $key ]['taxonomy_types'] = $term['taxonomy_types']; |
|
| 464 | + } |
|
| 465 | + } |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + // phpcs:ignore WordPress.Security.NonceVerification |
|
| 469 | + if ( in_array( $pagenow, $this->pagenows, true ) && isset( $_GET['taxonomy'] ) ) { |
|
| 470 | + if ( in_array( wp_unslash( $_GET['taxonomy'] ), $types, true ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 471 | + $this->parent->transients = get_transient( $this->parent->args['opt_name'] . '-transients-taxonomy' ); |
|
| 472 | + $this->parent->transients_check = $this->parent->transients; |
|
| 473 | + |
|
| 474 | + if ( isset( $this->parent->transients['notices'] ) ) { |
|
| 475 | + $this->notices = $this->parent->transients['notices']; |
|
| 476 | + $this->parent->transients['last_save_mode'] = 'taxonomy'; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + delete_transient( $this->parent->args['opt_name'] . '-transients-taxonomy' ); |
|
| 480 | + $this->parent->enqueue_class->init(); |
|
| 481 | + |
|
| 482 | + $this->parent->sections = $sections; |
|
| 483 | + |
|
| 484 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 485 | + do_action( "redux/taxonomy/{$this->parent->args['opt_name']}/enqueue" ); |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * Redux taxonomy CSS |
|
| 489 | + * filter 'redux/page/{opt_name}/enqueue/redux-extension-taxonomy-css' |
|
| 490 | + */ |
|
| 491 | + if ( true === $this->parent->args['dev_mode'] ) { |
|
| 492 | + wp_enqueue_style( |
|
| 493 | + 'redux-extension-taxonomy', |
|
| 494 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 495 | + apply_filters( "redux/taxonomy/{$this->parent->args['opt_name']}/enqueue/redux-extension-taxonomy-css", $this->extension_url . 'redux-extension-taxonomy.css' ), |
|
| 496 | + array( 'redux-admin-css' ), |
|
| 497 | + self::$version |
|
| 498 | + ); |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + /** |
|
| 502 | + * Redux taxonomy JS |
|
| 503 | + * filter 'redux/page/{opt_name}/enqueue/redux-extension-taxonomy-js |
|
| 504 | + */ |
|
| 505 | + wp_enqueue_script( |
|
| 506 | + 'redux-extension-taxonomy', |
|
| 507 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 508 | + apply_filters( "redux/taxonomy/{$this->parent->args['opt_name']}/enqueue/redux-extension-taxonomy-js", $this->extension_url . 'redux-extension-taxonomy' . Redux_Functions::is_min() . '.js' ), |
|
| 509 | + array( 'jquery', 'redux-js' ), |
|
| 510 | + self::$version, |
|
| 511 | + true |
|
| 512 | + ); |
|
| 513 | + } |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + /** |
|
| 518 | + * DEPRECATED |
|
| 519 | + */ |
|
| 520 | + public function default_values() { |
|
| 521 | + if ( ! empty( $this->terms ) && empty( $this->options_defaults ) ) { |
|
| 522 | + foreach ( $this->terms as $key => $term ) { |
|
| 523 | + if ( empty( $term['sections'] ) ) { |
|
| 524 | + continue; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + // fill the cache. |
|
| 528 | + foreach ( $term['sections'] as $sk => $section ) { |
|
| 529 | + if ( ! isset( $section['id'] ) ) { |
|
| 530 | + if ( ! is_numeric( $sk ) || ! isset( $section['title'] ) ) { |
|
| 531 | + $section['id'] = $sk; |
|
| 532 | + } else { |
|
| 533 | + $section['id'] = sanitize_title( $section['title'], $sk ); |
|
| 534 | + } |
|
| 535 | + $this->terms[ $key ]['sections'][ $sk ] = $section; |
|
| 536 | + } |
|
| 537 | + if ( isset( $section['fields'] ) ) { |
|
| 538 | + foreach ( $section['fields'] as $k => $field ) { |
|
| 539 | + if ( empty( $field['id'] ) && empty( $field['type'] ) ) { |
|
| 540 | + continue; |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + if ( 'ace_editor' === $field['type'] && isset( $field['options'] ) ) { |
|
| 544 | + $this->terms[ $key ]['sections'][ $sk ]['fields'][ $k ]['args'] = $field['options']; |
|
| 545 | + unset( $this->terms[ $key ]['sections'][ $sk ]['fields'][ $k ]['options'] ); |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + if ( 'section' === $field['type'] && isset( $field['indent'] ) && true === $field['indent'] ) { |
|
| 549 | + $field['class'] = $field['class'] ?? ''; |
|
| 550 | + $field['class'] .= 'redux-section-indent-start'; |
|
| 551 | + $this->terms[ $key ]['sections'][ $sk ]['fields'][ $k ] = $field; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + if ( ! isset( $this->parent->options_defaults_class ) ) { |
|
| 555 | + $this->parent->options_defaults_class = new Redux_Options_Defaults(); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + $this->parent->options_defaults_class->field_default_values( $this->parent->args['opt_name'], $field ); |
|
| 559 | + |
|
| 560 | + if ( 'repeater' === $field['type'] ) { |
|
| 561 | + foreach ( $field['fields'] as $f ) { |
|
| 562 | + $this->parent->options_defaults_class->field_default_values( $this->parent->args['opt_name'], $f, null, true ); |
|
| 563 | + } |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + $this->parent->options_defaults = $this->parent->options_defaults_class->options_defaults; |
|
| 567 | + } |
|
| 568 | + } |
|
| 569 | + } |
|
| 570 | + } |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + if ( empty( $this->meta[ $this->tag_id ] ) ) { |
|
| 574 | + $this->meta[ $this->tag_id ] = $this->get_meta( $this->tag_id ); |
|
| 575 | + } |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * Add Meta Terms. |
|
| 580 | + */ |
|
| 581 | + public function add_meta_terms() { |
|
| 582 | + if ( empty( $this->terms ) || ! is_array( $this->terms ) ) { |
|
| 583 | + return; |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + foreach ( $this->terms as $key => $term ) { |
|
| 587 | + if ( empty( $term['sections'] ) ) { |
|
| 588 | + continue; |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + $defaults = array( |
|
| 592 | + 'id' => "$key", |
|
| 593 | + 'section_id' => $key, |
|
| 594 | + 'terms' => array(), |
|
| 595 | + ); |
|
| 596 | + |
|
| 597 | + $term = wp_parse_args( $term, $defaults ); |
|
| 598 | + if ( ! empty( $term['taxonomy_types'] ) && isset( $_GET['taxonomy'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 599 | + foreach ( $term['taxonomy_types'] as $termtype ) { |
|
| 600 | + if ( $_GET['taxonomy'] !== $termtype ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 601 | + continue; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + // Override the parent args on a metaterm level. |
|
| 605 | + if ( empty( $this->orig_args ) ) { |
|
| 606 | + $this->orig_args = $this->parent->args; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + if ( isset( $term['args'] ) ) { |
|
| 610 | + $this->parent->args = wp_parse_args( $term['args'], $this->orig_args ); |
|
| 611 | + } elseif ( $this->parent->args !== $this->orig_args ) { |
|
| 612 | + $this->parent->args = $this->orig_args; |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + if ( ! isset( $term['class'] ) ) { |
|
| 616 | + $term['class'] = array(); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + if ( ! empty( $term['class'] ) ) { |
|
| 620 | + if ( ! is_array( $term['class'] ) ) { |
|
| 621 | + $term['class'] = array( $term['class'] ); |
|
| 622 | + } |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + $term['class'] = $this->add_term_classes( $term['class'] ); |
|
| 626 | + |
|
| 627 | + if ( isset( $term['post_format'] ) ) { |
|
| 628 | + $term['class'] = $this->add_term_hide_class( $term['class'] ); |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + global $pagenow; |
|
| 632 | + if ( strpos( $pagenow, 'edit-' ) !== false ) { |
|
| 633 | + |
|
| 634 | + $term['style'] = 'wp'; |
|
| 635 | + $term['class'][] = ' edit-page'; |
|
| 636 | + $term['class'][] = ' redux-wp-style'; |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + $this->generate_terms( array( 'args' => $term ) ); |
|
| 640 | + } |
|
| 641 | + } |
|
| 642 | + } |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + /** |
|
| 646 | + * Field Default. |
|
| 647 | + * |
|
| 648 | + * @param array $field_id ID. |
|
| 649 | + * |
|
| 650 | + * @return mixed|string |
|
| 651 | + */ |
|
| 652 | + private function field_default( array $field_id ) { |
|
| 653 | + if ( ! isset( $this->parent->options_defaults ) ) { |
|
| 654 | + $this->parent->options_defaults = $this->parent->default_values(); |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + if ( ! isset( $this->parent->options ) || empty( $this->parent->options ) ) { |
|
| 658 | + if ( ! isset( $this->parent->options_class ) ) { |
|
| 659 | + $this->parent->options_class = new Redux_Options_Constructor( $this->parent ); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + $this->parent->options_class->get(); |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + if ( isset( $this->parent->options[ $field_id['id'] ] ) && isset( $this->parent->options_defaults[ $field_id['id'] ] ) && $this->parent->options[ $field_id['id'] ] !== $this->parent->options_defaults[ $field_id['id'] ] ) { |
|
| 666 | + return $this->parent->options[ $field_id['id'] ]; |
|
| 667 | + } else { |
|
| 668 | + if ( empty( $this->options_defaults ) ) { |
|
| 669 | + $this->default_values(); // fill cache. |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + $data = ''; |
|
| 673 | + if ( ! empty( $this->options_defaults ) ) { |
|
| 674 | + $data = $this->options_defaults[ $field_id['id'] ] ?? ''; |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + if ( empty( $data ) && isset( $this->parent->options_defaults[ $field_id['id'] ] ) ) { |
|
| 678 | + $data = $this->parent->options_defaults[ $field_id['id'] ] ?? ''; |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + return $data; |
|
| 682 | + } |
|
| 683 | + } |
|
| 684 | + |
|
| 685 | + /** |
|
| 686 | + * Function to get and cache the post-meta. |
|
| 687 | + * |
|
| 688 | + * @param string $id ID. |
|
| 689 | + * |
|
| 690 | + * @return array |
|
| 691 | + */ |
|
| 692 | + private function get_meta( string $id ): array { |
|
| 693 | + if ( ! isset( $this->meta[ $id ] ) ) { |
|
| 694 | + $this->meta[ $id ] = array(); |
|
| 695 | + $o_data = get_post_meta( $id ); |
|
| 696 | + |
|
| 697 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 698 | + $o_data = apply_filters( "redux/taxonomy/{$this->parent->args['opt_name']}/get_meta", $o_data ); |
|
| 699 | + |
|
| 700 | + if ( ! empty( $o_data ) ) { |
|
| 701 | + foreach ( $o_data as $key => $value ) { |
|
| 702 | + if ( count( $value ) === 1 ) { |
|
| 703 | + $this->meta[ $id ][ $key ] = maybe_unserialize( $value[0] ); |
|
| 704 | + } else { |
|
| 705 | + $new_value = array_map( 'maybe_unserialize', $value ); |
|
| 706 | + |
|
| 707 | + $this->meta[ $id ][ $key ] = $new_value[0]; |
|
| 708 | + } |
|
| 709 | + } |
|
| 710 | + } |
|
| 711 | + |
|
| 712 | + if ( isset( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ) ) { |
|
| 713 | + $data = maybe_unserialize( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ); |
|
| 714 | + |
|
| 715 | + foreach ( $data as $key => $value ) { |
|
| 716 | + $this->meta[ $id ][ $key ] = $value; |
|
| 717 | + update_post_meta( $id, $key, $value ); |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + unset( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ); |
|
| 721 | + |
|
| 722 | + delete_post_meta( $id, $this->parent->args['opt_name'] ); |
|
| 723 | + } |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + return $this->meta[ $id ]; |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + /** |
|
| 730 | + * Get values. |
|
| 731 | + * |
|
| 732 | + * @param mixed $the_post WP_Post. |
|
| 733 | + * @param string $meta_key Meta key. |
|
| 734 | + * @param string $def_val Default value. |
|
| 735 | + * |
|
| 736 | + * @return mixed|string |
|
| 737 | + */ |
|
| 738 | + public function get_values( $the_post, string $meta_key = '', string $def_val = '' ) { |
|
| 739 | + |
|
| 740 | + // Override these values if they differ from the admin panel defaults. |
|
| 741 | + if ( isset( $the_post->taxonomy_type ) && in_array( $the_post->taxonomy_type, $this->taxonomy_types, true ) ) { |
|
| 742 | + if ( isset( $this->taxonomy_type_values[ $the_post->taxonomy_type ] ) ) { |
|
| 743 | + $meta = $this->taxonomy_type_fields[ $the_post->taxonomy_type ]; |
|
| 744 | + } else { |
|
| 745 | + $defaults = array(); |
|
| 746 | + if ( ! empty( $this->taxonomy_type_fields[ $the_post->taxonomy_type ] ) ) { |
|
| 747 | + foreach ( $this->taxonomy_type_fields[ $the_post->taxonomy_type ] as $key => $null ) { |
|
| 748 | + if ( isset( $this->options_defaults[ $key ] ) ) { |
|
| 749 | + $defaults[ $key ] = $this->options_defaults[ $key ]; |
|
| 750 | + } |
|
| 751 | + } |
|
| 752 | + } |
|
| 753 | + |
|
| 754 | + $meta = wp_parse_args( $this->get_meta( $the_post->ID ), $defaults ); |
|
| 755 | + |
|
| 756 | + $this->taxonomy_type_fields[ $the_post->taxonomy_type ] = $meta; |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + if ( ! empty( $meta_key ) ) { |
|
| 760 | + if ( ! isset( $meta[ $meta_key ] ) ) { |
|
| 761 | + $meta[ $meta_key ] = $def_val; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + return $meta[ $meta_key ]; |
|
| 765 | + } else { |
|
| 766 | + return $meta; |
|
| 767 | + } |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + return $def_val; |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * Check edit visibility. |
|
| 775 | + * |
|
| 776 | + * @param array $params Array. |
|
| 777 | + * @param bool $subscan Flag to perform scan for fields of fields. |
|
| 778 | + * |
|
| 779 | + * @return bool |
|
| 780 | + */ |
|
| 781 | + private function check_edit_visibility( array $params = array(), bool $subscan = true ): bool { |
|
| 782 | + global $pagenow; |
|
| 783 | + |
|
| 784 | + // Edit page visibility. |
|
| 785 | + if ( strpos( $pagenow, 'edit-' ) !== false ) { |
|
| 786 | + if ( true === $subscan ) { |
|
| 787 | + if ( isset( $params['fields'] ) ) { |
|
| 788 | + |
|
| 789 | + foreach ( $params['fields'] as $field ) { |
|
| 790 | + if ( in_array( $field['id'], Redux_Core::$fields_hidden, true ) ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement |
|
| 791 | + // Not visible. |
|
| 792 | + } elseif ( isset( $field['add_visibility'] ) && $field['add_visibility'] ) { |
|
| 793 | + return true; |
|
| 794 | + } |
|
| 795 | + } |
|
| 796 | + |
|
| 797 | + return false; |
|
| 798 | + } |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + if ( isset( $params['add_visibility'] ) && true === $params['add_visibility'] ) { |
|
| 802 | + return true; |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + return false; |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + return true; |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + /** |
|
| 812 | + * Generate Terms. |
|
| 813 | + * |
|
| 814 | + * @param array $metaterm Term. |
|
| 815 | + */ |
|
| 816 | + private function generate_terms( array $metaterm ) { |
|
| 817 | + if ( ! empty( $metaterm['args']['permissions'] ) && ! Redux_Helpers::current_user_can( $metaterm['args']['permissions'] ) ) { |
|
| 818 | + return; |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + if ( isset( $metaterm['args']['style'] ) && in_array( $metaterm['args']['style'], array( 'wp', 'wordpress' ), true ) ) { |
|
| 822 | + $container_class = 'redux-wp-style'; |
|
| 823 | + $metaterm['args']['sidebar'] = false; |
|
| 824 | + } elseif ( isset( $metaterm['args']['sidebar'] ) && ! $metaterm['args']['sidebar'] ) { |
|
| 825 | + $container_class = 'redux-no-sections'; |
|
| 826 | + } else { |
|
| 827 | + $container_class = 'redux-has-sections'; |
|
| 828 | + } |
|
| 829 | + |
|
| 830 | + $class = implode( ' ', $metaterm['args']['class'] ); |
|
| 831 | + echo '<div class="' . esc_attr( $class ) . '">'; |
|
| 832 | + |
|
| 833 | + $sections = $metaterm['args']['sections']; |
|
| 834 | + |
|
| 835 | + wp_nonce_field( 'redux_taxonomy_meta_nonce', 'redux_taxonomy_meta_nonce' ); |
|
| 836 | + |
|
| 837 | + wp_dequeue_script( 'json-view-js' ); |
|
| 838 | + |
|
| 839 | + $sidebar = true; |
|
| 840 | + if ( count( $sections ) === 1 || ( isset( $metaterm['args']['sidebar'] ) && false === $metaterm['args']['sidebar'] ) ) { |
|
| 841 | + $sidebar = false; // Show the section dividers or not. |
|
| 842 | + } |
|
| 843 | + |
|
| 844 | + ?> |
|
| 845 | 845 | <div data-opt-name="<?php echo esc_attr( $this->parent->args['opt_name'] ); ?>" |
| 846 | 846 | class="redux-container <?php echo( esc_attr( $container_class ) ); ?> redux-term redux-box-normal redux-term-normal"> |
| 847 | 847 | <div class="redux-notices"> |
@@ -865,292 +865,292 @@ discard block |
||
| 865 | 865 | </div> |
| 866 | 866 | </div> |
| 867 | 867 | <?php |
| 868 | - echo '<a href="javascript:void(0);" class="expand_options hide" style="display:none;">' . esc_html__( 'Expand', 'redux-framework' ) . '</a>'; |
|
| 869 | - if ( $sidebar ) { |
|
| 870 | - ?> |
|
| 868 | + echo '<a href="javascript:void(0);" class="expand_options hide" style="display:none;">' . esc_html__( 'Expand', 'redux-framework' ) . '</a>'; |
|
| 869 | + if ( $sidebar ) { |
|
| 870 | + ?> |
|
| 871 | 871 | <div class="redux-sidebar"> |
| 872 | 872 | <ul class="redux-group-menu"> |
| 873 | 873 | <?php |
| 874 | - foreach ( $sections as $s_key => $section ) { |
|
| 875 | - if ( ! empty( $section['permissions'] ) && ! Redux_Helpers::current_user_can( $section['permissions'] ) ) { |
|
| 876 | - continue; |
|
| 877 | - } |
|
| 878 | - |
|
| 879 | - // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 880 | - echo $this->parent->render_class->section_menu( $s_key, $section, '_' . $metaterm['args']['id'], $sections ); |
|
| 881 | - } |
|
| 882 | - ?> |
|
| 874 | + foreach ( $sections as $s_key => $section ) { |
|
| 875 | + if ( ! empty( $section['permissions'] ) && ! Redux_Helpers::current_user_can( $section['permissions'] ) ) { |
|
| 876 | + continue; |
|
| 877 | + } |
|
| 878 | + |
|
| 879 | + // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 880 | + echo $this->parent->render_class->section_menu( $s_key, $section, '_' . $metaterm['args']['id'], $sections ); |
|
| 881 | + } |
|
| 882 | + ?> |
|
| 883 | 883 | </ul> |
| 884 | 884 | </div> |
| 885 | 885 | <?php } ?> |
| 886 | 886 | <div class="redux-main"> |
| 887 | 887 | <?php |
| 888 | 888 | |
| 889 | - foreach ( $sections as $s_key => $section ) { |
|
| 890 | - if ( ! $this->check_edit_visibility( $section ) ) { |
|
| 891 | - continue; |
|
| 892 | - } |
|
| 893 | - |
|
| 894 | - if ( ! empty( $section['permissions'] ) && ! Redux_Helpers::current_user_can( $section['permissions'] ) ) { |
|
| 895 | - continue; |
|
| 896 | - } |
|
| 897 | - |
|
| 898 | - if ( ! empty( $section['fields'] ) ) { |
|
| 899 | - if ( isset( $section['args'] ) ) { |
|
| 900 | - $this->parent->args = wp_parse_args( $section['args'], $this->orig_args ); |
|
| 901 | - } elseif ( $this->parent->args !== $this->orig_args ) { |
|
| 902 | - $this->parent->args = $this->orig_args; |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - $hide = $sidebar ? '' : ' display-group'; |
|
| 906 | - $section['class'] = isset( $section['class'] ) ? " {$section['class']}" : ''; |
|
| 907 | - |
|
| 908 | - // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 909 | - echo "<div id='{$s_key}_{$metaterm['args']['id']}_section_group' class='redux-group-tab{$section['class']} redux_metaterm_panel$hide'>"; |
|
| 910 | - |
|
| 911 | - if ( ! empty( $section['title'] ) ) { |
|
| 912 | - echo '<h3 class="redux-section-title">' . wp_kses_post( $section['title'] ) . '</h3>'; |
|
| 913 | - } |
|
| 914 | - |
|
| 915 | - if ( ! empty( $section['desc'] ) ) { |
|
| 916 | - echo '<div class="redux-section-desc">' . wp_kses_post( $section['desc'] ) . '</div>'; |
|
| 917 | - } |
|
| 918 | - |
|
| 919 | - echo '<table class="form-table"><tbody>'; |
|
| 920 | - |
|
| 921 | - foreach ( $section['fields'] as $field ) { |
|
| 922 | - if ( ! $this->check_edit_visibility( $field, false ) ) { |
|
| 923 | - continue; |
|
| 924 | - } |
|
| 925 | - |
|
| 926 | - if ( ! empty( $field['permissions'] ) && ! Redux_Helpers::current_user_can( $field['permissions'] ) ) { |
|
| 927 | - continue; |
|
| 928 | - } |
|
| 929 | - |
|
| 930 | - $field['name'] = $this->parent->args['opt_name'] . '[' . $field['id'] . ']'; |
|
| 931 | - |
|
| 932 | - $is_hidden = false; |
|
| 933 | - $ex_style = ''; |
|
| 934 | - if ( isset( $field['hidden'] ) && $field['hidden'] ) { |
|
| 935 | - $is_hidden = true; |
|
| 936 | - $ex_style = ' style="border-bottom: none;"'; |
|
| 937 | - } |
|
| 938 | - |
|
| 939 | - // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 940 | - echo '<tr valign="top"' . $ex_style . '>'; |
|
| 941 | - |
|
| 942 | - $th = $this->parent->render_class->get_header_html( $field ); |
|
| 943 | - |
|
| 944 | - if ( $is_hidden ) { |
|
| 945 | - $str_pos = strpos( $th, 'redux_field_th' ); |
|
| 946 | - |
|
| 947 | - if ( $str_pos > - 1 ) { |
|
| 948 | - $th = str_replace( 'redux_field_th', 'redux_field_th hide', $th ); |
|
| 949 | - } |
|
| 950 | - } |
|
| 951 | - |
|
| 952 | - if ( $sidebar ) { |
|
| 953 | - if ( ! ( isset( $metaterm['args']['sections'] ) && count( $metaterm['args']['sections'] ) === 1 && isset( $metaterm['args']['sections'][0]['fields'] ) && count( $metaterm['args']['sections'][0]['fields'] ) === 1 ) && isset( $field['title'] ) ) { |
|
| 954 | - echo '<th scope="row">'; |
|
| 955 | - if ( ! empty( $th ) ) { |
|
| 956 | - echo $th; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 957 | - } |
|
| 958 | - echo '</th>'; |
|
| 959 | - echo '<td>'; |
|
| 960 | - } |
|
| 961 | - } else { |
|
| 962 | - echo '<td>' . $th; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 963 | - } |
|
| 964 | - |
|
| 965 | - if ( 'section' === $field['type'] && true === $field['indent'] ) { |
|
| 966 | - $field['class'] = $field['class'] ?? ''; |
|
| 967 | - $field['class'] .= 'redux-section-indent-start'; |
|
| 968 | - } |
|
| 969 | - |
|
| 970 | - if ( ! isset( $this->meta[ $this->tag_id ][ $field['id'] ] ) ) { |
|
| 971 | - $this->meta[ $this->tag_id ][ $field['id'] ] = ''; |
|
| 972 | - } |
|
| 973 | - |
|
| 974 | - $this->parent->render_class->field_input( $field, $this->meta[ $this->tag_id ][ $field['id'] ], true ); |
|
| 975 | - echo '</td></tr>'; |
|
| 976 | - } |
|
| 977 | - echo '</tbody></table>'; |
|
| 978 | - echo '</div>'; |
|
| 979 | - } |
|
| 980 | - } |
|
| 981 | - ?> |
|
| 889 | + foreach ( $sections as $s_key => $section ) { |
|
| 890 | + if ( ! $this->check_edit_visibility( $section ) ) { |
|
| 891 | + continue; |
|
| 892 | + } |
|
| 893 | + |
|
| 894 | + if ( ! empty( $section['permissions'] ) && ! Redux_Helpers::current_user_can( $section['permissions'] ) ) { |
|
| 895 | + continue; |
|
| 896 | + } |
|
| 897 | + |
|
| 898 | + if ( ! empty( $section['fields'] ) ) { |
|
| 899 | + if ( isset( $section['args'] ) ) { |
|
| 900 | + $this->parent->args = wp_parse_args( $section['args'], $this->orig_args ); |
|
| 901 | + } elseif ( $this->parent->args !== $this->orig_args ) { |
|
| 902 | + $this->parent->args = $this->orig_args; |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + $hide = $sidebar ? '' : ' display-group'; |
|
| 906 | + $section['class'] = isset( $section['class'] ) ? " {$section['class']}" : ''; |
|
| 907 | + |
|
| 908 | + // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 909 | + echo "<div id='{$s_key}_{$metaterm['args']['id']}_section_group' class='redux-group-tab{$section['class']} redux_metaterm_panel$hide'>"; |
|
| 910 | + |
|
| 911 | + if ( ! empty( $section['title'] ) ) { |
|
| 912 | + echo '<h3 class="redux-section-title">' . wp_kses_post( $section['title'] ) . '</h3>'; |
|
| 913 | + } |
|
| 914 | + |
|
| 915 | + if ( ! empty( $section['desc'] ) ) { |
|
| 916 | + echo '<div class="redux-section-desc">' . wp_kses_post( $section['desc'] ) . '</div>'; |
|
| 917 | + } |
|
| 918 | + |
|
| 919 | + echo '<table class="form-table"><tbody>'; |
|
| 920 | + |
|
| 921 | + foreach ( $section['fields'] as $field ) { |
|
| 922 | + if ( ! $this->check_edit_visibility( $field, false ) ) { |
|
| 923 | + continue; |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + if ( ! empty( $field['permissions'] ) && ! Redux_Helpers::current_user_can( $field['permissions'] ) ) { |
|
| 927 | + continue; |
|
| 928 | + } |
|
| 929 | + |
|
| 930 | + $field['name'] = $this->parent->args['opt_name'] . '[' . $field['id'] . ']'; |
|
| 931 | + |
|
| 932 | + $is_hidden = false; |
|
| 933 | + $ex_style = ''; |
|
| 934 | + if ( isset( $field['hidden'] ) && $field['hidden'] ) { |
|
| 935 | + $is_hidden = true; |
|
| 936 | + $ex_style = ' style="border-bottom: none;"'; |
|
| 937 | + } |
|
| 938 | + |
|
| 939 | + // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 940 | + echo '<tr valign="top"' . $ex_style . '>'; |
|
| 941 | + |
|
| 942 | + $th = $this->parent->render_class->get_header_html( $field ); |
|
| 943 | + |
|
| 944 | + if ( $is_hidden ) { |
|
| 945 | + $str_pos = strpos( $th, 'redux_field_th' ); |
|
| 946 | + |
|
| 947 | + if ( $str_pos > - 1 ) { |
|
| 948 | + $th = str_replace( 'redux_field_th', 'redux_field_th hide', $th ); |
|
| 949 | + } |
|
| 950 | + } |
|
| 951 | + |
|
| 952 | + if ( $sidebar ) { |
|
| 953 | + if ( ! ( isset( $metaterm['args']['sections'] ) && count( $metaterm['args']['sections'] ) === 1 && isset( $metaterm['args']['sections'][0]['fields'] ) && count( $metaterm['args']['sections'][0]['fields'] ) === 1 ) && isset( $field['title'] ) ) { |
|
| 954 | + echo '<th scope="row">'; |
|
| 955 | + if ( ! empty( $th ) ) { |
|
| 956 | + echo $th; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 957 | + } |
|
| 958 | + echo '</th>'; |
|
| 959 | + echo '<td>'; |
|
| 960 | + } |
|
| 961 | + } else { |
|
| 962 | + echo '<td>' . $th; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 963 | + } |
|
| 964 | + |
|
| 965 | + if ( 'section' === $field['type'] && true === $field['indent'] ) { |
|
| 966 | + $field['class'] = $field['class'] ?? ''; |
|
| 967 | + $field['class'] .= 'redux-section-indent-start'; |
|
| 968 | + } |
|
| 969 | + |
|
| 970 | + if ( ! isset( $this->meta[ $this->tag_id ][ $field['id'] ] ) ) { |
|
| 971 | + $this->meta[ $this->tag_id ][ $field['id'] ] = ''; |
|
| 972 | + } |
|
| 973 | + |
|
| 974 | + $this->parent->render_class->field_input( $field, $this->meta[ $this->tag_id ][ $field['id'] ], true ); |
|
| 975 | + echo '</td></tr>'; |
|
| 976 | + } |
|
| 977 | + echo '</tbody></table>'; |
|
| 978 | + echo '</div>'; |
|
| 979 | + } |
|
| 980 | + } |
|
| 981 | + ?> |
|
| 982 | 982 | </div> |
| 983 | 983 | <div class="clear"></div> |
| 984 | 984 | </div> |
| 985 | 985 | <?php |
| 986 | - echo '</div>'; |
|
| 987 | - } |
|
| 988 | - |
|
| 989 | - /** |
|
| 990 | - * Save meta terms |
|
| 991 | - * Runs when a post is saved and does an action which to write panel save scripts can hook into. |
|
| 992 | - * |
|
| 993 | - * @access public |
|
| 994 | - * |
|
| 995 | - * @param mixed $tag_id Yag ID. |
|
| 996 | - * |
|
| 997 | - * @return bool |
|
| 998 | - */ |
|
| 999 | - public function meta_terms_save( $tag_id ): bool { |
|
| 1000 | - |
|
| 1001 | - // Check if our nonce is set. |
|
| 1002 | - if ( ! isset( $_POST['redux_taxonomy_meta_nonce'] ) || ! isset( $_POST[ $this->parent->args['opt_name'] ] ) ) { |
|
| 1003 | - return false; |
|
| 1004 | - } |
|
| 1005 | - |
|
| 1006 | - // Verify that the nonce is valid. |
|
| 1007 | - // Validate fields (if needed). |
|
| 1008 | - if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['redux_taxonomy_meta_nonce'] ) ), 'redux_taxonomy_meta_nonce' ) ) { |
|
| 1009 | - return $tag_id; |
|
| 1010 | - } |
|
| 1011 | - |
|
| 1012 | - $meta = Redux_Taxonomy::get_term_meta( array( 'taxonomy' => $tag_id ) ); |
|
| 1013 | - |
|
| 1014 | - $to_save = array(); |
|
| 1015 | - $to_compare = array(); |
|
| 1016 | - $to_delete = array(); |
|
| 1017 | - $dont_save = true; |
|
| 1018 | - |
|
| 1019 | - $field_args = Redux_Taxonomy::$fields[ $this->parent->args['opt_name'] ]; |
|
| 1020 | - |
|
| 1021 | - foreach ( Redux_Helpers::sanitize_array( wp_unslash( $_POST[ $this->parent->args['opt_name'] ] ) ) as $key => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
|
| 1022 | - |
|
| 1023 | - // Do not save anything the user doesn't have permissions for. |
|
| 1024 | - if ( ! empty( $field_args[ $key ]['permissions'] ) ) { |
|
| 1025 | - foreach ( (array) $field_args[ $key ]['permissions'] as $pv ) { |
|
| 1026 | - |
|
| 1027 | - // Do not save anything the user doesn't have permissions for. |
|
| 1028 | - if ( isset( $field_args[ $key ] ) ) { |
|
| 1029 | - if ( user_can( get_current_user_id(), $pv ) ) { |
|
| 1030 | - break; |
|
| 1031 | - } |
|
| 1032 | - } |
|
| 1033 | - } |
|
| 1034 | - } |
|
| 1035 | - |
|
| 1036 | - // Have to remove the escaping for array comparison. |
|
| 1037 | - if ( is_array( $value ) ) { |
|
| 1038 | - foreach ( $value as $k => $v ) { |
|
| 1039 | - if ( ! is_array( $v ) ) { |
|
| 1040 | - $value[ $k ] = stripslashes( $v ); |
|
| 1041 | - } |
|
| 1042 | - } |
|
| 1043 | - } |
|
| 1044 | - |
|
| 1045 | - $save = true; |
|
| 1046 | - |
|
| 1047 | - // parent_options. |
|
| 1048 | - if ( ! $dont_save && isset( $this->options_defaults[ $key ] ) && $value === $this->options_defaults[ $key ] ) { |
|
| 1049 | - $save = false; |
|
| 1050 | - } |
|
| 1051 | - |
|
| 1052 | - if ( $save && isset( $this->parent_options[ $key ] ) && $this->parent_options[ $key ] !== $value ) { |
|
| 1053 | - $save = false; |
|
| 1054 | - } |
|
| 1055 | - |
|
| 1056 | - if ( $save ) { |
|
| 1057 | - $to_save[ $key ] = $value; |
|
| 1058 | - $to_compare[ $key ] = $this->parent->options[ $key ] ?? ''; |
|
| 1059 | - } else { |
|
| 1060 | - $to_delete[ $key ] = $value; |
|
| 1061 | - } |
|
| 1062 | - } |
|
| 1063 | - |
|
| 1064 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 1065 | - $to_save = apply_filters( 'redux/taxonomy/save/before_validate', $to_save, $to_compare, $this->sections ); |
|
| 1066 | - |
|
| 1067 | - $validate = $this->parent->validate_class->validate( $to_save, $to_compare, $this->sections ); |
|
| 1068 | - |
|
| 1069 | - // Validate fields (if needed). |
|
| 1070 | - foreach ( $to_save as $key => $value ) { |
|
| 1071 | - if ( isset( $validate[ $key ] ) && $validate[ $key ] !== $value ) { |
|
| 1072 | - if ( isset( $meta[ $key ] ) && $validate[ $key ] === $meta[ $key ] ) { |
|
| 1073 | - unset( $to_save[ $key ] ); |
|
| 1074 | - } else { |
|
| 1075 | - $to_save[ $key ] = $validate[ $key ]; |
|
| 1076 | - } |
|
| 1077 | - } |
|
| 1078 | - } |
|
| 1079 | - |
|
| 1080 | - if ( ! empty( $this->parent->errors ) || ! empty( $this->parent->warnings ) ) { |
|
| 1081 | - $this->parent->transients['notices'] = ( isset( $this->parent->transients['notices'] ) && is_array( $this->parent->transients['notices'] ) ) ? $this->parent->transients['notices'] : array(); |
|
| 1082 | - |
|
| 1083 | - if ( ! isset( $this->parent->transients['notices']['errors'] ) || $this->parent->transients['notices']['errors'] !== $this->parent->errors ) { |
|
| 1084 | - $this->parent->transients['notices']['errors'] = $this->parent->errors; |
|
| 1085 | - $update_transients = true; |
|
| 1086 | - } |
|
| 1087 | - |
|
| 1088 | - if ( ! isset( $this->parent->transients['notices']['warnings'] ) || $this->parent->transients['notices']['warnings'] !== $this->parent->warnings ) { |
|
| 1089 | - $this->parent->transients['notices']['warnings'] = $this->parent->warnings; |
|
| 1090 | - $update_transients = true; |
|
| 1091 | - } |
|
| 1092 | - |
|
| 1093 | - if ( isset( $update_transients ) ) { |
|
| 1094 | - $this->parent->transients['notices']['override'] = 1; |
|
| 1095 | - set_transient( $this->parent->args['opt_name'] . '-transients-taxonomy', $this->parent->transients ); |
|
| 1096 | - } |
|
| 1097 | - } |
|
| 1098 | - |
|
| 1099 | - $post_tax = isset( $_POST['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ) : ''; |
|
| 1100 | - |
|
| 1101 | - $check = $this->taxonomy_type_fields[ $post_tax ] ?? array(); |
|
| 1102 | - |
|
| 1103 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 1104 | - $to_save = apply_filters( 'redux/taxonomy/save', $to_save, $to_compare, $this->sections ); |
|
| 1105 | - |
|
| 1106 | - foreach ( $to_save as $key => $value ) { |
|
| 1107 | - $prev_value = $this->meta[ $tag_id ][ $key ] ?? ''; |
|
| 1108 | - |
|
| 1109 | - if ( isset( $check[ $key ] ) ) { |
|
| 1110 | - unset( $check[ $key ] ); |
|
| 1111 | - } |
|
| 1112 | - |
|
| 1113 | - update_term_meta( $tag_id, $key, $value, $prev_value ); |
|
| 1114 | - } |
|
| 1115 | - |
|
| 1116 | - foreach ( $to_delete as $key => $value ) { |
|
| 1117 | - if ( isset( $check[ $key ] ) ) { |
|
| 1118 | - unset( $check[ $key ] ); |
|
| 1119 | - } |
|
| 1120 | - |
|
| 1121 | - $prev_value = $this->meta[ $tag_id ][ $key ] ?? ''; |
|
| 1122 | - delete_term_meta( $tag_id, $key, $prev_value ); |
|
| 1123 | - } |
|
| 1124 | - |
|
| 1125 | - if ( ! empty( $check ) ) { |
|
| 1126 | - foreach ( $check as $key => $value ) { |
|
| 1127 | - delete_term_meta( $tag_id, $key ); |
|
| 1128 | - } |
|
| 1129 | - } |
|
| 1130 | - |
|
| 1131 | - return true; |
|
| 1132 | - } |
|
| 1133 | - |
|
| 1134 | - /** |
|
| 1135 | - * Show any stored error messages. |
|
| 1136 | - * |
|
| 1137 | - * @access public |
|
| 1138 | - * @return void |
|
| 1139 | - */ |
|
| 1140 | - public function meta_terms_show_errors() { |
|
| 1141 | - if ( isset( $this->notices['errors'] ) && ! empty( $this->notices['errors'] ) ) { |
|
| 1142 | - echo '<div id="redux_taxonomy_errors" class="error fade">'; |
|
| 1143 | - echo '<p><strong><span></span> ' . count( $this->notices['errors'] ) . ' ' . esc_html__( 'error(s) were found!', 'redux-framework' ) . '</strong></p>'; |
|
| 1144 | - echo '</div>'; |
|
| 1145 | - } |
|
| 1146 | - |
|
| 1147 | - if ( isset( $this->notices['warnings'] ) && ! empty( $this->notices['warnings'] ) ) { |
|
| 1148 | - echo '<div id="redux_taxonomy_warnings" class="error fade" style="border-left-color: #E8E20C;">'; |
|
| 1149 | - echo '<p><strong><span></span> ' . count( $this->notices['warnings'] ) . ' ' . esc_html__( 'warnings(s) were found!', 'redux-framework' ) . '</strong></p>'; |
|
| 1150 | - echo '</div>'; |
|
| 1151 | - } |
|
| 1152 | - } |
|
| 1153 | - } |
|
| 986 | + echo '</div>'; |
|
| 987 | + } |
|
| 988 | + |
|
| 989 | + /** |
|
| 990 | + * Save meta terms |
|
| 991 | + * Runs when a post is saved and does an action which to write panel save scripts can hook into. |
|
| 992 | + * |
|
| 993 | + * @access public |
|
| 994 | + * |
|
| 995 | + * @param mixed $tag_id Yag ID. |
|
| 996 | + * |
|
| 997 | + * @return bool |
|
| 998 | + */ |
|
| 999 | + public function meta_terms_save( $tag_id ): bool { |
|
| 1000 | + |
|
| 1001 | + // Check if our nonce is set. |
|
| 1002 | + if ( ! isset( $_POST['redux_taxonomy_meta_nonce'] ) || ! isset( $_POST[ $this->parent->args['opt_name'] ] ) ) { |
|
| 1003 | + return false; |
|
| 1004 | + } |
|
| 1005 | + |
|
| 1006 | + // Verify that the nonce is valid. |
|
| 1007 | + // Validate fields (if needed). |
|
| 1008 | + if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['redux_taxonomy_meta_nonce'] ) ), 'redux_taxonomy_meta_nonce' ) ) { |
|
| 1009 | + return $tag_id; |
|
| 1010 | + } |
|
| 1011 | + |
|
| 1012 | + $meta = Redux_Taxonomy::get_term_meta( array( 'taxonomy' => $tag_id ) ); |
|
| 1013 | + |
|
| 1014 | + $to_save = array(); |
|
| 1015 | + $to_compare = array(); |
|
| 1016 | + $to_delete = array(); |
|
| 1017 | + $dont_save = true; |
|
| 1018 | + |
|
| 1019 | + $field_args = Redux_Taxonomy::$fields[ $this->parent->args['opt_name'] ]; |
|
| 1020 | + |
|
| 1021 | + foreach ( Redux_Helpers::sanitize_array( wp_unslash( $_POST[ $this->parent->args['opt_name'] ] ) ) as $key => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
|
| 1022 | + |
|
| 1023 | + // Do not save anything the user doesn't have permissions for. |
|
| 1024 | + if ( ! empty( $field_args[ $key ]['permissions'] ) ) { |
|
| 1025 | + foreach ( (array) $field_args[ $key ]['permissions'] as $pv ) { |
|
| 1026 | + |
|
| 1027 | + // Do not save anything the user doesn't have permissions for. |
|
| 1028 | + if ( isset( $field_args[ $key ] ) ) { |
|
| 1029 | + if ( user_can( get_current_user_id(), $pv ) ) { |
|
| 1030 | + break; |
|
| 1031 | + } |
|
| 1032 | + } |
|
| 1033 | + } |
|
| 1034 | + } |
|
| 1035 | + |
|
| 1036 | + // Have to remove the escaping for array comparison. |
|
| 1037 | + if ( is_array( $value ) ) { |
|
| 1038 | + foreach ( $value as $k => $v ) { |
|
| 1039 | + if ( ! is_array( $v ) ) { |
|
| 1040 | + $value[ $k ] = stripslashes( $v ); |
|
| 1041 | + } |
|
| 1042 | + } |
|
| 1043 | + } |
|
| 1044 | + |
|
| 1045 | + $save = true; |
|
| 1046 | + |
|
| 1047 | + // parent_options. |
|
| 1048 | + if ( ! $dont_save && isset( $this->options_defaults[ $key ] ) && $value === $this->options_defaults[ $key ] ) { |
|
| 1049 | + $save = false; |
|
| 1050 | + } |
|
| 1051 | + |
|
| 1052 | + if ( $save && isset( $this->parent_options[ $key ] ) && $this->parent_options[ $key ] !== $value ) { |
|
| 1053 | + $save = false; |
|
| 1054 | + } |
|
| 1055 | + |
|
| 1056 | + if ( $save ) { |
|
| 1057 | + $to_save[ $key ] = $value; |
|
| 1058 | + $to_compare[ $key ] = $this->parent->options[ $key ] ?? ''; |
|
| 1059 | + } else { |
|
| 1060 | + $to_delete[ $key ] = $value; |
|
| 1061 | + } |
|
| 1062 | + } |
|
| 1063 | + |
|
| 1064 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 1065 | + $to_save = apply_filters( 'redux/taxonomy/save/before_validate', $to_save, $to_compare, $this->sections ); |
|
| 1066 | + |
|
| 1067 | + $validate = $this->parent->validate_class->validate( $to_save, $to_compare, $this->sections ); |
|
| 1068 | + |
|
| 1069 | + // Validate fields (if needed). |
|
| 1070 | + foreach ( $to_save as $key => $value ) { |
|
| 1071 | + if ( isset( $validate[ $key ] ) && $validate[ $key ] !== $value ) { |
|
| 1072 | + if ( isset( $meta[ $key ] ) && $validate[ $key ] === $meta[ $key ] ) { |
|
| 1073 | + unset( $to_save[ $key ] ); |
|
| 1074 | + } else { |
|
| 1075 | + $to_save[ $key ] = $validate[ $key ]; |
|
| 1076 | + } |
|
| 1077 | + } |
|
| 1078 | + } |
|
| 1079 | + |
|
| 1080 | + if ( ! empty( $this->parent->errors ) || ! empty( $this->parent->warnings ) ) { |
|
| 1081 | + $this->parent->transients['notices'] = ( isset( $this->parent->transients['notices'] ) && is_array( $this->parent->transients['notices'] ) ) ? $this->parent->transients['notices'] : array(); |
|
| 1082 | + |
|
| 1083 | + if ( ! isset( $this->parent->transients['notices']['errors'] ) || $this->parent->transients['notices']['errors'] !== $this->parent->errors ) { |
|
| 1084 | + $this->parent->transients['notices']['errors'] = $this->parent->errors; |
|
| 1085 | + $update_transients = true; |
|
| 1086 | + } |
|
| 1087 | + |
|
| 1088 | + if ( ! isset( $this->parent->transients['notices']['warnings'] ) || $this->parent->transients['notices']['warnings'] !== $this->parent->warnings ) { |
|
| 1089 | + $this->parent->transients['notices']['warnings'] = $this->parent->warnings; |
|
| 1090 | + $update_transients = true; |
|
| 1091 | + } |
|
| 1092 | + |
|
| 1093 | + if ( isset( $update_transients ) ) { |
|
| 1094 | + $this->parent->transients['notices']['override'] = 1; |
|
| 1095 | + set_transient( $this->parent->args['opt_name'] . '-transients-taxonomy', $this->parent->transients ); |
|
| 1096 | + } |
|
| 1097 | + } |
|
| 1098 | + |
|
| 1099 | + $post_tax = isset( $_POST['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ) : ''; |
|
| 1100 | + |
|
| 1101 | + $check = $this->taxonomy_type_fields[ $post_tax ] ?? array(); |
|
| 1102 | + |
|
| 1103 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 1104 | + $to_save = apply_filters( 'redux/taxonomy/save', $to_save, $to_compare, $this->sections ); |
|
| 1105 | + |
|
| 1106 | + foreach ( $to_save as $key => $value ) { |
|
| 1107 | + $prev_value = $this->meta[ $tag_id ][ $key ] ?? ''; |
|
| 1108 | + |
|
| 1109 | + if ( isset( $check[ $key ] ) ) { |
|
| 1110 | + unset( $check[ $key ] ); |
|
| 1111 | + } |
|
| 1112 | + |
|
| 1113 | + update_term_meta( $tag_id, $key, $value, $prev_value ); |
|
| 1114 | + } |
|
| 1115 | + |
|
| 1116 | + foreach ( $to_delete as $key => $value ) { |
|
| 1117 | + if ( isset( $check[ $key ] ) ) { |
|
| 1118 | + unset( $check[ $key ] ); |
|
| 1119 | + } |
|
| 1120 | + |
|
| 1121 | + $prev_value = $this->meta[ $tag_id ][ $key ] ?? ''; |
|
| 1122 | + delete_term_meta( $tag_id, $key, $prev_value ); |
|
| 1123 | + } |
|
| 1124 | + |
|
| 1125 | + if ( ! empty( $check ) ) { |
|
| 1126 | + foreach ( $check as $key => $value ) { |
|
| 1127 | + delete_term_meta( $tag_id, $key ); |
|
| 1128 | + } |
|
| 1129 | + } |
|
| 1130 | + |
|
| 1131 | + return true; |
|
| 1132 | + } |
|
| 1133 | + |
|
| 1134 | + /** |
|
| 1135 | + * Show any stored error messages. |
|
| 1136 | + * |
|
| 1137 | + * @access public |
|
| 1138 | + * @return void |
|
| 1139 | + */ |
|
| 1140 | + public function meta_terms_show_errors() { |
|
| 1141 | + if ( isset( $this->notices['errors'] ) && ! empty( $this->notices['errors'] ) ) { |
|
| 1142 | + echo '<div id="redux_taxonomy_errors" class="error fade">'; |
|
| 1143 | + echo '<p><strong><span></span> ' . count( $this->notices['errors'] ) . ' ' . esc_html__( 'error(s) were found!', 'redux-framework' ) . '</strong></p>'; |
|
| 1144 | + echo '</div>'; |
|
| 1145 | + } |
|
| 1146 | + |
|
| 1147 | + if ( isset( $this->notices['warnings'] ) && ! empty( $this->notices['warnings'] ) ) { |
|
| 1148 | + echo '<div id="redux_taxonomy_warnings" class="error fade" style="border-left-color: #E8E20C;">'; |
|
| 1149 | + echo '<p><strong><span></span> ' . count( $this->notices['warnings'] ) . ' ' . esc_html__( 'warnings(s) were found!', 'redux-framework' ) . '</strong></p>'; |
|
| 1150 | + echo '</div>'; |
|
| 1151 | + } |
|
| 1152 | + } |
|
| 1153 | + } |
|
| 1154 | 1154 | } |
| 1155 | 1155 | |
| 1156 | 1156 | class_alias( 'Redux_Extension_Taxonomy', 'ReduxFramework_extension_taxonomy' ); |
@@ -316,7 +316,7 @@ discard block |
||
| 316 | 316 | $box['page_template'] = array( $box['page_template'] ); |
| 317 | 317 | } |
| 318 | 318 | |
| 319 | - $this->wp_links[ $box_id ]['page_template'] = isset( $this->wp_links[ $box_id ]['page_template'] ) ? wp_parse_args( $this->wp_links[ $box_id ]['page_template'], $box['page_template'] ) : $box['page_template']; |
|
| 319 | + $this->wp_links[$box_id]['page_template'] = isset( $this->wp_links[$box_id]['page_template'] ) ? wp_parse_args( $this->wp_links[$box_id]['page_template'], $box['page_template'] ) : $box['page_template']; |
|
| 320 | 320 | } |
| 321 | 321 | |
| 322 | 322 | if ( isset( $box['post_format'] ) && ( in_array( $this->post_type, $this->post_types, true ) || '' === $this->post_type || false === $this->post_type ) ) { |
@@ -324,10 +324,10 @@ discard block |
||
| 324 | 324 | $box['post_format'] = array( $box['post_format'] ); |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | - $this->wp_links[ $box_id ]['post_format'] = isset( $this->wp_links[ $box_id ]['post_format'] ) ? wp_parse_args( $this->wp_links[ $box_id ]['post_format'], $box['post_format'] ) : $box['post_format']; |
|
| 327 | + $this->wp_links[$box_id]['post_format'] = isset( $this->wp_links[$box_id]['post_format'] ) ? wp_parse_args( $this->wp_links[$box_id]['post_format'], $box['post_format'] ) : $box['post_format']; |
|
| 328 | 328 | } |
| 329 | 329 | |
| 330 | - $this->meta[ $this->post_id ] = $this->get_meta( $this->post_id ); |
|
| 330 | + $this->meta[$this->post_id] = $this->get_meta( $this->post_id ); |
|
| 331 | 331 | |
| 332 | 332 | foreach ( $box['sections'] as $sk => $section ) { |
| 333 | 333 | if ( ! empty( $section['fields'] ) ) { |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | if ( ! isset( $field['class'] ) ) { |
| 336 | 336 | $field['class'] = ''; |
| 337 | 337 | |
| 338 | - $this->boxes[ $bk ]['sections'][ $sk ]['fields'][ $fk ] = $field; |
|
| 338 | + $this->boxes[$bk]['sections'][$sk]['fields'][$fk] = $field; |
|
| 339 | 339 | } |
| 340 | 340 | |
| 341 | 341 | if ( $add_field || ( ( is_admin() && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) || ( ! is_admin() ) ) ) { |
@@ -344,43 +344,43 @@ discard block |
||
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | if ( isset( $field['default'] ) ) { |
| 347 | - $this->options_defaults[ $field['id'] ] = $field['default']; |
|
| 347 | + $this->options_defaults[$field['id']] = $field['default']; |
|
| 348 | 348 | } else { |
| 349 | - $this->options_defaults[ $field['id'] ] = $this->field_default( $field ); |
|
| 349 | + $this->options_defaults[$field['id']] = $this->field_default( $field ); |
|
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | foreach ( $box['post_types'] as $type ) { |
| 353 | - $this->post_type_fields[ $type ][ $field['id'] ] = 1; |
|
| 353 | + $this->post_type_fields[$type][$field['id']] = 1; |
|
| 354 | 354 | |
| 355 | 355 | if ( 'repeater' === $field['type'] ) { |
| 356 | 356 | foreach ( $field['fields'] as $val ) { |
| 357 | - $this->post_type_fields[ $type ][ $val['id'] ] = 1; |
|
| 357 | + $this->post_type_fields[$type][$val['id']] = 1; |
|
| 358 | 358 | } |
| 359 | 359 | } |
| 360 | 360 | } |
| 361 | 361 | |
| 362 | 362 | if ( ! empty( $field['output'] ) ) { |
| 363 | - $this->output[ $field['id'] ] = isset( $this->output[ $field['id'] ] ) ? array_merge( $field['output'], $this->output[ $field['id'] ] ) : $field['output']; |
|
| 363 | + $this->output[$field['id']] = isset( $this->output[$field['id']] ) ? array_merge( $field['output'], $this->output[$field['id']] ) : $field['output']; |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | // Detect what field types are being used. |
| 367 | - if ( ! isset( $this->parent->fields[ $field['type'] ][ $field['id'] ] ) ) { |
|
| 368 | - $this->parent->fields[ $field['type'] ][ $field['id'] ] = 1; |
|
| 367 | + if ( ! isset( $this->parent->fields[$field['type']][$field['id']] ) ) { |
|
| 368 | + $this->parent->fields[$field['type']][$field['id']] = 1; |
|
| 369 | 369 | } else { |
| 370 | - $this->parent->fields[ $field['type'] ] = array( $field['id'] => 1 ); |
|
| 370 | + $this->parent->fields[$field['type']] = array( $field['id'] => 1 ); |
|
| 371 | 371 | } |
| 372 | 372 | |
| 373 | - if ( isset( $this->options_defaults[ $field['id'] ] ) ) { |
|
| 374 | - $this->to_replace[ $field['id'] ] = $field; |
|
| 373 | + if ( isset( $this->options_defaults[$field['id']] ) ) { |
|
| 374 | + $this->to_replace[$field['id']] = $field; |
|
| 375 | 375 | } |
| 376 | 376 | } |
| 377 | 377 | |
| 378 | - if ( ! isset( $this->parent->options[ $field['id'] ] ) ) { |
|
| 379 | - $this->parent->sections[ ( count( $this->parent->sections ) - 1 ) ]['fields'][] = $field; |
|
| 378 | + if ( ! isset( $this->parent->options[$field['id']] ) ) { |
|
| 379 | + $this->parent->sections[( count( $this->parent->sections ) - 1 )]['fields'][] = $field; |
|
| 380 | 380 | } |
| 381 | 381 | |
| 382 | - if ( ! isset( $this->meta[ $this->post_id ][ $field['id'] ] ) ) { |
|
| 383 | - $this->meta[ $this->post_id ][ $field['id'] ] = $this->options_defaults[ $field['id'] ]; |
|
| 382 | + if ( ! isset( $this->meta[$this->post_id][$field['id']] ) ) { |
|
| 383 | + $this->meta[$this->post_id][$field['id']] = $this->options_defaults[$field['id']]; |
|
| 384 | 384 | } |
| 385 | 385 | |
| 386 | 386 | // Only override if it exists, and it's not the default. |
@@ -420,8 +420,8 @@ discard block |
||
| 420 | 420 | * @return mixed |
| 421 | 421 | */ |
| 422 | 422 | public function replace_field( array $field ) { |
| 423 | - if ( isset( $this->to_replace[ $field['id'] ] ) ) { |
|
| 424 | - $field = $this->to_replace[ $field['id'] ]; |
|
| 423 | + if ( isset( $this->to_replace[$field['id']] ) ) { |
|
| 424 | + $field = $this->to_replace[$field['id']]; |
|
| 425 | 425 | } |
| 426 | 426 | |
| 427 | 427 | return $field; |
@@ -435,7 +435,7 @@ discard block |
||
| 435 | 435 | * @return array |
| 436 | 436 | */ |
| 437 | 437 | public function override_can_output_css( array $field ): array { |
| 438 | - if ( isset( $this->output[ $field['id'] ] ) ) { |
|
| 438 | + if ( isset( $this->output[$field['id']] ) ) { |
|
| 439 | 439 | $field['force_output'] = true; |
| 440 | 440 | } |
| 441 | 441 | |
@@ -450,8 +450,8 @@ discard block |
||
| 450 | 450 | * @return array |
| 451 | 451 | */ |
| 452 | 452 | public function output_css( array $field ): array { |
| 453 | - if ( isset( $this->output[ $field['id'] ] ) ) { |
|
| 454 | - $field['output'] = $this->output[ $field['id'] ]; |
|
| 453 | + if ( isset( $this->output[$field['id']] ) ) { |
|
| 454 | + $field['output'] = $this->output[$field['id']]; |
|
| 455 | 455 | } |
| 456 | 456 | |
| 457 | 457 | return $field; |
@@ -472,13 +472,13 @@ discard block |
||
| 472 | 472 | $data = wp_parse_args( $meta, $this->options_defaults ); |
| 473 | 473 | |
| 474 | 474 | foreach ( $data as $key => $value ) { |
| 475 | - if ( isset( $meta[ $key ] ) && '' !== $meta[ $key ] ) { |
|
| 476 | - $data[ $key ] = $meta[ $key ]; |
|
| 475 | + if ( isset( $meta[$key] ) && '' !== $meta[$key] ) { |
|
| 476 | + $data[$key] = $meta[$key]; |
|
| 477 | 477 | continue; |
| 478 | 478 | } |
| 479 | 479 | |
| 480 | - if ( isset( $options[ $key ] ) ) { |
|
| 481 | - $data[ $key ] = $options[ $key ]; |
|
| 480 | + if ( isset( $options[$key] ) ) { |
|
| 481 | + $data[$key] = $options[$key]; |
|
| 482 | 482 | } |
| 483 | 483 | } |
| 484 | 484 | |
@@ -504,9 +504,9 @@ discard block |
||
| 504 | 504 | $the_post = $post; |
| 505 | 505 | } |
| 506 | 506 | |
| 507 | - if ( isset( $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ) ) { |
|
| 508 | - $GLOBALS[ $this->parent->args['global_variable'] ] = $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ]; |
|
| 509 | - unset( $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ); |
|
| 507 | + if ( isset( $GLOBALS[$this->parent->args['global_variable'] . '-loop'] ) ) { |
|
| 508 | + $GLOBALS[$this->parent->args['global_variable']] = $GLOBALS[$this->parent->args['global_variable'] . '-loop']; |
|
| 509 | + unset( $GLOBALS[$this->parent->args['global_variable'] . '-loop'] ); |
|
| 510 | 510 | } |
| 511 | 511 | |
| 512 | 512 | // Override these values if they differ from the admin panel defaults. ;) . |
@@ -517,8 +517,8 @@ discard block |
||
| 517 | 517 | } |
| 518 | 518 | |
| 519 | 519 | // Backup the args. |
| 520 | - $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] = $GLOBALS[ $this->parent->args['global_variable'] ]; |
|
| 521 | - $GLOBALS[ $this->parent->args['global_variable'] ] = wp_parse_args( $meta, $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ); |
|
| 520 | + $GLOBALS[$this->parent->args['global_variable'] . '-loop'] = $GLOBALS[$this->parent->args['global_variable']]; |
|
| 521 | + $GLOBALS[$this->parent->args['global_variable']] = wp_parse_args( $meta, $GLOBALS[$this->parent->args['global_variable'] . '-loop'] ); |
|
| 522 | 522 | } |
| 523 | 523 | } |
| 524 | 524 | |
@@ -526,10 +526,10 @@ discard block |
||
| 526 | 526 | * Loop end. |
| 527 | 527 | */ |
| 528 | 528 | public function loop_end() { |
| 529 | - if ( isset( $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ) ) { |
|
| 530 | - $GLOBALS[ $this->parent->args['global_variable'] ] = $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ]; |
|
| 529 | + if ( isset( $GLOBALS[$this->parent->args['global_variable'] . '-loop'] ) ) { |
|
| 530 | + $GLOBALS[$this->parent->args['global_variable']] = $GLOBALS[$this->parent->args['global_variable'] . '-loop']; |
|
| 531 | 531 | |
| 532 | - unset( $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ); |
|
| 532 | + unset( $GLOBALS[$this->parent->args['global_variable'] . '-loop'] ); |
|
| 533 | 533 | } |
| 534 | 534 | } |
| 535 | 535 | |
@@ -554,7 +554,7 @@ discard block |
||
| 554 | 554 | if ( ! empty( $box['post_types'] ) ) { |
| 555 | 555 | if ( ! is_array( $box['post_types'] ) ) { |
| 556 | 556 | $box['post_types'] = array( $box['post_types'] ); |
| 557 | - $this->boxes[ $key ]['post_types'] = $box['post_types']; |
|
| 557 | + $this->boxes[$key]['post_types'] = $box['post_types']; |
|
| 558 | 558 | } |
| 559 | 559 | } |
| 560 | 560 | } |
@@ -644,7 +644,7 @@ discard block |
||
| 644 | 644 | } |
| 645 | 645 | |
| 646 | 646 | if ( isset( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 647 | - $post = (int) sanitize_text_field( wp_unslash( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 647 | + $post = ( int ) sanitize_text_field( wp_unslash( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 648 | 648 | |
| 649 | 649 | if ( ! empty( $post ) ) { |
| 650 | 650 | return $post; |
@@ -665,7 +665,7 @@ discard block |
||
| 665 | 665 | |
| 666 | 666 | $post_id = url_to_postid( $url ); |
| 667 | 667 | |
| 668 | - if ( isset( $post_id ) && '' !== (string) $post_id && 0 !== $post_id ) { |
|
| 668 | + if ( isset( $post_id ) && '' !== ( string ) $post_id && 0 !== $post_id ) { |
|
| 669 | 669 | return $post_id; |
| 670 | 670 | } |
| 671 | 671 | |
@@ -721,9 +721,9 @@ discard block |
||
| 721 | 721 | } |
| 722 | 722 | |
| 723 | 723 | foreach ( $GLOBALS['wp_post_types'] as $key => $value ) { |
| 724 | - if ( ! empty( $_GET[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 724 | + if ( ! empty( $_GET[$key] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 725 | 725 | $args = array( |
| 726 | - 'name' => sanitize_text_field( wp_unslash( $_GET[ $key ] ) ), // phpcs:ignore WordPress.Security.NonceVerification |
|
| 726 | + 'name' => sanitize_text_field( wp_unslash( $_GET[$key] ) ), // phpcs:ignore WordPress.Security.NonceVerification |
|
| 727 | 727 | 'post_type' => $key, |
| 728 | 728 | 'showposts' => 1, |
| 729 | 729 | ); |
@@ -809,7 +809,7 @@ discard block |
||
| 809 | 809 | |
| 810 | 810 | foreach ( $query_vars as $key => $value ) { |
| 811 | 811 | if ( in_array( $key, $wp->public_query_vars, true ) ) { |
| 812 | - $query[ $key ] = $value; |
|
| 812 | + $query[$key] = $value; |
|
| 813 | 813 | } |
| 814 | 814 | } |
| 815 | 815 | |
@@ -843,35 +843,35 @@ discard block |
||
| 843 | 843 | // Taken from class-wp.php. |
| 844 | 844 | foreach ( $GLOBALS['wp_post_types'] as $post_type => $t ) { |
| 845 | 845 | if ( isset( $t->query_var ) ) { |
| 846 | - $post_type_query_vars[ $t->query_var ] = $post_type; |
|
| 846 | + $post_type_query_vars[$t->query_var] = $post_type; |
|
| 847 | 847 | } |
| 848 | 848 | } |
| 849 | 849 | |
| 850 | 850 | foreach ( $wp->public_query_vars as $wpvar ) { |
| 851 | - if ( isset( $wp->extra_query_vars[ $wpvar ] ) ) { |
|
| 852 | - $query[ $wpvar ] = $wp->extra_query_vars[ $wpvar ]; |
|
| 853 | - } elseif ( isset( $_POST[ $wpvar ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 854 | - $query[ $wpvar ] = sanitize_text_field( wp_unslash( $_POST[ $wpvar ] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 855 | - } elseif ( isset( $_GET[ $wpvar ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 856 | - $query[ $wpvar ] = sanitize_text_field( wp_unslash( $_GET[ $wpvar ] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 857 | - } elseif ( isset( $query_vars[ $wpvar ] ) ) { |
|
| 858 | - $query[ $wpvar ] = $query_vars[ $wpvar ]; |
|
| 851 | + if ( isset( $wp->extra_query_vars[$wpvar] ) ) { |
|
| 852 | + $query[$wpvar] = $wp->extra_query_vars[$wpvar]; |
|
| 853 | + } elseif ( isset( $_POST[$wpvar] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 854 | + $query[$wpvar] = sanitize_text_field( wp_unslash( $_POST[$wpvar] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 855 | + } elseif ( isset( $_GET[$wpvar] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 856 | + $query[$wpvar] = sanitize_text_field( wp_unslash( $_GET[$wpvar] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 857 | + } elseif ( isset( $query_vars[$wpvar] ) ) { |
|
| 858 | + $query[$wpvar] = $query_vars[$wpvar]; |
|
| 859 | 859 | } |
| 860 | 860 | |
| 861 | - if ( ! empty( $query[ $wpvar ] ) ) { |
|
| 862 | - if ( ! is_array( $query[ $wpvar ] ) ) { |
|
| 863 | - $query[ $wpvar ] = (string) $query[ $wpvar ]; |
|
| 861 | + if ( ! empty( $query[$wpvar] ) ) { |
|
| 862 | + if ( ! is_array( $query[$wpvar] ) ) { |
|
| 863 | + $query[$wpvar] = ( string ) $query[$wpvar]; |
|
| 864 | 864 | } else { |
| 865 | - foreach ( $query[ $wpvar ] as $vkey => $v ) { |
|
| 865 | + foreach ( $query[$wpvar] as $vkey => $v ) { |
|
| 866 | 866 | if ( ! is_object( $v ) ) { |
| 867 | - $query[ $wpvar ][ $vkey ] = (string) $v; |
|
| 867 | + $query[$wpvar][$vkey] = ( string ) $v; |
|
| 868 | 868 | } |
| 869 | 869 | } |
| 870 | 870 | } |
| 871 | 871 | |
| 872 | - if ( isset( $post_type_query_vars[ $wpvar ] ) ) { |
|
| 873 | - $query['post_type'] = $post_type_query_vars[ $wpvar ]; |
|
| 874 | - $query['name'] = $query[ $wpvar ]; |
|
| 872 | + if ( isset( $post_type_query_vars[$wpvar] ) ) { |
|
| 873 | + $query['post_type'] = $post_type_query_vars[$wpvar]; |
|
| 874 | + $query['name'] = $query[$wpvar]; |
|
| 875 | 875 | } |
| 876 | 876 | } |
| 877 | 877 | } |
@@ -930,7 +930,7 @@ discard block |
||
| 930 | 930 | } else { |
| 931 | 931 | $section['id'] = sanitize_text_field( $section['title'] ); |
| 932 | 932 | } |
| 933 | - $this->boxes[ $key ]['sections'][ $sk ] = $section; |
|
| 933 | + $this->boxes[$key]['sections'][$sk] = $section; |
|
| 934 | 934 | } |
| 935 | 935 | if ( isset( $section['fields'] ) ) { |
| 936 | 936 | foreach ( $section['fields'] as $k => $field ) { |
@@ -940,15 +940,15 @@ discard block |
||
| 940 | 940 | } |
| 941 | 941 | |
| 942 | 942 | if ( 'ace_editor' === $field['type'] && isset( $field['options'] ) ) { |
| 943 | - $this->boxes[ $key ]['sections'][ $sk ]['fields'][ $k ]['args'] = $field['options']; |
|
| 944 | - unset( $this->boxes[ $key ]['sections'][ $sk ]['fields'][ $k ]['options'] ); |
|
| 943 | + $this->boxes[$key]['sections'][$sk]['fields'][$k]['args'] = $field['options']; |
|
| 944 | + unset( $this->boxes[$key]['sections'][$sk]['fields'][$k]['options'] ); |
|
| 945 | 945 | } |
| 946 | 946 | |
| 947 | 947 | if ( 'section' === $field['type'] && isset( $field['indent'] ) && ( true === $field['indent'] || 'true' === $field['indent'] ) ) { |
| 948 | 948 | $field['class'] = $field['class'] ?? ''; |
| 949 | 949 | $field['class'] .= 'redux-section-indent-start'; |
| 950 | 950 | |
| 951 | - $this->boxes[ $key ]['sections'][ $sk ]['fields'][ $k ] = $field; |
|
| 951 | + $this->boxes[$key]['sections'][$sk]['fields'][$k] = $field; |
|
| 952 | 952 | } |
| 953 | 953 | |
| 954 | 954 | if ( ! isset( $this->parent->options_defaults_class ) ) { |
@@ -970,8 +970,8 @@ discard block |
||
| 970 | 970 | } |
| 971 | 971 | } |
| 972 | 972 | |
| 973 | - if ( empty( $this->meta[ $this->post_id ] ) ) { |
|
| 974 | - $this->meta[ $this->post_id ] = $this->get_meta( $this->post_id ); |
|
| 973 | + if ( empty( $this->meta[$this->post_id] ) ) { |
|
| 974 | + $this->meta[$this->post_id] = $this->get_meta( $this->post_id ); |
|
| 975 | 975 | } |
| 976 | 976 | } |
| 977 | 977 | |
@@ -1085,8 +1085,8 @@ discard block |
||
| 1085 | 1085 | |
| 1086 | 1086 | $this->options = $this->parent->options; |
| 1087 | 1087 | |
| 1088 | - if ( isset( $this->parent->options[ $field_id['id'] ] ) && isset( $this->parent->options_defaults[ $field_id['id'] ] ) && $this->parent->options[ $field_id['id'] ] !== $this->parent->options_defaults[ $field_id['id'] ] ) { |
|
| 1089 | - return $this->parent->options[ $field_id['id'] ]; |
|
| 1088 | + if ( isset( $this->parent->options[$field_id['id']] ) && isset( $this->parent->options_defaults[$field_id['id']] ) && $this->parent->options[$field_id['id']] !== $this->parent->options_defaults[$field_id['id']] ) { |
|
| 1089 | + return $this->parent->options[$field_id['id']]; |
|
| 1090 | 1090 | } else { |
| 1091 | 1091 | if ( empty( $this->options_defaults ) ) { |
| 1092 | 1092 | $this->default_values(); // fill cache. |
@@ -1094,11 +1094,11 @@ discard block |
||
| 1094 | 1094 | |
| 1095 | 1095 | $data = ''; |
| 1096 | 1096 | if ( ! empty( $this->options_defaults ) ) { |
| 1097 | - $data = $this->options_defaults[ $field_id['id'] ] ?? ''; |
|
| 1097 | + $data = $this->options_defaults[$field_id['id']] ?? ''; |
|
| 1098 | 1098 | } |
| 1099 | 1099 | |
| 1100 | - if ( empty( $data ) && isset( $this->parent->options_defaults[ $field_id['id'] ] ) ) { |
|
| 1101 | - $data = $this->parent->options_defaults[ $field_id['id'] ] ?? ''; |
|
| 1100 | + if ( empty( $data ) && isset( $this->parent->options_defaults[$field_id['id']] ) ) { |
|
| 1101 | + $data = $this->parent->options_defaults[$field_id['id']] ?? ''; |
|
| 1102 | 1102 | } |
| 1103 | 1103 | |
| 1104 | 1104 | return $data; |
@@ -1113,8 +1113,8 @@ discard block |
||
| 1113 | 1113 | * @return array |
| 1114 | 1114 | */ |
| 1115 | 1115 | private function get_meta( $id ): array { |
| 1116 | - if ( ! isset( $this->meta[ $id ] ) ) { |
|
| 1117 | - $this->meta[ $id ] = array(); |
|
| 1116 | + if ( ! isset( $this->meta[$id] ) ) { |
|
| 1117 | + $this->meta[$id] = array(); |
|
| 1118 | 1118 | $o_data = get_post_meta( $id ); |
| 1119 | 1119 | |
| 1120 | 1120 | // phpcs:ignore WordPress.NamingConventions.ValidHookName |
@@ -1123,30 +1123,30 @@ discard block |
||
| 1123 | 1123 | if ( ! empty( $o_data ) ) { |
| 1124 | 1124 | foreach ( $o_data as $key => $value ) { |
| 1125 | 1125 | if ( 1 === count( $value ) ) { |
| 1126 | - $this->meta[ $id ][ $key ] = maybe_unserialize( $value[0] ); |
|
| 1126 | + $this->meta[$id][$key] = maybe_unserialize( $value[0] ); |
|
| 1127 | 1127 | } else { |
| 1128 | 1128 | $new_value = array_map( 'maybe_unserialize', $value ); |
| 1129 | 1129 | |
| 1130 | - $this->meta[ $id ][ $key ] = $new_value[0]; |
|
| 1130 | + $this->meta[$id][$key] = $new_value[0]; |
|
| 1131 | 1131 | } |
| 1132 | 1132 | } |
| 1133 | 1133 | } |
| 1134 | 1134 | |
| 1135 | - if ( isset( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ) ) { |
|
| 1136 | - $data = maybe_unserialize( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ); |
|
| 1135 | + if ( isset( $this->meta[$id][$this->parent->args['opt_name']] ) ) { |
|
| 1136 | + $data = maybe_unserialize( $this->meta[$id][$this->parent->args['opt_name']] ); |
|
| 1137 | 1137 | |
| 1138 | 1138 | foreach ( $data as $key => $value ) { |
| 1139 | - $this->meta[ $id ][ $key ] = $value; |
|
| 1139 | + $this->meta[$id][$key] = $value; |
|
| 1140 | 1140 | update_post_meta( $id, $key, $value ); |
| 1141 | 1141 | } |
| 1142 | 1142 | |
| 1143 | - unset( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ); |
|
| 1143 | + unset( $this->meta[$id][$this->parent->args['opt_name']] ); |
|
| 1144 | 1144 | |
| 1145 | 1145 | delete_post_meta( $id, $this->parent->args['opt_name'] ); |
| 1146 | 1146 | } |
| 1147 | 1147 | } |
| 1148 | 1148 | |
| 1149 | - return $this->meta[ $id ]; |
|
| 1149 | + return $this->meta[$id]; |
|
| 1150 | 1150 | } |
| 1151 | 1151 | |
| 1152 | 1152 | /** |
@@ -1162,29 +1162,29 @@ discard block |
||
| 1162 | 1162 | |
| 1163 | 1163 | // Override these values if they differ from the admin panel defaults. ;) . |
| 1164 | 1164 | if ( isset( $the_post->post_type ) && in_array( $the_post->post_type, $this->post_types, true ) ) { |
| 1165 | - if ( isset( $this->post_type_values[ $the_post->post_type ] ) ) { |
|
| 1166 | - $meta = $this->post_type_fields[ $the_post->post_type ]; |
|
| 1165 | + if ( isset( $this->post_type_values[$the_post->post_type] ) ) { |
|
| 1166 | + $meta = $this->post_type_fields[$the_post->post_type]; |
|
| 1167 | 1167 | } else { |
| 1168 | 1168 | $defaults = array(); |
| 1169 | - if ( ! empty( $this->post_type_fields[ $the_post->post_type ] ) ) { |
|
| 1170 | - foreach ( $this->post_type_fields[ $the_post->post_type ] as $key => $null ) { |
|
| 1171 | - if ( isset( $this->options_defaults[ $key ] ) ) { |
|
| 1172 | - $defaults[ $key ] = $this->options_defaults[ $key ]; |
|
| 1169 | + if ( ! empty( $this->post_type_fields[$the_post->post_type] ) ) { |
|
| 1170 | + foreach ( $this->post_type_fields[$the_post->post_type] as $key => $null ) { |
|
| 1171 | + if ( isset( $this->options_defaults[$key] ) ) { |
|
| 1172 | + $defaults[$key] = $this->options_defaults[$key]; |
|
| 1173 | 1173 | } |
| 1174 | 1174 | } |
| 1175 | 1175 | } |
| 1176 | 1176 | |
| 1177 | 1177 | $meta = wp_parse_args( $this->get_meta( $the_post->ID ), $defaults ); |
| 1178 | 1178 | |
| 1179 | - $this->post_type_fields[ $the_post->post_type ] = $meta; |
|
| 1179 | + $this->post_type_fields[$the_post->post_type] = $meta; |
|
| 1180 | 1180 | } |
| 1181 | 1181 | |
| 1182 | 1182 | if ( ! empty( $meta_key ) ) { |
| 1183 | - if ( ! isset( $meta[ $meta_key ] ) ) { |
|
| 1184 | - $meta[ $meta_key ] = $def_val; |
|
| 1183 | + if ( ! isset( $meta[$meta_key] ) ) { |
|
| 1184 | + $meta[$meta_key] = $def_val; |
|
| 1185 | 1185 | } |
| 1186 | 1186 | |
| 1187 | - return $meta[ $meta_key ]; |
|
| 1187 | + return $meta[$meta_key]; |
|
| 1188 | 1188 | } else { |
| 1189 | 1189 | return $meta; |
| 1190 | 1190 | } |
@@ -1335,11 +1335,11 @@ discard block |
||
| 1335 | 1335 | $field['class'] .= 'redux-section-indent-start'; |
| 1336 | 1336 | } |
| 1337 | 1337 | |
| 1338 | - if ( ! isset( $this->meta[ $this->post_id ][ $field['id'] ] ) ) { |
|
| 1339 | - $this->meta[ $this->post_id ][ $field['id'] ] = ''; |
|
| 1338 | + if ( ! isset( $this->meta[$this->post_id][$field['id']] ) ) { |
|
| 1339 | + $this->meta[$this->post_id][$field['id']] = ''; |
|
| 1340 | 1340 | } |
| 1341 | 1341 | |
| 1342 | - $this->parent->render_class->field_input( $field, $this->meta[ $this->post_id ][ $field['id'] ], true ); |
|
| 1342 | + $this->parent->render_class->field_input( $field, $this->meta[$this->post_id][$field['id']], true ); |
|
| 1343 | 1343 | echo '</td></tr>'; |
| 1344 | 1344 | } |
| 1345 | 1345 | echo '</tbody></table>'; |
@@ -1374,7 +1374,7 @@ discard block |
||
| 1374 | 1374 | } |
| 1375 | 1375 | |
| 1376 | 1376 | // Check if our nonce is set. |
| 1377 | - if ( ! isset( $_POST['redux_metaboxes_meta_nonce'] ) || ! isset( $_POST[ $this->parent->args['opt_name'] ] ) ) { |
|
| 1377 | + if ( ! isset( $_POST['redux_metaboxes_meta_nonce'] ) || ! isset( $_POST[$this->parent->args['opt_name']] ) ) { |
|
| 1378 | 1378 | return $post_id; |
| 1379 | 1379 | } |
| 1380 | 1380 | |
@@ -1396,17 +1396,17 @@ discard block |
||
| 1396 | 1396 | } |
| 1397 | 1397 | |
| 1398 | 1398 | // Import. |
| 1399 | - if ( isset( $_POST[ $this->parent->args['opt_name'] ]['import_code'] ) && ! empty( $_POST[ $this->parent->args['opt_name'] ]['import_code'] ) ) { |
|
| 1400 | - $import = json_decode( sanitize_text_field( wp_unslash( $_POST[ $this->parent->args['opt_name'] ]['import_code'] ) ), true ); |
|
| 1401 | - unset( $_POST[ $this->parent->args['opt_name'] ]['import_code'] ); |
|
| 1399 | + if ( isset( $_POST[$this->parent->args['opt_name']]['import_code'] ) && ! empty( $_POST[$this->parent->args['opt_name']]['import_code'] ) ) { |
|
| 1400 | + $import = json_decode( sanitize_text_field( wp_unslash( $_POST[$this->parent->args['opt_name']]['import_code'] ) ), true ); |
|
| 1401 | + unset( $_POST[$this->parent->args['opt_name']]['import_code'] ); |
|
| 1402 | 1402 | |
| 1403 | - foreach ( Redux_Helpers::sanitize_array( wp_unslash( $_POST[ $this->parent->args['opt_name'] ] ) ) as $key => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
|
| 1404 | - if ( ! isset( $import[ $key ] ) ) { |
|
| 1405 | - $import[ $key ] = $value; |
|
| 1403 | + foreach ( Redux_Helpers::sanitize_array( wp_unslash( $_POST[$this->parent->args['opt_name']] ) ) as $key => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
|
| 1404 | + if ( ! isset( $import[$key] ) ) { |
|
| 1405 | + $import[$key] = $value; |
|
| 1406 | 1406 | } |
| 1407 | 1407 | } |
| 1408 | 1408 | |
| 1409 | - $_POST[ $this->parent->args['opt_name'] ] = $import; |
|
| 1409 | + $_POST[$this->parent->args['opt_name']] = $import; |
|
| 1410 | 1410 | } |
| 1411 | 1411 | |
| 1412 | 1412 | $to_save = array(); |
@@ -1418,13 +1418,13 @@ discard block |
||
| 1418 | 1418 | $dont_save = false; |
| 1419 | 1419 | } |
| 1420 | 1420 | |
| 1421 | - foreach ( Redux_Helpers::sanitize_array( wp_unslash( $_POST[ $this->parent->args['opt_name'] ] ) ) as $key => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
|
| 1421 | + foreach ( Redux_Helpers::sanitize_array( wp_unslash( $_POST[$this->parent->args['opt_name']] ) ) as $key => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
|
| 1422 | 1422 | |
| 1423 | 1423 | // Have to remove the escaping for array comparison. |
| 1424 | 1424 | if ( is_array( $value ) ) { |
| 1425 | 1425 | foreach ( $value as $k => $v ) { |
| 1426 | 1426 | if ( ! is_array( $v ) ) { |
| 1427 | - $value[ $k ] = wp_unslash( $v ); |
|
| 1427 | + $value[$k] = wp_unslash( $v ); |
|
| 1428 | 1428 | } |
| 1429 | 1429 | } |
| 1430 | 1430 | } |
@@ -1432,19 +1432,19 @@ discard block |
||
| 1432 | 1432 | $save = true; |
| 1433 | 1433 | |
| 1434 | 1434 | // parent_options. |
| 1435 | - if ( ! $dont_save && isset( $this->options_defaults[ $key ] ) && $value === $this->options_defaults[ $key ] ) { |
|
| 1435 | + if ( ! $dont_save && isset( $this->options_defaults[$key] ) && $value === $this->options_defaults[$key] ) { |
|
| 1436 | 1436 | $save = false; |
| 1437 | 1437 | } |
| 1438 | 1438 | |
| 1439 | - if ( $save && isset( $this->parent_options[ $key ] ) && $this->parent_options[ $key ] !== $value ) { |
|
| 1439 | + if ( $save && isset( $this->parent_options[$key] ) && $this->parent_options[$key] !== $value ) { |
|
| 1440 | 1440 | $save = false; |
| 1441 | 1441 | } |
| 1442 | 1442 | |
| 1443 | 1443 | if ( $save ) { |
| 1444 | - $to_save[ $key ] = $value; |
|
| 1445 | - $to_compare[ $key ] = $this->parent->options[ $key ] ?? ''; |
|
| 1444 | + $to_save[$key] = $value; |
|
| 1445 | + $to_compare[$key] = $this->parent->options[$key] ?? ''; |
|
| 1446 | 1446 | } else { |
| 1447 | - $to_delete[ $key ] = $value; |
|
| 1447 | + $to_delete[$key] = $value; |
|
| 1448 | 1448 | } |
| 1449 | 1449 | } |
| 1450 | 1450 | |
@@ -1455,11 +1455,11 @@ discard block |
||
| 1455 | 1455 | |
| 1456 | 1456 | // Validate fields (if needed). |
| 1457 | 1457 | foreach ( $to_save as $key => $value ) { |
| 1458 | - if ( isset( $validate[ $key ] ) && $value !== $validate[ $key ] ) { |
|
| 1459 | - if ( isset( $this->parent->options[ $key ] ) && $validate[ $key ] === $this->parent->options[ $key ] ) { |
|
| 1460 | - unset( $to_save[ $key ] ); |
|
| 1458 | + if ( isset( $validate[$key] ) && $value !== $validate[$key] ) { |
|
| 1459 | + if ( isset( $this->parent->options[$key] ) && $validate[$key] === $this->parent->options[$key] ) { |
|
| 1460 | + unset( $to_save[$key] ); |
|
| 1461 | 1461 | } else { |
| 1462 | - $to_save[ $key ] = $validate[ $key ]; |
|
| 1462 | + $to_save[$key] = $validate[$key]; |
|
| 1463 | 1463 | } |
| 1464 | 1464 | } |
| 1465 | 1465 | } |
@@ -1484,28 +1484,28 @@ discard block |
||
| 1484 | 1484 | } |
| 1485 | 1485 | |
| 1486 | 1486 | if ( isset( $_POST['post_type'] ) ) { |
| 1487 | - $check = $this->post_type_fields[ sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) ]; |
|
| 1487 | + $check = $this->post_type_fields[sanitize_text_field( wp_unslash( $_POST['post_type'] ) )]; |
|
| 1488 | 1488 | } |
| 1489 | 1489 | |
| 1490 | 1490 | // phpcs:ignore WordPress.NamingConventions.ValidHookName |
| 1491 | 1491 | $to_save = apply_filters( 'redux/metaboxes/save', $to_save, $to_compare, $this->sections ); |
| 1492 | 1492 | |
| 1493 | 1493 | foreach ( $to_save as $key => $value ) { |
| 1494 | - $prev_value = $this->meta[ $post_id ][ $key ] ?? ''; |
|
| 1494 | + $prev_value = $this->meta[$post_id][$key] ?? ''; |
|
| 1495 | 1495 | |
| 1496 | - if ( isset( $check[ $key ] ) ) { |
|
| 1497 | - unset( $check[ $key ] ); |
|
| 1496 | + if ( isset( $check[$key] ) ) { |
|
| 1497 | + unset( $check[$key] ); |
|
| 1498 | 1498 | } |
| 1499 | 1499 | |
| 1500 | 1500 | update_post_meta( $post_id, $key, $value, $prev_value ); |
| 1501 | 1501 | } |
| 1502 | 1502 | |
| 1503 | 1503 | foreach ( $to_delete as $key => $value ) { |
| 1504 | - if ( isset( $check[ $key ] ) ) { |
|
| 1505 | - unset( $check[ $key ] ); |
|
| 1504 | + if ( isset( $check[$key] ) ) { |
|
| 1505 | + unset( $check[$key] ); |
|
| 1506 | 1506 | } |
| 1507 | 1507 | |
| 1508 | - $prev_value = $this->meta[ $post_id ][ $key ] ?? ''; |
|
| 1508 | + $prev_value = $this->meta[$post_id][$key] ?? ''; |
|
| 1509 | 1509 | delete_post_meta( $post_id, $key, $prev_value ); |
| 1510 | 1510 | } |
| 1511 | 1511 | |
@@ -14,1184 +14,1184 @@ discard block |
||
| 14 | 14 | // Don't duplicate me! |
| 15 | 15 | if ( ! class_exists( 'Redux_Extension_Metaboxes', false ) ) { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Main Redux_Extension_Metaboxes class |
|
| 19 | - * |
|
| 20 | - * @since 1.0.0 |
|
| 21 | - */ |
|
| 22 | - class Redux_Extension_Metaboxes extends Redux_Extension_Abstract { |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Extension version. |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - public static $version = '4.2.0'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Extension friendly name. |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - public string $extension_name = 'Metaboxes'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Boxes array. |
|
| 40 | - * |
|
| 41 | - * @var array|null |
|
| 42 | - */ |
|
| 43 | - public ?array $boxes = array(); |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Post types array. |
|
| 47 | - * |
|
| 48 | - * @var array|null |
|
| 49 | - */ |
|
| 50 | - public ?array $post_types = array(); |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Post type. |
|
| 54 | - * |
|
| 55 | - * @var string|null |
|
| 56 | - */ |
|
| 57 | - public ?string $post_type; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Sections array. |
|
| 61 | - * |
|
| 62 | - * @var array|null |
|
| 63 | - */ |
|
| 64 | - public ?array $orig_args; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Sections array. |
|
| 68 | - * |
|
| 69 | - * @var array|null |
|
| 70 | - */ |
|
| 71 | - public ?array $sections = array(); |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * CSS output array. |
|
| 75 | - * |
|
| 76 | - * @var array|null |
|
| 77 | - */ |
|
| 78 | - public ?array $output = array(); |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Options array. |
|
| 82 | - * |
|
| 83 | - * @var array |
|
| 84 | - */ |
|
| 85 | - public array $options = array(); |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Parent options array. |
|
| 89 | - * |
|
| 90 | - * @var array |
|
| 91 | - */ |
|
| 92 | - public array $parent_options = array(); |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Parent defaults array. |
|
| 96 | - * |
|
| 97 | - * @var array |
|
| 98 | - */ |
|
| 99 | - public array $parent_defaults = array(); |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Post type fields array. |
|
| 103 | - * |
|
| 104 | - * @var array |
|
| 105 | - */ |
|
| 106 | - public array $post_type_fields = array(); |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Options defaults array. |
|
| 110 | - * |
|
| 111 | - * @var array |
|
| 112 | - */ |
|
| 113 | - public array $options_defaults = array(); |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Replace array. |
|
| 117 | - * |
|
| 118 | - * @var array |
|
| 119 | - */ |
|
| 120 | - public array $to_replace = array(); |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Meta data array. |
|
| 124 | - * |
|
| 125 | - * @var array |
|
| 126 | - */ |
|
| 127 | - public array $meta = array(); |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Post ID. |
|
| 131 | - * |
|
| 132 | - * @var null|int |
|
| 133 | - */ |
|
| 134 | - public ?int $post_id = 0; |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Base URI. |
|
| 138 | - * |
|
| 139 | - * @var string|null |
|
| 140 | - */ |
|
| 141 | - public ?string $base_url; |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * WP_Links array. |
|
| 145 | - * |
|
| 146 | - * @var array |
|
| 147 | - */ |
|
| 148 | - public array $wp_links = array(); |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Notices. |
|
| 152 | - * |
|
| 153 | - * @var array |
|
| 154 | - */ |
|
| 155 | - private array $notices = array(); |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * ReduxFramework_extension_metaboxes constructor. |
|
| 159 | - * |
|
| 160 | - * @param object $redux ReduxFramework object. |
|
| 161 | - */ |
|
| 162 | - public function __construct( $redux ) { |
|
| 163 | - global $pagenow; |
|
| 164 | - |
|
| 165 | - parent::__construct( $redux, __FILE__ ); |
|
| 166 | - |
|
| 167 | - $this->parent->extensions['metaboxes'] = $this; |
|
| 168 | - |
|
| 169 | - $this->extension_dir = trailingslashit( str_replace( '\\', '/', __DIR__ ) ); |
|
| 170 | - $this->extension_url = site_url( str_replace( trailingslashit( str_replace( '\\', '/', ABSPATH ) ), '', $this->extension_dir ) ); |
|
| 171 | - |
|
| 172 | - // Only run metaboxes on the pages/posts, not the front-end. |
|
| 173 | - // The DOING_AJAX check allows for redux_post_meta to work inside |
|
| 174 | - // AJAX calls. - kp. |
|
| 175 | - if ( 'post-new.php' !== $pagenow && 'post.php' !== $pagenow ) { |
|
| 176 | - if ( is_admin() ) { |
|
| 177 | - if ( defined( 'DOING_AJAX' ) && ! DOING_AJAX ) { |
|
| 178 | - return; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - return; |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - if ( 'wp-cron.php' === $pagenow || 'wp-comments-post.php' === $pagenow ) { |
|
| 186 | - return; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - // Must not update the DB when just updating metaboxes. Sheesh. |
|
| 190 | - if ( is_admin() && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) { |
|
| 191 | - $this->parent->never_save_to_db = true; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - include_once Redux_Core::$dir . 'inc/extensions/metaboxes/redux-metaboxes-helpers.php'; |
|
| 195 | - |
|
| 196 | - // phpcs:ignore Generic.Strings.UnnecessaryStringConcat |
|
| 197 | - add_action( 'add_' . 'meta_' . 'boxes', array( $this, 'add' ) ); |
|
| 198 | - add_action( 'save_post', array( $this, 'meta_boxes_save' ), 1, 2 ); |
|
| 199 | - add_action( 'pre_post_update', array( $this, 'pre_post_update' ) ); |
|
| 200 | - add_action( 'admin_notices', array( $this, 'meta_boxes_show_errors' ), 0 ); |
|
| 201 | - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 20 ); |
|
| 202 | - |
|
| 203 | - // Global variable overrides for within loops. |
|
| 204 | - add_action( 'the_post', array( $this, 'loop_start' ), 0 ); |
|
| 205 | - add_action( 'loop_end', array( $this, 'loop_end' ), 0 ); |
|
| 206 | - |
|
| 207 | - $this->init(); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Added class names to metabox DIV. |
|
| 212 | - * |
|
| 213 | - * @param array $classes Class array. |
|
| 214 | - * |
|
| 215 | - * @return array |
|
| 216 | - */ |
|
| 217 | - public function add_box_classes( array $classes ): array { |
|
| 218 | - $classes[] = 'redux-metabox'; |
|
| 219 | - $classes[] = 'redux-' . $this->parent->args['opt_name']; |
|
| 220 | - |
|
| 221 | - if ( '' !== $this->parent->args['class'] ) { |
|
| 222 | - $classes[] = $this->parent->args['class']; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - return $classes; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Class init. |
|
| 230 | - */ |
|
| 231 | - public function init() { |
|
| 232 | - global $pagenow; |
|
| 233 | - |
|
| 234 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 235 | - $this->boxes = apply_filters( 'redux/metaboxes/' . $this->parent->args['opt_name'] . '/boxes', $this->boxes, $this->parent->args['opt_name'] ); |
|
| 236 | - |
|
| 237 | - if ( empty( $this->boxes ) && class_exists( 'Redux_Metaboxes' ) ) { |
|
| 238 | - $this->boxes = Redux_Metaboxes::construct_boxes( $this->parent->args['opt_name'] ); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - if ( empty( $this->boxes ) || ! is_array( $this->boxes ) ) { |
|
| 242 | - return; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - if ( isset( Redux_Core::$server['HTTP_HOST'] ) && isset( Redux_Core::$server['REQUEST_URI'] ) ) { |
|
| 246 | - $this->base_url = ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( wp_unslash( Redux_Core::$server['HTTP_HOST'] ) ) . sanitize_text_field( wp_unslash( Redux_Core::$server['REQUEST_URI'] ) ); // Safe & Reliable. |
|
| 247 | - $this->post_id = $this->url_to_postid( ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( wp_unslash( Redux_Core::$server['HTTP_HOST'] ) ) . sanitize_text_field( wp_unslash( Redux_Core::$server['REQUEST_URI'] ) ) ); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - if ( is_admin() && isset( $_GET['post_type'] ) && ! empty( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 251 | - $this->post_type = sanitize_text_field( wp_unslash( $_GET['post_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 252 | - } else { |
|
| 253 | - $this->post_type = get_post_type( $this->post_id ); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - foreach ( $this->boxes as $bk => $box ) { |
|
| 257 | - |
|
| 258 | - // If the post-ids for this box are set, we're limiting to the current post id. |
|
| 259 | - if ( ! empty( $box['post_ids'] ) ) { |
|
| 260 | - if ( ! is_array( $box['post_ids'] ) ) { |
|
| 261 | - $box['post_ids'] = array( $box['post_ids'] ); |
|
| 262 | - } |
|
| 263 | - if ( ! in_array( $this->post_id, $box['post_ids'], true ) ) { |
|
| 264 | - continue; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - if ( ! empty( $box['sections'] ) ) { |
|
| 269 | - $this->sections = wp_parse_args( $this->sections, $box['sections'] ); |
|
| 270 | - |
|
| 271 | - $this->post_types = wp_parse_args( $this->post_types, $box['post_types'] ); |
|
| 272 | - |
|
| 273 | - // Checking to override the parent variables. |
|
| 274 | - $add_field = false; |
|
| 275 | - |
|
| 276 | - foreach ( $box['post_types'] as $type ) { |
|
| 277 | - if ( $this->post_type === $type ) { |
|
| 278 | - $add_field = true; |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - // Replacing all the fields. |
|
| 283 | - if ( $add_field || ( ( is_admin() && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) || ( ! is_admin() ) ) ) { |
|
| 284 | - $run_hooks = true; |
|
| 285 | - |
|
| 286 | - $box_id = 'redux-' . $this->parent->args['opt_name'] . '-metabox-' . $box['id']; |
|
| 287 | - |
|
| 288 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 289 | - do_action( 'redux/' . $this->parent->args['opt_name'] . '/extensions/metabox/init', $this, $box ); |
|
| 290 | - |
|
| 291 | - if ( isset( $box['page_template'] ) && 'page' === $this->post_type ) { |
|
| 292 | - if ( ! is_array( $box['page_template'] ) ) { |
|
| 293 | - $box['page_template'] = array( $box['page_template'] ); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - $this->wp_links[ $box_id ]['page_template'] = isset( $this->wp_links[ $box_id ]['page_template'] ) ? wp_parse_args( $this->wp_links[ $box_id ]['page_template'], $box['page_template'] ) : $box['page_template']; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - if ( isset( $box['post_format'] ) && ( in_array( $this->post_type, $this->post_types, true ) || '' === $this->post_type || false === $this->post_type ) ) { |
|
| 300 | - if ( ! is_array( $box['post_format'] ) ) { |
|
| 301 | - $box['post_format'] = array( $box['post_format'] ); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - $this->wp_links[ $box_id ]['post_format'] = isset( $this->wp_links[ $box_id ]['post_format'] ) ? wp_parse_args( $this->wp_links[ $box_id ]['post_format'], $box['post_format'] ) : $box['post_format']; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - $this->meta[ $this->post_id ] = $this->get_meta( $this->post_id ); |
|
| 308 | - |
|
| 309 | - foreach ( $box['sections'] as $sk => $section ) { |
|
| 310 | - if ( ! empty( $section['fields'] ) ) { |
|
| 311 | - foreach ( $section['fields'] as $fk => $field ) { |
|
| 312 | - if ( ! isset( $field['class'] ) ) { |
|
| 313 | - $field['class'] = ''; |
|
| 314 | - |
|
| 315 | - $this->boxes[ $bk ]['sections'][ $sk ]['fields'][ $fk ] = $field; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - if ( $add_field || ( ( is_admin() && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) || ( ! is_admin() ) ) ) { |
|
| 319 | - if ( empty( $field['id'] ) ) { |
|
| 320 | - continue; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - if ( isset( $field['default'] ) ) { |
|
| 324 | - $this->options_defaults[ $field['id'] ] = $field['default']; |
|
| 325 | - } else { |
|
| 326 | - $this->options_defaults[ $field['id'] ] = $this->field_default( $field ); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - foreach ( $box['post_types'] as $type ) { |
|
| 330 | - $this->post_type_fields[ $type ][ $field['id'] ] = 1; |
|
| 331 | - |
|
| 332 | - if ( 'repeater' === $field['type'] ) { |
|
| 333 | - foreach ( $field['fields'] as $val ) { |
|
| 334 | - $this->post_type_fields[ $type ][ $val['id'] ] = 1; |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - if ( ! empty( $field['output'] ) ) { |
|
| 340 | - $this->output[ $field['id'] ] = isset( $this->output[ $field['id'] ] ) ? array_merge( $field['output'], $this->output[ $field['id'] ] ) : $field['output']; |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - // Detect what field types are being used. |
|
| 344 | - if ( ! isset( $this->parent->fields[ $field['type'] ][ $field['id'] ] ) ) { |
|
| 345 | - $this->parent->fields[ $field['type'] ][ $field['id'] ] = 1; |
|
| 346 | - } else { |
|
| 347 | - $this->parent->fields[ $field['type'] ] = array( $field['id'] => 1 ); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - if ( isset( $this->options_defaults[ $field['id'] ] ) ) { |
|
| 351 | - $this->to_replace[ $field['id'] ] = $field; |
|
| 352 | - } |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - if ( ! isset( $this->parent->options[ $field['id'] ] ) ) { |
|
| 356 | - $this->parent->sections[ ( count( $this->parent->sections ) - 1 ) ]['fields'][] = $field; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - if ( ! isset( $this->meta[ $this->post_id ][ $field['id'] ] ) ) { |
|
| 360 | - $this->meta[ $this->post_id ][ $field['id'] ] = $this->options_defaults[ $field['id'] ]; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - // Only override if it exists, and it's not the default. |
|
| 364 | - // phpcs:ignore Squiz.PHP.CommentedOutCode |
|
| 365 | - // if ( isset( $this->meta[ $this->post_id ][ $field['id'] ] ) && isset( $field['default'] ) && $this->meta[ $this->post_id ][ $field['id'] ] === $field['default'] ) { |
|
| 366 | - // unset($this->meta[$this->post_id][$field['id']]); |
|
| 367 | - // } . |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - if ( isset( $run_hooks ) && true === $run_hooks ) { |
|
| 376 | - $this->parent_options = array(); |
|
| 377 | - |
|
| 378 | - if ( ! empty( $this->to_replace ) ) { |
|
| 379 | - foreach ( $this->to_replace as $id => $field ) { |
|
| 380 | - |
|
| 381 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 382 | - add_filter( "redux/options/{$this->parent->args['opt_name']}/field/$id/register", array( $this, 'replace_field' ) ); |
|
| 383 | - } |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - add_filter( "redux/options/{$this->parent->args['opt_name']}/options", array( $this, 'override_options' ) ); |
|
| 387 | - add_filter( "redux/field/{$this->parent->args['opt_name']}/_can_output_css", array( $this, 'override_can_output_css' ) ); |
|
| 388 | - add_filter( "redux/field/{$this->parent->args['opt_name']}/output_css", array( $this, 'output_css' ) ); |
|
| 389 | - } |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * Replace Field. |
|
| 394 | - * |
|
| 395 | - * @param array $field Field array. |
|
| 396 | - * |
|
| 397 | - * @return mixed |
|
| 398 | - */ |
|
| 399 | - public function replace_field( array $field ) { |
|
| 400 | - if ( isset( $this->to_replace[ $field['id'] ] ) ) { |
|
| 401 | - $field = $this->to_replace[ $field['id'] ]; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - return $field; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Override CSS output. |
|
| 409 | - * |
|
| 410 | - * @param array $field Field array. |
|
| 411 | - * |
|
| 412 | - * @return array |
|
| 413 | - */ |
|
| 414 | - public function override_can_output_css( array $field ): array { |
|
| 415 | - if ( isset( $this->output[ $field['id'] ] ) ) { |
|
| 416 | - $field['force_output'] = true; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - return $field; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Output CSS. |
|
| 424 | - * |
|
| 425 | - * @param array $field Field array. |
|
| 426 | - * |
|
| 427 | - * @return array |
|
| 428 | - */ |
|
| 429 | - public function output_css( array $field ): array { |
|
| 430 | - if ( isset( $this->output[ $field['id'] ] ) ) { |
|
| 431 | - $field['output'] = $this->output[ $field['id'] ]; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - return $field; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Make sure the defaults are the defaults |
|
| 439 | - * |
|
| 440 | - * @param array $options Options array. |
|
| 441 | - * |
|
| 442 | - * @return array |
|
| 443 | - */ |
|
| 444 | - public function override_options( array $options ): array { |
|
| 445 | - $this->parent->default_values(); |
|
| 446 | - $this->parent_defaults = $this->parent->options_defaults; |
|
| 447 | - |
|
| 448 | - $meta = $this->get_meta( $this->post_id ); |
|
| 449 | - $data = wp_parse_args( $meta, $this->options_defaults ); |
|
| 450 | - |
|
| 451 | - foreach ( $data as $key => $value ) { |
|
| 452 | - if ( isset( $meta[ $key ] ) && '' !== $meta[ $key ] ) { |
|
| 453 | - $data[ $key ] = $meta[ $key ]; |
|
| 454 | - continue; |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - if ( isset( $options[ $key ] ) ) { |
|
| 458 | - $data[ $key ] = $options[ $key ]; |
|
| 459 | - } |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - $this->parent->options_defaults = wp_parse_args( $this->options_defaults, $this->parent->options_defaults ); |
|
| 463 | - |
|
| 464 | - return wp_parse_args( $data, $options ); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Loop start. |
|
| 469 | - * |
|
| 470 | - * @param mixed $the_post WP_Post object. |
|
| 471 | - * |
|
| 472 | - * @return array|void |
|
| 473 | - */ |
|
| 474 | - public function loop_start( $the_post = array() ) { |
|
| 475 | - if ( is_admin() ) { |
|
| 476 | - return $the_post; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - if ( isset( $the_post ) && is_array( $the_post ) ) { |
|
| 480 | - global $post; |
|
| 481 | - $the_post = $post; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - if ( isset( $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ) ) { |
|
| 485 | - $GLOBALS[ $this->parent->args['global_variable'] ] = $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ]; |
|
| 486 | - unset( $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - // Override these values if they differ from the admin panel defaults. ;) . |
|
| 490 | - if ( in_array( $the_post->post_type, $this->post_types, true ) ) { |
|
| 491 | - $meta = $this->get_meta( $the_post->ID ); |
|
| 492 | - if ( empty( $meta ) ) { |
|
| 493 | - return; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - // Backup the args. |
|
| 497 | - $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] = $GLOBALS[ $this->parent->args['global_variable'] ]; |
|
| 498 | - $GLOBALS[ $this->parent->args['global_variable'] ] = wp_parse_args( $meta, $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ); |
|
| 499 | - } |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * Loop end. |
|
| 504 | - */ |
|
| 505 | - public function loop_end() { |
|
| 506 | - if ( isset( $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ) ) { |
|
| 507 | - $GLOBALS[ $this->parent->args['global_variable'] ] = $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ]; |
|
| 508 | - |
|
| 509 | - unset( $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ); |
|
| 510 | - } |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - /** |
|
| 514 | - * Enqueue fields. |
|
| 515 | - */ |
|
| 516 | - public function enqueue() { |
|
| 517 | - global $pagenow; |
|
| 518 | - |
|
| 519 | - $types = array(); |
|
| 520 | - |
|
| 521 | - // Enqueue CSS. |
|
| 522 | - foreach ( $this->boxes as $key => $box ) { |
|
| 523 | - if ( empty( $box['sections'] ) ) { |
|
| 524 | - continue; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - if ( isset( $box['post_types'] ) ) { |
|
| 528 | - $types = array_merge( $box['post_types'], $types ); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - if ( ! empty( $box['post_types'] ) ) { |
|
| 532 | - if ( ! is_array( $box['post_types'] ) ) { |
|
| 533 | - $box['post_types'] = array( $box['post_types'] ); |
|
| 534 | - $this->boxes[ $key ]['post_types'] = $box['post_types']; |
|
| 535 | - } |
|
| 536 | - } |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - if ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) { |
|
| 540 | - global $post; |
|
| 541 | - |
|
| 542 | - if ( in_array( $post->post_type, $types, true ) ) { |
|
| 543 | - $this->parent->transients = get_transient( $this->parent->args['opt_name'] . '-transients-metaboxes' ); |
|
| 544 | - $this->parent->transients_check = $this->parent->transients; |
|
| 545 | - |
|
| 546 | - if ( isset( $this->parent->transients['notices'] ) ) { |
|
| 547 | - $this->notices = $this->parent->transients['notices']; |
|
| 548 | - $this->parent->transients['last_save_mode'] = 'metaboxes'; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - delete_transient( $this->parent->args['opt_name'] . '-transients-metaboxes' ); |
|
| 552 | - $this->parent->enqueue_class->init(); |
|
| 553 | - |
|
| 554 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 555 | - do_action( "redux/metaboxes/{$this->parent->args['opt_name']}/enqueue" ); |
|
| 556 | - |
|
| 557 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 558 | - do_action( "redux/{$this->parent->args['opt_name']}/extensions/metaboxes/enqueue" ); |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * Redux metaboxes CSS |
|
| 562 | - * filter 'redux/page/{opt_name}/enqueue/redux-extension-metaboxes-css' |
|
| 563 | - */ |
|
| 564 | - if ( $this->parent->args['dev_mode'] ) { |
|
| 565 | - wp_enqueue_style( |
|
| 566 | - 'redux-extension-metaboxes', |
|
| 567 | - apply_filters( "redux/metaboxes/{$this->parent->args['opt_name']}/enqueue/redux-extension-metaboxes-css", $this->extension_url . 'redux-extension-metaboxes.css' ), // phpcs:ignore: WordPress.NamingConventions.ValidHookName |
|
| 568 | - array(), |
|
| 569 | - self::$version |
|
| 570 | - ); |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - /** |
|
| 574 | - * Redux metaboxes JS |
|
| 575 | - * filter 'redux/page/{opt_name}/enqueue/redux-extension-metaboxes-js |
|
| 576 | - */ |
|
| 577 | - wp_enqueue_script( |
|
| 578 | - 'redux-extension-metaboxes', |
|
| 579 | - apply_filters( "redux/metaboxes/{$this->parent->args['opt_name']}/enqueue/redux-extension-metaboxes-js", $this->extension_url . 'redux-extension-metaboxes' . Redux_Functions::isMin() . '.js' ), // phpcs:ignore: WordPress.NamingConventions.ValidHookName |
|
| 580 | - array( 'jquery', 'redux-js' ), |
|
| 581 | - self::$version, |
|
| 582 | - true |
|
| 583 | - ); |
|
| 584 | - |
|
| 585 | - // Values used by the javascript. |
|
| 586 | - wp_localize_script( |
|
| 587 | - 'redux-extension-metaboxes', |
|
| 588 | - 'reduxMetaboxes', |
|
| 589 | - $this->wp_links |
|
| 590 | - ); |
|
| 591 | - |
|
| 592 | - wp_localize_script( |
|
| 593 | - 'redux-extension-metaboxes', |
|
| 594 | - 'reduxMetaboxesPageTemplate', |
|
| 595 | - array( |
|
| 596 | - '_wp_page_template' => get_post_meta( get_the_ID(), '_wp_page_template', true ), |
|
| 597 | - ) |
|
| 598 | - ); |
|
| 599 | - } |
|
| 600 | - } |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - /* Post URLs to IDs function, supports custom post types - borrowed and modified from url_to_postid() in wp-includes/rewrite.php */ |
|
| 604 | - |
|
| 605 | - // Taken from http://betterwp.net/wordpress-tips/url_to_postid-for-custom-post-types/ |
|
| 606 | - // Customized to work with non-rewrite URLs |
|
| 607 | - // Modified by Dovy Paukstys (@dovy) of Redux Framework. |
|
| 608 | - |
|
| 609 | - /** |
|
| 610 | - * URL to PostID. |
|
| 611 | - * |
|
| 612 | - * @param string $url URL. |
|
| 613 | - * |
|
| 614 | - * @return int|mixed|void |
|
| 615 | - */ |
|
| 616 | - private function url_to_postid( string $url ) { |
|
| 617 | - global $wp_rewrite, $pagenow; |
|
| 618 | - |
|
| 619 | - if ( ! empty( $this->post_id ) ) { |
|
| 620 | - return $this->post_id; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - if ( isset( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 624 | - $post = (int) sanitize_text_field( wp_unslash( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 625 | - |
|
| 626 | - if ( ! empty( $post ) ) { |
|
| 627 | - return $post; |
|
| 628 | - } |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - if ( 'post-new.php' === $pagenow || 'wp-login.php' === $pagenow ) { |
|
| 632 | - return; |
|
| 633 | - } |
|
| 634 | - |
|
| 635 | - if ( is_admin() && 'post.php' === $pagenow && isset( $_POST['post_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 636 | - $post_id = sanitize_text_field( wp_unslash( $_POST['post_ID'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 637 | - |
|
| 638 | - if ( ! empty( $post_id ) && is_numeric( $post_id ) ) { |
|
| 639 | - return $post_id; |
|
| 640 | - } |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - $post_id = url_to_postid( $url ); |
|
| 644 | - |
|
| 645 | - if ( isset( $post_id ) && '' !== (string) $post_id && 0 !== $post_id ) { |
|
| 646 | - return $post_id; |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - // First, check to see if there is a 'p=N' or 'page_id=N' to match against. |
|
| 650 | - if ( preg_match( '#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values ) ) { |
|
| 651 | - $id = absint( $values[2] ); |
|
| 652 | - if ( $id ) { |
|
| 653 | - return $id; |
|
| 654 | - } |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - // Check to see if we are using rewrite rules. |
|
| 658 | - if ( isset( $wp_rewrite ) ) { |
|
| 659 | - $rewrite = $wp_rewrite->wp_rewrite_rules(); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options. |
|
| 663 | - if ( empty( $rewrite ) ) { |
|
| 664 | - if ( ! empty( $_GET ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 665 | - |
|
| 666 | - /************************************************************************ |
|
| 17 | + /** |
|
| 18 | + * Main Redux_Extension_Metaboxes class |
|
| 19 | + * |
|
| 20 | + * @since 1.0.0 |
|
| 21 | + */ |
|
| 22 | + class Redux_Extension_Metaboxes extends Redux_Extension_Abstract { |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Extension version. |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + public static $version = '4.2.0'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Extension friendly name. |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + public string $extension_name = 'Metaboxes'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Boxes array. |
|
| 40 | + * |
|
| 41 | + * @var array|null |
|
| 42 | + */ |
|
| 43 | + public ?array $boxes = array(); |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Post types array. |
|
| 47 | + * |
|
| 48 | + * @var array|null |
|
| 49 | + */ |
|
| 50 | + public ?array $post_types = array(); |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Post type. |
|
| 54 | + * |
|
| 55 | + * @var string|null |
|
| 56 | + */ |
|
| 57 | + public ?string $post_type; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Sections array. |
|
| 61 | + * |
|
| 62 | + * @var array|null |
|
| 63 | + */ |
|
| 64 | + public ?array $orig_args; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Sections array. |
|
| 68 | + * |
|
| 69 | + * @var array|null |
|
| 70 | + */ |
|
| 71 | + public ?array $sections = array(); |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * CSS output array. |
|
| 75 | + * |
|
| 76 | + * @var array|null |
|
| 77 | + */ |
|
| 78 | + public ?array $output = array(); |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Options array. |
|
| 82 | + * |
|
| 83 | + * @var array |
|
| 84 | + */ |
|
| 85 | + public array $options = array(); |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Parent options array. |
|
| 89 | + * |
|
| 90 | + * @var array |
|
| 91 | + */ |
|
| 92 | + public array $parent_options = array(); |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Parent defaults array. |
|
| 96 | + * |
|
| 97 | + * @var array |
|
| 98 | + */ |
|
| 99 | + public array $parent_defaults = array(); |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Post type fields array. |
|
| 103 | + * |
|
| 104 | + * @var array |
|
| 105 | + */ |
|
| 106 | + public array $post_type_fields = array(); |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Options defaults array. |
|
| 110 | + * |
|
| 111 | + * @var array |
|
| 112 | + */ |
|
| 113 | + public array $options_defaults = array(); |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Replace array. |
|
| 117 | + * |
|
| 118 | + * @var array |
|
| 119 | + */ |
|
| 120 | + public array $to_replace = array(); |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Meta data array. |
|
| 124 | + * |
|
| 125 | + * @var array |
|
| 126 | + */ |
|
| 127 | + public array $meta = array(); |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Post ID. |
|
| 131 | + * |
|
| 132 | + * @var null|int |
|
| 133 | + */ |
|
| 134 | + public ?int $post_id = 0; |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Base URI. |
|
| 138 | + * |
|
| 139 | + * @var string|null |
|
| 140 | + */ |
|
| 141 | + public ?string $base_url; |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * WP_Links array. |
|
| 145 | + * |
|
| 146 | + * @var array |
|
| 147 | + */ |
|
| 148 | + public array $wp_links = array(); |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Notices. |
|
| 152 | + * |
|
| 153 | + * @var array |
|
| 154 | + */ |
|
| 155 | + private array $notices = array(); |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * ReduxFramework_extension_metaboxes constructor. |
|
| 159 | + * |
|
| 160 | + * @param object $redux ReduxFramework object. |
|
| 161 | + */ |
|
| 162 | + public function __construct( $redux ) { |
|
| 163 | + global $pagenow; |
|
| 164 | + |
|
| 165 | + parent::__construct( $redux, __FILE__ ); |
|
| 166 | + |
|
| 167 | + $this->parent->extensions['metaboxes'] = $this; |
|
| 168 | + |
|
| 169 | + $this->extension_dir = trailingslashit( str_replace( '\\', '/', __DIR__ ) ); |
|
| 170 | + $this->extension_url = site_url( str_replace( trailingslashit( str_replace( '\\', '/', ABSPATH ) ), '', $this->extension_dir ) ); |
|
| 171 | + |
|
| 172 | + // Only run metaboxes on the pages/posts, not the front-end. |
|
| 173 | + // The DOING_AJAX check allows for redux_post_meta to work inside |
|
| 174 | + // AJAX calls. - kp. |
|
| 175 | + if ( 'post-new.php' !== $pagenow && 'post.php' !== $pagenow ) { |
|
| 176 | + if ( is_admin() ) { |
|
| 177 | + if ( defined( 'DOING_AJAX' ) && ! DOING_AJAX ) { |
|
| 178 | + return; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + return; |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + if ( 'wp-cron.php' === $pagenow || 'wp-comments-post.php' === $pagenow ) { |
|
| 186 | + return; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + // Must not update the DB when just updating metaboxes. Sheesh. |
|
| 190 | + if ( is_admin() && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) { |
|
| 191 | + $this->parent->never_save_to_db = true; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + include_once Redux_Core::$dir . 'inc/extensions/metaboxes/redux-metaboxes-helpers.php'; |
|
| 195 | + |
|
| 196 | + // phpcs:ignore Generic.Strings.UnnecessaryStringConcat |
|
| 197 | + add_action( 'add_' . 'meta_' . 'boxes', array( $this, 'add' ) ); |
|
| 198 | + add_action( 'save_post', array( $this, 'meta_boxes_save' ), 1, 2 ); |
|
| 199 | + add_action( 'pre_post_update', array( $this, 'pre_post_update' ) ); |
|
| 200 | + add_action( 'admin_notices', array( $this, 'meta_boxes_show_errors' ), 0 ); |
|
| 201 | + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 20 ); |
|
| 202 | + |
|
| 203 | + // Global variable overrides for within loops. |
|
| 204 | + add_action( 'the_post', array( $this, 'loop_start' ), 0 ); |
|
| 205 | + add_action( 'loop_end', array( $this, 'loop_end' ), 0 ); |
|
| 206 | + |
|
| 207 | + $this->init(); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Added class names to metabox DIV. |
|
| 212 | + * |
|
| 213 | + * @param array $classes Class array. |
|
| 214 | + * |
|
| 215 | + * @return array |
|
| 216 | + */ |
|
| 217 | + public function add_box_classes( array $classes ): array { |
|
| 218 | + $classes[] = 'redux-metabox'; |
|
| 219 | + $classes[] = 'redux-' . $this->parent->args['opt_name']; |
|
| 220 | + |
|
| 221 | + if ( '' !== $this->parent->args['class'] ) { |
|
| 222 | + $classes[] = $this->parent->args['class']; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + return $classes; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Class init. |
|
| 230 | + */ |
|
| 231 | + public function init() { |
|
| 232 | + global $pagenow; |
|
| 233 | + |
|
| 234 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 235 | + $this->boxes = apply_filters( 'redux/metaboxes/' . $this->parent->args['opt_name'] . '/boxes', $this->boxes, $this->parent->args['opt_name'] ); |
|
| 236 | + |
|
| 237 | + if ( empty( $this->boxes ) && class_exists( 'Redux_Metaboxes' ) ) { |
|
| 238 | + $this->boxes = Redux_Metaboxes::construct_boxes( $this->parent->args['opt_name'] ); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + if ( empty( $this->boxes ) || ! is_array( $this->boxes ) ) { |
|
| 242 | + return; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + if ( isset( Redux_Core::$server['HTTP_HOST'] ) && isset( Redux_Core::$server['REQUEST_URI'] ) ) { |
|
| 246 | + $this->base_url = ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( wp_unslash( Redux_Core::$server['HTTP_HOST'] ) ) . sanitize_text_field( wp_unslash( Redux_Core::$server['REQUEST_URI'] ) ); // Safe & Reliable. |
|
| 247 | + $this->post_id = $this->url_to_postid( ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( wp_unslash( Redux_Core::$server['HTTP_HOST'] ) ) . sanitize_text_field( wp_unslash( Redux_Core::$server['REQUEST_URI'] ) ) ); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + if ( is_admin() && isset( $_GET['post_type'] ) && ! empty( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 251 | + $this->post_type = sanitize_text_field( wp_unslash( $_GET['post_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 252 | + } else { |
|
| 253 | + $this->post_type = get_post_type( $this->post_id ); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + foreach ( $this->boxes as $bk => $box ) { |
|
| 257 | + |
|
| 258 | + // If the post-ids for this box are set, we're limiting to the current post id. |
|
| 259 | + if ( ! empty( $box['post_ids'] ) ) { |
|
| 260 | + if ( ! is_array( $box['post_ids'] ) ) { |
|
| 261 | + $box['post_ids'] = array( $box['post_ids'] ); |
|
| 262 | + } |
|
| 263 | + if ( ! in_array( $this->post_id, $box['post_ids'], true ) ) { |
|
| 264 | + continue; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + if ( ! empty( $box['sections'] ) ) { |
|
| 269 | + $this->sections = wp_parse_args( $this->sections, $box['sections'] ); |
|
| 270 | + |
|
| 271 | + $this->post_types = wp_parse_args( $this->post_types, $box['post_types'] ); |
|
| 272 | + |
|
| 273 | + // Checking to override the parent variables. |
|
| 274 | + $add_field = false; |
|
| 275 | + |
|
| 276 | + foreach ( $box['post_types'] as $type ) { |
|
| 277 | + if ( $this->post_type === $type ) { |
|
| 278 | + $add_field = true; |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + // Replacing all the fields. |
|
| 283 | + if ( $add_field || ( ( is_admin() && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) || ( ! is_admin() ) ) ) { |
|
| 284 | + $run_hooks = true; |
|
| 285 | + |
|
| 286 | + $box_id = 'redux-' . $this->parent->args['opt_name'] . '-metabox-' . $box['id']; |
|
| 287 | + |
|
| 288 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 289 | + do_action( 'redux/' . $this->parent->args['opt_name'] . '/extensions/metabox/init', $this, $box ); |
|
| 290 | + |
|
| 291 | + if ( isset( $box['page_template'] ) && 'page' === $this->post_type ) { |
|
| 292 | + if ( ! is_array( $box['page_template'] ) ) { |
|
| 293 | + $box['page_template'] = array( $box['page_template'] ); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + $this->wp_links[ $box_id ]['page_template'] = isset( $this->wp_links[ $box_id ]['page_template'] ) ? wp_parse_args( $this->wp_links[ $box_id ]['page_template'], $box['page_template'] ) : $box['page_template']; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + if ( isset( $box['post_format'] ) && ( in_array( $this->post_type, $this->post_types, true ) || '' === $this->post_type || false === $this->post_type ) ) { |
|
| 300 | + if ( ! is_array( $box['post_format'] ) ) { |
|
| 301 | + $box['post_format'] = array( $box['post_format'] ); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + $this->wp_links[ $box_id ]['post_format'] = isset( $this->wp_links[ $box_id ]['post_format'] ) ? wp_parse_args( $this->wp_links[ $box_id ]['post_format'], $box['post_format'] ) : $box['post_format']; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + $this->meta[ $this->post_id ] = $this->get_meta( $this->post_id ); |
|
| 308 | + |
|
| 309 | + foreach ( $box['sections'] as $sk => $section ) { |
|
| 310 | + if ( ! empty( $section['fields'] ) ) { |
|
| 311 | + foreach ( $section['fields'] as $fk => $field ) { |
|
| 312 | + if ( ! isset( $field['class'] ) ) { |
|
| 313 | + $field['class'] = ''; |
|
| 314 | + |
|
| 315 | + $this->boxes[ $bk ]['sections'][ $sk ]['fields'][ $fk ] = $field; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + if ( $add_field || ( ( is_admin() && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) || ( ! is_admin() ) ) ) { |
|
| 319 | + if ( empty( $field['id'] ) ) { |
|
| 320 | + continue; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + if ( isset( $field['default'] ) ) { |
|
| 324 | + $this->options_defaults[ $field['id'] ] = $field['default']; |
|
| 325 | + } else { |
|
| 326 | + $this->options_defaults[ $field['id'] ] = $this->field_default( $field ); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + foreach ( $box['post_types'] as $type ) { |
|
| 330 | + $this->post_type_fields[ $type ][ $field['id'] ] = 1; |
|
| 331 | + |
|
| 332 | + if ( 'repeater' === $field['type'] ) { |
|
| 333 | + foreach ( $field['fields'] as $val ) { |
|
| 334 | + $this->post_type_fields[ $type ][ $val['id'] ] = 1; |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + if ( ! empty( $field['output'] ) ) { |
|
| 340 | + $this->output[ $field['id'] ] = isset( $this->output[ $field['id'] ] ) ? array_merge( $field['output'], $this->output[ $field['id'] ] ) : $field['output']; |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + // Detect what field types are being used. |
|
| 344 | + if ( ! isset( $this->parent->fields[ $field['type'] ][ $field['id'] ] ) ) { |
|
| 345 | + $this->parent->fields[ $field['type'] ][ $field['id'] ] = 1; |
|
| 346 | + } else { |
|
| 347 | + $this->parent->fields[ $field['type'] ] = array( $field['id'] => 1 ); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + if ( isset( $this->options_defaults[ $field['id'] ] ) ) { |
|
| 351 | + $this->to_replace[ $field['id'] ] = $field; |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + if ( ! isset( $this->parent->options[ $field['id'] ] ) ) { |
|
| 356 | + $this->parent->sections[ ( count( $this->parent->sections ) - 1 ) ]['fields'][] = $field; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + if ( ! isset( $this->meta[ $this->post_id ][ $field['id'] ] ) ) { |
|
| 360 | + $this->meta[ $this->post_id ][ $field['id'] ] = $this->options_defaults[ $field['id'] ]; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + // Only override if it exists, and it's not the default. |
|
| 364 | + // phpcs:ignore Squiz.PHP.CommentedOutCode |
|
| 365 | + // if ( isset( $this->meta[ $this->post_id ][ $field['id'] ] ) && isset( $field['default'] ) && $this->meta[ $this->post_id ][ $field['id'] ] === $field['default'] ) { |
|
| 366 | + // unset($this->meta[$this->post_id][$field['id']]); |
|
| 367 | + // } . |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + if ( isset( $run_hooks ) && true === $run_hooks ) { |
|
| 376 | + $this->parent_options = array(); |
|
| 377 | + |
|
| 378 | + if ( ! empty( $this->to_replace ) ) { |
|
| 379 | + foreach ( $this->to_replace as $id => $field ) { |
|
| 380 | + |
|
| 381 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 382 | + add_filter( "redux/options/{$this->parent->args['opt_name']}/field/$id/register", array( $this, 'replace_field' ) ); |
|
| 383 | + } |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + add_filter( "redux/options/{$this->parent->args['opt_name']}/options", array( $this, 'override_options' ) ); |
|
| 387 | + add_filter( "redux/field/{$this->parent->args['opt_name']}/_can_output_css", array( $this, 'override_can_output_css' ) ); |
|
| 388 | + add_filter( "redux/field/{$this->parent->args['opt_name']}/output_css", array( $this, 'output_css' ) ); |
|
| 389 | + } |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * Replace Field. |
|
| 394 | + * |
|
| 395 | + * @param array $field Field array. |
|
| 396 | + * |
|
| 397 | + * @return mixed |
|
| 398 | + */ |
|
| 399 | + public function replace_field( array $field ) { |
|
| 400 | + if ( isset( $this->to_replace[ $field['id'] ] ) ) { |
|
| 401 | + $field = $this->to_replace[ $field['id'] ]; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + return $field; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Override CSS output. |
|
| 409 | + * |
|
| 410 | + * @param array $field Field array. |
|
| 411 | + * |
|
| 412 | + * @return array |
|
| 413 | + */ |
|
| 414 | + public function override_can_output_css( array $field ): array { |
|
| 415 | + if ( isset( $this->output[ $field['id'] ] ) ) { |
|
| 416 | + $field['force_output'] = true; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + return $field; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Output CSS. |
|
| 424 | + * |
|
| 425 | + * @param array $field Field array. |
|
| 426 | + * |
|
| 427 | + * @return array |
|
| 428 | + */ |
|
| 429 | + public function output_css( array $field ): array { |
|
| 430 | + if ( isset( $this->output[ $field['id'] ] ) ) { |
|
| 431 | + $field['output'] = $this->output[ $field['id'] ]; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + return $field; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Make sure the defaults are the defaults |
|
| 439 | + * |
|
| 440 | + * @param array $options Options array. |
|
| 441 | + * |
|
| 442 | + * @return array |
|
| 443 | + */ |
|
| 444 | + public function override_options( array $options ): array { |
|
| 445 | + $this->parent->default_values(); |
|
| 446 | + $this->parent_defaults = $this->parent->options_defaults; |
|
| 447 | + |
|
| 448 | + $meta = $this->get_meta( $this->post_id ); |
|
| 449 | + $data = wp_parse_args( $meta, $this->options_defaults ); |
|
| 450 | + |
|
| 451 | + foreach ( $data as $key => $value ) { |
|
| 452 | + if ( isset( $meta[ $key ] ) && '' !== $meta[ $key ] ) { |
|
| 453 | + $data[ $key ] = $meta[ $key ]; |
|
| 454 | + continue; |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + if ( isset( $options[ $key ] ) ) { |
|
| 458 | + $data[ $key ] = $options[ $key ]; |
|
| 459 | + } |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + $this->parent->options_defaults = wp_parse_args( $this->options_defaults, $this->parent->options_defaults ); |
|
| 463 | + |
|
| 464 | + return wp_parse_args( $data, $options ); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Loop start. |
|
| 469 | + * |
|
| 470 | + * @param mixed $the_post WP_Post object. |
|
| 471 | + * |
|
| 472 | + * @return array|void |
|
| 473 | + */ |
|
| 474 | + public function loop_start( $the_post = array() ) { |
|
| 475 | + if ( is_admin() ) { |
|
| 476 | + return $the_post; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + if ( isset( $the_post ) && is_array( $the_post ) ) { |
|
| 480 | + global $post; |
|
| 481 | + $the_post = $post; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + if ( isset( $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ) ) { |
|
| 485 | + $GLOBALS[ $this->parent->args['global_variable'] ] = $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ]; |
|
| 486 | + unset( $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + // Override these values if they differ from the admin panel defaults. ;) . |
|
| 490 | + if ( in_array( $the_post->post_type, $this->post_types, true ) ) { |
|
| 491 | + $meta = $this->get_meta( $the_post->ID ); |
|
| 492 | + if ( empty( $meta ) ) { |
|
| 493 | + return; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + // Backup the args. |
|
| 497 | + $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] = $GLOBALS[ $this->parent->args['global_variable'] ]; |
|
| 498 | + $GLOBALS[ $this->parent->args['global_variable'] ] = wp_parse_args( $meta, $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ); |
|
| 499 | + } |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * Loop end. |
|
| 504 | + */ |
|
| 505 | + public function loop_end() { |
|
| 506 | + if ( isset( $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ) ) { |
|
| 507 | + $GLOBALS[ $this->parent->args['global_variable'] ] = $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ]; |
|
| 508 | + |
|
| 509 | + unset( $GLOBALS[ $this->parent->args['global_variable'] . '-loop' ] ); |
|
| 510 | + } |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + /** |
|
| 514 | + * Enqueue fields. |
|
| 515 | + */ |
|
| 516 | + public function enqueue() { |
|
| 517 | + global $pagenow; |
|
| 518 | + |
|
| 519 | + $types = array(); |
|
| 520 | + |
|
| 521 | + // Enqueue CSS. |
|
| 522 | + foreach ( $this->boxes as $key => $box ) { |
|
| 523 | + if ( empty( $box['sections'] ) ) { |
|
| 524 | + continue; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + if ( isset( $box['post_types'] ) ) { |
|
| 528 | + $types = array_merge( $box['post_types'], $types ); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + if ( ! empty( $box['post_types'] ) ) { |
|
| 532 | + if ( ! is_array( $box['post_types'] ) ) { |
|
| 533 | + $box['post_types'] = array( $box['post_types'] ); |
|
| 534 | + $this->boxes[ $key ]['post_types'] = $box['post_types']; |
|
| 535 | + } |
|
| 536 | + } |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + if ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) { |
|
| 540 | + global $post; |
|
| 541 | + |
|
| 542 | + if ( in_array( $post->post_type, $types, true ) ) { |
|
| 543 | + $this->parent->transients = get_transient( $this->parent->args['opt_name'] . '-transients-metaboxes' ); |
|
| 544 | + $this->parent->transients_check = $this->parent->transients; |
|
| 545 | + |
|
| 546 | + if ( isset( $this->parent->transients['notices'] ) ) { |
|
| 547 | + $this->notices = $this->parent->transients['notices']; |
|
| 548 | + $this->parent->transients['last_save_mode'] = 'metaboxes'; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + delete_transient( $this->parent->args['opt_name'] . '-transients-metaboxes' ); |
|
| 552 | + $this->parent->enqueue_class->init(); |
|
| 553 | + |
|
| 554 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 555 | + do_action( "redux/metaboxes/{$this->parent->args['opt_name']}/enqueue" ); |
|
| 556 | + |
|
| 557 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 558 | + do_action( "redux/{$this->parent->args['opt_name']}/extensions/metaboxes/enqueue" ); |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * Redux metaboxes CSS |
|
| 562 | + * filter 'redux/page/{opt_name}/enqueue/redux-extension-metaboxes-css' |
|
| 563 | + */ |
|
| 564 | + if ( $this->parent->args['dev_mode'] ) { |
|
| 565 | + wp_enqueue_style( |
|
| 566 | + 'redux-extension-metaboxes', |
|
| 567 | + apply_filters( "redux/metaboxes/{$this->parent->args['opt_name']}/enqueue/redux-extension-metaboxes-css", $this->extension_url . 'redux-extension-metaboxes.css' ), // phpcs:ignore: WordPress.NamingConventions.ValidHookName |
|
| 568 | + array(), |
|
| 569 | + self::$version |
|
| 570 | + ); |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + /** |
|
| 574 | + * Redux metaboxes JS |
|
| 575 | + * filter 'redux/page/{opt_name}/enqueue/redux-extension-metaboxes-js |
|
| 576 | + */ |
|
| 577 | + wp_enqueue_script( |
|
| 578 | + 'redux-extension-metaboxes', |
|
| 579 | + apply_filters( "redux/metaboxes/{$this->parent->args['opt_name']}/enqueue/redux-extension-metaboxes-js", $this->extension_url . 'redux-extension-metaboxes' . Redux_Functions::isMin() . '.js' ), // phpcs:ignore: WordPress.NamingConventions.ValidHookName |
|
| 580 | + array( 'jquery', 'redux-js' ), |
|
| 581 | + self::$version, |
|
| 582 | + true |
|
| 583 | + ); |
|
| 584 | + |
|
| 585 | + // Values used by the javascript. |
|
| 586 | + wp_localize_script( |
|
| 587 | + 'redux-extension-metaboxes', |
|
| 588 | + 'reduxMetaboxes', |
|
| 589 | + $this->wp_links |
|
| 590 | + ); |
|
| 591 | + |
|
| 592 | + wp_localize_script( |
|
| 593 | + 'redux-extension-metaboxes', |
|
| 594 | + 'reduxMetaboxesPageTemplate', |
|
| 595 | + array( |
|
| 596 | + '_wp_page_template' => get_post_meta( get_the_ID(), '_wp_page_template', true ), |
|
| 597 | + ) |
|
| 598 | + ); |
|
| 599 | + } |
|
| 600 | + } |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + /* Post URLs to IDs function, supports custom post types - borrowed and modified from url_to_postid() in wp-includes/rewrite.php */ |
|
| 604 | + |
|
| 605 | + // Taken from http://betterwp.net/wordpress-tips/url_to_postid-for-custom-post-types/ |
|
| 606 | + // Customized to work with non-rewrite URLs |
|
| 607 | + // Modified by Dovy Paukstys (@dovy) of Redux Framework. |
|
| 608 | + |
|
| 609 | + /** |
|
| 610 | + * URL to PostID. |
|
| 611 | + * |
|
| 612 | + * @param string $url URL. |
|
| 613 | + * |
|
| 614 | + * @return int|mixed|void |
|
| 615 | + */ |
|
| 616 | + private function url_to_postid( string $url ) { |
|
| 617 | + global $wp_rewrite, $pagenow; |
|
| 618 | + |
|
| 619 | + if ( ! empty( $this->post_id ) ) { |
|
| 620 | + return $this->post_id; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + if ( isset( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 624 | + $post = (int) sanitize_text_field( wp_unslash( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 625 | + |
|
| 626 | + if ( ! empty( $post ) ) { |
|
| 627 | + return $post; |
|
| 628 | + } |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + if ( 'post-new.php' === $pagenow || 'wp-login.php' === $pagenow ) { |
|
| 632 | + return; |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + if ( is_admin() && 'post.php' === $pagenow && isset( $_POST['post_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 636 | + $post_id = sanitize_text_field( wp_unslash( $_POST['post_ID'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 637 | + |
|
| 638 | + if ( ! empty( $post_id ) && is_numeric( $post_id ) ) { |
|
| 639 | + return $post_id; |
|
| 640 | + } |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + $post_id = url_to_postid( $url ); |
|
| 644 | + |
|
| 645 | + if ( isset( $post_id ) && '' !== (string) $post_id && 0 !== $post_id ) { |
|
| 646 | + return $post_id; |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + // First, check to see if there is a 'p=N' or 'page_id=N' to match against. |
|
| 650 | + if ( preg_match( '#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values ) ) { |
|
| 651 | + $id = absint( $values[2] ); |
|
| 652 | + if ( $id ) { |
|
| 653 | + return $id; |
|
| 654 | + } |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + // Check to see if we are using rewrite rules. |
|
| 658 | + if ( isset( $wp_rewrite ) ) { |
|
| 659 | + $rewrite = $wp_rewrite->wp_rewrite_rules(); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options. |
|
| 663 | + if ( empty( $rewrite ) ) { |
|
| 664 | + if ( ! empty( $_GET ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 665 | + |
|
| 666 | + /************************************************************************ |
|
| 667 | 667 | * ADDED: Trys checks URL for ?posttype=postname |
| 668 | 668 | ************************************************************************ */ |
| 669 | 669 | |
| 670 | - // Assign $url to $temp_url just in case. :) . |
|
| 671 | - $temp_url = $url; |
|
| 672 | - |
|
| 673 | - // Get rid of the #anchor. |
|
| 674 | - $url_split = explode( '#', $temp_url ); |
|
| 675 | - $temp_url = $url_split[0]; |
|
| 676 | - |
|
| 677 | - // Get rid of URL ?query=string. |
|
| 678 | - $url_query = explode( '&', $temp_url ); |
|
| 679 | - $temp_url = $url_query[0]; |
|
| 680 | - |
|
| 681 | - // Get rid of ? mark. |
|
| 682 | - $url_query = explode( '?', $temp_url ); |
|
| 683 | - |
|
| 684 | - if ( ! empty( $url_query[1] ) && strpos( $url_query[1], '=' ) ) { |
|
| 685 | - $url_query = explode( '=', $url_query[1] ); |
|
| 686 | - |
|
| 687 | - if ( isset( $url_query[0] ) && isset( $url_query[1] ) ) { |
|
| 688 | - $args = array( |
|
| 689 | - 'name' => $url_query[1], |
|
| 690 | - 'post_type' => $url_query[0], |
|
| 691 | - 'showposts' => 1, |
|
| 692 | - ); |
|
| 693 | - |
|
| 694 | - if ( get_posts( $args ) === $post ) { |
|
| 695 | - return $post[0]->ID; |
|
| 696 | - } |
|
| 697 | - } |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - foreach ( $GLOBALS['wp_post_types'] as $key => $value ) { |
|
| 701 | - if ( ! empty( $_GET[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 702 | - $args = array( |
|
| 703 | - 'name' => sanitize_text_field( wp_unslash( $_GET[ $key ] ) ), // phpcs:ignore WordPress.Security.NonceVerification |
|
| 704 | - 'post_type' => $key, |
|
| 705 | - 'showposts' => 1, |
|
| 706 | - ); |
|
| 707 | - |
|
| 708 | - if ( get_posts( $args ) === $post ) { |
|
| 709 | - return $post[0]->ID; |
|
| 710 | - } |
|
| 711 | - } |
|
| 712 | - } |
|
| 713 | - } |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - // Get rid of the #anchor. |
|
| 717 | - $url_split = explode( '#', $url ); |
|
| 718 | - $url = $url_split[0]; |
|
| 719 | - |
|
| 720 | - // Get rid of URL ?query=string. |
|
| 721 | - $url_query = explode( '?', $url ); |
|
| 722 | - $url = $url_query[0]; |
|
| 723 | - |
|
| 724 | - // Set the correct URL scheme. |
|
| 725 | - $scheme = wp_parse_url( home_url(), PHP_URL_SCHEME ); |
|
| 726 | - $url = set_url_scheme( $url, $scheme ); |
|
| 727 | - |
|
| 728 | - // Add 'www.' if it is absent and should be there. |
|
| 729 | - if ( false !== strpos( home_url(), '://www.' ) && false === strpos( $url, '://www.' ) ) { |
|
| 730 | - $url = str_replace( '://', '://www.', $url ); |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - // Strip 'www.' if it is present and shouldn't be. |
|
| 734 | - if ( false === strpos( home_url(), '://www.' ) ) { |
|
| 735 | - $url = str_replace( '://www.', '://', $url ); |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - // Strip 'index.php/' if we're not using path info permalinks. |
|
| 739 | - if ( isset( $wp_rewrite ) && ! $wp_rewrite->using_index_permalinks() ) { |
|
| 740 | - $url = str_replace( 'index.php/', '', $url ); |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - if ( false !== strpos( $url, home_url() ) ) { |
|
| 744 | - // Chop off http://domain.com. |
|
| 745 | - $url = str_replace( home_url(), '', $url ); |
|
| 746 | - } else { |
|
| 747 | - // Chop off /path/to/blog. |
|
| 748 | - $home_path = wp_parse_url( home_url() ); |
|
| 749 | - $home_path = $home_path['path'] ?? ''; |
|
| 750 | - $url = str_replace( $home_path, '', $url ); |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - // Trim leading and lagging slashes. |
|
| 754 | - $url = trim( $url, '/' ); |
|
| 755 | - |
|
| 756 | - $request = $url; |
|
| 757 | - |
|
| 758 | - if ( empty( $request ) && ( empty( $_GET ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 759 | - return get_option( 'page_on_front' ); |
|
| 760 | - } |
|
| 761 | - |
|
| 762 | - // Look for matches. |
|
| 763 | - $request_match = $request; |
|
| 764 | - |
|
| 765 | - foreach ( $rewrite as $match => $query ) { |
|
| 766 | - // If the requesting file is the anchor of the match, prepend it |
|
| 767 | - // to the path info. |
|
| 768 | - if ( ! empty( $url ) && ( $url !== $request ) && ( strpos( $match, $url ) === 0 ) ) { |
|
| 769 | - $request_match = $url . '/' . $request; |
|
| 770 | - } |
|
| 771 | - |
|
| 772 | - if ( preg_match( "!^$match!", $request_match, $matches ) ) { |
|
| 773 | - global $wp; |
|
| 774 | - |
|
| 775 | - // Got a match. |
|
| 776 | - // Trim the query of everything up to the '?'. |
|
| 777 | - $query = preg_replace( '!^.+\?!', '', $query ); |
|
| 778 | - |
|
| 779 | - // Substitute the substring matches into the query. |
|
| 780 | - $query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) ); |
|
| 781 | - |
|
| 782 | - // Filter out non-public query vars. |
|
| 783 | - parse_str( $query, $query_vars ); |
|
| 784 | - |
|
| 785 | - $query = array(); |
|
| 786 | - |
|
| 787 | - foreach ( $query_vars as $key => $value ) { |
|
| 788 | - if ( in_array( $key, $wp->public_query_vars, true ) ) { |
|
| 789 | - $query[ $key ] = $value; |
|
| 790 | - } |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - /************************************************************************ |
|
| 670 | + // Assign $url to $temp_url just in case. :) . |
|
| 671 | + $temp_url = $url; |
|
| 672 | + |
|
| 673 | + // Get rid of the #anchor. |
|
| 674 | + $url_split = explode( '#', $temp_url ); |
|
| 675 | + $temp_url = $url_split[0]; |
|
| 676 | + |
|
| 677 | + // Get rid of URL ?query=string. |
|
| 678 | + $url_query = explode( '&', $temp_url ); |
|
| 679 | + $temp_url = $url_query[0]; |
|
| 680 | + |
|
| 681 | + // Get rid of ? mark. |
|
| 682 | + $url_query = explode( '?', $temp_url ); |
|
| 683 | + |
|
| 684 | + if ( ! empty( $url_query[1] ) && strpos( $url_query[1], '=' ) ) { |
|
| 685 | + $url_query = explode( '=', $url_query[1] ); |
|
| 686 | + |
|
| 687 | + if ( isset( $url_query[0] ) && isset( $url_query[1] ) ) { |
|
| 688 | + $args = array( |
|
| 689 | + 'name' => $url_query[1], |
|
| 690 | + 'post_type' => $url_query[0], |
|
| 691 | + 'showposts' => 1, |
|
| 692 | + ); |
|
| 693 | + |
|
| 694 | + if ( get_posts( $args ) === $post ) { |
|
| 695 | + return $post[0]->ID; |
|
| 696 | + } |
|
| 697 | + } |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + foreach ( $GLOBALS['wp_post_types'] as $key => $value ) { |
|
| 701 | + if ( ! empty( $_GET[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 702 | + $args = array( |
|
| 703 | + 'name' => sanitize_text_field( wp_unslash( $_GET[ $key ] ) ), // phpcs:ignore WordPress.Security.NonceVerification |
|
| 704 | + 'post_type' => $key, |
|
| 705 | + 'showposts' => 1, |
|
| 706 | + ); |
|
| 707 | + |
|
| 708 | + if ( get_posts( $args ) === $post ) { |
|
| 709 | + return $post[0]->ID; |
|
| 710 | + } |
|
| 711 | + } |
|
| 712 | + } |
|
| 713 | + } |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + // Get rid of the #anchor. |
|
| 717 | + $url_split = explode( '#', $url ); |
|
| 718 | + $url = $url_split[0]; |
|
| 719 | + |
|
| 720 | + // Get rid of URL ?query=string. |
|
| 721 | + $url_query = explode( '?', $url ); |
|
| 722 | + $url = $url_query[0]; |
|
| 723 | + |
|
| 724 | + // Set the correct URL scheme. |
|
| 725 | + $scheme = wp_parse_url( home_url(), PHP_URL_SCHEME ); |
|
| 726 | + $url = set_url_scheme( $url, $scheme ); |
|
| 727 | + |
|
| 728 | + // Add 'www.' if it is absent and should be there. |
|
| 729 | + if ( false !== strpos( home_url(), '://www.' ) && false === strpos( $url, '://www.' ) ) { |
|
| 730 | + $url = str_replace( '://', '://www.', $url ); |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + // Strip 'www.' if it is present and shouldn't be. |
|
| 734 | + if ( false === strpos( home_url(), '://www.' ) ) { |
|
| 735 | + $url = str_replace( '://www.', '://', $url ); |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + // Strip 'index.php/' if we're not using path info permalinks. |
|
| 739 | + if ( isset( $wp_rewrite ) && ! $wp_rewrite->using_index_permalinks() ) { |
|
| 740 | + $url = str_replace( 'index.php/', '', $url ); |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + if ( false !== strpos( $url, home_url() ) ) { |
|
| 744 | + // Chop off http://domain.com. |
|
| 745 | + $url = str_replace( home_url(), '', $url ); |
|
| 746 | + } else { |
|
| 747 | + // Chop off /path/to/blog. |
|
| 748 | + $home_path = wp_parse_url( home_url() ); |
|
| 749 | + $home_path = $home_path['path'] ?? ''; |
|
| 750 | + $url = str_replace( $home_path, '', $url ); |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + // Trim leading and lagging slashes. |
|
| 754 | + $url = trim( $url, '/' ); |
|
| 755 | + |
|
| 756 | + $request = $url; |
|
| 757 | + |
|
| 758 | + if ( empty( $request ) && ( empty( $_GET ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 759 | + return get_option( 'page_on_front' ); |
|
| 760 | + } |
|
| 761 | + |
|
| 762 | + // Look for matches. |
|
| 763 | + $request_match = $request; |
|
| 764 | + |
|
| 765 | + foreach ( $rewrite as $match => $query ) { |
|
| 766 | + // If the requesting file is the anchor of the match, prepend it |
|
| 767 | + // to the path info. |
|
| 768 | + if ( ! empty( $url ) && ( $url !== $request ) && ( strpos( $match, $url ) === 0 ) ) { |
|
| 769 | + $request_match = $url . '/' . $request; |
|
| 770 | + } |
|
| 771 | + |
|
| 772 | + if ( preg_match( "!^$match!", $request_match, $matches ) ) { |
|
| 773 | + global $wp; |
|
| 774 | + |
|
| 775 | + // Got a match. |
|
| 776 | + // Trim the query of everything up to the '?'. |
|
| 777 | + $query = preg_replace( '!^.+\?!', '', $query ); |
|
| 778 | + |
|
| 779 | + // Substitute the substring matches into the query. |
|
| 780 | + $query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) ); |
|
| 781 | + |
|
| 782 | + // Filter out non-public query vars. |
|
| 783 | + parse_str( $query, $query_vars ); |
|
| 784 | + |
|
| 785 | + $query = array(); |
|
| 786 | + |
|
| 787 | + foreach ( $query_vars as $key => $value ) { |
|
| 788 | + if ( in_array( $key, $wp->public_query_vars, true ) ) { |
|
| 789 | + $query[ $key ] = $value; |
|
| 790 | + } |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + /************************************************************************ |
|
| 794 | 794 | * ADDED: $GLOBALS['wp_post_types'] doesn't seem to have custom posttypes |
| 795 | 795 | * Trying below to find posttypes in $rewrite rules |
| 796 | 796 | ************************************************************************ */ |
| 797 | 797 | |
| 798 | - // PostType Array. |
|
| 799 | - $post_types = array(); |
|
| 800 | - |
|
| 801 | - foreach ( $rewrite as $value ) { |
|
| 802 | - if ( preg_match( '/post_type=([^&]+)/i', $value, $matched ) ) { |
|
| 803 | - if ( isset( $matched[1] ) && ! in_array( $matched[1], $post_types, true ) ) { |
|
| 804 | - $post_types[] = $matched[1]; |
|
| 805 | - } |
|
| 806 | - } |
|
| 807 | - } |
|
| 808 | - |
|
| 809 | - foreach ( $query_vars as $key => $value ) { |
|
| 810 | - if ( in_array( $key, $post_types, true ) ) { |
|
| 811 | - $query['post_type'] = $key; |
|
| 812 | - $query['postname'] = $value; |
|
| 813 | - } |
|
| 814 | - } |
|
| 815 | - |
|
| 816 | - /************************************************************************ |
|
| 798 | + // PostType Array. |
|
| 799 | + $post_types = array(); |
|
| 800 | + |
|
| 801 | + foreach ( $rewrite as $value ) { |
|
| 802 | + if ( preg_match( '/post_type=([^&]+)/i', $value, $matched ) ) { |
|
| 803 | + if ( isset( $matched[1] ) && ! in_array( $matched[1], $post_types, true ) ) { |
|
| 804 | + $post_types[] = $matched[1]; |
|
| 805 | + } |
|
| 806 | + } |
|
| 807 | + } |
|
| 808 | + |
|
| 809 | + foreach ( $query_vars as $key => $value ) { |
|
| 810 | + if ( in_array( $key, $post_types, true ) ) { |
|
| 811 | + $query['post_type'] = $key; |
|
| 812 | + $query['postname'] = $value; |
|
| 813 | + } |
|
| 814 | + } |
|
| 815 | + |
|
| 816 | + /************************************************************************ |
|
| 817 | 817 | * END ADD |
| 818 | 818 | ************************************************************************ */ |
| 819 | 819 | |
| 820 | - // Taken from class-wp.php. |
|
| 821 | - foreach ( $GLOBALS['wp_post_types'] as $post_type => $t ) { |
|
| 822 | - if ( isset( $t->query_var ) ) { |
|
| 823 | - $post_type_query_vars[ $t->query_var ] = $post_type; |
|
| 824 | - } |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - foreach ( $wp->public_query_vars as $wpvar ) { |
|
| 828 | - if ( isset( $wp->extra_query_vars[ $wpvar ] ) ) { |
|
| 829 | - $query[ $wpvar ] = $wp->extra_query_vars[ $wpvar ]; |
|
| 830 | - } elseif ( isset( $_POST[ $wpvar ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 831 | - $query[ $wpvar ] = sanitize_text_field( wp_unslash( $_POST[ $wpvar ] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 832 | - } elseif ( isset( $_GET[ $wpvar ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 833 | - $query[ $wpvar ] = sanitize_text_field( wp_unslash( $_GET[ $wpvar ] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 834 | - } elseif ( isset( $query_vars[ $wpvar ] ) ) { |
|
| 835 | - $query[ $wpvar ] = $query_vars[ $wpvar ]; |
|
| 836 | - } |
|
| 837 | - |
|
| 838 | - if ( ! empty( $query[ $wpvar ] ) ) { |
|
| 839 | - if ( ! is_array( $query[ $wpvar ] ) ) { |
|
| 840 | - $query[ $wpvar ] = (string) $query[ $wpvar ]; |
|
| 841 | - } else { |
|
| 842 | - foreach ( $query[ $wpvar ] as $vkey => $v ) { |
|
| 843 | - if ( ! is_object( $v ) ) { |
|
| 844 | - $query[ $wpvar ][ $vkey ] = (string) $v; |
|
| 845 | - } |
|
| 846 | - } |
|
| 847 | - } |
|
| 848 | - |
|
| 849 | - if ( isset( $post_type_query_vars[ $wpvar ] ) ) { |
|
| 850 | - $query['post_type'] = $post_type_query_vars[ $wpvar ]; |
|
| 851 | - $query['name'] = $query[ $wpvar ]; |
|
| 852 | - } |
|
| 853 | - } |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - // Do the query. |
|
| 857 | - if ( ! empty( $query['pagename'] ) ) { |
|
| 858 | - $args = array( |
|
| 859 | - 'name' => $query['pagename'], |
|
| 860 | - 'post_type' => 'page', |
|
| 861 | - 'showposts' => 1, |
|
| 862 | - ); |
|
| 863 | - |
|
| 864 | - if ( isset( $post ) && get_posts( $args ) === $post ) { |
|
| 865 | - return $post[0]->ID; |
|
| 866 | - } |
|
| 867 | - } |
|
| 868 | - |
|
| 869 | - $query = new WP_Query( $query ); |
|
| 870 | - |
|
| 871 | - if ( ! empty( $query->posts ) && $query->is_singular ) { |
|
| 872 | - return $query->post->ID; |
|
| 873 | - } else { |
|
| 874 | - |
|
| 875 | - // WooCommerce override. |
|
| 876 | - if ( isset( $query->query['post_type'] ) && 'product' === $query->query['post_type'] && class_exists( 'WooCommerce' ) ) { |
|
| 877 | - return get_option( 'woocommerce_shop_page_id' ); |
|
| 878 | - } |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - if ( ( empty( $query['page'] ) ) && ( empty( $query['pagename'] ) ) ) { |
|
| 882 | - return 0; |
|
| 883 | - } |
|
| 884 | - } |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - return 0; |
|
| 888 | - } |
|
| 889 | - |
|
| 890 | - /** |
|
| 891 | - * Get default values. |
|
| 892 | - */ |
|
| 893 | - public function default_values() { |
|
| 894 | - if ( ! empty( $this->boxes ) && empty( $this->options_defaults ) ) { |
|
| 895 | - foreach ( $this->boxes as $key => $box ) { |
|
| 896 | - if ( empty( $box['sections'] ) ) { |
|
| 897 | - continue; |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - // fill the cache. |
|
| 901 | - foreach ( $box['sections'] as $sk => $section ) { |
|
| 902 | - if ( ! isset( $section['id'] ) ) { |
|
| 903 | - if ( ! is_numeric( $sk ) || ! isset( $section['title'] ) ) { |
|
| 904 | - $section['id'] = $sk; |
|
| 905 | - } else { |
|
| 906 | - $section['id'] = sanitize_text_field( $section['title'] ); |
|
| 907 | - } |
|
| 908 | - $this->boxes[ $key ]['sections'][ $sk ] = $section; |
|
| 909 | - } |
|
| 910 | - if ( isset( $section['fields'] ) ) { |
|
| 911 | - foreach ( $section['fields'] as $k => $field ) { |
|
| 912 | - |
|
| 913 | - if ( empty( $field['id'] ) && empty( $field['type'] ) ) { |
|
| 914 | - continue; |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - if ( 'ace_editor' === $field['type'] && isset( $field['options'] ) ) { |
|
| 918 | - $this->boxes[ $key ]['sections'][ $sk ]['fields'][ $k ]['args'] = $field['options']; |
|
| 919 | - unset( $this->boxes[ $key ]['sections'][ $sk ]['fields'][ $k ]['options'] ); |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - if ( 'section' === $field['type'] && isset( $field['indent'] ) && ( true === $field['indent'] || 'true' === $field['indent'] ) ) { |
|
| 923 | - $field['class'] = $field['class'] ?? ''; |
|
| 924 | - $field['class'] .= 'redux-section-indent-start'; |
|
| 925 | - |
|
| 926 | - $this->boxes[ $key ]['sections'][ $sk ]['fields'][ $k ] = $field; |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - if ( ! isset( $this->parent->options_defaults_class ) ) { |
|
| 930 | - $this->parent->options_defaults_class = new Redux_Options_Defaults(); |
|
| 931 | - } |
|
| 932 | - |
|
| 933 | - $this->parent->options_defaults_class->field_default_values( $this->parent->args['opt_name'], $field ); |
|
| 934 | - |
|
| 935 | - if ( 'repeater' === $field['type'] ) { |
|
| 936 | - foreach ( $field['fields'] as $f ) { |
|
| 937 | - $this->parent->options_defaults_class->field_default_values( $this->parent->args['opt_name'], $f, null, true ); |
|
| 938 | - } |
|
| 939 | - } |
|
| 940 | - |
|
| 941 | - $this->parent->options_defaults = $this->parent->options_defaults_class->options_defaults; |
|
| 942 | - } |
|
| 943 | - } |
|
| 944 | - } |
|
| 945 | - } |
|
| 946 | - } |
|
| 947 | - |
|
| 948 | - if ( empty( $this->meta[ $this->post_id ] ) ) { |
|
| 949 | - $this->meta[ $this->post_id ] = $this->get_meta( $this->post_id ); |
|
| 950 | - } |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - /** |
|
| 954 | - * Add Meta Boxes |
|
| 955 | - */ |
|
| 956 | - public function add() { |
|
| 957 | - if ( empty( $this->boxes ) || ! is_array( $this->boxes ) ) { |
|
| 958 | - return; |
|
| 959 | - } |
|
| 960 | - |
|
| 961 | - foreach ( $this->boxes as $key => $box ) { |
|
| 962 | - if ( empty( $box['sections'] ) ) { |
|
| 963 | - continue; |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - // Save users from themselves. |
|
| 967 | - if ( isset( $box['position'] ) && ! in_array( strtolower( $box['position'] ), array( 'normal', 'advanced', 'side' ), true ) ) { |
|
| 968 | - unset( $box['position'] ); |
|
| 969 | - } |
|
| 970 | - |
|
| 971 | - if ( isset( $box['priority'] ) && ! in_array( strtolower( $box['priority'] ), array( 'high', 'core', 'default', 'low' ), true ) ) { |
|
| 972 | - unset( $box['priority'] ); |
|
| 973 | - } |
|
| 974 | - |
|
| 975 | - $defaults = array( |
|
| 976 | - 'id' => $key . '-' . $this->parent->args['opt_name'], |
|
| 977 | - 'post_types' => array( 'page', 'post' ), |
|
| 978 | - 'position' => 'normal', |
|
| 979 | - 'priority' => 'high', |
|
| 980 | - ); |
|
| 981 | - |
|
| 982 | - $box = wp_parse_args( $box, $defaults ); |
|
| 983 | - if ( ! empty( $box['post_types'] ) ) { |
|
| 984 | - foreach ( $box['post_types'] as $posttype ) { |
|
| 985 | - if ( isset( $box['title'] ) ) { |
|
| 986 | - $title = $box['title']; |
|
| 987 | - } elseif ( isset( $box['sections'] ) && 1 === count( $box['sections'] ) && isset( $box['sections'][0]['fields'] ) && 1 === count( $box['sections'][0]['fields'] ) && isset( $box['sections'][0]['fields'][0]['title'] ) ) { |
|
| 988 | - |
|
| 989 | - // If only one field in this box. |
|
| 990 | - $title = $box['sections'][0]['fields'][0]['title']; |
|
| 991 | - } else { |
|
| 992 | - $title = ucfirst( $posttype ) . ' ' . __( 'Options', 'redux-framework' ); |
|
| 993 | - } |
|
| 994 | - |
|
| 995 | - $args = array( |
|
| 996 | - 'position' => $box['position'], |
|
| 997 | - 'sections' => $box['sections'], |
|
| 998 | - 'key' => $key, |
|
| 999 | - ); |
|
| 1000 | - |
|
| 1001 | - // Override the parent args on a metabox level. |
|
| 1002 | - if ( empty( $this->orig_args ) ) { |
|
| 1003 | - $this->orig_args = $this->parent->args; |
|
| 1004 | - } |
|
| 1005 | - |
|
| 1006 | - if ( isset( $box['args'] ) ) { |
|
| 1007 | - $this->parent->args = wp_parse_args( $box['args'], $this->orig_args ); |
|
| 1008 | - } elseif ( $this->parent->args !== $this->orig_args ) { |
|
| 1009 | - $this->parent->args = $this->orig_args; |
|
| 1010 | - } |
|
| 1011 | - |
|
| 1012 | - add_filter( 'postbox_classes_' . $posttype . '_redux-' . $this->parent->args['opt_name'] . '-metabox-' . $box['id'], array( $this, 'add_box_classes' ) ); |
|
| 1013 | - |
|
| 1014 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 1015 | - do_action( 'redux/' . $this->parent->args['opt_name'] . '/extensions/metabox/add', $this, $box, $posttype ); |
|
| 1016 | - |
|
| 1017 | - if ( isset( $box['post_format'] ) ) { |
|
| 1018 | - add_filter( 'postbox_classes_' . $posttype . '_redux-' . $this->parent->args['opt_name'] . '-metabox-' . $box['id'], array( $this, 'add_box_hide_class' ) ); |
|
| 1019 | - } |
|
| 1020 | - |
|
| 1021 | - // phpcs:ignore Generic.Strings.UnnecessaryStringConcat |
|
| 1022 | - call_user_func( 'add' . '_meta' . '_box', 'redux-' . $this->parent->args['opt_name'] . '-metabox-' . $box['id'], $title, array( $this, 'generate_boxes' ), $posttype, $box['position'], $box['priority'], $args ); |
|
| 1023 | - } |
|
| 1024 | - } |
|
| 1025 | - } |
|
| 1026 | - } |
|
| 1027 | - |
|
| 1028 | - /** |
|
| 1029 | - * Add hidden class to metabox DIV. |
|
| 1030 | - * |
|
| 1031 | - * @param array $classes Class array. |
|
| 1032 | - * |
|
| 1033 | - * @return array |
|
| 1034 | - */ |
|
| 1035 | - public function add_box_hide_class( array $classes ): array { |
|
| 1036 | - $classes[] = 'hide'; |
|
| 1037 | - |
|
| 1038 | - return $classes; |
|
| 1039 | - } |
|
| 1040 | - |
|
| 1041 | - /** |
|
| 1042 | - * Field Defaults. |
|
| 1043 | - * |
|
| 1044 | - * @param mixed $field_id ID. |
|
| 1045 | - * |
|
| 1046 | - * @return mixed|string |
|
| 1047 | - */ |
|
| 1048 | - private function field_default( $field_id ) { |
|
| 1049 | - if ( ! isset( $this->parent->options_defaults ) ) { |
|
| 1050 | - $this->parent->options_defaults = $this->parent->default_values(); |
|
| 1051 | - } |
|
| 1052 | - |
|
| 1053 | - if ( ! isset( $this->parent->options ) || empty( $this->parent->options ) ) { |
|
| 1054 | - if ( ! isset( $this->parent->options_class ) ) { |
|
| 1055 | - $this->parent->options_class = new Redux_Options_Constructor( $this->parent ); |
|
| 1056 | - } |
|
| 1057 | - |
|
| 1058 | - $this->parent->options_class->get(); |
|
| 1059 | - } |
|
| 1060 | - |
|
| 1061 | - $this->options = $this->parent->options; |
|
| 1062 | - |
|
| 1063 | - if ( isset( $this->parent->options[ $field_id['id'] ] ) && isset( $this->parent->options_defaults[ $field_id['id'] ] ) && $this->parent->options[ $field_id['id'] ] !== $this->parent->options_defaults[ $field_id['id'] ] ) { |
|
| 1064 | - return $this->parent->options[ $field_id['id'] ]; |
|
| 1065 | - } else { |
|
| 1066 | - if ( empty( $this->options_defaults ) ) { |
|
| 1067 | - $this->default_values(); // fill cache. |
|
| 1068 | - } |
|
| 1069 | - |
|
| 1070 | - $data = ''; |
|
| 1071 | - if ( ! empty( $this->options_defaults ) ) { |
|
| 1072 | - $data = $this->options_defaults[ $field_id['id'] ] ?? ''; |
|
| 1073 | - } |
|
| 1074 | - |
|
| 1075 | - if ( empty( $data ) && isset( $this->parent->options_defaults[ $field_id['id'] ] ) ) { |
|
| 1076 | - $data = $this->parent->options_defaults[ $field_id['id'] ] ?? ''; |
|
| 1077 | - } |
|
| 1078 | - |
|
| 1079 | - return $data; |
|
| 1080 | - } |
|
| 1081 | - } |
|
| 1082 | - |
|
| 1083 | - /** |
|
| 1084 | - * Function to get and cache the post meta. |
|
| 1085 | - * |
|
| 1086 | - * @param mixed $id ID. |
|
| 1087 | - * |
|
| 1088 | - * @return array |
|
| 1089 | - */ |
|
| 1090 | - private function get_meta( $id ): array { |
|
| 1091 | - if ( ! isset( $this->meta[ $id ] ) ) { |
|
| 1092 | - $this->meta[ $id ] = array(); |
|
| 1093 | - $o_data = get_post_meta( $id ); |
|
| 1094 | - |
|
| 1095 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 1096 | - $o_data = apply_filters( "redux/metaboxes/{$this->parent->args['opt_name']}/get_meta", $o_data ); |
|
| 1097 | - |
|
| 1098 | - if ( ! empty( $o_data ) ) { |
|
| 1099 | - foreach ( $o_data as $key => $value ) { |
|
| 1100 | - if ( 1 === count( $value ) ) { |
|
| 1101 | - $this->meta[ $id ][ $key ] = maybe_unserialize( $value[0] ); |
|
| 1102 | - } else { |
|
| 1103 | - $new_value = array_map( 'maybe_unserialize', $value ); |
|
| 1104 | - |
|
| 1105 | - $this->meta[ $id ][ $key ] = $new_value[0]; |
|
| 1106 | - } |
|
| 1107 | - } |
|
| 1108 | - } |
|
| 1109 | - |
|
| 1110 | - if ( isset( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ) ) { |
|
| 1111 | - $data = maybe_unserialize( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ); |
|
| 1112 | - |
|
| 1113 | - foreach ( $data as $key => $value ) { |
|
| 1114 | - $this->meta[ $id ][ $key ] = $value; |
|
| 1115 | - update_post_meta( $id, $key, $value ); |
|
| 1116 | - } |
|
| 1117 | - |
|
| 1118 | - unset( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ); |
|
| 1119 | - |
|
| 1120 | - delete_post_meta( $id, $this->parent->args['opt_name'] ); |
|
| 1121 | - } |
|
| 1122 | - } |
|
| 1123 | - |
|
| 1124 | - return $this->meta[ $id ]; |
|
| 1125 | - } |
|
| 1126 | - |
|
| 1127 | - /** |
|
| 1128 | - * Get values. |
|
| 1129 | - * |
|
| 1130 | - * @param mixed $the_post Post object/id. |
|
| 1131 | - * @param string $meta_key Meta key. |
|
| 1132 | - * @param mixed $def_val Def value. |
|
| 1133 | - * |
|
| 1134 | - * @return array|mixed|string |
|
| 1135 | - */ |
|
| 1136 | - public function get_values( $the_post, string $meta_key = '', $def_val = '' ) { |
|
| 1137 | - |
|
| 1138 | - // Override these values if they differ from the admin panel defaults. ;) . |
|
| 1139 | - if ( isset( $the_post->post_type ) && in_array( $the_post->post_type, $this->post_types, true ) ) { |
|
| 1140 | - if ( isset( $this->post_type_values[ $the_post->post_type ] ) ) { |
|
| 1141 | - $meta = $this->post_type_fields[ $the_post->post_type ]; |
|
| 1142 | - } else { |
|
| 1143 | - $defaults = array(); |
|
| 1144 | - if ( ! empty( $this->post_type_fields[ $the_post->post_type ] ) ) { |
|
| 1145 | - foreach ( $this->post_type_fields[ $the_post->post_type ] as $key => $null ) { |
|
| 1146 | - if ( isset( $this->options_defaults[ $key ] ) ) { |
|
| 1147 | - $defaults[ $key ] = $this->options_defaults[ $key ]; |
|
| 1148 | - } |
|
| 1149 | - } |
|
| 1150 | - } |
|
| 1151 | - |
|
| 1152 | - $meta = wp_parse_args( $this->get_meta( $the_post->ID ), $defaults ); |
|
| 1153 | - |
|
| 1154 | - $this->post_type_fields[ $the_post->post_type ] = $meta; |
|
| 1155 | - } |
|
| 1156 | - |
|
| 1157 | - if ( ! empty( $meta_key ) ) { |
|
| 1158 | - if ( ! isset( $meta[ $meta_key ] ) ) { |
|
| 1159 | - $meta[ $meta_key ] = $def_val; |
|
| 1160 | - } |
|
| 1161 | - |
|
| 1162 | - return $meta[ $meta_key ]; |
|
| 1163 | - } else { |
|
| 1164 | - return $meta; |
|
| 1165 | - } |
|
| 1166 | - } |
|
| 1167 | - |
|
| 1168 | - return $def_val; |
|
| 1169 | - } |
|
| 1170 | - |
|
| 1171 | - /** |
|
| 1172 | - * Generate Boxes. |
|
| 1173 | - * |
|
| 1174 | - * @param mixed $post Post. |
|
| 1175 | - * @param array $metabox Metabox array. |
|
| 1176 | - */ |
|
| 1177 | - public function generate_boxes( $post, array $metabox ) { |
|
| 1178 | - if ( ! empty( $metabox['args']['permissions'] ) && ! Redux_Helpers::current_user_can( $metabox['args']['permissions'] ) ) { |
|
| 1179 | - return; |
|
| 1180 | - } |
|
| 1181 | - |
|
| 1182 | - $sections = $metabox['args']['sections']; |
|
| 1183 | - |
|
| 1184 | - wp_nonce_field( 'redux_metaboxes_meta_nonce', 'redux_metaboxes_meta_nonce' ); |
|
| 1185 | - |
|
| 1186 | - wp_dequeue_script( 'json-view-js' ); |
|
| 1187 | - |
|
| 1188 | - $sidebar = true; |
|
| 1189 | - |
|
| 1190 | - if ( 'side' === $metabox['args']['position'] || 1 === count( $sections ) || ( isset( $metabox['args']['sidebar'] ) && false === $metabox['args']['sidebar'] ) ) { |
|
| 1191 | - $sidebar = false; // Show the section dividers or not. |
|
| 1192 | - } |
|
| 1193 | - |
|
| 1194 | - ?> |
|
| 820 | + // Taken from class-wp.php. |
|
| 821 | + foreach ( $GLOBALS['wp_post_types'] as $post_type => $t ) { |
|
| 822 | + if ( isset( $t->query_var ) ) { |
|
| 823 | + $post_type_query_vars[ $t->query_var ] = $post_type; |
|
| 824 | + } |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + foreach ( $wp->public_query_vars as $wpvar ) { |
|
| 828 | + if ( isset( $wp->extra_query_vars[ $wpvar ] ) ) { |
|
| 829 | + $query[ $wpvar ] = $wp->extra_query_vars[ $wpvar ]; |
|
| 830 | + } elseif ( isset( $_POST[ $wpvar ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 831 | + $query[ $wpvar ] = sanitize_text_field( wp_unslash( $_POST[ $wpvar ] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 832 | + } elseif ( isset( $_GET[ $wpvar ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 833 | + $query[ $wpvar ] = sanitize_text_field( wp_unslash( $_GET[ $wpvar ] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 834 | + } elseif ( isset( $query_vars[ $wpvar ] ) ) { |
|
| 835 | + $query[ $wpvar ] = $query_vars[ $wpvar ]; |
|
| 836 | + } |
|
| 837 | + |
|
| 838 | + if ( ! empty( $query[ $wpvar ] ) ) { |
|
| 839 | + if ( ! is_array( $query[ $wpvar ] ) ) { |
|
| 840 | + $query[ $wpvar ] = (string) $query[ $wpvar ]; |
|
| 841 | + } else { |
|
| 842 | + foreach ( $query[ $wpvar ] as $vkey => $v ) { |
|
| 843 | + if ( ! is_object( $v ) ) { |
|
| 844 | + $query[ $wpvar ][ $vkey ] = (string) $v; |
|
| 845 | + } |
|
| 846 | + } |
|
| 847 | + } |
|
| 848 | + |
|
| 849 | + if ( isset( $post_type_query_vars[ $wpvar ] ) ) { |
|
| 850 | + $query['post_type'] = $post_type_query_vars[ $wpvar ]; |
|
| 851 | + $query['name'] = $query[ $wpvar ]; |
|
| 852 | + } |
|
| 853 | + } |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + // Do the query. |
|
| 857 | + if ( ! empty( $query['pagename'] ) ) { |
|
| 858 | + $args = array( |
|
| 859 | + 'name' => $query['pagename'], |
|
| 860 | + 'post_type' => 'page', |
|
| 861 | + 'showposts' => 1, |
|
| 862 | + ); |
|
| 863 | + |
|
| 864 | + if ( isset( $post ) && get_posts( $args ) === $post ) { |
|
| 865 | + return $post[0]->ID; |
|
| 866 | + } |
|
| 867 | + } |
|
| 868 | + |
|
| 869 | + $query = new WP_Query( $query ); |
|
| 870 | + |
|
| 871 | + if ( ! empty( $query->posts ) && $query->is_singular ) { |
|
| 872 | + return $query->post->ID; |
|
| 873 | + } else { |
|
| 874 | + |
|
| 875 | + // WooCommerce override. |
|
| 876 | + if ( isset( $query->query['post_type'] ) && 'product' === $query->query['post_type'] && class_exists( 'WooCommerce' ) ) { |
|
| 877 | + return get_option( 'woocommerce_shop_page_id' ); |
|
| 878 | + } |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + if ( ( empty( $query['page'] ) ) && ( empty( $query['pagename'] ) ) ) { |
|
| 882 | + return 0; |
|
| 883 | + } |
|
| 884 | + } |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + return 0; |
|
| 888 | + } |
|
| 889 | + |
|
| 890 | + /** |
|
| 891 | + * Get default values. |
|
| 892 | + */ |
|
| 893 | + public function default_values() { |
|
| 894 | + if ( ! empty( $this->boxes ) && empty( $this->options_defaults ) ) { |
|
| 895 | + foreach ( $this->boxes as $key => $box ) { |
|
| 896 | + if ( empty( $box['sections'] ) ) { |
|
| 897 | + continue; |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + // fill the cache. |
|
| 901 | + foreach ( $box['sections'] as $sk => $section ) { |
|
| 902 | + if ( ! isset( $section['id'] ) ) { |
|
| 903 | + if ( ! is_numeric( $sk ) || ! isset( $section['title'] ) ) { |
|
| 904 | + $section['id'] = $sk; |
|
| 905 | + } else { |
|
| 906 | + $section['id'] = sanitize_text_field( $section['title'] ); |
|
| 907 | + } |
|
| 908 | + $this->boxes[ $key ]['sections'][ $sk ] = $section; |
|
| 909 | + } |
|
| 910 | + if ( isset( $section['fields'] ) ) { |
|
| 911 | + foreach ( $section['fields'] as $k => $field ) { |
|
| 912 | + |
|
| 913 | + if ( empty( $field['id'] ) && empty( $field['type'] ) ) { |
|
| 914 | + continue; |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + if ( 'ace_editor' === $field['type'] && isset( $field['options'] ) ) { |
|
| 918 | + $this->boxes[ $key ]['sections'][ $sk ]['fields'][ $k ]['args'] = $field['options']; |
|
| 919 | + unset( $this->boxes[ $key ]['sections'][ $sk ]['fields'][ $k ]['options'] ); |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + if ( 'section' === $field['type'] && isset( $field['indent'] ) && ( true === $field['indent'] || 'true' === $field['indent'] ) ) { |
|
| 923 | + $field['class'] = $field['class'] ?? ''; |
|
| 924 | + $field['class'] .= 'redux-section-indent-start'; |
|
| 925 | + |
|
| 926 | + $this->boxes[ $key ]['sections'][ $sk ]['fields'][ $k ] = $field; |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + if ( ! isset( $this->parent->options_defaults_class ) ) { |
|
| 930 | + $this->parent->options_defaults_class = new Redux_Options_Defaults(); |
|
| 931 | + } |
|
| 932 | + |
|
| 933 | + $this->parent->options_defaults_class->field_default_values( $this->parent->args['opt_name'], $field ); |
|
| 934 | + |
|
| 935 | + if ( 'repeater' === $field['type'] ) { |
|
| 936 | + foreach ( $field['fields'] as $f ) { |
|
| 937 | + $this->parent->options_defaults_class->field_default_values( $this->parent->args['opt_name'], $f, null, true ); |
|
| 938 | + } |
|
| 939 | + } |
|
| 940 | + |
|
| 941 | + $this->parent->options_defaults = $this->parent->options_defaults_class->options_defaults; |
|
| 942 | + } |
|
| 943 | + } |
|
| 944 | + } |
|
| 945 | + } |
|
| 946 | + } |
|
| 947 | + |
|
| 948 | + if ( empty( $this->meta[ $this->post_id ] ) ) { |
|
| 949 | + $this->meta[ $this->post_id ] = $this->get_meta( $this->post_id ); |
|
| 950 | + } |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + /** |
|
| 954 | + * Add Meta Boxes |
|
| 955 | + */ |
|
| 956 | + public function add() { |
|
| 957 | + if ( empty( $this->boxes ) || ! is_array( $this->boxes ) ) { |
|
| 958 | + return; |
|
| 959 | + } |
|
| 960 | + |
|
| 961 | + foreach ( $this->boxes as $key => $box ) { |
|
| 962 | + if ( empty( $box['sections'] ) ) { |
|
| 963 | + continue; |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + // Save users from themselves. |
|
| 967 | + if ( isset( $box['position'] ) && ! in_array( strtolower( $box['position'] ), array( 'normal', 'advanced', 'side' ), true ) ) { |
|
| 968 | + unset( $box['position'] ); |
|
| 969 | + } |
|
| 970 | + |
|
| 971 | + if ( isset( $box['priority'] ) && ! in_array( strtolower( $box['priority'] ), array( 'high', 'core', 'default', 'low' ), true ) ) { |
|
| 972 | + unset( $box['priority'] ); |
|
| 973 | + } |
|
| 974 | + |
|
| 975 | + $defaults = array( |
|
| 976 | + 'id' => $key . '-' . $this->parent->args['opt_name'], |
|
| 977 | + 'post_types' => array( 'page', 'post' ), |
|
| 978 | + 'position' => 'normal', |
|
| 979 | + 'priority' => 'high', |
|
| 980 | + ); |
|
| 981 | + |
|
| 982 | + $box = wp_parse_args( $box, $defaults ); |
|
| 983 | + if ( ! empty( $box['post_types'] ) ) { |
|
| 984 | + foreach ( $box['post_types'] as $posttype ) { |
|
| 985 | + if ( isset( $box['title'] ) ) { |
|
| 986 | + $title = $box['title']; |
|
| 987 | + } elseif ( isset( $box['sections'] ) && 1 === count( $box['sections'] ) && isset( $box['sections'][0]['fields'] ) && 1 === count( $box['sections'][0]['fields'] ) && isset( $box['sections'][0]['fields'][0]['title'] ) ) { |
|
| 988 | + |
|
| 989 | + // If only one field in this box. |
|
| 990 | + $title = $box['sections'][0]['fields'][0]['title']; |
|
| 991 | + } else { |
|
| 992 | + $title = ucfirst( $posttype ) . ' ' . __( 'Options', 'redux-framework' ); |
|
| 993 | + } |
|
| 994 | + |
|
| 995 | + $args = array( |
|
| 996 | + 'position' => $box['position'], |
|
| 997 | + 'sections' => $box['sections'], |
|
| 998 | + 'key' => $key, |
|
| 999 | + ); |
|
| 1000 | + |
|
| 1001 | + // Override the parent args on a metabox level. |
|
| 1002 | + if ( empty( $this->orig_args ) ) { |
|
| 1003 | + $this->orig_args = $this->parent->args; |
|
| 1004 | + } |
|
| 1005 | + |
|
| 1006 | + if ( isset( $box['args'] ) ) { |
|
| 1007 | + $this->parent->args = wp_parse_args( $box['args'], $this->orig_args ); |
|
| 1008 | + } elseif ( $this->parent->args !== $this->orig_args ) { |
|
| 1009 | + $this->parent->args = $this->orig_args; |
|
| 1010 | + } |
|
| 1011 | + |
|
| 1012 | + add_filter( 'postbox_classes_' . $posttype . '_redux-' . $this->parent->args['opt_name'] . '-metabox-' . $box['id'], array( $this, 'add_box_classes' ) ); |
|
| 1013 | + |
|
| 1014 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 1015 | + do_action( 'redux/' . $this->parent->args['opt_name'] . '/extensions/metabox/add', $this, $box, $posttype ); |
|
| 1016 | + |
|
| 1017 | + if ( isset( $box['post_format'] ) ) { |
|
| 1018 | + add_filter( 'postbox_classes_' . $posttype . '_redux-' . $this->parent->args['opt_name'] . '-metabox-' . $box['id'], array( $this, 'add_box_hide_class' ) ); |
|
| 1019 | + } |
|
| 1020 | + |
|
| 1021 | + // phpcs:ignore Generic.Strings.UnnecessaryStringConcat |
|
| 1022 | + call_user_func( 'add' . '_meta' . '_box', 'redux-' . $this->parent->args['opt_name'] . '-metabox-' . $box['id'], $title, array( $this, 'generate_boxes' ), $posttype, $box['position'], $box['priority'], $args ); |
|
| 1023 | + } |
|
| 1024 | + } |
|
| 1025 | + } |
|
| 1026 | + } |
|
| 1027 | + |
|
| 1028 | + /** |
|
| 1029 | + * Add hidden class to metabox DIV. |
|
| 1030 | + * |
|
| 1031 | + * @param array $classes Class array. |
|
| 1032 | + * |
|
| 1033 | + * @return array |
|
| 1034 | + */ |
|
| 1035 | + public function add_box_hide_class( array $classes ): array { |
|
| 1036 | + $classes[] = 'hide'; |
|
| 1037 | + |
|
| 1038 | + return $classes; |
|
| 1039 | + } |
|
| 1040 | + |
|
| 1041 | + /** |
|
| 1042 | + * Field Defaults. |
|
| 1043 | + * |
|
| 1044 | + * @param mixed $field_id ID. |
|
| 1045 | + * |
|
| 1046 | + * @return mixed|string |
|
| 1047 | + */ |
|
| 1048 | + private function field_default( $field_id ) { |
|
| 1049 | + if ( ! isset( $this->parent->options_defaults ) ) { |
|
| 1050 | + $this->parent->options_defaults = $this->parent->default_values(); |
|
| 1051 | + } |
|
| 1052 | + |
|
| 1053 | + if ( ! isset( $this->parent->options ) || empty( $this->parent->options ) ) { |
|
| 1054 | + if ( ! isset( $this->parent->options_class ) ) { |
|
| 1055 | + $this->parent->options_class = new Redux_Options_Constructor( $this->parent ); |
|
| 1056 | + } |
|
| 1057 | + |
|
| 1058 | + $this->parent->options_class->get(); |
|
| 1059 | + } |
|
| 1060 | + |
|
| 1061 | + $this->options = $this->parent->options; |
|
| 1062 | + |
|
| 1063 | + if ( isset( $this->parent->options[ $field_id['id'] ] ) && isset( $this->parent->options_defaults[ $field_id['id'] ] ) && $this->parent->options[ $field_id['id'] ] !== $this->parent->options_defaults[ $field_id['id'] ] ) { |
|
| 1064 | + return $this->parent->options[ $field_id['id'] ]; |
|
| 1065 | + } else { |
|
| 1066 | + if ( empty( $this->options_defaults ) ) { |
|
| 1067 | + $this->default_values(); // fill cache. |
|
| 1068 | + } |
|
| 1069 | + |
|
| 1070 | + $data = ''; |
|
| 1071 | + if ( ! empty( $this->options_defaults ) ) { |
|
| 1072 | + $data = $this->options_defaults[ $field_id['id'] ] ?? ''; |
|
| 1073 | + } |
|
| 1074 | + |
|
| 1075 | + if ( empty( $data ) && isset( $this->parent->options_defaults[ $field_id['id'] ] ) ) { |
|
| 1076 | + $data = $this->parent->options_defaults[ $field_id['id'] ] ?? ''; |
|
| 1077 | + } |
|
| 1078 | + |
|
| 1079 | + return $data; |
|
| 1080 | + } |
|
| 1081 | + } |
|
| 1082 | + |
|
| 1083 | + /** |
|
| 1084 | + * Function to get and cache the post meta. |
|
| 1085 | + * |
|
| 1086 | + * @param mixed $id ID. |
|
| 1087 | + * |
|
| 1088 | + * @return array |
|
| 1089 | + */ |
|
| 1090 | + private function get_meta( $id ): array { |
|
| 1091 | + if ( ! isset( $this->meta[ $id ] ) ) { |
|
| 1092 | + $this->meta[ $id ] = array(); |
|
| 1093 | + $o_data = get_post_meta( $id ); |
|
| 1094 | + |
|
| 1095 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 1096 | + $o_data = apply_filters( "redux/metaboxes/{$this->parent->args['opt_name']}/get_meta", $o_data ); |
|
| 1097 | + |
|
| 1098 | + if ( ! empty( $o_data ) ) { |
|
| 1099 | + foreach ( $o_data as $key => $value ) { |
|
| 1100 | + if ( 1 === count( $value ) ) { |
|
| 1101 | + $this->meta[ $id ][ $key ] = maybe_unserialize( $value[0] ); |
|
| 1102 | + } else { |
|
| 1103 | + $new_value = array_map( 'maybe_unserialize', $value ); |
|
| 1104 | + |
|
| 1105 | + $this->meta[ $id ][ $key ] = $new_value[0]; |
|
| 1106 | + } |
|
| 1107 | + } |
|
| 1108 | + } |
|
| 1109 | + |
|
| 1110 | + if ( isset( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ) ) { |
|
| 1111 | + $data = maybe_unserialize( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ); |
|
| 1112 | + |
|
| 1113 | + foreach ( $data as $key => $value ) { |
|
| 1114 | + $this->meta[ $id ][ $key ] = $value; |
|
| 1115 | + update_post_meta( $id, $key, $value ); |
|
| 1116 | + } |
|
| 1117 | + |
|
| 1118 | + unset( $this->meta[ $id ][ $this->parent->args['opt_name'] ] ); |
|
| 1119 | + |
|
| 1120 | + delete_post_meta( $id, $this->parent->args['opt_name'] ); |
|
| 1121 | + } |
|
| 1122 | + } |
|
| 1123 | + |
|
| 1124 | + return $this->meta[ $id ]; |
|
| 1125 | + } |
|
| 1126 | + |
|
| 1127 | + /** |
|
| 1128 | + * Get values. |
|
| 1129 | + * |
|
| 1130 | + * @param mixed $the_post Post object/id. |
|
| 1131 | + * @param string $meta_key Meta key. |
|
| 1132 | + * @param mixed $def_val Def value. |
|
| 1133 | + * |
|
| 1134 | + * @return array|mixed|string |
|
| 1135 | + */ |
|
| 1136 | + public function get_values( $the_post, string $meta_key = '', $def_val = '' ) { |
|
| 1137 | + |
|
| 1138 | + // Override these values if they differ from the admin panel defaults. ;) . |
|
| 1139 | + if ( isset( $the_post->post_type ) && in_array( $the_post->post_type, $this->post_types, true ) ) { |
|
| 1140 | + if ( isset( $this->post_type_values[ $the_post->post_type ] ) ) { |
|
| 1141 | + $meta = $this->post_type_fields[ $the_post->post_type ]; |
|
| 1142 | + } else { |
|
| 1143 | + $defaults = array(); |
|
| 1144 | + if ( ! empty( $this->post_type_fields[ $the_post->post_type ] ) ) { |
|
| 1145 | + foreach ( $this->post_type_fields[ $the_post->post_type ] as $key => $null ) { |
|
| 1146 | + if ( isset( $this->options_defaults[ $key ] ) ) { |
|
| 1147 | + $defaults[ $key ] = $this->options_defaults[ $key ]; |
|
| 1148 | + } |
|
| 1149 | + } |
|
| 1150 | + } |
|
| 1151 | + |
|
| 1152 | + $meta = wp_parse_args( $this->get_meta( $the_post->ID ), $defaults ); |
|
| 1153 | + |
|
| 1154 | + $this->post_type_fields[ $the_post->post_type ] = $meta; |
|
| 1155 | + } |
|
| 1156 | + |
|
| 1157 | + if ( ! empty( $meta_key ) ) { |
|
| 1158 | + if ( ! isset( $meta[ $meta_key ] ) ) { |
|
| 1159 | + $meta[ $meta_key ] = $def_val; |
|
| 1160 | + } |
|
| 1161 | + |
|
| 1162 | + return $meta[ $meta_key ]; |
|
| 1163 | + } else { |
|
| 1164 | + return $meta; |
|
| 1165 | + } |
|
| 1166 | + } |
|
| 1167 | + |
|
| 1168 | + return $def_val; |
|
| 1169 | + } |
|
| 1170 | + |
|
| 1171 | + /** |
|
| 1172 | + * Generate Boxes. |
|
| 1173 | + * |
|
| 1174 | + * @param mixed $post Post. |
|
| 1175 | + * @param array $metabox Metabox array. |
|
| 1176 | + */ |
|
| 1177 | + public function generate_boxes( $post, array $metabox ) { |
|
| 1178 | + if ( ! empty( $metabox['args']['permissions'] ) && ! Redux_Helpers::current_user_can( $metabox['args']['permissions'] ) ) { |
|
| 1179 | + return; |
|
| 1180 | + } |
|
| 1181 | + |
|
| 1182 | + $sections = $metabox['args']['sections']; |
|
| 1183 | + |
|
| 1184 | + wp_nonce_field( 'redux_metaboxes_meta_nonce', 'redux_metaboxes_meta_nonce' ); |
|
| 1185 | + |
|
| 1186 | + wp_dequeue_script( 'json-view-js' ); |
|
| 1187 | + |
|
| 1188 | + $sidebar = true; |
|
| 1189 | + |
|
| 1190 | + if ( 'side' === $metabox['args']['position'] || 1 === count( $sections ) || ( isset( $metabox['args']['sidebar'] ) && false === $metabox['args']['sidebar'] ) ) { |
|
| 1191 | + $sidebar = false; // Show the section dividers or not. |
|
| 1192 | + } |
|
| 1193 | + |
|
| 1194 | + ?> |
|
| 1195 | 1195 | <input |
| 1196 | 1196 | type="hidden" |
| 1197 | 1197 | id="currentSection" |
@@ -1221,21 +1221,21 @@ discard block |
||
| 1221 | 1221 | </div> |
| 1222 | 1222 | </div> |
| 1223 | 1223 | <?php |
| 1224 | - echo '<a href="javascript:void(0);" class="expand_options hide" style="display:none;">' . esc_html__( 'Expand', 'redux-framework' ) . '</a>'; |
|
| 1225 | - if ( $sidebar ) { |
|
| 1226 | - ?> |
|
| 1224 | + echo '<a href="javascript:void(0);" class="expand_options hide" style="display:none;">' . esc_html__( 'Expand', 'redux-framework' ) . '</a>'; |
|
| 1225 | + if ( $sidebar ) { |
|
| 1226 | + ?> |
|
| 1227 | 1227 | <div class="redux-sidebar"> |
| 1228 | 1228 | <ul class="redux-group-menu"> |
| 1229 | 1229 | <?php |
| 1230 | 1230 | |
| 1231 | - foreach ( $sections as $s_key => $section ) { |
|
| 1232 | - if ( ! empty( $section['permissions'] ) && ! Redux_Helpers::current_user_can( $section['permissions'] ) ) { |
|
| 1233 | - continue; |
|
| 1234 | - } |
|
| 1231 | + foreach ( $sections as $s_key => $section ) { |
|
| 1232 | + if ( ! empty( $section['permissions'] ) && ! Redux_Helpers::current_user_can( $section['permissions'] ) ) { |
|
| 1233 | + continue; |
|
| 1234 | + } |
|
| 1235 | 1235 | |
| 1236 | - echo $this->parent->render_class->section_menu( $s_key, $section, '_box_' . $metabox['id'], $sections ); // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 1237 | - } |
|
| 1238 | - ?> |
|
| 1236 | + echo $this->parent->render_class->section_menu( $s_key, $section, '_box_' . $metabox['id'], $sections ); // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 1237 | + } |
|
| 1238 | + ?> |
|
| 1239 | 1239 | </ul> |
| 1240 | 1240 | </div> |
| 1241 | 1241 | <?php } ?> |
@@ -1243,295 +1243,295 @@ discard block |
||
| 1243 | 1243 | <div class="redux-main"> |
| 1244 | 1244 | <?php |
| 1245 | 1245 | |
| 1246 | - foreach ( $sections as $s_key => $section ) { |
|
| 1247 | - if ( ! empty( $section['permissions'] ) && ! Redux_Helpers::current_user_can( $section['permissions'] ) ) { |
|
| 1248 | - continue; |
|
| 1249 | - } |
|
| 1250 | - if ( ! empty( $section['fields'] ) ) { |
|
| 1251 | - if ( isset( $section['args'] ) ) { |
|
| 1252 | - $this->parent->args = wp_parse_args( $section['args'], $this->orig_args ); |
|
| 1253 | - } elseif ( $this->parent->args !== $this->orig_args ) { |
|
| 1254 | - $this->parent->args = $this->orig_args; |
|
| 1255 | - } |
|
| 1256 | - |
|
| 1257 | - $hide = $sidebar ? '' : ' display-group'; |
|
| 1258 | - $section['class'] = isset( $section['class'] ) ? ' ' . $section['class'] : ''; |
|
| 1259 | - echo '<div id="' . esc_attr( $s_key ) . '_box_' . esc_attr( $metabox['id'] ) . '_section_group" class="redux-group-tab' . esc_attr( $section['class'] ) . ' redux_metabox_panel' . esc_attr( $hide ) . '">'; |
|
| 1260 | - |
|
| 1261 | - if ( ! empty( $section['title'] ) ) { |
|
| 1262 | - echo '<h3 class="redux-section-title">' . wp_kses_post( $section['title'] ) . '</h3>'; |
|
| 1263 | - } |
|
| 1264 | - |
|
| 1265 | - if ( ! empty( $section['desc'] ) ) { |
|
| 1266 | - echo '<div class="redux-section-desc">' . wp_kses_post( $section['desc'] ) . '</div>'; |
|
| 1267 | - } |
|
| 1268 | - |
|
| 1269 | - echo '<table class="form-table"><tbody>'; |
|
| 1270 | - foreach ( $section['fields'] as $field ) { |
|
| 1271 | - if ( ! empty( $field['permissions'] ) && ! Redux_Helpers::current_user_can( $field['permissions'] ) ) { |
|
| 1272 | - continue; |
|
| 1273 | - } |
|
| 1274 | - $field['name'] = $this->parent->args['opt_name'] . '[' . $field['id'] . ']'; |
|
| 1275 | - |
|
| 1276 | - $is_hidden = false; |
|
| 1277 | - $ex_style = ''; |
|
| 1278 | - if ( isset( $field['hidden'] ) && $field['hidden'] ) { |
|
| 1279 | - $is_hidden = true; |
|
| 1280 | - $ex_style = ' style="border-bottom: none;"'; |
|
| 1281 | - } |
|
| 1282 | - |
|
| 1283 | - echo '<tr valign="top"' . $ex_style . '>'; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 1284 | - |
|
| 1285 | - $th = $this->parent->render_class->get_header_html( $field ); |
|
| 1286 | - |
|
| 1287 | - if ( $is_hidden ) { |
|
| 1288 | - $str_pos = strpos( $th, 'redux_field_th' ); |
|
| 1289 | - |
|
| 1290 | - if ( $str_pos > - 1 ) { |
|
| 1291 | - $th = str_replace( 'redux_field_th', 'redux_field_th hide', $th ); |
|
| 1292 | - } |
|
| 1293 | - } |
|
| 1294 | - |
|
| 1295 | - if ( $sidebar ) { |
|
| 1296 | - if ( ! ( isset( $metabox['args']['sections'] ) && 1 === count( $metabox['args']['sections'] ) && isset( $metabox['args']['sections'][0]['fields'] ) && 1 === count( $metabox['args']['sections'][0]['fields'] ) ) && isset( $field['title'] ) ) { |
|
| 1297 | - echo '<th scope="row">'; |
|
| 1298 | - if ( ! empty( $th ) ) { |
|
| 1299 | - echo $th; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 1300 | - } |
|
| 1301 | - echo '</th>'; |
|
| 1302 | - echo '<td>'; |
|
| 1303 | - } |
|
| 1304 | - } else { |
|
| 1305 | - echo '<td>' . $th; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 1306 | - } |
|
| 1307 | - |
|
| 1308 | - if ( 'section' === $field['type'] && ( 'true' === $field['indent'] || true === $field['indent'] ) ) { |
|
| 1309 | - $field['class'] = $field['class'] ?? ''; |
|
| 1310 | - $field['class'] .= 'redux-section-indent-start'; |
|
| 1311 | - } |
|
| 1312 | - |
|
| 1313 | - if ( ! isset( $this->meta[ $this->post_id ][ $field['id'] ] ) ) { |
|
| 1314 | - $this->meta[ $this->post_id ][ $field['id'] ] = ''; |
|
| 1315 | - } |
|
| 1316 | - |
|
| 1317 | - $this->parent->render_class->field_input( $field, $this->meta[ $this->post_id ][ $field['id'] ], true ); |
|
| 1318 | - echo '</td></tr>'; |
|
| 1319 | - } |
|
| 1320 | - echo '</tbody></table>'; |
|
| 1321 | - echo '</div>'; |
|
| 1322 | - } |
|
| 1323 | - } |
|
| 1324 | - ?> |
|
| 1246 | + foreach ( $sections as $s_key => $section ) { |
|
| 1247 | + if ( ! empty( $section['permissions'] ) && ! Redux_Helpers::current_user_can( $section['permissions'] ) ) { |
|
| 1248 | + continue; |
|
| 1249 | + } |
|
| 1250 | + if ( ! empty( $section['fields'] ) ) { |
|
| 1251 | + if ( isset( $section['args'] ) ) { |
|
| 1252 | + $this->parent->args = wp_parse_args( $section['args'], $this->orig_args ); |
|
| 1253 | + } elseif ( $this->parent->args !== $this->orig_args ) { |
|
| 1254 | + $this->parent->args = $this->orig_args; |
|
| 1255 | + } |
|
| 1256 | + |
|
| 1257 | + $hide = $sidebar ? '' : ' display-group'; |
|
| 1258 | + $section['class'] = isset( $section['class'] ) ? ' ' . $section['class'] : ''; |
|
| 1259 | + echo '<div id="' . esc_attr( $s_key ) . '_box_' . esc_attr( $metabox['id'] ) . '_section_group" class="redux-group-tab' . esc_attr( $section['class'] ) . ' redux_metabox_panel' . esc_attr( $hide ) . '">'; |
|
| 1260 | + |
|
| 1261 | + if ( ! empty( $section['title'] ) ) { |
|
| 1262 | + echo '<h3 class="redux-section-title">' . wp_kses_post( $section['title'] ) . '</h3>'; |
|
| 1263 | + } |
|
| 1264 | + |
|
| 1265 | + if ( ! empty( $section['desc'] ) ) { |
|
| 1266 | + echo '<div class="redux-section-desc">' . wp_kses_post( $section['desc'] ) . '</div>'; |
|
| 1267 | + } |
|
| 1268 | + |
|
| 1269 | + echo '<table class="form-table"><tbody>'; |
|
| 1270 | + foreach ( $section['fields'] as $field ) { |
|
| 1271 | + if ( ! empty( $field['permissions'] ) && ! Redux_Helpers::current_user_can( $field['permissions'] ) ) { |
|
| 1272 | + continue; |
|
| 1273 | + } |
|
| 1274 | + $field['name'] = $this->parent->args['opt_name'] . '[' . $field['id'] . ']'; |
|
| 1275 | + |
|
| 1276 | + $is_hidden = false; |
|
| 1277 | + $ex_style = ''; |
|
| 1278 | + if ( isset( $field['hidden'] ) && $field['hidden'] ) { |
|
| 1279 | + $is_hidden = true; |
|
| 1280 | + $ex_style = ' style="border-bottom: none;"'; |
|
| 1281 | + } |
|
| 1282 | + |
|
| 1283 | + echo '<tr valign="top"' . $ex_style . '>'; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 1284 | + |
|
| 1285 | + $th = $this->parent->render_class->get_header_html( $field ); |
|
| 1286 | + |
|
| 1287 | + if ( $is_hidden ) { |
|
| 1288 | + $str_pos = strpos( $th, 'redux_field_th' ); |
|
| 1289 | + |
|
| 1290 | + if ( $str_pos > - 1 ) { |
|
| 1291 | + $th = str_replace( 'redux_field_th', 'redux_field_th hide', $th ); |
|
| 1292 | + } |
|
| 1293 | + } |
|
| 1294 | + |
|
| 1295 | + if ( $sidebar ) { |
|
| 1296 | + if ( ! ( isset( $metabox['args']['sections'] ) && 1 === count( $metabox['args']['sections'] ) && isset( $metabox['args']['sections'][0]['fields'] ) && 1 === count( $metabox['args']['sections'][0]['fields'] ) ) && isset( $field['title'] ) ) { |
|
| 1297 | + echo '<th scope="row">'; |
|
| 1298 | + if ( ! empty( $th ) ) { |
|
| 1299 | + echo $th; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 1300 | + } |
|
| 1301 | + echo '</th>'; |
|
| 1302 | + echo '<td>'; |
|
| 1303 | + } |
|
| 1304 | + } else { |
|
| 1305 | + echo '<td>' . $th; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 1306 | + } |
|
| 1307 | + |
|
| 1308 | + if ( 'section' === $field['type'] && ( 'true' === $field['indent'] || true === $field['indent'] ) ) { |
|
| 1309 | + $field['class'] = $field['class'] ?? ''; |
|
| 1310 | + $field['class'] .= 'redux-section-indent-start'; |
|
| 1311 | + } |
|
| 1312 | + |
|
| 1313 | + if ( ! isset( $this->meta[ $this->post_id ][ $field['id'] ] ) ) { |
|
| 1314 | + $this->meta[ $this->post_id ][ $field['id'] ] = ''; |
|
| 1315 | + } |
|
| 1316 | + |
|
| 1317 | + $this->parent->render_class->field_input( $field, $this->meta[ $this->post_id ][ $field['id'] ], true ); |
|
| 1318 | + echo '</td></tr>'; |
|
| 1319 | + } |
|
| 1320 | + echo '</tbody></table>'; |
|
| 1321 | + echo '</div>'; |
|
| 1322 | + } |
|
| 1323 | + } |
|
| 1324 | + ?> |
|
| 1325 | 1325 | </div> |
| 1326 | 1326 | <div class="clear"></div> |
| 1327 | 1327 | </div> |
| 1328 | 1328 | <?php |
| 1329 | - } |
|
| 1330 | - |
|
| 1331 | - /** |
|
| 1332 | - * Save meta boxes |
|
| 1333 | - * Runs when a post is saved and does an action which the write panel save scripts can hook into. |
|
| 1334 | - * |
|
| 1335 | - * @access public |
|
| 1336 | - * |
|
| 1337 | - * @param mixed $post_id Post ID. |
|
| 1338 | - * @param mixed $post Post. |
|
| 1339 | - * |
|
| 1340 | - * @return mixed |
|
| 1341 | - */ |
|
| 1342 | - public function meta_boxes_save( $post_id, $post ) { |
|
| 1343 | - if ( isset( $_POST['vc_inline'] ) && sanitize_text_field( wp_unslash( $_POST['vc_inline'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 1344 | - return $post_id; |
|
| 1345 | - } |
|
| 1346 | - |
|
| 1347 | - if ( isset( $_POST['post_ID'] ) && strval( $post_id ) !== $_POST['post_ID'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 1348 | - return $post_id; |
|
| 1349 | - } |
|
| 1350 | - |
|
| 1351 | - // Check if our nonce is set. |
|
| 1352 | - if ( ! isset( $_POST['redux_metaboxes_meta_nonce'] ) || ! isset( $_POST[ $this->parent->args['opt_name'] ] ) ) { |
|
| 1353 | - return $post_id; |
|
| 1354 | - } |
|
| 1355 | - |
|
| 1356 | - // Verify that the nonce is valid. |
|
| 1357 | - // Validate fields (if needed). |
|
| 1358 | - if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['redux_metaboxes_meta_nonce'] ) ), 'redux_metaboxes_meta_nonce' ) ) { |
|
| 1359 | - return $post_id; |
|
| 1360 | - } |
|
| 1361 | - |
|
| 1362 | - // If this is an autosave, our form has not been submitted, so we don't want to do anything. |
|
| 1363 | - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 1364 | - return $post_id; |
|
| 1365 | - } |
|
| 1366 | - |
|
| 1367 | - // Check the user's permissions, even allowing custom capabilities. |
|
| 1368 | - $obj = get_post_type_object( $post->post_type ); |
|
| 1369 | - if ( ! current_user_can( $obj->cap->edit_post, $post_id ) ) { |
|
| 1370 | - return $post_id; |
|
| 1371 | - } |
|
| 1372 | - |
|
| 1373 | - // Import. |
|
| 1374 | - if ( isset( $_POST[ $this->parent->args['opt_name'] ]['import_code'] ) && ! empty( $_POST[ $this->parent->args['opt_name'] ]['import_code'] ) ) { |
|
| 1375 | - $import = json_decode( sanitize_text_field( wp_unslash( $_POST[ $this->parent->args['opt_name'] ]['import_code'] ) ), true ); |
|
| 1376 | - unset( $_POST[ $this->parent->args['opt_name'] ]['import_code'] ); |
|
| 1377 | - |
|
| 1378 | - foreach ( Redux_Helpers::sanitize_array( wp_unslash( $_POST[ $this->parent->args['opt_name'] ] ) ) as $key => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
|
| 1379 | - if ( ! isset( $import[ $key ] ) ) { |
|
| 1380 | - $import[ $key ] = $value; |
|
| 1381 | - } |
|
| 1382 | - } |
|
| 1383 | - |
|
| 1384 | - $_POST[ $this->parent->args['opt_name'] ] = $import; |
|
| 1385 | - } |
|
| 1386 | - |
|
| 1387 | - $to_save = array(); |
|
| 1388 | - $to_compare = array(); |
|
| 1389 | - $to_delete = array(); |
|
| 1390 | - $dont_save = true; |
|
| 1391 | - |
|
| 1392 | - if ( isset( $this->parent->args['metaboxes_save_defaults'] ) && $this->parent->args['metaboxes_save_defaults'] ) { |
|
| 1393 | - $dont_save = false; |
|
| 1394 | - } |
|
| 1395 | - |
|
| 1396 | - foreach ( Redux_Helpers::sanitize_array( wp_unslash( $_POST[ $this->parent->args['opt_name'] ] ) ) as $key => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
|
| 1397 | - |
|
| 1398 | - // Have to remove the escaping for array comparison. |
|
| 1399 | - if ( is_array( $value ) ) { |
|
| 1400 | - foreach ( $value as $k => $v ) { |
|
| 1401 | - if ( ! is_array( $v ) ) { |
|
| 1402 | - $value[ $k ] = wp_unslash( $v ); |
|
| 1403 | - } |
|
| 1404 | - } |
|
| 1405 | - } |
|
| 1406 | - |
|
| 1407 | - $save = true; |
|
| 1408 | - |
|
| 1409 | - // parent_options. |
|
| 1410 | - if ( ! $dont_save && isset( $this->options_defaults[ $key ] ) && $value === $this->options_defaults[ $key ] ) { |
|
| 1411 | - $save = false; |
|
| 1412 | - } |
|
| 1413 | - |
|
| 1414 | - if ( $save && isset( $this->parent_options[ $key ] ) && $this->parent_options[ $key ] !== $value ) { |
|
| 1415 | - $save = false; |
|
| 1416 | - } |
|
| 1417 | - |
|
| 1418 | - if ( $save ) { |
|
| 1419 | - $to_save[ $key ] = $value; |
|
| 1420 | - $to_compare[ $key ] = $this->parent->options[ $key ] ?? ''; |
|
| 1421 | - } else { |
|
| 1422 | - $to_delete[ $key ] = $value; |
|
| 1423 | - } |
|
| 1424 | - } |
|
| 1425 | - |
|
| 1426 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 1427 | - $to_save = apply_filters( 'redux/metaboxes/save/before_validate', $to_save, $to_compare, $this->sections ); |
|
| 1428 | - |
|
| 1429 | - $validate = $this->parent->validate_class->validate( $to_save, $to_compare, $this->sections ); |
|
| 1430 | - |
|
| 1431 | - // Validate fields (if needed). |
|
| 1432 | - foreach ( $to_save as $key => $value ) { |
|
| 1433 | - if ( isset( $validate[ $key ] ) && $value !== $validate[ $key ] ) { |
|
| 1434 | - if ( isset( $this->parent->options[ $key ] ) && $validate[ $key ] === $this->parent->options[ $key ] ) { |
|
| 1435 | - unset( $to_save[ $key ] ); |
|
| 1436 | - } else { |
|
| 1437 | - $to_save[ $key ] = $validate[ $key ]; |
|
| 1438 | - } |
|
| 1439 | - } |
|
| 1440 | - } |
|
| 1441 | - |
|
| 1442 | - if ( ! empty( $this->parent->errors ) || ! empty( $this->parent->warnings ) ) { |
|
| 1443 | - $this->parent->transients['notices'] = ( isset( $this->parent->transients['notices'] ) && is_array( $this->parent->transients['notices'] ) ) ? $this->parent->transients['notices'] : array(); |
|
| 1444 | - |
|
| 1445 | - if ( ! isset( $this->parent->transients['notices']['errors'] ) || $this->parent->transients['notices']['errors'] !== $this->parent->errors ) { |
|
| 1446 | - $this->parent->transients['notices']['errors'] = $this->parent->errors; |
|
| 1447 | - $update_transients = true; |
|
| 1448 | - } |
|
| 1449 | - |
|
| 1450 | - if ( ! isset( $this->parent->transients['notices']['warnings'] ) || $this->parent->transients['notices']['warnings'] !== $this->parent->warnings ) { |
|
| 1451 | - $this->parent->transients['notices']['warnings'] = $this->parent->warnings; |
|
| 1452 | - $update_transients = true; |
|
| 1453 | - } |
|
| 1454 | - |
|
| 1455 | - if ( isset( $update_transients ) ) { |
|
| 1456 | - $this->parent->transients['notices']['override'] = 1; |
|
| 1457 | - set_transient( $this->parent->args['opt_name'] . '-transients-metaboxes', $this->parent->transients ); |
|
| 1458 | - } |
|
| 1459 | - } |
|
| 1460 | - |
|
| 1461 | - if ( isset( $_POST['post_type'] ) ) { |
|
| 1462 | - $check = $this->post_type_fields[ sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) ]; |
|
| 1463 | - } |
|
| 1464 | - |
|
| 1465 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 1466 | - $to_save = apply_filters( 'redux/metaboxes/save', $to_save, $to_compare, $this->sections ); |
|
| 1467 | - |
|
| 1468 | - foreach ( $to_save as $key => $value ) { |
|
| 1469 | - $prev_value = $this->meta[ $post_id ][ $key ] ?? ''; |
|
| 1470 | - |
|
| 1471 | - if ( isset( $check[ $key ] ) ) { |
|
| 1472 | - unset( $check[ $key ] ); |
|
| 1473 | - } |
|
| 1474 | - |
|
| 1475 | - update_post_meta( $post_id, $key, $value, $prev_value ); |
|
| 1476 | - } |
|
| 1477 | - |
|
| 1478 | - foreach ( $to_delete as $key => $value ) { |
|
| 1479 | - if ( isset( $check[ $key ] ) ) { |
|
| 1480 | - unset( $check[ $key ] ); |
|
| 1481 | - } |
|
| 1482 | - |
|
| 1483 | - $prev_value = $this->meta[ $post_id ][ $key ] ?? ''; |
|
| 1484 | - delete_post_meta( $post_id, $key, $prev_value ); |
|
| 1485 | - } |
|
| 1486 | - |
|
| 1487 | - if ( ! empty( $check ) ) { |
|
| 1488 | - foreach ( $check as $key => $value ) { |
|
| 1489 | - delete_post_meta( $post_id, $key ); |
|
| 1490 | - } |
|
| 1491 | - } |
|
| 1492 | - |
|
| 1493 | - return $post_id; |
|
| 1494 | - } |
|
| 1495 | - |
|
| 1496 | - /** |
|
| 1497 | - * Some functions, like the term recount, require the visibility to be set prior. Lets save that here. |
|
| 1498 | - * |
|
| 1499 | - * @access public |
|
| 1500 | - * |
|
| 1501 | - * @param mixed $post_id Post ID. |
|
| 1502 | - * |
|
| 1503 | - * @return void |
|
| 1504 | - */ |
|
| 1505 | - public function pre_post_update( $post_id ) { |
|
| 1506 | - if ( isset( $_POST['_visibility'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 1507 | - update_post_meta( $post_id, '_visibility', sanitize_text_field( wp_unslash( $_POST['_visibility'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 1508 | - } |
|
| 1509 | - |
|
| 1510 | - if ( isset( $_POST['_stock_status'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 1511 | - update_post_meta( $post_id, '_stock_status', sanitize_text_field( wp_unslash( $_POST['_stock_status'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 1512 | - } |
|
| 1513 | - } |
|
| 1514 | - |
|
| 1515 | - /** |
|
| 1516 | - * Show any stored error messages. |
|
| 1517 | - * |
|
| 1518 | - * @access public |
|
| 1519 | - * @return void |
|
| 1520 | - */ |
|
| 1521 | - public function meta_boxes_show_errors() { |
|
| 1522 | - if ( isset( $this->notices['errors'] ) && ! empty( $this->notices['errors'] ) ) { |
|
| 1523 | - echo '<div id="redux_metaboxes_errors" class="error fade">'; |
|
| 1524 | - echo '<p><strong><span></span> ' . count( $this->notices['errors'] ) . ' ' . esc_html__( 'error(s) were found!', 'redux-framework' ) . '</strong></p>'; |
|
| 1525 | - echo '</div>'; |
|
| 1526 | - } |
|
| 1527 | - |
|
| 1528 | - if ( isset( $this->notices['warnings'] ) && ! empty( $this->notices['warnings'] ) ) { |
|
| 1529 | - echo '<div id="redux_metaboxes_warnings" class="error fade" style="border-left-color: #E8E20C;">'; |
|
| 1530 | - echo '<p><strong><span></span> ' . count( $this->notices['warnings'] ) . ' ' . esc_html__( 'warnings(s) were found!', 'redux-framework' ) . '</strong></p>'; |
|
| 1531 | - echo '</div>'; |
|
| 1532 | - } |
|
| 1533 | - } |
|
| 1534 | - } |
|
| 1535 | - |
|
| 1536 | - class_alias( Redux_Extension_Metaboxes::class, 'ReduxFramework_Extension_metaboxes' ); |
|
| 1329 | + } |
|
| 1330 | + |
|
| 1331 | + /** |
|
| 1332 | + * Save meta boxes |
|
| 1333 | + * Runs when a post is saved and does an action which the write panel save scripts can hook into. |
|
| 1334 | + * |
|
| 1335 | + * @access public |
|
| 1336 | + * |
|
| 1337 | + * @param mixed $post_id Post ID. |
|
| 1338 | + * @param mixed $post Post. |
|
| 1339 | + * |
|
| 1340 | + * @return mixed |
|
| 1341 | + */ |
|
| 1342 | + public function meta_boxes_save( $post_id, $post ) { |
|
| 1343 | + if ( isset( $_POST['vc_inline'] ) && sanitize_text_field( wp_unslash( $_POST['vc_inline'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 1344 | + return $post_id; |
|
| 1345 | + } |
|
| 1346 | + |
|
| 1347 | + if ( isset( $_POST['post_ID'] ) && strval( $post_id ) !== $_POST['post_ID'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 1348 | + return $post_id; |
|
| 1349 | + } |
|
| 1350 | + |
|
| 1351 | + // Check if our nonce is set. |
|
| 1352 | + if ( ! isset( $_POST['redux_metaboxes_meta_nonce'] ) || ! isset( $_POST[ $this->parent->args['opt_name'] ] ) ) { |
|
| 1353 | + return $post_id; |
|
| 1354 | + } |
|
| 1355 | + |
|
| 1356 | + // Verify that the nonce is valid. |
|
| 1357 | + // Validate fields (if needed). |
|
| 1358 | + if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['redux_metaboxes_meta_nonce'] ) ), 'redux_metaboxes_meta_nonce' ) ) { |
|
| 1359 | + return $post_id; |
|
| 1360 | + } |
|
| 1361 | + |
|
| 1362 | + // If this is an autosave, our form has not been submitted, so we don't want to do anything. |
|
| 1363 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
| 1364 | + return $post_id; |
|
| 1365 | + } |
|
| 1366 | + |
|
| 1367 | + // Check the user's permissions, even allowing custom capabilities. |
|
| 1368 | + $obj = get_post_type_object( $post->post_type ); |
|
| 1369 | + if ( ! current_user_can( $obj->cap->edit_post, $post_id ) ) { |
|
| 1370 | + return $post_id; |
|
| 1371 | + } |
|
| 1372 | + |
|
| 1373 | + // Import. |
|
| 1374 | + if ( isset( $_POST[ $this->parent->args['opt_name'] ]['import_code'] ) && ! empty( $_POST[ $this->parent->args['opt_name'] ]['import_code'] ) ) { |
|
| 1375 | + $import = json_decode( sanitize_text_field( wp_unslash( $_POST[ $this->parent->args['opt_name'] ]['import_code'] ) ), true ); |
|
| 1376 | + unset( $_POST[ $this->parent->args['opt_name'] ]['import_code'] ); |
|
| 1377 | + |
|
| 1378 | + foreach ( Redux_Helpers::sanitize_array( wp_unslash( $_POST[ $this->parent->args['opt_name'] ] ) ) as $key => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
|
| 1379 | + if ( ! isset( $import[ $key ] ) ) { |
|
| 1380 | + $import[ $key ] = $value; |
|
| 1381 | + } |
|
| 1382 | + } |
|
| 1383 | + |
|
| 1384 | + $_POST[ $this->parent->args['opt_name'] ] = $import; |
|
| 1385 | + } |
|
| 1386 | + |
|
| 1387 | + $to_save = array(); |
|
| 1388 | + $to_compare = array(); |
|
| 1389 | + $to_delete = array(); |
|
| 1390 | + $dont_save = true; |
|
| 1391 | + |
|
| 1392 | + if ( isset( $this->parent->args['metaboxes_save_defaults'] ) && $this->parent->args['metaboxes_save_defaults'] ) { |
|
| 1393 | + $dont_save = false; |
|
| 1394 | + } |
|
| 1395 | + |
|
| 1396 | + foreach ( Redux_Helpers::sanitize_array( wp_unslash( $_POST[ $this->parent->args['opt_name'] ] ) ) as $key => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
|
| 1397 | + |
|
| 1398 | + // Have to remove the escaping for array comparison. |
|
| 1399 | + if ( is_array( $value ) ) { |
|
| 1400 | + foreach ( $value as $k => $v ) { |
|
| 1401 | + if ( ! is_array( $v ) ) { |
|
| 1402 | + $value[ $k ] = wp_unslash( $v ); |
|
| 1403 | + } |
|
| 1404 | + } |
|
| 1405 | + } |
|
| 1406 | + |
|
| 1407 | + $save = true; |
|
| 1408 | + |
|
| 1409 | + // parent_options. |
|
| 1410 | + if ( ! $dont_save && isset( $this->options_defaults[ $key ] ) && $value === $this->options_defaults[ $key ] ) { |
|
| 1411 | + $save = false; |
|
| 1412 | + } |
|
| 1413 | + |
|
| 1414 | + if ( $save && isset( $this->parent_options[ $key ] ) && $this->parent_options[ $key ] !== $value ) { |
|
| 1415 | + $save = false; |
|
| 1416 | + } |
|
| 1417 | + |
|
| 1418 | + if ( $save ) { |
|
| 1419 | + $to_save[ $key ] = $value; |
|
| 1420 | + $to_compare[ $key ] = $this->parent->options[ $key ] ?? ''; |
|
| 1421 | + } else { |
|
| 1422 | + $to_delete[ $key ] = $value; |
|
| 1423 | + } |
|
| 1424 | + } |
|
| 1425 | + |
|
| 1426 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 1427 | + $to_save = apply_filters( 'redux/metaboxes/save/before_validate', $to_save, $to_compare, $this->sections ); |
|
| 1428 | + |
|
| 1429 | + $validate = $this->parent->validate_class->validate( $to_save, $to_compare, $this->sections ); |
|
| 1430 | + |
|
| 1431 | + // Validate fields (if needed). |
|
| 1432 | + foreach ( $to_save as $key => $value ) { |
|
| 1433 | + if ( isset( $validate[ $key ] ) && $value !== $validate[ $key ] ) { |
|
| 1434 | + if ( isset( $this->parent->options[ $key ] ) && $validate[ $key ] === $this->parent->options[ $key ] ) { |
|
| 1435 | + unset( $to_save[ $key ] ); |
|
| 1436 | + } else { |
|
| 1437 | + $to_save[ $key ] = $validate[ $key ]; |
|
| 1438 | + } |
|
| 1439 | + } |
|
| 1440 | + } |
|
| 1441 | + |
|
| 1442 | + if ( ! empty( $this->parent->errors ) || ! empty( $this->parent->warnings ) ) { |
|
| 1443 | + $this->parent->transients['notices'] = ( isset( $this->parent->transients['notices'] ) && is_array( $this->parent->transients['notices'] ) ) ? $this->parent->transients['notices'] : array(); |
|
| 1444 | + |
|
| 1445 | + if ( ! isset( $this->parent->transients['notices']['errors'] ) || $this->parent->transients['notices']['errors'] !== $this->parent->errors ) { |
|
| 1446 | + $this->parent->transients['notices']['errors'] = $this->parent->errors; |
|
| 1447 | + $update_transients = true; |
|
| 1448 | + } |
|
| 1449 | + |
|
| 1450 | + if ( ! isset( $this->parent->transients['notices']['warnings'] ) || $this->parent->transients['notices']['warnings'] !== $this->parent->warnings ) { |
|
| 1451 | + $this->parent->transients['notices']['warnings'] = $this->parent->warnings; |
|
| 1452 | + $update_transients = true; |
|
| 1453 | + } |
|
| 1454 | + |
|
| 1455 | + if ( isset( $update_transients ) ) { |
|
| 1456 | + $this->parent->transients['notices']['override'] = 1; |
|
| 1457 | + set_transient( $this->parent->args['opt_name'] . '-transients-metaboxes', $this->parent->transients ); |
|
| 1458 | + } |
|
| 1459 | + } |
|
| 1460 | + |
|
| 1461 | + if ( isset( $_POST['post_type'] ) ) { |
|
| 1462 | + $check = $this->post_type_fields[ sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) ]; |
|
| 1463 | + } |
|
| 1464 | + |
|
| 1465 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 1466 | + $to_save = apply_filters( 'redux/metaboxes/save', $to_save, $to_compare, $this->sections ); |
|
| 1467 | + |
|
| 1468 | + foreach ( $to_save as $key => $value ) { |
|
| 1469 | + $prev_value = $this->meta[ $post_id ][ $key ] ?? ''; |
|
| 1470 | + |
|
| 1471 | + if ( isset( $check[ $key ] ) ) { |
|
| 1472 | + unset( $check[ $key ] ); |
|
| 1473 | + } |
|
| 1474 | + |
|
| 1475 | + update_post_meta( $post_id, $key, $value, $prev_value ); |
|
| 1476 | + } |
|
| 1477 | + |
|
| 1478 | + foreach ( $to_delete as $key => $value ) { |
|
| 1479 | + if ( isset( $check[ $key ] ) ) { |
|
| 1480 | + unset( $check[ $key ] ); |
|
| 1481 | + } |
|
| 1482 | + |
|
| 1483 | + $prev_value = $this->meta[ $post_id ][ $key ] ?? ''; |
|
| 1484 | + delete_post_meta( $post_id, $key, $prev_value ); |
|
| 1485 | + } |
|
| 1486 | + |
|
| 1487 | + if ( ! empty( $check ) ) { |
|
| 1488 | + foreach ( $check as $key => $value ) { |
|
| 1489 | + delete_post_meta( $post_id, $key ); |
|
| 1490 | + } |
|
| 1491 | + } |
|
| 1492 | + |
|
| 1493 | + return $post_id; |
|
| 1494 | + } |
|
| 1495 | + |
|
| 1496 | + /** |
|
| 1497 | + * Some functions, like the term recount, require the visibility to be set prior. Lets save that here. |
|
| 1498 | + * |
|
| 1499 | + * @access public |
|
| 1500 | + * |
|
| 1501 | + * @param mixed $post_id Post ID. |
|
| 1502 | + * |
|
| 1503 | + * @return void |
|
| 1504 | + */ |
|
| 1505 | + public function pre_post_update( $post_id ) { |
|
| 1506 | + if ( isset( $_POST['_visibility'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 1507 | + update_post_meta( $post_id, '_visibility', sanitize_text_field( wp_unslash( $_POST['_visibility'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 1508 | + } |
|
| 1509 | + |
|
| 1510 | + if ( isset( $_POST['_stock_status'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
|
| 1511 | + update_post_meta( $post_id, '_stock_status', sanitize_text_field( wp_unslash( $_POST['_stock_status'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification |
|
| 1512 | + } |
|
| 1513 | + } |
|
| 1514 | + |
|
| 1515 | + /** |
|
| 1516 | + * Show any stored error messages. |
|
| 1517 | + * |
|
| 1518 | + * @access public |
|
| 1519 | + * @return void |
|
| 1520 | + */ |
|
| 1521 | + public function meta_boxes_show_errors() { |
|
| 1522 | + if ( isset( $this->notices['errors'] ) && ! empty( $this->notices['errors'] ) ) { |
|
| 1523 | + echo '<div id="redux_metaboxes_errors" class="error fade">'; |
|
| 1524 | + echo '<p><strong><span></span> ' . count( $this->notices['errors'] ) . ' ' . esc_html__( 'error(s) were found!', 'redux-framework' ) . '</strong></p>'; |
|
| 1525 | + echo '</div>'; |
|
| 1526 | + } |
|
| 1527 | + |
|
| 1528 | + if ( isset( $this->notices['warnings'] ) && ! empty( $this->notices['warnings'] ) ) { |
|
| 1529 | + echo '<div id="redux_metaboxes_warnings" class="error fade" style="border-left-color: #E8E20C;">'; |
|
| 1530 | + echo '<p><strong><span></span> ' . count( $this->notices['warnings'] ) . ' ' . esc_html__( 'warnings(s) were found!', 'redux-framework' ) . '</strong></p>'; |
|
| 1531 | + echo '</div>'; |
|
| 1532 | + } |
|
| 1533 | + } |
|
| 1534 | + } |
|
| 1535 | + |
|
| 1536 | + class_alias( Redux_Extension_Metaboxes::class, 'ReduxFramework_Extension_metaboxes' ); |
|
| 1537 | 1537 | } |
@@ -8,49 +8,49 @@ |
||
| 8 | 8 | defined( 'ABSPATH' ) || exit; |
| 9 | 9 | |
| 10 | 10 | if ( ! function_exists( 'redux_metaboxes_loop_start' ) ) { |
| 11 | - /** |
|
| 12 | - * Start loop. |
|
| 13 | - * |
|
| 14 | - * @param string $opt_name Panel opt_name. |
|
| 15 | - * @param array $the_post Post object. |
|
| 16 | - */ |
|
| 17 | - function redux_metaboxes_loop_start( string $opt_name, array $the_post = array() ) { |
|
| 18 | - $redux = ReduxFrameworkInstances::get_instance( $opt_name ); |
|
| 19 | - $metaboxes = $redux->extensions['metaboxes']; |
|
| 11 | + /** |
|
| 12 | + * Start loop. |
|
| 13 | + * |
|
| 14 | + * @param string $opt_name Panel opt_name. |
|
| 15 | + * @param array $the_post Post object. |
|
| 16 | + */ |
|
| 17 | + function redux_metaboxes_loop_start( string $opt_name, array $the_post = array() ) { |
|
| 18 | + $redux = ReduxFrameworkInstances::get_instance( $opt_name ); |
|
| 19 | + $metaboxes = $redux->extensions['metaboxes']; |
|
| 20 | 20 | |
| 21 | - $metaboxes->loop_start( $the_post ); |
|
| 22 | - } |
|
| 21 | + $metaboxes->loop_start( $the_post ); |
|
| 22 | + } |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | if ( ! function_exists( 'redux_metaboxes_loop_end' ) ) { |
| 26 | - /** |
|
| 27 | - * End loop. |
|
| 28 | - * |
|
| 29 | - * @param string $opt_name Panel opt_name. |
|
| 30 | - * @param array $the_post Deprecated. |
|
| 31 | - */ |
|
| 32 | - function redux_metaboxes_loop_end( string $opt_name, array $the_post = array() ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
|
| 33 | - _deprecated_argument( __FUNCTION__, '4.5', '$the_post argument has been deprecated and will be removed in a future version. Please update your code accordingly!' ); |
|
| 26 | + /** |
|
| 27 | + * End loop. |
|
| 28 | + * |
|
| 29 | + * @param string $opt_name Panel opt_name. |
|
| 30 | + * @param array $the_post Deprecated. |
|
| 31 | + */ |
|
| 32 | + function redux_metaboxes_loop_end( string $opt_name, array $the_post = array() ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
|
| 33 | + _deprecated_argument( __FUNCTION__, '4.5', '$the_post argument has been deprecated and will be removed in a future version. Please update your code accordingly!' ); |
|
| 34 | 34 | |
| 35 | - $redux = ReduxFrameworkInstances::get_instance( $opt_name ); |
|
| 36 | - $metaboxes = $redux->extensions['metaboxes']; |
|
| 35 | + $redux = ReduxFrameworkInstances::get_instance( $opt_name ); |
|
| 36 | + $metaboxes = $redux->extensions['metaboxes']; |
|
| 37 | 37 | |
| 38 | - $metaboxes->loop_end(); |
|
| 39 | - } |
|
| 38 | + $metaboxes->loop_end(); |
|
| 39 | + } |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | if ( ! function_exists( 'redux_post_meta' ) ) { |
| 43 | - /** |
|
| 44 | - * Retrieve post meta values/settings. |
|
| 45 | - * |
|
| 46 | - * @param string $opt_name Panel opt_name. |
|
| 47 | - * @param mixed $the_post Post ID. |
|
| 48 | - * @param string $meta_key Meta key. |
|
| 49 | - * @param mixed $def_val Default value. |
|
| 50 | - * |
|
| 51 | - * @return string|void |
|
| 52 | - */ |
|
| 53 | - function redux_post_meta( string $opt_name = '', $the_post = array(), string $meta_key = '', $def_val = '' ) { |
|
| 54 | - return Redux::get_post_meta( $opt_name, $the_post, $meta_key, $def_val ); |
|
| 55 | - } |
|
| 43 | + /** |
|
| 44 | + * Retrieve post meta values/settings. |
|
| 45 | + * |
|
| 46 | + * @param string $opt_name Panel opt_name. |
|
| 47 | + * @param mixed $the_post Post ID. |
|
| 48 | + * @param string $meta_key Meta key. |
|
| 49 | + * @param mixed $def_val Default value. |
|
| 50 | + * |
|
| 51 | + * @return string|void |
|
| 52 | + */ |
|
| 53 | + function redux_post_meta( string $opt_name = '', $the_post = array(), string $meta_key = '', $def_val = '' ) { |
|
| 54 | + return Redux::get_post_meta( $opt_name, $the_post, $meta_key, $def_val ); |
|
| 55 | + } |
|
| 56 | 56 | } |