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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / ecommerce / OrderActivitiesController.php
tutor / ecommerce Last commit date
PaymentGateways 1 year ago AdminMenu.php 1 year ago BillingController.php 1 year ago CartController.php 1 year ago CheckoutController.php 1 year ago CouponController.php 1 year ago Ecommerce.php 1 year ago EmailController.php 1 year ago HooksHandler.php 1 year ago OptionKeys.php 1 year ago OrderActivitiesController.php 1 year ago OrderController.php 1 year ago PaymentHandler.php 1 year ago Settings.php 1 year ago Tax.php 1 year ago currency.php 1 year ago
OrderActivitiesController.php
177 lines
1 <?php
2 /**
3 * Manage Order
4 *
5 * @package Tutor\Ecommerce
6 * @author Themeum <support@themeum.com>
7 * @link https://themeum.com
8 * @since 3.0.0
9 */
10
11 namespace Tutor\Ecommerce;
12
13 use Tutor\Helpers\HttpHelper;
14 use Tutor\Helpers\ValidationHelper;
15 use Tutor\Models\OrderActivitiesModel;
16 use Tutor\Traits\JsonResponse;
17
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit;
20 }
21 /**
22 * OrderActivitiesController class
23 *
24 * @since 3.0.0
25 */
26 class OrderActivitiesController {
27
28 /**
29 * Order model
30 *
31 * @since 3.0.0
32 *
33 * @var Object
34 */
35 private $model;
36
37 /**
38 * Trait for sending JSON response
39 */
40 use JsonResponse;
41
42 /**
43 * Page Title
44 *
45 * @var $page_title
46 */
47 public $page_title;
48
49 /**
50 * Constructor.
51 *
52 * Initializes the Orders class, sets the page title, and optionally registers
53 * hooks for handling AJAX requests related to order data, bulk actions, order status updates,
54 * and order deletions.
55 *
56 * @since 3.0.0
57 *
58 * @return void
59 */
60 public function __construct() {
61 $this->model = new OrderActivitiesModel();
62 }
63
64 /**
65 * Store order activity.
66 *
67 * This function handles the process of storing order activity metadata in the database.
68 * It triggers actions before and after adding the order activity, sanitizes input data,
69 * validates the request, and constructs a payload object. The payload is then passed to
70 * the OrderActivitiesModel to be stored in the database.
71 *
72 * @since 3.0.0
73 *
74 * @param int $order_id The ID of the order.
75 * @param string $meta_key The meta key for the order activity.
76 * @param string $meta_value The meta value for the order activity.
77 *
78 * @return void
79 */
80 public static function store_order_activity( int $order_id, string $meta_key, string $meta_value ) {
81 $params = array(
82 'order_id' => $order_id,
83 'meta_key' => $meta_key,
84 'meta_value' => $meta_value,
85 );
86
87 do_action( 'tutor_before_adding_order_activity', $params );
88
89 // Validate request.
90 $validation = self::validate( $params );
91 if ( ! $validation->success ) {
92 self::json_response(
93 tutor_utils()->error_message( HttpHelper::STATUS_BAD_REQUEST ),
94 $validation->errors,
95 HttpHelper::STATUS_BAD_REQUEST
96 );
97 }
98
99 $payload = new \stdClass();
100 $payload->order_id = $params['order_id'];
101 $payload->meta_key = $params['meta_key'];
102 $payload->meta_value = $params['meta_value'];
103
104 $model = new OrderActivitiesModel();
105 $model->add_order_meta( $payload );
106
107 do_action( 'tutor_after_adding_order_activity', $params );
108 }
109
110 /**
111 * Store order activity for marking an order as paid.
112 *
113 * This method stores the order activity to log that an order has been marked as paid.
114 * It retrieves the current user's display name and includes it in the activity message
115 * if the user exists. The activity message and the current date and time are encoded
116 * as JSON and stored as order metadata.
117 *
118 * @param int $order_id The ID of the order being marked as paid.
119 *
120 * @return int The insert ID of the newly added order metadata entry. Returns 0 on failure.
121 */
122 public function store_order_activity_for_marked_as_paid( $order_id ) {
123 $user_name = '';
124 $current_user = wp_get_current_user();
125
126 if ( $current_user->exists() ) {
127 $user_name = $current_user->display_name;
128 }
129
130 $message = empty( $user_name ) ? __( 'Order marked as paid', 'tutor' ) : __( 'Order marked as paid by ' . $user_name, 'tutor' );
131
132 $payload = new \stdClass();
133 $payload->order_id = $order_id;
134 $payload->meta_key = $this->model::META_KEY_HISTORY;
135 $payload->meta_value = wp_json_encode(
136 array(
137 'date' => current_time( 'mysql' ),
138 'message' => $message,
139 )
140 );
141
142 return $this->model->add_order_meta( $payload );
143 }
144
145 /**
146 * Validate input data based on predefined rules.
147 *
148 * This protected method validates the provided data array against a set of
149 * predefined validation rules. The rules specify that 'order_id' is required
150 * and must be numeric. The method will skip validation rules for fields that
151 * are not present in the data array.
152 *
153 * @since 3.0.0
154 *
155 * @param array $data The data array to validate.
156 *
157 * @return object The validation result. It returns validation object.
158 */
159 protected static function validate( array $data ) {
160
161 $validation_rules = array(
162 'order_id' => 'required|numeric',
163 'meta_key' => 'required',
164 'meta_value' => 'required',
165 );
166
167 // Skip validation rules for not available fields in data.
168 foreach ( $validation_rules as $key => $value ) {
169 if ( ! array_key_exists( $key, $data ) ) {
170 unset( $validation_rules[ $key ] );
171 }
172 }
173
174 return ValidationHelper::validate( $validation_rules, $data );
175 }
176 }
177