| 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 = $this->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 |
private function sanitize( $list ) { |
| 77 |
$out = []; |
| 78 |
$seen = []; |
| 79 |
foreach ( $list as $item ) { |
| 80 |
$item = (array) $item; |
| 81 |
$key = isset( $item['key'] ) ? preg_replace( '/[^a-zA-Z0-9]/', '', (string) $item['key'] ) : ''; |
| 82 |
if ( '' === $key || isset( $seen[ $key ] ) ) { |
| 83 |
$key = 'k' . substr( md5( wp_json_encode( $item ) . wp_rand() ), 0, 6 ); |
| 84 |
} |
| 85 |
$seen[ $key ] = true; |
| 86 |
|
| 87 |
$label = isset( $item['label'] ) ? sanitize_text_field( $item['label'] ) : ''; |
| 88 |
$min = isset( $item['minWidth'] ) ? (int) $item['minWidth'] : 0; |
| 89 |
$max = isset( $item['maxWidth'] ) ? (int) $item['maxWidth'] : ( isset( $item['width'] ) ? (int) $item['width'] : 0 ); |
| 90 |
if ( $min < 1 && $max < 1 ) { |
| 91 |
continue; // needs at least one bound |
| 92 |
} |
| 93 |
|
| 94 |
$out[] = [ |
| 95 |
'key' => $key, |
| 96 |
'label' => $label, |
| 97 |
'minWidth' => $min > 0 ? $min : '', |
| 98 |
'maxWidth' => $max > 0 ? $max : '', |
| 99 |
]; |
| 100 |
} |
| 101 |
return $out; |
| 102 |
} |
| 103 |
} |
| 104 |
|