/** * See if an attribute name is valid * @param string $attribute_name * @return bool|WP_error result */ private static function valid_attribute_name($attribute_name) { if (strlen($attribute_name) >= 28) { return new WP_Error('error', sprintf(__('Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce'), sanitize_title($attribute_name))); } elseif (wc_check_if_attribute_name_is_reserved($attribute_name)) { return new WP_Error('error', sprintf(__('Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce'), sanitize_title($attribute_name))); } return true; }
/** * Validate attribute data. * * @since 2.4.0 * @param string $name * @param string $slug * @param string $type * @param string $order_by * @param bool $new_data * @return bool */ protected function validate_attribute_data($name, $slug, $type, $order_by, $new_data = true) { if (empty($name)) { throw new WC_API_Exception('woocommerce_api_missing_product_attribute_name', sprintf(__('Missing parameter %s', 'woocommerce'), 'name'), 400); } if (strlen($slug) >= 28) { throw new WC_API_Exception('woocommerce_api_invalid_product_attribute_slug_too_long', sprintf(__('Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce'), $slug), 400); } else { if (wc_check_if_attribute_name_is_reserved($slug)) { throw new WC_API_Exception('woocommerce_api_invalid_product_attribute_slug_reserved_name', sprintf(__('Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce'), $slug), 400); } else { if ($new_data && taxonomy_exists(wc_attribute_taxonomy_name($slug))) { throw new WC_API_Exception('woocommerce_api_invalid_product_attribute_slug_already_exists', sprintf(__('Slug "%s" is already in use. Change it, please.', 'woocommerce'), $slug), 400); } } } // Validate the attribute type if (!in_array(wc_clean($type), array_keys(wc_get_attribute_types()))) { throw new WC_API_Exception('woocommerce_api_invalid_product_attribute_type', sprintf(__('Invalid product attribute type - the product attribute type must be any of these: %s', 'woocommerce'), implode(', ', array_keys(wc_get_attribute_types()))), 400); } // Validate the attribute order by if (!in_array(wc_clean($order_by), array('menu_order', 'name', 'name_num', 'id'))) { throw new WC_API_Exception('woocommerce_api_invalid_product_attribute_order_by', sprintf(__('Invalid product attribute order_by type - the product attribute order_by type must be any of these: %s', 'woocommerce'), implode(', ', array('menu_order', 'name', 'name_num', 'id'))), 400); } return true; }
/** * Validate attribute slug. * * @param string $slug * @param bool $new_data * @return bool|WP_Error */ protected function validate_attribute_slug($slug, $new_data = true) { if (strlen($slug) >= 28) { return new WP_Error('woocommerce_rest_invalid_product_attribute_slug_too_long', sprintf(__('Slug "%s" is too long (28 characters max).', 'woocommerce'), $slug), array('status' => 400)); } elseif (wc_check_if_attribute_name_is_reserved($slug)) { return new WP_Error('woocommerce_rest_invalid_product_attribute_slug_reserved_name', sprintf(__('Slug "%s" is not allowed because it is a reserved term.', 'woocommerce'), $slug), array('status' => 400)); } elseif ($new_data && taxonomy_exists(wc_attribute_taxonomy_name($slug))) { return new WP_Error('woocommerce_rest_invalid_product_attribute_slug_already_exists', sprintf(__('Slug "%s" is already in use.', 'woocommerce'), $slug), array('status' => 400)); } return true; }