*/ class Pizzapilot_Admin { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the admin area. * * Loaded on every admin screen because it styles the "Upgrade to Pro" * menu item and plugins-list link, which appear everywhere in wp-admin, * as well as the settings screens and the order meta box. Admin only; * nothing here loads on the front end. * * @since 1.0.0 */ public function enqueue_styles() { wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/pizzapilot-admin.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the admin area. * * Only loaded on the WooCommerce order edit screens, where it hides the * duplicate PizzaPilot fields from the address display. Covers both the * legacy post-based screen and the HPOS screen. * * @since 1.0.0 * @param string $hook_suffix The current admin page hook suffix. */ public function enqueue_scripts( $hook_suffix ) { // Legacy order storage edits orders on post.php / post-new.php; HPOS // uses its own WooCommerce page. Bail early on everything else. if ( ! in_array( $hook_suffix, array( 'post.php', 'post-new.php', 'woocommerce_page_wc-orders' ), true ) ) { return; } // post.php serves every post type, so confirm it is an order screen. $screen = get_current_screen(); if ( ! $screen || ! in_array( $screen->id, array( 'shop_order', 'woocommerce_page_wc-orders' ), true ) ) { return; } wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/pizzapilot-admin.js', array( 'jquery' ), $this->version, true ); } /** * Display PizzaPilot delivery information in the order admin area. * * Shows the delivery type and time slot in a custom meta box on the order edit page. * * @since 1.0.0 * @param WC_Order $order The order object. WooCommerce passes a * WC_Order on both legacy and HPOS screens; * reading ->ID from it triggers a notice. * @return void */ public function pizzapilot_display_order_meta( $order ) { $order = wc_get_order( $order ); if ( ! $order ) { return; } // Get delivery type from WooCommerce additional checkout fields $delivery_type = $order->get_meta( '_wc_other/pizzapilot/delivery-type', true ); // Get delivery time from WooCommerce additional checkout fields $delivery_time = $order->get_meta( '_wc_other/pizzapilot/delivery-time', true ); // Also check legacy meta keys in case they exist if ( empty( $delivery_type ) ) { $delivery_type = $order->get_meta( '_pizzapilot_delivery_type', true ); } if ( empty( $delivery_time ) ) { $delivery_time = $order->get_meta( '_pizzapilot_delivery_time', true ); } // Don't display anything if no PizzaPilot data if ( empty( $delivery_type ) && empty( $delivery_time ) ) { return; } echo '
'; } /** * Format delivery time timestamp into human-readable format. * * Converts unix timestamp to formatted date and time string. * * @since 1.0.0 * @param int $timestamp Unix timestamp of the delivery slot. * @return string Formatted delivery time string. */ private function format_delivery_time( $timestamp ) { if ( empty( $timestamp ) || ! is_numeric( $timestamp ) ) { return __( 'Not set', 'pizzapilot' ); } // Get WordPress timezone $timezone = wp_timezone(); $datetime = new DateTime( '@' . $timestamp ); $datetime->setTimezone( $timezone ); // Calculate slot end time (30 minutes later) $end_datetime = clone $datetime; $end_datetime->modify( '+30 minutes' ); // Format: "Tuesday 24th Jun from 9:30am to 10:00am" $day_name = $datetime->format( 'l' ); $day_number = $datetime->format( 'jS' ); $month = $datetime->format( 'M' ); $start_time = $datetime->format( 'g:ia' ); $end_time = $end_datetime->format( 'g:ia' ); return sprintf( '%s %s %s from %s to %s', $day_name, $day_number, $month, $start_time, $end_time ); } /** * Hide PizzaPilot fields from default WooCommerce order meta display. * * Prevents the checkout fields from showing in the shipping section * since we display them in our custom meta box instead. * * @since 1.0.0 * @param bool $display Whether to display the field. * @param string $key The field key. * @param object $order The order object. * @return bool False to hide the field, true to show. */ public function pizzapilot_hide_checkout_field_display( $display, $key, $order ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- $order required by WC filter signature. // Hide PizzaPilot fields from the default display $fields_to_hide = array( 'pizzapilot/delivery-type', 'pizzapilot/delivery-time', ); if ( in_array( $key, $fields_to_hide, true ) ) { return false; } return $display; } /** * Add PizzaPilot column to orders list in admin. * * Adds a column showing delivery time slot to the orders table. * * @since 1.0.0 * @param array $columns Existing columns. * @return array Modified columns array. */ public function pizzapilot_add_order_column( $columns ) { $new_columns = array(); foreach ( $columns as $key => $column ) { $new_columns[ $key ] = $column; // Insert after order status column if ( 'order_status' === $key ) { $new_columns['pizzapilot_delivery'] = __( 'Delivery Slot', 'pizzapilot' ); } } return $new_columns; } /** * Populate PizzaPilot column content in orders list (CPT storage). * * Displays delivery type and time in the custom column. * * @since 1.0.0 * @param string $column Column name. * @param int $post_id Order post ID. * @return void */ public function pizzapilot_order_column_content( $column, $post_id ) { if ( 'pizzapilot_delivery' !== $column ) { return; } $order = wc_get_order( $post_id ); $this->render_delivery_column( $order ); } /** * Populate PizzaPilot column content in orders list (HPOS storage). * * Displays delivery type and time in the custom column for * WooCommerce High-Performance Order Storage. * * @since 1.1.0 * @param string $column Column name. * @param WC_Order $order The order object. * @return void */ public function pizzapilot_order_column_content_hpos( $column, $order ) { if ( 'pizzapilot_delivery' !== $column ) { return; } $this->render_delivery_column( $order ); } /** * Render the delivery slot column content for an order. * * Shared rendering logic used by both CPT and HPOS column callbacks. * * @since 1.1.0 * @access private * @param WC_Order|false $order The order object or false. * @return void */ private function render_delivery_column( $order ) { if ( ! $order ) { echo '—'; return; } // Get delivery type $delivery_type = $order->get_meta( '_wc_other/pizzapilot/delivery-type', true ); if ( empty( $delivery_type ) ) { $delivery_type = $order->get_meta( '_pizzapilot_delivery_type', true ); } // Get delivery time $delivery_time = $order->get_meta( '_wc_other/pizzapilot/delivery-time', true ); if ( empty( $delivery_time ) ) { $delivery_time = $order->get_meta( '_pizzapilot_delivery_time', true ); } if ( empty( $delivery_time ) ) { echo '—'; return; } // Convert timestamp to short format $timezone = wp_timezone(); $datetime = new DateTime( '@' . $delivery_time ); $datetime->setTimezone( $timezone ); $end_datetime = clone $datetime; $end_datetime->modify( '+30 minutes' ); // Short format: "2:00pm-2:30pm" $display = sprintf( '%s