| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Classes; |
| 4 |
|
| 5 |
use StoreEngine\Utils\Formatting; |
| 6 |
|
| 7 |
class Attributes extends AbstractWpdb { |
| 8 |
|
| 9 |
private int $page; |
| 10 |
private int $limit; |
| 11 |
|
| 12 |
public function __construct( int $page = 1, int $per_page = 10 ) { |
| 13 |
$this->page = max( 1, $page ); |
| 14 |
$this->limit = $per_page ?: 10; |
| 15 |
|
| 16 |
parent::__construct(); |
| 17 |
|
| 18 |
global $wpdb; |
| 19 |
$this->table = "{$wpdb->prefix}storeengine_attribute_taxonomies"; |
| 20 |
} |
| 21 |
|
| 22 |
public function get( $search = '' ) { |
| 23 |
$cache_key = Attribute::CACHE_KEY . md5( wp_json_encode( [ |
| 24 |
'search' => $search, |
| 25 |
'page' => $this->page, |
| 26 |
'limit' => $this->limit, |
| 27 |
] ) ); |
| 28 |
$has_cache = wp_cache_get( $cache_key, Attribute::CACHE_GROUP ); |
| 29 |
if ( $has_cache ) { |
| 30 |
return $has_cache; |
| 31 |
} |
| 32 |
|
| 33 |
global $wpdb; |
| 34 |
$query = "SELECT * FROM $this->table"; |
| 35 |
if ( ! empty( $search ) ) { |
| 36 |
$query .= $wpdb->prepare( ' WHERE attribute_label LIKE %s', '%' . $wpdb->esc_like( $search ) . '%' ); |
| 37 |
} |
| 38 |
$query .= ' ORDER BY attribute_id DESC'; |
| 39 |
|
| 40 |
if ( $this->limit > 0 ) { |
| 41 |
$offset = ( $this->limit * $this->page ) - $this->limit; |
| 42 |
$query .= $wpdb->prepare( ' LIMIT %d, %d', $offset, $this->limit ); |
| 43 |
} |
| 44 |
|
| 45 |
// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared -- Prepared query below. |
| 46 |
$results = $wpdb->get_results( $query ); |
| 47 |
// phpcs:enable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared |
| 48 |
if ( empty( $results ) ) { |
| 49 |
return []; |
| 50 |
} |
| 51 |
|
| 52 |
$attributes = []; |
| 53 |
foreach ( $results as $result ) { |
| 54 |
$attributes[] = ( new Attribute() )->set_data( $result ); |
| 55 |
} |
| 56 |
wp_cache_set( $cache_key, $attributes, Attribute::CACHE_GROUP ); |
| 57 |
|
| 58 |
return $attributes; |
| 59 |
} |
| 60 |
|
| 61 |
public function get_all_names() { |
| 62 |
$cache_key = Attribute::CACHE_KEY . 'all_names'; |
| 63 |
$has_cache = wp_cache_get( $cache_key, Attribute::CACHE_GROUP ); |
| 64 |
if ( $has_cache ) { |
| 65 |
return $has_cache; |
| 66 |
} |
| 67 |
global $wpdb; |
| 68 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 69 |
$results = $wpdb->get_results("SELECT attribute_name, attribute_label, attribute_public FROM {$wpdb->prefix}storeengine_attribute_taxonomies"); |
| 70 |
|
| 71 |
if ( empty( $results ) ) { |
| 72 |
return []; |
| 73 |
} |
| 74 |
|
| 75 |
$names = []; |
| 76 |
foreach ( $results as $result ) { |
| 77 |
$names[ $result->attribute_name ] = [ |
| 78 |
'label' => $result->attribute_label, |
| 79 |
'public' => Formatting::string_to_bool( $result->attribute_public ), |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
wp_cache_set( $cache_key, $names, Attribute::CACHE_GROUP ); |
| 84 |
|
| 85 |
return $names; |
| 86 |
} |
| 87 |
|
| 88 |
public function get_total_count( string $search = '' ): int { |
| 89 |
$cache_key = Attribute::CACHE_KEY . 'count' . $search; |
| 90 |
$has_cache = wp_cache_get( $cache_key, Attribute::CACHE_GROUP ); |
| 91 |
if ( $has_cache ) { |
| 92 |
return (int) $has_cache; |
| 93 |
} |
| 94 |
|
| 95 |
global $wpdb; |
| 96 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 97 |
if ( $search ) { |
| 98 |
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $this->table WHERE attribute_label LIKE %s", $search ) ); |
| 99 |
} else { |
| 100 |
$count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM $this->table" ); |
| 101 |
} |
| 102 |
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 103 |
|
| 104 |
wp_cache_set( $cache_key, $count, Attribute::CACHE_GROUP ); |
| 105 |
|
| 106 |
return $count; |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|