-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathblocks-everywhere.php
More file actions
137 lines (115 loc) · 3.55 KB
/
blocks-everywhere.php
File metadata and controls
137 lines (115 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/*
Plugin Name: Blocks Everywhere
Description: Because somewhere is just not enough. Add Gutenberg to WordPress comments, bbPress forums, and BuddyPress streams. Also enables Gutenberg for comment & bbPress moderation.
Version: 1.23.0
Author: Automattic
Text Domain: 'blocks-everywhere'
*/
namespace Automattic\Blocks_Everywhere;
use Automattic\Blocks_Everywhere\Handler;
require_once __DIR__ . '/classes/class-handler.php';
require_once __DIR__ . '/classes/class-editor.php';
require_once __DIR__ . '/classes/handlers/class-bbpress.php';
require_once __DIR__ . '/classes/handlers/class-buddypress.php';
require_once __DIR__ . '/classes/handlers/class-comments.php';
class Blocks_Everywhere {
const VERSION = '1.23.0';
/**
* Instance variable
* @var Blocks_Everywhere|null
*/
private static $instance = null;
/**
* Gutenberg editor
*
* @var Blocks_Everywhere|null
*/
private $gutenberg = null;
/**
* Gutenberg handlers
*
* @var Handler\Handler[]
*/
private $handlers = [];
/**
* Singleton access
*
* @return Blocks_Everywhere
*/
public static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new Blocks_Everywhere();
}
return self::$instance;
}
/**
* Constructor
*/
public function __construct() {
add_action( 'init', [ $this, 'load_handlers' ] );
// Admin editors
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
}
/**
* Load whatever handler is configured
*
* @return void
*/
public function load_handlers() {
$default_comments = defined( 'BLOCKS_EVERYWHERE_COMMENTS' ) ? BLOCKS_EVERYWHERE_COMMENTS : false;
$default_bbpress = defined( 'BLOCKS_EVERYWHERE_BBPRESS' ) ? BLOCKS_EVERYWHERE_BBPRESS : false;
$default_buddypress = defined( 'BLOCKS_EVERYWHERE_BUDDYPRESS' ) ? BLOCKS_EVERYWHERE_BUDDYPRESS : false;
if ( apply_filters( 'blocks_everywhere_comments', $default_comments ) ) {
$this->handlers['Comments'] = new Handler\Comments();
}
if ( apply_filters( 'blocks_everywhere_bbpress', $default_bbpress ) ) {
$this->handlers['bbPress'] = new Handler\bbPress();
}
if ( apply_filters( 'blocks_everywhere_buddypress', $default_buddypress ) ) {
$this->handlers['BuddyPress'] = new Handler\BuddyPress();
}
}
/**
* Get the instantiated handler class for the specified type, or null if it isn't configured or known.
*
* @param 'Comments'|'bbPress'|'BuddyPress' $which The handler type.
* @return Handler\Handler|null object or null it not configured.
*/
public function get_handler( $which ) {
if ( isset( $this->handlers[ $which ] ) ) {
return $this->handlers[ $which ];
}
return null;
}
/**
* Perform additional admin tasks when on the comment page
*
* @param String $hook Page hook.
* @return void
*/
public function admin_enqueue_scripts( $hook ) {
foreach ( $this->handlers as $handler ) {
if ( $handler->can_show_admin_editor( $hook ) ) {
add_action(
'admin_head',
function() use ( $handler ) {
add_filter( 'the_editor', [ $handler, 'the_editor' ] );
add_filter( 'wp_editor_settings', [ $handler, 'wp_editor_settings' ], 10, 2 );
}
);
// Stops a problem with the Gutenberg plugin accessing widgets that don't exist
remove_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' );
// Load Gutenberg in in_admin_header so WP admin doesn't set the 'block-editor-page' body class
add_action(
'in_admin_header',
function() use ( $handler ) {
$handler->load_editor( '.wp-editor-area' );
}
);
break;
}
}
}
}
Blocks_Everywhere::init();