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 / ui / woocommerce / ordersbulk.php

ordersbulk.php in MyRewards trunk, at include/ui/woocommerce/ordersbulk.php

108 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace LWS\WOOREWARDS\Ui\Woocommerce;
3
4 // don't call the file directly
5 if( !defined( 'ABSPATH' ) ) exit();
6
7 /** Add an entry in Orders list bluk action.
8 * Allow massive points remove (reset processed flags).
9 * Allow massive points process (reset refund flags).
10 */
11 class OrdersBulk
12 {
13 const ACTION_PROCESS_POINTS = 'lws_wr_process_points';
14 const ACTION_RESULT = 'lws_wr_bulk_counts';
15
16 private $points = 0;
17 private $processed = array();
18 private $checked = 0;
19
20 static function install()
21 {
22 $me = new self();
23 $screens = array('woocommerce_page_wc-orders', 'edit-shop_order');
24 foreach ($screens as $screen) {
25 \add_filter('bulk_actions-' . $screen, array($me, 'addActions'), 900, 1);
26 \add_filter('handle_bulk_actions-' . $screen, array($me, 'handleActions'), 10, 3);
27 }
28 \add_action('admin_notices', array($me, 'notice'));
29 }
30
31 static function getLabel()
32 {
33 return \apply_filters('lws_woorewards_orderbulk_action_process_points_label', __("Process MyRewards Points", 'woorewards-lite'));
34 }
35
36 public function addActions($actions)
37 {
38 $actions[self::ACTION_PROCESS_POINTS] = self::getLabel();
39 return $actions;
40 }
41
42 public function handleActions($redirectTo, $action, $postIds)
43 {
44 $this->resetCounters();
45 if (self::ACTION_PROCESS_POINTS === $action) {
46 // for stats
47 \add_action('lws_woorewards_wc_order_trigger_order_done', array($this, 'addProcessed'), 10, 3);
48 \add_filter('lws_woorewards_core_pool_point_add', array($this, 'addPoints'), 10, 2);
49 // process past orders
50 foreach ($postIds as $postId) {
51 $order = \wc_get_order($postId);
52 if ($order) {
53 \do_action('lws_woorewards_pool_on_order_done', $postId, $order);
54 $this->checked++;
55 }
56 }
57 // for stats
58 \remove_action('lws_woorewards_wc_order_trigger_order_done', array($this, 'addProcessed'), 10);
59 \remove_filter('lws_woorewards_core_pool_point_add', array($this, 'addPoints'), 10);
60
61 return \add_query_arg(array(
62 self::ACTION_RESULT => \implode('_', array($this->checked, count($this->processed), $this->points)),
63 ), $redirectTo);
64 } else {
65 return $redirectTo;
66 }
67 }
68
69 public function addProcessed($action, $order, $pool)
70 {
71 $orderId = $order->get_id();
72 $this->processed[$orderId] = true;
73 }
74
75 public function addPoints($value, $userId)
76 {
77 try{
78 $this->points += $value;
79 } catch(\Exception $e){
80 $this->points = PHP_INT_MAX ; // overflow
81 }
82 return $value;
83 }
84
85 protected function resetCounters()
86 {
87 $this->points = 0;
88 $this->processed = array();
89 $this->checked = 0;
90 }
91
92 public function notice()
93 {
94 $count_safe = isset($_REQUEST[self::ACTION_RESULT]) ? \sanitize_key(\wp_unslash($_REQUEST[self::ACTION_RESULT])) : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- result display after redirect
95 if (false !== $count_safe) {
96 $counts = \array_map('\intval', \explode('_', $count_safe));
97 while (count($counts) < 3)
98 $counts[] = 0;
99 list($checked, $processed, $points) = $counts;
100
101 if ($checked > 0) {
102 /* translators: %1$d: orders checked, %2$d: orders processed, %3$d: points total */
103 $content = sprintf(__('<b>%1$d</b> orders verified, including <b>%2$d</b> processed for a sum of <b>%3$d</b> points.', 'woorewards-lite'), $checked, $processed, $points);
104 echo wp_kses_post("<div class='notice notice-success lws-wr-order-bulk-action'><p>{$content}</p></div>");
105 }
106 }
107 }
108 }