class-redux-options-defaults.php (4997B)
1 <?php 2 /** 3 * Redux Option Defaults Class 4 * 5 * @class Redux_Options_Defaults 6 * @version 4.0.0 7 * @package Redux Framework/Classes 8 */ 9 10 defined( 'ABSPATH' ) || exit; 11 12 if ( ! class_exists( 'Redux_Options_Defaults', false ) ) { 13 14 /** 15 * Class Redux_Options_Defaults 16 */ 17 class Redux_Options_Defaults { 18 19 /** 20 * Default options. 21 * 22 * @var array 23 */ 24 public $options_defaults = array(); 25 26 /** 27 * Field array. 28 * 29 * @var array 30 */ 31 public $fields = array(); 32 33 /** 34 * Creates default options array. 35 * 36 * @param string $opt_name Panel opt_name. 37 * @param array $sections Panel sections array. 38 * @param null $wp_data_class WordPress data class. 39 * 40 * @return array|string 41 */ 42 public function default_values( string $opt_name = '', array $sections = array(), $wp_data_class = null ) { 43 // We want it to be clean each time this is run. 44 $this->options_defaults = array(); 45 46 // Check to make sure we're not in the select2 action, we don't want to fetch any there. 47 if ( isset( $_REQUEST['action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended 48 $action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended 49 50 if ( Redux_Functions_Ex::string_ends_with( $action, '_select2' ) && Redux_Functions_Ex::string_starts_with( $action, 'redux_' ) ) { 51 return ''; 52 } 53 } 54 55 if ( ! is_null( $sections ) && ! empty( $sections ) ) { 56 57 // Fill the cache. 58 foreach ( $sections as $sk => $section ) { 59 if ( ! isset( $section['id'] ) ) { 60 if ( ! is_numeric( $sk ) || ! isset( $section['title'] ) ) { 61 $section['id'] = $sk; 62 } else { 63 $section['id'] = sanitize_title( $section['title'], $sk ); 64 } 65 66 $sections[ $sk ] = $section; 67 } 68 if ( isset( $section['fields'] ) ) { 69 foreach ( $section['fields'] as $k => $field ) { 70 if ( empty( $field['id'] ) && empty( $field['type'] ) ) { 71 continue; 72 } 73 74 $this->field_default_values( $opt_name, $field, $wp_data_class ); 75 } 76 } 77 } 78 } 79 80 return $this->options_defaults; 81 } 82 83 /** 84 * Field default values. 85 * 86 * @param string $opt_name Panel opt_name. 87 * @param array $field Fiel array. 88 * @param object $wp_data_class WordPress data class. 89 */ 90 public function field_default_values( string $opt_name = '', array $field = array(), $wp_data_class = null ) { 91 if ( null === $wp_data_class && class_exists( 'Redux_WordPress_Data' ) && ! ( 'select' === $field['type'] && isset( $field['ajax'] ) && $field['ajax'] ) ) { 92 $wp_data_class = new Redux_WordPress_Data( $opt_name ); 93 } 94 95 // Detect what field types are being used. 96 if ( ! isset( $this->fields[ $field['type'] ][ $field['id'] ] ) ) { 97 $this->fields[ $field['type'] ][ $field['id'] ] = 1; 98 } else { 99 $this->fields[ $field['type'] ] = array( $field['id'] => 1 ); 100 } 101 102 if ( isset( $field['default'] ) ) { 103 // phpcs:ignore WordPress.NamingConventions.ValidHookName 104 $this->options_defaults[ $field['id'] ] = apply_filters( "redux/$opt_name/field/{$field['type']}/defaults", $field['default'], $field ); 105 } elseif ( ( 'ace_editor' !== $field['type'] ) && ! ( 'select' === $field['type'] && ! empty( $field['ajax'] ) ) ) { 106 if ( isset( $field['data'] ) ) { 107 if ( ! isset( $field['args'] ) ) { 108 $field['args'] = array(); 109 } 110 if ( is_array( $field['data'] ) && ! empty( $field['data'] ) ) { 111 foreach ( $field['data'] as $key => $data ) { 112 if ( ! empty( $data ) ) { 113 if ( ! isset( $field['args'][ $key ] ) ) { 114 $field['args'][ $key ] = array(); 115 } 116 if ( null !== $wp_data_class ) { 117 $field['options'][ $key ] = $wp_data_class->get( $data, $field['args'][ $key ], $opt_name ); 118 } 119 } 120 } 121 } elseif ( null !== $wp_data_class ) { 122 $field['options'] = $wp_data_class->get( $field['data'], $field['args'], $opt_name ); 123 } 124 125 if ( 'sorter' === $field['type'] && isset( $field['data'] ) && ! empty( $field['data'] ) && is_array( $field['data'] ) ) { 126 if ( ! isset( $field['args'] ) ) { 127 $field['args'] = array(); 128 } 129 foreach ( $field['data'] as $key => $data ) { 130 if ( ! isset( $field['args'][ $key ] ) ) { 131 $field['args'][ $key ] = array(); 132 } 133 if ( null !== $wp_data_class ) { 134 $field['options'][ $key ] = $wp_data_class->get( $data, $field['args'][ $key ], $opt_name ); 135 } 136 } 137 } 138 139 if ( isset( $field['options'] ) ) { 140 if ( 'sortable' === $field['type'] ) { 141 $this->options_defaults[ $field['id'] ] = array(); 142 } elseif ( 'image_select' === $field['type'] ) { 143 $this->options_defaults[ $field['id'] ] = ''; 144 } elseif ( 'select' === $field['type'] ) { 145 $this->options_defaults[ $field['id'] ] = ''; 146 } else { 147 $this->options_defaults[ $field['id'] ] = $field['options']; 148 } 149 } 150 } 151 } 152 } 153 } 154 }