PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/learnpress/inc/order/class-lp-order.php

https://gitlab.com/gregtyka/lfmawordpress
PHP | 423 lines | 340 code | 24 blank | 59 comment | 12 complexity | 1df30480827e4af83a3b863ea2d85e1b MD5 | raw file
  1. <?php
  2. /**
  3. * Class LP_Order
  4. *
  5. * @author ThimPress
  6. * @package LearnPress/Classes
  7. * @version 1.0
  8. */
  9. defined( 'ABSPATH' ) || exit();
  10. class LP_Order {
  11. /**
  12. * Store post object
  13. *
  14. * @var null|WP_Post object
  15. */
  16. public $post = null;
  17. /**
  18. * @var null|LP_Order object
  19. */
  20. protected static $_instance = null;
  21. /**
  22. * @param $order
  23. */
  24. function __construct( $order ) {
  25. $this->init( $order );
  26. }
  27. /**
  28. * Init/load the order object. Called from the constructor.
  29. *
  30. * @param int|object|LP_Order $order Order to init
  31. */
  32. protected function init( $order ) {
  33. if ( is_numeric( $order ) ) {
  34. $this->id = absint( $order );
  35. $this->post = get_post( $order );
  36. $this->get_order( $this->id );
  37. } elseif ( $order instanceof LP_Order ) {
  38. $this->id = absint( $order->id );
  39. $this->post = $order->post;
  40. $this->get_order( $this->id );
  41. } elseif ( isset( $order->ID ) ) {
  42. $this->id = absint( $order->ID );
  43. $this->post = $order;
  44. $this->get_order( $this->id );
  45. } else {
  46. $this->id = 0;
  47. $this->post = new WP_Post( new stdClass() );
  48. }
  49. }
  50. /**
  51. * Get order by it's ID
  52. *
  53. * @param $id
  54. *
  55. * @return bool
  56. */
  57. function get_order( $id ) {
  58. if ( !$id ) {
  59. return false;
  60. }
  61. if ( $result = get_post( $id ) ) {
  62. $this->id = $result->ID;
  63. $this->order_date = $result->post_date;
  64. $this->modified_date = $result->post_modified;
  65. $this->customer_message = $result->post_excerpt;
  66. $this->customer_note = $result->post_excerpt;
  67. $this->post_status = $result->post_status;
  68. return true;
  69. }
  70. return false;
  71. }
  72. /**
  73. * Get current status of order
  74. *
  75. * @return mixed
  76. */
  77. public function get_status() {
  78. $this->post->post_status = get_post_status( $this->id );
  79. return apply_filters( 'lp_order_get_status', 'lp-' === substr( $this->post->post_status, 0, 3 ) ? substr( $this->post->post_status, 3 ) : $this->post->post_status, $this );
  80. }
  81. /**
  82. * Checks to see if current order has status as passed
  83. *
  84. * @param $status
  85. *
  86. * @return mixed
  87. */
  88. public function has_status( $status ) {
  89. return apply_filters( 'lp_order_has_status', ( is_array( $status ) && in_array( $this->get_status(), $status ) ) || $this->get_status() === $status ? true : false, $this, $status );
  90. }
  91. /**
  92. * Updates order to new status if needed
  93. *
  94. * @param mixed $new_status
  95. *
  96. * @return bool
  97. * @throws Exception
  98. */
  99. function update_status( $new_status = 'pending' ) {
  100. global $post;
  101. $new_status = 'lp-' === substr( $new_status, 0, 3 ) ? substr( $new_status, 3 ) : $new_status;
  102. $old_status = $this->get_status();
  103. if ( $new_status !== $old_status && in_array( $new_status, array_keys( learn_press_get_order_statuses( false ) ) ) ) {
  104. // Update the order
  105. global $wpdb;
  106. $updated = $wpdb->update( $wpdb->posts, array( 'post_status' => 'lp-' . $new_status ), array( 'ID' => $this->id ), array( '%s' ) );
  107. if ( $updated === false ) {
  108. throw new Exception( __( 'Error! Update order failed', 'learnpress' ) );
  109. return false;
  110. }
  111. $this->post_status = 'lp-' . $new_status;
  112. $this->post->post_status = 'lp-' . $new_status;
  113. // update post cache
  114. wp_cache_set( $this->id, $this->post, 'posts' );
  115. // Status was changed
  116. do_action( 'learn_press_order_status_' . $new_status, $this->id );
  117. do_action( 'learn_press_order_status_' . $old_status . '_to_' . $new_status, $this->id );
  118. do_action( 'learn_press_order_status_changed', $this->id, $old_status, $new_status );
  119. switch ( $new_status ) {
  120. case 'completed' :
  121. break;
  122. case 'processing' :
  123. break;
  124. }
  125. // backward compatible
  126. do_action( 'learn_press_update_order_status', $new_status, $this->id );
  127. }
  128. }
  129. function set_payment_method( $payment_method ) {
  130. if ( is_object( $payment_method ) ) {
  131. update_post_meta( $this->id, '_payment_method', $payment_method->id );
  132. update_post_meta( $this->id, '_payment_method_title', $payment_method->get_title() );
  133. }
  134. $this->payment_method = $payment_method;
  135. }
  136. /**
  137. * Format order number id
  138. *
  139. * @return string
  140. */
  141. function get_order_number() {
  142. return apply_filters( 'learn_press_get_order_number', '#' . sprintf( "%'.010d", $this->id ), $this );
  143. }
  144. function get_order_status() {
  145. $statuses = learn_press_get_order_statuses();
  146. $status = '';
  147. if ( !empty( $statuses[$this->post_status] ) ) {
  148. $status = $statuses[$this->post_status];
  149. }
  150. return apply_filters( 'learn_press_get_order_status', $status, $this );
  151. }
  152. /**
  153. * Mark order as complete
  154. *
  155. * @param string - transaction ID provided payment gateway
  156. *
  157. * @return bool
  158. */
  159. function payment_complete( $transaction_id = '' ) {
  160. do_action( 'learn_press_pre_payment_complete', $this->id );
  161. LP()->session->order_awaiting_payment = null;
  162. $valid_order_statuses = apply_filters( 'learn_press_valid_order_statuses_for_payment_complete', array( 'pending', 'processing', 'on-hold' ), $this );
  163. if ( $this->id && $this->has_status( $valid_order_statuses ) ) {
  164. $this->update_status( 'completed' );
  165. if ( !empty( $transaction_id ) ) {
  166. add_post_meta( $this->id, '_transaction_id', $transaction_id, true );
  167. }
  168. do_action( 'learn_press_payment_complete', $this->id );
  169. } else {
  170. do_action( 'learn_press_payment_complete_order_status_' . $this->get_status(), $this->id );
  171. }
  172. return true;
  173. }
  174. /**
  175. * Get checkout order success url
  176. *
  177. * @return mixed
  178. */
  179. public function get_checkout_order_received_url() {
  180. $received_url = learn_press_get_endpoint_url( 'lp-order-received', $this->id, learn_press_get_page_link( 'checkout' ) );
  181. $received_url = add_query_arg( 'key', $this->order_key, $received_url );
  182. return apply_filters( 'learn_press_get_checkout_order_received_url', $received_url, $this );
  183. }
  184. /*********************************/
  185. public function get_items() {
  186. global $wpdb;
  187. $query = $wpdb->prepare( "
  188. SELECT order_item_id, order_item_name
  189. FROM {$wpdb->learnpress_order_items}
  190. WHERE order_id = %d ", $this->id );
  191. $_items = $wpdb->get_results( $query );
  192. $items = array();
  193. // Loop items
  194. if ( $_items ) foreach ( $_items as $item ) {
  195. $items[$item->order_item_id]['name'] = $item->order_item_name;
  196. $items[$item->order_item_id]['id'] = $item->order_item_id;
  197. $this->get_item_meta( $items[$item->order_item_id] );
  198. //$items[$item->order_item_id]['item_meta_array'] = $this->get_item_meta_array( $item->order_item_id );
  199. //$items[$item->order_item_id] = $this->expand_item_meta( $items[$item->order_item_id] );
  200. }
  201. return apply_filters( 'learn_press_order_get_items', $items, $this );
  202. }
  203. function get_item_meta( &$item ) {
  204. if ( $metas = get_metadata( 'learnpress_order_item', $item['id'] ) ) {
  205. foreach ( $metas as $k => $v ) {
  206. $item[preg_replace( '!^_!', '', $k )] = maybe_unserialize( $v[0] );
  207. }
  208. };
  209. }
  210. /**
  211. * Remove all items from an order
  212. */
  213. function remove_order_items() {
  214. global $wpdb;
  215. $wpdb->query(
  216. $wpdb->prepare( "
  217. DELETE FROM itemmeta
  218. USING {$wpdb->learnpress_order_itemmeta} itemmeta
  219. INNER JOIN {$wpdb->learnpress_order_items} items
  220. WHERE itemmeta.learnpress_order_item_id = items.order_item_id
  221. AND items.order_id = %d",
  222. $this->id
  223. )
  224. );
  225. $wpdb->query(
  226. $wpdb->prepare( "
  227. DELETE FROM {$wpdb->learnpress_order_items}
  228. WHERE order_id = %d",
  229. $this->id
  230. )
  231. );
  232. $wpdb->query( $wpdb->prepare( "ALTER TABLE {$wpdb->learnpress_order_itemmeta} AUTO_INCREMENT = %d", 1 ) );
  233. $wpdb->query( $wpdb->prepare( "ALTER TABLE {$wpdb->learnpress_order_items} AUTO_INCREMENT = %d", 1 ) );
  234. }
  235. /**
  236. * Add a new item to order
  237. *
  238. * @param $item
  239. * @param int $quantity
  240. * @param array $meta
  241. *
  242. * @return bool
  243. */
  244. function add_item( $item, $quantity = 1, $meta = array() ) {
  245. $item_id = learn_press_add_order_item( $this->id, $item );
  246. if ( !$item_id ) {
  247. return false;
  248. }
  249. learn_press_add_order_item_meta( $item_id, '_course_id', $item['item_id'] );
  250. learn_press_add_order_item_meta( $item_id, '_quantity', $item['quantity'] );
  251. learn_press_add_order_item_meta( $item_id, '_subtotal', $item['subtotal'] );
  252. learn_press_add_order_item_meta( $item_id, '_total', $item['total'] );
  253. return $item_id;
  254. }
  255. function get_user( $field = '' ) {
  256. $user = learn_press_get_user( $this->user_id );
  257. if ( $field ) {
  258. return $user->{$field};
  259. }
  260. return $user;
  261. }
  262. public function __get( $key ) {
  263. $value = null;
  264. if ( !isset( $this->{$key} ) ) {
  265. $value = get_post_meta( $this->id, '_' . $key, true );
  266. }
  267. return $value;
  268. }
  269. function get_checkout_payment_url() {
  270. }
  271. function get_formatted_order_subtotal() {
  272. $currency_symbol = learn_press_get_currency_symbol( $this->order_currency );
  273. return learn_press_format_price( $this->order_subtotal, $currency_symbol );
  274. }
  275. function get_formatted_order_total() {
  276. $currency_symbol = learn_press_get_currency_symbol( $this->order_currency );
  277. return learn_press_format_price( $this->order_total, $currency_symbol );
  278. }
  279. function get_payment_method_title() {
  280. if ( $this->order_total == 0 ) {
  281. $title = __( 'Free Payment', 'learnpress' );
  282. } else {
  283. $title = $this->payment_method_title;
  284. }
  285. return apply_filters( 'learn_press_display_payment_method_title', $title, $this->payment_method );
  286. }
  287. function get_view_order_url() {
  288. global $wp_query;
  289. $view_order_url = learn_press_get_endpoint_url( 'view-order', $this->id, learn_press_get_page_link( 'profile' ) );
  290. //
  291. $user = learn_press_get_current_user();
  292. $view_order_endpoint = LP()->settings->get( 'profile_endpoints.profile-order-details' );
  293. if ( !$view_order_endpoint ) {
  294. $view_order_endpoint = 'order-details';
  295. }
  296. if ( get_option( 'permalink_structure' ) ) {
  297. $view_order_url = learn_press_get_page_link( 'profile' ) . $user->user_login . '/' . $view_order_endpoint . '/' . $this->id;
  298. } else {
  299. $args = array(
  300. 'user' => $user->user_login
  301. );
  302. $args['view'] = $view_order_endpoint;
  303. if ( $view_order_endpoint ) {
  304. $args['id'] = $this->id;
  305. }
  306. $view_order_url = add_query_arg(
  307. $args,
  308. learn_press_get_page_link( 'profile' )
  309. );
  310. }
  311. return apply_filters( 'learn_press_view_order_url', $view_order_url, $this );
  312. }
  313. public function add_note( $note ) {
  314. if ( is_user_logged_in() ) {
  315. $user = get_user_by( 'id', get_current_user_id() );
  316. $comment_author = $user->display_name;
  317. $comment_author_email = $user->user_email;
  318. $comment_post_ID = $this->id;
  319. $comment_author_url = '';
  320. $comment_content = $note;
  321. $comment_agent = 'LearnPress';
  322. $comment_type = 'lp_order_note';
  323. $comment_parent = 0;
  324. $comment_approved = 1;
  325. $commentdata = apply_filters(
  326. 'learn_press_new_order_note_data',
  327. compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_agent', 'comment_type', 'comment_parent', 'comment_approved' ),
  328. $this->id
  329. );
  330. $comment_id = wp_insert_comment( $commentdata );
  331. return $comment_id;
  332. }
  333. return false;
  334. }
  335. function get_user_name() {
  336. return apply_filters( 'learn_press_order_user_name', sprintf( _x( '%1$s', 'full name', 'learnpress' ), $this->get_user( 'user_login' ) ) );
  337. }
  338. /**
  339. * Get an instance of LP_Order by post ID or WP_Post object
  340. *
  341. * @param $order
  342. * @param $force
  343. *
  344. * @return LP_Order
  345. */
  346. static function instance( $order, $force = true ) {
  347. $post = $order;
  348. if ( $order instanceof WP_Post ) {
  349. $id = $order->ID;
  350. } elseif ( is_object( $order ) && isset( $order->ID ) ) {
  351. $id = $order->ID;
  352. } else {
  353. $id = $order;
  354. }
  355. if ( empty( self::$_instance[$id] ) || $force ) {
  356. self::$_instance[$id] = new self( $post );
  357. }
  358. return self::$_instance[$id];
  359. }
  360. }