PluginProbe
Meow Gallery / 4.1.6
Meow Gallery v4.1.6
5.5.4 5.5.3 5.5.2 5.5.1 5.5.0 5.4.9 5.4.8 5.4.7 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 All 156 releases
meow-gallery / classes / rest.php

rest.php in Meow Gallery 4.1.6, at classes/rest.php

145 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 function rest_api_init() {
24 if ( !current_user_can( 'upload_files' ) ) {
25 return;
26 }
27
28 // SETTINGS
29 register_rest_route( $this->namespace, '/update_option/', array(
30 'methods' => 'POST',
31 'permission_callback' => '__return_true',
32 'callback' => array( $this, 'rest_update_option' )
33 ) );
34 register_rest_route( $this->namespace, '/all_settings/', array(
35 'methods' => 'GET',
36 'permission_callback' => '__return_true',
37 'callback' => array( $this, 'rest_all_settings' )
38 ) );
39
40 // BLOCK
41 register_rest_route( $this->namespace, '/preview', array(
42 'methods' => 'POST',
43 'permission_callback' => '__return_true',
44 'callback' => array( $this, 'preview' ),
45 ) );
46 }
47
48 function preview( WP_REST_Request $request ) {
49 $params = $request->get_body();
50 $params = json_decode( $params );
51 $params->ids = implode( ',', $params->ids );
52 $atts = (array) $params;
53 return $this->core->gallery( $atts, true );
54 }
55
56 function rest_all_settings() {
57 return new WP_REST_Response( [ 'success' => true, 'data' => $this->get_all_options() ], 200 );
58 }
59
60 function create_default_googlemaps_style( $force = false ) {
61 $style = get_option( 'mgl_googlemaps_style', "" );
62 if ( $force || empty( $style ) ) {
63 $style = '[]';
64 update_option( 'mgl_googlemaps_style', $style );
65 }
66 return $style;
67 }
68
69 function create_default_mapbox_style( $force = false ) {
70 $style = get_option( 'mgl_mapbox_style', "" );
71 if ( $force || empty( $style ) ) {
72 $style = '{"username":"", "style_id":""}';
73 update_option( 'mgl_mapbox_style', $style );
74 }
75 return $style;
76 }
77
78 function get_all_options() {
79 $options = $this->list_options();
80 $current_options = array();
81 foreach ( $options as $option => $default ) {
82 $current_options[$option] = get_option( $option, $default );
83 }
84 return $current_options;
85 }
86
87 // List all the options with their default values.
88 function list_options() {
89 return array(
90 'mgl_layout' => 'tiles',
91 'mgl_captions' => 'none',
92 'mgl_animation' => false,
93 'mgl_image_size' => 'srcset',
94 'mgl_infinite' => false,
95 'mgl_tiles_gutter' => 5,
96 'mgl_tiles_gutter_tablet' => 5,
97 'mgl_tiles_gutter_mobile' => 5,
98 'mgl_tiles_density' => 'high',
99 'mgl_tiles_density_tablet' => 'medium',
100 'mgl_tiles_density_mobile' => 'low',
101 'mgl_masonry_gutter' => 5,
102 'mgl_masonry_columns' => 3,
103 'mgl_justified_gutter' => 5,
104 'mgl_justified_row_height' => 200,
105 'mgl_square_gutter' => 5,
106 'mgl_square_columns' => 5,
107 'mgl_cascade_gutter' => 5,
108 'mgl_carousel_gutter' => 5,
109 'mgl_carousel_image_height' => 500,
110 'mgl_carousel_arrow_nav_enabled' => true,
111 'mgl_carousel_dot_nav_enabled' => true,
112 'mgl_map_engine' => '',
113 'mgl_map_height' => 400,
114 'mgl_googlemaps_token' => '',
115 'mgl_googlemaps_style' => $this->create_default_googlemaps_style(),
116 'mgl_mapbox_token' => '',
117 'mgl_mapbox_style' => $this->create_default_mapbox_style(),
118 'mgl_maptiler_token' => '',
119 'mgl_right_click' => false
120 );
121 }
122
123 function rest_update_option( $request ) {
124 $params = $request->get_json_params();
125 try {
126 $name = $params['name'];
127 $options = $this->list_options();
128 if ( !array_key_exists( $name, $options ) ) {
129 return new WP_REST_Response([ 'success' => false, 'message' => 'This option does not exist.' ], 200 );
130 }
131 $value = is_bool( $params['value'] ) ? ( $params['value'] ? '1' : '' ) : $params['value'];
132 $success = update_option( $name, $value );
133 if ( !$success ) {
134 return new WP_REST_Response([ 'success' => false, 'message' => 'Could not update option.' ], 200 );
135 }
136 return new WP_REST_Response([ 'success' => true, 'data' => $value ], 200 );
137 }
138 catch (Exception $e) {
139 return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 );
140 }
141 }
142
143 }
144
145 ?>