PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3
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 / block-template / class-block-until.php

class-block-until.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.3, at inc/block-template/class-block-until.php

205 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 class LP_Block_Template_Utils {
3 protected static $instance = null;
4
5 const PLUGIN_SLUG = 'learnpress/learnpress';
6
7 public function supports_block_templates() {
8 if ( ( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) && ( ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() ) ) {
9 return false;
10 }
11
12 return true;
13 }
14
15 public function theme_has_template( $template_name ) {
16 return is_readable( get_template_directory() . '/block-templates/' . $template_name . '.html' ) || is_readable( get_stylesheet_directory() . '/block-templates/' . $template_name . '.html' );
17 }
18
19 public function create_new_block_template_object( $template_file, $template_type, $template_slug, $template_is_from_theme = false ) {
20 $theme_name = wp_get_theme()->get( 'TextDomain' );
21
22 $new_template_item = array(
23 'slug' => $template_slug,
24 'id' => $template_is_from_theme ? $theme_name . '//' . $template_slug : self::PLUGIN_SLUG . '//' . $template_slug,
25 'path' => $template_file,
26 'type' => $template_type,
27 'theme' => $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG,
28 // Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909.
29 'source' => $template_is_from_theme ? 'theme' : 'plugin',
30 'title' => $this->convert_slug_to_title( $template_slug ),
31 'description' => '',
32 'post_types' => array(), // Don't appear in any Edit Post template selector dropdown.
33 );
34
35 return (object) $new_template_item;
36 }
37
38 public function gutenberg_get_template_paths( $base_directory ) {
39 $path_list = array();
40 if ( file_exists( $base_directory ) ) {
41 $nested_files = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $base_directory ) );
42 $nested_html_files = new \RegexIterator( $nested_files, '/^.+\.html$/i', \RecursiveRegexIterator::GET_MATCH );
43 foreach ( $nested_html_files as $path => $file ) {
44 $path_list[] = $path;
45 }
46 }
47 return $path_list;
48 }
49
50 public function generate_template_slug_from_path( $path, $directory_name = 'block-templates' ) {
51 return substr( $path, strpos( $path, $directory_name . DIRECTORY_SEPARATOR ) + 1 + strlen( $directory_name ), -5 );
52 }
53
54 public function convert_slug_to_title( $template_slug ) {
55 switch ( $template_slug ) {
56 case 'single-lp_course':
57 return __( 'Single Course', 'learnpress' );
58 case 'archive-lp_course':
59 return __( 'Archive Course Page', 'learnpress' );
60 case 'taxonomy-lp_course_cat':
61 return __( 'Course Category Page', 'learnpress' );
62 case 'taxonomy-lp_course_tag':
63 return __( 'Course Tag Page', 'learnpress' );
64 default:
65 // Replace all hyphens and underscores with spaces.
66 return ucwords( preg_replace( '/[\-_]/', ' ', $template_slug ) );
67 }
68 }
69
70 public static function gutenberg_build_template_result_from_post( $post ) {
71 $terms = get_the_terms( $post, 'wp_theme' );
72
73 if ( is_wp_error( $terms ) ) {
74 return $terms;
75 }
76
77 if ( ! $terms ) {
78 return new \WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.', 'learnpress' ) );
79 }
80
81 $theme = $terms[0]->name;
82 $has_theme_file = true;
83
84 $template = new \WP_Block_Template();
85 $template->wp_id = $post->ID;
86 $template->id = $theme . '//' . $post->post_name;
87 $template->theme = $theme;
88 $template->content = $post->post_content;
89 $template->slug = $post->post_name;
90 $template->source = 'custom';
91 $template->type = $post->post_type;
92 $template->description = $post->post_excerpt;
93 $template->title = $post->post_title;
94 $template->status = $post->post_status;
95 $template->has_theme_file = $has_theme_file;
96 $template->is_custom = false;
97 $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown.
98
99 if ( 'wp_template_part' === $post->post_type ) {
100 $type_terms = get_the_terms( $post, 'wp_template_part_area' );
101 if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) {
102 $template->area = $type_terms[0]->name;
103 }
104 }
105
106 if ( self::PLUGIN_SLUG === $theme || 'learnpress' === strtolower( $theme ) ) {
107 $template->origin = 'plugin';
108 }
109
110 return $template;
111 }
112
113 public function gutenberg_build_template_result_from_file( $template_file, $template_type ) {
114 $template_file = (object) $template_file;
115
116 // If the theme has an archive-course.html template but does not have course taxonomy templates
117 // then we will load in the archive-course.html template from the theme to use for course taxonomies on the frontend.
118 $template_is_from_theme = 'theme' === $template_file->source;
119 $theme_name = wp_get_theme()->get( 'TextDomain' );
120
121 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
122 $template_content = file_get_contents( $template_file->path );
123 $template = new \WP_Block_Template();
124 $template->id = $template_is_from_theme ? $theme_name . '//' . $template_file->slug : self::PLUGIN_SLUG . '//' . $template_file->slug;
125 $template->theme = $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG;
126 $template->content = $this->gutenberg_inject_theme_attribute_in_content( $template_content );
127 // Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909.
128 $template->source = $template_file->source ? $template_file->source : 'plugin';
129 $template->slug = $template_file->slug;
130 $template->type = $template_type;
131 $template->title = ! empty( $template_file->title ) ? $template_file->title : $this->convert_slug_to_title( $template_file->slug );
132 $template->status = 'publish';
133 $template->has_theme_file = true;
134 $template->origin = $template_file->source;
135 $template->is_custom = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are.
136 $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown.
137
138 if ( 'wp_template_part' === $template_type ) {
139 $template->area = 'uncategorized';
140 }
141
142 return $template;
143 }
144
145 public function gutenberg_inject_theme_attribute_in_content( $template_content ) {
146 $has_updated_content = false;
147 $new_content = '';
148 $template_blocks = parse_blocks( $template_content );
149
150 $blocks = $this->gutenberg_flatten_blocks( $template_blocks );
151 foreach ( $blocks as &$block ) {
152 if (
153 'core/template-part' === $block['blockName'] &&
154 ! isset( $block['attrs']['theme'] )
155 ) {
156 $block['attrs']['theme'] = wp_get_theme()->get_stylesheet();
157 $has_updated_content = true;
158 }
159 }
160
161 if ( $has_updated_content ) {
162 foreach ( $template_blocks as &$block ) {
163 $new_content .= serialize_block( $block );
164 }
165
166 return $new_content;
167 }
168
169 return $template_content;
170 }
171
172 public function gutenberg_flatten_blocks( &$blocks ) {
173 $all_blocks = array();
174 $queue = array();
175 foreach ( $blocks as &$block ) {
176 $queue[] = &$block;
177 }
178 $queue_count = count( $queue );
179
180 while ( $queue_count > 0 ) {
181 $block = &$queue[0];
182 array_shift( $queue );
183 $all_blocks[] = &$block;
184
185 if ( ! empty( $block['innerBlocks'] ) ) {
186 foreach ( $block['innerBlocks'] as &$inner_block ) {
187 $queue[] = &$inner_block;
188 }
189 }
190
191 $queue_count = count( $queue );
192 }
193
194 return $all_blocks;
195 }
196
197 public static function instance() {
198 if ( ! self::$instance ) {
199 self::$instance = new self();
200 }
201
202 return self::$instance;
203 }
204 }
205