@@ -11,179 +11,179 @@ |
||
| 11 | 11 | |
| 12 | 12 | if ( ! class_exists( 'Redux_Image_Filters' ) ) { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Class Redux_Image_Filters |
|
| 16 | - */ |
|
| 17 | - class Redux_Image_Filters { |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Render preview. |
|
| 21 | - * |
|
| 22 | - * @param array $data Data. |
|
| 23 | - * |
|
| 24 | - * @return string |
|
| 25 | - */ |
|
| 26 | - public static function render( array $data ): string { |
|
| 27 | - extract( $data ); // phpcs:ignore WordPress.PHP.DontExtract |
|
| 28 | - |
|
| 29 | - $output = ''; |
|
| 30 | - |
|
| 31 | - $filter_arr = array( |
|
| 32 | - 'grayscale', |
|
| 33 | - 'blur', |
|
| 34 | - 'sepia', |
|
| 35 | - 'saturate', |
|
| 36 | - 'opacity', |
|
| 37 | - 'brightness', |
|
| 38 | - 'contrast', |
|
| 39 | - 'hue-rotate', |
|
| 40 | - 'invert', |
|
| 41 | - ); |
|
| 42 | - |
|
| 43 | - // Make an array of in use filters. |
|
| 44 | - $in_use_filters = array(); |
|
| 45 | - |
|
| 46 | - foreach ( $filter_arr as $filter ) { |
|
| 47 | - if ( $field['filter'][ $filter ] ) { |
|
| 48 | - $in_use_filters[] = $filter; |
|
| 49 | - } |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - $filters = rawurlencode( wp_json_encode( $in_use_filters ) ); |
|
| 53 | - |
|
| 54 | - $output .= '<div class="redux-' . $mode . '-filter-container" data-filters="' . $filters . '">'; |
|
| 55 | - $output .= '<div class="container-label">' . esc_html__( 'Filters', 'redux-framework' ) . '</div>'; |
|
| 56 | - |
|
| 57 | - foreach ( $in_use_filters as $filter ) { |
|
| 58 | - $step = 1; |
|
| 59 | - $unit = self::get_filter_unit( $filter ); |
|
| 60 | - |
|
| 61 | - if ( 'grayscale' === $filter || 'invert' === $filter ) { |
|
| 62 | - $min = 0; |
|
| 63 | - $max = 100; |
|
| 64 | - } elseif ( 'blur' === $filter ) { |
|
| 65 | - $min = 0; |
|
| 66 | - $max = 30; |
|
| 67 | - } elseif ( 'sepia' === $filter || 'saturate' === $filter || 'opacity' === $filter ) { |
|
| 68 | - $min = 0; |
|
| 69 | - $max = 1; |
|
| 70 | - $step = .01; |
|
| 71 | - } elseif ( 'brightness' === $filter || 'contrast' === $filter ) { |
|
| 72 | - $min = 0; |
|
| 73 | - $max = 200; |
|
| 74 | - } elseif ( 'hue-rotate' === $filter ) { |
|
| 75 | - $min = 0; |
|
| 76 | - $max = 360; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $disabled = 'filters-disabled'; |
|
| 80 | - if ( $value['filter'][ $filter ]['checked'] ) { |
|
| 81 | - $disabled = ''; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - $output .= '<div class="filter filter-' . $filter . '">'; |
|
| 85 | - $output .= '<label for="' . esc_attr( $field['id'] ) . '-' . $filter . '" class="' . $disabled . '">'; |
|
| 86 | - $output .= '<input type="checkbox" id="' . esc_attr( $field['id'] ) . '-' . $filter . '" class="checkbox" value="1"' . checked( $value['filter'][ $filter ]['checked'], '1', false ) . '/>'; |
|
| 87 | - $output .= '<input type="hidden" data-val="1" value="' . esc_attr( $value['filter'][ $filter ]['checked'] ) . '" class="checkbox-check" name="' . esc_attr( $field['name'] . $field['name_suffix'] ) . '[filter][' . $filter . '][checked]"/>'; |
|
| 88 | - $output .= ucfirst( $filter ) . ': '; |
|
| 89 | - $output .= '<span class="filter-value"><strong>' . $value['filter'][ $filter ]['value'] . $unit . '</strong></span>'; |
|
| 90 | - $output .= '</label>'; |
|
| 91 | - |
|
| 92 | - $output .= '<div '; |
|
| 93 | - $output .= 'class="redux-' . $mode . '-slider redux-' . $mode . '-filter redux-filter redux-filter-' . $filter . esc_attr( $field['class'] ) . '"'; |
|
| 94 | - $output .= 'id="' . esc_attr( $field['id'] ) . '"'; |
|
| 95 | - $output .= 'data-id="' . esc_attr( $field['id'] . '-' . $filter ) . '"'; |
|
| 96 | - $output .= 'data-min="' . $min . '"'; |
|
| 97 | - $output .= 'data-max="' . $max . '"'; |
|
| 98 | - $output .= 'data-step="' . $step . '"'; |
|
| 99 | - $output .= 'data-rtl="' . esc_attr( is_rtl() ) . '"'; |
|
| 100 | - $output .= 'data-unit="' . $unit . '"'; |
|
| 101 | - $output .= 'data-default = "' . esc_attr( $value['filter'][ $filter ]['value'] ) . '" '; |
|
| 102 | - $output .= disabled( filter_var( $value['filter'][ $filter ]['checked'], FILTER_VALIDATE_BOOLEAN ), false, false ); |
|
| 103 | - $output .= '>'; |
|
| 104 | - $output .= '</div>'; |
|
| 105 | - |
|
| 106 | - if ( '°' === $unit ) { |
|
| 107 | - $unit = 'deg'; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $output .= '<input '; |
|
| 111 | - $output .= 'type="hidden"'; |
|
| 112 | - $output .= 'id="redux-slider-value-' . esc_attr( $field['id'] ) . '-' . $filter . '"'; |
|
| 113 | - $output .= 'class="' . $mode . '-filter-' . $filter . '"'; |
|
| 114 | - $output .= 'name="' . esc_attr( $field['name'] . $field['name_suffix'] ) . '[filter][' . $filter . '][value]"'; |
|
| 115 | - $output .= 'value="' . esc_attr( $value['filter'][ $filter ]['value'] ) . '"'; |
|
| 116 | - $output .= 'data-id="' . esc_attr( $field['id'] ) . '"'; |
|
| 117 | - $output .= 'data-unit="' . $unit . '"'; |
|
| 118 | - $output .= '/>'; |
|
| 119 | - $output .= '</div>'; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - $output .= '</div>'; |
|
| 123 | - |
|
| 124 | - return $output; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Get filter unit. |
|
| 129 | - * |
|
| 130 | - * @param string $filter Filter type. |
|
| 131 | - * |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public static function get_filter_unit( string $filter ): string { |
|
| 135 | - if ( 'grayscale' === $filter || 'invert' === $filter || 'brightness' === $filter || 'contrast' === $filter ) { |
|
| 136 | - return '%'; |
|
| 137 | - } elseif ( 'blur' === $filter ) { |
|
| 138 | - return 'px'; |
|
| 139 | - } elseif ( 'hue-rotate' === $filter ) { |
|
| 140 | - return '°'; |
|
| 141 | - } else { |
|
| 142 | - return ''; |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Enqueue support files. |
|
| 148 | - * |
|
| 149 | - * @param bool $filters_enabled Filtered enabled bit. |
|
| 150 | - */ |
|
| 151 | - public static function enqueue( bool $filters_enabled ) { |
|
| 152 | - $min = Redux_Functions::is_min(); |
|
| 153 | - |
|
| 154 | - if ( $filters_enabled ) { |
|
| 155 | - if ( ! wp_style_is( 'redux-nouislider' ) ) { |
|
| 156 | - wp_enqueue_style( |
|
| 157 | - 'redux-nouislider', |
|
| 158 | - Redux_Core::$url . 'assets/css/vendor/nouislider/redux.jquery.nouislider.css', |
|
| 159 | - array(), |
|
| 160 | - '5.0.0' |
|
| 161 | - ); |
|
| 162 | - |
|
| 163 | - wp_enqueue_script( |
|
| 164 | - 'redux-nouislider', |
|
| 165 | - Redux_Core::$url . 'assets/js/vendor/nouislider/redux.jquery.nouislider' . $min . '.js', |
|
| 166 | - array( 'jquery' ), |
|
| 167 | - '5.0.0', |
|
| 168 | - true |
|
| 169 | - ); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - wp_enqueue_script( |
|
| 173 | - 'redux-image-filters', |
|
| 174 | - Redux_Core::$url . 'inc/lib/image-filters/image-filters' . $min . '.js', |
|
| 175 | - array( 'jquery' ), |
|
| 176 | - Redux_Core::$version, |
|
| 177 | - true |
|
| 178 | - ); |
|
| 179 | - |
|
| 180 | - wp_enqueue_style( |
|
| 181 | - 'redux-image-filters', |
|
| 182 | - Redux_Core::$url . 'inc/lib/image-filters/image-filters.css', |
|
| 183 | - array(), |
|
| 184 | - Redux_Core::$version |
|
| 185 | - ); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - } |
|
| 14 | + /** |
|
| 15 | + * Class Redux_Image_Filters |
|
| 16 | + */ |
|
| 17 | + class Redux_Image_Filters { |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Render preview. |
|
| 21 | + * |
|
| 22 | + * @param array $data Data. |
|
| 23 | + * |
|
| 24 | + * @return string |
|
| 25 | + */ |
|
| 26 | + public static function render( array $data ): string { |
|
| 27 | + extract( $data ); // phpcs:ignore WordPress.PHP.DontExtract |
|
| 28 | + |
|
| 29 | + $output = ''; |
|
| 30 | + |
|
| 31 | + $filter_arr = array( |
|
| 32 | + 'grayscale', |
|
| 33 | + 'blur', |
|
| 34 | + 'sepia', |
|
| 35 | + 'saturate', |
|
| 36 | + 'opacity', |
|
| 37 | + 'brightness', |
|
| 38 | + 'contrast', |
|
| 39 | + 'hue-rotate', |
|
| 40 | + 'invert', |
|
| 41 | + ); |
|
| 42 | + |
|
| 43 | + // Make an array of in use filters. |
|
| 44 | + $in_use_filters = array(); |
|
| 45 | + |
|
| 46 | + foreach ( $filter_arr as $filter ) { |
|
| 47 | + if ( $field['filter'][ $filter ] ) { |
|
| 48 | + $in_use_filters[] = $filter; |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + $filters = rawurlencode( wp_json_encode( $in_use_filters ) ); |
|
| 53 | + |
|
| 54 | + $output .= '<div class="redux-' . $mode . '-filter-container" data-filters="' . $filters . '">'; |
|
| 55 | + $output .= '<div class="container-label">' . esc_html__( 'Filters', 'redux-framework' ) . '</div>'; |
|
| 56 | + |
|
| 57 | + foreach ( $in_use_filters as $filter ) { |
|
| 58 | + $step = 1; |
|
| 59 | + $unit = self::get_filter_unit( $filter ); |
|
| 60 | + |
|
| 61 | + if ( 'grayscale' === $filter || 'invert' === $filter ) { |
|
| 62 | + $min = 0; |
|
| 63 | + $max = 100; |
|
| 64 | + } elseif ( 'blur' === $filter ) { |
|
| 65 | + $min = 0; |
|
| 66 | + $max = 30; |
|
| 67 | + } elseif ( 'sepia' === $filter || 'saturate' === $filter || 'opacity' === $filter ) { |
|
| 68 | + $min = 0; |
|
| 69 | + $max = 1; |
|
| 70 | + $step = .01; |
|
| 71 | + } elseif ( 'brightness' === $filter || 'contrast' === $filter ) { |
|
| 72 | + $min = 0; |
|
| 73 | + $max = 200; |
|
| 74 | + } elseif ( 'hue-rotate' === $filter ) { |
|
| 75 | + $min = 0; |
|
| 76 | + $max = 360; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $disabled = 'filters-disabled'; |
|
| 80 | + if ( $value['filter'][ $filter ]['checked'] ) { |
|
| 81 | + $disabled = ''; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + $output .= '<div class="filter filter-' . $filter . '">'; |
|
| 85 | + $output .= '<label for="' . esc_attr( $field['id'] ) . '-' . $filter . '" class="' . $disabled . '">'; |
|
| 86 | + $output .= '<input type="checkbox" id="' . esc_attr( $field['id'] ) . '-' . $filter . '" class="checkbox" value="1"' . checked( $value['filter'][ $filter ]['checked'], '1', false ) . '/>'; |
|
| 87 | + $output .= '<input type="hidden" data-val="1" value="' . esc_attr( $value['filter'][ $filter ]['checked'] ) . '" class="checkbox-check" name="' . esc_attr( $field['name'] . $field['name_suffix'] ) . '[filter][' . $filter . '][checked]"/>'; |
|
| 88 | + $output .= ucfirst( $filter ) . ': '; |
|
| 89 | + $output .= '<span class="filter-value"><strong>' . $value['filter'][ $filter ]['value'] . $unit . '</strong></span>'; |
|
| 90 | + $output .= '</label>'; |
|
| 91 | + |
|
| 92 | + $output .= '<div '; |
|
| 93 | + $output .= 'class="redux-' . $mode . '-slider redux-' . $mode . '-filter redux-filter redux-filter-' . $filter . esc_attr( $field['class'] ) . '"'; |
|
| 94 | + $output .= 'id="' . esc_attr( $field['id'] ) . '"'; |
|
| 95 | + $output .= 'data-id="' . esc_attr( $field['id'] . '-' . $filter ) . '"'; |
|
| 96 | + $output .= 'data-min="' . $min . '"'; |
|
| 97 | + $output .= 'data-max="' . $max . '"'; |
|
| 98 | + $output .= 'data-step="' . $step . '"'; |
|
| 99 | + $output .= 'data-rtl="' . esc_attr( is_rtl() ) . '"'; |
|
| 100 | + $output .= 'data-unit="' . $unit . '"'; |
|
| 101 | + $output .= 'data-default = "' . esc_attr( $value['filter'][ $filter ]['value'] ) . '" '; |
|
| 102 | + $output .= disabled( filter_var( $value['filter'][ $filter ]['checked'], FILTER_VALIDATE_BOOLEAN ), false, false ); |
|
| 103 | + $output .= '>'; |
|
| 104 | + $output .= '</div>'; |
|
| 105 | + |
|
| 106 | + if ( '°' === $unit ) { |
|
| 107 | + $unit = 'deg'; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $output .= '<input '; |
|
| 111 | + $output .= 'type="hidden"'; |
|
| 112 | + $output .= 'id="redux-slider-value-' . esc_attr( $field['id'] ) . '-' . $filter . '"'; |
|
| 113 | + $output .= 'class="' . $mode . '-filter-' . $filter . '"'; |
|
| 114 | + $output .= 'name="' . esc_attr( $field['name'] . $field['name_suffix'] ) . '[filter][' . $filter . '][value]"'; |
|
| 115 | + $output .= 'value="' . esc_attr( $value['filter'][ $filter ]['value'] ) . '"'; |
|
| 116 | + $output .= 'data-id="' . esc_attr( $field['id'] ) . '"'; |
|
| 117 | + $output .= 'data-unit="' . $unit . '"'; |
|
| 118 | + $output .= '/>'; |
|
| 119 | + $output .= '</div>'; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + $output .= '</div>'; |
|
| 123 | + |
|
| 124 | + return $output; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Get filter unit. |
|
| 129 | + * |
|
| 130 | + * @param string $filter Filter type. |
|
| 131 | + * |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public static function get_filter_unit( string $filter ): string { |
|
| 135 | + if ( 'grayscale' === $filter || 'invert' === $filter || 'brightness' === $filter || 'contrast' === $filter ) { |
|
| 136 | + return '%'; |
|
| 137 | + } elseif ( 'blur' === $filter ) { |
|
| 138 | + return 'px'; |
|
| 139 | + } elseif ( 'hue-rotate' === $filter ) { |
|
| 140 | + return '°'; |
|
| 141 | + } else { |
|
| 142 | + return ''; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Enqueue support files. |
|
| 148 | + * |
|
| 149 | + * @param bool $filters_enabled Filtered enabled bit. |
|
| 150 | + */ |
|
| 151 | + public static function enqueue( bool $filters_enabled ) { |
|
| 152 | + $min = Redux_Functions::is_min(); |
|
| 153 | + |
|
| 154 | + if ( $filters_enabled ) { |
|
| 155 | + if ( ! wp_style_is( 'redux-nouislider' ) ) { |
|
| 156 | + wp_enqueue_style( |
|
| 157 | + 'redux-nouislider', |
|
| 158 | + Redux_Core::$url . 'assets/css/vendor/nouislider/redux.jquery.nouislider.css', |
|
| 159 | + array(), |
|
| 160 | + '5.0.0' |
|
| 161 | + ); |
|
| 162 | + |
|
| 163 | + wp_enqueue_script( |
|
| 164 | + 'redux-nouislider', |
|
| 165 | + Redux_Core::$url . 'assets/js/vendor/nouislider/redux.jquery.nouislider' . $min . '.js', |
|
| 166 | + array( 'jquery' ), |
|
| 167 | + '5.0.0', |
|
| 168 | + true |
|
| 169 | + ); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + wp_enqueue_script( |
|
| 173 | + 'redux-image-filters', |
|
| 174 | + Redux_Core::$url . 'inc/lib/image-filters/image-filters' . $min . '.js', |
|
| 175 | + array( 'jquery' ), |
|
| 176 | + Redux_Core::$version, |
|
| 177 | + true |
|
| 178 | + ); |
|
| 179 | + |
|
| 180 | + wp_enqueue_style( |
|
| 181 | + 'redux-image-filters', |
|
| 182 | + Redux_Core::$url . 'inc/lib/image-filters/image-filters.css', |
|
| 183 | + array(), |
|
| 184 | + Redux_Core::$version |
|
| 185 | + ); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | 189 | } |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | $in_use_filters = array(); |
| 45 | 45 | |
| 46 | 46 | foreach ( $filter_arr as $filter ) { |
| 47 | - if ( $field['filter'][ $filter ] ) { |
|
| 47 | + if ( $field['filter'][$filter] ) { |
|
| 48 | 48 | $in_use_filters[] = $filter; |
| 49 | 49 | } |
| 50 | 50 | } |
@@ -77,16 +77,16 @@ discard block |
||
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | $disabled = 'filters-disabled'; |
| 80 | - if ( $value['filter'][ $filter ]['checked'] ) { |
|
| 80 | + if ( $value['filter'][$filter]['checked'] ) { |
|
| 81 | 81 | $disabled = ''; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | $output .= '<div class="filter filter-' . $filter . '">'; |
| 85 | 85 | $output .= '<label for="' . esc_attr( $field['id'] ) . '-' . $filter . '" class="' . $disabled . '">'; |
| 86 | - $output .= '<input type="checkbox" id="' . esc_attr( $field['id'] ) . '-' . $filter . '" class="checkbox" value="1"' . checked( $value['filter'][ $filter ]['checked'], '1', false ) . '/>'; |
|
| 87 | - $output .= '<input type="hidden" data-val="1" value="' . esc_attr( $value['filter'][ $filter ]['checked'] ) . '" class="checkbox-check" name="' . esc_attr( $field['name'] . $field['name_suffix'] ) . '[filter][' . $filter . '][checked]"/>'; |
|
| 86 | + $output .= '<input type="checkbox" id="' . esc_attr( $field['id'] ) . '-' . $filter . '" class="checkbox" value="1"' . checked( $value['filter'][$filter]['checked'], '1', false ) . '/>'; |
|
| 87 | + $output .= '<input type="hidden" data-val="1" value="' . esc_attr( $value['filter'][$filter]['checked'] ) . '" class="checkbox-check" name="' . esc_attr( $field['name'] . $field['name_suffix'] ) . '[filter][' . $filter . '][checked]"/>'; |
|
| 88 | 88 | $output .= ucfirst( $filter ) . ': '; |
| 89 | - $output .= '<span class="filter-value"><strong>' . $value['filter'][ $filter ]['value'] . $unit . '</strong></span>'; |
|
| 89 | + $output .= '<span class="filter-value"><strong>' . $value['filter'][$filter]['value'] . $unit . '</strong></span>'; |
|
| 90 | 90 | $output .= '</label>'; |
| 91 | 91 | |
| 92 | 92 | $output .= '<div '; |
@@ -98,8 +98,8 @@ discard block |
||
| 98 | 98 | $output .= 'data-step="' . $step . '"'; |
| 99 | 99 | $output .= 'data-rtl="' . esc_attr( is_rtl() ) . '"'; |
| 100 | 100 | $output .= 'data-unit="' . $unit . '"'; |
| 101 | - $output .= 'data-default = "' . esc_attr( $value['filter'][ $filter ]['value'] ) . '" '; |
|
| 102 | - $output .= disabled( filter_var( $value['filter'][ $filter ]['checked'], FILTER_VALIDATE_BOOLEAN ), false, false ); |
|
| 101 | + $output .= 'data-default = "' . esc_attr( $value['filter'][$filter]['value'] ) . '" '; |
|
| 102 | + $output .= disabled( filter_var( $value['filter'][$filter]['checked'], FILTER_VALIDATE_BOOLEAN ), false, false ); |
|
| 103 | 103 | $output .= '>'; |
| 104 | 104 | $output .= '</div>'; |
| 105 | 105 | |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | $output .= 'id="redux-slider-value-' . esc_attr( $field['id'] ) . '-' . $filter . '"'; |
| 113 | 113 | $output .= 'class="' . $mode . '-filter-' . $filter . '"'; |
| 114 | 114 | $output .= 'name="' . esc_attr( $field['name'] . $field['name_suffix'] ) . '[filter][' . $filter . '][value]"'; |
| 115 | - $output .= 'value="' . esc_attr( $value['filter'][ $filter ]['value'] ) . '"'; |
|
| 115 | + $output .= 'value="' . esc_attr( $value['filter'][$filter]['value'] ) . '"'; |
|
| 116 | 116 | $output .= 'data-id="' . esc_attr( $field['id'] ) . '"'; |
| 117 | 117 | $output .= 'data-unit="' . $unit . '"'; |
| 118 | 118 | $output .= '/>'; |
@@ -69,12 +69,12 @@ discard block |
||
| 69 | 69 | foreach ( $settings as $a ) { |
| 70 | 70 | if ( isset( $a['data'] ) ) { |
| 71 | 71 | $a['data'] = rawurldecode( $a['data'] ); |
| 72 | - $a = (array) json_decode( $a['data'] ); |
|
| 72 | + $a = ( array ) json_decode( $a['data'] ); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | if ( $default_id === $a['id'] ) { |
| 76 | 76 | $search_default = false; |
| 77 | - $fixed_arr[ $key ] = $a; |
|
| 77 | + $fixed_arr[$key] = $a; |
|
| 78 | 78 | break; |
| 79 | 79 | } |
| 80 | 80 | } |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | |
| 88 | 88 | foreach ( $stock as $def_arr ) { |
| 89 | 89 | if ( $default_id === $def_arr['id'] ) { |
| 90 | - $fixed_arr[ $key ] = $def_arr; |
|
| 90 | + $fixed_arr[$key] = $def_arr; |
|
| 91 | 91 | break; |
| 92 | 92 | } |
| 93 | 93 | } |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | global $pagenow, $post; |
| 114 | 114 | |
| 115 | 115 | $redux_settings = get_option( $this->opt_name ); |
| 116 | - $settings = $redux_settings[ $this->field_id ] ?? array(); |
|
| 116 | + $settings = $redux_settings[$this->field_id] ?? array(); |
|
| 117 | 117 | |
| 118 | 118 | if ( is_admin() && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) { |
| 119 | 119 | $post_settings = get_post_meta( $post->ID, $this->field_id, true ); |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | $settings = $this->rebuild_setttings( $settings ); |
| 152 | 152 | |
| 153 | 153 | foreach ( $this->defaults as $key => $social_provider_default ) { |
| 154 | - $social_provider_option = ( $settings && is_array( $settings ) && array_key_exists( $key, $settings ) ) ? $settings[ $key ] : null; |
|
| 154 | + $social_provider_option = ( $settings && is_array( $settings ) && array_key_exists( $key, $settings ) ) ? $settings[$key] : null; |
|
| 155 | 155 | |
| 156 | 156 | $icon = ( $social_provider_option && array_key_exists( 'icon', $social_provider_option ) && $social_provider_option['icon'] ) ? $social_provider_option['icon'] : $social_provider_default['icon']; |
| 157 | 157 | $name = ( $social_provider_option && array_key_exists( 'name', $social_provider_option ) && $social_provider_option['name'] ) ? $social_provider_option['name'] : $social_provider_default['name']; |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | echo '<li id="redux-social-item-' . esc_attr( $key ) . '" data-key="' . esc_attr( $key ) . '" style="display: none;">'; |
| 175 | 175 | echo '<div class="redux-social-item-container">'; |
| 176 | 176 | |
| 177 | - $social_provider_option = ( $settings && is_array( $settings ) && array_key_exists( $key, $settings ) ) ? $settings[ $key ] : null; |
|
| 177 | + $social_provider_option = ( $settings && is_array( $settings ) && array_key_exists( $key, $settings ) ) ? $settings[$key] : null; |
|
| 178 | 178 | $icon = ( $social_provider_option && array_key_exists( 'icon', $social_provider_option ) && $social_provider_option['icon'] ) ? $social_provider_option['icon'] : $social_provider_default['icon']; |
| 179 | 179 | $id = ( $social_provider_option && array_key_exists( 'id', $social_provider_option ) && $social_provider_option['id'] ) ? $social_provider_option['id'] : $social_provider_default['id']; |
| 180 | 180 | $enabled = ( $social_provider_option && array_key_exists( 'enabled', $social_provider_option ) && $social_provider_option['enabled'] ) ? $social_provider_option['enabled'] : $social_provider_default['enabled']; |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | // For customizer. |
| 311 | 311 | if ( isset( $arr['data'] ) ) { |
| 312 | 312 | $arr = rawurldecode( $arr['data'] ); |
| 313 | - $arr = (array) json_decode( $arr ); |
|
| 313 | + $arr = ( array ) json_decode( $arr ); |
|
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | if ( $arr['enabled'] ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement |
@@ -11,355 +11,355 @@ |
||
| 11 | 11 | |
| 12 | 12 | if ( ! class_exists( 'Redux_Social_Profiles' ) ) { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Main Redux_Social_Profiles class |
|
| 16 | - * |
|
| 17 | - * @since 1.0.0 |
|
| 18 | - */ |
|
| 19 | - class Redux_Social_Profiles extends Redux_Field { |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * Field ID. |
|
| 23 | - * |
|
| 24 | - * @var null|string |
|
| 25 | - */ |
|
| 26 | - public ?string $field_id; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Panel opt_name. |
|
| 30 | - * |
|
| 31 | - * @var null|string |
|
| 32 | - */ |
|
| 33 | - public ?string $opt_name; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Defaults array. |
|
| 37 | - * |
|
| 38 | - * @var null|array |
|
| 39 | - */ |
|
| 40 | - |
|
| 41 | - private ?array $defaults = array(); |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Set defaults. |
|
| 45 | - */ |
|
| 46 | - public function set_defaults() { |
|
| 47 | - $this->opt_name = $this->parent->args['opt_name']; |
|
| 48 | - $this->field_id = $this->field['id']; |
|
| 49 | - |
|
| 50 | - $this->defaults = Redux_Social_Profiles_Functions::get_default_data(); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Rebuild Settings. |
|
| 55 | - * |
|
| 56 | - * @param array $settings Settings array. |
|
| 57 | - * |
|
| 58 | - * @return array |
|
| 59 | - */ |
|
| 60 | - private function rebuild_setttings( array $settings ): array { |
|
| 61 | - $fixed_arr = array(); |
|
| 62 | - $stock = ''; |
|
| 63 | - |
|
| 64 | - foreach ( $this->defaults as $key => $arr ) { |
|
| 65 | - $search_default = true; |
|
| 66 | - |
|
| 67 | - $default_id = $arr['id']; |
|
| 68 | - |
|
| 69 | - foreach ( $settings as $a ) { |
|
| 70 | - if ( isset( $a['data'] ) ) { |
|
| 71 | - $a['data'] = rawurldecode( $a['data'] ); |
|
| 72 | - $a = (array) json_decode( $a['data'] ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - if ( $default_id === $a['id'] ) { |
|
| 76 | - $search_default = false; |
|
| 77 | - $fixed_arr[ $key ] = $a; |
|
| 78 | - break; |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - if ( $search_default ) { |
|
| 83 | - if ( '' === $stock ) { |
|
| 84 | - $stock = Redux_Social_Profiles_Defaults::get_social_media_defaults(); |
|
| 85 | - $stock = Redux_Social_Profiles_Functions::add_extra_icons( $stock ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - foreach ( $stock as $def_arr ) { |
|
| 89 | - if ( $default_id === $def_arr['id'] ) { |
|
| 90 | - $fixed_arr[ $key ] = $def_arr; |
|
| 91 | - break; |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - return $fixed_arr; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Field Render Function. |
|
| 102 | - * Takes the vars and outputs the HTML for the field in the settings |
|
| 103 | - * |
|
| 104 | - * @since 1.0.0 |
|
| 105 | - * @access public |
|
| 106 | - * @return void |
|
| 107 | - */ |
|
| 108 | - public function render() { |
|
| 109 | - if ( empty( $this->field ) ) { |
|
| 110 | - return; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - global $pagenow, $post; |
|
| 114 | - |
|
| 115 | - $redux_settings = get_option( $this->opt_name ); |
|
| 116 | - $settings = $redux_settings[ $this->field_id ] ?? array(); |
|
| 117 | - |
|
| 118 | - if ( is_admin() && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) { |
|
| 119 | - $post_settings = get_post_meta( $post->ID, $this->field_id, true ); |
|
| 120 | - |
|
| 121 | - if ( ! empty( $post_settings ) ) { |
|
| 122 | - $settings = $post_settings; |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $color_pickers = $this->field['color_pickers'] ?? true; |
|
| 127 | - |
|
| 128 | - // Icon container. |
|
| 129 | - echo '<div |
|
| 14 | + /** |
|
| 15 | + * Main Redux_Social_Profiles class |
|
| 16 | + * |
|
| 17 | + * @since 1.0.0 |
|
| 18 | + */ |
|
| 19 | + class Redux_Social_Profiles extends Redux_Field { |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Field ID. |
|
| 23 | + * |
|
| 24 | + * @var null|string |
|
| 25 | + */ |
|
| 26 | + public ?string $field_id; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Panel opt_name. |
|
| 30 | + * |
|
| 31 | + * @var null|string |
|
| 32 | + */ |
|
| 33 | + public ?string $opt_name; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Defaults array. |
|
| 37 | + * |
|
| 38 | + * @var null|array |
|
| 39 | + */ |
|
| 40 | + |
|
| 41 | + private ?array $defaults = array(); |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Set defaults. |
|
| 45 | + */ |
|
| 46 | + public function set_defaults() { |
|
| 47 | + $this->opt_name = $this->parent->args['opt_name']; |
|
| 48 | + $this->field_id = $this->field['id']; |
|
| 49 | + |
|
| 50 | + $this->defaults = Redux_Social_Profiles_Functions::get_default_data(); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Rebuild Settings. |
|
| 55 | + * |
|
| 56 | + * @param array $settings Settings array. |
|
| 57 | + * |
|
| 58 | + * @return array |
|
| 59 | + */ |
|
| 60 | + private function rebuild_setttings( array $settings ): array { |
|
| 61 | + $fixed_arr = array(); |
|
| 62 | + $stock = ''; |
|
| 63 | + |
|
| 64 | + foreach ( $this->defaults as $key => $arr ) { |
|
| 65 | + $search_default = true; |
|
| 66 | + |
|
| 67 | + $default_id = $arr['id']; |
|
| 68 | + |
|
| 69 | + foreach ( $settings as $a ) { |
|
| 70 | + if ( isset( $a['data'] ) ) { |
|
| 71 | + $a['data'] = rawurldecode( $a['data'] ); |
|
| 72 | + $a = (array) json_decode( $a['data'] ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + if ( $default_id === $a['id'] ) { |
|
| 76 | + $search_default = false; |
|
| 77 | + $fixed_arr[ $key ] = $a; |
|
| 78 | + break; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + if ( $search_default ) { |
|
| 83 | + if ( '' === $stock ) { |
|
| 84 | + $stock = Redux_Social_Profiles_Defaults::get_social_media_defaults(); |
|
| 85 | + $stock = Redux_Social_Profiles_Functions::add_extra_icons( $stock ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + foreach ( $stock as $def_arr ) { |
|
| 89 | + if ( $default_id === $def_arr['id'] ) { |
|
| 90 | + $fixed_arr[ $key ] = $def_arr; |
|
| 91 | + break; |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + return $fixed_arr; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Field Render Function. |
|
| 102 | + * Takes the vars and outputs the HTML for the field in the settings |
|
| 103 | + * |
|
| 104 | + * @since 1.0.0 |
|
| 105 | + * @access public |
|
| 106 | + * @return void |
|
| 107 | + */ |
|
| 108 | + public function render() { |
|
| 109 | + if ( empty( $this->field ) ) { |
|
| 110 | + return; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + global $pagenow, $post; |
|
| 114 | + |
|
| 115 | + $redux_settings = get_option( $this->opt_name ); |
|
| 116 | + $settings = $redux_settings[ $this->field_id ] ?? array(); |
|
| 117 | + |
|
| 118 | + if ( is_admin() && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) { |
|
| 119 | + $post_settings = get_post_meta( $post->ID, $this->field_id, true ); |
|
| 120 | + |
|
| 121 | + if ( ! empty( $post_settings ) ) { |
|
| 122 | + $settings = $post_settings; |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $color_pickers = $this->field['color_pickers'] ?? true; |
|
| 127 | + |
|
| 128 | + // Icon container. |
|
| 129 | + echo '<div |
|
| 130 | 130 | class="redux-social-profiles-container ' . esc_attr( $this->field['class'] ) . '" |
| 131 | 131 | data-opt-name="' . esc_attr( $this->opt_name ) . '" |
| 132 | 132 | data-id="' . esc_attr( $this->field_id ) . '" |
| 133 | 133 | >'; |
| 134 | 134 | |
| 135 | - $show_msg = $this->field['hide_widget_msg'] ?? true; |
|
| 135 | + $show_msg = $this->field['hide_widget_msg'] ?? true; |
|
| 136 | 136 | |
| 137 | - // translators: %1$s: Widget page HTML/URL. %s: widget admin URL. |
|
| 138 | - $def_msg = sprintf( esc_html__( 'Go to the %1$s page to add the Redux Social Widget to any active widget area.', 'redux-framework' ), sprintf( '<a href="%s">' . esc_html__( 'Widgets', 'redux-framework' ) . '</a> ', admin_url( 'widgets.php' ) ) ); |
|
| 137 | + // translators: %1$s: Widget page HTML/URL. %s: widget admin URL. |
|
| 138 | + $def_msg = sprintf( esc_html__( 'Go to the %1$s page to add the Redux Social Widget to any active widget area.', 'redux-framework' ), sprintf( '<a href="%s">' . esc_html__( 'Widgets', 'redux-framework' ) . '</a> ', admin_url( 'widgets.php' ) ) ); |
|
| 139 | 139 | |
| 140 | - $msg = $this->field['widget_msg'] ?? $def_msg; |
|
| 140 | + $msg = $this->field['widget_msg'] ?? $def_msg; |
|
| 141 | 141 | |
| 142 | - if ( ! $show_msg ) { |
|
| 143 | - echo '<div class="redux-social-profiles-header">'; |
|
| 144 | - echo $msg; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 145 | - echo '</div>'; |
|
| 146 | - } |
|
| 142 | + if ( ! $show_msg ) { |
|
| 143 | + echo '<div class="redux-social-profiles-header">'; |
|
| 144 | + echo $msg; // phpcs:ignore WordPress.Security.EscapeOutput |
|
| 145 | + echo '</div>'; |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - echo '<div class="redux-social-profiles-selector-container">'; |
|
| 149 | - echo '<ul id="redux-social-profiles-selector-list">'; |
|
| 148 | + echo '<div class="redux-social-profiles-selector-container">'; |
|
| 149 | + echo '<ul id="redux-social-profiles-selector-list">'; |
|
| 150 | 150 | |
| 151 | - $settings = $this->rebuild_setttings( $settings ); |
|
| 151 | + $settings = $this->rebuild_setttings( $settings ); |
|
| 152 | 152 | |
| 153 | - foreach ( $this->defaults as $key => $social_provider_default ) { |
|
| 154 | - $social_provider_option = ( $settings && is_array( $settings ) && array_key_exists( $key, $settings ) ) ? $settings[ $key ] : null; |
|
| 153 | + foreach ( $this->defaults as $key => $social_provider_default ) { |
|
| 154 | + $social_provider_option = ( $settings && is_array( $settings ) && array_key_exists( $key, $settings ) ) ? $settings[ $key ] : null; |
|
| 155 | 155 | |
| 156 | - $icon = ( $social_provider_option && array_key_exists( 'icon', $social_provider_option ) && $social_provider_option['icon'] ) ? $social_provider_option['icon'] : $social_provider_default['icon']; |
|
| 157 | - $name = ( $social_provider_option && array_key_exists( 'name', $social_provider_option ) && $social_provider_option['name'] ) ? $social_provider_option['name'] : $social_provider_default['name']; |
|
| 158 | - $order = ( $social_provider_option && array_key_exists( 'order', $social_provider_option ) ) ? $social_provider_option['order'] : $key; |
|
| 159 | - $order = intval( $order ); |
|
| 160 | - $enabled = ( $social_provider_option && array_key_exists( 'enabled', $social_provider_option ) && $social_provider_option['enabled'] ) ? $social_provider_option['enabled'] : $social_provider_default['enabled']; |
|
| 161 | - $display = ( $enabled ) ? 'enabled' : ''; |
|
| 156 | + $icon = ( $social_provider_option && array_key_exists( 'icon', $social_provider_option ) && $social_provider_option['icon'] ) ? $social_provider_option['icon'] : $social_provider_default['icon']; |
|
| 157 | + $name = ( $social_provider_option && array_key_exists( 'name', $social_provider_option ) && $social_provider_option['name'] ) ? $social_provider_option['name'] : $social_provider_default['name']; |
|
| 158 | + $order = ( $social_provider_option && array_key_exists( 'order', $social_provider_option ) ) ? $social_provider_option['order'] : $key; |
|
| 159 | + $order = intval( $order ); |
|
| 160 | + $enabled = ( $social_provider_option && array_key_exists( 'enabled', $social_provider_option ) && $social_provider_option['enabled'] ) ? $social_provider_option['enabled'] : $social_provider_default['enabled']; |
|
| 161 | + $display = ( $enabled ) ? 'enabled' : ''; |
|
| 162 | 162 | |
| 163 | - echo '<li class="redux-social-profiles-item-enable ' . esc_attr( $display ) . '" id="redux-social-profiles-item-enable-' . esc_attr( $key ) . '" data-key="' . esc_attr( $key ) . '" data-order="' . esc_attr( $order ) . '">'; |
|
| 164 | - Redux_Social_Profiles_Functions::render_icon( $icon, '', '', $name ); |
|
| 165 | - echo '</li>'; |
|
| 166 | - } |
|
| 163 | + echo '<li class="redux-social-profiles-item-enable ' . esc_attr( $display ) . '" id="redux-social-profiles-item-enable-' . esc_attr( $key ) . '" data-key="' . esc_attr( $key ) . '" data-order="' . esc_attr( $order ) . '">'; |
|
| 164 | + Redux_Social_Profiles_Functions::render_icon( $icon, '', '', $name ); |
|
| 165 | + echo '</li>'; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - echo '</ul>'; |
|
| 169 | - echo '</div>'; |
|
| 168 | + echo '</ul>'; |
|
| 169 | + echo '</div>'; |
|
| 170 | 170 | |
| 171 | - echo '<ul id="redux-social-profiles-list">'; |
|
| 171 | + echo '<ul id="redux-social-profiles-list">'; |
|
| 172 | 172 | |
| 173 | - foreach ( $this->defaults as $key => $social_provider_default ) { |
|
| 174 | - echo '<li id="redux-social-item-' . esc_attr( $key ) . '" data-key="' . esc_attr( $key ) . '" style="display: none;">'; |
|
| 175 | - echo '<div class="redux-social-item-container">'; |
|
| 173 | + foreach ( $this->defaults as $key => $social_provider_default ) { |
|
| 174 | + echo '<li id="redux-social-item-' . esc_attr( $key ) . '" data-key="' . esc_attr( $key ) . '" style="display: none;">'; |
|
| 175 | + echo '<div class="redux-social-item-container">'; |
|
| 176 | 176 | |
| 177 | - $social_provider_option = ( $settings && is_array( $settings ) && array_key_exists( $key, $settings ) ) ? $settings[ $key ] : null; |
|
| 178 | - $icon = ( $social_provider_option && array_key_exists( 'icon', $social_provider_option ) && $social_provider_option['icon'] ) ? $social_provider_option['icon'] : $social_provider_default['icon']; |
|
| 179 | - $id = ( $social_provider_option && array_key_exists( 'id', $social_provider_option ) && $social_provider_option['id'] ) ? $social_provider_option['id'] : $social_provider_default['id']; |
|
| 180 | - $enabled = ( $social_provider_option && array_key_exists( 'enabled', $social_provider_option ) && $social_provider_option['enabled'] ) ? $social_provider_option['enabled'] : $social_provider_default['enabled']; |
|
| 181 | - $name = ( $social_provider_option && array_key_exists( 'name', $social_provider_option ) && $social_provider_option['name'] ) ? $social_provider_option['name'] : $social_provider_default['name']; |
|
| 177 | + $social_provider_option = ( $settings && is_array( $settings ) && array_key_exists( $key, $settings ) ) ? $settings[ $key ] : null; |
|
| 178 | + $icon = ( $social_provider_option && array_key_exists( 'icon', $social_provider_option ) && $social_provider_option['icon'] ) ? $social_provider_option['icon'] : $social_provider_default['icon']; |
|
| 179 | + $id = ( $social_provider_option && array_key_exists( 'id', $social_provider_option ) && $social_provider_option['id'] ) ? $social_provider_option['id'] : $social_provider_default['id']; |
|
| 180 | + $enabled = ( $social_provider_option && array_key_exists( 'enabled', $social_provider_option ) && $social_provider_option['enabled'] ) ? $social_provider_option['enabled'] : $social_provider_default['enabled']; |
|
| 181 | + $name = ( $social_provider_option && array_key_exists( 'name', $social_provider_option ) && $social_provider_option['name'] ) ? $social_provider_option['name'] : $social_provider_default['name']; |
|
| 182 | 182 | |
| 183 | - $label = ( $social_provider_option && array_key_exists( 'label', $social_provider_option ) && $social_provider_option['label'] ) ? $social_provider_option['label'] : __( 'Link URL', 'redux-framework' ); |
|
| 183 | + $label = ( $social_provider_option && array_key_exists( 'label', $social_provider_option ) && $social_provider_option['label'] ) ? $social_provider_option['label'] : __( 'Link URL', 'redux-framework' ); |
|
| 184 | 184 | |
| 185 | - $color = ( $social_provider_option && array_key_exists( 'color', $social_provider_option ) ) ? $social_provider_option['color'] : $social_provider_default['color']; |
|
| 186 | - $color = esc_attr( $color ); |
|
| 185 | + $color = ( $social_provider_option && array_key_exists( 'color', $social_provider_option ) ) ? $social_provider_option['color'] : $social_provider_default['color']; |
|
| 186 | + $color = esc_attr( $color ); |
|
| 187 | 187 | |
| 188 | - $background = ( $social_provider_option && array_key_exists( 'background', $social_provider_option ) ) ? $social_provider_option['background'] : $social_provider_default['background']; |
|
| 189 | - $background = esc_attr( $background ); |
|
| 188 | + $background = ( $social_provider_option && array_key_exists( 'background', $social_provider_option ) ) ? $social_provider_option['background'] : $social_provider_default['background']; |
|
| 189 | + $background = esc_attr( $background ); |
|
| 190 | 190 | |
| 191 | - $order = ( $social_provider_option && array_key_exists( 'order', $social_provider_option ) ) ? $social_provider_option['order'] : $key; |
|
| 192 | - $order = intval( $order ); |
|
| 191 | + $order = ( $social_provider_option && array_key_exists( 'order', $social_provider_option ) ) ? $social_provider_option['order'] : $key; |
|
| 192 | + $order = intval( $order ); |
|
| 193 | 193 | |
| 194 | - $url = ( $social_provider_option && array_key_exists( 'url', $social_provider_option ) ) ? $social_provider_option['url'] : $social_provider_default['url']; |
|
| 195 | - $url = esc_attr( $url ); |
|
| 194 | + $url = ( $social_provider_option && array_key_exists( 'url', $social_provider_option ) ) ? $social_provider_option['url'] : $social_provider_default['url']; |
|
| 195 | + $url = esc_attr( $url ); |
|
| 196 | 196 | |
| 197 | - $profile_data = array( |
|
| 198 | - 'id' => $id, |
|
| 199 | - 'icon' => $icon, |
|
| 200 | - 'enabled' => $enabled, |
|
| 201 | - 'url' => $url, |
|
| 202 | - 'color' => $color, |
|
| 203 | - 'background' => $background, |
|
| 204 | - 'order' => $order, |
|
| 205 | - 'name' => $name, |
|
| 206 | - 'label' => $label, |
|
| 207 | - ); |
|
| 197 | + $profile_data = array( |
|
| 198 | + 'id' => $id, |
|
| 199 | + 'icon' => $icon, |
|
| 200 | + 'enabled' => $enabled, |
|
| 201 | + 'url' => $url, |
|
| 202 | + 'color' => $color, |
|
| 203 | + 'background' => $background, |
|
| 204 | + 'order' => $order, |
|
| 205 | + 'name' => $name, |
|
| 206 | + 'label' => $label, |
|
| 207 | + ); |
|
| 208 | 208 | |
| 209 | - $profile_data = rawurlencode( wp_json_encode( $profile_data ) ); |
|
| 209 | + $profile_data = rawurlencode( wp_json_encode( $profile_data ) ); |
|
| 210 | 210 | |
| 211 | - echo '<input |
|
| 211 | + echo '<input |
|
| 212 | 212 | type="hidden" |
| 213 | 213 | class="redux-social-profiles-hidden-data-' . esc_attr( $key ) . '" |
| 214 | 214 | id="' . esc_attr( $this->field_id ) . '-' . esc_attr( $id ) . '-data" |
| 215 | 215 | name="' . esc_attr( $this->field['name'] ) . esc_attr( $this->field['name_suffix'] ) . '[' . esc_attr( $key ) . '][data]" |
| 216 | 216 | value="' . $profile_data . '" />'; // phpcs:ignore WordPress.Security.EscapeOutput |
| 217 | 217 | |
| 218 | - echo '<div class="redux-icon-preview">'; |
|
| 219 | - Redux_Social_Profiles_Functions::render_icon( $icon, $color, $background, $name ); |
|
| 220 | - echo ' </div>'; |
|
| 218 | + echo '<div class="redux-icon-preview">'; |
|
| 219 | + Redux_Social_Profiles_Functions::render_icon( $icon, $color, $background, $name ); |
|
| 220 | + echo ' </div>'; |
|
| 221 | 221 | |
| 222 | - echo '<div class="redux-social-profiles-item-name">'; |
|
| 223 | - echo esc_html( $name ); |
|
| 224 | - echo '</div>'; |
|
| 222 | + echo '<div class="redux-social-profiles-item-name">'; |
|
| 223 | + echo esc_html( $name ); |
|
| 224 | + echo '</div>'; |
|
| 225 | 225 | |
| 226 | - echo '<div class="redux-social-profiles-item-enabled">'; |
|
| 227 | - $checked = ( $enabled ) ? 'checked' : ''; |
|
| 228 | - echo '<input type="checkbox" id="' . esc_attr( $this->field['id'] ) . '-checkbox-' . esc_attr( $key ) . '" class="checkbox-' . esc_attr( $key ) . '" data-key="' . esc_attr( $key ) . '" value="1" ' . esc_attr( $checked ) . '/>'; |
|
| 229 | - esc_html_e( 'Enabled', 'redux-framework' ); |
|
| 230 | - echo '</div>'; |
|
| 226 | + echo '<div class="redux-social-profiles-item-enabled">'; |
|
| 227 | + $checked = ( $enabled ) ? 'checked' : ''; |
|
| 228 | + echo '<input type="checkbox" id="' . esc_attr( $this->field['id'] ) . '-checkbox-' . esc_attr( $key ) . '" class="checkbox-' . esc_attr( $key ) . '" data-key="' . esc_attr( $key ) . '" value="1" ' . esc_attr( $checked ) . '/>'; |
|
| 229 | + esc_html_e( 'Enabled', 'redux-framework' ); |
|
| 230 | + echo '</div>'; |
|
| 231 | 231 | |
| 232 | - $color_class = $color_pickers ? '' : ' no-color-pickers'; |
|
| 232 | + $color_class = $color_pickers ? '' : ' no-color-pickers'; |
|
| 233 | 233 | |
| 234 | - echo '<div class="redux-social-profiles-link-url input_wrapper' . esc_attr( $color_class ) . '">'; |
|
| 235 | - echo '<label for="redux-social-profiles-url-' . esc_attr( $key ) . '-text" class="redux-text-url-label">' . esc_html( $label ) . '</label>'; |
|
| 236 | - echo '<input id="redux-social-profiles-url-' . esc_attr( $key ) . '-text" class="redux-social-profiles-url-text" data-key="' . esc_attr( $key ) . '" type="text" value="' . esc_url( $url ) . '" />'; |
|
| 237 | - echo '</div>'; |
|
| 234 | + echo '<div class="redux-social-profiles-link-url input_wrapper' . esc_attr( $color_class ) . '">'; |
|
| 235 | + echo '<label for="redux-social-profiles-url-' . esc_attr( $key ) . '-text" class="redux-text-url-label">' . esc_html( $label ) . '</label>'; |
|
| 236 | + echo '<input id="redux-social-profiles-url-' . esc_attr( $key ) . '-text" class="redux-social-profiles-url-text" data-key="' . esc_attr( $key ) . '" type="text" value="' . esc_url( $url ) . '" />'; |
|
| 237 | + echo '</div>'; |
|
| 238 | 238 | |
| 239 | - $reset_text = __( 'Reset', 'redux-framework' ); |
|
| 240 | - echo '<div class="redux-social-profiles-item-reset">'; |
|
| 241 | - echo '<a class="button" data-value="' . esc_attr( $key ) . '" value="' . esc_attr( $reset_text ) . '" />' . esc_html( $reset_text ) . '</a>'; |
|
| 242 | - echo '</div>'; |
|
| 239 | + $reset_text = __( 'Reset', 'redux-framework' ); |
|
| 240 | + echo '<div class="redux-social-profiles-item-reset">'; |
|
| 241 | + echo '<a class="button" data-value="' . esc_attr( $key ) . '" value="' . esc_attr( $reset_text ) . '" />' . esc_html( $reset_text ) . '</a>'; |
|
| 242 | + echo '</div>'; |
|
| 243 | 243 | |
| 244 | - if ( $color_pickers ) { |
|
| 245 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 246 | - $label = apply_filters( 'redux/extensions/social_profiles/' . $this->opt_name . '/color_picker/text', esc_html__( 'Text', 'redux-framework' ) ); |
|
| 244 | + if ( $color_pickers ) { |
|
| 245 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 246 | + $label = apply_filters( 'redux/extensions/social_profiles/' . $this->opt_name . '/color_picker/text', esc_html__( 'Text', 'redux-framework' ) ); |
|
| 247 | 247 | |
| 248 | - echo '<div class="redux-social-profiles-text-color picker_wrapper" >'; |
|
| 249 | - echo '<label for="redux-social-profiles-color-picker-' . esc_attr( $key ) . ' text" class="redux-text-color-label">' . esc_html( $label ) . '</label>'; |
|
| 250 | - echo '<input |
|
| 248 | + echo '<div class="redux-social-profiles-text-color picker_wrapper" >'; |
|
| 249 | + echo '<label for="redux-social-profiles-color-picker-' . esc_attr( $key ) . ' text" class="redux-text-color-label">' . esc_html( $label ) . '</label>'; |
|
| 250 | + echo '<input |
|
| 251 | 251 | class="redux-social-profiles-color-picker-' . esc_attr( $key ) . ' text" |
| 252 | 252 | id="redux-social-profiles-color-picker-' . esc_attr( $key ) . ' text" |
| 253 | 253 | type="text" |
| 254 | 254 | value="' . esc_attr( $color ) . '" |
| 255 | 255 | data-key="' . esc_attr( $key ) . '" |
| 256 | 256 | />'; |
| 257 | - echo '</div>'; |
|
| 257 | + echo '</div>'; |
|
| 258 | 258 | |
| 259 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 260 | - $label = apply_filters( 'redux/extensions/social_profiles/' . $this->opt_name . '/color_picker/background', esc_html__( 'Background', 'redux-framework' ) ); |
|
| 259 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName |
|
| 260 | + $label = apply_filters( 'redux/extensions/social_profiles/' . $this->opt_name . '/color_picker/background', esc_html__( 'Background', 'redux-framework' ) ); |
|
| 261 | 261 | |
| 262 | - echo '<div class="redux-social-profiles-background-color picker_wrapper">'; |
|
| 263 | - echo '<label for="redux-social-profiles-color-picker-' . esc_attr( $key ) . ' background" class="redux-background-color-label" for="redux-social-profiles-color-picker-' . esc_attr( $key ) . ' background">' . esc_html( $label ) . '</label>'; |
|
| 264 | - echo '<input |
|
| 262 | + echo '<div class="redux-social-profiles-background-color picker_wrapper">'; |
|
| 263 | + echo '<label for="redux-social-profiles-color-picker-' . esc_attr( $key ) . ' background" class="redux-background-color-label" for="redux-social-profiles-color-picker-' . esc_attr( $key ) . ' background">' . esc_html( $label ) . '</label>'; |
|
| 264 | + echo '<input |
|
| 265 | 265 | class="redux-social-profiles-color-picker-' . esc_attr( $key ) . ' background" |
| 266 | 266 | id="redux-social-profiles-color-picker-' . esc_attr( $key ) . ' background" |
| 267 | 267 | type="text" |
| 268 | 268 | value="' . esc_attr( $background ) . '" |
| 269 | 269 | data-key="' . esc_attr( $key ) . '" |
| 270 | 270 | />'; |
| 271 | - echo '</div>'; |
|
| 272 | - } |
|
| 271 | + echo '</div>'; |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | - echo '<div class="redux-social-profiles-item-order">'; |
|
| 275 | - echo '<input |
|
| 274 | + echo '<div class="redux-social-profiles-item-order">'; |
|
| 275 | + echo '<input |
|
| 276 | 276 | id="' . esc_attr( $this->field['id'] ) . '-item-order-' . esc_attr( $key ) . '" |
| 277 | 277 | type="hidden" |
| 278 | 278 | value="' . esc_attr( $order ) . '" |
| 279 | 279 | />'; |
| 280 | - echo '</div>'; |
|
| 281 | - |
|
| 282 | - echo '</div>'; |
|
| 283 | - echo '</li>'; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - echo '</ul>'; |
|
| 287 | - echo '</div>'; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * This function is unused, but necessary to trigger output. |
|
| 292 | - * |
|
| 293 | - * @param mixed $data CSS data. |
|
| 294 | - * |
|
| 295 | - * @return mixed|string|void |
|
| 296 | - */ |
|
| 297 | - public function css_style( $data ) { |
|
| 298 | - return $data; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Used to enqueue to the front-end |
|
| 303 | - * |
|
| 304 | - * @param string|null|array $style Style. |
|
| 305 | - */ |
|
| 306 | - public function output( $style = '' ) { |
|
| 307 | - if ( ! empty( $this->value ) ) { |
|
| 308 | - foreach ( $this->value as $arr ) { |
|
| 309 | - |
|
| 310 | - // For customizer. |
|
| 311 | - if ( isset( $arr['data'] ) ) { |
|
| 312 | - $arr = rawurldecode( $arr['data'] ); |
|
| 313 | - $arr = (array) json_decode( $arr ); |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - if ( $arr['enabled'] ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement |
|
| 317 | - Redux_Functions_Ex::enqueue_font_awesome(); |
|
| 318 | - } |
|
| 319 | - } |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Enqueue Function. |
|
| 325 | - * If this field requires any scripts, or css define this function and register/enqueue the scripts/css |
|
| 326 | - * |
|
| 327 | - * @since 1.0.0 |
|
| 328 | - * @access public |
|
| 329 | - * @return void |
|
| 330 | - */ |
|
| 331 | - public function enqueue() { |
|
| 332 | - if ( empty( $this->field ) ) { |
|
| 333 | - return; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - $min = Redux_Functions::isMin(); |
|
| 337 | - |
|
| 338 | - Redux_Functions_Ex::enqueue_font_awesome(); |
|
| 339 | - |
|
| 340 | - // Field dependent JS. |
|
| 341 | - wp_enqueue_script( |
|
| 342 | - 'redux-field-social-profiles', |
|
| 343 | - $this->url . 'redux-social-profiles' . $min . '.js', |
|
| 344 | - array( 'jquery', 'jquery-ui-sortable', 'redux-spectrum-js', 'redux-js' ), |
|
| 345 | - Redux_Extension_Social_Profiles::$version, |
|
| 346 | - true |
|
| 347 | - ); |
|
| 348 | - |
|
| 349 | - wp_localize_script( |
|
| 350 | - 'redux-field-social-profiles', |
|
| 351 | - 'reduxSocialDefaults', |
|
| 352 | - $this->defaults |
|
| 353 | - ); |
|
| 354 | - |
|
| 355 | - if ( $this->parent->args['dev_mode'] ) { |
|
| 356 | - wp_enqueue_style( |
|
| 357 | - 'redux-field-social-profiles', |
|
| 358 | - $this->url . 'redux-social-profiles.css', |
|
| 359 | - array( 'redux-spectrum-css' ), |
|
| 360 | - Redux_Extension_Social_Profiles::$version |
|
| 361 | - ); |
|
| 362 | - } |
|
| 363 | - } |
|
| 364 | - } |
|
| 280 | + echo '</div>'; |
|
| 281 | + |
|
| 282 | + echo '</div>'; |
|
| 283 | + echo '</li>'; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + echo '</ul>'; |
|
| 287 | + echo '</div>'; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * This function is unused, but necessary to trigger output. |
|
| 292 | + * |
|
| 293 | + * @param mixed $data CSS data. |
|
| 294 | + * |
|
| 295 | + * @return mixed|string|void |
|
| 296 | + */ |
|
| 297 | + public function css_style( $data ) { |
|
| 298 | + return $data; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Used to enqueue to the front-end |
|
| 303 | + * |
|
| 304 | + * @param string|null|array $style Style. |
|
| 305 | + */ |
|
| 306 | + public function output( $style = '' ) { |
|
| 307 | + if ( ! empty( $this->value ) ) { |
|
| 308 | + foreach ( $this->value as $arr ) { |
|
| 309 | + |
|
| 310 | + // For customizer. |
|
| 311 | + if ( isset( $arr['data'] ) ) { |
|
| 312 | + $arr = rawurldecode( $arr['data'] ); |
|
| 313 | + $arr = (array) json_decode( $arr ); |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + if ( $arr['enabled'] ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement |
|
| 317 | + Redux_Functions_Ex::enqueue_font_awesome(); |
|
| 318 | + } |
|
| 319 | + } |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Enqueue Function. |
|
| 325 | + * If this field requires any scripts, or css define this function and register/enqueue the scripts/css |
|
| 326 | + * |
|
| 327 | + * @since 1.0.0 |
|
| 328 | + * @access public |
|
| 329 | + * @return void |
|
| 330 | + */ |
|
| 331 | + public function enqueue() { |
|
| 332 | + if ( empty( $this->field ) ) { |
|
| 333 | + return; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + $min = Redux_Functions::isMin(); |
|
| 337 | + |
|
| 338 | + Redux_Functions_Ex::enqueue_font_awesome(); |
|
| 339 | + |
|
| 340 | + // Field dependent JS. |
|
| 341 | + wp_enqueue_script( |
|
| 342 | + 'redux-field-social-profiles', |
|
| 343 | + $this->url . 'redux-social-profiles' . $min . '.js', |
|
| 344 | + array( 'jquery', 'jquery-ui-sortable', 'redux-spectrum-js', 'redux-js' ), |
|
| 345 | + Redux_Extension_Social_Profiles::$version, |
|
| 346 | + true |
|
| 347 | + ); |
|
| 348 | + |
|
| 349 | + wp_localize_script( |
|
| 350 | + 'redux-field-social-profiles', |
|
| 351 | + 'reduxSocialDefaults', |
|
| 352 | + $this->defaults |
|
| 353 | + ); |
|
| 354 | + |
|
| 355 | + if ( $this->parent->args['dev_mode'] ) { |
|
| 356 | + wp_enqueue_style( |
|
| 357 | + 'redux-field-social-profiles', |
|
| 358 | + $this->url . 'redux-social-profiles.css', |
|
| 359 | + array( 'redux-spectrum-css' ), |
|
| 360 | + Redux_Extension_Social_Profiles::$version |
|
| 361 | + ); |
|
| 362 | + } |
|
| 363 | + } |
|
| 364 | + } |
|
| 365 | 365 | } |
@@ -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 | |
@@ -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_string( $args ) && function_exists( $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_string( $args ) && function_exists( $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 | } |
@@ -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 |
@@ -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' ); |