query->init_query_vars(); WC()->query->add_endpoints(); do_action( 'woocommerce_settings_saved' ); } /** * Add a message. * * @param string $text Message. */ public static function add_message( $text ) { self::$messages[] = $text; } /** * Add an error. * * @param string $text Message. */ public static function add_error( $text ) { self::$errors[] = $text; } /** * Output messages + errors. */ public static function show_messages() { if ( count( self::$errors ) > 0 ) { foreach ( self::$errors as $error ) { echo '
' . esc_html( $error ) . '
' . esc_html( $message ) . '
' . wp_kses_post( $description ) . '
'; } if ( $tooltip_html && in_array( $value['type'], array( 'checkbox' ), true ) ) { $tooltip_html = '' . $tooltip_html . '
'; } elseif ( $tooltip_html ) { $tooltip_html = wc_help_tip( $tooltip_html ); } return array( 'description' => $description, 'tooltip_html' => $tooltip_html, ); } /** * Save admin fields. * * Loops through the woocommerce options array and outputs each field. * * @param array $options Options array to output. * @param array $data Optional. Data to use for saving. Defaults to $_POST. * @return bool */ public static function save_fields( $options, $data = null ) { if ( is_null( $data ) ) { $data = $_POST; // WPCS: input var okay, CSRF ok. } if ( empty( $data ) ) { return false; } // Options to update will be stored here and saved later. $update_options = array(); $autoload_options = array(); // Loop options and get values to save. foreach ( $options as $option ) { if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) || ( isset( $option['is_option'] ) && false === $option['is_option'] ) ) { continue; } $option_name = $option['field_name'] ?? $option['id']; // Get posted value. if ( strstr( $option_name, '[' ) ) { parse_str( $option_name, $option_name_array ); $option_name = current( array_keys( $option_name_array ) ); $setting_name = key( $option_name_array[ $option_name ] ); $raw_value = isset( $data[ $option_name ][ $setting_name ] ) ? wp_unslash( $data[ $option_name ][ $setting_name ] ) : null; } else { $setting_name = ''; $raw_value = isset( $data[ $option_name ] ) ? wp_unslash( $data[ $option_name ] ) : null; } // Format the value based on option type. switch ( $option['type'] ) { case 'checkbox': $value = '1' === $raw_value || 'yes' === $raw_value ? 'yes' : 'no'; break; case 'textarea': $value = wp_kses_post( trim( $raw_value ) ); break; case 'multiselect': case 'multi_select_countries': $value = array_filter( array_map( 'wc_clean', (array) $raw_value ) ); break; case 'image_width': $value = array(); if ( isset( $raw_value['width'] ) ) { $value['width'] = wc_clean( $raw_value['width'] ); $value['height'] = wc_clean( $raw_value['height'] ); $value['crop'] = isset( $raw_value['crop'] ) ? 1 : 0; } else { $value['width'] = $option['default']['width']; $value['height'] = $option['default']['height']; $value['crop'] = $option['default']['crop']; } break; case 'select': $allowed_values = empty( $option['options'] ) ? array() : array_map( 'strval', array_keys( $option['options'] ) ); if ( empty( $option['default'] ) && empty( $allowed_values ) ) { $value = null; break; } $default = ( empty( $option['default'] ) ? $allowed_values[0] : $option['default'] ); $value = in_array( $raw_value, $allowed_values, true ) ? $raw_value : $default; break; case 'relative_date_selector': $value = wc_parse_relative_date_option( $raw_value ); break; default: $value = wc_clean( $raw_value ); break; } /** * Fire an action when a certain 'type' of field is being saved. * * @deprecated 2.4.0 - doesn't allow manipulation of values! */ if ( has_action( 'woocommerce_update_option_' . sanitize_title( $option['type'] ) ) ) { wc_deprecated_function( 'The woocommerce_update_option_X action', '2.4.0', 'woocommerce_admin_settings_sanitize_option filter' ); do_action( 'woocommerce_update_option_' . sanitize_title( $option['type'] ), $option ); continue; } /** * Sanitize the value of an option. * * @since 2.4.0 */ $value = apply_filters( 'woocommerce_admin_settings_sanitize_option', $value, $option, $raw_value ); /** * Sanitize the value of an option by option name. * * @since 2.4.0 */ $value = apply_filters( "woocommerce_admin_settings_sanitize_option_$option_name", $value, $option, $raw_value ); if ( is_null( $value ) ) { continue; } // Check if option is an array and handle that differently to single values. if ( $option_name && $setting_name ) { if ( ! isset( $update_options[ $option_name ] ) ) { $update_options[ $option_name ] = get_option( $option_name, array() ); } if ( ! is_array( $update_options[ $option_name ] ) ) { $update_options[ $option_name ] = array(); } $update_options[ $option_name ][ $setting_name ] = $value; } else { $update_options[ $option_name ] = $value; } $autoload_options[ $option_name ] = isset( $option['autoload'] ) ? (bool) $option['autoload'] : true; /** * Fire an action before saved. * * @deprecated 2.4.0 - doesn't allow manipulation of values! */ do_action( 'woocommerce_update_option', $option ); } // Save all options in our array. foreach ( $update_options as $name => $value ) { update_option( $name, $value, $autoload_options[ $name ] ? 'yes' : 'no' ); } return true; } /** * Checks which method we're using to serve downloads. * * If using force or x-sendfile, this ensures the .htaccess is in place. */ public static function check_download_folder_protection() { $upload_dir = wp_get_upload_dir(); $downloads_path = $upload_dir['basedir'] . '/woocommerce_uploads'; $download_method = get_option( 'woocommerce_file_download_method' ); $file_path = $downloads_path . '/.htaccess'; $file_content = 'redirect' === $download_method ? 'Options -Indexes' : 'deny from all'; $create = false; if ( wp_mkdir_p( $downloads_path ) && ! file_exists( $file_path ) ) { $create = true; } else { $current_content = @file_get_contents( $file_path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents if ( $current_content !== $file_content ) { unlink( $file_path ); $create = true; } } if ( $create ) { $file_handle = @fopen( $file_path, 'wb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_fopen if ( $file_handle ) { fwrite( $file_handle, $file_content ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite fclose( $file_handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose } } } } endif;