PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.7
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / Gutenberg / Blocks / AbstractBlockType.php

AbstractBlockType.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.7, at inc/Gutenberg/Blocks/AbstractBlockType.php

193 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace LearnPress\Gutenberg\Blocks;
3
4 use LearnPress\Gutenberg\Utils\StyleAttributes;
5 use WP_Block;
6 use WP_Block_Type;
7
8 /**
9 * Class AbstractBlockType
10 *
11 * Handle register, render block template
12 */
13 abstract class AbstractBlockType extends WP_Block_Type {
14 public $namespace = 'learnpress';
15 public $textdomain = 'learnpress';
16 public $api_version = 3;
17 /**
18 * @var string block name - Field of LP.
19 */
20 public $block_name = '';
21 /**
22 * @var string path of the file run js - Field of LP.
23 */
24 public $source_js = '';
25 /**
26 * Metadata of block.
27 * @var string path of the file block.js - Field of LP.
28 */
29 public $path_block_json = '';
30 /**
31 * @var string Screen Template want to display - Field of LP
32 */
33 public $display_on_templates = [];
34 /**
35 * Tracks if assets have been enqueued.
36 *
37 * @var boolean
38 */
39 protected $enqueued_assets = false;
40 /**
41 * @var string Class hash - Field of LP
42 */
43 protected $class_hash = '';
44
45 public function __construct() {
46 $this->source_js = $this->get_source_js();
47 parent::__construct( $this->get_block_type() );
48 $this->editor_script_handles = $this->get_editor_script_handles();
49 $this->render_callback = $this->get_render_callback();
50 $this->supports = $this->get_supports();
51 $this->attributes = $this->get_attributes();
52 $this->ancestor = $this->get_ancestor();
53 $this->provides_context = $this->get_provides_context();
54 }
55
56 /**
57 * Set to name block.
58 * Or set path js handle of block.
59 * For Backend
60 *
61 * @return string[]
62 */
63 protected function get_editor_script_handles(): array {
64 return [ $this->name ];
65 }
66
67 /**
68 * Set render callback for block.
69 *
70 * @return array
71 */
72 protected function get_render_callback(): array {
73 return [ $this, 'render_content_block_template' ];
74 }
75
76 /**
77 * Get supports.
78 *
79 * @return array|null
80 */
81 protected function get_supports() {
82 return null;
83 }
84
85 /**
86 * Get supports.
87 *
88 * @return array|null
89 */
90 protected function get_ancestor() {
91 return null;
92 }
93
94 /**
95 * Get supports.
96 *
97 * @return array|null
98 */
99 protected function get_provides_context() {
100 return null;
101 }
102
103 /**
104 * Render content of block tag
105 *
106 * @param array $attributes | Attributes of block tag.
107 * @param string $content
108 * @param WP_Block $block
109 *
110 * @return string
111 */
112 abstract public function render_content_block_template( array $attributes, $content, $block ): string;
113
114 protected function enqueue_assets() {
115 if ( $this->enqueued_assets ) {
116 return;
117 }
118 wp_enqueue_style( 'lp-blocks-style', get_stylesheet_uri() );
119 $this->enqueued_assets = true;
120 }
121
122 protected function get_block_type() {
123 return $this->namespace . '/' . $this->block_name;
124 }
125
126 protected function get_source_js() {
127 return LP_PLUGIN_URL . 'assets/js/dist/blocks/' . $this->block_name . '.js';
128 }
129
130 protected function get_class_hash() {
131 $hash = bin2hex( random_bytes( 16 ) );
132 $timestamp = time();
133 $this->class_hash = 'lp-elements-' . $timestamp . '-' . $hash;
134 }
135
136 /**
137 * Wrap content in a block tag.
138 * Will get the attributes, supports, classes... from the block.
139 * The block's supports must be configured in both block.json and PHP to be consistent.
140 * If method generate not provide enough to handle special logic, you can override this method.
141 *
142 * @param string $content
143 * @param string $tag
144 * @param array $extra_attributes
145 *
146 * @return string
147 */
148 protected function get_output( string $content, string $tag = 'div', array $extra_attributes = [] ): string {
149 $wrapper = get_block_wrapper_attributes( $extra_attributes );
150 return sprintf(
151 "<$tag %s>%s</$tag>",
152 $wrapper,
153 $content
154 );
155 }
156
157 protected function get_output_with_class_hash( $attributes, $content, $properties = array(), $exclude = array() ) {
158 $output = '';
159 $class_hash = $this->class_hash ?? '';
160 $classes = $attributes['className'] ?? '';
161 $border_classes_and_styles = StyleAttributes::get_classes_and_styles_by_attributes( $attributes, $properties, $exclude );
162 $class_default = 'wp-block-' . $this->namespace . '-' . $this->block_name;
163 $class = $class_default ? $class_default : '';
164 $style = '';
165
166 if ( ! empty( $classes ) ) {
167 $class .= ' ' . $classes;
168 }
169
170 if ( ! empty( $class_hash ) ) {
171 $class .= ' ' . $class_hash;
172 }
173
174 if ( ! empty( $border_classes_and_styles['classes'] ) ) {
175 $class .= ' ' . $border_classes_and_styles['classes'];
176 }
177
178 if ( ! empty( $border_classes_and_styles['classes'] ) ) {
179 $style = sprintf( 'style="%s"', $border_classes_and_styles['styles'] );
180 }
181
182 ob_start();
183 echo sprintf(
184 '<div class="%s" %s>%s</div>',
185 $class,
186 $style,
187 $content
188 );
189 $output = ob_get_clean();
190 return $output;
191 }
192 }
193