blocks.php
4.23 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace uncanny_learndash_toolkit;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class learndashBreadcrumbs
* @package uncanny_custom_toolkit
*/
class Blocks {
/*
* Plugin prefix
* @var string
*/
public $prefix = '';
/*
* Plugin version
* @var string
*/
public $version = '';
/*
* Active Classes
* @var string
*/
public $active_classes = '';
/**
* Blocks constructor.
*
* @param string $prefix
* @param string $version
* @param array $active_classes
*/
public function __construct( $prefix = '', $version = '', $active_classes = [] ) {
$this->prefix = $prefix;
$this->version = $version;
$this->active_classes = $active_classes;
$add_block_scripts = false;
// Check if Gutenberg exists
if ( function_exists( 'register_block_type' ) ) {
if (
isset( $active_classes['uncanny_learndash_toolkit\Breadcrumbs'] ) ||
isset( $active_classes['uncanny_learndash_toolkit\LearnDashResume'] ) ||
isset( $active_classes['uncanny_learndash_toolkit\FrontendLoginPlus'] )
) {
$add_block_scripts = true;
}
// Register Blocks
add_action( 'init', function () {
if ( isset( $this->active_classes['uncanny_learndash_toolkit\Breadcrumbs'] ) ) {
require_once( dirname( __FILE__ ) . '/src/toolkit-breadcrumbs/block.php' );
}
if ( isset( $this->active_classes['uncanny_learndash_toolkit\LearnDashResume'] ) ) {
require_once( dirname( __FILE__ ) . '/src/toolkit-resume-button/block.php' );
}
if ( isset( $this->active_classes['uncanny_learndash_toolkit\FrontendLoginPlus'] ) ) {
require_once( dirname( __FILE__ ) . '/src/toolkit-frontend-login/block.php' );
}
} );
if ( $add_block_scripts ) {
// Enqueue Gutenberg block assets for both frontend + backend
// add_action( 'enqueue_block_assets', function () {
// wp_enqueue_style(
// $this->prefix . '-gutenberg-blocks',
// plugins_url( 'blocks/dist/style-index.css', dirname( __FILE__ ) ),
// array(),
// UNCANNY_TOOLKIT_VERSION
// );
// } );
// Enqueue Gutenberg block assets for backend editor
add_action( 'enqueue_block_editor_assets', function () {
wp_enqueue_script(
$this->prefix . '-gutenberg-blocks-editor',
plugins_url( 'blocks/dist/index.js', dirname( __FILE__ ) ),
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ),
UNCANNY_TOOLKIT_VERSION,
true
);
// Get only the Free blocks
$free_blocks = array_values( array_map( function ( $block ) {
// Remove the prefix
return str_replace( 'uncanny_learndash_toolkit\\', '', $block );
}, array_filter( $this->active_classes, function ( $block ) {
// Filter only Pro blocks
return strpos( $block, 'uncanny_learndash_toolkit\\' ) !== false;
} ) ) );
wp_add_inline_script( $this->prefix . '-gutenberg-blocks-editor', 'var ultGutenbergModules = ' . json_encode( $free_blocks ), 'before' );
// Add support for Uncanny Toolkit Pro for LearnDash > 3.4.3
if ( defined( 'UNCANNY_TOOLKIT_PRO_VERSION' ) ) {
if ( version_compare( UNCANNY_TOOLKIT_PRO_VERSION, '3.4.3', '<' ) ) {
// Add a variable with the old data
wp_localize_script( $this->prefix . '-gutenberg-blocks-editor', 'ultpModules', array(
'active' => $this->active_classes,
) );
}
}
wp_enqueue_style(
$this->prefix . '-gutenberg-blocks-editor',
plugins_url( 'blocks/dist/index.css', dirname( __FILE__ ) ),
array( 'wp-edit-blocks' ),
UNCANNY_TOOLKIT_VERSION
);
} );
}
if ( version_compare( get_bloginfo( 'version' ), '5.8', '<' ) ) {
// Legacy filter
// Create custom block category
add_filter( 'block_categories', array( $this, 'block_categories' ), 10, 2 );
} else {
// Create custom block category
add_filter( 'block_categories_all', array( $this, 'block_categories' ), 10, 2 );
}
}
}
/**
* @param $categories
* @param $post
*
* @return array
*/
public function block_categories( $categories, $post ) {
return array_merge(
$categories,
array(
array(
'slug' => 'uncanny-learndash-toolkit',
'title' => __( 'Uncanny Toolkit for LearnDash', 'uncanny-learndash-toolkit' ),
),
)
);
}
}