| @@ -1,0 +1,310 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Give Extension | |
| 5 | + * | |
| 6 | + * @package NotificationX\Extensions | |
| 7 | + */ | |
| 8 | + | |
| 9 | +namespace NotificationX\Extensions\Give; | |
| 10 | + | |
| 11 | +use NotificationX\Core\Rules; | |
| 12 | +use NotificationX\GetInstance; | |
| 13 | +use NotificationX\Extensions\Extension; | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * Give Extension | |
| 17 | + * @method static Give get_instance($args = null) | |
| 18 | + */ | |
| 19 | +class Give extends Extension { | |
| 20 | + /** | |
| 21 | + * Instance of Admin | |
| 22 | + * | |
| 23 | + * @var Admin | |
| 24 | + */ | |
| 25 | + use GetInstance; | |
| 26 | + | |
| 27 | + public $priority = 5; | |
| 28 | + public $id = 'give'; | |
| 29 | + public $img = NOTIFICATIONX_ADMIN_URL . 'images/extensions/sources/give.png'; | |
| 30 | + public $doc_link = 'https://notificationx.com/docs/givewp-donation-alert/'; | |
| 31 | + public $types = 'donation'; | |
| 32 | + public $module = 'modules_give'; | |
| 33 | + public $module_priority = 6; | |
| 34 | + public $version = '1.2.5'; | |
| 35 | + public $class = '\Give'; | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * Initially Invoked when initialized. | |
| 39 | + */ | |
| 40 | + public function __construct() { | |
| 41 | + parent::__construct(); | |
| 42 | + } | |
| 43 | + | |
| 44 | + public function init_extension() | |
| 45 | + { | |
| 46 | + $this->title = __('Give', 'notificationx'); | |
| 47 | + $this->module_title = __('GiveWP', 'notificationx'); | |
| 48 | + } | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * This functions is hooked | |
| 52 | + * | |
| 53 | + * @return void | |
| 54 | + */ | |
| 55 | + public function admin_actions() { | |
| 56 | + parent::admin_actions(); | |
| 57 | + | |
| 58 | + // @todo Something | |
| 59 | + add_filter("nx_can_entry_{$this->id}", array($this, 'limit_by_selected_form'), 10, 3); | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * This functions is hooked | |
| 64 | + * | |
| 65 | + * @hooked nx_public_action | |
| 66 | + * @return void | |
| 67 | + */ | |
| 68 | + public function public_actions() { | |
| 69 | + parent::public_actions(); | |
| 70 | + // public actions will be here | |
| 71 | + add_action('give_complete_donation', [$this, 'save_new_donation'], 10, 1); | |
| 72 | + add_filter("nx_filtered_entry_{$this->id}", array($this, 'conversion_data'), 10, 2); | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * @param array $data | |
| 77 | + * @param array $saved_data | |
| 78 | + * @param stdClass $settings | |
| 79 | + * @return array | |
| 80 | + */ | |
| 81 | + public function fallback_data($data, $saved_data, $settings) { | |
| 82 | + $data['name'] = __('Someone', 'notificationx'); | |
| 83 | + $data['first_name'] = __('Someone', 'notificationx'); | |
| 84 | + $data['last_name'] = __('Someone', 'notificationx'); | |
| 85 | + $data['anonymous_title'] = __('Anonymous Product', 'notificationx'); | |
| 86 | + $data['sometime'] = __('Some time ago', 'notificationx'); | |
| 87 | + return $data; | |
| 88 | + } | |
| 89 | + | |
| 90 | + // @todo Frontend | |
| 91 | + public function conversion_data($saved_data, $settings) { | |
| 92 | + if (isset($saved_data['amount']) && function_exists('give_currency_filter')) { | |
| 93 | + $saved_data['amount'] = html_entity_decode(\give_currency_filter($saved_data['amount'])); | |
| 94 | + } | |
| 95 | + return $saved_data; | |
| 96 | + } | |
| 97 | + | |
| 98 | + public function source_error_message($messages) { | |
| 99 | + if (!$this->class_exists()) { | |
| 100 | + $url = admin_url('plugin-install.php?s=give&tab=search&type=term'); | |
| 101 | + $messages[$this->id] = [ | |
| 102 | + 'message' => sprintf( '%s <a href="%s" target="_blank">%s</a> %s', | |
| 103 | + __( 'You have to install', 'notificationx' ), | |
| 104 | + $url, | |
| 105 | + __( 'GiveWP Donation', 'notificationx' ), | |
| 106 | + __( 'plugin first.', 'notificationx' ) | |
| 107 | + ), | |
| 108 | + 'html' => true, | |
| 109 | + 'type' => 'error', | |
| 110 | + 'rules' => Rules::is('source', $this->id), | |
| 111 | + ]; | |
| 112 | + } | |
| 113 | + return $messages; | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * Image action callback | |
| 118 | + * @param array $image_data | |
| 119 | + * @param array $data | |
| 120 | + * @param array $settings | |
| 121 | + * @return array | |
| 122 | + */ | |
| 123 | + public function notification_image($image_data, $data, $settings) { | |
| 124 | + if (!$settings['show_default_image'] && $settings['show_notification_image'] === 'featured_image') { | |
| 125 | + if (!empty($data['give_form_id'])) { | |
| 126 | + $image_data['url'] = get_the_post_thumbnail_url($data['give_form_id'], 'thumbnail'); | |
| 127 | + } | |
| 128 | + if (empty($image_data['url']) && !empty($data['give_page_id'])) { | |
| 129 | + $image_data['url'] = get_the_post_thumbnail_url($data['give_page_id'], 'thumbnail'); | |
| 130 | + } | |
| 131 | + } | |
| 132 | + return $image_data; | |
| 133 | + } | |
| 134 | + | |
| 135 | + /** | |
| 136 | + * This function is generate and save a new notification when a new donation occur | |
| 137 | + * @param object $donation | |
| 138 | + * @return void | |
| 139 | + */ | |
| 140 | + public function save_new_donation($payment_id) { | |
| 141 | + if (empty($payment_id)) { | |
| 142 | + return; | |
| 143 | + } | |
| 144 | + $args = array( | |
| 145 | + 'post__in' => [$payment_id] | |
| 146 | + ); | |
| 147 | + | |
| 148 | + $result = new \Give_Payments_Query($args); | |
| 149 | + $result = $result->get_payments(); | |
| 150 | + if (!empty($result[0])) { | |
| 151 | + $result = $result[0]; | |
| 152 | + $key = $result->ID . '-' . $result->form_id; | |
| 153 | + // if (!in_array($key, $this->notifications)) { | |
| 154 | + $donation_data = array_merge(array( | |
| 155 | + 'id' => $result->ID, | |
| 156 | + 'title' => $result->form_title, | |
| 157 | + 'amount' => $result->total . ' ' . __('for', 'notificationx'), | |
| 158 | + 'link' => isset($result->payment_meta['_give_current_url']) ? $result->payment_meta['_give_current_url'] : '', | |
| 159 | + 'give_form_id' => $result->form_id, | |
| 160 | + 'give_page_id' => isset($result->payment_meta['_give_current_page_id']) ? $result->payment_meta['_give_current_page_id'] : '', | |
| 161 | + 'timestamp' => get_gmt_from_date($result->date), | |
| 162 | + ), $this->get_donor($result)); | |
| 163 | + | |
| 164 | + $this->update_notification([ | |
| 165 | + 'source' => $this->id, | |
| 166 | + 'entry_key' => $key, | |
| 167 | + 'data' => $donation_data, | |
| 168 | + ]); | |
| 169 | + // } | |
| 170 | + } | |
| 171 | + } | |
| 172 | + | |
| 173 | + public function saved_post($post, $data, $nx_id) { | |
| 174 | + $this->delete_notification(null, $nx_id); | |
| 175 | + $this->get_notification_ready($data); | |
| 176 | + } | |
| 177 | + | |
| 178 | + /** | |
| 179 | + * This function is responsible for making the notification ready for first time we make the notification. | |
| 180 | + * | |
| 181 | + * @param string $type | |
| 182 | + * @param array $data | |
| 183 | + * @return void | |
| 184 | + */ | |
| 185 | + public function get_notification_ready($data = array()) { | |
| 186 | + $donations = $this->get_give_donations($data); | |
| 187 | + if (!empty($donations)) { | |
| 188 | + // $this->update_notification($donations, null, $data['nx_id']); | |
| 189 | + $entries = []; | |
| 190 | + foreach ($donations as $key => $donation) { | |
| 191 | + $entries[] = [ | |
| 192 | + 'nx_id' => $data['nx_id'], | |
| 193 | + 'source' => $this->id, | |
| 194 | + 'entry_key' => $key, | |
| 195 | + 'data' => $donation, | |
| 196 | + ]; | |
| 197 | + } | |
| 198 | + $this->update_notifications($entries); | |
| 199 | + } | |
| 200 | + } | |
| 201 | + /** | |
| 202 | + * Get previous give donations after select as source | |
| 203 | + * @param array $data | |
| 204 | + * @return array | |
| 205 | + */ | |
| 206 | + protected function get_give_donations($data) { | |
| 207 | + if (empty($data)) { | |
| 208 | + return null; | |
| 209 | + } | |
| 210 | + $donations = []; | |
| 211 | + $from = gmdate(get_option('date_format'), strtotime('-' . intval($data['display_from']) . ' days')); | |
| 212 | + $args = array( | |
| 213 | + 'number' => -1, | |
| 214 | + 'date_query' => array( | |
| 215 | + array( | |
| 216 | + 'after' => $from, | |
| 217 | + ), | |
| 218 | + ), | |
| 219 | + ); | |
| 220 | + | |
| 221 | + if( ! class_exists('\Give_Payments_Query') ) { | |
| 222 | + return []; | |
| 223 | + } | |
| 224 | + | |
| 225 | + $results = new \Give_Payments_Query($args); | |
| 226 | + $results = $results->get_payments(); | |
| 227 | + if (!empty($results)) { | |
| 228 | + foreach ($results as $result) { | |
| 229 | + $donations[$result->ID . '-' . $result->form_id] = array_merge( | |
| 230 | + array( | |
| 231 | + 'id' => $result->ID, | |
| 232 | + 'title' => $result->form_title, | |
| 233 | + 'link' => isset($result->payment_meta['_give_current_url']) ? $result->payment_meta['_give_current_url'] : '', | |
| 234 | + 'give_form_id' => $result->form_id, | |
| 235 | + 'amount' => $result->total . ' ' . __('for', 'notificationx'), | |
| 236 | + 'give_page_id' => isset($result->payment_meta['_give_current_page_id']) ? $result->payment_meta['_give_current_page_id'] : '', | |
| 237 | + 'timestamp' => get_gmt_from_date($result->date), | |
| 238 | + ), | |
| 239 | + $this->get_donor($result) | |
| 240 | + ); | |
| 241 | + } | |
| 242 | + } | |
| 243 | + return $donations; | |
| 244 | + } | |
| 245 | + | |
| 246 | + /** | |
| 247 | + * Get donor information | |
| 248 | + * @param object $donation | |
| 249 | + * @return array | |
| 250 | + */ | |
| 251 | + protected function get_donor($donation) { | |
| 252 | + $user_data = []; | |
| 253 | + $first_name = $donation->first_name; | |
| 254 | + $last_name = $donation->last_name; | |
| 255 | + if (!empty($first_name)) { | |
| 256 | + $user_data['first_name'] = $first_name; | |
| 257 | + } else { | |
| 258 | + $user_data['first_name'] = ''; | |
| 259 | + } | |
| 260 | + if (!empty($last_name)) { | |
| 261 | + $user_data['last_name'] = $last_name; | |
| 262 | + } else { | |
| 263 | + $user_data['last_name'] = ''; | |
| 264 | + } | |
| 265 | + $user_data['name'] = trim($user_data['first_name'] . ' ' . mb_substr($user_data['last_name'], 0, 1)); | |
| 266 | + $user_data['email'] = $donation->email; | |
| 267 | + $user_data['country'] = $donation->address['country']; | |
| 268 | + $user_data['city'] = $donation->address['city']; | |
| 269 | + $user_data['ip'] = give_get_payment_user_ip($donation->ID); | |
| 270 | + | |
| 271 | + return $user_data; | |
| 272 | + } | |
| 273 | + | |
| 274 | + /** | |
| 275 | + * Hooked with nx_can_entry_give | |
| 276 | + * | |
| 277 | + * @param [type] $return | |
| 278 | + * @param [type] $entry | |
| 279 | + * @param [type] $settings | |
| 280 | + * @return void | |
| 281 | + */ | |
| 282 | + public function limit_by_selected_form( $return, $entry, $settings ){ | |
| 283 | + if( empty( $settings['give_forms_control'] ) || empty( $settings['give_form_list'] ) || $settings['give_forms_control'] === 'none' ) { | |
| 284 | + return $return; | |
| 285 | + } | |
| 286 | + | |
| 287 | + if($settings['give_forms_control'] === 'give_form' && !empty($entry['data']['give_form_id'])){ | |
| 288 | + $give_form_id = $entry['data']['give_form_id']; | |
| 289 | + if(!in_array($give_form_id, $settings['give_form_list'])){ | |
| 290 | + return false; | |
| 291 | + } | |
| 292 | + } | |
| 293 | + return $return; | |
| 294 | + } | |
| 295 | + | |
| 296 | + public function doc() { | |
| 297 | + /* translators: %1$s: GiveWP installed & configured link URL, %2$s: documentation link URL, %3$s: Watch video tutorial link URL, %4$s: Integration with GiveWP link URL, %5$s: NotificationX Increase Sales on WordPress link URL */ | |
| 298 | + return sprintf(__('<p>Make sure that you have <a target="_blank" href="%1$s">GiveWP installed & configured</a> to use its campaign & donars data. For further assistance, check out our step by step <a href="%2$s">documentation</a>.</p> | |
| 299 | + <p>🎦 <a target="_blank" href="%3$s">Watch video tutorial</a> to learn quickly</p> | |
| 300 | + <p>👉 NotificationX <a target="_blank" href="%4$s">Integration with GiveWP</a></p> | |
| 301 | + <p><strong>Recommended Blog:</strong></p> | |
| 302 | + <p>🔥 How Does <a target="_blank" href="%5$s">NotificationX Increase Sales on WordPress</a> Websites?"</p>', 'notificationx'), | |
| 303 | + 'https://wordpress.org/plugins/give/', | |
| 304 | + 'https://notificationx.com/docs/givewp-donation-alert/', | |
| 305 | + 'https://www.youtube.com/watch?v=8EFgHSA8mOg', | |
| 306 | + 'https://notificationx.com/integrations/givewp/', | |
| 307 | + 'https://wpdeveloper.com/notificationx-increase-sales-wordpress/' | |
| 308 | + ); | |
| 309 | + } | |
| 310 | +} | |