PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.15.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.15.0
2.15.0 2.14.0 2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 All 82 releases
ablocks / includes / classes / breakpoints.php

breakpoints.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.15.0, at includes/classes/breakpoints.php

108 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks\Classes;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 use ABlocks\Helper;
9
10 /**
11 * Read/write the custom responsive breakpoints (the `breakpoint_custom` key of
12 * the aBlocks settings option) from the block editor, so breakpoints can be
13 * managed from the editor modal as well as the admin settings page.
14 */
15 class Breakpoints {
16
17 public static function init() {
18 $self = new self();
19 add_action( 'wp_ajax_ablocks/breakpoints/get', [ $self, 'ajax_get' ] );
20 add_action( 'wp_ajax_ablocks/breakpoints/save', [ $self, 'ajax_save' ] );
21 }
22
23 /** The stored custom-breakpoint list (raw items). */
24 public static function get_custom() {
25 $c = Helper::get_settings( 'breakpoint_custom', [] );
26 return is_array( $c ) ? array_values( $c ) : [];
27 }
28
29 /** Response payload shared by get + save: the raw list + resolved devices. */
30 private static function payload() {
31 return [
32 'custom' => self::get_custom(),
33 'devices' => Helper::get_responsive_devices(),
34 ];
35 }
36
37 public function ajax_get() {
38 check_ajax_referer( 'ablocks_nonce', 'security' );
39 if ( ! current_user_can( 'edit_posts' ) ) {
40 wp_send_json_error( [ 'message' => 'forbidden' ], 403 );
41 }
42 wp_send_json_success( self::payload() );
43 }
44
45 public function ajax_save() {
46 check_ajax_referer( 'ablocks_nonce', 'security' );
47 if ( ! current_user_can( 'edit_posts' ) ) {
48 wp_send_json_error( [ 'message' => 'forbidden' ], 403 );
49 }
50
51 $list = [];
52 if ( isset( $_POST['breakpoints'] ) ) {
53 $decoded = json_decode( wp_unslash( $_POST['breakpoints'] ), true );
54 if ( is_array( $decoded ) ) {
55 $list = self::sanitize( $decoded );
56 }
57 }
58
59 // Merge into the settings option without disturbing other keys.
60 $raw = get_option( ABLOCKS_SETTINGS_NAME );
61 $settings = $raw ? json_decode( $raw, true ) : [];
62 if ( ! is_array( $settings ) ) {
63 $settings = [];
64 }
65 $settings['breakpoint_custom'] = array_values( $list );
66 update_option( ABLOCKS_SETTINGS_NAME, wp_json_encode( $settings ) );
67
68 // Refresh the runtime settings global so this request sees the new list.
69 global $ablocks_settings;
70 $ablocks_settings = json_decode( get_option( ABLOCKS_SETTINGS_NAME ) );
71 Helper::flush_responsive_devices_cache();
72
73 wp_send_json_success( self::payload() );
74 }
75
76 /**
77 * Normalise a raw breakpoint list. Shared with Ajax\Settings::save_settings()
78 * so the editor modal and the admin settings page store the same shape.
79 */
80 public static function sanitize( $list ) {
81 $out = [];
82 $seen = [];
83 foreach ( $list as $item ) {
84 $item = (array) $item;
85 $key = isset( $item['key'] ) ? preg_replace( '/[^a-zA-Z0-9]/', '', (string) $item['key'] ) : '';
86 if ( '' === $key || isset( $seen[ $key ] ) ) {
87 $key = 'k' . substr( md5( wp_json_encode( $item ) . wp_rand() ), 0, 6 );
88 }
89 $seen[ $key ] = true;
90
91 $label = isset( $item['label'] ) ? sanitize_text_field( $item['label'] ) : '';
92 $min = isset( $item['minWidth'] ) ? (int) $item['minWidth'] : 0;
93 $max = isset( $item['maxWidth'] ) ? (int) $item['maxWidth'] : ( isset( $item['width'] ) ? (int) $item['width'] : 0 );
94 if ( $min < 1 && $max < 1 ) {
95 continue; // needs at least one bound
96 }
97
98 $out[] = [
99 'key' => $key,
100 'label' => $label,
101 'minWidth' => $min > 0 ? $min : '',
102 'maxWidth' => $max > 0 ? $max : '',
103 ];
104 }
105 return $out;
106 }
107 }
108