*/ private static array $request_vars = array(); /** * Hooks the class into WordPress and WooCommerce. */ public static function init(): void { add_action( 'init', array( __CLASS__, 'register_assets_and_block' ) ); add_filter( 'query_vars', array( __CLASS__, 'add_query_vars' ) ); add_action( 'parse_request', array( __CLASS__, 'take_request_vars' ) ); Quotanto_Settings::init(); Quotanto_Statuses::init(); Quotanto_Lifecycle::init(); Quotanto_Emails::init(); Quotanto_Account::init(); Quotanto_Abilities::init(); Quotanto_Request_Form::init(); Quotanto_Captcha::init(); Quotanto_Quote::init(); Quotanto_Catalog::init(); } /** * Lets WordPress read the plugin's own values from the URL. * * @param string[] $vars Public query variables. * @return string[] */ public static function add_query_vars( array $vars ): array { return array_merge( $vars, self::REQUEST_VARS ); } /** * Keeps the plugin's values and takes them out of the page query, so the page itself resolves as it would without them. * * @param WP $wp Current request. */ public static function take_request_vars( WP $wp ): void { foreach ( self::REQUEST_VARS as $name ) { if ( isset( $wp->query_vars[ $name ] ) && is_scalar( $wp->query_vars[ $name ] ) ) { self::$request_vars[ $name ] = sanitize_text_field( (string) $wp->query_vars[ $name ] ); } unset( $wp->query_vars[ $name ] ); } } /** * One of the plugin's values from the URL, or an empty string. * * @param string $name Query variable. */ public static function request_var( string $name ): string { return self::$request_vars[ $name ] ?? ''; } /** * Registers the stylesheet and the Request a quote block. */ public static function register_assets_and_block(): void { wp_register_style( 'quotanto', QUOTANTO_URL . 'assets/quotanto.css', array(), QUOTANTO_VERSION ); register_block_type_from_metadata( QUOTANTO_DIR . 'blocks/request-button', array( 'render_callback' => array( __CLASS__, 'render_block' ), ) ); } /** * Render callback for the "Request a quote" block. * * @param array $attributes Block attributes. * @param string $content Inner content. * @param WP_Block $block Block instance. */ public static function render_block( array $attributes, string $content, WP_Block $block ): string { $post_id = isset( $block->context['postId'] ) ? (int) $block->context['postId'] : (int) get_the_ID(); $product = $post_id ? wc_get_product( $post_id ) : false; return $product ? Quotanto_Request_Form::render( $product ) : ''; } }