| 1 |
<?php |
| 2 |
namespace Photonic_Plugin\Admin; |
| 3 |
|
| 4 |
use Photonic_Plugin\Core\Photonic; |
| 5 |
use Photonic_Plugin\Options\Defaults; |
| 6 |
|
| 7 |
class Admin_Menu { |
| 8 |
var $file, $core; |
| 9 |
function __construct($file, $core) { |
| 10 |
$this->file = $file; |
| 11 |
$this->core = $core; |
| 12 |
|
| 13 |
$this->register_settings(); |
| 14 |
|
| 15 |
add_action('admin_enqueue_scripts', [&$this, 'enqueue']); |
| 16 |
} |
| 17 |
|
| 18 |
function settings() { |
| 19 |
require_once("Options_Manager.php"); |
| 20 |
$options_manager = new Options_Manager($this->file, $this->core); |
| 21 |
$options_manager->init(); |
| 22 |
$options_manager->render('Settings'); |
| 23 |
} |
| 24 |
|
| 25 |
function getting_started() { |
| 26 |
require_once("Getting_Started.php"); |
| 27 |
$getting_started = Getting_Started::get_instance(); |
| 28 |
$getting_started->render('Getting Started'); |
| 29 |
} |
| 30 |
|
| 31 |
function authentication() { |
| 32 |
require_once("Authentication.php"); |
| 33 |
$auth_page = Authentication::get_instance(); |
| 34 |
$auth_page->render('Authentication'); |
| 35 |
} |
| 36 |
|
| 37 |
function gutenberg() { |
| 38 |
require_once('Gutenberg.php'); |
| 39 |
$gutenberg = Gutenberg::get_instance(); |
| 40 |
$gutenberg->render('Switching to Gutenberg'); |
| 41 |
} |
| 42 |
|
| 43 |
function helpers() { |
| 44 |
require_once("Helper.php"); |
| 45 |
$helper_page = new Helper(); |
| 46 |
$helper_page->render('Helpers'); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Registers settings for the Settings API. Though this applies to the Options_Manager, the settings are required here. |
| 51 |
* The Options_Manager class is big and heavy, so we don't load it on all Photonic admin pages. |
| 52 |
* But since saving options calls WP's native options.php, when options.php is loaded without registering settings, |
| 53 |
* it triggers an "Error: options page not found" error. |
| 54 |
* |
| 55 |
* So, the definition of the settings is done here, since this file is always loaded in the admin page. |
| 56 |
*/ |
| 57 |
function register_settings() { |
| 58 |
$pages = Defaults::get_options_pages(); |
| 59 |
foreach ($pages as $page) { |
| 60 |
register_setting('photonic_options-'.$page, 'photonic_options', [&$this, 'validate_options']); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* A wrapper for the <code>validate_options</code> function in Options_Manager. This is required because of the |
| 66 |
* <code>register_settings</code> call (which is a run-time call requiring a validation function) running without initiating |
| 67 |
* the Options_Manager at compile-time. |
| 68 |
* |
| 69 |
* @param $options |
| 70 |
*/ |
| 71 |
function validate_options($options) { |
| 72 |
require_once("Options_Manager.php"); |
| 73 |
$options_manager = new Options_Manager($this->file, $this->core); |
| 74 |
return $options_manager->validate_options($options); |
| 75 |
} |
| 76 |
|
| 77 |
function enqueue($hook) { |
| 78 |
$prefix = ['toplevel_page_', 'photonic_page_']; |
| 79 |
$mod_hook = str_replace($prefix, '', $hook); |
| 80 |
if ('photonic-options-manager' == $mod_hook) { |
| 81 |
wp_enqueue_style('photonic-admin-css', PHOTONIC_URL.'include/css/admin/admin.css', ['wp-color-picker'], Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin.css')); |
| 82 |
global $photonic_options; |
| 83 |
$js_array = [ |
| 84 |
'category' => isset($photonic_options) && isset($photonic_options['last-set-section']) ? $photonic_options['last-set-section'] : 'generic-settings', |
| 85 |
]; |
| 86 |
wp_enqueue_script('photonic-options-js', PHOTONIC_URL.'include/scripts/admin/options-manager.js', ['jquery', 'wp-color-picker'], Photonic::get_version(PHOTONIC_PATH.'/include/scripts/admin/options-manager.js')); |
| 87 |
wp_localize_script('photonic-options-js', 'Photonic_Options_JS', $js_array); |
| 88 |
} |
| 89 |
else if (in_array($mod_hook, ['photonic-helpers', 'photonic-auth', 'photonic-gutenberg'])) { |
| 90 |
wp_enqueue_script('photonic-admin-js', PHOTONIC_URL.'include/scripts/admin/helpers.js', ['jquery'], Photonic::get_version(PHOTONIC_PATH.'/include/scripts/admin/helpers.js')); |
| 91 |
wp_enqueue_style('photonic-admin-css', PHOTONIC_URL.'include/css/admin/admin.css', [], Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin.css')); |
| 92 |
|
| 93 |
$js_array = [ |
| 94 |
'obtain_token' => esc_attr__('Step 2: Obtain Token', 'photonic') |
| 95 |
]; |
| 96 |
wp_localize_script('photonic-admin-js', 'Photonic_Admin_JS', $js_array); |
| 97 |
} |
| 98 |
else if (in_array($mod_hook, ['photonic-getting-started'])) { |
| 99 |
wp_enqueue_style('photonic-admin-css', PHOTONIC_URL.'include/css/admin/admin.css', [], Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin.css')); |
| 100 |
} |
| 101 |
} |
| 102 |
} |