Repository navigation
Expand file tree
/
Copy pathpatterns.php
More file actions
167 lines (142 loc) · 3.59 KB
/
Copy pathpatterns.php
File metadata and controls
167 lines (142 loc) · 3.59 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare( strict_types=1 );
namespace Blockify\Plugin;
use Blockify\Utilities\Str;
use WP_Post;
use function add_filter;
use function array_merge;
use function get_stylesheet;
use function get_the_terms;
use function in_array;
use function register_block_pattern;
use function sprintf;
use function str_contains;
use function wp_json_file_decode;
use function wp_list_pluck;
/**
* Returns a list of child theme slugs.
*
* @since 1.5.0
*
* @return string[]
*/
function get_child_themes(): array {
return [
'adaptify',
'blockify',
'brandify',
'codeify',
'creatify',
'launchify',
'mintify',
'saasify',
];
}
add_filter( 'blockify_pattern_export_dir', __NAMESPACE__ . '\\patterns_export_dir', 10, 3 );
/**
* Adds pro pattern export directory.
*
* @since 1.0.0
*
* @param string $default_dir Filtered pattern directory.
* @param ?WP_Post $post Post object (optional).
* @param string $content Replaced content (optional).
*
* @return string
*/
function patterns_export_dir( string $default_dir, ?WP_Post $post = null, string $content = '' ): string {
$themes = get_child_themes();
$stylesheet = get_stylesheet();
if ( ! in_array( $stylesheet, $themes, true ) ) {
return $default_dir;
}
$pro_dir = DIR . "patterns/$stylesheet";
$categories = get_the_terms( $post, 'wp_pattern_category' );
if ( in_array( 'pro', wp_list_pluck( $categories, 'slug' ), true ) ) {
$default_dir = $pro_dir;
}
if ( str_contains( $content, 'wp:blockify/' ) ) {
$default_dir = $pro_dir;
}
if ( str_contains( $post->post_title, 'Utility Dark Mode Toggle' ) ) {
$default_dir = DIR . "patterns";
}
return $default_dir;
}
add_filter( 'blockify_pattern_dirs', __NAMESPACE__ . '\\add_pro_patterns' );
/**
* Add Pro patterns.
*
* @since 1.0.0
*
* @param string[] $dirs The pattern directories.
*
* @return string[]
*/
function add_pro_patterns( array $dirs ): array {
if ( ! is_license_active() ) {
return $dirs;
}
$patterns_dir = get_cache_dir( 'patterns' );
$dirs = array_merge(
[
$patterns_dir . 'blockify',
],
$dirs
);
$dirs[] = $patterns_dir . get_stylesheet();
return $dirs;
}
add_action( 'init', __NAMESPACE__ . '\\add_placeholder_patterns', 11 );
/**
* Adds placeholder patterns.
*
* @since 1.5.0
*
* @return void
*/
function add_placeholder_patterns(): void {
if ( is_license_active() ) {
return;
}
$patterns_json_file = DIR . 'patterns.json';
if ( ! file_exists( $patterns_json_file ) ) {
return;
}
$html = <<<HTML
<!-- wp:group {"align":"full","layout":{"type":"default"}} -->
<div class="wp-block-group alignfull">
<!-- wp:image {"sizeSlug":"full","align":"full","style":{"minWidth":{"all":"100vw"}}} -->
<figure class="wp-block-image alignfull size-full">
<img src="https://blockifywp.com/wp-content/themes/blockifywp/screenshots/%s/pro/%s.webp" alt="%s"/>
</figure>
<!-- /wp:image -->
</div>
<!-- /wp:group -->
HTML;
$stylesheet = get_stylesheet();
$patterns_json = wp_json_file_decode( $patterns_json_file );
foreach ( $patterns_json as $theme => $patterns ) {
if ( $theme !== $stylesheet ) {
continue;
}
foreach ( $patterns as $pattern ) {
$category = explode( '-', $pattern )[0] ?? 'text';
$content = sprintf(
$html,
$theme,
$pattern,
Str::title_case( $theme . ' ' . $pattern )
);
register_block_pattern(
"$theme/$pattern-pro",
[
'title' => Str::title_case( $theme . ' ' . $pattern ) . ' (Pro)',
'content' => $content,
'categories' => [ $category ],
'description' => 'This is a placeholder pattern. Activate your license to unlock the full pattern.',
]
);
}
}
}