PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.0
YayMail – WooCommerce Email Customizer v4.4.0
4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 3.4.2 3.4.3 All 55 releases
yaymail / src / TemplateLibrary / TemplateLibraryLoader.php

TemplateLibraryLoader.php in YayMail – WooCommerce Email Customizer 4.4.0, at src/TemplateLibrary/TemplateLibraryLoader.php

60 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\TemplateLibrary;
4
5 use YayMail\Abstracts\BaseTemplate;
6 use YayMail\Utils\SingletonTrait;
7
8 /**
9 * Loader that auto-discovers and registers Template Library templates
10 * from the Templates directory. Add new template classes under
11 * Templates/{EmailType}/{TemplateName}.php and they will be registered automatically.
12 *
13 * @method static TemplateLibraryLoader get_instance()
14 */
15 class TemplateLibraryLoader {
16 use SingletonTrait;
17
18 private function __construct() {
19 $this->load_templates_from_directory();
20 }
21
22 /**
23 * Recursively scan a directory for PHP files and register template classes.
24 *
25 * @return void
26 */
27 protected function load_templates_from_directory() {
28 $template_library_service = TemplateLibraryService::get_instance();
29 $templates_base_path = YAYMAIL_PLUGIN_PATH . '/src/TemplateLibrary/Templates';
30 $templates_base_namespace = 'YayMail\\TemplateLibrary\\Templates';
31
32 if ( ! is_dir( $templates_base_path ) ) {
33 return;
34 }
35
36 $iterator = new \RecursiveIteratorIterator(
37 new \RecursiveDirectoryIterator( $templates_base_path, \RecursiveDirectoryIterator::SKIP_DOTS ),
38 \RecursiveIteratorIterator::SELF_FIRST
39 );
40
41 foreach ( $iterator as $fileinfo ) {
42 if ( ! $fileinfo->isFile() || $fileinfo->getExtension() !== 'php' ) {
43 continue;
44 }
45
46 $relative_path = substr( $fileinfo->getPathname(), strlen( $templates_base_path ) + 1 );
47 $relative_path = str_replace( '\\', '/', $relative_path );
48 $class_name = str_replace( '/', '\\', pathinfo( $relative_path, PATHINFO_DIRNAME ) . '/' . pathinfo( $relative_path, PATHINFO_FILENAME ) );
49 $class_name = trim( $class_name, '\\' );
50 $full_class = $templates_base_namespace . '\\' . $class_name;
51
52 if ( class_exists( $full_class ) && is_subclass_of( $full_class, BaseTemplate::class, true ) ) {
53 $template_library_service->register( $full_class::get_instance() );
54 }
55 }
56
57 do_action( 'yaymail_register_template_library', $template_library_service );
58 }
59 }
60