| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Block Manager |
| 4 |
Plugin URI: https://connekthq.com/plugins/block-manager/ |
| 5 |
Description: Globally manage the active state of each Gutenberg block. |
| 6 |
Text Domain: block-manager |
| 7 |
Author: Darren Cooney |
| 8 |
Author URI: https://connekthq.com |
| 9 |
Version: 1.1 |
| 10 |
License: GPL |
| 11 |
Copyright: Darren Cooney & Connekt Media |
| 12 |
*/ |
| 13 |
|
| 14 |
|
| 15 |
// Exit if accessed directly. |
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
define( 'BLOCK_MANAGER_VERSION', '1.1' ); |
| 21 |
define( 'BLOCK_MANAGER_RELEASE', 'January 19, 2021' ); |
| 22 |
define( 'BLOCK_MANAGER_DIR_PATH', plugin_dir_path( __FILE__ ) ); |
| 23 |
define( 'BLOCK_MANAGER_OPTION', 'gbm_disabled_blocks' ); |
| 24 |
|
| 25 |
/** |
| 26 |
* Block Manager Class. |
| 27 |
* |
| 28 |
* @since 1.0 |
| 29 |
*/ |
| 30 |
class Gutenberg_Block_Manager { |
| 31 |
|
| 32 |
/** |
| 33 |
* Block Manager Instance variable. |
| 34 |
* |
| 35 |
* @var $instance |
| 36 |
* @since 1.0 |
| 37 |
*/ |
| 38 |
private static $instance = null; |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Define the Block Manager Instance |
| 43 |
* |
| 44 |
* @author ConnektMedia |
| 45 |
* @since 1.0 |
| 46 |
*/ |
| 47 |
public static function instance() { |
| 48 |
if ( null === self::$instance ) { |
| 49 |
self::$instance = new Gutenberg_Block_Manager(); |
| 50 |
} |
| 51 |
return self::$instance; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Initialize plugin. |
| 57 |
* |
| 58 |
* @author ConnektMedia |
| 59 |
* @since 1.0 |
| 60 |
*/ |
| 61 |
private function __construct() { |
| 62 |
|
| 63 |
add_action( 'enqueue_block_editor_assets', array( $this, 'gbm_enqueue' ) ); |
| 64 |
load_plugin_textdomain( 'gutenberg-block-manager', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' ); |
| 65 |
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( &$this, 'gbm_action_links' ) ); |
| 66 |
require_once BLOCK_MANAGER_DIR_PATH . 'class-admin.php'; |
| 67 |
require_once 'api/toggle.php'; |
| 68 |
require_once 'api/bulk_process.php'; |
| 69 |
require_once 'api/export.php'; |
| 70 |
include_once 'vendor/connekt-plugin-installer/class-connekt-plugin-installer.php'; |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Enqueue the scripts. |
| 76 |
* |
| 77 |
* @author ConnektMedia |
| 78 |
* @since 1.0 |
| 79 |
*/ |
| 80 |
public function gbm_enqueue() { |
| 81 |
$script = 'dist/js/gbm.js'; |
| 82 |
wp_enqueue_script( |
| 83 |
'gutenberg-block-manager', |
| 84 |
plugins_url( $script, __FILE__ ), |
| 85 |
array( 'wp-edit-post' ), |
| 86 |
BLOCK_MANAGER_VERSION, false |
| 87 |
); |
| 88 |
wp_localize_script( |
| 89 |
'gutenberg-block-manager', |
| 90 |
'gutenberg_block_manager', |
| 91 |
$this->gbm_get_disabled_blocks() |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get all disabled blocks. |
| 97 |
* |
| 98 |
* @author ConnektMedia |
| 99 |
* @since 1.0 |
| 100 |
* @return array |
| 101 |
*/ |
| 102 |
public static function gbm_get_disabled_blocks() { |
| 103 |
$blocks_manual = (array) get_option( BLOCK_MANAGER_OPTION, array() ); // Get manually disabled blocks. |
| 104 |
$blocks_filter = apply_filters( 'gbm_disabled_blocks', [] ); // Get filtered disabled blocks. |
| 105 |
$blocks_array = array_merge( $blocks_manual, $blocks_filter ); // Merge arrays. |
| 106 |
$blocks = array_unique( $blocks_array ); // Remove Duplicates. |
| 107 |
|
| 108 |
return $blocks ? $blocks : []; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get all filtered blocks. |
| 113 |
* |
| 114 |
* @author ConnektMedia |
| 115 |
* @since 1.1 |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public static function gbm_get_filtered_blocks() { |
| 119 |
$blocks = apply_filters( 'gbm_disabled_blocks', [] ); // Get filtered disabled blocks. |
| 120 |
return $blocks ? $blocks : []; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Add plugin action links to WP plugin screen |
| 125 |
* |
| 126 |
* @author ConnektMedia |
| 127 |
* @since 1.0 |
| 128 |
* @param array $links The action links. |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
public static function gbm_action_links( $links ) { |
| 132 |
$settings = '<a href="' . get_admin_url( null, 'options-general.php?page=gutenberg-block-manager' ) . '">' . __( 'Manage Blocks', 'gutenberg-block-manager' ).'</a>'; |
| 133 |
array_unshift( $links, $settings ); |
| 134 |
return $links; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Confirm user has access to Block Manager. |
| 139 |
* |
| 140 |
* @author ConnektMedia |
| 141 |
* @since 1.1 |
| 142 |
* @return Boolean |
| 143 |
*/ |
| 144 |
public static function has_access() { |
| 145 |
$access = false; |
| 146 |
if ( is_user_logged_in() && current_user_can( apply_filters( 'gutenberg_block_manager_user_role', 'activate_plugins' ) ) ) { |
| 147 |
$access = true; |
| 148 |
} |
| 149 |
return $access; |
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* The main function for Gutenberg_Block_Manager_Init |
| 156 |
* |
| 157 |
* @author ConnektMedia |
| 158 |
* @since 1.0 |
| 159 |
*/ |
| 160 |
function block_manager_init() { |
| 161 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 162 |
if ( is_plugin_active( 'gutenberg/gutenberg.php' ) || version_compare( get_bloginfo( 'version' ), '4.9.9', '>' ) ) { |
| 163 |
Gutenberg_Block_Manager::instance(); |
| 164 |
} |
| 165 |
} |
| 166 |
add_action( 'plugins_loaded', 'block_manager_init', 100 ); |
| 167 |
|