Files
old-new-wiaas/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-dashboard.php
2018-11-29 11:13:15 +01:00

173 lines
5.2 KiB
PHP

<?php
class Wiaas_Admin_Dashboard {
public static function init() {
add_action( 'wp_dashboard_setup', array( __CLASS__, 'setup_dashboard' ) );
}
public static function setup_dashboard() {
wp_add_dashboard_widget( 'wiaas_orders', __( 'Order Central', 'wiaas' ), array( __CLASS__, 'orders_widget' ) );
wp_add_dashboard_widget( 'wiaas_next_actions', __( 'Next Actions', 'wiaas' ), array( __CLASS__, 'next_actions_widget' ) );
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );
}
public static function enqueue_scripts() {
$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );
wp_enqueue_style( 'wiaas_admin_dashboard', $plugin_url . '/assets/css/wiaas-admin-dashboard.css' );
wp_enqueue_script( 'wiaas_admin_dashboard', $plugin_url . '/assets/js/wiaas-admin-dashboard.js' );
}
public static function orders_widget() {
$orders = wc_get_orders(array(
'limit' => 5,
'status' => array('open', 'processing')
));
if (empty($orders)) {
?>
<div class="wiaas-dashboard-table-empty">
<p>
<?php esc_html_e( 'No new orders', 'wiaas' ); ?>
</p>
</div>
<?php
return;
}
?>
<table id="wiaas_order_central_table" class="wiaas-dashboard-table wiaas-orders-table">
<thead>
<tr>
<th><?php esc_html_e( 'Order', 'wiaas' ); ?></th>
<th><?php esc_html_e( 'Status', 'wiaas' ); ?></th>
<th><?php esc_html_e( 'Total', 'wiaas' ); ?></th>
</tr>
</thead>
<tbody id="wiaas-orders-tbody">
<?php
foreach ($orders as $order) {
// format date
$order_timestamp = $order->get_date_created() ? $order->get_date_created()->getTimestamp() : '';
$show_date = '&ndash;';
if ( $order_timestamp ) {
if ( $order_timestamp > strtotime( '-1 day', current_time( 'timestamp', true ) ) && $order_timestamp <= current_time( 'timestamp', true ) ) {
$show_date = sprintf(
/* translators: %s: human-readable time difference */
_x( '%s ago', '%s = human-readable time difference', 'wiaas' ),
human_time_diff( $order->get_date_created()->getTimestamp(), current_time( 'timestamp', true ) )
);
} else {
$show_date = $order->get_date_created()->date_i18n( __( 'M j, Y', 'wiaas' ) );
}
}
?>
<tr>
<td>
<strong>
<?php
if (current_user_can('edit_others_shop_orders')) {
$order_edit_url = admin_url( 'post.php?post=' . absint( $order->get_id() ) . '&action=edit' );
} else {
$order_edit_url = admin_url('admin.php?page=wiaas-order-delivery&id=' . absint( $order->get_id() ) );
}
echo '<a href="'. $order_edit_url . '">#' .
esc_html($order->get_order_number()) .
'</a>';
?>
</strong>
<br>
<time
class="wiaas-order-date"
datetime="<?php esc_attr_e( $order->get_date_created()->date( 'c' ), 'wiaas' ) ?>"
title="<?php esc_html_e( $order->get_date_created()->date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ), 'wiaas' ) ?>"
>
<?php esc_html_e($show_date, 'wiaas') ?>
</time>
</td>
<td>
<mark class="wiaas-order-status <?php esc_attr_e( sanitize_html_class( 'wiaas-order-status-' . $order->get_status() ), 'wiaas' ) ?>">
<span>
<?php esc_html_e( wc_get_order_status_name( $order->get_status() ) ) ?>
</span>
</mark>
</td>
<td><?php echo $order->get_formatted_order_total() ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
public static function next_actions_widget() {
$next_actions = Wiaas_Delivery_Process::get_next_actions_for_current_user();
if (empty($next_actions)) {
?>
<div class="wiaas-dashboard-table-empty">
<p>
<?php esc_html_e( 'No pending actions', 'wiaas' ); ?>
</p>
</div>
<?php
return;
}
?>
<table id="wiaas_next_actions_table" class="wiaas-dashboard-table">
<thead>
<tr>
<th><?php esc_html_e( 'Order', 'wiaas' ); ?></th>
<th><?php esc_html_e( 'Action', 'wiaas' ); ?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($next_actions as $action) {
?>
<tr>
<td>
<a href="<?php echo esc_url(admin_url('admin.php?page=wiaas-order-delivery&id=' . absint( $action['order_id'] ) )) ?>">
<strong>#<?php esc_html_e($action['order_number'], 'wiaas')?></strong>
</a>
</td>
<td>
<a href="<?php echo $action['url'] ?>">
<?php esc_html_e($action['action_title'], 'wiaas')?>
</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
}
Wiaas_Admin_Dashboard::init();