helper
5 years ago
importers
5 years ago
list-tables
5 years ago
marketplace-suggestions
5 years ago
meta-boxes
5 years ago
notes
5 years ago
plugin-updates
5 years ago
reports
5 years ago
settings
5 years ago
views
5 years ago
class-wc-admin-addons.php
5 years ago
class-wc-admin-api-keys-table-list.php
6 years ago
class-wc-admin-api-keys.php
6 years ago
class-wc-admin-assets.php
5 years ago
class-wc-admin-attributes.php
5 years ago
class-wc-admin-customize.php
5 years ago
class-wc-admin-dashboard.php
5 years ago
class-wc-admin-duplicate-product.php
5 years ago
class-wc-admin-exporters.php
5 years ago
class-wc-admin-help.php
5 years ago
class-wc-admin-importers.php
5 years ago
class-wc-admin-log-table-list.php
5 years ago
class-wc-admin-menus.php
5 years ago
class-wc-admin-meta-boxes.php
5 years ago
class-wc-admin-notices.php
5 years ago
class-wc-admin-permalink-settings.php
5 years ago
class-wc-admin-pointers.php
5 years ago
class-wc-admin-post-types.php
5 years ago
class-wc-admin-profile.php
5 years ago
class-wc-admin-reports.php
5 years ago
class-wc-admin-settings.php
5 years ago
class-wc-admin-setup-wizard.php
5 years ago
class-wc-admin-status.php
5 years ago
class-wc-admin-taxonomies.php
5 years ago
class-wc-admin-webhooks-table-list.php
7 years ago
class-wc-admin-webhooks.php
5 years ago
class-wc-admin.php
5 years ago
wc-admin-functions.php
5 years ago
wc-meta-box-functions.php
5 years ago
class-wc-admin-settings.php
901 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Admin Settings Class |
| 4 | * |
| 5 | * @package WooCommerce\Admin |
| 6 | * @version 3.4.0 |
| 7 | */ |
| 8 | |
| 9 | use Automattic\Jetpack\Constants; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | if ( ! class_exists( 'WC_Admin_Settings', false ) ) : |
| 16 | |
| 17 | /** |
| 18 | * WC_Admin_Settings Class. |
| 19 | */ |
| 20 | class WC_Admin_Settings { |
| 21 | |
| 22 | /** |
| 23 | * Setting pages. |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | private static $settings = array(); |
| 28 | |
| 29 | /** |
| 30 | * Error messages. |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | private static $errors = array(); |
| 35 | |
| 36 | /** |
| 37 | * Update messages. |
| 38 | * |
| 39 | * @var array |
| 40 | */ |
| 41 | private static $messages = array(); |
| 42 | |
| 43 | /** |
| 44 | * Include the settings page classes. |
| 45 | */ |
| 46 | public static function get_settings_pages() { |
| 47 | if ( empty( self::$settings ) ) { |
| 48 | $settings = array(); |
| 49 | |
| 50 | include_once dirname( __FILE__ ) . '/settings/class-wc-settings-page.php'; |
| 51 | |
| 52 | $settings[] = include __DIR__ . '/settings/class-wc-settings-general.php'; |
| 53 | $settings[] = include __DIR__ . '/settings/class-wc-settings-products.php'; |
| 54 | $settings[] = include __DIR__ . '/settings/class-wc-settings-tax.php'; |
| 55 | $settings[] = include __DIR__ . '/settings/class-wc-settings-shipping.php'; |
| 56 | $settings[] = include __DIR__ . '/settings/class-wc-settings-payment-gateways.php'; |
| 57 | $settings[] = include __DIR__ . '/settings/class-wc-settings-accounts.php'; |
| 58 | $settings[] = include __DIR__ . '/settings/class-wc-settings-emails.php'; |
| 59 | $settings[] = include __DIR__ . '/settings/class-wc-settings-integrations.php'; |
| 60 | $settings[] = include __DIR__ . '/settings/class-wc-settings-advanced.php'; |
| 61 | |
| 62 | self::$settings = apply_filters( 'woocommerce_get_settings_pages', $settings ); |
| 63 | } |
| 64 | |
| 65 | return self::$settings; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Save the settings. |
| 70 | */ |
| 71 | public static function save() { |
| 72 | global $current_tab; |
| 73 | |
| 74 | check_admin_referer( 'woocommerce-settings' ); |
| 75 | |
| 76 | // Trigger actions. |
| 77 | do_action( 'woocommerce_settings_save_' . $current_tab ); |
| 78 | do_action( 'woocommerce_update_options_' . $current_tab ); |
| 79 | do_action( 'woocommerce_update_options' ); |
| 80 | |
| 81 | self::add_message( __( 'Your settings have been saved.', 'woocommerce' ) ); |
| 82 | self::check_download_folder_protection(); |
| 83 | |
| 84 | // Clear any unwanted data and flush rules. |
| 85 | update_option( 'woocommerce_queue_flush_rewrite_rules', 'yes' ); |
| 86 | WC()->query->init_query_vars(); |
| 87 | WC()->query->add_endpoints(); |
| 88 | |
| 89 | do_action( 'woocommerce_settings_saved' ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Add a message. |
| 94 | * |
| 95 | * @param string $text Message. |
| 96 | */ |
| 97 | public static function add_message( $text ) { |
| 98 | self::$messages[] = $text; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Add an error. |
| 103 | * |
| 104 | * @param string $text Message. |
| 105 | */ |
| 106 | public static function add_error( $text ) { |
| 107 | self::$errors[] = $text; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Output messages + errors. |
| 112 | */ |
| 113 | public static function show_messages() { |
| 114 | if ( count( self::$errors ) > 0 ) { |
| 115 | foreach ( self::$errors as $error ) { |
| 116 | echo '<div id="message" class="error inline"><p><strong>' . esc_html( $error ) . '</strong></p></div>'; |
| 117 | } |
| 118 | } elseif ( count( self::$messages ) > 0 ) { |
| 119 | foreach ( self::$messages as $message ) { |
| 120 | echo '<div id="message" class="updated inline"><p><strong>' . esc_html( $message ) . '</strong></p></div>'; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Settings page. |
| 127 | * |
| 128 | * Handles the display of the main woocommerce settings page in admin. |
| 129 | */ |
| 130 | public static function output() { |
| 131 | global $current_section, $current_tab; |
| 132 | |
| 133 | $suffix = Constants::is_true( 'SCRIPT_DEBUG' ) ? '' : '.min'; |
| 134 | |
| 135 | do_action( 'woocommerce_settings_start' ); |
| 136 | |
| 137 | wp_enqueue_script( 'woocommerce_settings', WC()->plugin_url() . '/assets/js/admin/settings' . $suffix . '.js', array( 'jquery', 'wp-util', 'jquery-ui-datepicker', 'jquery-ui-sortable', 'iris', 'selectWoo' ), WC()->version, true ); |
| 138 | |
| 139 | wp_localize_script( |
| 140 | 'woocommerce_settings', |
| 141 | 'woocommerce_settings_params', |
| 142 | array( |
| 143 | 'i18n_nav_warning' => __( 'The changes you made will be lost if you navigate away from this page.', 'woocommerce' ), |
| 144 | 'i18n_moved_up' => __( 'Item moved up', 'woocommerce' ), |
| 145 | 'i18n_moved_down' => __( 'Item moved down', 'woocommerce' ), |
| 146 | 'i18n_no_specific_countries_selected' => __( 'Selecting no country / region to sell to prevents from completing the checkout. Continue anyway?', 'woocommerce' ), |
| 147 | ) |
| 148 | ); |
| 149 | |
| 150 | // Get tabs for the settings page. |
| 151 | $tabs = apply_filters( 'woocommerce_settings_tabs_array', array() ); |
| 152 | |
| 153 | include dirname( __FILE__ ) . '/views/html-admin-settings.php'; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Get a setting from the settings API. |
| 158 | * |
| 159 | * @param string $option_name Option name. |
| 160 | * @param mixed $default Default value. |
| 161 | * @return mixed |
| 162 | */ |
| 163 | public static function get_option( $option_name, $default = '' ) { |
| 164 | if ( ! $option_name ) { |
| 165 | return $default; |
| 166 | } |
| 167 | |
| 168 | // Array value. |
| 169 | if ( strstr( $option_name, '[' ) ) { |
| 170 | |
| 171 | parse_str( $option_name, $option_array ); |
| 172 | |
| 173 | // Option name is first key. |
| 174 | $option_name = current( array_keys( $option_array ) ); |
| 175 | |
| 176 | // Get value. |
| 177 | $option_values = get_option( $option_name, '' ); |
| 178 | |
| 179 | $key = key( $option_array[ $option_name ] ); |
| 180 | |
| 181 | if ( isset( $option_values[ $key ] ) ) { |
| 182 | $option_value = $option_values[ $key ]; |
| 183 | } else { |
| 184 | $option_value = null; |
| 185 | } |
| 186 | } else { |
| 187 | // Single value. |
| 188 | $option_value = get_option( $option_name, null ); |
| 189 | } |
| 190 | |
| 191 | if ( is_array( $option_value ) ) { |
| 192 | $option_value = wp_unslash( $option_value ); |
| 193 | } elseif ( ! is_null( $option_value ) ) { |
| 194 | $option_value = stripslashes( $option_value ); |
| 195 | } |
| 196 | |
| 197 | return ( null === $option_value ) ? $default : $option_value; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Output admin fields. |
| 202 | * |
| 203 | * Loops through the woocommerce options array and outputs each field. |
| 204 | * |
| 205 | * @param array[] $options Opens array to output. |
| 206 | */ |
| 207 | public static function output_fields( $options ) { |
| 208 | foreach ( $options as $value ) { |
| 209 | if ( ! isset( $value['type'] ) ) { |
| 210 | continue; |
| 211 | } |
| 212 | if ( ! isset( $value['id'] ) ) { |
| 213 | $value['id'] = ''; |
| 214 | } |
| 215 | if ( ! isset( $value['title'] ) ) { |
| 216 | $value['title'] = isset( $value['name'] ) ? $value['name'] : ''; |
| 217 | } |
| 218 | if ( ! isset( $value['class'] ) ) { |
| 219 | $value['class'] = ''; |
| 220 | } |
| 221 | if ( ! isset( $value['css'] ) ) { |
| 222 | $value['css'] = ''; |
| 223 | } |
| 224 | if ( ! isset( $value['default'] ) ) { |
| 225 | $value['default'] = ''; |
| 226 | } |
| 227 | if ( ! isset( $value['desc'] ) ) { |
| 228 | $value['desc'] = ''; |
| 229 | } |
| 230 | if ( ! isset( $value['desc_tip'] ) ) { |
| 231 | $value['desc_tip'] = false; |
| 232 | } |
| 233 | if ( ! isset( $value['placeholder'] ) ) { |
| 234 | $value['placeholder'] = ''; |
| 235 | } |
| 236 | if ( ! isset( $value['suffix'] ) ) { |
| 237 | $value['suffix'] = ''; |
| 238 | } |
| 239 | if ( ! isset( $value['value'] ) ) { |
| 240 | $value['value'] = self::get_option( $value['id'], $value['default'] ); |
| 241 | } |
| 242 | |
| 243 | // Custom attribute handling. |
| 244 | $custom_attributes = array(); |
| 245 | |
| 246 | if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) ) { |
| 247 | foreach ( $value['custom_attributes'] as $attribute => $attribute_value ) { |
| 248 | $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // Description handling. |
| 253 | $field_description = self::get_field_description( $value ); |
| 254 | $description = $field_description['description']; |
| 255 | $tooltip_html = $field_description['tooltip_html']; |
| 256 | |
| 257 | // Switch based on type. |
| 258 | switch ( $value['type'] ) { |
| 259 | |
| 260 | // Section Titles. |
| 261 | case 'title': |
| 262 | if ( ! empty( $value['title'] ) ) { |
| 263 | echo '<h2>' . esc_html( $value['title'] ) . '</h2>'; |
| 264 | } |
| 265 | if ( ! empty( $value['desc'] ) ) { |
| 266 | echo '<div id="' . esc_attr( sanitize_title( $value['id'] ) ) . '-description">'; |
| 267 | echo wp_kses_post( wpautop( wptexturize( $value['desc'] ) ) ); |
| 268 | echo '</div>'; |
| 269 | } |
| 270 | echo '<table class="form-table">' . "\n\n"; |
| 271 | if ( ! empty( $value['id'] ) ) { |
| 272 | do_action( 'woocommerce_settings_' . sanitize_title( $value['id'] ) ); |
| 273 | } |
| 274 | break; |
| 275 | |
| 276 | // Section Ends. |
| 277 | case 'sectionend': |
| 278 | if ( ! empty( $value['id'] ) ) { |
| 279 | do_action( 'woocommerce_settings_' . sanitize_title( $value['id'] ) . '_end' ); |
| 280 | } |
| 281 | echo '</table>'; |
| 282 | if ( ! empty( $value['id'] ) ) { |
| 283 | do_action( 'woocommerce_settings_' . sanitize_title( $value['id'] ) . '_after' ); |
| 284 | } |
| 285 | break; |
| 286 | |
| 287 | // Standard text inputs and subtypes like 'number'. |
| 288 | case 'text': |
| 289 | case 'password': |
| 290 | case 'datetime': |
| 291 | case 'datetime-local': |
| 292 | case 'date': |
| 293 | case 'month': |
| 294 | case 'time': |
| 295 | case 'week': |
| 296 | case 'number': |
| 297 | case 'email': |
| 298 | case 'url': |
| 299 | case 'tel': |
| 300 | $option_value = $value['value']; |
| 301 | |
| 302 | ?><tr valign="top"> |
| 303 | <th scope="row" class="titledesc"> |
| 304 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // WPCS: XSS ok. ?></label> |
| 305 | </th> |
| 306 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 307 | <input |
| 308 | name="<?php echo esc_attr( $value['id'] ); ?>" |
| 309 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 310 | type="<?php echo esc_attr( $value['type'] ); ?>" |
| 311 | style="<?php echo esc_attr( $value['css'] ); ?>" |
| 312 | value="<?php echo esc_attr( $option_value ); ?>" |
| 313 | class="<?php echo esc_attr( $value['class'] ); ?>" |
| 314 | placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>" |
| 315 | <?php echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?> |
| 316 | /><?php echo esc_html( $value['suffix'] ); ?> <?php echo $description; // WPCS: XSS ok. ?> |
| 317 | </td> |
| 318 | </tr> |
| 319 | <?php |
| 320 | break; |
| 321 | |
| 322 | // Color picker. |
| 323 | case 'color': |
| 324 | $option_value = $value['value']; |
| 325 | |
| 326 | ?> |
| 327 | <tr valign="top"> |
| 328 | <th scope="row" class="titledesc"> |
| 329 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // WPCS: XSS ok. ?></label> |
| 330 | </th> |
| 331 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>">‎ |
| 332 | <span class="colorpickpreview" style="background: <?php echo esc_attr( $option_value ); ?>"> </span> |
| 333 | <input |
| 334 | name="<?php echo esc_attr( $value['id'] ); ?>" |
| 335 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 336 | type="text" |
| 337 | dir="ltr" |
| 338 | style="<?php echo esc_attr( $value['css'] ); ?>" |
| 339 | value="<?php echo esc_attr( $option_value ); ?>" |
| 340 | class="<?php echo esc_attr( $value['class'] ); ?>colorpick" |
| 341 | placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>" |
| 342 | <?php echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?> |
| 343 | />‎ <?php echo $description; // WPCS: XSS ok. ?> |
| 344 | <div id="colorPickerDiv_<?php echo esc_attr( $value['id'] ); ?>" class="colorpickdiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"></div> |
| 345 | </td> |
| 346 | </tr> |
| 347 | <?php |
| 348 | break; |
| 349 | |
| 350 | // Textarea. |
| 351 | case 'textarea': |
| 352 | $option_value = $value['value']; |
| 353 | |
| 354 | ?> |
| 355 | <tr valign="top"> |
| 356 | <th scope="row" class="titledesc"> |
| 357 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // WPCS: XSS ok. ?></label> |
| 358 | </th> |
| 359 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 360 | <?php echo $description; // WPCS: XSS ok. ?> |
| 361 | |
| 362 | <textarea |
| 363 | name="<?php echo esc_attr( $value['id'] ); ?>" |
| 364 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 365 | style="<?php echo esc_attr( $value['css'] ); ?>" |
| 366 | class="<?php echo esc_attr( $value['class'] ); ?>" |
| 367 | placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>" |
| 368 | <?php echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?> |
| 369 | ><?php echo esc_textarea( $option_value ); // WPCS: XSS ok. ?></textarea> |
| 370 | </td> |
| 371 | </tr> |
| 372 | <?php |
| 373 | break; |
| 374 | |
| 375 | // Select boxes. |
| 376 | case 'select': |
| 377 | case 'multiselect': |
| 378 | $option_value = $value['value']; |
| 379 | |
| 380 | ?> |
| 381 | <tr valign="top"> |
| 382 | <th scope="row" class="titledesc"> |
| 383 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // WPCS: XSS ok. ?></label> |
| 384 | </th> |
| 385 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 386 | <select |
| 387 | name="<?php echo esc_attr( $value['id'] ); ?><?php echo ( 'multiselect' === $value['type'] ) ? '[]' : ''; ?>" |
| 388 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 389 | style="<?php echo esc_attr( $value['css'] ); ?>" |
| 390 | class="<?php echo esc_attr( $value['class'] ); ?>" |
| 391 | <?php echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?> |
| 392 | <?php echo 'multiselect' === $value['type'] ? 'multiple="multiple"' : ''; ?> |
| 393 | > |
| 394 | <?php |
| 395 | foreach ( $value['options'] as $key => $val ) { |
| 396 | ?> |
| 397 | <option value="<?php echo esc_attr( $key ); ?>" |
| 398 | <?php |
| 399 | |
| 400 | if ( is_array( $option_value ) ) { |
| 401 | selected( in_array( (string) $key, $option_value, true ), true ); |
| 402 | } else { |
| 403 | selected( $option_value, (string) $key ); |
| 404 | } |
| 405 | |
| 406 | ?> |
| 407 | ><?php echo esc_html( $val ); ?></option> |
| 408 | <?php |
| 409 | } |
| 410 | ?> |
| 411 | </select> <?php echo $description; // WPCS: XSS ok. ?> |
| 412 | </td> |
| 413 | </tr> |
| 414 | <?php |
| 415 | break; |
| 416 | |
| 417 | // Radio inputs. |
| 418 | case 'radio': |
| 419 | $option_value = $value['value']; |
| 420 | |
| 421 | ?> |
| 422 | <tr valign="top"> |
| 423 | <th scope="row" class="titledesc"> |
| 424 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // WPCS: XSS ok. ?></label> |
| 425 | </th> |
| 426 | <td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 427 | <fieldset> |
| 428 | <?php echo $description; // WPCS: XSS ok. ?> |
| 429 | <ul> |
| 430 | <?php |
| 431 | foreach ( $value['options'] as $key => $val ) { |
| 432 | ?> |
| 433 | <li> |
| 434 | <label><input |
| 435 | name="<?php echo esc_attr( $value['id'] ); ?>" |
| 436 | value="<?php echo esc_attr( $key ); ?>" |
| 437 | type="radio" |
| 438 | style="<?php echo esc_attr( $value['css'] ); ?>" |
| 439 | class="<?php echo esc_attr( $value['class'] ); ?>" |
| 440 | <?php echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?> |
| 441 | <?php checked( $key, $option_value ); ?> |
| 442 | /> <?php echo esc_html( $val ); ?></label> |
| 443 | </li> |
| 444 | <?php |
| 445 | } |
| 446 | ?> |
| 447 | </ul> |
| 448 | </fieldset> |
| 449 | </td> |
| 450 | </tr> |
| 451 | <?php |
| 452 | break; |
| 453 | |
| 454 | // Checkbox input. |
| 455 | case 'checkbox': |
| 456 | $option_value = $value['value']; |
| 457 | $visibility_class = array(); |
| 458 | |
| 459 | if ( ! isset( $value['hide_if_checked'] ) ) { |
| 460 | $value['hide_if_checked'] = false; |
| 461 | } |
| 462 | if ( ! isset( $value['show_if_checked'] ) ) { |
| 463 | $value['show_if_checked'] = false; |
| 464 | } |
| 465 | if ( 'yes' === $value['hide_if_checked'] || 'yes' === $value['show_if_checked'] ) { |
| 466 | $visibility_class[] = 'hidden_option'; |
| 467 | } |
| 468 | if ( 'option' === $value['hide_if_checked'] ) { |
| 469 | $visibility_class[] = 'hide_options_if_checked'; |
| 470 | } |
| 471 | if ( 'option' === $value['show_if_checked'] ) { |
| 472 | $visibility_class[] = 'show_options_if_checked'; |
| 473 | } |
| 474 | |
| 475 | if ( ! isset( $value['checkboxgroup'] ) || 'start' === $value['checkboxgroup'] ) { |
| 476 | ?> |
| 477 | <tr valign="top" class="<?php echo esc_attr( implode( ' ', $visibility_class ) ); ?>"> |
| 478 | <th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ); ?></th> |
| 479 | <td class="forminp forminp-checkbox"> |
| 480 | <fieldset> |
| 481 | <?php |
| 482 | } else { |
| 483 | ?> |
| 484 | <fieldset class="<?php echo esc_attr( implode( ' ', $visibility_class ) ); ?>"> |
| 485 | <?php |
| 486 | } |
| 487 | |
| 488 | if ( ! empty( $value['title'] ) ) { |
| 489 | ?> |
| 490 | <legend class="screen-reader-text"><span><?php echo esc_html( $value['title'] ); ?></span></legend> |
| 491 | <?php |
| 492 | } |
| 493 | |
| 494 | ?> |
| 495 | <label for="<?php echo esc_attr( $value['id'] ); ?>"> |
| 496 | <input |
| 497 | name="<?php echo esc_attr( $value['id'] ); ?>" |
| 498 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 499 | type="checkbox" |
| 500 | class="<?php echo esc_attr( isset( $value['class'] ) ? $value['class'] : '' ); ?>" |
| 501 | value="1" |
| 502 | <?php checked( $option_value, 'yes' ); ?> |
| 503 | <?php echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?> |
| 504 | /> <?php echo $description; // WPCS: XSS ok. ?> |
| 505 | </label> <?php echo $tooltip_html; // WPCS: XSS ok. ?> |
| 506 | <?php |
| 507 | |
| 508 | if ( ! isset( $value['checkboxgroup'] ) || 'end' === $value['checkboxgroup'] ) { |
| 509 | ?> |
| 510 | </fieldset> |
| 511 | </td> |
| 512 | </tr> |
| 513 | <?php |
| 514 | } else { |
| 515 | ?> |
| 516 | </fieldset> |
| 517 | <?php |
| 518 | } |
| 519 | break; |
| 520 | |
| 521 | // Image width settings. @todo deprecate and remove in 4.0. No longer needed by core. |
| 522 | case 'image_width': |
| 523 | $image_size = str_replace( '_image_size', '', $value['id'] ); |
| 524 | $size = wc_get_image_size( $image_size ); |
| 525 | $width = isset( $size['width'] ) ? $size['width'] : $value['default']['width']; |
| 526 | $height = isset( $size['height'] ) ? $size['height'] : $value['default']['height']; |
| 527 | $crop = isset( $size['crop'] ) ? $size['crop'] : $value['default']['crop']; |
| 528 | $disabled_attr = ''; |
| 529 | $disabled_message = ''; |
| 530 | |
| 531 | if ( has_filter( 'woocommerce_get_image_size_' . $image_size ) ) { |
| 532 | $disabled_attr = 'disabled="disabled"'; |
| 533 | $disabled_message = '<p><small>' . esc_html__( 'The settings of this image size have been disabled because its values are being overwritten by a filter.', 'woocommerce' ) . '</small></p>'; |
| 534 | } |
| 535 | |
| 536 | ?> |
| 537 | <tr valign="top"> |
| 538 | <th scope="row" class="titledesc"> |
| 539 | <label><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html . $disabled_message; // WPCS: XSS ok. ?></label> |
| 540 | </th> |
| 541 | <td class="forminp image_width_settings"> |
| 542 | |
| 543 | <input name="<?php echo esc_attr( $value['id'] ); ?>[width]" <?php echo $disabled_attr; // WPCS: XSS ok. ?> id="<?php echo esc_attr( $value['id'] ); ?>-width" type="text" size="3" value="<?php echo esc_attr( $width ); ?>" /> × <input name="<?php echo esc_attr( $value['id'] ); ?>[height]" <?php echo $disabled_attr; // WPCS: XSS ok. ?> id="<?php echo esc_attr( $value['id'] ); ?>-height" type="text" size="3" value="<?php echo esc_attr( $height ); ?>" />px |
| 544 | |
| 545 | <label><input name="<?php echo esc_attr( $value['id'] ); ?>[crop]" <?php echo $disabled_attr; // WPCS: XSS ok. ?> id="<?php echo esc_attr( $value['id'] ); ?>-crop" type="checkbox" value="1" <?php checked( 1, $crop ); ?> /> <?php esc_html_e( 'Hard crop?', 'woocommerce' ); ?></label> |
| 546 | |
| 547 | </td> |
| 548 | </tr> |
| 549 | <?php |
| 550 | break; |
| 551 | |
| 552 | // Single page selects. |
| 553 | case 'single_select_page': |
| 554 | $args = array( |
| 555 | 'name' => $value['id'], |
| 556 | 'id' => $value['id'], |
| 557 | 'sort_column' => 'menu_order', |
| 558 | 'sort_order' => 'ASC', |
| 559 | 'show_option_none' => ' ', |
| 560 | 'class' => $value['class'], |
| 561 | 'echo' => false, |
| 562 | 'selected' => absint( $value['value'] ), |
| 563 | 'post_status' => 'publish,private,draft', |
| 564 | ); |
| 565 | |
| 566 | if ( isset( $value['args'] ) ) { |
| 567 | $args = wp_parse_args( $value['args'], $args ); |
| 568 | } |
| 569 | |
| 570 | ?> |
| 571 | <tr valign="top" class="single_select_page"> |
| 572 | <th scope="row" class="titledesc"> |
| 573 | <label><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // WPCS: XSS ok. ?></label> |
| 574 | </th> |
| 575 | <td class="forminp"> |
| 576 | <?php echo str_replace( ' id=', " data-placeholder='" . esc_attr__( 'Select a page…', 'woocommerce' ) . "' style='" . $value['css'] . "' class='" . $value['class'] . "' id=", wp_dropdown_pages( $args ) ); // WPCS: XSS ok. ?> <?php echo $description; // WPCS: XSS ok. ?> |
| 577 | </td> |
| 578 | </tr> |
| 579 | <?php |
| 580 | break; |
| 581 | |
| 582 | // Single country selects. |
| 583 | case 'single_select_country': |
| 584 | $country_setting = (string) $value['value']; |
| 585 | |
| 586 | if ( strstr( $country_setting, ':' ) ) { |
| 587 | $country_setting = explode( ':', $country_setting ); |
| 588 | $country = current( $country_setting ); |
| 589 | $state = end( $country_setting ); |
| 590 | } else { |
| 591 | $country = $country_setting; |
| 592 | $state = '*'; |
| 593 | } |
| 594 | ?> |
| 595 | <tr valign="top"> |
| 596 | <th scope="row" class="titledesc"> |
| 597 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // WPCS: XSS ok. ?></label> |
| 598 | </th> |
| 599 | <td class="forminp"><select name="<?php echo esc_attr( $value['id'] ); ?>" style="<?php echo esc_attr( $value['css'] ); ?>" data-placeholder="<?php esc_attr_e( 'Choose a country / region…', 'woocommerce' ); ?>" aria-label="<?php esc_attr_e( 'Country / Region', 'woocommerce' ); ?>" class="wc-enhanced-select"> |
| 600 | <?php WC()->countries->country_dropdown_options( $country, $state ); ?> |
| 601 | </select> <?php echo $description; // WPCS: XSS ok. ?> |
| 602 | </td> |
| 603 | </tr> |
| 604 | <?php |
| 605 | break; |
| 606 | |
| 607 | // Country multiselects. |
| 608 | case 'multi_select_countries': |
| 609 | $selections = (array) $value['value']; |
| 610 | |
| 611 | if ( ! empty( $value['options'] ) ) { |
| 612 | $countries = $value['options']; |
| 613 | } else { |
| 614 | $countries = WC()->countries->countries; |
| 615 | } |
| 616 | |
| 617 | asort( $countries ); |
| 618 | ?> |
| 619 | <tr valign="top"> |
| 620 | <th scope="row" class="titledesc"> |
| 621 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // WPCS: XSS ok. ?></label> |
| 622 | </th> |
| 623 | <td class="forminp"> |
| 624 | <select multiple="multiple" name="<?php echo esc_attr( $value['id'] ); ?>[]" style="width:350px" data-placeholder="<?php esc_attr_e( 'Choose countries / regions…', 'woocommerce' ); ?>" aria-label="<?php esc_attr_e( 'Country / Region', 'woocommerce' ); ?>" class="wc-enhanced-select"> |
| 625 | <?php |
| 626 | if ( ! empty( $countries ) ) { |
| 627 | foreach ( $countries as $key => $val ) { |
| 628 | echo '<option value="' . esc_attr( $key ) . '"' . wc_selected( $key, $selections ) . '>' . esc_html( $val ) . '</option>'; // WPCS: XSS ok. |
| 629 | } |
| 630 | } |
| 631 | ?> |
| 632 | </select> <?php echo ( $description ) ? $description : ''; // WPCS: XSS ok. ?> <br /><a class="select_all button" href="#"><?php esc_html_e( 'Select all', 'woocommerce' ); ?></a> <a class="select_none button" href="#"><?php esc_html_e( 'Select none', 'woocommerce' ); ?></a> |
| 633 | </td> |
| 634 | </tr> |
| 635 | <?php |
| 636 | break; |
| 637 | |
| 638 | // Days/months/years selector. |
| 639 | case 'relative_date_selector': |
| 640 | $periods = array( |
| 641 | 'days' => __( 'Day(s)', 'woocommerce' ), |
| 642 | 'weeks' => __( 'Week(s)', 'woocommerce' ), |
| 643 | 'months' => __( 'Month(s)', 'woocommerce' ), |
| 644 | 'years' => __( 'Year(s)', 'woocommerce' ), |
| 645 | ); |
| 646 | $option_value = wc_parse_relative_date_option( $value['value'] ); |
| 647 | ?> |
| 648 | <tr valign="top"> |
| 649 | <th scope="row" class="titledesc"> |
| 650 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; // WPCS: XSS ok. ?></label> |
| 651 | </th> |
| 652 | <td class="forminp"> |
| 653 | <input |
| 654 | name="<?php echo esc_attr( $value['id'] ); ?>[number]" |
| 655 | id="<?php echo esc_attr( $value['id'] ); ?>" |
| 656 | type="number" |
| 657 | style="width: 80px;" |
| 658 | value="<?php echo esc_attr( $option_value['number'] ); ?>" |
| 659 | class="<?php echo esc_attr( $value['class'] ); ?>" |
| 660 | placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>" |
| 661 | step="1" |
| 662 | min="1" |
| 663 | <?php echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?> |
| 664 | /> |
| 665 | <select name="<?php echo esc_attr( $value['id'] ); ?>[unit]" style="width: auto;"> |
| 666 | <?php |
| 667 | foreach ( $periods as $value => $label ) { |
| 668 | echo '<option value="' . esc_attr( $value ) . '"' . selected( $option_value['unit'], $value, false ) . '>' . esc_html( $label ) . '</option>'; |
| 669 | } |
| 670 | ?> |
| 671 | </select> <?php echo ( $description ) ? $description : ''; // WPCS: XSS ok. ?> |
| 672 | </td> |
| 673 | </tr> |
| 674 | <?php |
| 675 | break; |
| 676 | |
| 677 | // Default: run an action. |
| 678 | default: |
| 679 | do_action( 'woocommerce_admin_field_' . $value['type'], $value ); |
| 680 | break; |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * Helper function to get the formatted description and tip HTML for a |
| 687 | * given form field. Plugins can call this when implementing their own custom |
| 688 | * settings types. |
| 689 | * |
| 690 | * @param array $value The form field value array. |
| 691 | * @return array The description and tip as a 2 element array. |
| 692 | */ |
| 693 | public static function get_field_description( $value ) { |
| 694 | $description = ''; |
| 695 | $tooltip_html = ''; |
| 696 | |
| 697 | if ( true === $value['desc_tip'] ) { |
| 698 | $tooltip_html = $value['desc']; |
| 699 | } elseif ( ! empty( $value['desc_tip'] ) ) { |
| 700 | $description = $value['desc']; |
| 701 | $tooltip_html = $value['desc_tip']; |
| 702 | } elseif ( ! empty( $value['desc'] ) ) { |
| 703 | $description = $value['desc']; |
| 704 | } |
| 705 | |
| 706 | if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ), true ) ) { |
| 707 | $description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>'; |
| 708 | } elseif ( $description && in_array( $value['type'], array( 'checkbox' ), true ) ) { |
| 709 | $description = wp_kses_post( $description ); |
| 710 | } elseif ( $description ) { |
| 711 | $description = '<p class="description">' . wp_kses_post( $description ) . '</p>'; |
| 712 | } |
| 713 | |
| 714 | if ( $tooltip_html && in_array( $value['type'], array( 'checkbox' ), true ) ) { |
| 715 | $tooltip_html = '<p class="description">' . $tooltip_html . '</p>'; |
| 716 | } elseif ( $tooltip_html ) { |
| 717 | $tooltip_html = wc_help_tip( $tooltip_html ); |
| 718 | } |
| 719 | |
| 720 | return array( |
| 721 | 'description' => $description, |
| 722 | 'tooltip_html' => $tooltip_html, |
| 723 | ); |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Save admin fields. |
| 728 | * |
| 729 | * Loops through the woocommerce options array and outputs each field. |
| 730 | * |
| 731 | * @param array $options Options array to output. |
| 732 | * @param array $data Optional. Data to use for saving. Defaults to $_POST. |
| 733 | * @return bool |
| 734 | */ |
| 735 | public static function save_fields( $options, $data = null ) { |
| 736 | if ( is_null( $data ) ) { |
| 737 | $data = $_POST; // WPCS: input var okay, CSRF ok. |
| 738 | } |
| 739 | if ( empty( $data ) ) { |
| 740 | return false; |
| 741 | } |
| 742 | |
| 743 | // Options to update will be stored here and saved later. |
| 744 | $update_options = array(); |
| 745 | $autoload_options = array(); |
| 746 | |
| 747 | // Loop options and get values to save. |
| 748 | foreach ( $options as $option ) { |
| 749 | if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) || ( isset( $option['is_option'] ) && false === $option['is_option'] ) ) { |
| 750 | continue; |
| 751 | } |
| 752 | |
| 753 | // Get posted value. |
| 754 | if ( strstr( $option['id'], '[' ) ) { |
| 755 | parse_str( $option['id'], $option_name_array ); |
| 756 | $option_name = current( array_keys( $option_name_array ) ); |
| 757 | $setting_name = key( $option_name_array[ $option_name ] ); |
| 758 | $raw_value = isset( $data[ $option_name ][ $setting_name ] ) ? wp_unslash( $data[ $option_name ][ $setting_name ] ) : null; |
| 759 | } else { |
| 760 | $option_name = $option['id']; |
| 761 | $setting_name = ''; |
| 762 | $raw_value = isset( $data[ $option['id'] ] ) ? wp_unslash( $data[ $option['id'] ] ) : null; |
| 763 | } |
| 764 | |
| 765 | // Format the value based on option type. |
| 766 | switch ( $option['type'] ) { |
| 767 | case 'checkbox': |
| 768 | $value = '1' === $raw_value || 'yes' === $raw_value ? 'yes' : 'no'; |
| 769 | break; |
| 770 | case 'textarea': |
| 771 | $value = wp_kses_post( trim( $raw_value ) ); |
| 772 | break; |
| 773 | case 'multiselect': |
| 774 | case 'multi_select_countries': |
| 775 | $value = array_filter( array_map( 'wc_clean', (array) $raw_value ) ); |
| 776 | break; |
| 777 | case 'image_width': |
| 778 | $value = array(); |
| 779 | if ( isset( $raw_value['width'] ) ) { |
| 780 | $value['width'] = wc_clean( $raw_value['width'] ); |
| 781 | $value['height'] = wc_clean( $raw_value['height'] ); |
| 782 | $value['crop'] = isset( $raw_value['crop'] ) ? 1 : 0; |
| 783 | } else { |
| 784 | $value['width'] = $option['default']['width']; |
| 785 | $value['height'] = $option['default']['height']; |
| 786 | $value['crop'] = $option['default']['crop']; |
| 787 | } |
| 788 | break; |
| 789 | case 'select': |
| 790 | $allowed_values = empty( $option['options'] ) ? array() : array_map( 'strval', array_keys( $option['options'] ) ); |
| 791 | if ( empty( $option['default'] ) && empty( $allowed_values ) ) { |
| 792 | $value = null; |
| 793 | break; |
| 794 | } |
| 795 | $default = ( empty( $option['default'] ) ? $allowed_values[0] : $option['default'] ); |
| 796 | $value = in_array( $raw_value, $allowed_values, true ) ? $raw_value : $default; |
| 797 | break; |
| 798 | case 'relative_date_selector': |
| 799 | $value = wc_parse_relative_date_option( $raw_value ); |
| 800 | break; |
| 801 | default: |
| 802 | $value = wc_clean( $raw_value ); |
| 803 | break; |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * Fire an action when a certain 'type' of field is being saved. |
| 808 | * |
| 809 | * @deprecated 2.4.0 - doesn't allow manipulation of values! |
| 810 | */ |
| 811 | if ( has_action( 'woocommerce_update_option_' . sanitize_title( $option['type'] ) ) ) { |
| 812 | wc_deprecated_function( 'The woocommerce_update_option_X action', '2.4.0', 'woocommerce_admin_settings_sanitize_option filter' ); |
| 813 | do_action( 'woocommerce_update_option_' . sanitize_title( $option['type'] ), $option ); |
| 814 | continue; |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * Sanitize the value of an option. |
| 819 | * |
| 820 | * @since 2.4.0 |
| 821 | */ |
| 822 | $value = apply_filters( 'woocommerce_admin_settings_sanitize_option', $value, $option, $raw_value ); |
| 823 | |
| 824 | /** |
| 825 | * Sanitize the value of an option by option name. |
| 826 | * |
| 827 | * @since 2.4.0 |
| 828 | */ |
| 829 | $value = apply_filters( "woocommerce_admin_settings_sanitize_option_$option_name", $value, $option, $raw_value ); |
| 830 | |
| 831 | if ( is_null( $value ) ) { |
| 832 | continue; |
| 833 | } |
| 834 | |
| 835 | // Check if option is an array and handle that differently to single values. |
| 836 | if ( $option_name && $setting_name ) { |
| 837 | if ( ! isset( $update_options[ $option_name ] ) ) { |
| 838 | $update_options[ $option_name ] = get_option( $option_name, array() ); |
| 839 | } |
| 840 | if ( ! is_array( $update_options[ $option_name ] ) ) { |
| 841 | $update_options[ $option_name ] = array(); |
| 842 | } |
| 843 | $update_options[ $option_name ][ $setting_name ] = $value; |
| 844 | } else { |
| 845 | $update_options[ $option_name ] = $value; |
| 846 | } |
| 847 | |
| 848 | $autoload_options[ $option_name ] = isset( $option['autoload'] ) ? (bool) $option['autoload'] : true; |
| 849 | |
| 850 | /** |
| 851 | * Fire an action before saved. |
| 852 | * |
| 853 | * @deprecated 2.4.0 - doesn't allow manipulation of values! |
| 854 | */ |
| 855 | do_action( 'woocommerce_update_option', $option ); |
| 856 | } |
| 857 | |
| 858 | // Save all options in our array. |
| 859 | foreach ( $update_options as $name => $value ) { |
| 860 | update_option( $name, $value, $autoload_options[ $name ] ? 'yes' : 'no' ); |
| 861 | } |
| 862 | |
| 863 | return true; |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Checks which method we're using to serve downloads. |
| 868 | * |
| 869 | * If using force or x-sendfile, this ensures the .htaccess is in place. |
| 870 | */ |
| 871 | public static function check_download_folder_protection() { |
| 872 | $upload_dir = wp_get_upload_dir(); |
| 873 | $downloads_path = $upload_dir['basedir'] . '/woocommerce_uploads'; |
| 874 | $download_method = get_option( 'woocommerce_file_download_method' ); |
| 875 | $file_path = $downloads_path . '/.htaccess'; |
| 876 | $file_content = 'redirect' === $download_method ? 'Options -Indexes' : 'deny from all'; |
| 877 | $create = false; |
| 878 | |
| 879 | if ( wp_mkdir_p( $downloads_path ) && ! file_exists( $file_path ) ) { |
| 880 | $create = true; |
| 881 | } else { |
| 882 | $current_content = @file_get_contents( $file_path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 883 | |
| 884 | if ( $current_content !== $file_content ) { |
| 885 | unlink( $file_path ); |
| 886 | $create = true; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | if ( $create ) { |
| 891 | $file_handle = @fopen( $file_path, 'wb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_fopen |
| 892 | if ( $file_handle ) { |
| 893 | fwrite( $file_handle, $file_content ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite |
| 894 | fclose( $file_handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | endif; |
| 901 |