| 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 |
return array_values( $options ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Remove any duplicate block categories when using category hook. |
| 128 |
* |
| 129 |
* @param array $options The categories from WP options. |
| 130 |
* @param array $filtered The filtered categories. |
| 131 |
* @return array Modified options. |
| 132 |
*/ |
| 133 |
public function gbm_remove_duplicate_categories( $options, $filtered ) { |
| 134 |
if ( $options && $filtered ) { |
| 135 |
$updated = false; |
| 136 |
foreach ( $filtered as $filter ) { |
| 137 |
// Search array for filtered category. |
| 138 |
$key = array_search( $filter['block'], array_column( $options, 'block' ), true ); |
| 139 |
if ( $key !== false ) { |
| 140 |
unset( $options[ $key ] ); // Remove filtered item from array. |
| 141 |
$options = array_values( $options ); // Reset array keys. |
| 142 |
$updated = true; |
| 143 |
} |
| 144 |
} |
| 145 |
if ( $updated ) { |
| 146 |
update_option( BLOCK_MANAGER_CATEGORIES, $options ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return array_values( $options ); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Register submenu item. |
| 155 |
* |
| 156 |
* @author ConnektMedia |
| 157 |
* @since 1.0 |
| 158 |
*/ |
| 159 |
public function gbm_register_sub_menu() { |
| 160 |
add_submenu_page( |
| 161 |
'options-general.php', |
| 162 |
esc_html__( 'Block Manager', 'block-manager' ), |
| 163 |
esc_html__( 'Block Manager', 'block-manager' ), |
| 164 |
apply_filters( 'block_manager_user_role', 'activate_plugins' ), |
| 165 |
'block-manager', |
| 166 |
[ |
| 167 |
$this, |
| 168 |
'gbm_submenu_page_callback', |
| 169 |
] |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* The Block Manager admin page. |
| 175 |
* |
| 176 |
* @author ConnektMedia |
| 177 |
* @since 1.0 |
| 178 |
*/ |
| 179 |
public function gbm_submenu_page_callback() { |
| 180 |
$active = 'blocks'; |
| 181 |
if ( isset( $_GET ) && isset( $_GET['category-switcher'] ) && empty( $_GET['category-switcher'] ) ) { |
| 182 |
$active = 'categories'; |
| 183 |
} |
| 184 |
?> |
| 185 |
<h1 class="gbm-h1"><?php esc_html_e( 'Block Manager', 'block-manager' ); ?></h1> |
| 186 |
<div class="gbm-page-wrap"> |
| 187 |
<div class="gbm-page-wrap--header"> |
| 188 |
<div class="gbm-page-wrap--header-title"> |
| 189 |
<h2><?php esc_html_e( 'Block Manager', 'block-manager' ); ?> <span><?php echo esc_attr( BLOCK_MANAGER_VERSION ); ?></span></h2> |
| 190 |
<p><?php esc_html_e( 'Take control of your WordPress blocks.', 'block-manager' ); ?></p> |
| 191 |
</div> |
| 192 |
<button class="gbm-other-button" id="otherPlugins"> |
| 193 |
<span class="dashicons dashicons-admin-plugins"></span> <?php esc_html_e( 'Other Plugins', 'block-manager' ); ?> |
| 194 |
</button> |
| 195 |
</div> |
| 196 |
<div id="gbm-container"> |
| 197 |
<div id="gbm-other-plugins"> |
| 198 |
<?php |
| 199 |
$plugin_array = [ |
| 200 |
[ |
| 201 |
'slug' => 'ajax-load-more', |
| 202 |
], |
| 203 |
[ |
| 204 |
'slug' => 'easy-query', |
| 205 |
], |
| 206 |
[ |
| 207 |
'slug' => 'instant-images', |
| 208 |
], |
| 209 |
]; |
| 210 |
?> |
| 211 |
<section> |
| 212 |
<div> |
| 213 |
<h2> |
| 214 |
<?php |
| 215 |
/* translators: %1$s & %2$s is replaced with the link content */ |
| 216 |
echo sprintf( __( 'Other WordPress Plugins from %1$s Connekt %2$s', 'block-manager' ), '<a href="https://connekthq.com" target="_blank">', '</a>' ); // @codingStandardsIgnoreLine |
| 217 |
?> |
| 218 |
</h2> |
| 219 |
</div> |
| 220 |
<div class="cta-wrap"> |
| 221 |
<?php |
| 222 |
if ( class_exists( 'Connekt_Plugin_Installer' ) ) { |
| 223 |
Connekt_Plugin_Installer::init( $plugin_array ); |
| 224 |
} |
| 225 |
?> |
| 226 |
</div> |
| 227 |
<div class="gbm-close-wrap"> |
| 228 |
<button class="gbm-other-button--close" id="otherPluginsClose"><?php esc_html_e( 'Close', 'block-manager' ); ?></button> |
| 229 |
</div> |
| 230 |
</section> |
| 231 |
</div> |
| 232 |
<div class="nav-tab-wrapper"> |
| 233 |
<a class="nav-tab |
| 234 |
<?php |
| 235 |
if ( 'blocks' === $active ) { |
| 236 |
echo ' nav-tab-active'; } |
| 237 |
?> |
| 238 |
" href="options-general.php?page=block-manager"> |
| 239 |
<?php esc_html_e( 'Blocks', 'block-manager' ); ?> |
| 240 |
</a> |
| 241 |
<a class="nav-tab |
| 242 |
<?php |
| 243 |
if ( 'categories' === $active ) { |
| 244 |
echo ' nav-tab-active'; } |
| 245 |
?> |
| 246 |
" href="options-general.php?page=block-manager&category-switcher"> |
| 247 |
<?php esc_html_e( 'Block Categories', 'block-manager' ); ?> |
| 248 |
</a> |
| 249 |
</div> |
| 250 |
<div id="app" class="gbm"></div> |
| 251 |
</div> |
| 252 |
</div> |
| 253 |
<?php |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
new GBM_Admin(); |
| 258 |
|