| 1 |
<?php |
| 2 |
namespace Friends\Blocks_Everywhere; |
| 3 |
/* |
| 4 |
This is a copy of https://github.com/Automattic/blocks-everywhere version 1.14.1 |
| 5 |
|
| 6 |
Changes: |
| 7 |
- Adjusted namespaces to start with Friends. |
| 8 |
- Handlers for the Friends plugin. |
| 9 |
- Removed the existing handlers. |
| 10 |
*/ |
| 11 |
|
| 12 |
use Friends\Blocks_Everywhere\Handler; |
| 13 |
|
| 14 |
require_once __DIR__ . '/classes/class-handler.php'; |
| 15 |
require_once __DIR__ . '/classes/class-editor.php'; |
| 16 |
|
| 17 |
|
| 18 |
class Blocks_Everywhere { |
| 19 |
const VERSION = '1.20.1'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Instance variable |
| 23 |
* @var Blocks_Everywhere|null |
| 24 |
*/ |
| 25 |
private static $instance = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Gutenberg editor |
| 29 |
* |
| 30 |
* @var Blocks_Everywhere|null |
| 31 |
*/ |
| 32 |
private $gutenberg = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* Gutenberg handlers |
| 36 |
* |
| 37 |
* @var Handler\Handler[] |
| 38 |
*/ |
| 39 |
private $handlers = []; |
| 40 |
|
| 41 |
/** |
| 42 |
* Singleton access |
| 43 |
* |
| 44 |
* @return Blocks_Everywhere |
| 45 |
*/ |
| 46 |
public static function init() { |
| 47 |
if ( is_null( self::$instance ) ) { |
| 48 |
self::$instance = new Blocks_Everywhere(); |
| 49 |
} |
| 50 |
|
| 51 |
return self::$instance; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Constructor |
| 56 |
*/ |
| 57 |
public function __construct() { |
| 58 |
add_action( 'init', [ $this, 'load_handlers' ] ); |
| 59 |
|
| 60 |
// Admin editors |
| 61 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Load whatever handler is configured |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function load_handlers() { |
| 70 |
$this->handlers[] = new Handler\Friends_Message(); |
| 71 |
$this->handlers[] = new Handler\Friends_Status_Post(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get the instantiated handler class for the specified type, or null if it isn't configured or known. |
| 76 |
* |
| 77 |
* @param 'Comments'|'bbPress'|'BuddyPress' $which The handler type. |
| 78 |
* @return Handler\Handler|null object or null it not configured. |
| 79 |
*/ |
| 80 |
public function get_handler( $which ) { |
| 81 |
if ( isset( $this->handlers[ $which ] ) ) { |
| 82 |
return $this->handlers[ $which ]; |
| 83 |
} |
| 84 |
|
| 85 |
return null; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Perform additional admin tasks when on the comment page |
| 90 |
* |
| 91 |
* @param String $hook Page hook. |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function admin_enqueue_scripts( $hook ) { |
| 95 |
foreach ( $this->handlers as $handler ) { |
| 96 |
if ( $handler->can_show_admin_editor( $hook ) ) { |
| 97 |
add_action( |
| 98 |
'admin_head', |
| 99 |
function() use ( $handler ) { |
| 100 |
add_filter( 'the_editor', [ $handler, 'the_editor' ] ); |
| 101 |
add_filter( 'wp_editor_settings', [ $handler, 'wp_editor_settings' ], 10, 2 ); |
| 102 |
} |
| 103 |
); |
| 104 |
|
| 105 |
// Stops a problem with the Gutenberg plugin accessing widgets that don't exist |
| 106 |
remove_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' ); |
| 107 |
|
| 108 |
// Load Gutenberg in in_admin_header so WP admin doesn't set the 'block-editor-page' body class |
| 109 |
add_action( |
| 110 |
'in_admin_header', |
| 111 |
function() use ( $handler ) { |
| 112 |
$handler->load_editor( '.wp-editor-area' ); |
| 113 |
} |
| 114 |
); |
| 115 |
|
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
Blocks_Everywhere::init(); |
| 123 |
|