← All changes
|
app/Modules/Templating/Bricks/Elements/ProductContent.php
+71
-22
1.3.21
→
1.6.6
View file →
| @@ -4,30 +4,61 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Bricks\Element; |
| 6 | 6 | use Bricks\Helpers; |
| 7 | 7 | use FluentCart\App\CPT\FluentProducts; |
| 8 | -use FluentCart\App\Services\URL; | |
| 8 | +use FluentCart\App\Modules\Templating\Bricks\BricksLoader; | |
| 9 | +use FluentCart\App\Modules\Templating\Bricks\BricksHelper; | |
| 10 | +use FluentCart\App\Modules\Data\ProductDataSetup; | |
| 11 | +use FluentCart\App\Services\Renderer\ProductRenderer; | |
| 9 | 12 | |
| 10 | 13 | class ProductContent extends Element |
| 11 | 14 | { |
| 12 | - public $category = 'fluent_cart_product'; | |
| 15 | + public $category = 'fluent-cart'; | |
| 13 | 16 | public $name = 'fct-product-content'; |
| 14 | - public $icon = 'ti-wordpress'; | |
| 17 | + public $icon = 'ion-md-list-box fluent-cart-element-icon'; | |
| 15 | 18 | |
| 16 | 19 | public function get_label() |
| 17 | 20 | { |
| 18 | - return esc_html__('Product content', 'fluent-cart'); | |
| 21 | + return esc_html__('Product Content', 'fluent-cart'); | |
| 19 | 22 | } |
| 20 | 23 | |
| 21 | 24 | public function set_controls() |
| 22 | 25 | { |
| 23 | - $edit_link = Helpers::get_preview_post_link( get_the_ID() ); | |
| 24 | - $label = esc_html__('Edit product content in FluentCart.', 'fluent-cart'); | |
| 26 | + $this->controls['queryType'] = [ | |
| 27 | + 'tab' => 'content', | |
| 28 | + 'type' => 'select', | |
| 29 | + 'label' => esc_html__('Query Type', 'fluent-cart'), | |
| 30 | + 'options' => [ | |
| 31 | + 'default' => esc_html__('Default', 'fluent-cart'), | |
| 32 | + 'custom' => esc_html__('Custom', 'fluent-cart'), | |
| 33 | + ], | |
| 34 | + 'default' => 'default', | |
| 35 | + 'inline' => true, | |
| 36 | + ]; | |
| 25 | 37 | |
| 38 | + $this->controls['productId'] = [ | |
| 39 | + 'tab' => 'content', | |
| 40 | + 'type' => 'select', | |
| 41 | + 'label' => esc_html__('Product', 'fluent-cart'), | |
| 42 | + 'options' => BricksLoader::getProductOptions(), | |
| 43 | + 'placeholder' => esc_html__('Select a product', 'fluent-cart'), | |
| 44 | + 'searchable' => true, | |
| 45 | + 'rerender' => true, | |
| 46 | + 'required' => ['queryType', '=', 'custom'], | |
| 47 | + ]; | |
| 48 | + | |
| 49 | + $this->controls['manualProductId'] = [ | |
| 50 | + 'tab' => 'content', | |
| 51 | + 'type' => 'text', | |
| 52 | + 'label' => esc_html__('Manual Product ID', 'fluent-cart'), | |
| 53 | + 'description' => esc_html__('Use this if the product is not available in dropdown.', 'fluent-cart'), | |
| 54 | + 'required' => [['queryType', '=', 'custom'], ['productId', '=', '']], | |
| 55 | + ]; | |
| 56 | + | |
| 26 | 57 | $this->controls['info'] = [ |
| 27 | 58 | 'tab' => 'content', |
| 28 | 59 | 'type' => 'info', |
| 29 | - 'content' => $edit_link ? '<a href="' . esc_url($edit_link) . '" target="_blank">' . $label . '</a>' : $label, | |
| 60 | + 'content' => esc_html__('Edit product content in FluentCart.', 'fluent-cart'), | |
| 30 | 61 | ]; |
| 31 | 62 | } |
| 32 | 63 | |
| 33 | 64 | public function render() |
| @@ -32,28 +63,36 @@ | ||
| 32 | 63 | |
| 33 | 64 | public function render() |
| 34 | 65 | { |
| 35 | 66 | $settings = $this->settings; |
| 67 | + $queryType = $settings['queryType'] ?? 'default'; | |
| 36 | 68 | |
| 37 | - $product = get_post($this->post_id); | |
| 69 | + if ($queryType === 'default') { | |
| 70 | + $productId = $this->post_id; | |
| 71 | + } else { | |
| 72 | + $productId = !empty($settings['productId']) ? \intval($settings['productId']) : 0; | |
| 73 | + $manualProductId = !empty($settings['manualProductId']) ? \intval($settings['manualProductId']) : 0; | |
| 38 | 74 | |
| 39 | - if (empty($product) || $product->post_type !== FluentProducts::CPT_NAME) { | |
| 40 | - return $this->render_element_placeholder( | |
| 41 | - [ | |
| 42 | - 'title' => esc_html__('For better preview select content to show.', 'fluent-cart'), | |
| 43 | - 'description' => esc_html__('Go to: Settings > Template Settings > Populate Content', 'fluent-cart'), | |
| 44 | - ] | |
| 45 | - ); | |
| 75 | + if (!$productId && $manualProductId) { | |
| 76 | + $productId = $manualProductId; | |
| 77 | + } | |
| 46 | 78 | } |
| 47 | 79 | |
| 48 | - $content = get_post_field('post_content', $this->post_id); | |
| 80 | + $product = ProductDataSetup::getProductModel($productId); | |
| 49 | 81 | |
| 82 | + if (!$product) { | |
| 83 | + return $this->render_element_placeholder([ | |
| 84 | + 'title' => esc_html__('Select a product', 'fluent-cart'), | |
| 85 | + ]); | |
| 86 | + } | |
| 87 | + | |
| 88 | + $content = $product->post_content; | |
| 89 | + | |
| 90 | + | |
| 50 | 91 | if (!$content) { |
| 51 | - return $this->render_element_placeholder( | |
| 52 | - [ | |
| 53 | - 'title' => esc_html__('Product content is empty.', 'fluent-cart'), | |
| 54 | - ] | |
| 55 | - ); | |
| 92 | + return $this->render_element_placeholder([ | |
| 93 | + 'title' => esc_html__('Product content is empty.', 'fluent-cart'), | |
| 94 | + ]); | |
| 56 | 95 | } |
| 57 | 96 | |
| 58 | 97 | $content = $this->render_dynamic_data($content); |
| 59 | 98 | $content = Helpers::parse_editor_content($content); |
| @@ -58,7 +97,17 @@ | ||
| 58 | 97 | $content = $this->render_dynamic_data($content); |
| 59 | 98 | $content = Helpers::parse_editor_content($content); |
| 60 | 99 | $content = str_replace(']]>', ']]>', $content); |
| 61 | 100 | |
| 62 | - echo "<div {$this->render_attributes( '_root' )}>" . wp_kses_post($content) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render_attributes() handles escaping | |
| 101 | + ?> | |
| 102 | + <div | |
| 103 | + <?php | |
| 104 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render_attributes() handles escaping | |
| 105 | + echo $this->render_attributes( '_root' ); | |
| 106 | + ?> | |
| 107 | + > | |
| 108 | + <?php echo wp_kses($content, BricksHelper::getAllowedHtmlForContent()); ?> | |
| 109 | + </div> | |
| 110 | + | |
| 111 | + <?php | |
| 63 | 112 | } |
| 64 | 113 | } |