array( $this, 'render_block' ) ) ); wp_set_script_translations( 'thridify-view-in-3d-button-editor-script', 'thridify' ); } /** * Block render. * * @param array $attributes Block attributes. * @param string $content Inner content (unused). * @param \WP_Block|null $block Block instance. * @return string */ public function render_block( $attributes, $content = '', $block = null ) { $post_id = $block instanceof \WP_Block && ! empty( $block->context['postId'] ) ? (int) $block->context['postId'] : get_the_ID(); $wrapper = function_exists( 'get_block_wrapper_attributes' ) ? get_block_wrapper_attributes() : ''; return $this->render( (int) $post_id, (array) $attributes, $wrapper ); } /** * Shortcode: [thridify_3d_button label="See it in your room" style="filled" full_width="yes"]. * * @param array|string $atts Shortcode attributes. * @return string */ public function shortcode( $atts ) { $atts = shortcode_atts( array( 'label' => '', 'style' => 'outline', 'full_width' => 'no', 'icon' => 'yes', 'product_id' => '', ), $atts, 'thridify_3d_button' ); $post_id = '' !== $atts['product_id'] ? absint( $atts['product_id'] ) : get_the_ID(); return $this->render( (int) $post_id, array( 'label' => $atts['label'], 'variant' => $atts['style'], 'fullWidth' => 'yes' === $atts['full_width'], 'showIcon' => 'no' !== $atts['icon'], ), '' ); } /** * Shared markup. * * @param int $post_id Product ID. * @param array $attributes Button options. * @param string $wrapper Block wrapper attributes (already escaped by core). * @return string */ private function render( $post_id, array $attributes, $wrapper ) { if ( ! Storefront::active() || ! function_exists( 'is_product' ) || ! is_product() || get_queried_object_id() !== $post_id ) { return ''; // Only on the product's own page. } $product = Storefront::current_product(); if ( ! $product ) { return ''; } Storefront::enqueue_product( $product, true ); wp_enqueue_style( 'thridify-view-in-3d-button-style' ); $variant = in_array( $attributes['variant'] ?? '', array( 'filled', 'outline', 'link' ), true ) ? $attributes['variant'] : 'outline'; $label = isset( $attributes['label'] ) && '' !== trim( (string) $attributes['label'] ) ? (string) $attributes['label'] : Settings::button_text(); $classes = 'thridify-3d-button is-' . $variant . ( ! empty( $attributes['fullWidth'] ) ? ' is-full-width' : '' ); $icon = empty( $attributes['showIcon'] ) ? '' : ''; return sprintf( '
', $wrapper, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() returns escaped attributes. esc_attr( $classes ), $icon, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static SVG markup. esc_html( $label ) ); } }