| @@ -1,0 +1,142 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.dashboard | |
| 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 | +JLoader::import('adapter.dashboard.widget'); | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * Helper class used to manage the widgets that can be included and | |
| 18 | + * configured within the admin dashboard of WordPress. | |
| 19 | + * | |
| 20 | + * @since 10.1.31 | |
| 21 | + * @link https://developer.wordpress.org/apis/handbook/dashboard-widgets/ | |
| 22 | + */ | |
| 23 | +class JDashboardAdmin | |
| 24 | +{ | |
| 25 | + /** | |
| 26 | + * Loads all the widgets under the specified folder/file. | |
| 27 | + * | |
| 28 | + * @param mixed $folder Either a file or a folder that contains the widgets. | |
| 29 | + * @param string $prefix The prefix of the classname. If not specified | |
| 30 | + * `JDashboardWidget` is assumed. | |
| 31 | + * | |
| 32 | + * @return void | |
| 33 | + */ | |
| 34 | + public static function load($folder, $prefix = null) | |
| 35 | + { | |
| 36 | + if (is_null($prefix)) | |
| 37 | + { | |
| 38 | + // use default prefix | |
| 39 | + $prefix = 'JDashboardWidget'; | |
| 40 | + } | |
| 41 | + | |
| 42 | + // check if we have a directory or a file | |
| 43 | + if (is_dir($folder)) | |
| 44 | + { | |
| 45 | + JLoader::import('adapter.filesystem.folder'); | |
| 46 | + | |
| 47 | + // load list of files contained within the specified folder | |
| 48 | + $folder = JFolder::files($folder, '\.php$', $recursive = false, $full = true); | |
| 49 | + } | |
| 50 | + else | |
| 51 | + { | |
| 52 | + // cast file to array | |
| 53 | + $folder = (array) $folder; | |
| 54 | + | |
| 55 | + // iterate specified files | |
| 56 | + foreach ($folder as $file) | |
| 57 | + { | |
| 58 | + // make sure the file exists and is a PHP file | |
| 59 | + if (!is_file($file) || !preg_match("/\.php$/", $file)) | |
| 60 | + { | |
| 61 | + // file not found, raise error | |
| 62 | + throw new RuntimeException(sprintf('Widget file [%s] not found', $file), 404); | |
| 63 | + } | |
| 64 | + } | |
| 65 | + } | |
| 66 | + | |
| 67 | + // at this point, all the specified files exist | |
| 68 | + foreach ($folder as $file) | |
| 69 | + { | |
| 70 | + // load the file | |
| 71 | + require_once $file; | |
| 72 | + | |
| 73 | + // extract file name from path | |
| 74 | + $filename = preg_replace("/\.php$/", '', basename($file)); | |
| 75 | + // remove any unexpected character and make the next character uppercase | |
| 76 | + $filename = preg_replace_callback("/[^a-zA-Z0-9]+([a-zA-Z0-9])/", function($match) | |
| 77 | + { | |
| 78 | + return strtoupper(end($match)); | |
| 79 | + }, $filename); | |
| 80 | + | |
| 81 | + // prepare widget class name | |
| 82 | + $classname = $prefix . ucfirst($filename); | |
| 83 | + | |
| 84 | + // make sure the class exists | |
| 85 | + if (!class_exists($classname)) | |
| 86 | + { | |
| 87 | + // missing class, raise error | |
| 88 | + throw new RuntimeException(sprintf('Class [%s] not found', $classname), 404); | |
| 89 | + } | |
| 90 | + | |
| 91 | + // instantiate class | |
| 92 | + $widget = new $classname(); | |
| 93 | + | |
| 94 | + // make sure the class is a valid instance | |
| 95 | + if (!$widget instanceof JDashboardWidget) | |
| 96 | + { | |
| 97 | + // the widget doesn't inherit the correct class, raise error | |
| 98 | + throw new RuntimeException(sprintf('Class [%s] is not a valid instance', $classname), 500); | |
| 99 | + } | |
| 100 | + | |
| 101 | + // register class within the WordPress dashboard | |
| 102 | + static::register($widget); | |
| 103 | + } | |
| 104 | + } | |
| 105 | + | |
| 106 | + /** | |
| 107 | + * Registers a widget within the dashboard. | |
| 108 | + * | |
| 109 | + * @param JDashboardWidget $widget The instance of the widget to register. | |
| 110 | + * | |
| 111 | + * @return void | |
| 112 | + */ | |
| 113 | + public static function register(JDashboardWidget $widget) | |
| 114 | + { | |
| 115 | + if (!$widget->canAccess()) | |
| 116 | + { | |
| 117 | + // do not display widget in case the user is not authorized | |
| 118 | + return false; | |
| 119 | + } | |
| 120 | + | |
| 121 | + $args = array(); | |
| 122 | + | |
| 123 | + // set widget ID as first argument | |
| 124 | + $args[] = $widget->getID(); | |
| 125 | + // set widget name as second argument | |
| 126 | + $args[] = $widget->getName(); | |
| 127 | + // set callback to display the widget contents | |
| 128 | + $args[] = array($widget, 'getHtml'); | |
| 129 | + | |
| 130 | + // check if the widget supports a configuration | |
| 131 | + if ($widget->getForm()) | |
| 132 | + { | |
| 133 | + // set callback to display the configuration form | |
| 134 | + $args[] = array($widget, 'config'); | |
| 135 | + // set configuration settings as last argument | |
| 136 | + $args[] = $widget->getConfig(); | |
| 137 | + } | |
| 138 | + | |
| 139 | + // call function to register the widget | |
| 140 | + call_user_func_array('wp_add_dashboard_widget', $args); | |
| 141 | + } | |
| 142 | +} | |