PluginProbe
Commerce7 for WordPress / trunk
Commerce7 for WordPress vtrunk
1.8.1 1.8.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.3.4 1.3.5 1.4.0 1.4.1 All 37 releases
wp-commerce7 / includes / class-c7wp-widgets.php

class-c7wp-widgets.php in Commerce7 for WordPress trunk, at includes/class-c7wp-widgets.php

525 lines 16.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Central Commerce7 widget manifest.
4 *
5 * @package wp-commerce7
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 return;
10 }
11
12 /**
13 * Widget registry shared across page builders.
14 */
15 class C7WP_Widgets {
16
17 /**
18 * Widget definitions.
19 *
20 * @return array
21 */
22 public static function get_manifest() {
23 return array(
24 'default' => array(
25 'c7_type' => 'default',
26 'title' => __( 'Default Content', 'wp-commerce7' ),
27 'description' => __( 'Display the default Commerce7 content area.', 'wp-commerce7' ),
28 'v2' => true,
29 'beta' => true,
30 'custom_render' => false,
31 'shortcode_base' => 'c7wp_default',
32 'fields' => array(),
33 ),
34 'personalization' => array(
35 'c7_type' => 'personalization',
36 'title' => __( 'Personalization Block', 'wp-commerce7' ),
37 'description' => __( 'Add a Commerce7 personalization block to your layout.', 'wp-commerce7' ),
38 'v2' => true,
39 'beta' => true,
40 'custom_render' => false,
41 'shortcode_base' => 'c7wp_personalization',
42 'fields' => array(
43 'data' => array(
44 'label' => __( 'Block Code', 'wp-commerce7' ),
45 'type' => 'text',
46 'description' => __( 'Enter the personalization block code.', 'wp-commerce7' ),
47 ),
48 ),
49 ),
50 'buy' => array(
51 'c7_type' => 'buy',
52 'title' => __( 'Buy Button (SKU)', 'wp-commerce7' ),
53 'description' => __( 'Add a Commerce7 buy button for a variant SKU.', 'wp-commerce7' ),
54 'v2' => false,
55 'beta' => true,
56 'custom_render' => false,
57 'shortcode_base' => 'c7wp_buy',
58 'fields' => array(
59 'data' => array(
60 'label' => __( 'Variant SKU', 'wp-commerce7' ),
61 'type' => 'text',
62 'description' => __( 'Enter the variant SKU.', 'wp-commerce7' ),
63 ),
64 ),
65 ),
66 'buyslug' => array(
67 'c7_type' => 'buyslug',
68 'title' => __( 'Buy Button', 'wp-commerce7' ),
69 'description' => __( 'Add a Commerce7 buy button for a product slug.', 'wp-commerce7' ),
70 'v2' => true,
71 'beta' => true,
72 'custom_render' => false,
73 'shortcode_base' => 'c7wp_buyslug',
74 'fields' => array(
75 'data' => array(
76 'label' => __( 'Product Slug', 'wp-commerce7' ),
77 'type' => 'text',
78 'description' => __( 'Enter the product slug.', 'wp-commerce7' ),
79 ),
80 ),
81 ),
82 'subscribe' => array(
83 'c7_type' => 'subscribe',
84 'title' => __( 'Subscribe Form', 'wp-commerce7' ),
85 'description' => __( 'Add a Commerce7 email subscribe form.', 'wp-commerce7' ),
86 'v2' => true,
87 'beta' => true,
88 'custom_render' => false,
89 'shortcode_base' => 'c7wp_subscribe',
90 'fields' => array(
91 'data' => array(
92 'label' => __( 'Show Name Fields?', 'wp-commerce7' ),
93 'type' => 'select',
94 'options' => array(
95 'false' => __( 'No', 'wp-commerce7' ),
96 'true' => __( 'Yes', 'wp-commerce7' ),
97 ),
98 'default' => 'false',
99 ),
100 ),
101 ),
102 'collection' => array(
103 'c7_type' => 'collection',
104 'title' => __( 'Collection', 'wp-commerce7' ),
105 'description' => __( 'Add a Commerce7 collection grid to your layout.', 'wp-commerce7' ),
106 'v2' => true,
107 'beta' => true,
108 'custom_render' => false,
109 'shortcode_base' => 'c7wp_collection',
110 'fields' => array(
111 'data' => array(
112 'label' => __( 'Collection Slug', 'wp-commerce7' ),
113 'type' => 'text',
114 'description' => __( 'Enter the collection slug.', 'wp-commerce7' ),
115 ),
116 ),
117 ),
118 'login' => array(
119 'c7_type' => 'login',
120 'title' => __( 'Login/Logout Link', 'wp-commerce7' ),
121 'description' => __( 'Add a Commerce7 login and logout link.', 'wp-commerce7' ),
122 'v2' => true,
123 'beta' => true,
124 'custom_render' => false,
125 'shortcode_base' => 'c7wp_login',
126 'fields' => array(),
127 ),
128 'cart' => array(
129 'c7_type' => 'cart',
130 'title' => __( 'Cart', 'wp-commerce7' ),
131 'description' => __( 'Add a Commerce7 cart icon or flyout.', 'wp-commerce7' ),
132 'v2' => true,
133 'beta' => true,
134 'custom_render' => false,
135 'shortcode_base' => 'c7wp_cart',
136 'fields' => array(),
137 ),
138 'reservation' => array(
139 'c7_type' => 'reservation',
140 'title' => __( 'Reservation Widget', 'wp-commerce7' ),
141 'description' => __( 'Add a Commerce7 reservation availability widget.', 'wp-commerce7' ),
142 'v2' => true,
143 'beta' => true,
144 'custom_render' => false,
145 'shortcode_base' => 'c7wp_reservation',
146 'fields' => array(
147 'data' => array(
148 'label' => __( 'Experience Slug', 'wp-commerce7' ),
149 'type' => 'text',
150 'description' => __( 'Enter the experience slug.', 'wp-commerce7' ),
151 ),
152 ),
153 ),
154 'form' => array(
155 'c7_type' => 'form',
156 'title' => __( 'General Form', 'wp-commerce7' ),
157 'description' => __( 'Add a Commerce7 custom form.', 'wp-commerce7' ),
158 'v2' => true,
159 'beta' => true,
160 'custom_render' => false,
161 'shortcode_base' => 'c7wp_form',
162 'fields' => array(
163 'data' => array(
164 'label' => __( 'Form Slug', 'wp-commerce7' ),
165 'type' => 'text',
166 'description' => __( 'Enter the form slug.', 'wp-commerce7' ),
167 ),
168 ),
169 ),
170 'joinnow' => array(
171 'c7_type' => 'joinnow',
172 'title' => __( 'Club Join Button', 'wp-commerce7' ),
173 'description' => __( 'Add a Commerce7 club join or edit membership button.', 'wp-commerce7' ),
174 'v2' => true,
175 'beta' => true,
176 'custom_render' => false,
177 'shortcode_base' => 'c7wp_joinnow',
178 'fields' => array(
179 'data' => array(
180 'label' => __( 'Club Slug', 'wp-commerce7' ),
181 'type' => 'text',
182 'description' => __( 'Enter the club slug.', 'wp-commerce7' ),
183 ),
184 'join-text' => array(
185 'label' => __( 'Join Club Text', 'wp-commerce7' ),
186 'type' => 'text',
187 'description' => __( 'Optional text for the join button.', 'wp-commerce7' ),
188 ),
189 'edit-text' => array(
190 'label' => __( 'Edit Membership Text', 'wp-commerce7' ),
191 'type' => 'text',
192 'description' => __( 'Optional text for the edit membership button.', 'wp-commerce7' ),
193 ),
194 ),
195 ),
196 'collectionlist' => array(
197 'c7_type' => 'collectionlist',
198 'title' => __( 'Collection List', 'wp-commerce7' ),
199 'description' => __( 'Display a Commerce7 collection list.', 'wp-commerce7' ),
200 'v2' => true,
201 'beta' => false,
202 'custom_render' => false,
203 'shortcode_base' => 'c7wp_collectionlist',
204 'fields' => array(),
205 ),
206 'clubselector' => array(
207 'c7_type' => null,
208 'title' => __( 'Club Selector', 'wp-commerce7' ),
209 'description' => __( 'Add a Commerce7 club selector with radio or select display.', 'wp-commerce7' ),
210 'v2' => true,
211 'beta' => false,
212 'custom_render' => true,
213 'shortcode_base' => 'c7wp_clubselector',
214 'fields' => array(),
215 ),
216 'clubselector-v2' => array(
217 'c7_type' => null,
218 'title' => __( 'Club Selector v2', 'wp-commerce7' ),
219 'description' => __( 'Club selector with Commerce7 join buttons (no redirect).', 'wp-commerce7' ),
220 'v2' => true,
221 'beta' => false,
222 'custom_render' => true,
223 'shortcode_base' => 'c7wp_clubselector_v2',
224 'fields' => array(),
225 ),
226 'loginform' => array(
227 'c7_type' => 'loginform',
228 'title' => __( 'Login Form', 'wp-commerce7' ),
229 'description' => __( 'Add a Commerce7 login form.', 'wp-commerce7' ),
230 'v2' => true,
231 'beta' => true,
232 'custom_render' => false,
233 'shortcode_base' => 'c7wp_loginform',
234 'fields' => array(
235 'data' => array(
236 'label' => __( 'Redirect To Path', 'wp-commerce7' ),
237 'type' => 'text',
238 'description' => __( 'Optional path to redirect after login.', 'wp-commerce7' ),
239 ),
240 ),
241 ),
242 'quickshop' => array(
243 'c7_type' => 'quickshop',
244 'title' => __( 'Quick Shop Form', 'wp-commerce7' ),
245 'description' => __( 'Add a Commerce7 quick shop form for a collection.', 'wp-commerce7' ),
246 'v2' => false,
247 'beta' => true,
248 'custom_render' => false,
249 'shortcode_base' => 'c7wp_quickshop',
250 'fields' => array(
251 'data' => array(
252 'label' => __( 'Collection Slug', 'wp-commerce7' ),
253 'type' => 'text',
254 'description' => __( 'Enter the collection slug.', 'wp-commerce7' ),
255 ),
256 ),
257 ),
258 'createaccount' => array(
259 'c7_type' => 'createaccount',
260 'title' => __( 'Create Account Form', 'wp-commerce7' ),
261 'description' => __( 'Add a Commerce7 create account form.', 'wp-commerce7' ),
262 'v2' => false,
263 'beta' => true,
264 'custom_render' => false,
265 'shortcode_base' => 'c7wp_createaccount',
266 'fields' => array(
267 'data' => array(
268 'label' => __( 'Redirect To', 'wp-commerce7' ),
269 'type' => 'text',
270 'description' => __( 'Optional path to redirect after account creation.', 'wp-commerce7' ),
271 ),
272 ),
273 ),
274 );
275 }
276
277 /**
278 * Build a PHP class suffix from a hyphenated widget slug.
279 *
280 * @param string $slug Widget slug.
281 * @return string
282 */
283 public static function get_builder_class_suffix( $slug ) {
284 $parts = explode( '-', $slug );
285 $parts = array_map( 'ucfirst', $parts );
286
287 return implode( '_', $parts );
288 }
289
290 /**
291 * Get widget slugs for the active widget version.
292 *
293 * @param string $widgetsver Widget version setting.
294 * @param string|null $builder Optional builder key (gutenberg, elementor, beaverbuilder, wpbakery, themeco).
295 * @return array
296 */
297 public static function get_slugs_for_version( $widgetsver, $builder = null ) {
298 $is_v2 = in_array( $widgetsver, array( 'v2', 'v2-compat' ), true );
299 $slugs = array();
300 $manifest = self::get_manifest();
301
302 foreach ( $manifest as $slug => $widget ) {
303 if ( $builder && ! empty( $widget['builders'] ) && ! in_array( $builder, $widget['builders'], true ) ) {
304 continue;
305 }
306
307 if ( $is_v2 && ! empty( $widget['v2'] ) ) {
308 $slugs[] = $slug;
309 } elseif ( ! $is_v2 && ! empty( $widget['beta'] ) ) {
310 $slugs[] = $slug;
311 }
312 }
313
314 return $slugs;
315 }
316
317 /**
318 * Get a single widget definition.
319 *
320 * @param string $slug Widget slug.
321 * @return array|null
322 */
323 public static function get_widget( $slug ) {
324 $manifest = self::get_manifest();
325 return isset( $manifest[ $slug ] ) ? $manifest[ $slug ] : null;
326 }
327
328 /**
329 * Build a [c7wp] shortcode string from type and attributes.
330 *
331 * @param string $type Widget type.
332 * @param array $atts Attributes.
333 * @return string
334 */
335 public static function build_shortcode( $type, $atts = array() ) {
336 $parts = array( 'type="' . esc_attr( $type ) . '"' );
337
338 foreach ( array( 'data', 'join-text', 'edit-text' ) as $key ) {
339 if ( ! empty( $atts[ $key ] ) ) {
340 $parts[] = $key . '="' . esc_attr( $atts[ $key ] ) . '"';
341 }
342 }
343
344 return '[c7wp ' . implode( ' ', $parts ) . ']';
345 }
346
347 /**
348 * All shortcode tag patterns used by Commerce7 widgets.
349 *
350 * @return array
351 */
352 public static function get_shortcode_tags() {
353 $tags = array( 'c7wp' );
354 foreach ( self::get_manifest() as $widget ) {
355 if ( ! empty( $widget['shortcode_base'] ) ) {
356 $tags[] = $widget['shortcode_base'];
357 }
358 }
359 return array_unique( $tags );
360 }
361
362 /**
363 * Detect Commerce7 widgets in post content.
364 *
365 * @param string $content Post content.
366 * @return bool
367 */
368 public static function content_has_c7_widgets( $content ) {
369 if ( empty( $content ) ) {
370 return false;
371 }
372
373 foreach ( self::get_shortcode_tags() as $tag ) {
374 if ( has_shortcode( $content, $tag ) ) {
375 return true;
376 }
377 }
378
379 if ( false !== strpos( $content, '<!-- wp:c7wp/' ) ) {
380 return true;
381 }
382
383 if ( false !== strpos( $content, 'club-selector-wrapper' ) ) {
384 return true;
385 }
386
387 if ( false !== strpos( $content, 'club-selector-v2-wrapper' ) ) {
388 return true;
389 }
390
391 return false;
392 }
393
394 /**
395 * Detect which widget slugs appear in content.
396 *
397 * @param string $content Post content.
398 * @return array
399 */
400 public static function get_slugs_in_content( $content ) {
401 $found = array();
402
403 if ( empty( $content ) ) {
404 return $found;
405 }
406
407 $manifest = self::get_manifest();
408 foreach ( $manifest as $slug => $widget ) {
409 if ( ! empty( $widget['shortcode_base'] ) && has_shortcode( $content, $widget['shortcode_base'] ) ) {
410 $found[] = $slug;
411 }
412 if ( has_shortcode( $content, 'c7wp' ) && 'default' !== $slug ) {
413 // Legacy combined shortcode may contain any type.
414 if ( ! in_array( 'legacy', $found, true ) ) {
415 $found[] = 'legacy';
416 }
417 }
418 if ( false !== strpos( $content, '<!-- wp:c7wp/' . $slug ) ) {
419 $found[] = $slug;
420 }
421 }
422
423 if ( false !== strpos( $content, 'club-selector-wrapper' ) ) {
424 $found[] = 'clubselector';
425 }
426
427 if ( false !== strpos( $content, 'club-selector-v2-wrapper' ) || false !== strpos( $content, '<!-- wp:c7wp/clubselector-v2' ) ) {
428 $found[] = 'clubselector-v2';
429 }
430
431 return array_unique( $found );
432 }
433
434 /**
435 * Register and enqueue club selector frontend assets.
436 *
437 * @param C7WP|null $instance Plugin instance for settings.
438 */
439 public static function register_clubselector_assets( $instance = null ) {
440 if ( null === $instance ) {
441 $instance = C7WP::getInstance();
442 }
443
444 $frontend_js_path = 'blocks-v2/clubselector/frontend.js';
445 $frontend_css_path = 'blocks-v2/clubselector/frontend.css';
446 $handle = 'c7wp-clubselector-frontend';
447
448 if ( file_exists( C7WP_ROOT . '/includes/gutenberg/' . $frontend_js_path ) ) {
449 wp_register_script(
450 $handle,
451 plugins_url( $frontend_js_path, C7WP_ROOT . '/includes/gutenberg/load.php' ),
452 array(),
453 C7WP_VERSION,
454 true
455 );
456 wp_localize_script( $handle, 'c7wp_settings', $instance->get_public_js_settings() );
457 }
458
459 if ( file_exists( C7WP_ROOT . '/includes/gutenberg/' . $frontend_css_path ) ) {
460 wp_register_style(
461 $handle,
462 plugins_url( $frontend_css_path, C7WP_ROOT . '/includes/gutenberg/load.php' ),
463 array(),
464 C7WP_VERSION
465 );
466 }
467 }
468
469 /**
470 * Enqueue club selector frontend assets.
471 */
472 public static function enqueue_clubselector_assets() {
473 self::register_clubselector_assets();
474 if ( wp_script_is( 'c7wp-clubselector-frontend', 'registered' ) ) {
475 wp_enqueue_script( 'c7wp-clubselector-frontend' );
476 }
477 if ( wp_style_is( 'c7wp-clubselector-frontend', 'registered' ) ) {
478 wp_enqueue_style( 'c7wp-clubselector-frontend' );
479 }
480 }
481
482 /**
483 * Register and enqueue club selector v2 frontend assets.
484 *
485 * @param C7WP|null $instance Plugin instance for settings.
486 */
487 public static function register_clubselector_v2_assets( $instance = null ) {
488 $frontend_js_path = 'blocks-v2/clubselector-v2/frontend.js';
489 $frontend_css_path = 'blocks-v2/clubselector-v2/frontend.css';
490 $handle = 'c7wp-clubselector-v2-frontend';
491
492 if ( file_exists( C7WP_ROOT . '/includes/gutenberg/' . $frontend_js_path ) ) {
493 wp_register_script(
494 $handle,
495 plugins_url( $frontend_js_path, C7WP_ROOT . '/includes/gutenberg/load.php' ),
496 array(),
497 C7WP_VERSION,
498 true
499 );
500 }
501
502 if ( file_exists( C7WP_ROOT . '/includes/gutenberg/' . $frontend_css_path ) ) {
503 wp_register_style(
504 $handle,
505 plugins_url( $frontend_css_path, C7WP_ROOT . '/includes/gutenberg/load.php' ),
506 array(),
507 C7WP_VERSION
508 );
509 }
510 }
511
512 /**
513 * Enqueue club selector v2 frontend assets.
514 */
515 public static function enqueue_clubselector_v2_assets() {
516 self::register_clubselector_v2_assets();
517 if ( wp_script_is( 'c7wp-clubselector-v2-frontend', 'registered' ) ) {
518 wp_enqueue_script( 'c7wp-clubselector-v2-frontend' );
519 }
520 if ( wp_style_is( 'c7wp-clubselector-v2-frontend', 'registered' ) ) {
521 wp_enqueue_style( 'c7wp-clubselector-v2-frontend' );
522 }
523 }
524 }
525