ConvertsBlocks.php
170 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Integrations\Bricks\Concerns; |
| 4 | |
| 5 | trait ConvertsBlocks { |
| 6 | /** |
| 7 | * Show if we should populate content on empty. |
| 8 | * |
| 9 | * @var string |
| 10 | */ |
| 11 | public $show_populate_on_empty = false; |
| 12 | |
| 13 | /** |
| 14 | * Get the block html |
| 15 | * |
| 16 | * @param array $block_attributes The block attributes. |
| 17 | * @param string $content The block content. |
| 18 | * |
| 19 | * @return string |
| 20 | */ |
| 21 | protected function html( $block_attributes = [], $content = '' ) { |
| 22 | // Previewing a template, show a placeholder to populate. |
| 23 | if ( $this->show_populate_on_empty ) { |
| 24 | $product = sc_get_product(); |
| 25 | |
| 26 | if ( empty( $product ) ) { |
| 27 | return $this->render_element_placeholder( |
| 28 | [ |
| 29 | 'title' => esc_html__( 'For better preview select content to show.', 'surecart' ), |
| 30 | 'description' => esc_html__( 'Go to: Settings > Template Settings > Populate Content', 'surecart' ), |
| 31 | ] |
| 32 | ); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // add the rendered class and id. |
| 37 | $rendered_attributes = $this->get_block_rendered_attributes(); |
| 38 | $block_attributes['className'] = $rendered_attributes['class']; |
| 39 | $block_attributes['anchor'] = $rendered_attributes['id']; |
| 40 | |
| 41 | // get the bricks attributes. |
| 42 | $key = '_root'; |
| 43 | $attributes = apply_filters( 'bricks/element/render_attributes', $this->attributes, $key, $this ); |
| 44 | |
| 45 | $block = sc_pre_render_blocks( '<!-- wp:' . $this->block_name . ' ' . ( is_array( $block_attributes ) ? wp_json_encode( $block_attributes, JSON_FORCE_OBJECT ) : '' ) . ' -->' . $content . '<!-- /wp:' . $this->block_name . ' -->' ); |
| 46 | |
| 47 | // start the processor and select the first tag. |
| 48 | $processor = new \WP_HTML_Tag_Processor( $block ); |
| 49 | $processor->next_tag(); |
| 50 | |
| 51 | // Return: No attributes set for this element. |
| 52 | if ( ! isset( $attributes[ $key ] ) ) { |
| 53 | return $processor->get_updated_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 54 | } |
| 55 | |
| 56 | foreach ( $attributes[ $key ] as $name => $value ) { |
| 57 | // handle array and filter out empty values. |
| 58 | if ( is_array( $value ) ) { |
| 59 | $value = array_filter( |
| 60 | $value, |
| 61 | function ( $val ) { |
| 62 | return ! empty( $val ) || is_numeric( $val ); |
| 63 | } |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | // handle class array. |
| 68 | if ( 'class' === $name ) { |
| 69 | foreach ( $value as $class ) { |
| 70 | $processor->add_class( $class ); |
| 71 | } |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | // handle the rest of the string value attributes. |
| 76 | if ( is_string( $value ) ) { |
| 77 | $processor->set_attribute( $name, $value ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // return updated html. |
| 82 | return $processor->get_updated_html(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Render the raw block output. |
| 87 | * |
| 88 | * @param array $block_attributes The block attributes. |
| 89 | * @param string $content The block content. |
| 90 | * |
| 91 | * @return string |
| 92 | */ |
| 93 | public function raw( $block_attributes = [], $content = '' ) { |
| 94 | if ( $this->show_populate_on_empty ) { |
| 95 | $product = sc_get_product(); |
| 96 | |
| 97 | if ( empty( $product ) ) { |
| 98 | return $this->render_element_placeholder( |
| 99 | [ |
| 100 | 'title' => esc_html__( 'For better preview select content to show.', 'surecart' ), |
| 101 | 'description' => esc_html__( 'Go to: Settings > Template Settings > Populate Content', 'surecart' ), |
| 102 | ] |
| 103 | ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | $rendered_attributes = $this->get_block_rendered_attributes(); |
| 108 | $block_attributes['className'] = $rendered_attributes['class']; |
| 109 | $block_attributes['anchor'] = $rendered_attributes['id']; |
| 110 | |
| 111 | return '<!-- wp:' . $this->block_name . ' ' . ( is_array( $block_attributes ) ? wp_json_encode( $block_attributes, JSON_FORCE_OBJECT ) : '' ) . ' -->' . $content . '<!-- /wp:' . $this->block_name . ' -->'; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Preview block. |
| 116 | * |
| 117 | * @param string $content The block content. |
| 118 | * @param string $class_name The class name. |
| 119 | * @param string $tag The tag. |
| 120 | * |
| 121 | * @return string |
| 122 | */ |
| 123 | public function preview( $content = '', $class_name = '', $tag = 'div' ) { |
| 124 | $rendered_attributes = $this->get_block_rendered_attributes(); |
| 125 | $class = $class_name . ' ' . $rendered_attributes['class']; |
| 126 | $id = $rendered_attributes['id']; |
| 127 | |
| 128 | $output = "<$tag id='$id' class='$class'>"; |
| 129 | $output .= $content; |
| 130 | $output .= "</$tag>"; |
| 131 | |
| 132 | return $output; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get the rendered attributes. |
| 137 | * |
| 138 | * @return array |
| 139 | */ |
| 140 | public function get_block_rendered_attributes(): array { |
| 141 | $processor = new \WP_HTML_Tag_Processor( "<div {$this->render_attributes( '_root' )}></div>" ); |
| 142 | $processor->next_tag(); |
| 143 | |
| 144 | $block_attributes['class'] = $processor->get_attribute( 'class' ) ?? ''; |
| 145 | $block_attributes['id'] = $processor->get_attribute( 'id' ) ?? ''; |
| 146 | |
| 147 | return $block_attributes; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Whether it is editor or not. |
| 152 | * |
| 153 | * @return bool |
| 154 | */ |
| 155 | public function is_admin_editor() { |
| 156 | return ! bricks_is_frontend() || bricks_is_builder_call(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get raw color. |
| 161 | * |
| 162 | * @param string $key The key. |
| 163 | * |
| 164 | * @return string |
| 165 | */ |
| 166 | public function get_raw_color( $key ) { |
| 167 | return $this->settings[ $key ]['hex'] ?? $this->settings[ $key ]['raw'] ?? ''; |
| 168 | } |
| 169 | } |
| 170 |