| 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 |
$should_combine = apply_filters('nx_should_combine', true, $data, $settings); |
| 115 |
if (!$should_combine || empty($settings['combine_multiorder']) || $settings['combine_multiorder'] != '1') { |
| 116 |
return $data; |
| 117 |
} |
| 118 |
$items = []; |
| 119 |
$item_counts = []; |
| 120 |
foreach ($data as $key => $item) { |
| 121 |
$payment_id = !empty($item['id']) ? $item['id'] : $item['product_id']; |
| 122 |
if (!isset($items[$payment_id])) { |
| 123 |
$items[$payment_id] = $item; |
| 124 |
} else { |
| 125 |
$item_counts[$payment_id] = isset($item_counts[$payment_id]) ? ++$item_counts[$payment_id] : 1; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
$products_more_title = isset($settings['combine_multiorder_text']) && !empty($settings['combine_multiorder_text']) ? __($settings['combine_multiorder_text'], 'notificationx') : __('more products', 'notificationx'); |
| 130 |
foreach ($item_counts as $key => $item) { |
| 131 |
$items[$key]['title'] = $items[$key]['title'] . ' & ' . $item . ' ' . $products_more_title; |
| 132 |
} |
| 133 |
|
| 134 |
// @todo maybe sort |
| 135 |
return $items; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Adds option to Link Type field in Content tab. |
| 140 |
* |
| 141 |
* @param array $options |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
public function link_types( $options ) { |
| 145 |
$options = GlobalFields::get_instance()->normalize_fields( |
| 146 |
array( |
| 147 |
'product_image' => __( 'Product Page', 'notificationx' ), |
| 148 |
), 'source', $this->id, $options |
| 149 |
); |
| 150 |
|
| 151 |
return $options; |
| 152 |
} |
| 153 |
|
| 154 |
public function saved_post($post, $data, $nx_id) { |
| 155 |
$this->delete_notification(null, $nx_id); |
| 156 |
$this->get_notification_ready($data); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* This function is responsible for making the notification ready for first time we make the notification. |
| 161 |
* |
| 162 |
* @param string $type |
| 163 |
* @param array $data |
| 164 |
* @return void |
| 165 |
*/ |
| 166 |
public function get_notification_ready( $post = array() ) { |
| 167 |
$orders = $this->get_orders( $post ); |
| 168 |
if ( is_array( $orders ) && ! empty( $orders ) ) { |
| 169 |
// $orders = NotificationX_Helper::sortBy($orders, 'edd'); |
| 170 |
$entries = []; |
| 171 |
foreach ( $orders as $key => $order ) { |
| 172 |
$entries[] = [ |
| 173 |
'nx_id' => $post['nx_id'], |
| 174 |
'source' => $this->id, |
| 175 |
'entry_key' => $order['key'], |
| 176 |
'data' => $order, |
| 177 |
]; |
| 178 |
} |
| 179 |
$this->update_notifications($entries); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Update Payment Status |
| 185 |
* |
| 186 |
* @param int $payment_id |
| 187 |
* @param string $new_status |
| 188 |
* @param string $old_status |
| 189 |
* @return void |
| 190 |
* @since 1.3.9 |
| 191 |
*/ |
| 192 |
public function update_payment_status( $payment_id, $new_status, $old_status ) { |
| 193 |
if ( $new_status !== 'publish' && $new_status !== 'complete' ) { |
| 194 |
return; |
| 195 |
} |
| 196 |
$offset = get_option( 'gmt_offset' ); |
| 197 |
$temp_notifications = $this->single_order( $payment_id, $offset ); |
| 198 |
if ( is_array( $temp_notifications ) ) { |
| 199 |
foreach ( $temp_notifications as $notification ) { |
| 200 |
$this->update_notification([ |
| 201 |
'source' => $this->id, |
| 202 |
'entry_key' => $notification['key'], |
| 203 |
'data' => $notification, |
| 204 |
]); |
| 205 |
} |
| 206 |
} |
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* This function is responsible for get all payments |
| 212 |
* |
| 213 |
* @param int $days |
| 214 |
* @param int $amount |
| 215 |
* @return array |
| 216 |
*/ |
| 217 |
public function get_payments( $days, $amount ) { |
| 218 |
// $date = '-' . intval( $days ) . ' days'; |
| 219 |
// $start_date = strtotime( $date ); |
| 220 |
$from = date('Y-m-d H:i:s', $days); |
| 221 |
|
| 222 |
$amount = $amount > 0 ? $amount : -1; |
| 223 |
|
| 224 |
$args = array( |
| 225 |
'number' => $amount, |
| 226 |
'status' => array( 'publish', 'complete' ), |
| 227 |
'date_query' => array( |
| 228 |
'after' => $from, |
| 229 |
), |
| 230 |
); |
| 231 |
|
| 232 |
return edd_get_payments( $args ); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Get all the orders from database using a date query |
| 237 |
* for limitation. |
| 238 |
* |
| 239 |
* @param array $data |
| 240 |
* @return void |
| 241 |
*/ |
| 242 |
public function get_orders( $data ) { |
| 243 |
if ( empty( $data ) ) { |
| 244 |
return; |
| 245 |
} |
| 246 |
$days = Helper::generate_time_string($data); //$data['display_from']; |
| 247 |
$amount = $data['display_last']; |
| 248 |
$payments = $this->get_payments( $days, $amount ); |
| 249 |
$notifications = array(); |
| 250 |
if ( is_array( $payments ) && ! empty( $payments ) ) { |
| 251 |
$notifications = $this->ordered_products( $payments, true ); |
| 252 |
} |
| 253 |
return $notifications; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* This function is responsible for |
| 258 |
* making ready the product notifications array |
| 259 |
* |
| 260 |
* @param int $payment_id |
| 261 |
* @return void |
| 262 |
*/ |
| 263 |
protected function ordered_products( $payments, $ready = false ) { |
| 264 |
$offset = get_option( 'gmt_offset' ); |
| 265 |
$notifications = array(); |
| 266 |
foreach ( $payments as $payment ) : |
| 267 |
$temp_notifications = $this->single_order( $payment->ID, $offset ); |
| 268 |
if ( is_array( $temp_notifications ) ) { |
| 269 |
foreach ( $temp_notifications as $notification ) { |
| 270 |
$notifications[] = $notification; |
| 271 |
} |
| 272 |
} |
| 273 |
endforeach; |
| 274 |
return $notifications; |
| 275 |
} |
| 276 |
|
| 277 |
protected function single_order( $payment_id, $offset = 0 ) { |
| 278 |
if ( empty( $payment_id ) ) { |
| 279 |
return null; |
| 280 |
} |
| 281 |
$notifications = array(); |
| 282 |
$data = array(); |
| 283 |
$payment = new \EDD_Payment( $payment_id ); |
| 284 |
$cart_details = $payment->cart_details; |
| 285 |
$user_info = $payment->user_info; |
| 286 |
$is_3x = defined('EDD_VERSION') ? version_compare( EDD_VERSION, '3', '<' ) : false; |
| 287 |
$offset_s = $is_3x ? ( $offset * 60 * 60 ) : 0; |
| 288 |
|
| 289 |
unset( $user_info['id'] ); |
| 290 |
unset( $user_info['discount'] ); |
| 291 |
unset( $user_info['address'] ); |
| 292 |
|
| 293 |
$user_info['name'] = $this->name( $user_info['first_name'], $user_info['last_name'] ); |
| 294 |
$user_info['timestamp'] = strtotime( $payment->date ) - $offset_s; |
| 295 |
$user_info['ip'] = $payment->ip; |
| 296 |
$user_info['id'] = $payment_id; |
| 297 |
if ( is_array( $cart_details ) ) { |
| 298 |
foreach ( $cart_details as $cart_index => $download ) { |
| 299 |
$if_has_course = false; |
| 300 |
if ( function_exists( 'tutor_utils' ) ) { |
| 301 |
$if_has_course = tutor_utils()->product_belongs_with_course( $download['id'] ); |
| 302 |
} |
| 303 |
if ( $if_has_course ) { |
| 304 |
continue; |
| 305 |
} |
| 306 |
if ( ! $if_has_course ) { |
| 307 |
$data['title'] = $download['name']; |
| 308 |
$data['link'] = get_permalink( $download['id'] ); |
| 309 |
$data['product_id'] = $download['id']; |
| 310 |
$data['key'] = $payment->key . '-' . $download['id']; |
| 311 |
$notification = array_merge( $user_info, $data ); |
| 312 |
$notifications[] = $notification; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
return $notifications; |
| 317 |
} |
| 318 |
/* #endregion */ |
| 319 |
|
| 320 |
/** |
| 321 |
* Image action callback |
| 322 |
* @param array $image_data |
| 323 |
* @param array $data |
| 324 |
* @param stdClass $settings |
| 325 |
* @return array |
| 326 |
*/ |
| 327 |
public function notification_image($image_data, $data, $settings) { |
| 328 |
if (!$settings['show_default_image'] && $settings['show_notification_image'] === 'featured_image') { |
| 329 |
if (!empty($data['product_id']) && has_post_thumbnail($data['product_id'])) { |
| 330 |
$image_size = (string) Settings::get_instance()->get('settings.notification_image_size', '100_100'); |
| 331 |
$image_size = explode('_', $image_size); |
| 332 |
$width = 100; |
| 333 |
$height = 100; |
| 334 |
if( !empty( $image_size[0] ) && !empty( $image_size[1] ) ) { |
| 335 |
$width = $image_size[0]; |
| 336 |
$height = $image_size[1]; |
| 337 |
} |
| 338 |
$product_image = wp_get_attachment_image_src( |
| 339 |
get_post_thumbnail_id($data['product_id']), |
| 340 |
[$width, $height], |
| 341 |
false |
| 342 |
); |
| 343 |
$image_data['url'] = is_array($product_image) ? $product_image[0] : ''; |
| 344 |
} |
| 345 |
} |
| 346 |
return $image_data; |
| 347 |
} |
| 348 |
|
| 349 |
/* #region frontend */ |
| 350 |
|
| 351 |
// @todo |
| 352 |
public function fallback_data( $data, $saved_data, $settings ) { |
| 353 |
$data['anonymous_title'] = __( 'Anonymous Product', 'notificationx' ); |
| 354 |
if(empty($saved_data['product_title']) && !empty($saved_data['title'])){ |
| 355 |
$data['product_title'] = $saved_data['title']; |
| 356 |
} |
| 357 |
return $data; |
| 358 |
} |
| 359 |
/* #endregion */ |
| 360 |
|
| 361 |
public function doc(){ |
| 362 |
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> |
| 363 |
<p>👉 NotificationX <a target="_blank" href="%3$s">Integration with Easy Digital Downloads</a></p> |
| 364 |
<p><strong>Recommended Blog:</strong></p> |
| 365 |
<p>🔥 How Does <a target="_blank" href="%4$s">NotificationX Increase Sales on WordPress</a> Websites?</p>', 'notificationx'), |
| 366 |
'https://wordpress.org/plugins/easy-digital-downloads/', |
| 367 |
'https://notificationx.com/docs/notificationx-easy-digital-downloads/', |
| 368 |
'https://notificationx.com/integrations/easy-digital-downloads/', |
| 369 |
'https://wpdeveloper.com/notificationx-increase-sales-wordpress/' |
| 370 |
); |
| 371 |
} |
| 372 |
} |
| 373 |
|