BaseModel.php
9 months ago
BillingModel.php
1 year ago
CartItemModel.php
9 months ago
CartModel.php
5 months ago
CouponModel.php
4 months ago
CourseModel.php
3 months ago
LessonModel.php
9 months ago
OrderActivitiesModel.php
1 year ago
OrderItemMetaModel.php
9 months ago
OrderItemModel.php
9 months ago
OrderMetaModel.php
1 year ago
OrderModel.php
2 months ago
QuizModel.php
3 months ago
UserModel.php
1 year ago
WithdrawModel.php
3 weeks ago
OrderActivitiesModel.php
211 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Order Model |
| 4 | * |
| 5 | * @package Tutor\Models |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 3.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Tutor\Models; |
| 12 | |
| 13 | use Tutor\Helpers\DateTimeHelper; |
| 14 | use Tutor\Helpers\QueryHelper; |
| 15 | |
| 16 | /** |
| 17 | * OrderActivitiesModel Class |
| 18 | * |
| 19 | * @since 3.0.0 |
| 20 | */ |
| 21 | class OrderActivitiesModel { |
| 22 | /** |
| 23 | * Order Meta keys for history, refund & comment |
| 24 | * |
| 25 | * @since 3.0.0 |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | const META_KEY_HISTORY = 'history'; |
| 30 | const META_KEY_REFUND = 'refund'; |
| 31 | const META_KEY_PARTIALLY_REFUND = 'partially-refund'; |
| 32 | const META_KEY_COMMENT = 'comment'; |
| 33 | const META_KEY_CANCEL_REASON = 'cancel-reason'; |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Order meta table name |
| 38 | * |
| 39 | * @since 3.0.0 |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | private $table_name = 'tutor_ordermeta'; |
| 44 | |
| 45 | /** |
| 46 | * Resolve props & dependencies |
| 47 | * |
| 48 | * @since 3.0.0 |
| 49 | */ |
| 50 | public function __construct() { |
| 51 | global $wpdb; |
| 52 | $this->table_name = $wpdb->prefix . $this->table_name; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get table name with wp prefix |
| 57 | * |
| 58 | * @since 3.0.0 |
| 59 | * |
| 60 | * @return string |
| 61 | */ |
| 62 | public function get_table_name() { |
| 63 | return $this->table_name; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Get all Meta keys |
| 69 | * |
| 70 | * @since 3.0.0 |
| 71 | * |
| 72 | * @return array |
| 73 | */ |
| 74 | public static function get_meta_keys() { |
| 75 | return array( |
| 76 | self::META_KEY_HISTORY => __( 'History', 'tutor' ), |
| 77 | self::META_KEY_REFUND => __( 'Refund', 'tutor' ), |
| 78 | self::META_KEY_COMMENT => __( 'Comment', 'tutor' ), |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Retrieve order activities by order ID. |
| 84 | * |
| 85 | * This function fetches all order activities from the 'tutor_ordermeta' table |
| 86 | * based on the given order ID and the 'history' meta key. It uses a helper |
| 87 | * function from the QueryHelper class to perform the database query. |
| 88 | * |
| 89 | * If no order activities are found, the function returns an empty array. |
| 90 | * Otherwise, it decodes the JSON-encoded meta values and returns them as an array. |
| 91 | * |
| 92 | * @global wpdb $wpdb WordPress database abstraction object. |
| 93 | * |
| 94 | * @param int $order_id The ID of the order to retrieve activities for. |
| 95 | * |
| 96 | * @since 3.0.0 |
| 97 | * |
| 98 | * @return array An array of order activities, each decoded from its JSON representation. |
| 99 | */ |
| 100 | public function get_order_activities( $order_id ) { |
| 101 | global $wpdb; |
| 102 | |
| 103 | // Retrieve order activities for the given order ID from the 'tutor_ordermeta' table. |
| 104 | $meta_keys = array( |
| 105 | self::META_KEY_COMMENT, |
| 106 | self::META_KEY_HISTORY, |
| 107 | self::META_KEY_REFUND, |
| 108 | self::META_KEY_PARTIALLY_REFUND, |
| 109 | ); |
| 110 | |
| 111 | $order_activities = QueryHelper::get_all( |
| 112 | $this->table_name, |
| 113 | array( |
| 114 | 'order_id' => $order_id, |
| 115 | 'meta_key' => $meta_keys, |
| 116 | ), |
| 117 | 'id' |
| 118 | ); |
| 119 | |
| 120 | if ( empty( $order_activities ) ) { |
| 121 | return array(); |
| 122 | } |
| 123 | |
| 124 | $response = array(); |
| 125 | |
| 126 | try { |
| 127 | foreach ( $order_activities as &$activity ) { |
| 128 | $values = new \stdClass(); |
| 129 | if ( tutor_is_json( $activity->meta_value ) ) { |
| 130 | $values = json_decode( $activity->meta_value ); |
| 131 | } else { |
| 132 | $values->message = $activity->meta_value; |
| 133 | } |
| 134 | $values->id = (int) $activity->id; |
| 135 | $values->created_at_readable = DateTimeHelper::get_gmt_to_user_timezone_date( $activity->created_at_gmt ); |
| 136 | $values->type = $activity->meta_key; |
| 137 | $response[] = $values; |
| 138 | } |
| 139 | } catch ( \Throwable $th ) { |
| 140 | // Log error message with file info. |
| 141 | error_log( $th->getMessage() . ' in ' . $th->getFile() . ' at line ' . $th->getLine() ); |
| 142 | } |
| 143 | |
| 144 | unset( $activity ); |
| 145 | |
| 146 | return $response; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Add order metadata. |
| 151 | * |
| 152 | * Inserts metadata into the 'tutor_ordermeta' table based on the provided data object. |
| 153 | * |
| 154 | * @since 3.0.0 |
| 155 | * |
| 156 | * @param object $data An object containing order metadata: |
| 157 | * - $data->order_id (int) The ID of the order. |
| 158 | * - $data->meta_key (string) The meta key for the metadata. |
| 159 | * - $data->meta_value (string) The meta value(JSON) for the metadata. |
| 160 | * |
| 161 | * @return int The ID of the inserted row on success, or 0 on failure. |
| 162 | */ |
| 163 | public function add_order_meta( object $data ) { |
| 164 | $current_time = current_time( 'mysql', true ); |
| 165 | $current_user_id = get_current_user_id(); |
| 166 | |
| 167 | return QueryHelper::insert( |
| 168 | $this->table_name, |
| 169 | array( |
| 170 | 'order_id' => $data->order_id, |
| 171 | 'meta_key' => $data->meta_key, |
| 172 | 'meta_value' => $data->meta_value, |
| 173 | 'created_at_gmt' => $current_time, |
| 174 | 'created_by' => $current_user_id, |
| 175 | 'updated_at_gmt' => $current_time, |
| 176 | 'updated_by' => $current_user_id, |
| 177 | ) |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Update order metadata. |
| 183 | * |
| 184 | * Updates metadata in the 'tutor_ordermeta' table based on the provided data object. |
| 185 | * |
| 186 | * @since 3.0.0 |
| 187 | * |
| 188 | * @param object $data An object containing order metadata: |
| 189 | * - $data->order_id (int) The ID of the order. |
| 190 | * - $data->meta_key (string) The meta key for the metadata. |
| 191 | * - $data->meta_value (string) The meta value(JSON) for the metadata. |
| 192 | * |
| 193 | * @return bool True on success, false on failure. |
| 194 | */ |
| 195 | public function update_order_meta( object $data ) { |
| 196 | return QueryHelper::update( |
| 197 | $this->table_name, |
| 198 | array( |
| 199 | 'meta_key' => $data->meta_key, |
| 200 | 'meta_value' => $data->meta_value, |
| 201 | 'updated_at_gmt' => current_time( 'mysql', true ), |
| 202 | 'updated_by' => get_current_user_id(), |
| 203 | ), |
| 204 | array( |
| 205 | 'id' => $data->id, |
| 206 | 'order_id' => $data->order_id, |
| 207 | ) |
| 208 | ); |
| 209 | } |
| 210 | } |
| 211 |