| 1 |
<?php |
| 2 |
/** |
| 3 |
* Abstract base class for all WPFunnels Divi 5 modules. |
| 4 |
* |
| 5 |
* Every Divi 5 module must implement DependencyInterface so Divi's dependency |
| 6 |
* manager can call load() when it builds the module library tree. |
| 7 |
* |
| 8 |
* @package WPFunnels\Widgets\DiviModules\D5 |
| 9 |
* @since 2.9.1 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace WPFunnels\Widgets\DiviModules\D5; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
die( 'Direct access forbidden.' ); |
| 16 |
} |
| 17 |
|
| 18 |
use ET\Builder\Framework\DependencyManagement\Interfaces\DependencyInterface; |
| 19 |
use ET\Builder\Packages\ModuleLibrary\ModuleRegistration; |
| 20 |
|
| 21 |
abstract class BaseModule implements DependencyInterface { |
| 22 |
|
| 23 |
/** Tracks registered modules to prevent duplicate registration. */ |
| 24 |
private static array $registered_modules = []; |
| 25 |
|
| 26 |
/** Module name, e.g. 'Checkout'. Used as class name and directory name. */ |
| 27 |
abstract protected function get_module_name(): string; |
| 28 |
|
| 29 |
/** Fully-qualified namespace of the concrete module class. */ |
| 30 |
abstract protected function get_module_namespace(): string; |
| 31 |
|
| 32 |
/** Directory name inside divi-5/modules/, e.g. 'Checkout'. */ |
| 33 |
abstract protected function get_module_dir(): string; |
| 34 |
|
| 35 |
/** |
| 36 |
* Trait files to load from {ModuleDir}/{ModuleName}Trait/. |
| 37 |
* Override to add or remove traits. |
| 38 |
*/ |
| 39 |
protected function get_trait_files(): array { |
| 40 |
return [ |
| 41 |
'RenderCallbackTrait.php', |
| 42 |
'ModuleClassnamesTrait.php', |
| 43 |
'ModuleStylesTrait.php', |
| 44 |
'ModuleScriptDataTrait.php', |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
/** Required by DependencyInterface — modules have no external deps. */ |
| 49 |
public function getDependencies(): array { |
| 50 |
return []; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Load trait files for this module. |
| 55 |
*/ |
| 56 |
protected function load_traits(): void { |
| 57 |
$module_dir = __DIR__ . '/' . $this->get_module_dir(); |
| 58 |
$trait_dir = $module_dir . '/' . $this->get_module_name() . 'Trait/'; |
| 59 |
|
| 60 |
foreach ( $this->get_trait_files() as $file ) { |
| 61 |
$path = $trait_dir . $file; |
| 62 |
if ( file_exists( $path ) ) { |
| 63 |
require_once $path; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Called by Divi 5's dependency manager (or our fallback). |
| 70 |
* Validates prerequisites, loads traits and the main class file, |
| 71 |
* then registers the module with Divi 5. |
| 72 |
*/ |
| 73 |
public function load(): void { |
| 74 |
$module_dir = __DIR__ . '/' . $this->get_module_dir(); |
| 75 |
$json_folder = $module_dir . '/module-json'; |
| 76 |
$json_file = $json_folder . '/module.json'; |
| 77 |
|
| 78 |
if ( ! is_dir( $json_folder ) || ! file_exists( $json_file ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$id = $this->get_module_namespace() . '\\' . $this->get_module_name(); |
| 83 |
if ( isset( self::$registered_modules[ $id ] ) ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
if ( ! class_exists( 'ET\Builder\Packages\ModuleLibrary\ModuleRegistration' ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$class = $this->get_module_namespace() . '\\' . $this->get_module_name(); |
| 92 |
|
| 93 |
// Load traits first, then the main class file (traits are inlined into the class) |
| 94 |
$this->load_traits(); |
| 95 |
|
| 96 |
$class_file = $module_dir . '/' . $this->get_module_name() . '.php'; |
| 97 |
if ( file_exists( $class_file ) ) { |
| 98 |
require_once $class_file; |
| 99 |
} |
| 100 |
|
| 101 |
if ( ! method_exists( $class, 'render_callback' ) ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
try { |
| 106 |
ModuleRegistration::register_module( |
| 107 |
$json_folder, |
| 108 |
[ 'render_callback' => [ $class, 'render_callback' ] ] |
| 109 |
); |
| 110 |
self::$registered_modules[ $id ] = true; |
| 111 |
} catch ( \Exception $e ) { |
| 112 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 113 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 114 |
error_log( 'WPFunnels Divi5: Module registration failed for ' . $id . ': ' . $e->getMessage() ); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|