PluginProbe
MyRewards / trunk
MyRewards vtrunk
5.7.8 5.7.7 5.7.6 trunk 1.2.2 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.1 2.2.0 2.3.0 2.4.0 2.4.1 2.5.0 2.6.4 2.6.6 3.0.0.0 3.1.0 3.1.2 3.1.2.1 3.10.4 3.10.9 All 165 releases
woorewards / include / events / firstorder.php

firstorder.php in MyRewards trunk, at include/events/firstorder.php

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