| @@ -1,0 +1,110 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.module | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Factory class to handle the internal modules. | |
| 16 | + * | |
| 17 | + * @since 10.0 | |
| 18 | + */ | |
| 19 | +class JModuleFactory | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * An array containing the list of registered modules. | |
| 23 | + * | |
| 24 | + * @var array | |
| 25 | + */ | |
| 26 | + protected static $registered = array(); | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Loads all the modules contained in the specified path. | |
| 30 | + * | |
| 31 | + * @param string $path The modules path. | |
| 32 | + * | |
| 33 | + * @return boolean True on success, otherwise false. | |
| 34 | + * | |
| 35 | + * @uses loadModule() | |
| 36 | + */ | |
| 37 | + public static function load($path) | |
| 38 | + { | |
| 39 | + // get all the modules (folders) | |
| 40 | + $folders = glob($path . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR); | |
| 41 | + | |
| 42 | + // do not proceed if the folder is empty | |
| 43 | + if (!count($folders)) | |
| 44 | + { | |
| 45 | + return false; | |
| 46 | + } | |
| 47 | + | |
| 48 | + $loaded = false; | |
| 49 | + | |
| 50 | + // iterate the folders | |
| 51 | + foreach ($folders as $folder) | |
| 52 | + { | |
| 53 | + // load the module and update (on success only) the result | |
| 54 | + $loaded = self::loadModule($folder) || $loaded; | |
| 55 | + } | |
| 56 | + | |
| 57 | + return $loaded; | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * Loads the module contained in the given path. | |
| 62 | + * The folder must contain at least 2 files: | |
| 63 | + * - widget.php to extend the module functions. | |
| 64 | + * - [mod_name].php to render the module layout. | |
| 65 | + * | |
| 66 | + * @param string $path The module path. | |
| 67 | + * | |
| 68 | + * @return boolean True if loaded, otherwise false. | |
| 69 | + */ | |
| 70 | + public static function loadModule($path) | |
| 71 | + { | |
| 72 | + // make sure the module hasn't been yet registered | |
| 73 | + if (isset(static::$registered[$path])) | |
| 74 | + { | |
| 75 | + return static::$registered[$path]; | |
| 76 | + } | |
| 77 | + | |
| 78 | + // before all, flag the module as not loaded | |
| 79 | + static::$registered[$path] = 0; | |
| 80 | + | |
| 81 | + // check if the widget file exists | |
| 82 | + $widget = $path . DIRECTORY_SEPARATOR . 'widget.php'; | |
| 83 | + | |
| 84 | + if (!is_file($widget)) | |
| 85 | + { | |
| 86 | + return false; | |
| 87 | + } | |
| 88 | + | |
| 89 | + include $widget; | |
| 90 | + | |
| 91 | + // build widget classname | |
| 92 | + $classname = basename($path); | |
| 93 | + $classname = str_replace('_', ' ', $classname); | |
| 94 | + $classname = preg_replace("/\s+/", '', ucwords($classname)) . '_Widget'; | |
| 95 | + | |
| 96 | + // make sure the widget handler exists | |
| 97 | + if (!class_exists($classname)) | |
| 98 | + { | |
| 99 | + return false; | |
| 100 | + } | |
| 101 | + | |
| 102 | + // register the widget in WP pool | |
| 103 | + register_widget($classname); | |
| 104 | + | |
| 105 | + // mark the module as loaded | |
| 106 | + static::$registered[$path] = 1; | |
| 107 | + | |
| 108 | + return true; | |
| 109 | + } | |
| 110 | +} | |