Order Archive. * * @package Orderloft */ defined( 'ABSPATH' ) || exit; /** * Overview, archived orders, settings, the order-screen box and redirects for archived order links. */ class Orderloft_Admin { const PAGE = 'orderloft'; /** * Page hook suffix. * * @var string */ private static $hook = ''; /** * Hooks. */ public static function init() { add_action( 'admin_menu', array( __CLASS__, 'menu' ), 60 ); add_action( 'admin_init', array( __CLASS__, 'register_settings' ) ); add_action( 'admin_init', array( __CLASS__, 'redirect_archived_order_links' ) ); add_action( 'admin_post_orderloft_run', array( __CLASS__, 'handle_run' ) ); add_action( 'admin_post_orderloft_stop', array( __CLASS__, 'handle_stop' ) ); add_action( 'admin_post_orderloft_restore', array( __CLASS__, 'handle_restore' ) ); add_action( 'admin_post_orderloft_archive_one', array( __CLASS__, 'handle_archive_one' ) ); add_action( 'admin_post_orderloft_export', array( __CLASS__, 'handle_export' ) ); add_action( 'admin_post_orderloft_reset_skips', array( __CLASS__, 'handle_reset_skips' ) ); add_action( 'add_meta_boxes', array( __CLASS__, 'add_order_box' ), 30 ); add_action( 'admin_notices', array( __CLASS__, 'orders_screen_notice' ) ); add_filter( 'plugin_action_links_' . plugin_basename( ORDERLOFT_FILE ), array( __CLASS__, 'plugin_links' ) ); add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 2 ); } /** * Page of the Orderloft Pro add-on on the author's site. */ const PRO_URL = 'https://bealinawaz.com/wordpress-plugins/pro/orderloft/'; /** * Whether the Orderloft Pro add-on is active. * * @return bool */ public static function pro_active() { return defined( 'ORDERLOFT_PRO_VERSION' ); } /** * Admin URL of the plugin page. * * @param array $args Query args. * @return string */ public static function url( $args = array() ) { return add_query_arg( array_merge( array( 'page' => self::PAGE ), $args ), admin_url( 'admin.php' ) ); } /** * Settings link on the Plugins screen. * * @param array $links Links. * @return array */ public static function plugin_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'orderloft' ) . '' ); return $links; } /** * "Orderloft Pro" link in the plugin's row on the Plugins screen, only while Pro isn't active. * * @param array $meta Row meta links. * @param string $file Plugin file. * @return array */ public static function plugin_row_meta( $meta, $file ) { if ( plugin_basename( ORDERLOFT_FILE ) === $file && ! self::pro_active() ) { $meta[] = '' . esc_html__( 'Orderloft Pro', 'orderloft' ) . ''; } return $meta; } /** * Submenu under WooCommerce. */ public static function menu() { self::$hook = (string) add_submenu_page( 'woocommerce', __( 'Order Archive', 'orderloft' ), __( 'Order Archive', 'orderloft' ), 'manage_woocommerce', self::PAGE, array( __CLASS__, 'render_page' ) ); add_action( 'load-' . self::$hook, array( __CLASS__, 'handle_bulk' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) ); } /** * Stylesheet on the plugin page. * * @param string $hook Current page hook. */ public static function assets( $hook ) { if ( $hook === self::$hook ) { wp_enqueue_style( 'orderloft-admin', plugins_url( 'assets/admin.css', ORDERLOFT_FILE ), array(), ORDERLOFT_VERSION ); } } /** * Settings API registration. */ public static function register_settings() { register_setting( 'orderloft', Orderloft_Settings::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( 'Orderloft_Settings', 'sanitize' ), 'default' => Orderloft_Settings::defaults(), ) ); } /** * Store a notice for the current user, shown on the next page load. * * @param string $type success, error, warning or info. * @param string $message Message. */ private static function flash( $type, $message ) { set_transient( 'orderloft_notice_' . get_current_user_id(), array( $type, $message ), 120 ); } /** * Print and clear the stored notice. */ private static function print_flash() { $key = 'orderloft_notice_' . get_current_user_id(); $notice = get_transient( $key ); if ( is_array( $notice ) ) { delete_transient( $key ); printf( '

%2$s

', esc_attr( $notice[0] ), esc_html( $notice[1] ) ); } } /** * Stop unless the user may manage WooCommerce and the nonce is valid. * * @param string $action Nonce action. */ private static function guard( $action ) { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'orderloft' ), 403 ); } check_admin_referer( $action ); } /** * Start a manual run. */ public static function handle_run() { self::guard( 'orderloft_run' ); $check = Orderloft_Archiver::preflight(); if ( is_wp_error( $check ) ) { self::flash( 'error', $check->get_error_message() ); } elseif ( Orderloft_Scheduler::is_running() ) { self::flash( 'info', __( 'A run is already in progress.', 'orderloft' ) ); } else { Orderloft_Scheduler::start_run( 'manual' ); self::flash( 'success', __( 'Archiving started. It runs in the background in small batches, so you can leave this page.', 'orderloft' ) ); } wp_safe_redirect( self::url() ); exit; } /** * Stop a run. */ public static function handle_stop() { self::guard( 'orderloft_stop' ); Orderloft_Scheduler::stop_run(); self::flash( 'info', __( 'Archiving stopped after the current batch.', 'orderloft' ) ); wp_safe_redirect( self::url() ); exit; } /** * Clear the skip marks so skipped orders are checked again. */ public static function handle_reset_skips() { self::guard( 'orderloft_reset_skips' ); $ids = wc_get_orders( array( 'type' => 'shop_order', 'limit' => -1, 'return' => 'ids', 'meta_key' => Orderloft_Archiver::SKIP_META, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Admin-triggered one-off. ) ); foreach ( (array) $ids as $id ) { $order = wc_get_order( $id ); if ( $order ) { $order->delete_meta_data( Orderloft_Archiver::SKIP_META ); $order->save_meta_data(); } } /* translators: %d: number of orders. */ self::flash( 'success', sprintf( _n( '%d skipped order will be checked again on the next run.', '%d skipped orders will be checked again on the next run.', count( (array) $ids ), 'orderloft' ), count( (array) $ids ) ) ); wp_safe_redirect( self::url() ); exit; } /** * Restore one order. */ public static function handle_restore() { $order_id = isset( $_GET['order_id'] ) ? absint( $_GET['order_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Checked in guard(). self::guard( 'orderloft_restore_' . $order_id ); $result = Orderloft_Archiver::restore_order( $order_id ); if ( is_wp_error( $result ) ) { self::flash( 'orderloft_sync' === $result->get_error_code() ? 'warning' : 'error', $result->get_error_message() ); wp_safe_redirect( self::url( array( 'tab' => 'archived' ) ) ); exit; } /* translators: %d: order ID. */ self::flash( 'success', sprintf( __( 'Order #%d was restored to your live orders.', 'orderloft' ), $order_id ) ); $order = wc_get_order( $order_id ); wp_safe_redirect( $order ? $order->get_edit_order_url() : self::url( array( 'tab' => 'archived' ) ) ); exit; } /** * Archive one order from its edit screen. */ public static function handle_archive_one() { $order_id = isset( $_GET['order_id'] ) ? absint( $_GET['order_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Checked in guard(). self::guard( 'orderloft_archive_' . $order_id ); $result = Orderloft_Archiver::archive_order( $order_id ); if ( is_wp_error( $result ) ) { self::flash( 'error', $result->get_error_message() ); $order = wc_get_order( $order_id ); wp_safe_redirect( $order ? $order->get_edit_order_url() : self::url() ); exit; } /* translators: %d: order ID. */ self::flash( 'success', sprintf( __( 'Order #%d was archived.', 'orderloft' ), $order_id ) ); wp_safe_redirect( self::url( array( 'tab' => 'archived', 'view' => $order_id, ) ) ); exit; } /** * Bulk restore from the archived orders list. */ public static function handle_bulk() { $action = ''; // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Nonce is checked below before acting. foreach ( array( 'action', 'action2' ) as $key ) { if ( isset( $_REQUEST[ $key ] ) && 'orderloft_restore' === $_REQUEST[ $key ] ) { $action = 'orderloft_restore'; } } if ( 'orderloft_restore' !== $action || empty( $_REQUEST['order_ids'] ) ) { return; } // phpcs:enable WordPress.Security.NonceVerification.Recommended self::guard( 'bulk-archived_orders' ); $ids = array_map( 'absint', (array) wp_unslash( $_REQUEST['order_ids'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce checked in guard() above. $done = 0; $errors = array(); foreach ( $ids as $id ) { $result = Orderloft_Archiver::restore_order( $id ); if ( is_wp_error( $result ) ) { $errors[] = $result->get_error_message(); } else { ++$done; } } /* translators: %d: number of orders. */ $message = sprintf( _n( '%d order was restored.', '%d orders were restored.', $done, 'orderloft' ), $done ); if ( $errors ) { $message .= ' ' . implode( ' ', array_unique( $errors ) ); } self::flash( $errors ? 'warning' : 'success', $message ); wp_safe_redirect( self::url( array( 'tab' => 'archived' ) ) ); exit; } /** * CSV export of the archived orders (all, or the current search). */ public static function handle_export() { self::guard( 'orderloft_export' ); list( $rows ) = Orderloft_List_Table::query( Orderloft_List_Table::search_term(), 'date_created_gmt', 'ASC', 1000000, 0, true ); nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=orderloft-archived-orders-' . gmdate( 'Y-m-d' ) . '.csv' ); $out = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- Streaming the CSV download. fputcsv( $out, array( 'order_id', 'order_number', 'date_created_gmt', 'status', 'total', 'currency', 'customer_id', 'billing_name', 'billing_email', 'items', 'archived_gmt' ) ); foreach ( (array) $rows as $row ) { $summary = json_decode( (string) $row['summary'], true ); $items = array(); foreach ( is_array( $summary ) && isset( $summary['items'] ) ? $summary['items'] : array() as $item ) { if ( 'line_item' === ( $item['type'] ?? '' ) ) { $items[] = $item['name'] . ' x ' . $item['qty']; } } fputcsv( $out, array_map( array( __CLASS__, 'csv_safe' ), array( $row['order_id'], $row['order_number'], $row['date_created_gmt'], $row['status'], $row['total'], $row['currency'], $row['customer_id'], $row['billing_name'], $row['billing_email'], implode( '; ', $items ), $row['archived_gmt'] ) ) ); } fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- Closing the output stream. exit; } /** * Prevent spreadsheet formula injection in CSV cells. * * @param mixed $value Cell value. * @return string */ public static function csv_safe( $value ) { $value = (string) $value; return ( '' !== $value && in_array( $value[0], array( '=', '+', '-', '@' ), true ) && ! is_numeric( $value ) ) ? "'" . $value : $value; } /** * Archived order edit links (from emails, notes or bookmarks) open the archived copy instead of an error. */ public static function redirect_archived_order_links() { global $pagenow; // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only redirect. $id = 0; if ( 'admin.php' === $pagenow && isset( $_GET['page'], $_GET['action'], $_GET['id'] ) && 'wc-orders' === $_GET['page'] && 'edit' === $_GET['action'] ) { $id = absint( $_GET['id'] ); } elseif ( 'post.php' === $pagenow && isset( $_GET['post'], $_GET['action'] ) && 'edit' === $_GET['action'] ) { $id = absint( $_GET['post'] ); } // phpcs:enable WordPress.Security.NonceVerification.Recommended if ( ! $id || ! current_user_can( 'manage_woocommerce' ) || wc_get_order( $id ) || ! Orderloft_Archiver::get_archived( $id ) ) { return; } wp_safe_redirect( self::url( array( 'tab' => 'archived', 'view' => $id, ) ) ); exit; } /** * A line on the Orders screen pointing to the archive. */ public static function orders_screen_notice() { $screen = get_current_screen(); if ( ! $screen || ! current_user_can( 'manage_woocommerce' ) ) { return; } $ids = array( 'edit-shop_order', 'shop_order', 'woocommerce_page_wc-orders' ); if ( ! in_array( $screen->id, $ids, true ) ) { return; } // Messages from a restore that redirected to the order screen. self::print_flash(); // The archive line belongs on the order list only, not on a single order's edit screen. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check of the current screen. if ( 'shop_order' === $screen->id || isset( $_GET['action'] ) ) { return; } $count = Orderloft_Archiver::archived_count(); if ( ! $count ) { return; } printf( '

%s %s

', /* translators: %s: number of archived orders. */ esc_html( sprintf( _n( '%s older order is in the archive.', '%s older orders are in the archive.', $count, 'orderloft' ), number_format_i18n( $count ) ) ), esc_url( self::url( array( 'tab' => 'archived' ) ) ), esc_html__( 'Search archived orders', 'orderloft' ) ); } /** * "Archive this order" box on the order edit screen. */ public static function add_order_box() { $screens = array( 'shop_order' ); if ( function_exists( 'wc_get_page_screen_id' ) ) { $screens[] = wc_get_page_screen_id( 'shop-order' ); } foreach ( array_unique( $screens ) as $screen ) { add_meta_box( 'orderloft-archive', __( 'Order archive', 'orderloft' ), array( __CLASS__, 'render_order_box' ), $screen, 'side', 'low' ); } } /** * Box contents. * * @param WP_Post|WC_Order $post_or_order Post or order. */ public static function render_order_box( $post_or_order ) { $order = $post_or_order instanceof WC_Order ? $post_or_order : wc_get_order( $post_or_order->ID ); if ( ! $order || ! $order->get_id() || 'auto-draft' === $order->get_status() ) { echo '

' . esc_html__( 'Save the order before archiving it.', 'orderloft' ) . '

'; return; } $url = wp_nonce_url( admin_url( 'admin-post.php?action=orderloft_archive_one&order_id=' . $order->get_id() ), 'orderloft_archive_' . $order->get_id() ); echo '

' . esc_html__( 'Move this order and its refunds out of your live orders. You can restore it at any time.', 'orderloft' ) . '

'; echo '

' . esc_html__( 'Archive this order', 'orderloft' ) . '

'; } /** * Page router. */ public static function render_page() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Navigation only. $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'overview'; $view = isset( $_GET['view'] ) ? absint( $_GET['view'] ) : 0; // phpcs:enable WordPress.Security.NonceVerification.Recommended $tabs = array( 'overview' => __( 'Overview', 'orderloft' ), 'archived' => __( 'Archived orders', 'orderloft' ), 'settings' => __( 'Settings', 'orderloft' ), ); if ( ! isset( $tabs[ $tab ] ) ) { $tab = 'overview'; } echo '
'; echo '

' . esc_html__( 'Order Archive', 'orderloft' ) . '

'; self::print_flash(); echo ''; if ( 'archived' === $tab && $view ) { self::render_view( $view ); } elseif ( 'archived' === $tab ) { self::render_archived(); } elseif ( 'settings' === $tab ) { self::render_settings(); } else { self::render_overview(); } echo '
'; } /** * Human label of the order storage. * * @return string */ private static function storage_label() { $labels = array( 'hpos' => __( 'High-performance order storage (HPOS)', 'orderloft' ), 'hpos_sync' => __( 'HPOS with compatibility mode (orders are also copied to the posts table)', 'orderloft' ), 'posts' => __( 'WordPress posts storage (legacy)', 'orderloft' ), ); return $labels[ Orderloft_Archiver::storage_mode() ]; } /** * Overview tab. */ private static function render_overview() { $settings = Orderloft_Settings::all(); $check = Orderloft_Archiver::preflight(); $candidates = Orderloft_Archiver::count_candidates( $settings ); $archived = Orderloft_Archiver::archived_count(); $live = wc_get_orders( array( 'type' => 'shop_order', 'status' => array_keys( wc_get_order_statuses() ), 'limit' => 1, 'paginate' => true, 'return' => 'ids', ) ); $skipped = wc_get_orders( array( 'type' => 'shop_order', 'limit' => 1, 'paginate' => true, 'return' => 'ids', 'meta_key' => Orderloft_Archiver::SKIP_META, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Admin overview count. ) ); $run = Orderloft_Scheduler::last_run(); $statuses = array_map( 'wc_get_order_status_name', (array) $settings['statuses'] ); if ( is_wp_error( $check ) ) { echo '

' . esc_html( $check->get_error_message() ) . '

'; } ?>
total ) ); ?>

(int) $settings['daily_limit'] ) : ?>

total > 0 ) : ?>

total, 'orderloft' ), number_format_i18n( (int) $skipped->total ) ) ); ?>

prepare_items(); $export = wp_nonce_url( add_query_arg( array( 'action' => 'orderloft_export', 's' => Orderloft_List_Table::search_term(), ), admin_url( 'admin-post.php' ) ), 'orderloft_export' ); ?>
search_box( __( 'Search archived orders', 'orderloft' ), 'orderloft-search' ); ?>

display(); ?>
' . esc_html__( 'This order is not in the archive.', 'orderloft' ) . '

'; return; } $s = json_decode( (string) $row->summary, true ); $s = is_array( $s ) ? $s : array(); $currency = array( 'currency' => $row->currency ); $date = function ( $ts ) { return $ts ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), (int) $ts ) : '—'; }; $restore = wp_nonce_url( admin_url( 'admin-post.php?action=orderloft_restore&order_id=' . absint( $row->order_id ) ), 'orderloft_restore_' . absint( $row->order_id ) ); ?>

←

order_number ) ); ?> status ) ); ?>

archived_gmt . ' UTC' ) ) ) ); ?>

:
:
:
:
:
: customer_id ) ) { $user = get_userdata( (int) $row->customer_id ); echo $user ? '' . esc_html( $user->display_name ) . '' : esc_html__( 'Deleted user', 'orderloft' ); } else { esc_html_e( 'Guest', 'orderloft' ); } ?>


billing_email ); ?>

:

(' . esc_html( $item['sku'] ) . ')'; } if ( 'line_item' !== $item['type'] ) { echo ' ' . esc_html( ucfirst( $item['type'] ) ) . ''; } ?>
total, $currency ) ); ?>
-