diff --git a/includes/wc-attribute-functions.php b/includes/wc-attribute-functions.php index 250b46cc8e1..cb4df866ae6 100644 --- a/includes/wc-attribute-functions.php +++ b/includes/wc-attribute-functions.php @@ -2,23 +2,17 @@ /** * WooCommerce Attribute Functions * - * @author WooThemes - * @category Core - * @package WooCommerce/Functions - * @version 2.1.0 + * @package WooCommerce/Functions + * @version 2.1.0 */ -if ( ! defined( 'ABSPATH' ) ) { - exit; // Exit if accessed directly -} +defined( 'ABSPATH' ) || exit; /** * Gets text attributes from a string. * * @since 2.4 - * - * @param string $raw_attributes - * + * @param string $raw_attributes Raw attributes. * @return array */ function wc_get_text_attributes( $raw_attributes ) { @@ -27,8 +21,9 @@ function wc_get_text_attributes( $raw_attributes ) { /** * See if an attribute is actually valid. + * * @since 3.0.0 - * @param string $value + * @param string $value Value. * @return bool */ function wc_get_text_attributes_filter_callback( $value ) { @@ -37,8 +32,9 @@ function wc_get_text_attributes_filter_callback( $value ) { /** * Implode an array of attributes using WC_DELIMITER. + * * @since 3.0.0 - * @param array $attributes + * @param array $attributes Attributes list. * @return string */ function wc_implode_text_attributes( $attributes ) { @@ -51,10 +47,12 @@ function wc_implode_text_attributes( $attributes ) { * @return array of objects */ function wc_get_attribute_taxonomies() { - if ( false === ( $attribute_taxonomies = get_transient( 'wc_attribute_taxonomies' ) ) ) { + $attribute_taxonomies = get_transient( 'wc_attribute_taxonomies' ); + + if ( false === $attribute_taxonomies ) { global $wpdb; - $attribute_taxonomies = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name != '' ORDER BY attribute_name ASC;" ); + $attribute_taxonomies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name != '' ORDER BY attribute_name ASC;" ); set_transient( 'wc_attribute_taxonomies', $attribute_taxonomies ); } @@ -93,11 +91,15 @@ function wc_variation_attribute_name( $attribute_name ) { function wc_attribute_taxonomy_name_by_id( $attribute_id ) { global $wpdb; - $attribute_name = $wpdb->get_var( $wpdb->prepare( " + $attribute_name = $wpdb->get_var( + $wpdb->prepare( + " SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d - ", $attribute_id ) ); + ", $attribute_id + ) + ); if ( $attribute_name && ! is_wp_error( $attribute_name ) ) { return wc_attribute_taxonomy_name( $attribute_name ); @@ -123,8 +125,8 @@ function wc_attribute_taxonomy_id_by_name( $name ) { /** * Get a product attributes label. * - * @param string $name - * @param object $product object Optional + * @param string $name Attribute name. + * @param WC_Product $product Product data. * @return string */ function wc_attribute_label( $name, $product = '' ) { @@ -136,8 +138,14 @@ function wc_attribute_label( $name, $product = '' ) { if ( $product->is_type( 'variation' ) ) { $product = wc_get_product( $product->get_parent_id() ); } + $attributes = array(); + + if ( false !== $product ) { + $attributes = $product->get_attributes(); + } + // Attempt to get label from product, as entered by the user. - if ( false !== $product && ( $attributes = $product->get_attributes() ) && isset( $attributes[ sanitize_title( $name ) ] ) ) { + if ( $attributes && isset( $attributes[ sanitize_title( $name ) ] ) ) { $label = $attributes[ sanitize_title( $name ) ]->get_name(); } else { $label = $name; @@ -152,7 +160,7 @@ function wc_attribute_label( $name, $product = '' ) { /** * Get a product attributes orderby setting. * - * @param mixed $name + * @param string $name Attribute name. * @return string */ function wc_attribute_orderby( $name ) { @@ -163,7 +171,7 @@ function wc_attribute_orderby( $name ) { if ( isset( $wc_product_attributes[ 'pa_' . $name ] ) ) { $orderby = $wc_product_attributes[ 'pa_' . $name ]->attribute_orderby; } else { - $orderby = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_orderby FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) ); + $orderby = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_orderby FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) ); } return apply_filters( 'woocommerce_attribute_orderby', $orderby, $name ); @@ -175,7 +183,7 @@ function wc_attribute_orderby( $name ) { * @return array */ function wc_get_attribute_taxonomy_names() { - $taxonomy_names = array(); + $taxonomy_names = array(); $attribute_taxonomies = wc_get_attribute_taxonomies(); if ( ! empty( $attribute_taxonomies ) ) { foreach ( $attribute_taxonomies as $tax ) { @@ -192,9 +200,11 @@ function wc_get_attribute_taxonomy_names() { * @return array */ function wc_get_attribute_types() { - return (array) apply_filters( 'product_attributes_type_selector', array( - 'select' => __( 'Select', 'woocommerce' ), - ) ); + return (array) apply_filters( + 'product_attributes_type_selector', array( + 'select' => __( 'Select', 'woocommerce' ), + ) + ); } /** @@ -227,11 +237,11 @@ function wc_get_attribute_type_label( $type ) { * https://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms. * * @since 2.4.0 - * @param string $attribute_name + * @param string $attribute_name Attribute name. * @return bool */ function wc_check_if_attribute_name_is_reserved( $attribute_name ) { - // Forbidden attribute names + // Forbidden attribute names. $reserved_terms = array( 'attachment', 'attachment_id', @@ -309,14 +319,14 @@ function wc_check_if_attribute_name_is_reserved( $attribute_name ) { 'year', ); - return in_array( $attribute_name, $reserved_terms ); + return in_array( $attribute_name, $reserved_terms, true ); } /** * Callback for array filter to get visible only. * * @since 3.0.0 - * @param WC_Product_Attribute $attribute + * @param WC_Product_Attribute $attribute Attribute data. * @return bool */ function wc_attributes_array_filter_visible( $attribute ) { @@ -327,7 +337,7 @@ function wc_attributes_array_filter_visible( $attribute ) { * Callback for array filter to get variation attributes only. * * @since 3.0.0 - * @param WC_Product_Attribute $attribute + * @param WC_Product_Attribute $attribute Attribute data. * @return bool */ function wc_attributes_array_filter_variation( $attribute ) { @@ -338,8 +348,8 @@ function wc_attributes_array_filter_variation( $attribute ) { * Check if an attribute is included in the attributes area of a variation name. * * @since 3.0.2 - * @param string $attribute Attribute value to check for - * @param string $name Product name to check in + * @param string $attribute Attribute value to check for. + * @param string $name Product name to check in. * @return bool */ function wc_is_attribute_in_product_name( $attribute, $name ) { @@ -352,7 +362,7 @@ function wc_is_attribute_in_product_name( $attribute, $name ) { * class PHP FALSE equivalents normally. * * @since 3.1.0 - * @param mixed $attribute Attribute being considered for exclusion from parent array. + * @param mixed $attribute Attribute being considered for exclusion from parent array. * @return bool */ function wc_array_filter_default_attributes( $attribute ) { @@ -369,11 +379,15 @@ function wc_array_filter_default_attributes( $attribute ) { function wc_get_attribute( $id ) { global $wpdb; - $data = $wpdb->get_row( $wpdb->prepare( " + $data = $wpdb->get_row( + $wpdb->prepare( + " SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d - ", $id ) ); + ", $id + ) + ); if ( is_wp_error( $data ) || is_null( $data ) ) { return null; @@ -431,10 +445,13 @@ function wc_create_attribute( $args ) { // Validate slug. if ( strlen( $slug ) >= 28 ) { + /* translators: %s: attribute slug */ return new WP_Error( 'invalid_product_attribute_slug_too_long', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) ); } elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) { + /* translators: %s: attribute slug */ return new WP_Error( 'invalid_product_attribute_slug_reserved_name', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) ); } elseif ( ( 0 === $id && taxonomy_exists( wc_attribute_taxonomy_name( $slug ) ) ) || ( isset( $args['old_slug'] ) && $args['old_slug'] !== $args['slug'] && taxonomy_exists( wc_attribute_taxonomy_name( $slug ) ) ) ) { + /* translators: %s: attribute slug */ return new WP_Error( 'invalid_product_attribute_slug_already_exists', sprintf( __( 'Slug "%s" is already in use. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) ); } @@ -514,24 +531,27 @@ function wc_create_attribute( $args ) { $table_name = get_option( 'db_version' ) < 34370 ? $wpdb->prefix . 'woocommerce_termmeta' : $wpdb->termmeta; $wpdb->update( $table_name, - array( 'meta_key' => 'order_pa_' . sanitize_title( $data['attribute_name'] ) ), - array( 'meta_key' => 'order_pa_' . sanitize_title( $args['old_slug'] ) ) + array( 'meta_key' => 'order_pa_' . sanitize_title( $data['attribute_name'] ) ), // WPCS: slow query ok. + array( 'meta_key' => 'order_pa_' . sanitize_title( $args['old_slug'] ) ) // WPCS: slow query ok. ); // Update product attributes which use this taxonomy. $old_attribute_name_length = strlen( $args['old_slug'] ) + 3; $attribute_name_length = strlen( $data['attribute_name'] ) + 3; - $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = REPLACE( meta_value, %s, %s ) WHERE meta_key = '_product_attributes'", - 's:' . $old_attribute_name_length . ':"pa_' . $args['old_slug'] . '"', - 's:' . $attribute_name_length . ':"pa_' . $data['attribute_name'] . '"' - ) ); + $wpdb->query( + $wpdb->prepare( + "UPDATE {$wpdb->postmeta} SET meta_value = REPLACE( meta_value, %s, %s ) WHERE meta_key = '_product_attributes'", + 's:' . $old_attribute_name_length . ':"pa_' . $args['old_slug'] . '"', + 's:' . $attribute_name_length . ':"pa_' . $data['attribute_name'] . '"' + ) + ); // Update variations which use this taxonomy. $wpdb->update( $wpdb->postmeta, - array( 'meta_key' => 'attribute_pa_' . sanitize_title( $data['attribute_name'] ) ), - array( 'meta_key' => 'attribute_pa_' . sanitize_title( $args['old_slug'] ) ) + array( 'meta_key' => 'attribute_pa_' . sanitize_title( $data['attribute_name'] ) ), // WPCS: slow query ok. + array( 'meta_key' => 'attribute_pa_' . sanitize_title( $args['old_slug'] ) ) // WPCS: slow query ok. ); } } @@ -549,7 +569,7 @@ function wc_create_attribute( $args ) { * For available args see wc_create_attribute(). * * @since 3.2.0 - * @param int $id Attribute ID. + * @param int $id Attribute ID. * @param array $args Attribute arguments. * @return int|WP_Error */ @@ -564,12 +584,15 @@ function wc_update_attribute( $id, $args ) { $args['name'] = $attribute->name; } - $args['old_slug'] = $wpdb->get_var( $wpdb->prepare( " + $args['old_slug'] = $wpdb->get_var( + $wpdb->prepare( + " SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d ", $args['id'] - ) ); + ) + ); return wc_create_attribute( $args ); } @@ -584,11 +607,15 @@ function wc_update_attribute( $id, $args ) { function wc_delete_attribute( $id ) { global $wpdb; - $name = $wpdb->get_var( $wpdb->prepare( " + $name = $wpdb->get_var( + $wpdb->prepare( + " SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d - ", $id ) ); + ", $id + ) + ); $taxonomy = wc_attribute_taxonomy_name( $name ); @@ -601,7 +628,7 @@ function wc_delete_attribute( $id ) { */ do_action( 'woocommerce_before_attribute_delete', $id, $name, $taxonomy ); - if ( $name && $wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = $id" ) ) { + if ( $name && $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d", $id ) ) ) { if ( taxonomy_exists( $taxonomy ) ) { $terms = get_terms( $taxonomy, 'orderby=name&hide_empty=0' ); foreach ( $terms as $term ) {