PluginProbe
Toggle Content / trunk
Toggle Content vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.8 1.3.0
toggle-content / toggle-content.php

toggle-content.php in Toggle Content trunk, at toggle-content.php

171 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Name: Toggle Content
5 * Plugin URI: https://essential-blocks.com
6 * Description: Toggle Content block for Gutenberg
7 * Author: WPDeveloper
8 * Author URI: https://wpdeveloper.net
9 * Version: 1.3.0
10 * License: GPL3+
11 * License URI: http://www.gnu.org/licenses/gpl-3.0.txt
12 * Text Domain: toggle-content
13 * Requires at least: 6.0
14 * Tested up to: 7.1
15 * Requires PHP: 7.4
16 *
17 * @package toggle-content
18 */
19
20 // Exit if accessed directly.
21 if ( ! defined( 'ABSPATH' ) ) {
22 exit;
23 }
24
25 /**
26 * Registers all block assets so that they can be enqueued through the block editor
27 * in the corresponding context.
28 *
29 * @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/applying-styles-with-stylesheets/
30 */
31
32 define( 'TOGGLE_CONTENT_VERSION', "1.3.0" );
33 define( 'TOGGLE_CONTENT_ADMIN_URL', plugin_dir_url( __FILE__ ) );
34 define( 'TOGGLE_CONTENT_ADMIN_PATH', dirname( __FILE__ ) );
35
36 require_once __DIR__ . '/includes/font-loader.php';
37 require_once __DIR__ . '/includes/post-meta.php';
38 require_once __DIR__ . '/includes/helpers.php';
39
40 /**
41 * The style-handler is shipped as a git submodule. It is what turns each block's
42 * `blockMeta` attribute into the generated stylesheet under `uploads/eb-style/`
43 * and enqueues it on the frontend. Without it the editor still renders its inline
44 * `<style>`, but the frontend gets no generated CSS at all.
45 *
46 * Guard the include so an uninitialised submodule degrades instead of fatalling,
47 * but warn loudly: a silent skip looks exactly like "the block has no styles".
48 */
49 if ( file_exists( __DIR__ . '/lib/style-handler/style-handler.php' ) ) {
50 require_once __DIR__ . '/lib/style-handler/style-handler.php';
51 } else {
52 add_action( 'admin_notices', 'toggle_content_missing_style_handler_notice' );
53 }
54
55 function create_block_toggle_content_block_init() {
56
57 $script_asset_path = TOGGLE_CONTENT_ADMIN_PATH . "/dist/index.asset.php";
58 if ( ! file_exists( $script_asset_path ) ) {
59 // Missing build output must not fatal the whole site on `init`.
60 add_action( 'admin_notices', 'toggle_content_missing_build_notice' );
61 return;
62 }
63 $script_asset = require $script_asset_path;
64 if ( ! is_array( $script_asset ) || ! isset( $script_asset['dependencies'] ) || ! is_array( $script_asset['dependencies'] ) ) {
65 $script_asset = [
66 'dependencies' => [],
67 'version' => TOGGLE_CONTENT_VERSION
68 ];
69 }
70 $all_dependencies = array_merge( $script_asset['dependencies'], [
71 'wp-blocks',
72 'wp-i18n',
73 'wp-element',
74 'wp-block-editor',
75 'toggle-content-controls-util',
76 'essential-blocks-eb-animation'
77 ] );
78
79 $index_js = TOGGLE_CONTENT_ADMIN_URL . 'dist/index.js';
80 wp_register_script(
81 'create-block-toggle-content-block-editor',
82 $index_js,
83 $all_dependencies,
84 $script_asset['version'],
85 true
86 );
87
88 $load_animation_js = TOGGLE_CONTENT_ADMIN_URL . 'assets/js/eb-animation-load.js';
89 wp_register_script(
90 'essential-blocks-eb-animation',
91 $load_animation_js,
92 [],
93 TOGGLE_CONTENT_VERSION,
94 true
95 );
96
97 $animate_css = TOGGLE_CONTENT_ADMIN_URL . 'assets/css/animate.min.css';
98 wp_register_style(
99 'essential-blocks-animation',
100 $animate_css,
101 [],
102 TOGGLE_CONTENT_VERSION
103 );
104
105 $style_css = TOGGLE_CONTENT_ADMIN_URL . 'dist/style.css';
106 wp_register_style(
107 'create-block-toggle-content-block-editor-css',
108 $style_css,
109 [ 'essential-blocks-animation' ],
110 TOGGLE_CONTENT_VERSION,
111 "all"
112 );
113
114 $frontend_js = TOGGLE_CONTENT_ADMIN_URL . 'dist/frontend/index.js';
115 wp_register_script(
116 'essential-blocks-toggle-content-frontend',
117 $frontend_js,
118 [ 'essential-blocks-eb-animation' ],
119 TOGGLE_CONTENT_VERSION,
120 true
121 );
122
123 if ( ! WP_Block_Type_Registry::get_instance()->is_registered( 'essential-blocks/toggle-content' ) ) {
124 register_block_type(
125 Toggle_Content_Helper::get_block_register_path( "toggle-content/toggle-content", TOGGLE_CONTENT_ADMIN_PATH ),
126 [
127 'editor_script' => 'create-block-toggle-content-block-editor',
128 'editor_style' => 'create-block-toggle-content-block-editor-css',
129 'render_callback' => function ( $attributes, $content ) {
130 if ( ! is_admin() ) {
131 wp_enqueue_script( 'essential-blocks-toggle-content-frontend' );
132 wp_enqueue_style( 'create-block-toggle-content-block-editor-css' );
133 }
134 return $content;
135 }
136 ]
137 );
138 }
139 }
140
141 add_action( 'init', 'create_block_toggle_content_block_init', 99 );
142
143 /**
144 * Admin notice shown when the block build output is missing.
145 */
146 function toggle_content_missing_build_notice() {
147 if ( ! current_user_can( 'activate_plugins' ) ) {
148 return;
149 }
150 printf(
151 '<div class="notice notice-error"><p>%s</p></div>',
152 esc_html__( 'Toggle Content: build output is missing. Run `npm install && npm run build` in the plugin directory.', 'toggle-content' )
153 );
154 }
155
156 /**
157 * Admin notice shown when the style-handler submodule is missing.
158 *
159 * Without it no generated CSS is written or enqueued, so blocks render unstyled
160 * on the frontend while still looking correct in the editor.
161 */
162 function toggle_content_missing_style_handler_notice() {
163 if ( ! current_user_can( 'activate_plugins' ) ) {
164 return;
165 }
166 printf(
167 '<div class="notice notice-error"><p>%s</p></div>',
168 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' )
169 );
170 }
171