settings = $settings; } /** * Instantiate admin and front-end controllers. * * @return void */ public static function init() { $settings = new Aetos_QOC_Settings(); $settings->register_hooks(); $plugin = new self( $settings ); $plugin->register_hooks(); } /** * Declare compatibility with the WooCommerce features used by the plugin. * * @return void */ public static function declare_woocommerce_compatibility() { if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) { foreach ( array( 'custom_order_tables', 'cart_checkout_blocks' ) as $feature_id ) { \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( $feature_id, AETOS_QOC_FILE, true ); } } } /** * Register front-end hooks. * * @return void */ private function register_hooks() { add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_frontend_assets' ) ); add_action( 'woocommerce_after_add_to_cart_form', array( $this, 'render_product_button' ), 15 ); add_action( 'woocommerce_proceed_to_checkout', array( $this, 'render_cart_button' ), 15 ); add_filter( 'render_block', array( $this, 'append_button_to_cart_block' ), 10, 2 ); add_shortcode( 'aetos_quick_order_chat', array( $this, 'render_shortcode' ) ); } /** * Register front-end assets only where output may exist. * * @return void */ public function maybe_enqueue_frontend_assets() { $settings = $this->settings->get_settings(); $builder = new Aetos_QOC_Message_Builder( $settings ); if ( ! $builder->is_ready() ) { return; } $is_product = function_exists( 'is_product' ) && is_product() && $settings['product_enabled']; $is_cart = function_exists( 'is_cart' ) && is_cart() && $settings['cart_enabled']; if ( $is_product || $is_cart || $this->current_post_has_shortcode() ) { $this->enqueue_frontend_assets(); } } /** * Check the current singular post for deliberate shortcode placement. * * @return bool */ private function current_post_has_shortcode() { if ( ! is_singular() ) { return false; } $content = (string) get_post_field( 'post_content', get_queried_object_id() ); return has_shortcode( $content, 'aetos_quick_order_chat' ); } /** * Enqueue the small static front-end files. * * @return void */ private function enqueue_frontend_assets() { $script_dependencies = array( 'wp-data' ); if ( wp_script_is( 'wc-blocks-data-store', 'registered' ) ) { $script_dependencies[] = 'wc-blocks-data-store'; } wp_enqueue_style( self::STYLE_HANDLE, plugins_url( 'assets/css/frontend.css', AETOS_QOC_FILE ), array(), AETOS_QOC_VERSION ); wp_enqueue_script( self::SCRIPT_HANDLE, plugins_url( 'assets/js/product-message.js', AETOS_QOC_FILE ), $script_dependencies, AETOS_QOC_VERSION, true ); } /** * Echo the default product button. * * @return void */ public function render_product_button() { $settings = $this->settings->get_settings(); if ( ! $settings['product_enabled'] || ! function_exists( 'wc_get_product' ) ) { return; } $product = wc_get_product( get_queried_object_id() ); $html = $this->product_button_html( $product, 'aetos-qoc-product-button' ); if ( '' !== $html ) { $this->enqueue_frontend_assets(); echo wp_kses( $html, $this->button_allowed_html() ); } } /** * Echo one default button on the classic cart template. * * @return void */ public function render_cart_button() { if ( $this->cart_button_rendered ) { return; } $html = $this->cart_button_html( 'aetos-qoc-cart-button' ); if ( '' !== $html ) { $this->cart_button_rendered = true; $this->enqueue_frontend_assets(); echo wp_kses( $html, $this->button_allowed_html() ); } } /** * Append the cart button after the modern WooCommerce Cart Block. * * @param string $block_content Rendered block HTML. * @param array $block Parsed block data. * @return string */ public function append_button_to_cart_block( $block_content, $block ) { $block_name = isset( $block['blockName'] ) ? $block['blockName'] : ''; if ( is_admin() || 'woocommerce/cart' !== $block_name || $this->cart_button_rendered ) { return $block_content; } $html = $this->cart_button_html( 'aetos-qoc-cart-block-button' ); if ( '' === $html ) { return $block_content; } $this->cart_button_rendered = true; $this->enqueue_frontend_assets(); return $block_content . '
' . wp_kses( $html, $this->button_allowed_html() ) . '
'; } /** * Render an explicitly placed product or cart button. * * @param array $attributes Shortcode attributes. * @return string */ public function render_shortcode( $attributes ) { $attributes = shortcode_atts( array( 'context' => 'auto', 'product_id' => 0, ), $attributes, 'aetos_quick_order_chat' ); $context = sanitize_key( (string) $attributes['context'] ); $html = $this->shortcode_button_html( $context, absint( $attributes['product_id'] ) ); if ( '' !== $html ) { $this->enqueue_frontend_assets(); } return wp_kses( $html, $this->button_allowed_html() ); } /** * Resolve shortcode context without changing default-button state. * * @param string $context Requested context. * @param int $product_id Optional product ID. * @return string */ private function shortcode_button_html( $context, $product_id ) { $is_product = 'product' === $context || ( 'auto' === $context && function_exists( 'is_product' ) && is_product() ); if ( $is_product ) { if ( ! function_exists( 'wc_get_product' ) ) { return ''; } $product_id = $product_id ? $product_id : get_queried_object_id(); $is_current_product = function_exists( 'is_product' ) && is_product() && get_queried_object_id() === $product_id; $product = wc_get_product( $product_id ); if ( $is_current_product ) { return $this->product_button_html( $product, 'aetos-qoc-shortcode-button' ); } return $this->static_product_button_html( $product, 'aetos-qoc-shortcode-button' ); } if ( 'cart' === $context || 'auto' === $context ) { return $this->cart_button_html( 'aetos-qoc-shortcode-button' ); } return ''; } /** * Build product button HTML and browser message data. * * @param mixed $product WooCommerce product. * @param string $context_class Context-specific CSS class. * @return string */ private function product_button_html( $product, $context_class ) { $parts = $this->get_product_button_parts( $product ); if ( empty( $parts ) ) { return ''; } $data = $this->product_button_data( $parts['builder'], $parts['settings'], $parts['context'] ); return $this->button_html( $parts['url'], $context_class, $data ); } /** * Build static product shortcode HTML away from the product's own form. * * @param mixed $product WooCommerce product. * @param string $context_class Context-specific CSS class. * @return string */ private function static_product_button_html( $product, $context_class ) { $parts = $this->get_product_button_parts( $product ); return empty( $parts ) ? '' : $this->button_html( $parts['url'], $context_class, array() ); } /** * Resolve the shared URL and live-message context for a product button. * * @param mixed $product WooCommerce product. * @return array */ private function get_product_button_parts( $product ) { $settings = $this->settings->get_settings(); $builder = new Aetos_QOC_Message_Builder( $settings ); if ( ! $settings['product_enabled'] || ! $builder->is_ready() || ! $product instanceof WC_Product ) { return array(); } $context = $builder->get_product_context( $product ); $url = $builder->get_chat_url( $builder->get_product_message_from_context( $context, 1 ) ); return '' === $url ? array() : compact( 'url', 'builder', 'settings', 'context' ); } /** * Return fixed browser data for live product variation updates. * * @param Aetos_QOC_Message_Builder $builder Message builder. * @param array $settings Current settings. * @param array $context Product context. * @return array */ private function product_button_data( $builder, $settings, $context ) { return array( 'aetos-qoc-product' => '1', 'aetos-qoc-base-url' => $builder->get_base_url(), 'aetos-qoc-intro' => $settings['intro'], 'aetos-qoc-name-label' => __( 'Product', 'aetos-quick-order-chat' ), 'aetos-qoc-name' => $context['name'], 'aetos-qoc-quantity-label' => __( 'Quantity', 'aetos-quick-order-chat' ), 'aetos-qoc-options-label' => __( 'Options', 'aetos-quick-order-chat' ), 'aetos-qoc-sku-label' => __( 'SKU', 'aetos-quick-order-chat' ), 'aetos-qoc-sku' => $settings['include_sku'] ? $context['sku'] : '', 'aetos-qoc-price-label' => __( 'Price', 'aetos-quick-order-chat' ), 'aetos-qoc-price' => $settings['include_price'] ? $context['price'] : '', 'aetos-qoc-link-label' => __( 'Link', 'aetos-quick-order-chat' ), 'aetos-qoc-link' => $settings['include_link'] ? $context['url'] : '', ); } /** * Build a button for the current cart. * * @param string $context_class Context-specific CSS class. * @return string */ private function cart_button_html( $context_class ) { $settings = $this->settings->get_settings(); $builder = new Aetos_QOC_Message_Builder( $settings ); if ( ! $settings['cart_enabled'] || ! $builder->is_ready() ) { return ''; } $url = $builder->get_chat_url( $builder->get_cart_message() ); return '' !== $url ? $this->button_html( $url, $context_class, $this->cart_button_data( $builder, $settings ) ) : ''; } /** * Return fixed labels used to refresh a Cart Block message on click. * * @param Aetos_QOC_Message_Builder $builder Message builder. * @param array $settings Current settings. * @return array */ private function cart_button_data( $builder, $settings ) { return array( 'aetos-qoc-cart' => '1', 'aetos-qoc-base-url' => $builder->get_base_url(), 'aetos-qoc-intro' => $settings['intro'], 'aetos-qoc-sku-label' => $settings['include_sku'] ? __( 'SKU', 'aetos-quick-order-chat' ) : '', 'aetos-qoc-line-total-label' => $settings['include_price'] ? __( 'Line total', 'aetos-quick-order-chat' ) : '', 'aetos-qoc-cart-total-label' => $settings['include_price'] ? __( 'Cart total', 'aetos-quick-order-chat' ) : '', 'aetos-qoc-cart-link-label' => $settings['include_link'] ? __( 'Cart link', 'aetos-quick-order-chat' ) : '', 'aetos-qoc-cart-link' => $settings['include_link'] ? wc_get_cart_url() : '', ); } /** * Build one escaped anchor and its optional fixed data attributes. * * @param string $url Destination URL. * @param string $context_class Context-specific class. * @param array $data Product message data. * @return string */ private function button_html( $url, $context_class, $data = array() ) { $settings = $this->settings->get_settings(); $data_html = $this->data_attributes_html( $data ); $target = $settings['new_tab'] ? ' target="_blank"' : ''; $class_name = 'aetos-qoc-button ' . sanitize_html_class( $context_class ); return sprintf( '%5$s', esc_attr( $class_name ), esc_attr( $url ), $target, esc_attr( $settings['button_label'] ), esc_html( $settings['button_label'] ), $data_html ); } /** * Escape fixed product data attributes. * * @param array $attributes Product message data. * @return string */ private function data_attributes_html( $attributes ) { $html = ''; foreach ( $attributes as $name => $value ) { $html .= sprintf( ' data-%1$s="%2$s"', esc_attr( $name ), esc_attr( $value ) ); } return $html; } /** * Allowed markup for the fully constructed button. * * @return array> */ private function button_allowed_html() { return array( 'a' => array( 'class' => true, 'href' => true, 'target' => true, 'rel' => true, 'aria-label' => true, 'data-aetos-qoc-product' => true, 'data-aetos-qoc-cart' => true, 'data-aetos-qoc-base-url' => true, 'data-aetos-qoc-intro' => true, 'data-aetos-qoc-name-label' => true, 'data-aetos-qoc-name' => true, 'data-aetos-qoc-quantity-label' => true, 'data-aetos-qoc-options-label' => true, 'data-aetos-qoc-sku-label' => true, 'data-aetos-qoc-sku' => true, 'data-aetos-qoc-price-label' => true, 'data-aetos-qoc-price' => true, 'data-aetos-qoc-link-label' => true, 'data-aetos-qoc-link' => true, 'data-aetos-qoc-line-total-label' => true, 'data-aetos-qoc-cart-total-label' => true, 'data-aetos-qoc-cart-link-label' => true, 'data-aetos-qoc-cart-link' => true, ), 'span' => array( 'class' => true, 'aria-hidden' => true, ), ); } }