PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / trunk
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar vtrunk
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / Extensions / EDD / EDD.php

EDD.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar trunk, at includes/Extensions/EDD/EDD.php

414 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * EDD Extension
5 *
6 * @package NotificationX\Extensions
7 */
8
9 namespace NotificationX\Extensions\EDD;
10
11 use NotificationX\Admin\Settings;
12 use NotificationX\Core\Database;
13 use NotificationX\Core\Helper;
14 use NotificationX\Core\Rules;
15 use NotificationX\GetInstance;
16 use NotificationX\Extensions\Extension;
17 use NotificationX\Extensions\GlobalFields;
18
19 /**
20 * EDD Extension
21 * @method static EDD get_instance($args = null)
22 */
23 class EDD extends Extension {
24 /**
25 * Instance of EDD
26 *
27 * @var EDD
28 */
29 use GetInstance;
30
31 public $priority = 10;
32 public $id = 'edd';
33 public $img = NOTIFICATIONX_ADMIN_URL . 'images/extensions/sources/edd.png';
34 public $doc_link = 'https://notificationx.com/docs/notificationx-easy-digital-downloads/';
35 public $types = 'conversions';
36 public $module = 'modules_edd';
37 public $module_priority = 5;
38 public $class = 'Easy_Digital_Downloads';
39
40 /**
41 * Initially Invoked when initialized.
42 */
43 public function __construct() {
44 parent::__construct();
45 }
46
47 public function init_extension()
48 {
49 $this->title = __( 'Easy Digital Downloads', 'notificationx' );
50 $this->module_title = __( 'Easy Digital Downloads', 'notificationx' );
51 }
52
53 /**
54 * Called from Extension::_construct if module is active.
55 * Not hook to ini action.
56 *
57 * @return void
58 */
59 public function init() {
60 parent::init();
61
62 add_action( 'edd_update_payment_status', array( $this, 'update_payment_status' ), 10, 3 );
63 }
64
65 public function init_fields(){
66 parent::init_fields();
67 add_filter('nx_link_types', [$this, 'link_types']);
68 }
69
70 /**
71 * This functions is hooked
72 *
73 * @return void
74 */
75 public function admin_actions() {
76 parent::admin_actions();
77
78 }
79
80 /**
81 * This functions is hooked
82 *
83 * @hooked nx_public_action
84 * @return void
85 */
86 public function public_actions() {
87 parent::public_actions();
88 add_filter("nx_filtered_data_{$this->id}", array($this, 'multiorder_combine'), 11, 3);
89 }
90
91 public function source_error_message($messages) {
92 if (!$this->class_exists()) {
93 $url = admin_url('plugin-install.php?s=easy-digital-downloads&tab=search&type=term');
94 $messages[$this->id] = [
95 'message' => sprintf( '%s <a href="%s" target="_blank">%s</a> %s',
96 __( 'You have to install', 'notificationx' ),
97 $url,
98 __( 'Easy Digital Downloads', 'notificationx' ),
99 __( 'plugin first.', 'notificationx' )
100 ),
101 'html' => true,
102 'type' => 'error',
103 'rules' => Rules::is('source', $this->id),
104 ];
105 }
106 return $messages;
107 }
108
109
110 /* #region backend */
111
112
113 public function multiorder_combine($data, $settings) {
114 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context.
115 $should_combine = apply_filters('nx_should_combine', true, $data, $settings);
116 if (!$should_combine || empty($settings['combine_multiorder']) || $settings['combine_multiorder'] != '1') {
117 return $data;
118 }
119 $items = [];
120 $item_counts = [];
121 $item_titles = [];
122 foreach ($data as $key => $item) {
123 $payment_id = !empty($item['id']) ? $item['id'] : $item['product_id'];
124 if (!isset($items[$payment_id])) {
125 $items[$payment_id] = $item;
126 } else {
127 $item_counts[$payment_id] = isset($item_counts[$payment_id]) ? ++$item_counts[$payment_id] : 1;
128 if ( isset( $item['title'] ) ) {
129 $item_titles[$payment_id][] = $item['title'];
130 }
131 }
132 }
133
134 $display = !empty($settings['combine_multiorder_display'])
135 ? $settings['combine_multiorder_display']
136 : 'count';
137
138 foreach ($item_counts as $key => $item) {
139
140 if ( $display === 'list' && ! empty( $item_titles[$key] ) ) {
141 // List the actual product names, e.g. "Product A & Product B".
142 $products_more_title = implode(
143 __(' & ', 'notificationx'),
144 $item_titles[$key]
145 );
146 } else {
147 $singular = !empty($settings['combine_multiorder_text'])
148 ? $settings['combine_multiorder_text']
149 : __('more product', 'notificationx');
150
151 $plural = !empty($settings['combine_multiorder_text_plural'])
152 ? $settings['combine_multiorder_text_plural']
153 : __('more products', 'notificationx');
154
155 // Both forms are already resolved here - either a user-entered
156 // override or an already-translated default - so they are never
157 // catalogue msgids. _n() would find no entry and fall back to
158 // exactly this choice, so make it explicit.
159 $more_product_text = sprintf(
160 1 == $item ? $singular : $plural,
161 $item
162 );
163 $products_more_title = sprintf('%d %s', $item, $more_product_text);
164 }
165
166 $items[$key]['title'] = sprintf(
167 /* translators: %1$s: product title, %2$s: combined "and N more products" text */
168 __('%1$s & %2$s', 'notificationx'),
169 $items[$key]['title'],
170 $products_more_title
171 );
172 }
173
174 // @todo maybe sort
175 return $items;
176 }
177
178 /**
179 * Adds option to Link Type field in Content tab.
180 *
181 * @param array $options
182 * @return array
183 */
184 public function link_types( $options ) {
185 $options = GlobalFields::get_instance()->normalize_fields(
186 array(
187 'product_image' => __( 'Product Page', 'notificationx' ),
188 ), 'source', $this->id, $options
189 );
190
191 return $options;
192 }
193
194 public function saved_post($post, $data, $nx_id) {
195 $this->delete_notification(null, $nx_id);
196 $this->get_notification_ready($data);
197 }
198
199 /**
200 * This function is responsible for making the notification ready for first time we make the notification.
201 *
202 * @param string $type
203 * @param array $data
204 * @return void
205 */
206 public function get_notification_ready( $post = array() ) {
207 $orders = $this->get_orders( $post );
208 if ( is_array( $orders ) && ! empty( $orders ) ) {
209 // $orders = NotificationX_Helper::sortBy($orders, 'edd');
210 $entries = [];
211 foreach ( $orders as $key => $order ) {
212 $entries[] = [
213 'nx_id' => $post['nx_id'],
214 'source' => $this->id,
215 'entry_key' => $order['key'],
216 'data' => $order,
217 ];
218 }
219 $this->update_notifications($entries);
220 }
221 }
222
223 /**
224 * Update Payment Status
225 *
226 * @param int $payment_id
227 * @param string $new_status
228 * @param string $old_status
229 * @return void
230 * @since 1.3.9
231 */
232 public function update_payment_status( $payment_id, $new_status, $old_status ) {
233 if ( $new_status !== 'publish' && $new_status !== 'complete' ) {
234 return;
235 }
236 $offset = get_option( 'gmt_offset' );
237 $temp_notifications = $this->single_order( $payment_id, $offset );
238 if ( is_array( $temp_notifications ) ) {
239 foreach ( $temp_notifications as $notification ) {
240 $this->update_notification([
241 'source' => $this->id,
242 'entry_key' => $notification['key'],
243 'data' => $notification,
244 ]);
245 }
246 }
247 return true;
248 }
249
250 /**
251 * This function is responsible for get all payments
252 *
253 * @param int $days
254 * @param int $amount
255 * @return array
256 */
257 public function get_payments( $days, $amount ) {
258 // $date = '-' . intval( $days ) . ' days';
259 // $start_date = strtotime( $date );
260 $from = gmdate('Y-m-d H:i:s', $days);
261
262 $amount = $amount > 0 ? $amount : -1;
263
264 $args = array(
265 'number' => $amount,
266 'status' => array( 'publish', 'complete' ),
267 'date_query' => array(
268 'after' => $from,
269 ),
270 );
271
272 return edd_get_payments( $args );
273 }
274
275 /**
276 * Get all the orders from database using a date query
277 * for limitation.
278 *
279 * @param array $data
280 * @return void
281 */
282 public function get_orders( $data ) {
283 if ( empty( $data ) ) {
284 return;
285 }
286 $days = Helper::generate_time_string($data); //$data['display_from'];
287 $amount = $data['display_last'];
288 $payments = $this->get_payments( $days, $amount );
289 $notifications = array();
290 if ( is_array( $payments ) && ! empty( $payments ) ) {
291 $notifications = $this->ordered_products( $payments, true );
292 }
293 return $notifications;
294 }
295
296 /**
297 * This function is responsible for
298 * making ready the product notifications array
299 *
300 * @param int $payment_id
301 * @return void
302 */
303 protected function ordered_products( $payments, $ready = false ) {
304 $offset = get_option( 'gmt_offset' );
305 $notifications = array();
306 foreach ( $payments as $payment ) :
307 $temp_notifications = $this->single_order( $payment->ID, $offset );
308 if ( is_array( $temp_notifications ) ) {
309 foreach ( $temp_notifications as $notification ) {
310 $notifications[] = $notification;
311 }
312 }
313 endforeach;
314 return $notifications;
315 }
316
317 protected function single_order( $payment_id, $offset = 0 ) {
318 if ( empty( $payment_id ) ) {
319 return null;
320 }
321 $notifications = array();
322 $data = array();
323 $payment = new \EDD_Payment( $payment_id );
324 $cart_details = $payment->cart_details;
325 $user_info = $payment->user_info;
326 $is_3x = defined('EDD_VERSION') ? version_compare( EDD_VERSION, '3', '<' ) : false;
327 $offset_s = $is_3x ? ( $offset * 60 * 60 ) : 0;
328
329 unset( $user_info['id'] );
330 unset( $user_info['discount'] );
331 unset( $user_info['address'] );
332
333 $user_info['name'] = $this->name( $user_info['first_name'], $user_info['last_name'] );
334 $user_info['timestamp'] = strtotime( $payment->date ) - $offset_s;
335 $user_info['ip'] = $payment->ip;
336 $user_info['id'] = $payment_id;
337 if ( is_array( $cart_details ) ) {
338 foreach ( $cart_details as $cart_index => $download ) {
339 $if_has_course = false;
340 if ( function_exists( 'tutor_utils' ) ) {
341 $if_has_course = tutor_utils()->product_belongs_with_course( $download['id'] );
342 }
343 if ( $if_has_course ) {
344 continue;
345 }
346 if ( ! $if_has_course ) {
347 $data['title'] = $download['name'];
348 $data['link'] = get_permalink( $download['id'] );
349 $data['product_id'] = $download['id'];
350 $data['key'] = $payment->key . '-' . $download['id'];
351 $notification = array_merge( $user_info, $data );
352 $notifications[] = $notification;
353 }
354 }
355 }
356 return $notifications;
357 }
358 /* #endregion */
359
360 /**
361 * Image action callback
362 * @param array $image_data
363 * @param array $data
364 * @param stdClass $settings
365 * @return array
366 */
367 public function notification_image($image_data, $data, $settings) {
368 if (!$settings['show_default_image'] && $settings['show_notification_image'] === 'featured_image') {
369 if (!empty($data['product_id']) && has_post_thumbnail($data['product_id'])) {
370 $image_size = (string) Settings::get_instance()->get('settings.notification_image_size', '100_100');
371 $image_size = explode('_', $image_size);
372 $width = 100;
373 $height = 100;
374 if( !empty( $image_size[0] ) && !empty( $image_size[1] ) ) {
375 $width = $image_size[0];
376 $height = $image_size[1];
377 }
378 $product_image = wp_get_attachment_image_src(
379 get_post_thumbnail_id($data['product_id']),
380 [$width, $height],
381 false
382 );
383 $image_data['url'] = is_array($product_image) ? $product_image[0] : '';
384 }
385 }
386 return $image_data;
387 }
388
389 /* #region frontend */
390
391 // @todo
392 public function fallback_data( $data, $saved_data, $settings ) {
393 $data['anonymous_title'] = __( 'Anonymous Product', 'notificationx' );
394 if(empty($saved_data['product_title']) && !empty($saved_data['title'])){
395 $data['product_title'] = $saved_data['title'];
396 }
397 return $data;
398 }
399 /* #endregion */
400
401 public function doc(){
402 /* translators: %1$s: Easy Digital Downloads installed & activated link URL, %2$s: documentation link URL, %3$s: Integration with Easy Digital Downloads link URL, %4$s: NotificationX Increase Sales on WordPress link URL */
403 return sprintf(__('<p>Make sure that you have <a href="%1$s" target="_blank">Easy Digital Downloads installed & activated</a> to use its campaign & product sales data. For further assistance, check out our step by step <a target="_blank" href="%2$s">documentation</a>.</p>
404 <p>👉 NotificationX <a target="_blank" href="%3$s">Integration with Easy Digital Downloads</a></p>
405 <p><strong>Recommended Blog:</strong></p>
406 <p>🔥 How Does <a target="_blank" href="%4$s">NotificationX Increase Sales on WordPress</a> Websites?</p>', 'notificationx'),
407 'https://wordpress.org/plugins/easy-digital-downloads/',
408 'https://notificationx.com/docs/notificationx-easy-digital-downloads/',
409 'https://notificationx.com/integrations/easy-digital-downloads/',
410 'https://wpdeveloper.com/notificationx-increase-sales-wordpress/'
411 );
412 }
413 }
414