PluginProbe
Ultimate Store Kit / trunk
Ultimate Store Kit vtrunk
3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.0.7 3.0.5 3.0.4 3.0.3 3.0.2 trunk 1.5.0 1.5.1 1.5.2 1.6.1 1.6.2 1.6.3 1.6.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 92 releases
ultimate-store-kit / includes / builder / cart-render.php

cart-render.php in Ultimate Store Kit trunk, at includes/builder/cart-render.php

150 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace UltimateStoreKit\Builder;
4
5 use Elementor\Plugin;
6 use UltimateStoreKit\Modules\PageCart\Widgets\Page_Cart;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 class Cart_Render {
13
14 /**
15 * Whether the Page Cart widget can be rendered on the storefront.
16 */
17 public static function is_available() {
18 return function_exists( 'WC' ) && self::is_widget_registered( Page_Cart::WIDGET_NAME );
19 }
20
21 /**
22 * Output cart page content for the builder cart template.
23 *
24 * Priority: Theme Builder cart template → Elementor cart page → widget fallback.
25 *
26 * @param int|null $builder_template_id Assigned Theme Builder cart template ID.
27 */
28 public static function render_cart_page( $builder_template_id = null ) {
29 $builder_template_id = absint( $builder_template_id );
30
31 if ( $builder_template_id && self::echo_template_content( $builder_template_id ) ) {
32 return;
33 }
34
35 $cart_page_content = self::get_wc_page_elementor_content( wc_get_page_id( 'cart' ) );
36
37 if ( $cart_page_content ) {
38 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
39 echo $cart_page_content;
40
41 return;
42 }
43
44 self::render_widget();
45 }
46
47 /**
48 * Render Elementor content from a WooCommerce page built with Elementor.
49 *
50 * @param int $page_id Page ID.
51 * @return string
52 */
53 protected static function get_wc_page_elementor_content( $page_id ) {
54 $page_id = absint( $page_id );
55
56 if ( ! $page_id || ! did_action( 'elementor/loaded' ) ) {
57 return '';
58 }
59
60 if ( ! Plugin::instance()->db->is_built_with_elementor( $page_id ) ) {
61 return '';
62 }
63
64 return Plugin::instance()->frontend->get_builder_content_for_display( $page_id );
65 }
66
67 /**
68 * Render the Page Cart widget with default settings (fallback).
69 *
70 * @param array $settings Optional widget settings.
71 */
72 public static function render_widget( array $settings = [] ) {
73 if ( ! self::is_available() ) {
74 return;
75 }
76
77 self::enqueue_assets();
78
79 $widget_type = Plugin::instance()->widgets_manager->get_widget_types( Page_Cart::WIDGET_NAME );
80
81 if ( ! $widget_type ) {
82 return;
83 }
84
85 $element = Plugin::instance()->elements_manager->create_element_instance(
86 [
87 'elType' => 'widget',
88 'widgetType' => Page_Cart::WIDGET_NAME,
89 'id' => Page_Cart::WIDGET_NAME . '-fallback',
90 'settings' => wp_parse_args( $settings, Page_Cart::get_default_settings() ),
91 'elements' => [],
92 ],
93 [],
94 $widget_type
95 );
96
97 if ( ! $element ) {
98 return;
99 }
100
101 ob_start();
102 $element->render_content();
103 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
104 echo ob_get_clean();
105 }
106
107 /**
108 * Whether an Elementor widget is registered.
109 *
110 * @param string $widget_slug Widget slug.
111 */
112 protected static function is_widget_registered( $widget_slug ) {
113 if ( ! did_action( 'elementor/loaded' ) ) {
114 return false;
115 }
116
117 return (bool) Plugin::instance()->widgets_manager->get_widget_types( $widget_slug );
118 }
119
120 /**
121 * Render saved Elementor builder template content.
122 *
123 * @param int $template_id Builder template post ID.
124 * @return bool Whether content was rendered.
125 */
126 protected static function echo_template_content( $template_id ) {
127 $template_id = absint( $template_id );
128
129 if ( ! $template_id ) {
130 return false;
131 }
132
133 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
134 echo Plugin::instance()->frontend->get_builder_content( $template_id, false );
135
136 return true;
137 }
138
139 /**
140 * Enqueue Page Cart widget styles on fallback render.
141 */
142 protected static function enqueue_assets() {
143 foreach ( Page_Cart::get_frontend_style_handles() as $style_handle ) {
144 if ( ! wp_style_is( $style_handle, 'enqueued' ) ) {
145 wp_enqueue_style( $style_handle );
146 }
147 }
148 }
149 }
150