PluginProbe
MailerLite – WooCommerce integration / 3.1.25
MailerLite – WooCommerce integration v3.1.25
3.1.25 3.1.26 3.1.24 3.1.23 3.1.22 3.1.21 3.1.20 3.1.19 3.1.18 3.1.17 3.1.16 3.1.15 1.8.7 1.8.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 All 147 releases
woo-mailerlite / includes / common / traits / WooMailerLiteResources.php

WooMailerLiteResources.php in MailerLite – WooCommerce integration 3.1.25, at includes/common/traits/WooMailerLiteResources.php

333 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 trait WooMailerLiteResources
4 {
5
6 /**
7 * @var array $resources
8 */
9 public array $resources = [
10 'products' => 'WooMailerLiteProduct',
11 'categories' => 'WooMailerLiteCategory',
12 'customers' => 'WooMailerLiteCustomer',
13 ];
14
15 /**
16 * @var array $args
17 */
18 protected array $args = [];
19
20
21 /**
22 * @var string $resource
23 */
24 protected ?string $resource;
25
26 protected int $counter = 0;
27
28 protected bool $setCustomerResource = false;
29 public $customerResourceCount = [];
30 /**
31 * Set resource
32 * @param string $resource
33 * @return WooMailerLiteDBConnection|WooMailerLiteModel|WooMailerLiteResources
34 */
35 public function set_resource(string $resource)
36 {
37 $this->resource = $resource;
38 return $this;
39 }
40
41 /**
42 * Get all resources
43 * @return void|WooMailerLiteCollection|int
44 */
45 public function resource_all($count = 0)
46 {
47 $collection = new WooMailerLiteCollection();
48 switch ($this->resource) {
49 case 'WooMailerLiteProduct':
50 if (isset($this->args['id'])) {
51 $this->args['include'] = [$this->args['id']];
52 unset($this->args['id']);
53 }
54 $this->args = array_merge([
55 'limit' => -1,
56 'return' => 'objects',
57 ], $this->args);
58
59 if ($this->args['limit'] == -1) {
60 $this->args = array_merge($this->args, [
61 'paginate' => true,
62 'return' => 'ids',
63 'status' => 'publish',
64 ]);
65 return (int) (new WC_Product_Query($this->args))->get_products()->total;
66 }
67 $items = (new WC_Product_Query($this->args))->get_products();
68 break;
69
70 case 'WooMailerLiteCategory':
71 if (isset($this->args['id'])) {
72 $this->args['include'] = [$this->args['id']];
73 unset($this->args['id']);
74 }
75 $this->args = array_merge([
76 'taxonomy' => 'product_cat',
77 'orderby' => 'name',
78 'order' => 'ASC',
79 'hide_empty' => false,
80 ], $this->args);
81
82 $items = new WP_Term_Query($this->args);
83 $items = $items->terms;
84 break;
85 case 'WooMailerLiteCustomer':
86 $this->args = array_merge([
87 'status' => WooMailerLiteOptions::COMPLETE_ORDER_STATUSES,
88 'type' => 'shop_order',
89 'orderby' => 'date',
90 'order' => 'ASC',
91 'return' => 'ids'
92 ], $this->args);
93 if ($count == 1) {
94 $this->args['order'] = 'DESC';
95 }
96 $items = wc_get_orders($this->args);
97 if ($count > 1) {
98 $items = array_filter($items, function ($order_id) {
99 return $order_id > WooMailerLiteOptions::get('lastSyncedOrder', 0);
100 });
101 }
102 break;
103 default:
104 $this->args = [];
105 return $collection;
106 }
107 $collection->collect(null);
108 if (!empty($items)) {
109 $collection->empty();
110 foreach ($items as $item) {
111 $this->counter++;
112 if (($item instanceof WC_Product_Simple) && (empty($item->get_permalink()) || !is_string($item->get_permalink()))) {
113 continue;
114 }
115 if (is_int($item)) {
116 $data = $item;
117 } else {
118 $data = ($item instanceof WC_Product_Simple) ? $item->get_data() : get_object_vars($item);
119 }
120 $first = false;
121 if ($count == 1) {
122 $first = true;
123 }
124
125 $this->prepareResourceData($this->resource, $data, $item, $first);
126 if ($this->resource === 'WooMailerLiteCustomer') {
127 continue;
128 }
129 $model = new $this->resource();
130
131 if (!empty($this->model->getCastsArray())) {
132 $model->attributes = array_intersect_key($data, array_flip($this->model->getCastsArray()));
133
134 } else {
135 $model->attributes = $data;
136 }
137
138 if (!empty($this->model->getRemoveEmptyArray())) {
139 foreach ($this->model->getRemoveEmptyArray() as $key) {
140 if (isset($model->attributes[$key]) && empty($model->attributes[$key])) {
141 unset($model->attributes[$key]);
142 }
143 }
144 }
145
146 $collection->collect($model);
147 }
148 }
149 if ($this->resource === 'WooMailerLiteCustomer') {
150 foreach ($this->customerResourceCount as $item) {
151 if (!empty($item)) {
152 $model = new $this->resource();
153 if ($this->model->getCastsArray()) {
154 $model->attributes = array_intersect_key($item, array_flip($this->model->getCastsArray() ?? []));
155 } else {
156 $model->attributes = $item;
157 }
158 $collection->collect($model);
159 }
160 }
161 }
162 return $collection;
163 }
164
165 /**
166 * Get resources
167 * @param int $count
168 * @return WooMailerLiteCollection|WooMailerLiteProduct|WooMailerLiteCustomer|WooMailerLiteCategory|null
169 */
170 public function resource_get(int $count = -1)
171 {
172 switch ($this->resource) {
173 case 'WooMailerLiteCategory':
174 $this->args['number'] = $count;
175 return $this->resource_all();
176 default:
177 $this->args['limit'] = $count;
178 return $this->resource_all($count);
179 }
180 }
181
182 /**
183 * Where clause for resources
184 * @param $column
185 * @param $value
186 * @return array|mixed|void
187 */
188 public function resource_where($column, $value)
189 {
190 $this->args[$column] = $value;
191 }
192
193 public function setArgs($args)
194 {
195 $this->args = array_merge($this->args, $args);
196 $prefix = $this->db()->prefix;
197 $this->select = "{$prefix}posts.ID";
198 if (isset($args['meta_query'][0]['key']) && ($args['meta_query'][0]['key'] !== '_woo_ml_category_tracked')) {
199 return $this;
200 }
201 if (isset($args['metaQuery'][0]['value']) && ($args['metaQuery'][0]['value'] === true) && ($args['metaQuery'][0]['key'] === '_woo_ml_product_tracked')) {
202 $this->leftJoin('postmeta', [
203 'posts.id' => "postmeta.post_id",
204 'postmeta.meta_key' => '_woo_ml_product_tracked'
205 ])
206 ->where('posts.post_type', 'product')
207 ->where('posts.post_status', 'publish')
208 ->andCombine(function($query) {
209 $query->where('postmeta.meta_value', '1')
210 ->orWhere('postmeta.meta_value', true);
211 })
212 ->columnsOnly();
213 return $this;
214 }
215 $this->leftJoin('postmeta', [
216 'posts.id' => "postmeta.post_id",
217 'postmeta.meta_key' => '_woo_ml_product_tracked'
218 ])
219 ->where('posts.post_type', 'product')
220 ->where('posts.post_status', 'publish')
221 ->andCombine(function($query) {
222 $query->where('postmeta.meta_value', '0')
223 ->orWhere('postmeta.meta_value', null)
224 ->orWhere('postmeta.meta_value', false);
225 })
226 ->columnsOnly();
227 return $this;
228 }
229
230 public function prepareResourceData($resource, &$data, $item, $first = false)
231 {
232 switch ($resource) {
233 case 'WooMailerLiteProduct':
234 $variableProduct = $item->is_type('variation') ?? false;
235 $data['resource_id'] = (string) ($variableProduct ? $item->get_parent_id() : $item->get_id());
236 $data['url'] = get_permalink($item->get_id());
237 $data['name'] = $item->get_name();
238 $data['price'] = floatval($item->get_price());
239 $data['url'] = get_permalink( $item->get_id() );
240 $data['category_ids'] = $item->get_category_ids();
241 $data['image'] = (string) wp_get_attachment_image_url($item->get_image_id(), 'full');
242 $data['description'] = $item->get_description();
243 $data['short_description'] = $item->get_short_description();
244 $data['status'] = $item->get_status();
245 $data['ignored'] = get_post_meta($data['resource_id'], '_woo_ml_product_ignored', true);
246 $data['tracked'] = get_post_meta($data['resource_id'], '_woo_ml_product_tracked', true);
247 break;
248 case 'WooMailerLiteCategory':
249 $data['resource_id'] = (string) $item->term_id;
250 $data['name'] = $item->name;
251 $data['tracked'] = get_term_meta($item->term_id, '_woo_ml_category_tracked', true);
252 break;
253 case 'WooMailerLiteCustomer':
254 $item = wc_get_order($item);
255 $data = (array) $data;
256 $email = $item->get_billing_email();
257 $similarOrderCount = 0;
258 $similarSpent = 0;
259 if (($item->get_customer_id() != 0) && in_array($item->get_customer_id(), array_column($this->customerResourceCount, 'resource_id'))) {
260 $key = array_search($item->get_customer_id(), array_column($this->customerResourceCount, 'resource_id', 'email'));
261 $similarOrderCount = $this->customerResourceCount[$key]['orders_count'] ?? 0;
262 $similarSpent = $this->customerResourceCount[$key]['total_spent'] ?? 0;
263 unset($this->customerResourceCount[$key]);
264 }
265 if (!isset($this->customerResourceCount[$email])) {
266 $this->customerResourceCount[$email] = [
267 'orders_count' => 0,
268 'total_spent' => 0,
269 'create_subscriber' => false,
270 'accepts_marketing' => false,
271 'key' => 0,
272 'resource_id' => 0,
273 // 'customer_id' => $data['customer_id'] ?? $this->counter,
274 ];
275
276 }
277
278
279 if ($first) {
280 $orders = wc_get_orders([
281 'billing_email' => $item->get_billing_email(),
282 'status' => WooMailerLiteOptions::COMPLETE_ORDER_STATUSES,
283 'customer_id' => $item->get_customer_id(),
284 'limit' => -1
285 ]);
286
287 if (count($orders) > 1) {
288 foreach ($orders as $order) {
289 if ($order->get_id() == $item->get_id()) {
290 continue;
291 }
292 $similarSpent += $order->get_total() ?? 0;
293 $similarOrderCount += 1;
294 }
295 }
296 }
297
298
299
300 $this->customerResourceCount[$email] = [
301 'customer_id' => $data['customer_id'] ?? $this->counter,
302 'resource_id' => $data['customer_id'] ?? $this->counter,
303 'email' => $item->get_billing_email(),
304 'name' => $item->get_billing_first_name(),
305 'last_name' => $item->get_billing_last_name(),
306 'accepts_marketing' => $item->get_meta('_woo_ml_subscribe'),
307 'create_subscriber' => $item->get_meta('_woo_ml_subscribe'),
308 'city' => $item->get_billing_city(),
309 'state' => $item->get_billing_state(),
310 'country' => $item->get_billing_country(),
311 'zip' => $item->get_billing_postcode(),
312 'last_order_id' => $item->get_id(),
313 'last_order' => $item->get_date_created()->date('Y-m-d H:i:s'),
314 'orders_count' => $this->customerResourceCount[$email]['orders_count'] + $similarOrderCount + 1,
315 'total_spent' => $this->customerResourceCount[$email]['total_spent'] + $similarSpent + $item->get_total(),
316 // 'tracked'
317 ];
318 $data = $this->customerResourceCount[$email];
319 break;
320 }
321 }
322
323 public function customTableEnabled()
324 {
325 // temporarily because it works
326 return true;
327 if ($this->model->isResource) {
328 return false;
329 }
330 return 'yes' === get_option(\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION);
331 }
332 }
333