| 1 |
<?php |
| 2 |
/** |
| 3 |
* Block Manager admin class. |
| 4 |
* |
| 5 |
* @author ConnektMedia |
| 6 |
* @since 1.0 |
| 7 |
*/ |
| 8 |
class GBM_Admin { |
| 9 |
|
| 10 |
/** |
| 11 |
* Construct method. |
| 12 |
* |
| 13 |
* @author ConnektMedia |
| 14 |
* @since 1.0 |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
|
| 18 |
// Register the submenu. |
| 19 |
add_action( 'admin_menu', array( $this, 'gbm_register_sub_menu' ) ); |
| 20 |
add_action( 'admin_enqueue_scripts', array( $this, 'gbm_admin_enqueue' ) ); |
| 21 |
|
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Enqueue the scripts and styles. |
| 26 |
* |
| 27 |
* @author ConnektMedia |
| 28 |
* @since 1.0 |
| 29 |
* @param string $hook The current page ID. |
| 30 |
* @return null |
| 31 |
*/ |
| 32 |
public function gbm_admin_enqueue( $hook ) { |
| 33 |
|
| 34 |
if ( 'settings_page_gutenberg-block-manager' !== $hook ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
// Register Block Categories. |
| 39 |
$block_categories = array(); |
| 40 |
if ( function_exists( 'get_block_categories' ) ) { |
| 41 |
$block_categories = get_block_categories( get_post() ); |
| 42 |
} |
| 43 |
wp_add_inline_script( 'wp-blocks', sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( $block_categories ) ), 'after' ); |
| 44 |
|
| 45 |
do_action( 'enqueue_block_editor_assets' ); |
| 46 |
wp_dequeue_script( 'gutenberg-block-manager' ); |
| 47 |
|
| 48 |
$block_registry = WP_Block_Type_Registry::get_instance(); |
| 49 |
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { |
| 50 |
// Front-end script. |
| 51 |
if ( ! empty( $block_type->editor_script ) ) { |
| 52 |
wp_enqueue_script( $block_type->editor_script ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// Enqueue Scripts. |
| 57 |
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; // Use minified libraries if SCRIPT_DEBUG is turned off. |
| 58 |
|
| 59 |
wp_enqueue_style( |
| 60 |
'gutenberg-block-manager-styles', |
| 61 |
plugins_url( 'dist/css/style.css', |
| 62 |
__FILE__ ), |
| 63 |
array(), |
| 64 |
BLOCK_MANAGER_VERSION |
| 65 |
); |
| 66 |
|
| 67 |
wp_enqueue_script( |
| 68 |
'gutenberg-block-manager-admin', |
| 69 |
plugins_url( 'dist/js/gbm-admin' . $suffix . '.js', __FILE__ ), |
| 70 |
array( 'wp-blocks', 'wp-element', 'wp-data', 'wp-edit-post', 'wp-components', 'wp-block-library' ), |
| 71 |
BLOCK_MANAGER_VERSION, |
| 72 |
true |
| 73 |
); |
| 74 |
|
| 75 |
// Localize Scripts. |
| 76 |
wp_localize_script( |
| 77 |
'gutenberg-block-manager-admin', |
| 78 |
'gbm_localize', |
| 79 |
array( |
| 80 |
'disabledBlocks' => Gutenberg_Block_Manager::gbm_get_disabled_blocks(), |
| 81 |
'filteredBlocks' => Gutenberg_Block_Manager::gbm_get_filtered_blocks(), |
| 82 |
'filteredCategories' => Gutenberg_Block_Manager::gbm_get_filtered_cats(), |
| 83 |
'root' => esc_url_raw( rest_url() ), |
| 84 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 85 |
'enable' => __( 'Enable', 'gutenberg-block-manager' ), |
| 86 |
'disable' => __( 'Disable', 'gutenberg-block-manager' ), |
| 87 |
'enable_all' => __( 'Enable All', 'gutenberg-block-manager' ), |
| 88 |
'disable_all' => __( 'Disable All', 'gutenberg-block-manager' ), |
| 89 |
'toggle_all' => __( 'Toggle All Blocks', 'gutenberg-block-manager' ), |
| 90 |
'toggle' => __( 'Toggle Block Activation', 'gutenberg-block-manager' ), |
| 91 |
'search_label' => __( 'Filter Blocks', 'gutenberg-block-manager' ), |
| 92 |
'submit' => __( 'Submit', 'gutenberg-block-manager' ), |
| 93 |
'loading' => __( 'Loading Blocks', 'gutenberg-block-manager' ), |
| 94 |
'loading_export' => __( 'Getting export data...', 'gutenberg-block-manager' ), |
| 95 |
'copy' => __( 'Copy Code', 'gutenberg-block-manager' ), |
| 96 |
'copied' => __( 'Copied', 'gutenberg-block-manager' ), |
| 97 |
'close' => __( 'Close', 'gutenberg-block-manager' ), |
| 98 |
'grid' => __( 'Grid', 'gutenberg-block-manager' ), |
| 99 |
'list' => __( 'List', 'gutenberg-block-manager' ), |
| 100 |
'cat_switch' => __( 'Block Category', 'gutenberg-block-manager' ), |
| 101 |
'updated' => __( 'Updated', 'gutenberg-block-manager' ), |
| 102 |
'block_switch' => __( 'Block Name', 'gutenberg-block-manager' ), |
| 103 |
'reset_cats' => __( 'Reset Categories', 'gutenberg-block-manager' ), |
| 104 |
'cat_intro' => __( 'The Category Switcher provides functionality for changing the categories of core Gutenberg blocks.', 'gutenberg-block-manager' ), |
| 105 |
'cat_intro2' => __( 'Changing a block category will update the location of the block in the Gutenberg Block Inserter while editing posts.', 'gutenberg-block-manager' ), |
| 106 |
'export' => __( 'Export', 'gutenberg-block-manager' ), |
| 107 |
'export_intro' => __( 'Add the the following code to your functions.php to remove blocks at the theme level.', 'gutenberg-block-manager' ), |
| 108 |
'filtered_alert' => __( 'This block has been globally disabled via the `gbm_disabled_blocks` filter and cannot be activated.', 'gutenberg-block-manager' ), |
| 109 |
) |
| 110 |
); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Register submenu item. |
| 116 |
* |
| 117 |
* @author ConnektMedia |
| 118 |
* @since 1.0 |
| 119 |
*/ |
| 120 |
public function gbm_register_sub_menu() { |
| 121 |
add_submenu_page( |
| 122 |
'options-general.php', |
| 123 |
esc_html__( 'Block Manager', 'gutenberg-block-manager' ), |
| 124 |
esc_html__( 'Block Manager', 'gutenberg-block-manager' ), |
| 125 |
apply_filters( 'gutenberg_block_manager_user_role', 'activate_plugins' ), |
| 126 |
'gutenberg-block-manager', |
| 127 |
array( $this, 'gbm_submenu_page_callback' ) |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* The Block Manager admin page. |
| 133 |
* |
| 134 |
* @author ConnektMedia |
| 135 |
* @since 1.0 |
| 136 |
*/ |
| 137 |
public function gbm_submenu_page_callback() { |
| 138 |
$active = 'blocks'; |
| 139 |
if ( isset ( $_GET ) && isset ( $_GET['category-switcher'] ) && empty ( $_GET['category-switcher'] ) ) { |
| 140 |
$active = 'categories'; |
| 141 |
} |
| 142 |
?> |
| 143 |
|
| 144 |
<div class="gbm-page-wrap"> |
| 145 |
<div class="gbm-page-wrap--header"> |
| 146 |
<h2><?php esc_html_e( 'Gutenberg Block Manager', 'gutenberg-block-manager' ); ?> <span><a href="https://connekthq.com" target="_blank"><?php esc_html_e( 'by Connekt', 'gutenberg-block-manager' ); ?></a></span></h2> |
| 147 |
<?php if ( 'blocks' === $active ) { ?> |
| 148 |
<p><?php printf( esc_html__( 'Manage the status of your %s Gutenberg blocks - disabled blocks will be globally removed from the block inserter.', 'gutenberg-block-manager' ), '<span class="cnkt-block-totals block-total">--</span>'); ?> |
| 149 |
<?php } ?> |
| 150 |
<?php if ( 'categories' === $active ) { ?> |
| 151 |
<p><?php printf( esc_html__( 'Update the categories of your %s blocks with the category switcher.', 'gutenberg-block-manager' ), '<span class="cnkt-block-totals block-total">--</span>'); ?> |
| 152 |
<?php } ?> |
| 153 |
<button class="button" id="otherPlugins"><span class="dashicons dashicons-admin-plugins"></span> <?php esc_html_e( 'Other Plugins', 'gutenberg-block-manager' ); ?></button> |
| 154 |
</div> |
| 155 |
<div id="gbm-container"> |
| 156 |
<div id="gbm-other-plugins"> |
| 157 |
<?php |
| 158 |
$plugin_array = array( |
| 159 |
array( |
| 160 |
'slug' => 'ajax-load-more', |
| 161 |
), |
| 162 |
array( |
| 163 |
'slug' => 'easy-query', |
| 164 |
), |
| 165 |
array( |
| 166 |
'slug' => 'instant-images', |
| 167 |
), |
| 168 |
array( |
| 169 |
'slug' => 'velocity', |
| 170 |
), |
| 171 |
); |
| 172 |
?> |
| 173 |
<section> |
| 174 |
<h2><?php echo sprintf( __("Other Plugins from %s Connekt %s", 'gutenberg-block-manager' ), '<a href="https://connekthq.com" target="_blank">', '</a>' ); ?></h2> |
| 175 |
<button class="button button-secondary" id="otherPluginsClose">× <?php esc_html_e( 'Close', 'gutenberg-block-manager' ); ?></button> |
| 176 |
<div class="cta-wrap"> |
| 177 |
<?php |
| 178 |
if ( class_exists( 'Connekt_Plugin_Installer' ) ) { |
| 179 |
Connekt_Plugin_Installer::init( $plugin_array ); |
| 180 |
} |
| 181 |
?> |
| 182 |
</div> |
| 183 |
</section> |
| 184 |
</div> |
| 185 |
<div class="nav-tab-wrapper"> |
| 186 |
<span><?php esc_html_e( 'Manage', 'gutenberg-block-manager' ); ?> <i class="fa fa-chevron-right" aria-hidden="true"></i></span> |
| 187 |
<a class="nav-tab<?php if ( 'blocks' === $active ) { echo ' nav-tab-active'; } ?>" href="options-general.php?page=gutenberg-block-manager"> |
| 188 |
<?php esc_html_e( 'Blocks', 'gutenberg-block-manager' ); ?> |
| 189 |
</a> |
| 190 |
<a class="nav-tab<?php if ( 'categories' === $active ) { echo ' nav-tab-active'; } ?>" href="options-general.php?page=gutenberg-block-manager&category-switcher"> |
| 191 |
<?php esc_html_e( 'Categories', 'gutenberg-block-manager' ); ?> |
| 192 |
</a> |
| 193 |
</div> |
| 194 |
<div id="app" class="gbm"></div> |
| 195 |
</div> |
| 196 |
</div> |
| 197 |
<?php |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
new GBM_Admin(); |
| 202 |
|