PluginProbe
Block Manager / 2.0.0
Block Manager v2.0.0
trunk 1.0 1.0.1 1.1 1.2 1.2.1 1.2.2 1.2.3c 1.2.4 1.2.5 2.0.0 2.1.0 2.1.0.1 2.1.1 3.0.0 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1
block-manager / class-admin.php

class-admin.php in Block Manager 2.0.0, at class-admin.php

259 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file contains admin functions.
4 *
5 * @package blockmanager.
6 */
7
8 /**
9 * Block Manager admin class.
10 *
11 * @author ConnektMedia
12 * @since 1.0
13 */
14 class GBM_Admin {
15
16 /**
17 * Construct method.
18 *
19 * @author ConnektMedia
20 * @since 1.0
21 */
22 public function __construct() {
23 add_action( 'admin_menu', [ $this, 'gbm_register_sub_menu' ] );
24 add_action( 'admin_enqueue_scripts', [ $this, 'gbm_admin_enqueue' ] );
25 }
26
27 /**
28 * Enqueue the scripts and styles.
29 *
30 * @author ConnektMedia
31 * @since 1.0
32 * @param string $page The current page ID.
33 * @return null
34 */
35 public function gbm_admin_enqueue( $page ) {
36 if ( $page !== 'settings_page_block-manager' ) {
37 return;
38 }
39
40 // Register Block Categories.
41 $block_categories = [];
42 if ( function_exists( 'get_block_categories' ) ) {
43 $block_categories = get_block_categories( get_post() );
44 }
45 wp_add_inline_script( 'wp-blocks', sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( $block_categories ) ), 'after' );
46
47 do_action( 'enqueue_block_editor_assets' );
48 wp_dequeue_script( 'block-manager' );
49
50 // -> https://github.com/WordPress/gutenberg/issues/22812
51 wp_add_inline_script( 'wp-blocks', 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' );
52
53 $block_registry = WP_Block_Type_Registry::get_instance();
54 foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
55 // Front-end script.
56 if ( ! empty( $block_type->editor_script ) ) {
57 wp_enqueue_script( $block_type->editor_script );
58 }
59 }
60
61 // Enqueue Scripts.
62 wp_enqueue_style(
63 'block-manager-styles',
64 plugins_url(
65 'build/style-block-manager-admin.css',
66 __FILE__
67 ),
68 [],
69 BLOCK_MANAGER_VERSION
70 );
71
72 wp_enqueue_script(
73 'block-manager-admin',
74 plugins_url( 'build/block-manager-admin.js', __FILE__ ),
75 [ 'wp-blocks', 'wp-element', 'wp-data', 'wp-components', 'wp-block-library' ],
76 BLOCK_MANAGER_VERSION,
77 true
78 );
79
80 $filtered_blocks = Gutenberg_Block_Manager::gbm_get_filtered_blocks();
81 $filtered_categories = Gutenberg_Block_Manager::gbm_get_filtered_categories();
82
83 // Localize Scripts.
84 wp_localize_script(
85 'block-manager-admin',
86 'gbm_localize',
87 [
88 'root' => esc_url_raw( rest_url() ),
89 'nonce' => wp_create_nonce( 'wp_rest' ),
90 'disabledBlocks' => $this->gbm_remove_duplicate_blocks( Gutenberg_Block_Manager::gbm_get_disabled_blocks(), $filtered_blocks ),
91 'filteredBlocks' => $filtered_blocks,
92 'disabledBlocksAll' => Gutenberg_Block_Manager::gbm_get_all_disabled_blocks(),
93 'blockCategories' => $this->gbm_remove_duplicate_categories( Gutenberg_Block_Manager::gbm_get_block_categories(), $filtered_categories ),
94 'filteredCategories' => $filtered_categories,
95 'filteredCategoriesAll' => Gutenberg_Block_Manager::gbm_get_all_block_categories(),
96 ]
97 );
98 }
99
100 /**
101 * Remove any duplicate block categories when using category hook.
102 *
103 * @param array $options The categories from WP options.
104 * @param array $filtered The filtered categories.
105 * @return array Modified options.
106 */
107 public function gbm_remove_duplicate_blocks( $options, $filtered ) {
108 if ( $options && $filtered ) {
109 $updated = false;
110 foreach ( $filtered as $filter ) {
111 // Search array for filtered block.
112 $key = array_search( $filter, $options, true );
113 if ( $key !== false ) {
114 unset( $options[ $key ] ); // Remove filtered item from array.
115 $options = array_values( $options ); // Reset array keys.
116 $updated = true;
117 }
118 }
119 if ( $updated ) {
120 update_option( BLOCK_MANAGER_OPTION, $options );
121 }
122 }
123
124 return $options;
125 }
126
127 /**
128 * Remove any duplicate block categories when using category hook.
129 *
130 * @param array $options The categories from WP options.
131 * @param array $filtered The filtered categories.
132 * @return array Modified options.
133 */
134 public function gbm_remove_duplicate_categories( $options, $filtered ) {
135 if ( $options && $filtered ) {
136 $updated = false;
137 foreach ( $filtered as $filter ) {
138 // Search array for filtered category.
139 $key = array_search( $filter['block'], array_column( $options, 'block' ), true );
140 if ( $key !== false ) {
141 unset( $options[ $key ] ); // Remove filtered item from array.
142 $options = array_values( $options ); // Reset array keys.
143 $updated = true;
144 }
145 }
146 if ( $updated ) {
147 update_option( BLOCK_MANAGER_CATEGORIES, $options );
148 }
149 }
150
151 return $options;
152 }
153
154 /**
155 * Register submenu item.
156 *
157 * @author ConnektMedia
158 * @since 1.0
159 */
160 public function gbm_register_sub_menu() {
161 add_submenu_page(
162 'options-general.php',
163 esc_html__( 'Block Manager', 'block-manager' ),
164 esc_html__( 'Block Manager', 'block-manager' ),
165 apply_filters( 'block_manager_user_role', 'activate_plugins' ),
166 'block-manager',
167 [
168 $this,
169 'gbm_submenu_page_callback',
170 ]
171 );
172 }
173
174 /**
175 * The Block Manager admin page.
176 *
177 * @author ConnektMedia
178 * @since 1.0
179 */
180 public function gbm_submenu_page_callback() {
181 $active = 'blocks';
182 if ( isset( $_GET ) && isset( $_GET['category-switcher'] ) && empty( $_GET['category-switcher'] ) ) {
183 $active = 'categories';
184 }
185 ?>
186 <h1 class="gbm-h1"><?php esc_html_e( 'Block Manager', 'block-manager' ); ?></h1>
187 <div class="gbm-page-wrap">
188 <div class="gbm-page-wrap--header">
189 <div class="gbm-page-wrap--header-title">
190 <h2><?php esc_html_e( 'Block Manager', 'block-manager' ); ?> <span><?php echo esc_attr( BLOCK_MANAGER_VERSION ); ?></span></h2>
191 <p><?php esc_html_e( 'Take control of your WordPress blocks.', 'block-manager' ); ?></p>
192 </div>
193 <button class="gbm-other-button" id="otherPlugins">
194 <span class="dashicons dashicons-admin-plugins"></span> <?php esc_html_e( 'Other Plugins', 'block-manager' ); ?>
195 </button>
196 </div>
197 <div id="gbm-container">
198 <div id="gbm-other-plugins">
199 <?php
200 $plugin_array = [
201 [
202 'slug' => 'ajax-load-more',
203 ],
204 [
205 'slug' => 'easy-query',
206 ],
207 [
208 'slug' => 'instant-images',
209 ],
210 ];
211 ?>
212 <section>
213 <div>
214 <h2>
215 <?php
216 /* translators: %1$s & %2$s is replaced with the link content */
217 echo sprintf( __( 'Other WordPress Plugins from %1$s Connekt %2$s', 'block-manager' ), '<a href="https://connekthq.com" target="_blank">', '</a>' ); // @codingStandardsIgnoreLine
218 ?>
219 </h2>
220 </div>
221 <div class="cta-wrap">
222 <?php
223 if ( class_exists( 'Connekt_Plugin_Installer' ) ) {
224 Connekt_Plugin_Installer::init( $plugin_array );
225 }
226 ?>
227 </div>
228 <div class="gbm-close-wrap">
229 <button class="gbm-other-button--close" id="otherPluginsClose"><?php esc_html_e( 'Close', 'block-manager' ); ?></button>
230 </div>
231 </section>
232 </div>
233 <div class="nav-tab-wrapper">
234 <a class="nav-tab
235 <?php
236 if ( 'blocks' === $active ) {
237 echo ' nav-tab-active'; }
238 ?>
239 " href="options-general.php?page=block-manager">
240 <?php esc_html_e( 'Blocks', 'block-manager' ); ?>
241 </a>
242 <a class="nav-tab
243 <?php
244 if ( 'categories' === $active ) {
245 echo ' nav-tab-active'; }
246 ?>
247 " href="options-general.php?page=block-manager&category-switcher">
248 <?php esc_html_e( 'Block Categories', 'block-manager' ); ?>
249 </a>
250 </div>
251 <div id="app" class="gbm"></div>
252 </div>
253 </div>
254 <?php
255 }
256 }
257
258 new GBM_Admin();
259