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 / models / price.php

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

309 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Models;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use StoreEngine\Classes\AbstractModel;
10 use StoreEngine\Classes\Exceptions\StoreEngineException;
11 use StoreEngine\Classes\enums\ProductTaxStatus;
12 use WP_Error;
13
14 /**
15 * Use Price entity & PriceCollection class
16 *
17 * @see \StoreEngine\Classes\Price
18 * @see \StoreEngine\Classes\PriceCollection
19 * @deprecated
20 */
21 class Price extends AbstractModel {
22 protected string $table = 'storeengine_product_price';
23
24 /**
25 * @param int[] $price_ids
26 *
27 * @return array
28 */
29 public static function get_pricing_with_products( array $price_ids ): array {
30 global $wpdb;
31
32 $price_ids = array_unique( array_filter( array_map( 'absint', $price_ids ) ) );
33
34 if ( empty( $price_ids ) ) {
35 return [];
36 }
37
38 $id_placeholders = implode( ',', array_fill( 0, count( $price_ids ), '%d' ) );
39 $where = $wpdb->prepare( "AND price.id IN ({$id_placeholders})", $price_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
40 $orderby = $wpdb->prepare( " FIELD(price.id, {$id_placeholders})", $price_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
41
42 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Query prepared above.
43 $results = $wpdb->get_results(
44 "
45 SELECT price.*, product.post_title
46 FROM {$wpdb->prefix}storeengine_product_price price
47 RIGHT JOIN {$wpdb->prefix}posts product ON price.product_id = product.ID
48 WHERE
49 product.post_type = 'storeengine_product' AND product.post_status = 'publish'
50 {$where}
51 ORDER BY {$orderby};
52 "
53 );
54 // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
55
56 if ( ! $results ) {
57 return [];
58 }
59
60 $data = [];
61 foreach ( $results as $result ) {
62 self::parsePrice( $result );
63 $data[ $result->id ] = $result;
64 }
65
66 return $data;
67 }
68
69 public static function get_price_with_product( int $price_id ) {
70 global $wpdb;
71
72 if ( ! $price_id ) {
73 return false;
74 }
75
76 $row = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
77 $wpdb->prepare( "SELECT price.*, product.post_title
78 FROM {$wpdb->prefix}storeengine_product_price price
79 RIGHT JOIN {$wpdb->prefix}posts product ON price.product_id = product.ID
80 WHERE price.id = %d
81 AND product.post_type = 'storeengine_product'
82 AND product.post_status = 'publish'",
83 $price_id
84 )
85 );
86
87 if ( $row ) {
88 self::parsePrice( $row );
89 }
90
91 return $row ?: false;
92 }
93
94 public function save( array $args = [] ) {
95 global $wpdb;
96
97 $args = wp_parse_args( $args, [
98 'price_name' => '',
99 'price_type' => '',
100 'price' => '',
101 'compare_price' => null,
102 'product_id' => 0,
103 'settings' => [],
104 'order' => 0,
105 ] );
106
107 // Parse settings.
108 $args['settings'] = self::prepare_settings( $args['settings'] );
109
110 $price = new \StoreEngine\Classes\Price();
111 $price->set_props( [
112 'price_name' => $args['price_name'],
113 'price_type' => $args['price_type'],
114 'price' => (float) $args['price'],
115 'compare_price' => (float) $args['compare_price'] > 0 ? (float) $args['compare_price'] : null,
116 'product_id' => absint( $args['product_id'] ),
117 'order' => absint( $args['order'] ),
118 'setup_fee' => (bool) ( $args['settings']['setup_fee'] ?: false ),
119 'setup_fee_name' => (string) ( $args['settings']['setup_fee_name'] ?: '' ),
120 'setup_fee_price' => (float) ( $args['settings']['setup_fee_price'] ?: 0 ),
121 'setup_fee_type' => (string) ( $args['settings']['setup_fee_type'] ?: 'fixed' ),
122 'trial' => (bool) ( $args['settings']['trial'] ?: false ),
123 'trial_days' => absint( $args['settings']['trial_days'] ?: 0 ),
124 'expire' => (bool) ( $args['settings']['expire'] ?: false ),
125 'expire_days' => absint( $args['settings']['expire_days'] ?: 0 ),
126 'payment_duration' => absint( $args['settings']['payment_duration'] ?: 1 ),
127 'payment_duration_type' => (string) ( $args['settings']['payment_duration_type'] ?: 'monthly' ),
128 'upgradeable' => (bool) ( $args['settings']['upgradeable'] ?: false ),
129 ] );
130
131 if ( is_wp_error( $price->save() ) ) {
132 return false;
133 }
134
135 return $price->get_id();
136 }
137
138 public function update( int $id, array $args ) {
139 $args = wp_parse_args( $args, [
140 'price_name' => '',
141 'price_type' => '',
142 'price' => '',
143 'compare_price' => null,
144 'settings' => [],
145 'order' => 0,
146 ] );
147
148 // Parse settings.
149 $args['settings'] = self::prepare_settings( $args['settings'] );
150
151 try {
152 $price = new \StoreEngine\Classes\Price( $id );
153 $price->set_props( [
154 'price_name' => $args['price_name'],
155 'price_type' => $args['price_type'],
156 'price' => (float) $args['price'],
157 'compare_price' => (float) $args['compare_price'] > 0 ? (float) $args['compare_price'] : null,
158 'product_id' => absint( $args['product_id'] ),
159 'order' => absint( $args['order'] ),
160 'setup_fee' => (bool) ( $args['settings']['setup_fee'] ?: false ),
161 'setup_fee_name' => (string) ( $args['settings']['setup_fee_name'] ?: '' ),
162 'setup_fee_price' => (float) ( $args['settings']['setup_fee_price'] ?: 0 ),
163 'setup_fee_type' => (string) ( $args['settings']['setup_fee_type'] ?: 'fixed' ),
164 'trial' => (bool) ( $args['settings']['trial'] ?: false ),
165 'trial_days' => absint( $args['settings']['trial_days'] ?: 0 ),
166 'expire' => (bool) ( $args['settings']['expire'] ?: false ),
167 'expire_days' => absint( $args['settings']['expire_days'] ?: 0 ),
168 'payment_duration' => absint( $args['settings']['payment_duration'] ?: 1 ),
169 'payment_duration_type' => (string) ( $args['settings']['payment_duration_type'] ?: 'monthly' ),
170 'upgradeable' => (bool) ( $args['settings']['upgradeable'] ?: false ),
171 ] );
172
173 $save = $price->save();
174 if ( is_wp_error( $save ) ) {
175 return $save;
176 }
177
178 return $price->get_id();
179 } catch ( StoreEngineException $e ) {
180 return $e->get_wp_error();
181 }
182 }
183
184 public function delete( ?int $id = null ): bool {
185 if ( null === $id ) {
186 return false;
187 }
188
189 try {
190 $price = new \StoreEngine\Classes\Price( $id );
191
192 return $price->delete();
193 } catch ( StoreEngineException $e ) {
194 return false;
195 }
196 }
197
198 /**
199 * @param int $id
200 *
201 * @return ?object
202 * @deprecated
203 */
204 public function get( int $id ): ?object {
205 global $wpdb;
206
207 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}storeengine_product_price WHERE id = %d", $id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
208
209 if ( $row ) {
210 self::parsePrice( $row );
211 }
212
213 return $row;
214 }
215
216 public function get_prices() {
217 global $wpdb;
218
219 $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}storeengine_product_price" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
220
221 foreach ( $results as &$result ) {
222 self::parsePrice( $result );
223 }
224
225 return $results;
226 }
227
228 public function get_prices_by_product_id( int $product_id ) {
229 global $wpdb;
230
231 $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}storeengine_product_price WHERE product_id = %d", $product_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
232
233 if ( ! $results ) {
234 return [];
235 }
236
237 foreach ( $results as &$result ) {
238 self::parsePrice( $result );
239 }
240
241 return $results;
242 }
243
244 protected static function parsePrice( object &$price ) {
245 $price->id = (int) $price->id;
246 $price->price = (float) $price->price;
247 $price->compare_price = $price->compare_price ? (float) $price->compare_price : null;
248 $price->product_id = (int) $price->product_id;
249 $price->order = (int) $price->order;
250
251 if ( $price->settings ) {
252 $price->settings = maybe_unserialize( $price->settings );
253 if ( ! is_array( $price->settings ) ) {
254 $price->settings = json_decode( $price->settings, true );
255 }
256
257 if ( ! is_array( $price->settings ) ) {
258 $price->settings = [];
259 }
260 }
261
262 if ( ! is_array( $price->settings ) ) {
263 $price->settings = [];
264 }
265
266 $price->settings = self::prepare_settings( $price->settings );
267 }
268
269 protected static function get_default_settings(): array {
270 return [
271 'setup_fee' => false,
272 'setup_fee_name' => '',
273 'setup_fee_price' => 0.0,
274 'setup_fee_type' => 'fixed',
275 'trial' => false,
276 'trial_days' => 0,
277 'expire' => false,
278 'expire_days' => 0,
279 'payment_duration' => 1,
280 'payment_duration_type' => 'monthly',
281 'upgradeable' => false,
282 'enable_license_activation_limit' => false,
283 'license_activation_limit' => 0,
284 'tax_status' => ProductTaxStatus::TAXABLE,
285 ];
286 }
287
288 protected static function prepare_settings( array $settings ): array {
289 $settings = wp_parse_args( $settings, self::get_default_settings() );
290
291 return [
292 'setup_fee' => (bool) ( $settings['setup_fee'] ?: false ),
293 'setup_fee_name' => (string) ( $settings['setup_fee_name'] ?: '' ),
294 'setup_fee_price' => (float) ( $settings['setup_fee_price'] ?: 0 ),
295 'setup_fee_type' => (string) ( $settings['setup_fee_type'] ?: 'fixed' ),
296 'trial' => (bool) ( $settings['trial'] ?: false ),
297 'trial_days' => absint( $settings['trial_days'] ?: 0 ),
298 'expire' => (bool) ( $settings['expire'] ?: false ),
299 'expire_days' => absint( $settings['expire_days'] ?: 0 ),
300 'payment_duration' => absint( $settings['payment_duration'] ?: 1 ),
301 'payment_duration_type' => (string) ( $settings['payment_duration_type'] ?: 'monthly' ),
302 'upgradeable' => (bool) ( $settings['upgradeable'] ?: false ),
303 'enable_license_activation_limit' => (bool) ( $settings['enable_license_activation_limit'] ?: false ),
304 'license_activation_limit' => absint( $settings['license_activation_limit'] ?: 0 ),
305 'tax_status' => (string) ( $settings['tax_status'] ?: ProductTaxStatus::TAXABLE ),
306 ];
307 }
308 }
309