| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Utils\traits; |
| 4 |
|
| 5 |
use StoreEngine\Classes\Attributes; |
| 6 |
|
| 7 |
trait Attribute { |
| 8 |
|
| 9 |
public static function get_product_attribute( int $attribute_id ) { |
| 10 |
return ( new \StoreEngine\Classes\Attribute( $attribute_id ) )->get(); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* @param array $args |
| 15 |
* |
| 16 |
* @return \StoreEngine\Classes\Attribute[] |
| 17 |
*/ |
| 18 |
public static function get_product_attributes( array $args = [] ): array { |
| 19 |
$args = wp_parse_args( $args, [ |
| 20 |
'per_page' => 10, |
| 21 |
'page' => 1, |
| 22 |
'search' => '', |
| 23 |
] ); |
| 24 |
|
| 25 |
return ( new Attributes( $args['page'], $args['per_page'] ) )->get( $args['search'] ); |
| 26 |
} |
| 27 |
|
| 28 |
public static function get_total_product_attributes_count( string $search = '' ): int { |
| 29 |
return ( new Attributes() )->get_total_count( $search ); |
| 30 |
} |
| 31 |
|
| 32 |
public static function delete_product_attribute( int $attribute_id ): bool { |
| 33 |
$attribute = self::get_product_attribute( $attribute_id ); |
| 34 |
if ( ! $attribute ) { |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
return $attribute->delete(); |
| 39 |
} |
| 40 |
|
| 41 |
public static function get_attribute_taxonomy_name( string $name ): string { |
| 42 |
// @TODO add migration for old prefix |
| 43 |
//return STOREENGINE_PLUGIN_SLUG . '_pa_' . $name; |
| 44 |
//return 'storeengine_pa_' . $name; |
| 45 |
return 'se_pa_' . $name; |
| 46 |
} |
| 47 |
|
| 48 |
public static function strip_attribute_taxonomy_name( string $name ): string { |
| 49 |
return preg_replace( '/^se_pa\_/', '', $name ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Sanitize taxonomy names. Slug format (no spaces, lowercase). |
| 54 |
* Urldecode is used to reverse munging of UTF8 characters. |
| 55 |
* |
| 56 |
* @param string $taxonomy Taxonomy name. |
| 57 |
* @return string |
| 58 |
*/ |
| 59 |
public static function sanitize_taxonomy_name( string $taxonomy ): string { |
| 60 |
return apply_filters( 'storeengine/sanitize_taxonomy_name', urldecode( sanitize_title( urldecode( $taxonomy ) ) ), $taxonomy ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Check if attribute name is reserved. |
| 65 |
* https://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms. |
| 66 |
* |
| 67 |
* @param string $attribute_name Attribute name. |
| 68 |
* |
| 69 |
* @return bool |
| 70 |
*/ |
| 71 |
public static function check_if_attribute_name_is_reserved( string $attribute_name ): bool { |
| 72 |
// Forbidden attribute names. |
| 73 |
$reserved_terms = [ |
| 74 |
'attachment', |
| 75 |
'attachment_id', |
| 76 |
'author', |
| 77 |
'author_name', |
| 78 |
'calendar', |
| 79 |
'cat', |
| 80 |
'category', |
| 81 |
'category__and', |
| 82 |
'category__in', |
| 83 |
'category__not_in', |
| 84 |
'category_name', |
| 85 |
'comments_per_page', |
| 86 |
'comments_popup', |
| 87 |
'cpage', |
| 88 |
'day', |
| 89 |
'debug', |
| 90 |
'error', |
| 91 |
'exact', |
| 92 |
'feed', |
| 93 |
'hour', |
| 94 |
'link_category', |
| 95 |
'm', |
| 96 |
'minute', |
| 97 |
'monthnum', |
| 98 |
'more', |
| 99 |
'name', |
| 100 |
'nav_menu', |
| 101 |
'nopaging', |
| 102 |
'offset', |
| 103 |
'order', |
| 104 |
'orderby', |
| 105 |
'p', |
| 106 |
'page', |
| 107 |
'page_id', |
| 108 |
'paged', |
| 109 |
'pagename', |
| 110 |
'pb', |
| 111 |
'perm', |
| 112 |
'post', |
| 113 |
'post__in', |
| 114 |
'post__not_in', |
| 115 |
'post_format', |
| 116 |
'post_mime_type', |
| 117 |
'post_status', |
| 118 |
'post_tag', |
| 119 |
'post_type', |
| 120 |
'posts', |
| 121 |
'posts_per_archive_page', |
| 122 |
'posts_per_page', |
| 123 |
'preview', |
| 124 |
'robots', |
| 125 |
's', |
| 126 |
'search', |
| 127 |
'second', |
| 128 |
'sentence', |
| 129 |
'showposts', |
| 130 |
'static', |
| 131 |
'subpost', |
| 132 |
'subpost_id', |
| 133 |
'tag', |
| 134 |
'tag__and', |
| 135 |
'tag__in', |
| 136 |
'tag__not_in', |
| 137 |
'tag_id', |
| 138 |
'tag_slug__and', |
| 139 |
'tag_slug__in', |
| 140 |
'taxonomy', |
| 141 |
'tb', |
| 142 |
'term', |
| 143 |
'type', |
| 144 |
'w', |
| 145 |
'withcomments', |
| 146 |
'withoutcomments', |
| 147 |
'year', |
| 148 |
]; |
| 149 |
|
| 150 |
return in_array( $attribute_name, $reserved_terms, true ); |
| 151 |
} |
| 152 |
} |
| 153 |
|