Cart
10 months ago
PaymentGateways
9 months ago
AdminMenu.php
9 months ago
BillingController.php
1 year ago
CartController.php
1 year ago
CheckoutController.php
11 months ago
CouponController.php
11 months ago
Ecommerce.php
1 year ago
EmailController.php
11 months ago
HooksHandler.php
9 months ago
OptionKeys.php
1 year ago
OrderActivitiesController.php
1 year ago
OrderController.php
9 months ago
PaymentHandler.php
9 months ago
Settings.php
9 months ago
Tax.php
9 months ago
currency.php
1 year ago
OrderActivitiesController.php
178 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 | /* translators: %s: username */ |
| 131 | $message = empty( $user_name ) ? __( 'Order marked as paid', 'tutor' ) : sprintf( __( 'Order marked as paid by %s', 'tutor' ), $user_name ); |
| 132 | |
| 133 | $payload = new \stdClass(); |
| 134 | $payload->order_id = $order_id; |
| 135 | $payload->meta_key = $this->model::META_KEY_HISTORY; |
| 136 | $payload->meta_value = wp_json_encode( |
| 137 | array( |
| 138 | 'date' => current_time( 'mysql' ), |
| 139 | 'message' => $message, |
| 140 | ) |
| 141 | ); |
| 142 | |
| 143 | return $this->model->add_order_meta( $payload ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Validate input data based on predefined rules. |
| 148 | * |
| 149 | * This protected method validates the provided data array against a set of |
| 150 | * predefined validation rules. The rules specify that 'order_id' is required |
| 151 | * and must be numeric. The method will skip validation rules for fields that |
| 152 | * are not present in the data array. |
| 153 | * |
| 154 | * @since 3.0.0 |
| 155 | * |
| 156 | * @param array $data The data array to validate. |
| 157 | * |
| 158 | * @return object The validation result. It returns validation object. |
| 159 | */ |
| 160 | protected static function validate( array $data ) { |
| 161 | |
| 162 | $validation_rules = array( |
| 163 | 'order_id' => 'required|numeric', |
| 164 | 'meta_key' => 'required', |
| 165 | 'meta_value' => 'required', |
| 166 | ); |
| 167 | |
| 168 | // Skip validation rules for not available fields in data. |
| 169 | foreach ( $validation_rules as $key => $value ) { |
| 170 | if ( ! array_key_exists( $key, $data ) ) { |
| 171 | unset( $validation_rules[ $key ] ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return ValidationHelper::validate( $validation_rules, $data ); |
| 176 | } |
| 177 | } |
| 178 |