Repository navigation
Expand file tree
/
Copy pathpatternswp.php
More file actions
336 lines (289 loc) · 10.2 KB
/
Copy pathpatternswp.php
File metadata and controls
336 lines (289 loc) · 10.2 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* Plugin Name: PatternsWP
* Plugin URI: https://thepatternswp.com
* Description: A growing library of ready-made block patterns can help you build websites faster in no time.
* Author: PatternsWP
* Author URI: https://thepatternswp.com
* Version: 1.1.0
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: patternswp
* Domain Path: /languages
*/
// If this file is called directly, abort.
if (!defined('WPINC')) {
die;
}
// Define plugin constants.
define('PWP_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('PWP_PLUGIN_URL', plugin_dir_url(__FILE__));
define('PWP_P_VERSION', '1.1.0');
define('PWP_PLUGIN_FILE', __FILE__);
define('PWP_ABSPATH', dirname(__FILE__) . '/');
define('PWP_VERSION', get_file_data(__FILE__, ['Version'])[0]);
define('PATTERSWP_PLUGIN_API_URL', 'https://api.lemonsqueezy.com/v1/licenses');
// Include necessary files.
require_once PWP_PLUGIN_DIR . 'includes/class-patternswp-admin.php';
require_once PWP_PLUGIN_DIR . 'includes/class-patternswp-api.php';
require_once PWP_PLUGIN_DIR . 'includes/lib/class-patterns-license.php';
/**
* Enqueue assets for Block Editor.
*/
function patternswp_enqueue_editor_assets() {
$patternswp_api_section = PatternsWP_API_Section::get_instance();
$script_path = 'assets/js/patternswp-editor.js';
$script_deps = array(
'lodash',
'wp-a11y',
'wp-block-editor',
'wp-blocks',
'wp-components',
'wp-compose',
'wp-data',
'wp-dom-ready',
'wp-element',
'wp-i18n',
'wp-notices',
'wp-plugins',
'wp-editor',
'wp-primitives',
);
wp_enqueue_script(
'patternswp-editor-scripts',
PWP_PLUGIN_URL . $script_path,
$script_deps,
patternswp_asset_version( $script_path ),
true
);
$localize_data = array(
'isLicenseActive' => $patternswp_api_section->is_license_active(),
'externalPatterns' => array(),
'patternCategories' => $patternswp_api_section->get_patternswp_category_type(),
'patternsNonce' => wp_create_nonce( 'patternswp_nonce' ),
'libraryComplete' => $patternswp_api_section->is_catalog_ready(),
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
);
wp_localize_script( 'patternswp-editor-scripts', 'patternsWpData', $localize_data );
patternswp_register_editor_style();
wp_enqueue_style( 'patternswp-editor-styles' );
}
add_action( 'enqueue_block_editor_assets', 'patternswp_enqueue_editor_assets' );
/**
* Register shared editor stylesheet handle.
*/
function patternswp_register_editor_style() {
$style_path = 'assets/css/patternswp-editor.css';
wp_register_style(
'patternswp-editor-styles',
PWP_PLUGIN_URL . $style_path,
array( 'wp-components' ),
patternswp_asset_version( $style_path )
);
}
/**
* Load styles into the iframed editor canvas (block content lives there in WP 6.3+).
* enqueue_block_editor_assets alone only reaches the parent chrome / modal portals.
*/
function patternswp_enqueue_canvas_styles() {
if ( ! is_admin() ) {
return;
}
patternswp_register_editor_style();
wp_enqueue_style( 'patternswp-editor-styles' );
}
add_action( 'enqueue_block_assets', 'patternswp_enqueue_canvas_styles' );
/**
* Register the PatternsWP Pattern Library launcher block.
*
* Renders nothing on the front end — it is an editor-only entry point
* for browsing and inserting patterns (similar to a pattern inserter).
*/
function patternswp_register_blocks() {
patternswp_register_editor_style();
register_block_type(
'patternswp/library',
array(
'api_version' => 3,
'title' => __( 'PatternsWP Pattern Library', 'patternswp' ),
'description' => __( 'Browse the PatternsWP pattern library and insert patterns into your page.', 'patternswp' ),
'category' => 'widgets',
'icon' => 'layout',
'keywords' => array( 'patternswp', 'patterns', 'library', 'templates', 'blocks' ),
'supports' => array(
'html' => false,
'multiple' => true,
'reusable' => false,
),
'editor_style' => 'patternswp-editor-styles',
'render_callback' => '__return_empty_string',
)
);
}
add_action( 'init', 'patternswp_register_blocks' );
/**
* Get asset file data (legacy build/*.asset.php support).
*/
function patternswp_get_asset_file( $filepath ) {
$asset_path = PWP_ABSPATH . $filepath . '.asset.php';
return file_exists( $asset_path ) ? require $asset_path : array(
'dependencies' => array(),
'version' => PWP_VERSION,
);
}
/**
* Cache-busting version for ready-to-use assets.
*
* @param string $relative_path Path relative to the plugin root.
* @return string
*/
function patternswp_asset_version( $relative_path ) {
$absolute = PWP_PLUGIN_DIR . ltrim( $relative_path, '/' );
if ( file_exists( $absolute ) ) {
return (string) filemtime( $absolute );
}
return PWP_VERSION;
}
// Plugin activation hook.
register_activation_hook(__FILE__, 'patternswp_schedule_daily_cron');
function patternswp_schedule_daily_cron() {
wp_clear_scheduled_hook( 'patternswp_hourly_transient_load' );
if ( ! wp_next_scheduled( 'patternswp_daily_transient_load' ) ) {
wp_schedule_event( time() + DAY_IN_SECONDS, 'daily', 'patternswp_daily_transient_load' );
}
if ( ! wp_next_scheduled( 'patternswp_library_warm' ) ) {
wp_schedule_single_event( time() + 3, 'patternswp_library_warm' );
}
}
// Plugin deactivation hook.
register_deactivation_hook(__FILE__, 'patternswp_remove_daily_cron');
function patternswp_remove_daily_cron() {
wp_clear_scheduled_hook( 'patternswp_hourly_transient_load' );
wp_clear_scheduled_hook( 'patternswp_daily_transient_load' );
wp_clear_scheduled_hook( 'patternswp_library_warm' );
}
/**
* AJAX handler to fetch patterns.
*/
function patternswp_fetch_patterns_handler() {
// Verify nonce
if (!isset($_POST['nonce'])) {
wp_send_json_error('Missing nonce');
wp_die();
}
$nonce = sanitize_text_field(wp_unslash($_POST['nonce']));
if (!wp_verify_nonce($nonce, 'patternswp_nonce')) {
wp_send_json_error('Invalid nonce');
wp_die();
}
if (!current_user_can('edit_posts')) {
wp_send_json_error('Forbidden');
wp_die();
}
patternswp_release_ajax_lock();
if (!isset($_POST['page']) || !isset($_POST['patternsPerPage'])) {
wp_send_json_error('Missing parameters');
wp_die();
}
$page = intval($_POST['page']);
$patterns_per_page = intval($_POST['patternsPerPage']);
$search = sanitize_text_field(wp_unslash($_POST['search'] ?? ''));
$category = sanitize_text_field(wp_unslash($_POST['category'] ?? ''));
$patternswp_api_section = PatternsWP_API_Section::get_instance();
try {
$localize_data_ajax = $patternswp_api_section->query_library_page($page, $patterns_per_page, $search, $category);
} catch ( \Throwable $e ) {
wp_send_json_error( 'Failed to fetch patterns' );
wp_die();
}
if (is_array($localize_data_ajax)) {
wp_send_json_success($localize_data_ajax);
} else {
wp_send_json_error('Failed to fetch patterns');
}
wp_die();
}
add_action('wp_ajax_fetch_patterns', 'patternswp_fetch_patterns_handler');
/**
* AJAX: continue downloading the catalog in short bursts.
*/
function patternswp_warm_library_handler() {
if (!isset($_POST['nonce'])) {
wp_send_json_error('Missing nonce');
}
$nonce = sanitize_text_field(wp_unslash($_POST['nonce']));
if (!wp_verify_nonce($nonce, 'patternswp_nonce')) {
wp_send_json_error('Invalid nonce');
}
if (!current_user_can('edit_posts')) {
wp_send_json_error('Forbidden');
}
patternswp_release_ajax_lock();
$api = PatternsWP_API_Section::get_instance();
$mode = isset($_POST['mode']) ? sanitize_key(wp_unslash($_POST['mode'])) : 'sync';
$force = ! empty($_POST['force']);
try {
if ( 'check' === $mode ) {
$status = $api->maybe_pull_remote_updates( $force );
} else {
$status = $api->warm_library_for_request();
}
} catch ( \Throwable $e ) {
wp_send_json_error('Failed to refresh patterns');
}
wp_send_json_success(is_array($status) ? $status : array());
}
add_action('wp_ajax_patternswp_warm_library', 'patternswp_warm_library_handler');
/**
* Warm the library after install or plugin update.
*/
function patternswp_maybe_upgrade_library() {
if (!is_admin()) {
return;
}
$installed = get_option('patternswp_installed_version', '');
if ($installed === PWP_P_VERSION) {
return;
}
update_option('patternswp_installed_version', PWP_P_VERSION, false);
PatternsWP_API_Section::get_instance()->maybe_schedule_warm();
}
add_action('admin_init', 'patternswp_maybe_upgrade_library', 5);
/**
* Add plugin action links.
*/
function patternswp_plugin_action_links($links, $file) {
if ($file === plugin_basename(__FILE__)) {
$is_active = PatternsWP_API_Section::get_instance()->is_license_active();
$support_link = '<a href="https://thepatternswp.com/contact/" target="_blank">Support</a>';
array_unshift($links, $support_link);
if (!$is_active) {
$upgrade_link = '<a href="https://thepatternswp.com/pricing/" target="_blank">Upgrade to Pro</a>';
array_unshift($links, $upgrade_link);
}
}
return $links;
}
add_filter('plugin_action_links', 'patternswp_plugin_action_links', 10, 2);
/**
* Add custom plugin meta links.
*/
function patternswp_add_plugin_meta_links($links, $file) {
if ($file === plugin_basename(__FILE__)) {
$links[] = '<a href="https://thepatternswp.com/suggest-feature" target="_blank">Suggest a Feature</a>';
}
return $links;
}
add_filter('plugin_row_meta', 'patternswp_add_plugin_meta_links', 10, 2);
/**
* Let long catalog AJAX run without blocking other editor requests.
*/
function patternswp_release_ajax_lock() {
if ( function_exists( 'session_status' ) && PHP_SESSION_ACTIVE === session_status() ) {
session_write_close();
return;
}
if ( session_id() ) {
session_write_close();
}
}