| 1 |
<?php |
| 2 |
|
| 3 |
namespace PXLBSAdminify\Inc\Classes; |
| 4 |
|
| 5 |
use PXLBSAdminify\Inc\Utils; |
| 6 |
use PXLBSAdminify\Inc\Admin\AdminSettings; |
| 7 |
|
| 8 |
// no direct access allowed |
| 9 |
if (!defined('ABSPATH')) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Assets for the shared WP Adminify icon picker outside the Menu Editor. |
| 15 |
* |
| 16 |
* The Menu Editor loads the picker itself (Inc/Modules/MenuEditor/MenuEditorAssets.php). |
| 17 |
* This class covers the other screens that render the `adminify_icon` options-framework |
| 18 |
* field (Inc/Fields/adminify_icon.php): the Dashboard Widget settings page and the Pro |
| 19 |
* Admin Pages editor. |
| 20 |
* |
| 21 |
* @package PXLBSAdminify |
| 22 |
*/ |
| 23 |
class IconPicker_Assets |
| 24 |
{ |
| 25 |
|
| 26 |
/** |
| 27 |
* Screens that render an `adminify_icon` field. |
| 28 |
* |
| 29 |
* Kept in one place so it stays in step with the two field declarations in |
| 30 |
* DashboardWidget_Setttings and AdminPages_MetaBoxes. |
| 31 |
*/ |
| 32 |
const OPTIONS_PAGE_SLUG = 'adminify-dashboard-widgets'; |
| 33 |
const POST_TYPE = 'adminify_admin_page'; |
| 34 |
|
| 35 |
public function __construct() |
| 36 |
{ |
| 37 |
// Priority 110: Assets::pxlbsadminify_admin_scripts() registers the |
| 38 |
// adminify-icon-picker handles at 100, so they exist by the time we enqueue. |
| 39 |
add_action('admin_enqueue_scripts', [$this, 'enqueue'], 110); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Whether the current screen renders an `adminify_icon` field. |
| 44 |
* |
| 45 |
* @param \WP_Screen|null $screen Current screen. |
| 46 |
* @return bool |
| 47 |
*/ |
| 48 |
private function is_icon_field_screen($screen) |
| 49 |
{ |
| 50 |
if (empty($screen->id)) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
// Framework options pages are suffixed with their menu slug. |
| 55 |
if (self::OPTIONS_PAGE_SLUG === substr($screen->id, -strlen(self::OPTIONS_PAGE_SLUG))) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
// Pro admin page editor (metabox lives on the post edit screen only). |
| 60 |
if ('post' === $screen->base && self::POST_TYPE === $screen->post_type) { |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Enqueue the picker and hand it the settings it needs. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function enqueue() |
| 73 |
{ |
| 74 |
$screen = get_current_screen(); |
| 75 |
|
| 76 |
if (!$this->is_icon_field_screen($screen)) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
// The Menu Editor already loads and localises the picker on its own screen; |
| 81 |
// enqueueing twice is a no-op, but localising twice would overwrite. |
| 82 |
wp_enqueue_style('dashicons'); |
| 83 |
wp_enqueue_style('adminify-icon-picker'); |
| 84 |
wp_enqueue_style('adminify-simple-line-icons'); |
| 85 |
wp_enqueue_script('adminify-icon-picker'); |
| 86 |
|
| 87 |
// Loaded here rather than in the constructor so unrelated admin pages |
| 88 |
// don't pay for it. |
| 89 |
$options = (array) AdminSettings::get_instance()->get(); |
| 90 |
|
| 91 |
wp_localize_script( |
| 92 |
'adminify-icon-picker', |
| 93 |
'PXLBSADMINIFY_ICON_PICKER', |
| 94 |
[ |
| 95 |
'is_elementor_active' => Utils::is_plugin_active('elementor/elementor.php'), |
| 96 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 97 |
// Same nonce the Menu Editor issues: the custom-icon list and upload |
| 98 |
// endpoints (MenuEditor::load_custom_icons_callback / file_upload_callback) |
| 99 |
// verify against this action. |
| 100 |
'security' => wp_create_nonce('pxlbsadminify-menu-editor-security-nonce'), |
| 101 |
'max_upload_size' => size_format(wp_max_upload_size()), |
| 102 |
// Uploaded icons land in the uploads folder; the picker builds their |
| 103 |
// URL from this base after an upload finishes. |
| 104 |
'baseurl' => wp_upload_dir()['baseurl'], |
| 105 |
'icon_picker_logo' => PXLBSADMINIFY_ASSETS_IMAGE . 'logos/menu-icon.svg', |
| 106 |
// Libraries whose stylesheet the user switched off in the Assets |
| 107 |
// Manager are dropped from the picker, as the Menu Editor does. |
| 108 |
'assets_manager' => !empty($options['adminify_assets']) ? $options['adminify_assets'] : [], |
| 109 |
] |
| 110 |
); |
| 111 |
|
| 112 |
// Boots the picker and hands it the shared icon libraries. The picker only |
| 113 |
// ships Dashicons of its own; Simple Line Icons come from |
| 114 |
// dev/shared/icon-libraries.js, the same list the Menu Editor uses. |
| 115 |
wp_enqueue_script( |
| 116 |
'adminify-icon-picker-init', |
| 117 |
PXLBSADMINIFY_ASSETS . 'admin/js/icon-picker-init' . Utils::assets_ext('.js'), |
| 118 |
['jquery', 'adminify-icon-picker'], |
| 119 |
PXLBSADMINIFY_VER, |
| 120 |
true |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
|