| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Admin; |
| 4 |
|
| 5 |
use SpringDevs\Subscription\Illuminate\Helper; |
| 6 |
|
| 7 |
// HPOS: This file is compatible with WooCommerce High-Performance Order Storage (HPOS). |
| 8 |
// All WooCommerce order data is accessed via WooCommerce CRUD methods (wc_get_order, etc.). |
| 9 |
// All direct post meta access is for subscription data only, not WooCommerce order data. |
| 10 |
// If you add new order data access, use WooCommerce CRUD for HPOS compatibility. |
| 11 |
|
| 12 |
/** |
| 13 |
* Order class |
| 14 |
* |
| 15 |
* @package SpringDevs\Subscription\Admin |
| 16 |
*/ |
| 17 |
class Order { |
| 18 |
|
| 19 |
/** |
| 20 |
* Initialize the class |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); |
| 24 |
add_action( 'woocommerce_admin_order_data_after_payment_info', array( $this, 'add_subscription_label' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Add subscriotion label after order title. |
| 29 |
* |
| 30 |
* @param \WC_Order $order Order object. |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function add_subscription_label( $order ) { |
| 35 |
$histories = Helper::get_subscriptions_from_order( $order->get_id() ); // HPOS: Safe, uses custom table for subscription relations. |
| 36 |
if ( count( $histories ) > 0 ) : |
| 37 |
?> |
| 38 |
<div class="subscrpt-order-label"><?php echo esc_html_e( 'Subscription order', 'subscription' ); ?></div> |
| 39 |
<?php |
| 40 |
endif; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Related Subscriptions meta box on Orders. |
| 45 |
*/ |
| 46 |
public function add_meta_boxes() { |
| 47 |
$screen = wps_subscription_is_wc_order_hpos_enabled() |
| 48 |
? wc_get_page_screen_id( 'shop-order' ) |
| 49 |
: 'shop_order'; |
| 50 |
$order_id = wps_subscription_is_wc_order_hpos_enabled() && isset( $_GET['id'] ) ? ( sanitize_text_field( wp_unslash( $_GET['id'] ) ) ) : get_the_ID(); |
| 51 |
$histories = Helper::get_subscriptions_from_order( $order_id ); |
| 52 |
if ( is_array( $histories ) ) { |
| 53 |
$order = wc_get_order( $order_id ); // HPOS: Safe, uses WooCommerce CRUD. |
| 54 |
add_meta_box( |
| 55 |
'subscrpt_order_related', |
| 56 |
__( 'Related Subscriptions', 'subscription' ), |
| 57 |
array( $this, 'subscrpt_order_related' ), |
| 58 |
$screen, |
| 59 |
'normal', |
| 60 |
'default', |
| 61 |
array( |
| 62 |
'histories' => $histories, |
| 63 |
'order' => $order, |
| 64 |
) |
| 65 |
); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Display content related subscriptions. |
| 71 |
* |
| 72 |
* @param mixed $order_post Current Order. |
| 73 |
* @param array $info Meta box Info. |
| 74 |
*/ |
| 75 |
public function subscrpt_order_related( $order_post, $info ) { |
| 76 |
$histories = $info['args']['histories']; |
| 77 |
$order = $info['args']['order']; |
| 78 |
|
| 79 |
include 'views/related-subscriptions.php'; |
| 80 |
} |
| 81 |
} |
| 82 |
|