-
Notifications
You must be signed in to change notification settings - Fork 20
/
snow-monkey-blocks.php
executable file
·232 lines (205 loc) · 5.64 KB
/
snow-monkey-blocks.php
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* Plugin name: Snow Monkey Blocks
* Version: 22.3.2
* Description: Gutenberg blocks collection made by MonkeyWrench.
* Author: inc2734
* Author URI: https://2inc.org
* License: GPL2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: snow-monkey-blocks
*
* @package snow-monkey-blocks
* @author inc2734
* @license GPL-2.0+
*/
namespace Snow_Monkey\Plugin\Blocks;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class Bootstrap {
/**
* constructor
*/
public function __construct() {
add_action( 'plugins_loaded', array( $this, '_bootstrap' ) );
}
/**
* bootstrap
*/
public function _bootstrap() {
new App\Setup\TextDomain();
new App\Setup\Assets();
new App\Setup\Blocks();
new App\Setup\JsonLd();
new App\Controller\Manager();
add_filter( 'block_categories_all', array( $this, '_block_categories_all' ) );
add_action( 'init', array( $this, '_register_blocks' ) );
add_action( 'add_meta_boxes', array( $this, '_add_pr_meta_box' ) );
add_filter( 'the_content', array( $this, '_the_content_for_slider' ), 11 );
}
/**
* Add block categories.
*
* @param array $categories Array of block categories.
* @return array
*/
public function _block_categories_all( $categories ) {
$slugs = array_column( $categories, 'slug' );
if ( ! in_array( 'smb', $slugs, true ) ) {
$categories[] = array(
'slug' => 'smb',
'title' => __( 'Snow Monkey Blocks', 'snow-monkey-blocks' )
. ' '
. __( '[Common]', 'snow-monkey-blocks' ),
);
}
if ( ! in_array( 'smb-section', $slugs, true ) ) {
$categories[] = array(
'slug' => 'smb-section',
'title' => __( 'Snow Monkey Blocks', 'snow-monkey-blocks' )
. ' '
. __( '[Sections]', 'snow-monkey-blocks' ),
);
}
if ( ! in_array( 'smb-layout', $slugs, true ) ) {
$categories[] = array(
'slug' => 'smb-layout',
'title' => __( 'Snow Monkey Blocks', 'snow-monkey-blocks' )
. ' '
. __( '[Layout]', 'snow-monkey-blocks' ),
);
}
if ( ! in_array( 'smb-deprecated', $slugs, true ) ) {
$categories[] = array(
'slug' => 'smb-deprecated',
'title' => __( 'Snow Monkey Blocks', 'snow-monkey-blocks' )
. ' '
. __( '[Deprecated]', 'snow-monkey-blocks' ),
);
}
return $categories;
}
/**
* Register blocks.
*/
public function _register_blocks() {
$iterator = new RecursiveDirectoryIterator( SNOW_MONKEY_BLOCKS_DIR_PATH . '/dist/blocks', FilesystemIterator::SKIP_DOTS );
$iterator = new RecursiveIteratorIterator( $iterator );
foreach ( $iterator as $file ) {
if ( ! $file->isFile() ) {
continue;
}
if ( 'index.php' !== $file->getBasename() ) {
continue;
}
include_once realpath( $file->getPathname() );
}
}
/**
* Add meta box for the Snow Monkey PR when the Gutenberg page or not using the Snow Monkey.
*
* @param string $post_type The post type.
* @return void
*/
public function _add_pr_meta_box( $post_type ) {
if ( ! is_block_editor() || is_pro() ) {
return;
}
add_meta_box(
'snow-monkey-pr',
__( '[ PR ] Premium WordPress Theme Snow Monkey', 'snow-monkey-blocks' ),
array( $this, '_pr_meta_box_html' ),
$post_type,
'normal'
);
}
/**
* Display Snow Monkey PR meta box html.
*
* @return void
*/
public function _pr_meta_box_html() {
?>
<p>
<?php
printf(
// translators: %1$s: Start a tag, %2$s: End a tag.
esc_html__( 'Snow Monkey Blocks is optimized for the %1$sSnow Monkey%2$s theme, but it can also be used with other themes.', 'snow-monkey-blocks' ),
'<a href="https://snow-monkey.2inc.org/" target="_blank">',
'</a>'
);
printf(
// translators: %1$s: Start a tag, %2$s: End a tag.
esc_html__( 'When used together with the %1$sSnow Monkey%2$s theme, it can be displayed with the most beautiful balance, and it is displayed on the edit screen with the same design as the front screen.', 'snow-monkey-blocks' ),
'<a href="https://snow-monkey.2inc.org/" target="_blank">',
'</a>'
);
?>
</p>
<?php
}
/**
* Because the data attribute is destroyed by the influence of wptexturize, it corrects it.
*
* @param string $content The post content.
* @return string
*/
public function _the_content_for_slider( $content ) {
$content = preg_replace_callback(
'|data-slick="\{([^}]+?)\}"|',
function ( $matches ) {
$matches[0] = str_replace( '"', '\'', $matches[0] );
$matches[0] = str_replace( '"', '"', $matches[0] );
return $matches[0];
},
$content
);
return $content;
}
}
require_once __DIR__ . '/vendor/autoload.php';
/**
* Directory url of this plugin.
*
* @var string
*/
define( 'SNOW_MONKEY_BLOCKS_DIR_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) );
/**
* Directory path of this plugin.
*
* @var string
*/
define( 'SNOW_MONKEY_BLOCKS_DIR_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
/**
* Whether pro edition.
*
* @return boolean
*/
function is_pro() {
$is_pro = 'snow-monkey' === get_template() || 'snow-monkey/resources' === get_template();
return apply_filters( 'snow_monkey_blocks_pro', $is_pro );
}
/**
* Return true when active the Gutenberg plugin.
*
* @return boolean
*/
function is_gutenberg_page() {
$post = get_post();
if ( ! $post ) {
return false;
}
return function_exists( '\is_gutenberg_page' ) && \is_gutenberg_page();
}
/**
* Return true when the page has block editor.
*
* @return boolean
*/
function is_block_editor() {
return is_gutenberg_page()
|| ( function_exists( '\use_block_editor_for_post' )
&& \use_block_editor_for_post( get_post() ) );
}
new \Snow_Monkey\Plugin\Blocks\Bootstrap();