Blocks.php
1.92 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
<?php
namespace AIOSEO\Plugin\Common\Utils;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Extendable Block class helper
*
* @since 4.1.1
*/
class Blocks {
/**
* Class constructor.
*
* @since 4.1.1
*/
public function __construct() {
add_action( 'init', [ $this, 'init' ] );
global $wp_version;
if ( version_compare( $wp_version, '5.8', '<' ) ) {
add_filter( 'block_categories', [ $this, 'blockCategories' ], 10 );
return;
}
add_filter( 'block_categories_all', [ $this, 'blockCategories' ], 10 );
}
/**
* Initializes our blocks.
*
* @since 4.1.1
*
* @return void
*/
public function init() {
if ( ! $this->isBlockEditorActive() ) {
return;
}
$this->register();
}
/**
* Registers the block. This is a wrapper to be extended in the child class.
*
* @since 4.1.1
*
* @return void
*/
public function register() {}
/**
* Adds a new AIOSEO block category.
*
* @since 4.1.1
*
* @param array $categories Array of block categories.
* @return void
*/
public function blockCategories( $categories ) {
$exists = wp_list_filter( $categories, [ 'slug' => 'aioseo' ] );
if ( ! empty( $exists ) ) {
return $categories;
}
return array_merge(
$categories,
[
[
'slug' => 'aioseo',
'title' => AIOSEO_PLUGIN_SHORT_NAME,
]
]
);
}
/**
* Helper function to determine if we're rendering the block inside Gutenberg.
*
* @since 4.1.1
*
* @return bool In gutenberg.
*/
public function isGBEditor() {
return \defined( 'REST_REQUEST' ) && REST_REQUEST && ! empty( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context']; // phpcs:ignore HM.Security.NonceVerification.Recommended
}
/**
* Helper function to determine if we can register blocks.
*
* @since 4.1.1
*
* @return bool Can register block.
*/
public function isBlockEditorActive() {
return function_exists( 'register_block_type' );
}
}