| 1 |
<?php |
| 2 |
namespace LWS\WOOREWARDS\Events; |
| 3 |
|
| 4 |
// don't call the file directly |
| 5 |
if( !defined( 'ABSPATH' ) ) exit(); |
| 6 |
|
| 7 |
/** Earn point each time a customer complete an order. */ |
| 8 |
class OrderCompleted extends \LWS\WOOREWARDS\Abstracts\Event |
| 9 |
{ |
| 10 |
use \LWS\WOOREWARDS\Events\T_SponsorshipOrigin; |
| 11 |
|
| 12 |
protected $eventPriority = 102; |
| 13 |
|
| 14 |
function getInformation() |
| 15 |
{ |
| 16 |
return array_merge(parent::getInformation(), array( |
| 17 |
'icon' => 'lws-icon-shop', |
| 18 |
'color' => '#cc1d25', |
| 19 |
'short' => __("The customer will receive points upon completing an order", 'woorewards-lite'), |
| 20 |
'help' => __("This method can also be used to give extra points on a first order", 'woorewards-lite'), |
| 21 |
)); |
| 22 |
} |
| 23 |
|
| 24 |
public function getDisplayType() |
| 25 |
{ |
| 26 |
return _x("Place an order", "getDisplayType", 'woorewards-lite'); |
| 27 |
} |
| 28 |
|
| 29 |
function getData() |
| 30 |
{ |
| 31 |
$prefix = $this->getDataKeyPrefix(); |
| 32 |
$data = parent::getData(); |
| 33 |
$data[$prefix.'event_priority'] = $this->getEventPriority(); |
| 34 |
$data = $this->filterSponsorshipData($data, $prefix); |
| 35 |
return $data; |
| 36 |
} |
| 37 |
|
| 38 |
function getForm($context='editlist') |
| 39 |
{ |
| 40 |
$prefix = $this->getDataKeyPrefix(); |
| 41 |
$form = parent::getForm($context); |
| 42 |
|
| 43 |
// just hidden since we do not want to reset the value on save |
| 44 |
$noPri = (\get_option('lws_woorewards_show_loading_order_and_priority') ? '' : ' style="display: none;"'); |
| 45 |
$label = __("Priority", 'woorewards-lite'); |
| 46 |
$tooltip = __("Customer orders will run by ascending priority value.", 'woorewards-lite'); |
| 47 |
$str = "<div class='field-help'{$noPri}>$tooltip</div>" |
| 48 |
. "<div class='lws-$context-opt-title label'{$noPri}>$label<div class='bt-field-help'>?</div></div>" |
| 49 |
. "<div class='lws-$context-opt-input value'{$noPri}>" |
| 50 |
. "<input type='text' id='{$prefix}event_priority' name='{$prefix}event_priority' placeholder='10' size='5' />" |
| 51 |
. "</div>"; |
| 52 |
|
| 53 |
$phb0 = $this->getFieldsetPlaceholder(false, 0); |
| 54 |
$form = str_replace($phb0, $str.$phb0, $form); |
| 55 |
|
| 56 |
return $this->filterSponsorshipForm($form, $prefix, $context, 10); |
| 57 |
} |
| 58 |
|
| 59 |
function submit($form=array(), $source='editlist') |
| 60 |
{ |
| 61 |
$prefix = $this->getDataKeyPrefix(); |
| 62 |
$values = \apply_filters('lws_adminpanel_arg_parse', array( |
| 63 |
'post' => ($source == 'post'), |
| 64 |
'values' => $form, |
| 65 |
'format' => array( |
| 66 |
$prefix.'event_priority' => 'd', |
| 67 |
), |
| 68 |
'defaults' => array( |
| 69 |
$prefix.'event_priority' => $this->getEventPriority(), |
| 70 |
), |
| 71 |
'labels' => array( |
| 72 |
$prefix.'event_priority' => __("Event Priority", 'woorewards-lite'), |
| 73 |
) |
| 74 |
)); |
| 75 |
if( !(isset($values['valid']) && $values['valid']) ) |
| 76 |
return isset($values['error']) ? $values['error'] : false; |
| 77 |
|
| 78 |
$valid = parent::submit($form, $source); |
| 79 |
if ($valid === true) |
| 80 |
$valid = $this->optSponsorshipSubmit($prefix, $form, $source); |
| 81 |
if( $valid === true ) |
| 82 |
{ |
| 83 |
$this->setEventPriority ($values['values'][$prefix.'event_priority']); |
| 84 |
} |
| 85 |
return $valid; |
| 86 |
} |
| 87 |
|
| 88 |
protected function _fromPost(\WP_Post $post) |
| 89 |
{ |
| 90 |
$this->setEventPriority($this->getSinglePostMeta($post->ID, 'wre_event_priority', $this->getEventPriority())); |
| 91 |
$this->optSponsorshipFromPost($post); |
| 92 |
return $this; |
| 93 |
} |
| 94 |
|
| 95 |
protected function _save($id) |
| 96 |
{ |
| 97 |
\update_post_meta($id, 'wre_event_priority', $this->getEventPriority()); |
| 98 |
$this->optSponsorshipSave($id); |
| 99 |
return $this; |
| 100 |
} |
| 101 |
|
| 102 |
function getEventPriority() |
| 103 |
{ |
| 104 |
return \intval($this->eventPriority); |
| 105 |
} |
| 106 |
|
| 107 |
public function setEventPriority($priority) |
| 108 |
{ |
| 109 |
$this->eventPriority = \intval($priority); |
| 110 |
return $this; |
| 111 |
} |
| 112 |
|
| 113 |
protected function _install() |
| 114 |
{ |
| 115 |
\add_filter('lws_woorewards_wc_order_done_'.$this->getPoolName(), array($this, 'orderDone'), $this->getEventPriority()); |
| 116 |
} |
| 117 |
|
| 118 |
function orderDone($order) |
| 119 |
{ |
| 120 |
$userId = \LWS\Adminpanel\Tools\Conveniences::getCustomerId(false, $order->order); |
| 121 |
if (!$userId) |
| 122 |
return $order; |
| 123 |
if (!$this->isValidOriginByOrder($order->order, $this->isGuestAllowed())) |
| 124 |
return $order; |
| 125 |
|
| 126 |
if( $points = \apply_filters('trigger_'.$this->getType(), 1, $this, $order->order) ) |
| 127 |
{ |
| 128 |
$reason = \LWS\WOOREWARDS\Core\Trace::byOrder($order->order) |
| 129 |
->setProvider($order->order->get_customer_id('edit')) |
| 130 |
->setReason(array("Order #%s completed", $order->order->get_order_number()), 'woorewards-lite'); |
| 131 |
|
| 132 |
$this->addPoint(array( |
| 133 |
'user' => $userId, |
| 134 |
'order' => $order->order, |
| 135 |
), $reason, $points); |
| 136 |
} |
| 137 |
return $order; |
| 138 |
} |
| 139 |
|
| 140 |
/** Never call, only to have poedit/wpml able to extract the sentance. */ |
| 141 |
private function poeditDeclare() |
| 142 |
{ |
| 143 |
/* translators: %s: order number */ |
| 144 |
__("Order #%s completed", 'woorewards-lite'); |
| 145 |
} |
| 146 |
|
| 147 |
/** Event categories, used to filter out events from pool options. |
| 148 |
* @return array with category_id => category_label. */ |
| 149 |
public function getCategories() |
| 150 |
{ |
| 151 |
return array_merge(parent::getCategories(), array( |
| 152 |
'woocommerce' => __("WooCommerce", 'woorewards-lite'), |
| 153 |
'order' => __("Order", 'woorewards-lite') |
| 154 |
)); |
| 155 |
} |
| 156 |
|
| 157 |
function isGuestAllowed() |
| 158 |
{ |
| 159 |
return false; |
| 160 |
} |
| 161 |
} |