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 / integration-repository.php

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

185 lines 4.9 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\Classes\Data\IntegrationRepositoryData;
6
7 class IntegrationRepository {
8
9 protected string $provider;
10
11 public const CACHE_KEY = 'storeengine_integration_repository_';
12
13 public const CACHE_GROUP = 'storeengine_integration_repositories';
14
15 /**
16 * @var IntegrationRepositoryData[]
17 */
18 protected array $objects = [];
19
20 public function __construct( string $provider ) {
21 $this->provider = $provider;
22 }
23
24 public function get_by_id( int $integration_id ): IntegrationRepository {
25 $cache_key = self::CACHE_KEY . $integration_id;
26 $has_cache = wp_cache_get( $cache_key, self::CACHE_GROUP );
27
28 if ( $has_cache && is_array( $has_cache ) ) {
29 $this->objects = array_map( fn( $result ) => new IntegrationRepositoryData(
30 ( new Integration() )->set_data( $result ),
31 new Price( $result->price_id )
32 ), $has_cache );
33
34 return $this;
35 }
36
37 global $wpdb;
38 //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
39 $results = $wpdb->get_results(
40 $wpdb->prepare( "SELECT
41 i.id,
42 i.product_id,
43 i.price_id,
44 i.provider,
45 i.integration_id,
46 i.variation_id,
47 i.created_at,
48 i.updated_at,
49 pr.price_name,
50 pr.price_type,
51 pr.price,
52 pr.compare_price,
53 pr.settings,
54 pr.`order`
55 FROM {$wpdb->prefix}storeengine_integrations i
56 JOIN {$wpdb->prefix}storeengine_product_price pr ON pr.id = i.price_id
57 WHERE i.provider = %s AND i.integration_id = %d
58 ORDER BY pr.`order`", $this->get_provider(), $integration_id )
59 );
60
61 wp_cache_set( $cache_key, $results, self::CACHE_GROUP );
62
63 foreach ( $results as $result ) {
64 $this->objects[] = new IntegrationRepositoryData(
65 ( new Integration() )->set_data( $result ),
66 new Price( $result->price_id )
67 );
68 }
69
70
71 return $this;
72 }
73
74 public function get_by_price_id( int $price_id ): ?IntegrationRepositoryData {
75 $cache_key = self::CACHE_KEY . $this->get_provider() . '_price_' . $price_id;
76 $has_cache = wp_cache_get( $cache_key, self::CACHE_GROUP );
77
78 if ( $has_cache ) {
79 return new IntegrationRepositoryData(
80 ( new Integration() )->set_data( $has_cache ),
81 new Price( $has_cache->price_id )
82 );
83 }
84
85 global $wpdb;
86 //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
87 $result = $wpdb->get_row(
88 $wpdb->prepare( "SELECT
89 i.id,
90 i.product_id,
91 i.price_id,
92 i.provider,
93 i.integration_id,
94 i.variation_id,
95 i.created_at,
96 i.updated_at,
97 pr.price_name,
98 pr.price_type,
99 pr.price,
100 pr.compare_price,
101 pr.settings,
102 pr.`order`
103 FROM {$wpdb->prefix}storeengine_integrations i
104 JOIN {$wpdb->prefix}storeengine_product_price pr ON pr.id = i.price_id
105 WHERE i.provider = %s AND i.price_id = %d
106 ORDER BY pr.`order`", $this->get_provider(), $price_id )
107 );
108
109 if ( ! $result ) {
110 return null;
111 }
112
113 wp_cache_set( $cache_key, $result, self::CACHE_GROUP );
114
115 return new IntegrationRepositoryData(
116 ( new Integration() )->set_data( $result ),
117 new Price( $result->price_id )
118 );
119 }
120
121 public function get_by_provider( array $args = [] ): IntegrationRepository {
122 $cache_key = self::CACHE_KEY . $this->get_provider();
123 $has_cache = wp_cache_get( $cache_key, self::CACHE_GROUP );
124 if ( $has_cache && is_array( $has_cache ) ) {
125 $this->objects = array_map( fn( $result ) => new IntegrationRepositoryData(
126 ( new Integration() )->set_data( $result ),
127 new Price( $result->price_id )
128 ), $has_cache );
129
130 return $this;
131 }
132
133 $orderby = 'ASC';
134 if ( isset( $args['orderby'] ) && 'DESC' === $args['orderby'] ) {
135 $orderby = 'DESC';
136 }
137
138 global $wpdb;
139
140 // phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
141 $results = $wpdb->get_results(
142 $wpdb->prepare( "SELECT
143 i.id,
144 i.product_id,
145 i.price_id,
146 i.provider,
147 i.integration_id,
148 i.created_at,
149 i.updated_at,
150 pr.price_name,
151 pr.price_type,
152 pr.price,
153 pr.compare_price,
154 pr.settings,
155 pr.`order`
156 FROM {$wpdb->prefix}storeengine_integrations i
157 JOIN {$wpdb->prefix}storeengine_product_price pr ON pr.id = i.price_id
158 JOIN $wpdb->posts p ON p.id = i.product_id AND p.post_status = 'publish'
159 WHERE i.provider = %s
160 ORDER BY i.id $orderby", $this->get_provider() )
161 );
162 // phpcs:enable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
163
164 wp_cache_set( $cache_key, $results, self::CACHE_GROUP );
165
166 foreach ( $results as $result ) {
167 $this->objects[] = new IntegrationRepositoryData(
168 ( new Integration() )->set_data( $result ),
169 new Price( $result->price_id )
170 );
171 }
172
173
174 return $this;
175 }
176
177 public function get_provider(): string {
178 return $this->provider;
179 }
180
181 public function get_objects(): array {
182 return $this->objects;
183 }
184 }
185