verifier = $verifier; } /** * Hooks. */ public function register() { add_action( 'add_meta_boxes', array( $this, 'meta_box' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); add_action( 'wp_ajax_afrihex_recheck', array( $this, 'ajax_recheck' ) ); // Orders list column: legacy posts screen and HPOS screen. add_filter( 'manage_edit-shop_order_columns', array( $this, 'column' ), 20 ); add_action( 'manage_shop_order_posts_custom_column', array( $this, 'column_value_legacy' ), 10, 2 ); add_filter( 'manage_woocommerce_page_wc-orders_columns', array( $this, 'column' ), 20 ); add_action( 'manage_woocommerce_page_wc-orders_custom_column', array( $this, 'column_value' ), 10, 2 ); } /** * The order edit screen ID for the active storage. * * @return string */ private static function screen() { if ( class_exists( OrderUtil::class ) && OrderUtil::custom_orders_table_usage_is_enabled() ) { return wc_get_page_screen_id( 'shop-order' ); } return 'shop_order'; } /** * Adds the box beside the order. */ public function meta_box() { add_meta_box( 'afrihex-location', __( 'Delivery location (AfriHex)', 'afrihex-address-for-woocommerce' ), array( $this, 'render_box' ), self::screen(), 'side', 'high' ); } /** * Script for the re-check button and the settings page. */ public function enqueue() { $screen = get_current_screen(); $ours = $screen && ( self::screen() === $screen->id || 'woocommerce_page_' . AfriHex_Settings::PAGE === $screen->id ); if ( ! $ours ) { return; } wp_enqueue_script( 'afrihex-address-admin', AFRIHEX_ADDRESS_URL . 'assets/js/admin.js', array(), AFRIHEX_ADDRESS_VERSION, true ); wp_localize_script( 'afrihex-address-admin', 'afrihexAdmin', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'i18n' => array( 'checking' => __( 'Checking…', 'afrihex-address-for-woocommerce' ), 'failed' => __( 'Request failed.', 'afrihex-address-for-woocommerce' ), ), ) ); } /** * Renders the box. * * @param WP_Post|WC_Order $post_or_order Post (legacy) or order (HPOS). */ public function render_box( $post_or_order ) { $order = $post_or_order instanceof WC_Order ? $post_or_order : wc_get_order( $post_or_order->ID ); if ( ! $order ) { return; } $rec = AfriHex_Order_Display::record( $order ); if ( ! $rec ) { echo '

' . esc_html__( 'No delivery location was recorded for this order.', 'afrihex-address-for-woocommerce' ) . '

'; $this->recheck_form( $order, null ); return; } $badge = self::badge( $rec ); echo '

' . esc_html( $badge[0] ) . '

'; echo ''; foreach ( AfriHex_Order_Display::rows( $rec, true ) as $r ) { echo ''; } echo '
' . esc_html( $r[0] ) . '' . esc_html( $r[1] ) . '
'; $map = AfriHex_Location::map_url( $rec ); if ( $map ) { echo '

' . esc_html__( 'Open in Google Maps', 'afrihex-address-for-woocommerce' ) . '

'; } if ( ! empty( $rec['issues'] ) ) { echo ''; } if ( AfriHex_Location::STATUS_UNVERIFIED === $rec['status'] && '' !== $rec['error'] ) { /* translators: %s: error message */ echo '

' . esc_html( sprintf( __( 'Last error: %s', 'afrihex-address-for-woocommerce' ), $rec['error'] ) ) . '

'; } /* translators: %s: date and time */ echo '

' . esc_html( sprintf( __( 'Checked %s', 'afrihex-address-for-woocommerce' ), wc_format_datetime( new WC_DateTime( $rec['checked_at'] ) ) . ' ' . wc_format_datetime( new WC_DateTime( $rec['checked_at'] ), wc_time_format() ) ) ) . '

'; $this->recheck_form( $order, $rec ); } /** * The re-check controls. Not a
: the box sits inside the order form. * * @param WC_Order $order Order. * @param array|null $rec Record. */ private function recheck_form( $order, $rec ) { ?>

__( 'Not allowed.', 'afrihex-address-for-woocommerce' ) ), 403 ); } $order = wc_get_order( $order_id ); if ( ! $order ) { wp_send_json_error( array( 'message' => __( 'Order not found.', 'afrihex-address-for-woocommerce' ) ), 404 ); } $code = isset( $_POST['gps_code'] ) ? sanitize_text_field( wp_unslash( $_POST['gps_code'] ) ) : ''; if ( '' !== $code && '' === AfriHex_Location::gps_code( $code ) ) { wp_send_json_error( array( 'message' => __( 'That is not a GhanaPostGPS code (e.g. GA-183-8164).', 'afrihex-address-for-woocommerce' ) ) ); } $rec = $this->verifier->recheck( $order, $code ); $note = AfriHex_Verifier::note( $rec ); /* translators: 1: user display name, 2: outcome note */ $order->add_order_note( sprintf( __( '%1$s re-checked the delivery location. %2$s', 'afrihex-address-for-woocommerce' ), wp_get_current_user()->display_name, $note ) ); if ( AfriHex_Location::STATUS_UNVERIFIED === $rec['status'] ) { wp_send_json_error( array( 'message' => $note ) ); } wp_send_json_success( array( 'message' => AfriHex_Order_Display::verdict_label( $rec ) ) ); } /** * Adds the column after the shipping address. * * @param array $columns Columns. * @return array */ public function column( $columns ) { $out = array(); foreach ( $columns as $key => $label ) { $out[ $key ] = $label; if ( 'shipping_address' === $key ) { $out['afrihex_location'] = __( 'Location check', 'afrihex-address-for-woocommerce' ); } } if ( ! isset( $out['afrihex_location'] ) ) { $out['afrihex_location'] = __( 'Location check', 'afrihex-address-for-woocommerce' ); } return $out; } /** * Column value (legacy posts screen). * * @param string $column Column. * @param int $post_id Order ID. */ public function column_value_legacy( $column, $post_id ) { $this->column_value( $column, wc_get_order( $post_id ) ); } /** * Column value. * * @param string $column Column. * @param WC_Order $order Order. */ public function column_value( $column, $order ) { if ( 'afrihex_location' !== $column || ! $order ) { return; } $rec = AfriHex_Order_Display::record( $order ); if ( ! $rec ) { echo '–'; return; } $badge = self::badge( $rec ); echo '' . esc_html( $badge[0] ) . ''; if ( '' !== $rec['gps_code'] ) { echo '
' . esc_html( $rec['gps_code'] ) . ''; } } }