| @@ -1,417 +1,415 @@ | ||
| 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 ( $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.6 | |
| 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 | - $query_result[] = $singleCourseItemBlockTemplate; | |
| 179 | - return $query_result; | |
| 180 | - } | |
| 181 | - } | |
| 182 | - // End check course item. | |
| 183 | - | |
| 184 | - // Check is course offline. | |
| 185 | - if ( ! is_admin() ) { | |
| 186 | - global $wp; | |
| 187 | - $object = get_queried_object(); | |
| 188 | - if ( $object && isset( $object->ID ) ) { | |
| 189 | - $courseModel = CourseModel::find( $object->ID, true ); | |
| 190 | - if ( $courseModel && $courseModel->is_offline() ) { | |
| 191 | - $singleCourseOfflineBlockTemplate = new SingleCourseOfflineBlockTemplate(); | |
| 192 | - $block_custom = $this->is_custom_block_template( $template_type, $singleCourseOfflineBlockTemplate->slug ); | |
| 193 | - if ( $block_custom ) { | |
| 194 | - $singleCourseOfflineBlockTemplate->is_custom = true; | |
| 195 | - $singleCourseOfflineBlockTemplate->source = 'custom'; | |
| 196 | - $singleCourseOfflineBlockTemplate->content = traverse_and_serialize_blocks( parse_blocks( $block_custom->post_content ) ); | |
| 197 | - } | |
| 198 | - | |
| 199 | - $singleCourseOfflineBlockTemplate->slug = 'single-lp_course'; | |
| 200 | - $query_result[] = $singleCourseOfflineBlockTemplate; | |
| 201 | - return $query_result; | |
| 202 | - } | |
| 203 | - } | |
| 204 | - } | |
| 205 | - // End check course offline. | |
| 206 | - | |
| 207 | - // wp_enqueue_script( 'editor-check' ); | |
| 208 | - | |
| 209 | - /** | |
| 210 | - * @var AbstractBlockTemplate[] $lp_block_templates | |
| 211 | - */ | |
| 212 | - $lp_block_templates = Config::instance()->get( 'block-templates', 'gutenberg' ); | |
| 213 | - foreach ( $lp_block_templates as $block_template ) { | |
| 214 | - | |
| 215 | - $instance = new $block_template(); | |
| 216 | - if ( isset( $query['post_type'] ) && | |
| 217 | - isset( $instance->post_types ) && | |
| 218 | - ! in_array( $query['post_type'], $instance->post_types, true ) | |
| 219 | - ) { | |
| 220 | - continue; | |
| 221 | - } | |
| 222 | - | |
| 223 | - // Get block template if custom - save on table posts - with post_name = slug of block. | |
| 224 | - $block_custom = $this->is_custom_block_template( $template_type, $block_template->slug ); | |
| 225 | - if ( $block_custom ) { | |
| 226 | - $block_template->is_custom = true; | |
| 227 | - $block_template->source = 'custom'; | |
| 228 | - if ( version_compare( get_bloginfo( 'version' ), '6.4-beta', '>=' ) ) { | |
| 229 | - $block_template->content = traverse_and_serialize_blocks( parse_blocks( $block_custom->post_content ) ); | |
| 230 | - } else { | |
| 231 | - $block_template->content = _inject_theme_attribute_in_block_template_content( $block_custom->post_content ); | |
| 232 | - } | |
| 233 | - } | |
| 234 | - | |
| 235 | - // Frontend: check block template match with slug in query will display. | |
| 236 | - $fits_slug_query = ! isset( $query['slug__in'] ) || in_array( $instance->slug, $query['slug__in'], true ); | |
| 237 | - $fits_area_query = ! isset( $query['area'] ) || ( property_exists( $instance, 'area' ) && $instance->area === $query['area'] ); | |
| 238 | - $should_include = $fits_slug_query && $fits_area_query; | |
| 239 | - if ( $should_include ) { | |
| 240 | - $query_result[] = $block_template; | |
| 241 | - } | |
| 242 | - } | |
| 243 | - | |
| 244 | - return $query_result; | |
| 245 | - } | |
| 246 | - | |
| 247 | - /** | |
| 248 | - * Load template block when edit and save. | |
| 249 | - * | |
| 250 | - * @param WP_Block_Template|null|mixed $template | |
| 251 | - * @param string $id | |
| 252 | - * @param string $template_type | |
| 253 | - * | |
| 254 | - * @return WP_Block_Template|null | |
| 255 | - */ | |
| 256 | - public function edit_block_file_template( $template = null, string $id = '', string $template_type = '' ) { | |
| 257 | - /** | |
| 258 | - * @var AbstractBlockTemplate[] $template | |
| 259 | - */ | |
| 260 | - $lp_block_templates = Config::instance()->get( 'block-templates', 'gutenberg' ); | |
| 261 | - foreach ( $lp_block_templates as $block_template ) { | |
| 262 | - if ( $id === $block_template->id ) { | |
| 263 | - $template = $block_template; | |
| 264 | - break; | |
| 265 | - } | |
| 266 | - } | |
| 267 | - | |
| 268 | - return $template; | |
| 269 | - } | |
| 270 | - | |
| 271 | - /** | |
| 272 | - * Check is custom block template | |
| 273 | - * | |
| 274 | - * @param $template_type | |
| 275 | - * @param $post_name | |
| 276 | - * | |
| 277 | - * @return WP_Post|null | |
| 278 | - */ | |
| 279 | - public function is_custom_block_template( $template_type, $post_name ) { | |
| 280 | - $post_block_theme = null; | |
| 281 | - | |
| 282 | - $check_query_args = array( | |
| 283 | - 'post_type' => $template_type, | |
| 284 | - 'posts_per_page' => 1, | |
| 285 | - 'no_found_rows' => true, | |
| 286 | - 'post_name__in' => array( $post_name ), | |
| 287 | - ); | |
| 288 | - | |
| 289 | - $check = new \WP_Query( $check_query_args ); | |
| 290 | - | |
| 291 | - if ( count( $check->get_posts() ) > 0 ) { | |
| 292 | - $post_block_theme = $check->get_posts()[0]; | |
| 293 | - } | |
| 294 | - | |
| 295 | - return $post_block_theme; | |
| 296 | - } | |
| 297 | - | |
| 298 | - /** | |
| 299 | - * Register block category | |
| 300 | - * | |
| 301 | - * @param array $block_categories | |
| 302 | - * @param $editor_context | |
| 303 | - * | |
| 304 | - * @return array | |
| 305 | - */ | |
| 306 | - public function add_block_category( array $block_categories, $editor_context ) { | |
| 307 | - $lp_block_categories = [ | |
| 308 | - [ | |
| 309 | - 'slug' => 'learnpress-legacy', | |
| 310 | - 'title' => __( 'LearnPress Legacy', 'learnpress' ), | |
| 311 | - 'icon' => null, | |
| 312 | - ], | |
| 313 | - [ | |
| 314 | - 'slug' => 'learnpress-category', | |
| 315 | - 'title' => __( 'LearnPress Global', 'learnpress' ), | |
| 316 | - 'icon' => null, | |
| 317 | - ], | |
| 318 | - [ | |
| 319 | - 'slug' => 'learnpress-course-elements', | |
| 320 | - 'title' => __( 'LearnPress Course Elements', 'learnpress' ), | |
| 321 | - 'icon' => null, | |
| 322 | - ], | |
| 323 | - ]; | |
| 324 | - | |
| 325 | - foreach ( $lp_block_categories as $block_category ) { | |
| 326 | - array_unshift( $block_categories, $block_category ); | |
| 327 | - } | |
| 328 | - | |
| 329 | - return $block_categories; | |
| 330 | - } | |
| 331 | - | |
| 332 | - public function get_edit_template() { | |
| 333 | - $template_current = ''; | |
| 334 | - | |
| 335 | - if ( is_admin() && isset( $_GET['p'] ) ) { | |
| 336 | - if ( function_exists( '\get_current_screen' ) ) { | |
| 337 | - $screen = \get_current_screen(); | |
| 338 | - if ( $screen && ( | |
| 339 | - $screen->base === 'site-editor' || | |
| 340 | - $screen->id === 'appearance_page_gutenberg-edit-site' || | |
| 341 | - strpos( $screen->id, 'edit-site' ) !== false | |
| 342 | - ) ) { | |
| 343 | - $template_path = urldecode( $_GET['p'] ); | |
| 344 | - $template_path = str_replace( '/wp_template/', '', $template_path ); | |
| 345 | - $template_current = trim( $template_path, '/' ); | |
| 346 | - } | |
| 347 | - } else { | |
| 348 | - $template_path = urldecode( $_GET['p'] ); | |
| 349 | - $template_path = str_replace( '/wp_template/', '', $template_path ); | |
| 350 | - $template_current = trim( $template_path, '/' ); | |
| 351 | - } | |
| 352 | - } | |
| 353 | - | |
| 354 | - if ( empty( $template_current ) ) { | |
| 355 | - if ( ! empty( $_REQUEST['postId'] ) ) { | |
| 356 | - $template_current = $_REQUEST['postId']; | |
| 357 | - } elseif ( ! empty( $_REQUEST['post'] ) ) { | |
| 358 | - $template_current = $_REQUEST['post']; | |
| 359 | - } elseif ( ! empty( $_REQUEST['post_id'] ) ) { | |
| 360 | - $template_current = $_REQUEST['post_id']; | |
| 361 | - } elseif ( function_exists( 'get_the_ID' ) && get_the_ID() ) { | |
| 362 | - $template_current = get_the_ID(); | |
| 363 | - } elseif ( isset( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->ID ) ) { | |
| 364 | - $template_current = $GLOBALS['post']->ID; | |
| 365 | - } | |
| 366 | - if ( ! is_numeric( $template_current ) ) { | |
| 367 | - $template_post = get_page_by_path( $template_current, OBJECT, 'wp_template' ); | |
| 368 | - if ( $template_post ) { | |
| 369 | - $template_current = $template_post->ID; | |
| 370 | - } | |
| 371 | - } | |
| 372 | - } | |
| 373 | - | |
| 374 | - return $template_current; | |
| 375 | - } | |
| 376 | - | |
| 377 | - public function add_block_patterns() { | |
| 378 | - $list_course_pattern = file_get_contents( Template::instance( false )->get_frontend_template_type_block( 'patterns/list-courses-pattern.html' ) ); | |
| 379 | - register_block_pattern( | |
| 380 | - 'learnpress/list-course-pattern', | |
| 381 | - array( | |
| 382 | - 'title' => __( 'Layout List Course', 'learnpress' ), | |
| 383 | - 'description' => __( 'List Course Learnpress', 'learnpress' ), | |
| 384 | - 'categories' => array( 'learnpress-patterns' ), | |
| 385 | - 'content' => $list_course_pattern, | |
| 386 | - ) | |
| 387 | - ); | |
| 388 | - $grid_course_pattern = file_get_contents( Template::instance( false )->get_frontend_template_type_block( 'patterns/grid-courses-pattern.html' ) ); | |
| 389 | - register_block_pattern( | |
| 390 | - 'learnpress/grid-course-pattern', | |
| 391 | - array( | |
| 392 | - 'title' => __( 'Layout Grid Course', 'learnpress' ), | |
| 393 | - 'description' => __( 'List Course Learnpress - layout grid', 'learnpress' ), | |
| 394 | - 'categories' => array( 'learnpress-patterns' ), | |
| 395 | - 'content' => $grid_course_pattern, | |
| 396 | - ) | |
| 397 | - ); | |
| 398 | - | |
| 399 | - $single_instructor_pattern = file_get_contents( Template::instance( false )->get_frontend_template_type_block( 'patterns/single-instructor-pattern.html' ) ); | |
| 400 | - register_block_pattern( | |
| 401 | - 'learnpress/single-instructor-pattern', | |
| 402 | - array( | |
| 403 | - 'title' => __( 'Single Instructor', 'learnpress' ), | |
| 404 | - 'description' => __( 'Single Instructor Learnpress', 'learnpress' ), | |
| 405 | - 'categories' => array( 'learnpress-patterns' ), | |
| 406 | - 'content' => $single_instructor_pattern, | |
| 407 | - ) | |
| 408 | - ); | |
| 409 | - } | |
| 410 | - | |
| 411 | - public function add_block_pattern_categories() { | |
| 412 | - register_block_pattern_category( | |
| 413 | - 'learnpress-patterns', | |
| 414 | - array( 'label' => __( 'LearnPress', 'learnpress' ) ) | |
| 415 | - ); | |
| 416 | - } | |
| 417 | -} | |
| 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 | +} | |