PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / classes / attributes.php

attributes.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/classes/attributes.php

110 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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