1, 'max' => $count, 'default' => $count]); } /** * Enqueue CSS and javascript files. */ public static function enqueueAssets(string $hook): void { if ($hook !== 'index.php') { return; // for dashboard only } // plugin assets $pluginAssetsURL = plugin_dir_url(TEKOD_ADWMAN_DIR . '/.') . 'assets'; $pluginAssetsDir = TEKOD_ADWMAN_DIR . '/assets'; $slug = 'adwman'; wp_enqueue_style("$slug-Style", "$pluginAssetsURL/$slug.css", [], filemtime("$pluginAssetsDir/$slug.css")); wp_enqueue_script("$slug-Script", "$pluginAssetsURL/$slug.js", ['jquery'], filemtime("$pluginAssetsDir/$slug.js"), ['in_footer' => true]); // jquery and select2 wp_enqueue_script('jquery-ui-dialog'); wp_enqueue_style('wp-jquery-ui-dialog'); wp_enqueue_script("$slug-select2-js", "$pluginAssetsURL/select2.min.js", ['jquery'], '4.1.0', ['in_footer' => true]); wp_enqueue_style("$slug-select2-css", "$pluginAssetsURL/select2.min.css", [], '4.1.0'); // js template add_action('admin_footer', [__CLASS__, 'renderCtrlTemplate']); } /** * Echo content required for javascript. */ public static function renderCtrlTemplate(): void { $config = Store::getConfig(); $adwmanData = [ 'ajax' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('tekod-adwman-nonce'), 'config' => [ 'view' => $config['view'] ?? 'dropdown', ], 'dashboards' => Store::getListOfDashboards(), ]; $widgetGroups = []; foreach (Widgets::getAllWidgets() as $widget) { $widgetGroups[$widget['group']][] = $widget; } ?>
id !== 'dashboard') { return; } // phpcs:ignore WordPress.Security.NonceVerification -- nonce not needed here, it is retrieving content $selectedDashboard = sanitize_text_field(wp_unslash($_POST['dashboard'] ?? null ?: 'Dashboard')); $inUse = Store::getDashboardByName($selectedDashboard); $inUse = array_filter(explode(',', implode(',', $inUse))); foreach (Widgets::getAllWidgets() as $widget) { if (in_array($widget['id'], $inUse, true)) { wp_add_dashboard_widget($widget['id'], $widget['title'], $widget['callback'], null, $widget['args']); } } // remove unused widgets foreach ($wp_meta_boxes['dashboard'] as $columnName => $columnArray) { foreach ($columnArray as $priority => $widgets) { foreach ($widgets as $widgetId => $widget) { if (!in_array($widgetId, $inUse, true)) { unset($wp_meta_boxes['dashboard'][$columnName][$priority][$widgetId]); } } } } } /** * Return flat list of all widgets in native dashboard. * * @return array */ public static function getCurrentUserWidgetsList(): array { static $cached = null; if ($cached === null) { $meta = get_user_meta(wp_get_current_user()->ID, 'meta-box-order_dashboard', true); $cached = array_filter(explode(',', $meta ? implode(',', $meta) : '')); } return $cached; } /** * Collect debug data. */ public function prepareDebugTable(): void { global $wp_meta_boxes; if (empty($wp_meta_boxes['dashboard'] ?? [])) { return; } self::$debugTable = []; foreach ($wp_meta_boxes['dashboard'] as $context) { foreach ($context as $priority) { foreach ($priority as $widgetId => $widget) { self::$debugTable[] = self::prepareDebugRow($widgetId, $widget); } } } } protected static function prepareDebugRow($widgetId, $widget): array { $callback = $widget['callback']; $file = 'unknown'; try { if (is_array($callback)) { $ref = new \ReflectionMethod($callback[0], $callback[1]); $file = $ref->getFileName(); } elseif (is_string($callback)) { $ref = new \ReflectionFunction($callback); $file = $ref->getFileName(); } } catch (\Throwable $err) { // silence $err = null; } $plugin = 'core'; if ($file) { $pluginsDir = WP_PLUGIN_DIR; if (strpos($file, $pluginsDir) === 0) { $relative = str_replace($pluginsDir . '/', '', $file); $plugin = explode('/', $relative)[0]; } } return [$widgetId, $plugin, $file]; } /** * Render debug data into page footer. */ public static function displayDebugTable(): void { $screen = get_current_screen(); if (!$screen || $screen->id !== 'dashboard') { return; } echo "\n\n"; if (self::$debugTable === null) { echo "\n"; } foreach (self::$debugTable as $row) { echo "\n"; } } }