Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Admin/API
File: CustomAttributeTraits.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Traits for handling custom product attributes and their terms.
[2] Fix | Delete
*/
[3] Fix | Delete
[4] Fix | Delete
namespace Automattic\WooCommerce\Admin\API;
[5] Fix | Delete
[6] Fix | Delete
defined( 'ABSPATH' ) || exit;
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* CustomAttributeTraits class.
[10] Fix | Delete
*
[11] Fix | Delete
* @internal
[12] Fix | Delete
*/
[13] Fix | Delete
trait CustomAttributeTraits {
[14] Fix | Delete
/**
[15] Fix | Delete
* Get a single attribute by its slug.
[16] Fix | Delete
*
[17] Fix | Delete
* @internal
[18] Fix | Delete
* @param string $slug The attribute slug.
[19] Fix | Delete
* @return WP_Error|object The matching attribute object or WP_Error if not found.
[20] Fix | Delete
*/
[21] Fix | Delete
public function get_custom_attribute_by_slug( $slug ) {
[22] Fix | Delete
$matching_attributes = $this->get_custom_attributes( array( 'slug' => $slug ) );
[23] Fix | Delete
[24] Fix | Delete
if ( empty( $matching_attributes ) ) {
[25] Fix | Delete
return new \WP_Error(
[26] Fix | Delete
'woocommerce_rest_product_attribute_not_found',
[27] Fix | Delete
__( 'No product attribute with that slug was found.', 'woocommerce' ),
[28] Fix | Delete
array( 'status' => 404 )
[29] Fix | Delete
);
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
foreach ( $matching_attributes as $attribute_key => $attribute_value ) {
[33] Fix | Delete
return array( $attribute_key => $attribute_value );
[34] Fix | Delete
}
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* Query custom attributes by name or slug.
[39] Fix | Delete
*
[40] Fix | Delete
* @param string $args Search arguments, either name or slug.
[41] Fix | Delete
* @return array Matching attributes, formatted for response.
[42] Fix | Delete
*/
[43] Fix | Delete
protected function get_custom_attributes( $args ) {
[44] Fix | Delete
global $wpdb;
[45] Fix | Delete
[46] Fix | Delete
$args = wp_parse_args(
[47] Fix | Delete
$args,
[48] Fix | Delete
array(
[49] Fix | Delete
'name' => '',
[50] Fix | Delete
'slug' => '',
[51] Fix | Delete
)
[52] Fix | Delete
);
[53] Fix | Delete
[54] Fix | Delete
if ( empty( $args['name'] ) && empty( $args['slug'] ) ) {
[55] Fix | Delete
return array();
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
$mode = $args['name'] ? 'name' : 'slug';
[59] Fix | Delete
[60] Fix | Delete
if ( 'name' === $mode ) {
[61] Fix | Delete
$name = $args['name'];
[62] Fix | Delete
// Get as close as we can to matching the name property of custom attributes using SQL.
[63] Fix | Delete
$like = '%"name";s:%:"%' . $wpdb->esc_like( $name ) . '%"%';
[64] Fix | Delete
} else {
[65] Fix | Delete
$slug = sanitize_title_for_query( $args['slug'] );
[66] Fix | Delete
// Get as close as we can to matching the slug property of custom attributes using SQL.
[67] Fix | Delete
$like = '%s:' . strlen( $slug ) . ':"' . $slug . '";a:6:{%';
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
// Find all serialized product attributes with names like the search string.
[71] Fix | Delete
$query_results = $wpdb->get_results(
[72] Fix | Delete
$wpdb->prepare(
[73] Fix | Delete
"SELECT meta_value
[74] Fix | Delete
FROM {$wpdb->postmeta}
[75] Fix | Delete
WHERE meta_key = '_product_attributes'
[76] Fix | Delete
AND meta_value LIKE %s
[77] Fix | Delete
LIMIT 100",
[78] Fix | Delete
$like
[79] Fix | Delete
),
[80] Fix | Delete
ARRAY_A
[81] Fix | Delete
);
[82] Fix | Delete
[83] Fix | Delete
$custom_attributes = array();
[84] Fix | Delete
[85] Fix | Delete
foreach ( $query_results as $raw_product_attributes ) {
[86] Fix | Delete
[87] Fix | Delete
$meta_attributes = maybe_unserialize( $raw_product_attributes['meta_value'] );
[88] Fix | Delete
[89] Fix | Delete
if ( empty( $meta_attributes ) || ! is_array( $meta_attributes ) ) {
[90] Fix | Delete
continue;
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
foreach ( $meta_attributes as $meta_attribute_key => $meta_attribute_value ) {
[94] Fix | Delete
$meta_value = array_merge(
[95] Fix | Delete
array(
[96] Fix | Delete
'name' => '',
[97] Fix | Delete
'is_taxonomy' => 0,
[98] Fix | Delete
),
[99] Fix | Delete
(array) $meta_attribute_value
[100] Fix | Delete
);
[101] Fix | Delete
[102] Fix | Delete
// Skip non-custom attributes.
[103] Fix | Delete
if ( ! empty( $meta_value['is_taxonomy'] ) ) {
[104] Fix | Delete
continue;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
// Skip custom attributes that didn't match the query.
[108] Fix | Delete
// (There can be any number of attributes in the meta value).
[109] Fix | Delete
if ( ( 'name' === $mode ) && ( false === stripos( $meta_value['name'], $name ) ) ) {
[110] Fix | Delete
continue;
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
if ( ( 'slug' === $mode ) && ( $meta_attribute_key !== $slug ) ) {
[114] Fix | Delete
continue;
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
// Combine all values when there are multiple matching custom attributes.
[118] Fix | Delete
if ( isset( $custom_attributes[ $meta_attribute_key ] ) ) {
[119] Fix | Delete
$custom_attributes[ $meta_attribute_key ]['value'] .= ' ' . WC_DELIMITER . ' ' . $meta_value['value'];
[120] Fix | Delete
} else {
[121] Fix | Delete
$custom_attributes[ $meta_attribute_key ] = $meta_attribute_value;
[122] Fix | Delete
}
[123] Fix | Delete
}
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
return $custom_attributes;
[127] Fix | Delete
}
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function