Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoggle-content.php
More file actions
Latest commit
170 lines (153 loc) · 5.8 KB
/
Copy pathtoggle-content.php
File metadata and controls
170 lines (153 loc) · 5.8 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* Plugin Name: Toggle Content
* Plugin URI: https://essential-blocks.com
* Description: Toggle Content block for Gutenberg
* Author: WPDeveloper
* Author URI: https://wpdeveloper.net
* Version: 1.3.0
* License: GPL3+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
* Text Domain: toggle-content
* Requires at least: 6.0
* Tested up to: 7.1
* Requires PHP: 7.4
*
* @package toggle-content
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Registers all block assets so that they can be enqueued through the block editor
* in the corresponding context.
*
* @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/applying-styles-with-stylesheets/
*/
define( 'TOGGLE_CONTENT_VERSION', "1.3.0" );
define( 'TOGGLE_CONTENT_ADMIN_URL', plugin_dir_url( __FILE__ ) );
define( 'TOGGLE_CONTENT_ADMIN_PATH', dirname( __FILE__ ) );
require_once__DIR__ . '/includes/font-loader.php';
require_once__DIR__ . '/includes/post-meta.php';
require_once__DIR__ . '/includes/helpers.php';
/**
* The style-handler is shipped as a git submodule. It is what turns each block's
* `blockMeta` attribute into the generated stylesheet under `uploads/eb-style/`
* and enqueues it on the frontend. Without it the editor still renders its inline
* `<style>`, but the frontend gets no generated CSS at all.
*
* Guard the include so an uninitialised submodule degrades instead of fatalling,
* but warn loudly: a silent skip looks exactly like "the block has no styles".
*/
if ( file_exists( __DIR__ . '/lib/style-handler/style-handler.php' ) ) {
require_once__DIR__ . '/lib/style-handler/style-handler.php';
} else {
add_action( 'admin_notices', 'toggle_content_missing_style_handler_notice' );
}
functioncreate_block_toggle_content_block_init() {
$script_asset_path = TOGGLE_CONTENT_ADMIN_PATH . "/dist/index.asset.php";
if ( ! file_exists( $script_asset_path ) ) {
// Missing build output must not fatal the whole site on `init`.
add_action( 'admin_notices', 'toggle_content_missing_build_notice' );
return;
}
$script_asset = require$script_asset_path;
if ( ! is_array( $script_asset ) || ! isset( $script_asset['dependencies'] ) || ! is_array( $script_asset['dependencies'] ) ) {
$script_asset = [
'dependencies' => [],
'version' => TOGGLE_CONTENT_VERSION
];
}
$all_dependencies = array_merge( $script_asset['dependencies'], [
'wp-blocks',
'wp-i18n',
'wp-element',
'wp-block-editor',
'toggle-content-controls-util',
'essential-blocks-eb-animation'
] );
$index_js = TOGGLE_CONTENT_ADMIN_URL . 'dist/index.js';
wp_register_script(
'create-block-toggle-content-block-editor',
$index_js,
$all_dependencies,
$script_asset['version'],
true
);
$load_animation_js = TOGGLE_CONTENT_ADMIN_URL . 'assets/js/eb-animation-load.js';
wp_register_script(
'essential-blocks-eb-animation',
$load_animation_js,
[],
TOGGLE_CONTENT_VERSION,
true
);
$animate_css = TOGGLE_CONTENT_ADMIN_URL . 'assets/css/animate.min.css';
wp_register_style(
'essential-blocks-animation',
$animate_css,
[],
TOGGLE_CONTENT_VERSION
);
$style_css = TOGGLE_CONTENT_ADMIN_URL . 'dist/style.css';
wp_register_style(
'create-block-toggle-content-block-editor-css',
$style_css,
[ 'essential-blocks-animation' ],
TOGGLE_CONTENT_VERSION,
"all"
);
$frontend_js = TOGGLE_CONTENT_ADMIN_URL . 'dist/frontend/index.js';
wp_register_script(
'essential-blocks-toggle-content-frontend',
$frontend_js,
[ 'essential-blocks-eb-animation' ],
TOGGLE_CONTENT_VERSION,
true
);
if ( ! WP_Block_Type_Registry::get_instance()->is_registered( 'essential-blocks/toggle-content' ) ) {
register_block_type(
Toggle_Content_Helper::get_block_register_path( "toggle-content/toggle-content", TOGGLE_CONTENT_ADMIN_PATH ),
[
'editor_script' => 'create-block-toggle-content-block-editor',
'editor_style' => 'create-block-toggle-content-block-editor-css',
'render_callback' => function ( $attributes, $content ) {
if ( ! is_admin() ) {
wp_enqueue_script( 'essential-blocks-toggle-content-frontend' );
wp_enqueue_style( 'create-block-toggle-content-block-editor-css' );
}
return$content;
}
]
);
}
}
add_action( 'init', 'create_block_toggle_content_block_init', 99 );
/**
* Admin notice shown when the block build output is missing.
*/
functiontoggle_content_missing_build_notice() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
printf(
'<div class="notice notice-error"><p>%s</p></div>',
esc_html__( 'Toggle Content: build output is missing. Run `npm install && npm run build` in the plugin directory.', 'toggle-content' )
);
}
/**
* Admin notice shown when the style-handler submodule is missing.
*
* Without it no generated CSS is written or enqueued, so blocks render unstyled
* on the frontend while still looking correct in the editor.
*/
functiontoggle_content_missing_style_handler_notice() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
printf(
'<div class="notice notice-error"><p>%s</p></div>',
esc_html__( 'Toggle Content: the style-handler library is missing, so block styles will not be applied on the frontend. Run `git submodule update --init --recursive` in the plugin directory, or reinstall the plugin from a complete package.', 'toggle-content' )
);
}