\Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema::IDENTIFIER, 'namespace' => self::STORE_API_NAMESPACE, 'data_callback' => array( __CLASS__, 'cart_data' ), 'schema_callback' => array( __CLASS__, 'cart_schema' ), 'schema_type' => ARRAY_A, ) ); woocommerce_store_api_register_update_callback( array( 'namespace' => self::STORE_API_NAMESPACE, 'callback' => array( __CLASS__, 'update_from_store_api' ), ) ); } /** * Data the block widget renders from. * * @return array */ public static function cart_data() { $enabled = OrderCTRL_Bridge_Protection_Settings::is_enabled(); $fee = $enabled ? OrderCTRL_Bridge_Protection::current_fee() : 0.0; $title = trim( (string) OrderCTRL_Bridge_Protection_Settings::get( 'widget_title' ) ); return array( 'enabled' => $enabled && $fee > 0, 'chosen' => $enabled && OrderCTRL_Bridge_Protection::is_chosen(), 'fee' => $enabled ? html_entity_decode( wp_strip_all_tags( wc_price( $fee ) ), ENT_QUOTES, 'UTF-8' ) : '', 'title' => '' !== $title ? $title : OrderCTRL_Bridge_Protection_Settings::fee_name(), 'label' => __( 'Add package protection', 'orderctrl-bridge' ), 'description' => (string) OrderCTRL_Bridge_Protection_Settings::get( 'widget_description' ), 'tooltip' => (string) OrderCTRL_Bridge_Protection_Settings::get( 'tooltip_text' ), 'show_on_cart' => OrderCTRL_Bridge_Protection_Settings::is_yes( 'display_on_cart' ), 'design' => OrderCTRL_Bridge_Protection_Settings::design_type(), 'background_color' => OrderCTRL_Bridge_Protection_Settings::color( 'design_background_color' ), 'border_color' => OrderCTRL_Bridge_Protection_Settings::color( 'design_border_color' ), 'link_color' => OrderCTRL_Bridge_Protection_Settings::color( 'design_link_color' ), 'logo_url' => OrderCTRL_Bridge_Protection_Settings::is_yes( 'display_logo' ) ? ORDERCTRL_BRIDGE_URL . 'public/images/orderctrl-mark.svg' : '', ); } /** * Store API schema for cart_data(). * * @return array */ public static function cart_schema() { $string = array( 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ); $bool = array( 'type' => 'boolean', 'context' => array( 'view', 'edit' ), 'readonly' => true, ); return array( 'enabled' => $bool, 'chosen' => $bool, 'fee' => $string, 'title' => $string, 'label' => $string, 'description' => $string, 'tooltip' => $string, 'show_on_cart' => $bool, 'design' => $string, 'background_color' => $string, 'border_color' => $string, 'link_color' => $string, 'logo_url' => $string, ); } /** * Called by POST /wc/store/v1/cart/extensions with namespace orderctrl-bridge. * * @param array $data { chosen: bool }. */ public static function update_from_store_api( $data ) { if ( ! OrderCTRL_Bridge_Protection_Settings::is_enabled() ) { return; } OrderCTRL_Bridge_Protection::set_choice( is_array( $data ) && ! empty( $data['chosen'] ) && rest_sanitize_boolean( $data['chosen'] ) ); } /** * Registers the frontend script with the Cart and Checkout blocks. * * @param object $registry IntegrationRegistry. */ public function register_integration( $registry ) { if ( ! interface_exists( '\Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface' ) ) { return; } require_once ORDERCTRL_BRIDGE_DIR . 'includes/class-orderctrl-bridge-blocks-integration.php'; if ( ! $registry->is_registered( OrderCTRL_Bridge_Blocks_Integration::NAME ) ) { $registry->register( new OrderCTRL_Bridge_Blocks_Integration() ); } } }