PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 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 All 139 releases
learnpress / inc / Gutenberg / GutenbergHandleMain.php

GutenbergHandleMain.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.8, at inc/Gutenberg/GutenbergHandleMain.php

416 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\Gutenberg;
4
5 use LearnPress\Gutenberg\Blocks\AbstractBlockType;
6 use LearnPress\Gutenberg\Templates\AbstractBlockTemplate;
7 use LearnPress\Gutenberg\Templates\SingleCourseItemBlockTemplate;
8 use LearnPress\Gutenberg\Templates\SingleCourseOfflineBlockTemplate;
9 use LearnPress\Helpers\Config;
10 use LearnPress\Helpers\Singleton;
11 use LearnPress\Helpers\Template;
12 use LearnPress\Models\CourseModel;
13 use WP_Block_Template;
14 use WP_Post;
15
16 /**
17 * Class GutenbergHandleMain
18 *
19 * Handle register, render block template
20 * @since 4.2.8.3 Convert from old class Block_Template_Handle
21 * @version 1.0.0
22 */
23 class GutenbergHandleMain {
24 use Singleton;
25
26 /**
27 * Hooks handle block template
28 */
29 public function init() {
30 if ( ! wp_is_block_theme() ) {
31 return;
32 }
33
34 // Register block template and register patterns.
35 add_action( 'init', array( $this, 'wp_hook_init' ) );
36 // Set block template need to show on frontend/backend
37 add_filter( 'get_block_templates', array( $this, 'set_blocks_template' ), 10, 3 );
38 // Load block template when Edit.
39 add_filter( 'pre_get_block_file_template', array( $this, 'edit_block_file_template' ), 10, 3 );
40 // Register block category
41 add_filter( 'block_categories_all', array( $this, 'add_block_category' ), 10, 2 );
42 // Fixed case code block of WooCommerce run on screen list Order is error.
43 add_filter(
44 'current_theme_supports-block-templates',
45 function ( $flag ) {
46 if ( ! is_admin() ) {
47 return $flag;
48 }
49
50 if ( ! function_exists( 'get_current_screen' ) ) {
51 return $flag;
52 }
53
54 $wp_screen = get_current_screen();
55 if ( ! empty( $wp_screen->id ) && $wp_screen->id === 'edit-lp_order' ) {
56 $flag = false;
57 }
58
59 return $flag;
60 }
61 );
62 }
63
64 /**
65 * Hook init of WordPress
66 * .1 Register blocks Gutenberg
67 *
68 * @return void
69 */
70 public function wp_hook_init() {
71 $this->register_blocks();
72 $this->add_block_pattern_categories();
73 $this->add_block_patterns();
74 }
75
76 /**
77 * Register block, render content of block
78 *
79 * @return void
80 */
81 public function register_blocks() {
82 /**
83 * @var AbstractBlockType[] $blocks
84 */
85 $blocks = Config::instance()->get( 'block-elements', 'gutenberg' );
86 //$template_current = $this->get_edit_template();
87
88 foreach ( $blocks as $block_template ) {
89 // Set block maybe display when Edit on Template.
90 /*if ( ! empty( $template_current ) && ! empty( $block_template->display_on_templates )
91 && ! in_array( $template_current, $block_template->display_on_templates ) ) {
92 if ( ! empty( $block_template->ancestor ) ) {
93 $block_template->display_on_templates = [];
94 } else {
95 continue;
96 }
97 } elseif ( ! empty( $block_template->ancestor )
98 && ! empty( $block_template->display_on_templates ) ) {
99 $block_template->ancestor = null;
100 }*/
101
102 // Register script to load on the Backend Edit.
103 wp_register_script(
104 $block_template->name, // Block name
105 $block_template->source_js, // Block script
106 [], // Dependencies
107 uniqid(), // Version,
108 [ 'strategy' => 'defer' ]
109 );
110
111 $args = [];
112 foreach ( get_object_vars( $block_template ) as $property => $value ) {
113 $args[ $property ] = $value;
114 }
115
116 $block_type = $block_template->name;
117 /**
118 * register_block_type_from_metadata
119 *
120 * For case declare path to file block.json
121 */
122 if ( ! empty( $block_template->path_block_json ) ) {
123 $block_type = $block_template->path_block_json;
124 }
125
126 register_block_type(
127 $block_type,
128 $args
129 );
130 }
131 }
132
133 /**
134 * Set block template need to show on frontend/backend
135 * 1. Get all templates of LP declare.
136 * 2. Frontend: check blocks match with type page. Ex: single course, archive course....
137 * 3. If correct, set Block to $query_result.
138 *
139 * @param array $query_result Array of template objects.
140 * @param array $query Optional. Arguments to retrieve templates.
141 * @param mixed $template_type wp_template or wp_template_part.
142 *
143 * @return array
144 * @since 4.2.2.3
145 * @version 1.0.7
146 */
147 public function set_blocks_template( array $query_result, array $query, $template_type ): array {
148 if ( $template_type === 'wp_template_part' ) { // Template not Template part
149 return $query_result;
150 }
151
152 if ( ! is_admin() && post_password_required() ) {
153 return $query_result;
154 }
155
156 // Check is course item.
157 if ( ! is_admin() ) {
158 global $wp;
159 $vars = $wp->query_vars;
160 $item_type = $vars['item-type'] ?? '';
161 $lp_course_item = learn_press_get_post_by_name( $vars['course-item'] ?? '', $item_type );
162 $item_types = CourseModel::item_types_support();
163 if ( $lp_course_item && in_array( $lp_course_item->post_type, $item_types ) ) {
164 $singleCourseItemBlockTemplate = new SingleCourseItemBlockTemplate();
165 $block_custom = $this->is_custom_block_template( $template_type, $singleCourseItemBlockTemplate->slug );
166 if ( $block_custom ) {
167 $singleCourseItemBlockTemplate->is_custom = true;
168 $singleCourseItemBlockTemplate->source = 'custom';
169 $singleCourseItemBlockTemplate->content = traverse_and_serialize_blocks( parse_blocks( $block_custom->post_content ) );
170 }
171
172 /**
173 * Set slug to single course, to compare with slug in query.
174 * Because don't have slug 'single-lp_course_item'.
175 * Slug 'single-lp_course_item' is for save content of block template.
176 */
177 $singleCourseItemBlockTemplate->slug = 'single-lp_course';
178 return [ $singleCourseItemBlockTemplate ];
179 }
180 }
181 // End check course item.
182
183 // Check is course offline.
184 if ( ! is_admin() ) {
185 global $wp;
186 $object = get_queried_object();
187 if ( $object && isset( $object->ID ) ) {
188 $courseModel = CourseModel::find( $object->ID, true );
189 if ( $courseModel && $courseModel->is_offline() ) {
190 $singleCourseOfflineBlockTemplate = new SingleCourseOfflineBlockTemplate();
191 $block_custom = $this->is_custom_block_template( $template_type, $singleCourseOfflineBlockTemplate->slug );
192 if ( $block_custom ) {
193 $singleCourseOfflineBlockTemplate->is_custom = true;
194 $singleCourseOfflineBlockTemplate->source = 'custom';
195 $singleCourseOfflineBlockTemplate->content = traverse_and_serialize_blocks( parse_blocks( $block_custom->post_content ) );
196 }
197
198 $singleCourseOfflineBlockTemplate->slug = 'single-lp_course';
199 return [ $singleCourseOfflineBlockTemplate ];
200 }
201 }
202 }
203 // End check course offline.
204
205 // wp_enqueue_script( 'editor-check' );
206
207 /**
208 * @var AbstractBlockTemplate[] $lp_block_templates
209 */
210 $lp_block_templates = Config::instance()->get( 'block-templates', 'gutenberg' );
211 foreach ( $lp_block_templates as $block_template ) {
212
213 $instance = new $block_template();
214 if ( isset( $query['post_type'] ) &&
215 isset( $instance->post_types ) &&
216 ! in_array( $query['post_type'], $instance->post_types, true )
217 ) {
218 continue;
219 }
220
221 // Get block template if custom - save on table posts - with post_name = slug of block.
222 $block_custom = $this->is_custom_block_template( $template_type, $block_template->slug );
223 if ( $block_custom ) {
224 $block_template->is_custom = true;
225 $block_template->source = 'custom';
226 if ( version_compare( get_bloginfo( 'version' ), '6.4-beta', '>=' ) ) {
227 $block_template->content = traverse_and_serialize_blocks( parse_blocks( $block_custom->post_content ) );
228 } else {
229 $block_template->content = _inject_theme_attribute_in_block_template_content( $block_custom->post_content );
230 }
231 }
232
233 // Frontend: check block template match with slug in query will display.
234 $fits_slug_query = ! isset( $query['slug__in'] ) || in_array( $instance->slug, $query['slug__in'], true );
235 $fits_area_query = ! isset( $query['area'] ) || ( property_exists( $instance, 'area' ) && $instance->area === $query['area'] );
236 $should_include = $fits_slug_query && $fits_area_query;
237 if ( $should_include ) {
238 $query_result[] = $block_template;
239 }
240 }
241
242 return $query_result;
243 }
244
245 /**
246 * Load template block when edit and save.
247 *
248 * @param WP_Block_Template|null|mixed $template
249 * @param string $id
250 * @param string $template_type
251 *
252 * @return WP_Block_Template|null
253 */
254 public function edit_block_file_template( $template = null, string $id = '', string $template_type = '' ) {
255 /**
256 * @var AbstractBlockTemplate[] $template
257 */
258 $lp_block_templates = Config::instance()->get( 'block-templates', 'gutenberg' );
259 foreach ( $lp_block_templates as $block_template ) {
260 if ( $id === $block_template->id ) {
261 $template = $block_template;
262 break;
263 }
264 }
265
266 return $template;
267 }
268
269 /**
270 * Check is custom block template
271 *
272 * @param $template_type
273 * @param $post_name
274 *
275 * @return WP_Post|null
276 */
277 public function is_custom_block_template( $template_type, $post_name ) {
278 $post_block_theme = null;
279
280 $check_query_args = array(
281 'post_type' => $template_type,
282 'posts_per_page' => 1,
283 'no_found_rows' => true,
284 'post_name__in' => array( $post_name ),
285 );
286
287 $check = new \WP_Query( $check_query_args );
288
289 if ( count( $check->get_posts() ) > 0 ) {
290 $post_block_theme = $check->get_posts()[0];
291 }
292
293 return $post_block_theme;
294 }
295
296 /**
297 * Register block category
298 *
299 * @param array $block_categories
300 * @param $editor_context
301 *
302 * @return array
303 */
304 public function add_block_category( array $block_categories, $editor_context ) {
305 $lp_block_categories = [
306 [
307 'slug' => 'learnpress-legacy',
308 'title' => __( 'LearnPress Legacy', 'learnpress' ),
309 'icon' => null,
310 ],
311 [
312 'slug' => 'learnpress-category',
313 'title' => __( 'LearnPress Global', 'learnpress' ),
314 'icon' => null,
315 ],
316 [
317 'slug' => 'learnpress-course-elements',
318 'title' => __( 'LearnPress Course Elements', 'learnpress' ),
319 'icon' => null,
320 ],
321 ];
322
323 foreach ( $lp_block_categories as $block_category ) {
324 array_unshift( $block_categories, $block_category );
325 }
326
327 return $block_categories;
328 }
329
330 public function get_edit_template() {
331 $template_current = '';
332
333 if ( is_admin() && isset( $_GET['p'] ) ) {
334 if ( function_exists( '\get_current_screen' ) ) {
335 $screen = \get_current_screen();
336 if ( $screen && (
337 $screen->base === 'site-editor' ||
338 $screen->id === 'appearance_page_gutenberg-edit-site' ||
339 strpos( $screen->id, 'edit-site' ) !== false
340 ) ) {
341 $template_path = urldecode( $_GET['p'] );
342 $template_path = str_replace( '/wp_template/', '', $template_path );
343 $template_current = trim( $template_path, '/' );
344 }
345 } else {
346 $template_path = urldecode( $_GET['p'] );
347 $template_path = str_replace( '/wp_template/', '', $template_path );
348 $template_current = trim( $template_path, '/' );
349 }
350 }
351
352 if ( empty( $template_current ) ) {
353 if ( ! empty( $_REQUEST['postId'] ) ) {
354 $template_current = $_REQUEST['postId'];
355 } elseif ( ! empty( $_REQUEST['post'] ) ) {
356 $template_current = $_REQUEST['post'];
357 } elseif ( ! empty( $_REQUEST['post_id'] ) ) {
358 $template_current = $_REQUEST['post_id'];
359 } elseif ( function_exists( 'get_the_ID' ) && get_the_ID() ) {
360 $template_current = get_the_ID();
361 } elseif ( isset( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->ID ) ) {
362 $template_current = $GLOBALS['post']->ID;
363 }
364 if ( ! is_numeric( $template_current ) ) {
365 $template_post = get_page_by_path( $template_current, OBJECT, 'wp_template' );
366 if ( $template_post ) {
367 $template_current = $template_post->ID;
368 }
369 }
370 }
371
372 return $template_current;
373 }
374
375 public function add_block_patterns() {
376 $list_course_pattern = file_get_contents( Template::instance( false )->get_frontend_template_type_block( 'patterns/list-courses-pattern.html' ) );
377 register_block_pattern(
378 'learnpress/list-course-pattern',
379 array(
380 'title' => __( 'Layout List Course', 'learnpress' ),
381 'description' => __( 'List Course Learnpress', 'learnpress' ),
382 'categories' => array( 'learnpress-patterns' ),
383 'content' => $list_course_pattern,
384 )
385 );
386 $grid_course_pattern = file_get_contents( Template::instance( false )->get_frontend_template_type_block( 'patterns/grid-courses-pattern.html' ) );
387 register_block_pattern(
388 'learnpress/grid-course-pattern',
389 array(
390 'title' => __( 'Layout Grid Course', 'learnpress' ),
391 'description' => __( 'List Course Learnpress - layout grid', 'learnpress' ),
392 'categories' => array( 'learnpress-patterns' ),
393 'content' => $grid_course_pattern,
394 )
395 );
396
397 $single_instructor_pattern = file_get_contents( Template::instance( false )->get_frontend_template_type_block( 'patterns/single-instructor-pattern.html' ) );
398 register_block_pattern(
399 'learnpress/single-instructor-pattern',
400 array(
401 'title' => __( 'Single Instructor', 'learnpress' ),
402 'description' => __( 'Single Instructor Learnpress', 'learnpress' ),
403 'categories' => array( 'learnpress-patterns' ),
404 'content' => $single_instructor_pattern,
405 )
406 );
407 }
408
409 public function add_block_pattern_categories() {
410 register_block_pattern_category(
411 'learnpress-patterns',
412 array( 'label' => __( 'LearnPress', 'learnpress' ) )
413 );
414 }
415 }
416