* @copyright Copyright (c) 2020 Samuel Marshall / JCH Optimize
* @license GNU/GPLv3, or later. See LICENSE file
*
* If LICENSE file missing, see
{$message['message']}
HTML; } delete_transient('jch-optimize_notices'); } } catch (Exception $e) { } } public function addAdminMenu(): void { $menuTitle = JCH_PRO ? 'JCH Optimize Pro' : 'JCH Optimize'; $this->hookSuffix = (string)add_options_page( __('JCH Optimize Settings', 'jch-optimize'), $menuTitle, 'manage_options', 'jch_optimize', [$this, 'loadAdminPage'] ); add_action('admin_enqueue_scripts', [$this, 'loadResourceFiles']); if ($this->hookSuffix !== '') { add_action("load-{$this->hookSuffix}", [$this, 'initializeSettings']); } add_action('admin_init', [$this, 'checkMessages']); } public function addPageCacheMenu(): void { if (current_user_can('manage_options')) { return; } $this->hookSuffix = (string)add_submenu_page( 'tools.php', // parent __('JCH Optimize Page Cache', 'jch-optimize'), // page title __('Page Cache', 'jch-optimize'), // menu title 'jch_clear_page_cache', // capability 'jch_optimize_page_cache', // slug [$this, 'renderPageCacheOnly'] ); if ($this->hookSuffix !== '') { add_action("load-{$this->hookSuffix}", [$this, 'initializeSettings']); add_action("load-{$this->hookSuffix}", [ $this->getContainer()->get(PageCache::class), 'enqueueAssets' ]); } // Show a card on Tools → Available Tools add_action('tool_box', function () { if (!current_user_can('jch_clear_page_cache')) { return; } $url = menu_page_url('jch_optimize_page_cache', false); ?> resolvedController?->execute() ?? $this->controllerResolver->resolve(); } catch (Exception $e) { $class = get_class($e); echo <<Application ErrorPlease submit the following error message and trace in a support request:
{$e->getTraceAsString()}
HTML;
}
}
public function renderPageCacheOnly(): void
{
$this->getContainer()->get(PageCache::class)
->setRedirect('tools.php?page=jch_optimize_page_cache')
->setLayout('page_cache_only.php')
->execute();
}
public function registerOptions(): void
{
//Buffer output to allow for redirection
ob_start();
register_setting('jchOptimizeOptionsPage', 'jch-optimize_settings', ['type' => 'array']);
}
/**
* @param string $hookSuffix The current admin page
*
* @return void
*/
public function loadResourceFiles(string $hookSuffix): void
{
if ($this->hookSuffix !== $hookSuffix) {
return;
}
$this->enqueueGlobalAssets();
if ($this->resolvedController instanceof WillEnqueueAssets) {
$this->resolvedController->enqueueAssets();
}
}
private function enqueueGlobalAssets(): void
{
wp_enqueue_style('jch-bootstrap');
wp_enqueue_style('jch-admin');
wp_enqueue_style('jch-fonts');
//wp_enqueue_style('jch-choices-base');
wp_enqueue_style('jch-dashicons-wordpress');
wp_enqueue_style('jch-dashicons');
wp_enqueue_style('jch-wordpress');
wp_enqueue_script('jch-platform-wordpress');
wp_enqueue_script('jch-bootstrap');
wp_enqueue_script('jch-admin-utility');
wp_enqueue_script('jch-collapsible-js');
}
public function initializeSettings(): void
{
//Css files
wp_register_style(
'jch-bootstrap',
JCH_PLUGIN_URL . 'media/bootstrap/css/bootstrap.min.css',
[],
JCH_VERSION
);
wp_register_style('jch-admin', JCH_PLUGIN_URL . 'media/core/css/admin.css', [], JCH_VERSION);
wp_register_style('jch-fonts', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css');
//wp_register_style('jch-choices-base', JCH_PLUGIN_URL . 'media/choices.js/styles/base.css', [], JCH_VERSION);
wp_register_style('jch-wordpress', JCH_PLUGIN_URL . 'media/css/wordpress.css', ['jch-dashicons'], JCH_VERSION);
wp_register_style(
'jch-dashicons-wordpress',
JCH_PLUGIN_URL . 'media/css/dashicons-wordpress.css',
[],
JCH_VERSION
);
wp_register_style(
'jch-dashicons',
JCH_PLUGIN_URL . 'media/core/css/dashicons.css',
['jch-dashicons-wordpress'],
JCH_VERSION
);
//JavaScript files
wp_register_script(
'jch-bootstrap',
JCH_PLUGIN_URL . 'media/bootstrap/js/bootstrap.bundle.min.js',
['jquery'],
JCH_VERSION,
true
);
wp_register_script(
'jch-platform-wordpress',
JCH_PLUGIN_URL . 'media/js/platform-wordpress.js',
['jquery'],
JCH_VERSION,
true
);
wp_register_script(
'jch-admin-utility',
JCH_PLUGIN_URL . 'media/core/js/admin-utility.js',
['jquery'],
JCH_VERSION,
true
);
$this->resolvedController = $this->controllerResolver->getController();
$loader_image = $this->paths->mediaUrl() . '/core/images/loader.gif';
$multiselect_nonce = wp_create_nonce('jch_optimize_multiselect');
$optimize_image_nonce = wp_create_nonce('jch_optimize_image');
$imageLoaderJs = <<