| 1 |
<?php |
| 2 |
namespace LWS\WOOREWARDS\Events; |
| 3 |
|
| 4 |
// don't call the file directly |
| 5 |
if( !defined( 'ABSPATH' ) ) exit(); |
| 6 |
|
| 7 |
|
| 8 |
/** Sponsor Earns points for the first time sponsored places an order. */ |
| 9 |
class SponsoredOrder extends \LWS\WOOREWARDS\Abstracts\Event |
| 10 |
{ |
| 11 |
use \LWS\WOOREWARDS\Events\T_SponsorshipOrigin; |
| 12 |
|
| 13 |
protected $firstOrderOnly = true; |
| 14 |
protected $eventPriority = 101; |
| 15 |
|
| 16 |
function getInformation() |
| 17 |
{ |
| 18 |
return array_merge(parent::getInformation(), array( |
| 19 |
'icon' => 'lws-icon-shop', |
| 20 |
'short' => __("The customer will earn points when a person he referred placed an order. You can restrict this to the first referee order.", 'woorewards-lite'), |
| 21 |
'help' => __("This method will only reward the Referrer, not the Referee", 'woorewards-lite'), |
| 22 |
)); |
| 23 |
} |
| 24 |
|
| 25 |
function getData() |
| 26 |
{ |
| 27 |
$prefix = $this->getDataKeyPrefix(); |
| 28 |
$data = parent::getData(); |
| 29 |
$data[$prefix . 'event_priority'] = $this->getEventPriority(); |
| 30 |
$data[$prefix . 'first_order_only'] = $this->isFirstOrderOnly() ? 'on' : ''; |
| 31 |
$data = $this->filterSponsorshipData($data, $prefix); |
| 32 |
return $data; |
| 33 |
} |
| 34 |
|
| 35 |
function getForm($context='editlist') |
| 36 |
{ |
| 37 |
$prefix = $this->getDataKeyPrefix(); |
| 38 |
$form = parent::getForm($context); |
| 39 |
|
| 40 |
// just hidden since we do not want to reset the value on save |
| 41 |
$noPri = (\get_option('lws_woorewards_show_loading_order_and_priority') ? '' : ' style="display: none;"'); |
| 42 |
$label = __("Priority", 'woorewards-lite'); |
| 43 |
$tooltip = __("Customer orders will run by ascending priority value.", 'woorewards-lite'); |
| 44 |
$str = "<div class='field-help'{$noPri}>$tooltip</div>" |
| 45 |
. "<div class='lws-$context-opt-title label'{$noPri}>$label<div class='bt-field-help'>?</div></div>" |
| 46 |
. "<div class='lws-$context-opt-input value'{$noPri}>" |
| 47 |
. "<input type='text' id='{$prefix}event_priority' name='{$prefix}event_priority' placeholder='10' size='5' />" |
| 48 |
. "</div>"; |
| 49 |
$phb0 = $this->getFieldsetPlaceholder(false, 0); |
| 50 |
$form = str_replace($phb0, $str.$phb0, $form); |
| 51 |
|
| 52 |
$form .= $this->getFieldsetBegin(2, __("Options", 'woorewards-lite')); |
| 53 |
|
| 54 |
// First Order Only |
| 55 |
$label = _x("First order only", "Referral Order Event", 'woorewards-lite'); |
| 56 |
$tooltip = __("If checked, only the first order placed by each referee will give points.", 'woorewards-lite'); |
| 57 |
$toggle = \LWS\Adminpanel\Pages\Field\Checkbox::compose($prefix . 'first_order_only', array( |
| 58 |
'id' => $prefix . 'first_order_only', |
| 59 |
'layout' => 'toggle', |
| 60 |
)); |
| 61 |
$form .= "<div class='field-help'>$tooltip</div>"; |
| 62 |
$form .= "<div class='lws-$context-opt-title label'>$label<div class='bt-field-help'>?</div></div>"; |
| 63 |
$form .= "<div class='lws-$context-opt-input value'>$toggle</div>"; |
| 64 |
|
| 65 |
$form .= $this->getFieldsetEnd(2); |
| 66 |
return $this->filterSponsorshipForm($form, $prefix, $context, 10); |
| 67 |
} |
| 68 |
|
| 69 |
function submit($form=array(), $source='editlist') |
| 70 |
{ |
| 71 |
$prefix = $this->getDataKeyPrefix(); |
| 72 |
$values = \apply_filters('lws_adminpanel_arg_parse', array( |
| 73 |
'post' => ($source == 'post'), |
| 74 |
'values' => $form, |
| 75 |
'format' => array( |
| 76 |
$prefix . 'first_order_only' => 's', |
| 77 |
$prefix . 'event_priority' => 'd', |
| 78 |
), |
| 79 |
'defaults' => array( |
| 80 |
$prefix . 'first_order_only' => '', |
| 81 |
$prefix . 'event_priority' => $this->getEventPriority(), |
| 82 |
), |
| 83 |
'labels' => array( |
| 84 |
$prefix . 'first_order_only' => __("First order only", 'woorewards-lite'), |
| 85 |
$prefix . 'event_priority' => __("Event priority", 'woorewards-lite'), |
| 86 |
) |
| 87 |
)); |
| 88 |
if( !(isset($values['valid']) && $values['valid']) ) |
| 89 |
return isset($values['error']) ? $values['error'] : false; |
| 90 |
|
| 91 |
$valid = parent::submit($form, $source); |
| 92 |
if (true !== $valid) |
| 93 |
return $valid; |
| 94 |
$valid = $this->optSponsorshipSubmit($prefix, $form, $source); |
| 95 |
if (true !== $valid) |
| 96 |
return $valid; |
| 97 |
|
| 98 |
$this->setFirstOrderOnly($values['values'][$prefix.'first_order_only']); |
| 99 |
$this->setEventPriority ($values['values'][$prefix.'event_priority']); |
| 100 |
return $valid; |
| 101 |
} |
| 102 |
|
| 103 |
public function setFirstOrderOnly($yes=false) |
| 104 |
{ |
| 105 |
$this->firstOrderOnly = boolval($yes); |
| 106 |
return $this; |
| 107 |
} |
| 108 |
|
| 109 |
function isFirstOrderOnly() |
| 110 |
{ |
| 111 |
return $this->firstOrderOnly; |
| 112 |
} |
| 113 |
|
| 114 |
function isGuestAllowed() |
| 115 |
{ |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
/** Inhereted Event already instanciated from WP_Post, $this->id is availble. It is up to you to load any extra configuration. */ |
| 120 |
protected function _fromPost(\WP_Post $post) |
| 121 |
{ |
| 122 |
$firstOnly = \get_post_meta($post->ID, 'wre_event_first_order_only', false); // backward compatibility, option introduced on 3.6.0 |
| 123 |
$this->setFirstOrderOnly( empty($firstOnly) ? 'on' : reset($firstOnly) ); |
| 124 |
$this->setEventPriority($this->getSinglePostMeta($post->ID, 'wre_event_priority', $this->getEventPriority())); |
| 125 |
$this->optSponsorshipFromPost($post); |
| 126 |
return $this; |
| 127 |
} |
| 128 |
|
| 129 |
/** Event already saved as WP_Post, $this->id is availble. It is up to you to save any extra configuration. */ |
| 130 |
protected function _save($id) |
| 131 |
{ |
| 132 |
\update_post_meta($id, 'wre_event_first_order_only', $this->isFirstOrderOnly() ? 'on' : ''); |
| 133 |
\update_post_meta($id, 'wre_event_priority', $this->getEventPriority()); |
| 134 |
$this->optSponsorshipSave($id); |
| 135 |
return $this; |
| 136 |
} |
| 137 |
|
| 138 |
function getDescription($context='backend') |
| 139 |
{ |
| 140 |
$descr = parent::getDescription($context); |
| 141 |
if( $this->isFirstOrderOnly() ) |
| 142 |
{ |
| 143 |
$descr .= __(" (first order only)", 'woorewards-lite'); |
| 144 |
} |
| 145 |
return $descr; |
| 146 |
} |
| 147 |
|
| 148 |
/** @return string a human readable type for UI */ |
| 149 |
public function getDisplayType() |
| 150 |
{ |
| 151 |
return _x("Referee orders", "getDisplayType", 'woorewards-lite'); |
| 152 |
} |
| 153 |
|
| 154 |
function getEventPriority() |
| 155 |
{ |
| 156 |
return \intval($this->eventPriority); |
| 157 |
} |
| 158 |
|
| 159 |
public function setEventPriority($priority) |
| 160 |
{ |
| 161 |
$this->eventPriority = \intval($priority); |
| 162 |
return $this; |
| 163 |
} |
| 164 |
|
| 165 |
/** Add hook to grab events and add points. */ |
| 166 |
protected function _install() |
| 167 |
{ |
| 168 |
\add_filter('lws_woorewards_wc_order_done_'.$this->getPoolName(), array($this, 'orderDone'), $this->getEventPriority()); |
| 169 |
} |
| 170 |
|
| 171 |
function orderDone($order, $sponsorshipInfo=false) |
| 172 |
{ |
| 173 |
if ($sponsorshipInfo) { |
| 174 |
$this->sponsorship = $sponsorshipInfo; |
| 175 |
} else { |
| 176 |
$sponsorship = new \LWS\WOOREWARDS\Core\Sponsorship(); |
| 177 |
$this->sponsorship = $sponsorship->getUsersFromOrder($order->order, $this->isGuestAllowed()); |
| 178 |
} |
| 179 |
|
| 180 |
if( !$this->sponsorship->sponsor_id ) |
| 181 |
return $order; |
| 182 |
if( !$this->isValidOrigin($this->sponsorship->origin) ) |
| 183 |
return $order; |
| 184 |
|
| 185 |
if( $this->isFirstOrderOnly() ) |
| 186 |
{ |
| 187 |
$c = \LWS\WOOREWARDS\Conveniences::getOrderCount( |
| 188 |
$this->sponsorship->sponsored_id, |
| 189 |
$order->order->get_id(), |
| 190 |
'sponsorship', |
| 191 |
$this->sponsorship->sponsored_email |
| 192 |
); |
| 193 |
if ($c) |
| 194 |
return $order; |
| 195 |
} |
| 196 |
|
| 197 |
if( $points = \apply_filters('trigger_'.$this->getType(), 1, $this, $order->order) ) |
| 198 |
{ |
| 199 |
$reason = \LWS\WOOREWARDS\Core\Trace::byOrder($order->order) |
| 200 |
->setProvider($order->order->get_customer_id('edit')) |
| 201 |
->setReason( |
| 202 |
array( |
| 203 |
"Referred friend %1\$s order #%2\$s completed", |
| 204 |
$this->sponsorship->sponsored_email, |
| 205 |
$order->order->get_order_number() |
| 206 |
), |
| 207 |
'woorewards-lite' |
| 208 |
); |
| 209 |
|
| 210 |
$this->addPoint(array( |
| 211 |
'user' => $this->sponsorship->sponsor_id, |
| 212 |
'sponsee' => $this->sponsorship->sponsored_id, |
| 213 |
'order' => $order->order, |
| 214 |
), $reason, $points); |
| 215 |
} |
| 216 |
return $order; |
| 217 |
} |
| 218 |
|
| 219 |
/** Never call, only to have poedit/wpml able to extract the sentance. */ |
| 220 |
private function poeditDeclare() |
| 221 |
{ |
| 222 |
/* translators: %1$s: friend name, %2$s: order number */ |
| 223 |
__("Referred friend %1\$s order #%2\$s completed", 'woorewards-lite'); |
| 224 |
} |
| 225 |
|
| 226 |
/** Event categories, used to filter out events from pool options. |
| 227 |
* @return array with category_id => category_label. */ |
| 228 |
public function getCategories() |
| 229 |
{ |
| 230 |
return array_merge(parent::getCategories(), array( |
| 231 |
'woocommerce' => __("WooCommerce", 'woorewards-lite'), |
| 232 |
'sponsorship' => __("Available for referees", 'woorewards-lite') |
| 233 |
)); |
| 234 |
} |
| 235 |
} |