*/ private static $sections = array(); /** * Whether hooks are added. * * @var bool */ private static $hooked = false; /** * Add a section. * * @since 1.0.0 * @param string $id Section ID. * @param string $label Section heading. * @param callable $render Callback( WC_Order $order ): prints escaped markup. * @param int $priority Lower comes first. * @return void */ public static function add_section( $id, $label, callable $render, $priority = 10 ) { self::$sections[ $id ] = array( 'label' => $label, 'render' => $render, 'priority' => (int) $priority, ); if ( ! self::$hooked ) { add_action( 'add_meta_boxes', array( __CLASS__, 'register' ), 45 ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue' ) ); self::$hooked = true; } } /** * Registered sections, sorted. * * @since 1.0.0 * @return array */ public static function sections() { $sections = self::$sections; uasort( $sections, static fn( $a, $b ) => $a['priority'] <=> $b['priority'] ); return $sections; } /** * Remove all sections (tests). * * @since 1.0.0 * @return void */ public static function reset() { self::$sections = array(); self::$hooked = false; } /** * Register the box (side column, under Order actions). * * @since 1.0.0 * @return void */ public static function register() { if ( ! self::$sections || ! current_user_can( 'edit_shop_orders' ) ) { return; } add_meta_box( self::ID, __( 'Customer pages', 'admin-powerups-for-woocommerce' ), array( __CLASS__, 'render' ), Screen::order_screen_id(), 'side', 'high' ); } /** * Assets on the order screen. * * @since 1.0.0 * @return void */ public static function enqueue() { if ( ! Screen::is_order_edit() ) { return; } wp_enqueue_style( 'apwc-modules', Assets::url( 'assets/css/modules.css' ), array(), Assets::ver( 'assets/css/modules.css' ) ); wp_enqueue_script( 'apwc-customer-pages', Assets::url( 'assets/js/customer-pages.js' ), array( 'wp-api-fetch', 'wp-i18n' ), Assets::ver( 'assets/js/customer-pages.js' ), true ); wp_set_script_translations( 'apwc-customer-pages', 'admin-powerups-for-woocommerce' ); } /** * Render the box. * * @since 1.0.0 * @param WC_Order|\WP_Post $post_or_order Order or post. * @return void */ public static function render( $post_or_order ) { $order = $post_or_order instanceof WC_Order ? $post_or_order : wc_get_order( $post_or_order->ID ); if ( ! $order instanceof WC_Order ) { return; } echo '
'; foreach ( self::sections() as $id => $section ) { echo '
'; echo '

' . esc_html( $section['label'] ) . '

'; call_user_func( $section['render'], $order ); echo '
'; } /** * Print extra markup inside the Customer pages box. * * @since 1.0.0 * @param WC_Order $order Order. */ do_action( 'apwc_customer_pages_box', $order ); echo '

'; echo '
'; } }