| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_MGL_Rest |
| 4 |
{ |
| 5 |
private $core; |
| 6 |
private $namespace = 'meow-gallery/v1'; |
| 7 |
|
| 8 |
public function __construct( $core ) { |
| 9 |
$this->core = $core; |
| 10 |
|
| 11 |
// FOR DEBUG |
| 12 |
// For experiencing the UI behavior on a slower install. |
| 13 |
// sleep(1); |
| 14 |
// For experiencing the UI behavior on a buggy install. |
| 15 |
// trigger_error( "Error", E_USER_ERROR); |
| 16 |
// trigger_error( "Warning", E_USER_WARNING); |
| 17 |
// trigger_error( "Notice", E_USER_NOTICE); |
| 18 |
// trigger_error( "Deprecated", E_USER_DEPRECATED); |
| 19 |
|
| 20 |
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
function rest_api_init() { |
| 25 |
|
| 26 |
// Settings |
| 27 |
register_rest_route( $this->namespace, '/update_option/', array( |
| 28 |
'methods' => 'POST', |
| 29 |
'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 30 |
'callback' => array( $this, 'rest_update_option' ) |
| 31 |
) ); |
| 32 |
register_rest_route( $this->namespace, '/all_settings/', array( |
| 33 |
'methods' => 'GET', |
| 34 |
'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 35 |
'callback' => array( $this, 'rest_all_settings' ) |
| 36 |
) ); |
| 37 |
|
| 38 |
// Gutenberg Block |
| 39 |
register_rest_route( $this->namespace, '/preview', array( |
| 40 |
'methods' => 'POST', |
| 41 |
'permission_callback' => array( $this->core, 'can_access_features' ), |
| 42 |
'callback' => array( $this, 'preview' ), |
| 43 |
) ); |
| 44 |
} |
| 45 |
|
| 46 |
function preview( WP_REST_Request $request ) { |
| 47 |
$params = $request->get_body(); |
| 48 |
$params = json_decode( $params ); |
| 49 |
$params->ids = implode( ',', $params->ids ); |
| 50 |
$atts = (array) $params; |
| 51 |
$html = $this->core->gallery( $atts, true ); |
| 52 |
return new WP_REST_Response( [ 'success' => true, 'data' => $html ], 200 ); |
| 53 |
} |
| 54 |
|
| 55 |
function rest_all_settings() { |
| 56 |
return new WP_REST_Response( [ 'success' => true, 'data' => $this->get_all_options() ], 200 ); |
| 57 |
} |
| 58 |
|
| 59 |
function create_default_googlemaps_style( $force = false ) { |
| 60 |
$style = get_option( 'mgl_googlemaps_style', "" ); |
| 61 |
if ( $force || empty( $style ) ) { |
| 62 |
$style = '[]'; |
| 63 |
update_option( 'mgl_googlemaps_style', $style ); |
| 64 |
} |
| 65 |
return $style; |
| 66 |
} |
| 67 |
|
| 68 |
function create_default_mapbox_style( $force = false ) { |
| 69 |
$style = get_option( 'mgl_mapbox_style', "" ); |
| 70 |
if ( $force || empty( $style ) ) { |
| 71 |
$style = '{"username":"", "style_id":""}'; |
| 72 |
update_option( 'mgl_mapbox_style', $style ); |
| 73 |
} |
| 74 |
return $style; |
| 75 |
} |
| 76 |
|
| 77 |
function get_all_options() { |
| 78 |
$options = $this->list_options(); |
| 79 |
$current_options = array(); |
| 80 |
foreach ( $options as $option => $default ) { |
| 81 |
$current_options[$option] = get_option( $option, $default ); |
| 82 |
} |
| 83 |
return $current_options; |
| 84 |
} |
| 85 |
|
| 86 |
// List all the options with their default values. |
| 87 |
function list_options() { |
| 88 |
return array( |
| 89 |
'mgl_layout' => 'tiles', |
| 90 |
'mgl_captions' => 'none', |
| 91 |
'mgl_animation' => false, |
| 92 |
'mgl_image_size' => 'srcset', |
| 93 |
'mgl_infinite' => false, |
| 94 |
'mgl_tiles_gutter' => 5, |
| 95 |
'mgl_tiles_gutter_tablet' => 5, |
| 96 |
'mgl_tiles_gutter_mobile' => 5, |
| 97 |
'mgl_tiles_density' => 'high', |
| 98 |
'mgl_tiles_density_tablet' => 'medium', |
| 99 |
'mgl_tiles_density_mobile' => 'low', |
| 100 |
'mgl_masonry_gutter' => 5, |
| 101 |
'mgl_masonry_columns' => 3, |
| 102 |
'mgl_justified_gutter' => 5, |
| 103 |
'mgl_justified_row_height' => 200, |
| 104 |
'mgl_square_gutter' => 5, |
| 105 |
'mgl_square_columns' => 5, |
| 106 |
'mgl_cascade_gutter' => 5, |
| 107 |
'mgl_horizontal_gutter' => 5, |
| 108 |
'mgl_horizontal_image_height' => 500, |
| 109 |
'mgl_horizontal_hide_scrollbar' => false, |
| 110 |
'mgl_carousel_gutter' => 5, |
| 111 |
'mgl_carousel_image_height' => 500, |
| 112 |
'mgl_carousel_arrow_nav_enabled' => true, |
| 113 |
'mgl_carousel_dot_nav_enabled' => true, |
| 114 |
'mgl_map_engine' => '', |
| 115 |
'mgl_map_height' => 400, |
| 116 |
'mgl_googlemaps_token' => '', |
| 117 |
'mgl_googlemaps_style' => $this->create_default_googlemaps_style(), |
| 118 |
'mgl_mapbox_token' => '', |
| 119 |
'mgl_mapbox_style' => $this->create_default_mapbox_style(), |
| 120 |
'mgl_maptiler_token' => '', |
| 121 |
'mgl_right_click' => false |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
function rest_update_option( $request ) { |
| 126 |
$params = $request->get_json_params(); |
| 127 |
try { |
| 128 |
$name = $params['name']; |
| 129 |
$options = $this->list_options(); |
| 130 |
if ( !array_key_exists( $name, $options ) ) { |
| 131 |
return new WP_REST_Response([ 'success' => false, 'message' => 'This option does not exist.' ], 200 ); |
| 132 |
} |
| 133 |
$value = is_bool( $params['value'] ) ? ( $params['value'] ? '1' : '' ) : $params['value']; |
| 134 |
$success = update_option( $name, $value ); |
| 135 |
if ( !$success ) { |
| 136 |
return new WP_REST_Response([ 'success' => false, 'message' => 'Could not update option.' ], 200 ); |
| 137 |
} |
| 138 |
return new WP_REST_Response([ 'success' => true, 'data' => $value ], 200 ); |
| 139 |
} |
| 140 |
catch (Exception $e) { |
| 141 |
return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
?> |