-
Notifications
You must be signed in to change notification settings - Fork 2
/
better-custom-classes-for-gutenberg.php
139 lines (119 loc) · 3.95 KB
/
better-custom-classes-for-gutenberg.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* @version 0.2.2
*/
/*
Plugin Name: Better Custom Classes for Gutenberg
Description: A better interface for applying custom classes to blocks in the gutenberg editor.
Author: Anton Plauche
Version: 0.2.2
Author URI: https://antonplauche.com
*/
if (!defined('ABSPATH')) exit; // Exit if accessed directly
class BetterCustomClassesForGutenberg
{
/**
* Constructor function
* - adds asset info from build directory
* - hooks into block editor asset enqueue
*/
function __construct()
{
$this->assetInfo = include(plugin_dir_path(__FILE__) . 'build/index.asset.php');
$this->adminAssetInfo = include(plugin_dir_path(__FILE__) . 'build/admin.asset.php');
// Enqueue Scripts for editor and admin
add_action('admin_enqueue_scripts', array($this, 'adminAssets'));
add_action('enqueue_block_editor_assets', array($this, 'editorAssets'));
// Register the class library menu page
add_action('admin_menu', array($this, 'menu_page'));
// Activation script to register option
add_action('activate_plugin', array($this, 'on_activate'));
// Add our setting to the REST API
add_action('admin_init', array($this, 'add_settings'));
add_action('rest_api_init', array($this, 'add_settings'));
}
// Utility for sanitizing an array of class attributes
function sanitize_array_of_classes(array $array)
{
return array_map('sanitize_html_class', $array);
}
/**
* Enqueue JS for admin page class library
*/
function adminAssets($hook)
{
//var_dump($hook);
// Load only on /tools.php?page=bccfg-class-library-settings.
if ( 'tools_page_bccfg-class-library-settings' !== $hook ) {
return;
}
wp_enqueue_script('custom-class-library-script', plugin_dir_url(__FILE__) . 'build/admin.js', $this->adminAssetInfo['dependencies'], $this->adminAssetInfo['version'], true);
wp_enqueue_style('custom-class-library-styles', plugin_dir_url(__FILE__) . 'build/admin.css', array(), $this->adminAssetInfo['version']);
}
/**
* Enqueue JS for editor
* Enqueue custom styles within block editor for added settings pane
*/
function editorAssets($hook)
{
wp_enqueue_script('custom-class-filters', plugin_dir_url(__FILE__) . 'build/index.js', $this->assetInfo['dependencies'], $this->assetInfo['version']);
wp_enqueue_style('custom-class-styles', plugin_dir_url(__FILE__) . 'build/index.css', array(), $this->assetInfo['version']);
}
function on_activate()
{
// OPTIONS SETUP
// first check if options are already there
$options = get_option('bccfg_class_library');
// on first activation will return false
if (!$options) {
add_option('bccfg_class_library', array());
} else if (!is_array($options)) {
delete_option('bccfg_class_library');
add_option('bccfg_class_library', array());
}
}
function menu_page()
{
add_submenu_page(
'tools.php',
'Class Library',
'Class Library',
'manage_options',
'bccfg-class-library-settings',
array($this, 'render_menu_page')
);
}
function render_menu_page(){
?>
<div class="wrap">
<h1 style="margin-bottom: 16px;">Class Library</h1>
<div class="bccfg-intro">
<h2>Manage your class library</h2>
<p>Classes added here will populate your auto-complete field globally within the block editor.</p>
</div>
<div id="bccfg-library-app"></div>
</div>
<?php
}
function add_settings()
{
/**
* Registers a setting for Wordpress 4.7 and higher.
**/
$args = array(
'type' => 'array',
'sanitize_callback' => [$this, 'sanitize_array_of_classes'],
'show_in_rest' => array(
'schema' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
)
);
register_setting('bccfg-class-library-settings', 'bccfg_class_library', $args);
}
}
// Entry Point
new BetterCustomClassesForGutenberg();