| 1 |
<?php |
| 2 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 3 |
|
| 4 |
if ( !class_exists( 'ewdotpAdminOrders' ) ) { |
| 5 |
/** |
| 6 |
* Class to handle the admin orders page for Order Tracking |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
class ewdotpAdminOrders { |
| 11 |
|
| 12 |
/** |
| 13 |
* The orders table |
| 14 |
* |
| 15 |
* This is only instantiated on the orders admin page at the moment when |
| 16 |
* it is generated. |
| 17 |
* |
| 18 |
* @see self::show_admin_orders_page() |
| 19 |
* @see WP_List_table.BookingsTable.class.php |
| 20 |
* @since 3.0.0 |
| 21 |
*/ |
| 22 |
public $orders_table; |
| 23 |
|
| 24 |
public function __construct() { |
| 25 |
|
| 26 |
// Add the admin menu |
| 27 |
add_action( 'admin_menu', array( $this, 'add_menu_page' ) ); |
| 28 |
|
| 29 |
// Hide the 'Add New' item from the side menu |
| 30 |
add_action( 'admin_head', array( $this, 'hide_add_new_menu_item' ) ); |
| 31 |
|
| 32 |
// Saves the orders per page screen option |
| 33 |
add_filter( 'set-screen-option', array( $this, 'save_screen_options' ), 10, 3 ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Add the top-level admin menu page |
| 38 |
* @since 3.0.0 |
| 39 |
*/ |
| 40 |
public function add_menu_page() { |
| 41 |
global $ewd_otp_controller; |
| 42 |
|
| 43 |
$orders_page = add_menu_page( |
| 44 |
_x( 'Orders', 'Title of admin page that lists orders', 'order-tracking' ), |
| 45 |
_x( 'Tracking', 'Title of orders admin menu item', 'order-tracking' ), |
| 46 |
$ewd_otp_controller->settings->get_setting( 'access-role' ), |
| 47 |
'ewd-otp-orders', |
| 48 |
array( $this, 'show_admin_orders_page' ), |
| 49 |
'dashicons-location', |
| 50 |
'50.8' |
| 51 |
); |
| 52 |
|
| 53 |
add_action( 'load-' . $orders_page, array( $this, 'add_screen_options' ) ); |
| 54 |
|
| 55 |
add_submenu_page( |
| 56 |
'ewd-otp-orders', |
| 57 |
_x( 'Add/Edit Order', 'Title of admin page that lets you add or edit an order', 'order-tracking' ), |
| 58 |
_x( 'Add New', 'Title of the add/edit order admin menu item', 'order-tracking' ), |
| 59 |
'read', |
| 60 |
'ewd-otp-add-edit-order', |
| 61 |
array( $this, 'add_edit_order' ) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Hide the 'Add New' admin page from the WordPress sidebar menu |
| 67 |
* @since 3.0.0 |
| 68 |
*/ |
| 69 |
public function hide_add_new_menu_item() { |
| 70 |
|
| 71 |
remove_submenu_page( 'ewd-otp-orders', 'ewd-otp-add-edit-order' ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Display the admin orders page |
| 76 |
* @since 3.0.0 |
| 77 |
*/ |
| 78 |
public function show_admin_orders_page() { |
| 79 |
|
| 80 |
require_once( EWD_OTP_PLUGIN_DIR . '/includes/WP_List_Table.OrdersTable.class.php' ); |
| 81 |
$this->orders_table = new ewdotpOrdersTable(); |
| 82 |
$this->orders_table->prepare_items(); |
| 83 |
?> |
| 84 |
|
| 85 |
<div class="wrap"> |
| 86 |
<h1> |
| 87 |
<?php _e( 'Orders', 'order-tracking' ); ?> |
| 88 |
<a href="admin.php?page=ewd-otp-add-edit-order" class="add-new-h2 page-title-action add-order"><?php _e( 'Add New', 'order-tracking' ); ?></a> |
| 89 |
</h1> |
| 90 |
|
| 91 |
<?php do_action( 'ewd_otp_orders_table_top' ); ?> |
| 92 |
<form id="ewd-otp-orders-table" method="POST" action=""> |
| 93 |
<input type="hidden" name="page" value="ewd-otp-orders"> |
| 94 |
|
| 95 |
<div class="ewd-otp-primary-controls clearfix"> |
| 96 |
<div class="ewd-otp-views"> |
| 97 |
<?php $this->orders_table->views(); ?> |
| 98 |
</div> |
| 99 |
<?php $this->orders_table->advanced_filters(); ?> |
| 100 |
</div> |
| 101 |
|
| 102 |
<?php $this->orders_table->display(); ?> |
| 103 |
</form> |
| 104 |
<?php do_action( 'ewd_otp_orders_table_bottom' ); ?> |
| 105 |
</div> |
| 106 |
|
| 107 |
<?php |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Adds screen options to the "Orders" screen |
| 112 |
* @since 3.0.10 |
| 113 |
*/ |
| 114 |
public function add_screen_options() { |
| 115 |
|
| 116 |
$args = array( |
| 117 |
'label' => __( 'Orders per page', 'order-tracking' ), |
| 118 |
'default' => 30, |
| 119 |
'option' => 'ewd_otp_orders_per_page' |
| 120 |
); |
| 121 |
|
| 122 |
add_screen_option( 'per_page', $args ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Save the screen options for the "Orders" screen |
| 127 |
* @since 3.0.10 |
| 128 |
*/ |
| 129 |
public function save_screen_options( $status, $option, $value ) { |
| 130 |
|
| 131 |
if ( $option == 'ewd_otp_orders_per_page' ) { return $value; } |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Display the order add/edit page |
| 136 |
* @since 3.0.0 |
| 137 |
*/ |
| 138 |
public function add_edit_order() { |
| 139 |
global $ewd_otp_controller; |
| 140 |
|
| 141 |
if ( ! current_user_can( $ewd_otp_controller->settings->get_setting( 'access-role' ) ) ) { |
| 142 |
|
| 143 |
$sales_rep = new ewdotpSalesRep(); |
| 144 |
|
| 145 |
$sales_rep->load_sales_rep_from_wp_id( get_current_user_id() ); |
| 146 |
|
| 147 |
if ( empty( $sales_rep->id ) ) { return; } |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
$order_id = ! empty( $_POST['ewd_otp_order_id'] ) ? intval( $_POST['ewd_otp_order_id'] ) : |
| 152 |
( ! empty( $_GET['order_id'] ) ? intval( $_GET['order_id'] ) : 0 ); |
| 153 |
|
| 154 |
$order = new ewdotpOrder(); |
| 155 |
|
| 156 |
if ( $order_id ) { |
| 157 |
|
| 158 |
$order->load_order_from_id( $order_id ); |
| 159 |
|
| 160 |
$order->load_order_status_history(); |
| 161 |
} |
| 162 |
|
| 163 |
// Delete an entry for status history |
| 164 |
if ( array_key_exists( 'action', $_GET ) and 'delete_status' == $_GET['action'] ) { |
| 165 |
|
| 166 |
$ewd_otp_controller->order_manager->delete_order_status( $_GET['status_id'] ); |
| 167 |
|
| 168 |
$order->load_order_status_history(); |
| 169 |
} |
| 170 |
|
| 171 |
if ( isset( $_POST['ewd_otp_admin_order_submit'] ) ) { |
| 172 |
|
| 173 |
$order->process_admin_order_submission(); |
| 174 |
} |
| 175 |
|
| 176 |
ewd_otp_load_view_files(); |
| 177 |
|
| 178 |
$args = array( |
| 179 |
'order' => $order |
| 180 |
); |
| 181 |
|
| 182 |
if ( ! empty( $sales_rep ) ) { |
| 183 |
|
| 184 |
$args['sales_rep'] = $sales_rep; |
| 185 |
} |
| 186 |
|
| 187 |
$admin_order_view = new ewdotpAdminOrderFormView( $args ); |
| 188 |
|
| 189 |
echo $admin_order_view->render(); |
| 190 |
} |
| 191 |
} |
| 192 |
} // endif; |
| 193 |
|