| 1 |
<?php |
| 2 |
/** |
| 3 |
* Optimole Rest related actions. |
| 4 |
* |
| 5 |
* @package Optimole/Inc |
| 6 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 7 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Optml_Rest |
| 12 |
*/ |
| 13 |
class Optml_Rest { |
| 14 |
/** |
| 15 |
* Rest api namespace. |
| 16 |
* |
| 17 |
* @var string Namespace. |
| 18 |
*/ |
| 19 |
private $namespace; |
| 20 |
|
| 21 |
/** |
| 22 |
* Optml_Rest constructor. |
| 23 |
*/ |
| 24 |
public function __construct() { |
| 25 |
$this->namespace = OPTML_NAMESPACE . '/v1'; |
| 26 |
add_action( 'rest_api_init', array( $this, 'register' ) ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Register rest routes. |
| 31 |
*/ |
| 32 |
public function register() { |
| 33 |
|
| 34 |
register_rest_route( |
| 35 |
$this->namespace, '/connect', array( |
| 36 |
array( |
| 37 |
'methods' => \WP_REST_Server::CREATABLE, |
| 38 |
'permission_callback' => function () { |
| 39 |
return current_user_can( 'manage_options' ); |
| 40 |
}, |
| 41 |
'callback' => array( $this, 'connect' ), |
| 42 |
'args' => array( |
| 43 |
'api_key' => array( |
| 44 |
'type' => 'string', |
| 45 |
'required' => true, |
| 46 |
), |
| 47 |
), |
| 48 |
), |
| 49 |
) |
| 50 |
); |
| 51 |
register_rest_route( |
| 52 |
$this->namespace, '/register', array( |
| 53 |
array( |
| 54 |
'methods' => \WP_REST_Server::CREATABLE, |
| 55 |
'permission_callback' => function () { |
| 56 |
return current_user_can( 'manage_options' ); |
| 57 |
}, |
| 58 |
'callback' => array( $this, 'register_service' ), |
| 59 |
'args' => array( |
| 60 |
'email' => array( |
| 61 |
'type' => 'string', |
| 62 |
'required' => true, |
| 63 |
), |
| 64 |
), |
| 65 |
), |
| 66 |
) |
| 67 |
); |
| 68 |
register_rest_route( |
| 69 |
$this->namespace, '/disconnect', array( |
| 70 |
array( |
| 71 |
'methods' => \WP_REST_Server::READABLE, |
| 72 |
'permission_callback' => function () { |
| 73 |
return current_user_can( 'manage_options' ); |
| 74 |
}, |
| 75 |
'callback' => array( $this, 'disconnect' ), |
| 76 |
), |
| 77 |
) |
| 78 |
); |
| 79 |
register_rest_route( |
| 80 |
$this->namespace, '/update_option', array( |
| 81 |
array( |
| 82 |
'methods' => \WP_REST_Server::CREATABLE, |
| 83 |
'permission_callback' => function () { |
| 84 |
return current_user_can( 'manage_options' ); |
| 85 |
}, |
| 86 |
'callback' => array( $this, 'update_option' ), |
| 87 |
), |
| 88 |
) |
| 89 |
); |
| 90 |
register_rest_route( |
| 91 |
$this->namespace, '/poll_optimized_images', array( |
| 92 |
array( |
| 93 |
'methods' => \WP_REST_Server::READABLE, |
| 94 |
'permission_callback' => function () { |
| 95 |
return current_user_can( 'manage_options' ); |
| 96 |
}, |
| 97 |
'callback' => array( $this, 'poll_optimized_images' ), |
| 98 |
), |
| 99 |
) |
| 100 |
); |
| 101 |
register_rest_route( |
| 102 |
$this->namespace, '/images-sample-rate', array( |
| 103 |
array( |
| 104 |
'methods' => \WP_REST_Server::CREATABLE, |
| 105 |
'permission_callback' => function () { |
| 106 |
return current_user_can( 'manage_options' ); |
| 107 |
}, |
| 108 |
'callback' => array( $this, 'get_sample_rate' ), |
| 109 |
), |
| 110 |
) |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Connect to optimole service. |
| 116 |
* |
| 117 |
* @param WP_REST_Request $request connect rest request. |
| 118 |
* |
| 119 |
* @return WP_Error|WP_REST_Response |
| 120 |
*/ |
| 121 |
public function connect( WP_REST_Request $request ) { |
| 122 |
$api_key = $request->get_param( 'api_key' ); |
| 123 |
$request = new Optml_Api(); |
| 124 |
$data = $request->get_user_data( $api_key ); |
| 125 |
if ( $data === false ) { |
| 126 |
wp_send_json_error( __( 'Can not connect to optimole service', 'optimole-wp' ) ); |
| 127 |
} |
| 128 |
$settings = new Optml_Settings(); |
| 129 |
$settings->update( 'service_data', $data ); |
| 130 |
$settings->update( 'api_key', $api_key ); |
| 131 |
|
| 132 |
return $this->response( $data ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Wrapper for api response. |
| 137 |
* |
| 138 |
* @param array $data data from api. |
| 139 |
* |
| 140 |
* @return WP_REST_Response |
| 141 |
*/ |
| 142 |
private function response( $data ) { |
| 143 |
return new WP_REST_Response( array( 'data' => $data, 'code' => 'success' ), 200 ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Connect to optimole service. |
| 148 |
* |
| 149 |
* @param WP_REST_Request $request connect rest request. |
| 150 |
* |
| 151 |
* @return WP_Error|WP_REST_Response |
| 152 |
*/ |
| 153 |
public function register_service( WP_REST_Request $request ) { |
| 154 |
$email = $request->get_param( 'email' ); |
| 155 |
$api = new Optml_Api(); |
| 156 |
$user = $api->create_account( $email ); |
| 157 |
if ( $user === false ) { |
| 158 |
return new WP_Error( 'error', 'Error creating account.' ); |
| 159 |
} |
| 160 |
|
| 161 |
return $this->response( $user ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Return image samples. |
| 166 |
* |
| 167 |
* @param WP_REST_Request $request Rest request. |
| 168 |
* |
| 169 |
* @return WP_REST_Response Image urls. |
| 170 |
*/ |
| 171 |
public function get_sample_rate( WP_REST_Request $request ) { |
| 172 |
|
| 173 |
add_filter( 'optml_dont_replace_url', '__return_true' ); |
| 174 |
|
| 175 |
$accepted_mimes = array( 'image/jpeg', 'image/png' ); |
| 176 |
$args = array( |
| 177 |
'post_type' => 'attachment', |
| 178 |
'post_status' => 'any', |
| 179 |
'number' => '5', |
| 180 |
'no_found_rows' => true, |
| 181 |
'fields' => 'ids', |
| 182 |
'post_mime_type' => $accepted_mimes, |
| 183 |
); |
| 184 |
$image_result = new WP_Query( $args ); |
| 185 |
if ( empty( $image_result->posts ) ) { |
| 186 |
return $this->response( array() ); |
| 187 |
} |
| 188 |
|
| 189 |
$image = array( |
| 190 |
'id' => $image_result->posts [ array_rand( $image_result->posts, 1 ) ], |
| 191 |
); |
| 192 |
$image['original'] = wp_get_attachment_image_url( $image['id'], 'full' ); |
| 193 |
|
| 194 |
remove_filter( 'optml_dont_replace_url', '__return_true' ); |
| 195 |
|
| 196 |
$image['optimized'] = apply_filters( 'optml_replace_image', $image['original'], array( |
| 197 |
'width' => 'auto', |
| 198 |
'height' => 'auto', |
| 199 |
'quality' => $request->get_param( 'quality' ) |
| 200 |
) ); |
| 201 |
|
| 202 |
return $this->response( $image ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Disconnect from optimole service. |
| 207 |
* |
| 208 |
* @param WP_REST_Request $request disconnect rest request. |
| 209 |
*/ |
| 210 |
public function disconnect( WP_REST_Request $request ) { |
| 211 |
$settings = new Optml_Settings(); |
| 212 |
$settings->reset(); |
| 213 |
wp_send_json_success( 'Disconnected' ); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Get optimized images from API. |
| 218 |
* |
| 219 |
* @param WP_REST_Request $request rest request. |
| 220 |
* |
| 221 |
* @return WP_REST_Response |
| 222 |
*/ |
| 223 |
public function poll_optimized_images( WP_REST_Request $request ) { |
| 224 |
$api_key = $request->get_param( 'api_key' ); |
| 225 |
$request = new Optml_Api(); |
| 226 |
$images = $request->get_optimized_images( $api_key ); |
| 227 |
if ( ! isset ( $images['list'] ) || empty( $images['list'] ) ) { |
| 228 |
return $this->response( array() ); |
| 229 |
} |
| 230 |
|
| 231 |
$final_images = array_splice( $images['list'], 0, 10 ); |
| 232 |
|
| 233 |
return $this->response( $final_images ); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Update options method. |
| 238 |
* |
| 239 |
* @param WP_REST_Request $request option update rest request. |
| 240 |
*/ |
| 241 |
public function update_option( WP_REST_Request $request ) { |
| 242 |
$option_key = $request->get_param( 'option_key' ); |
| 243 |
$option_type = $request->get_param( 'type' ); |
| 244 |
$option_value = $request->get_param( 'option_value' ); |
| 245 |
if ( empty( $option_key ) ) { |
| 246 |
wp_send_json_error( 'No option key set.' ); |
| 247 |
} |
| 248 |
if ( empty( $option_type ) ) { |
| 249 |
wp_send_json_error( 'No option type set.' ); |
| 250 |
} |
| 251 |
|
| 252 |
$accepted_types = array( 'toggle', 'enum' ); |
| 253 |
|
| 254 |
if ( ! in_array( $option_type, $accepted_types ) ) { |
| 255 |
wp_send_json_error( 'Invalid option type.' ); |
| 256 |
} |
| 257 |
|
| 258 |
$settings = new Optml_Settings(); |
| 259 |
|
| 260 |
switch ( $option_type ) { |
| 261 |
case 'toggle': |
| 262 |
$status = $settings->get( $option_key ); |
| 263 |
if ( $status === 'enabled' ) { |
| 264 |
$settings->update( $option_key, 'disabled' ); |
| 265 |
wp_send_json_success( $option_key . ' disabled.' ); |
| 266 |
} else { |
| 267 |
$settings->update( $option_key, 'enabled' ); |
| 268 |
wp_send_json_success( $option_key . ' enabled.' ); |
| 269 |
} |
| 270 |
break; |
| 271 |
case 'enum': |
| 272 |
$settings->update( $option_key, $option_value ); |
| 273 |
wp_send_json_success( $option_key . ' saved to ' . $option_value ); |
| 274 |
break; |
| 275 |
default: |
| 276 |
break; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
} |
| 281 |
|