| 1 |
<?php |
| 2 |
/** |
| 3 |
* Main plugin class. |
| 4 |
* |
| 5 |
* @package BetterBlockEditor |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BetterBlockEditor; |
| 9 |
|
| 10 |
use BetterBlockEditor\Core\ModulesManager; |
| 11 |
use BetterBlockEditor\Core\BundledAssetsManager; |
| 12 |
use BetterBlockEditor\Core\Settings; |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
class Plugin { |
| 16 |
|
| 17 |
/** |
| 18 |
* @var Plugin |
| 19 |
*/ |
| 20 |
private static $_instance; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var ModulesManager |
| 24 |
*/ |
| 25 |
public $modules_manager; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var BundledAssetsManager |
| 29 |
*/ |
| 30 |
public $bundled_assets_manager; |
| 31 |
|
| 32 |
/** |
| 33 |
* Plugin constructor. |
| 34 |
*/ |
| 35 |
private function __construct() { |
| 36 |
add_action( 'init', array( $this, 'on_init' ), 0 ); |
| 37 |
|
| 38 |
// settings menu item and page. |
| 39 |
add_action( 'admin_init', array( Settings::class, 'settings_init' ) ); |
| 40 |
add_action( 'rest_api_init', array( Settings::class, 'rest_settings_init' ) ); |
| 41 |
add_action( 'admin_menu', array( Settings::class, 'settings_page' ) ); |
| 42 |
|
| 43 |
// add link to settings page in plugins list. |
| 44 |
add_filter( |
| 45 |
'plugin_action_links_' . WPBBE_BASE, |
| 46 |
function ( $links ) { |
| 47 |
$url = admin_url( 'options-general.php?page=' . Settings::MENU_PAGE_SLUG ); |
| 48 |
array_push( $links, '<a href="' . $url . '">' . esc_html( __( 'Settings', 'better-block-editor' ) ) . '</a>' ); |
| 49 |
|
| 50 |
return $links; |
| 51 |
} |
| 52 |
); |
| 53 |
|
| 54 |
// add custom links to plugin row meta |
| 55 |
add_filter( 'plugin_row_meta', function( $links, $file ) { |
| 56 |
if ( $file === 'better-block-editor/better-block-editor.php' ) { |
| 57 |
$links[] = sprintf( |
| 58 |
'<a href="%s" target="_blank">%s</a>', |
| 59 |
esc_url( 'https://docs.wpbbe.io' ), |
| 60 |
esc_html__( 'User guide', 'better-block-editor' ) |
| 61 |
); |
| 62 |
|
| 63 |
$links[] = sprintf( |
| 64 |
'<a href="%s" target="_blank">%s</a>', |
| 65 |
esc_url( 'https://wpbbe.io/contact/' ), |
| 66 |
esc_html__( 'Report a problem', 'better-block-editor' ) |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
return $links; |
| 71 |
}, 10, 2 ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Singleton implementation |
| 76 |
* |
| 77 |
* @return self |
| 78 |
*/ |
| 79 |
public static function instance() { |
| 80 |
if ( is_null( self::$_instance ) ) { |
| 81 |
self::$_instance = new self(); |
| 82 |
|
| 83 |
do_action( 'wpbbe/loaded' ); |
| 84 |
} |
| 85 |
|
| 86 |
return self::$_instance; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Uninstall hook. |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public static function on_uninstall() { |
| 95 |
// This is a placeholder for any future uninstall logic. |
| 96 |
} |
| 97 |
|
| 98 |
public function on_init() { |
| 99 |
$this->modules_manager = new ModulesManager(); |
| 100 |
$this->modules_manager->setup_hooks(); |
| 101 |
|
| 102 |
// in case there is BBE Pro Kit we have to add BBE Pro Kit bundles as dependencies for our bundles |
| 103 |
// so they are loaded before our bundles (based on defer loading strategy). |
| 104 |
$dependencies =array(); |
| 105 |
|
| 106 |
if ( defined('BBE_PRO_KIT_PLUGIN_ID')) { |
| 107 |
$supported_bundles = array( |
| 108 |
BundledAssetsManager::EDITOR_BUNDLE, |
| 109 |
BundledAssetsManager::EDITOR_CONTENT_BUNDLE, |
| 110 |
BundledAssetsManager::VIEW_BUNDLE, |
| 111 |
); |
| 112 |
|
| 113 |
foreach ( $supported_bundles as $bundle ) { |
| 114 |
$dependencies[ $bundle ] = array( |
| 115 |
BundledAssetsManager::build_handle( BBE_PRO_KIT_PLUGIN_ID, $bundle, 'script' ) |
| 116 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
$this->bundled_assets_manager = new BundledAssetsManager( |
| 121 |
WPBBE_PLUGIN_ID, |
| 122 |
WPBBE_DIST, |
| 123 |
WPBBE_URL_DIST, |
| 124 |
$dependencies |
| 125 |
); |
| 126 |
|
| 127 |
if ( $this->is_asset_bundle_mode() ) { |
| 128 |
if ( is_admin() ) { |
| 129 |
$this->bundled_assets_manager->process_editor_assets(); |
| 130 |
$this->bundled_assets_manager->process_editor_content_assets(); |
| 131 |
} else { |
| 132 |
$this->bundled_assets_manager->process_view_assets(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
do_action( 'wpbbe/init' ); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* Check if the plugin is in asset bundle mode. |
| 142 |
* Asset bundle mode means that all modules are enabled and their assets |
| 143 |
* can be loaded as a single bundle. |
| 144 |
* If any module is disabled, we need to load each module assets separately. |
| 145 |
* |
| 146 |
* @return bool |
| 147 |
*/ |
| 148 |
public function is_asset_bundle_mode() { |
| 149 |
$disabled_modules = array_filter( |
| 150 |
$this->modules_manager->get_managable_modules_data(), |
| 151 |
function ( $module_data ) { |
| 152 |
// hardcoded check to exclude Design System Parts module from asset bundle mode check |
| 153 |
// this module is disabled by default and has no effect on asset bundle mode |
| 154 |
if ($module_data['identifier'] === Modules\DesignSystemParts\Module::MODULE_IDENTIFIER) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
return ! $module_data['enabled']; |
| 159 |
} |
| 160 |
); |
| 161 |
|
| 162 |
return empty( $disabled_modules ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Check if a feature (module) is active |
| 167 |
* |
| 168 |
* @param string $feature Feature identifier. |
| 169 |
* |
| 170 |
* @return bool |
| 171 |
*/ |
| 172 |
public function is_feature_active( $feature ) { |
| 173 |
return (bool) $this->modules_manager->get_modules( $feature ); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* Get all features (modules) |
| 179 |
* |
| 180 |
* @return array |
| 181 |
*/ |
| 182 |
public function get_active_features_keys() { |
| 183 |
$data = array(); |
| 184 |
|
| 185 |
$modules = $this->modules_manager->get_modules(); |
| 186 |
|
| 187 |
foreach ( $modules as $module ) { |
| 188 |
if ( $module::is_core_module() ) { |
| 189 |
continue; |
| 190 |
} |
| 191 |
$data[] = $module::get_identifier(); |
| 192 |
} |
| 193 |
|
| 194 |
return $data; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Clone. |
| 200 |
* Disable class cloning and throw an error on object clone. |
| 201 |
* The whole idea of the singleton design pattern is that there is a single |
| 202 |
* object. Therefore, we don't want the object to be cloned. |
| 203 |
* |
| 204 |
* @since 1.7.0 |
| 205 |
* @access public |
| 206 |
*/ |
| 207 |
public function __clone() { |
| 208 |
_doing_it_wrong( |
| 209 |
__FUNCTION__, |
| 210 |
sprintf( 'Cloning instances of the singleton "%s" class is forbidden.', get_class( $this ) ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 211 |
'1.0.0' |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Wakeup. |
| 217 |
* Disable unserializing of the class. |
| 218 |
* |
| 219 |
* @since 1.7.0 |
| 220 |
* @access public |
| 221 |
*/ |
| 222 |
public function __wakeup() { |
| 223 |
_doing_it_wrong( |
| 224 |
__FUNCTION__, |
| 225 |
sprintf( 'Unserializing instances of the singleton "%s" class is forbidden.', get_class( $this ) ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 226 |
'1.0.0' |
| 227 |
); |
| 228 |
} |
| 229 |
} |
| 230 |
|