| 1 |
<?php |
| 2 |
namespace Vimeography\Api; |
| 3 |
|
| 4 |
class Galleries extends \WP_REST_Controller |
| 5 |
{ |
| 6 |
public function __construct() |
| 7 |
{ |
| 8 |
// needed for is_plugin_active() calls |
| 9 |
if (!function_exists('is_plugin_active')) { |
| 10 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 11 |
} |
| 12 |
|
| 13 |
// needed for duplicate gallery calls |
| 14 |
if (!function_exists('request_filesystem_credentials')) { |
| 15 |
include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 16 |
} |
| 17 |
|
| 18 |
add_action('rest_api_init', function () { |
| 19 |
$this->register_routes(); |
| 20 |
}); |
| 21 |
} |
| 22 |
/** |
| 23 |
* Register the routes for the objects of the controller. |
| 24 |
*/ |
| 25 |
public function register_routes() |
| 26 |
{ |
| 27 |
$version = '1'; |
| 28 |
$namespace = 'vimeography/v' . $version; |
| 29 |
$base = 'galleries'; |
| 30 |
register_rest_route($namespace, '/' . $base, array( |
| 31 |
array( |
| 32 |
'methods' => \WP_REST_Server::READABLE, |
| 33 |
'callback' => array($this, 'get_items'), |
| 34 |
'permission_callback' => array($this, 'get_items_permissions_check'), |
| 35 |
'args' => array() |
| 36 |
), |
| 37 |
array( |
| 38 |
'methods' => \WP_REST_Server::CREATABLE, |
| 39 |
'callback' => array($this, 'create_item'), |
| 40 |
'permission_callback' => array($this, 'create_item_permissions_check'), |
| 41 |
'args' => $this->get_endpoint_args_for_item_schema(true) |
| 42 |
) |
| 43 |
)); |
| 44 |
register_rest_route($namespace, '/' . $base . '/(?P<id>[\d]+)', array( |
| 45 |
array( |
| 46 |
'methods' => \WP_REST_Server::READABLE, |
| 47 |
'callback' => array($this, 'get_item'), |
| 48 |
'permission_callback' => array($this, 'get_item_permissions_check'), |
| 49 |
'args' => array( |
| 50 |
'context' => array( |
| 51 |
'default' => 'view' |
| 52 |
) |
| 53 |
) |
| 54 |
), |
| 55 |
array( |
| 56 |
'methods' => \WP_REST_Server::EDITABLE, |
| 57 |
'callback' => array($this, 'update_item'), |
| 58 |
'permission_callback' => array($this, 'update_item_permissions_check'), |
| 59 |
'args' => array( |
| 60 |
'allow_downloads' => array( |
| 61 |
'description' => esc_html__( |
| 62 |
'Enable downloads for a gallery. Requires Vimeography Pro.', |
| 63 |
'vimeography' |
| 64 |
), |
| 65 |
'default' => false, |
| 66 |
'type' => 'boolean' |
| 67 |
), |
| 68 |
'cache_timeout' => array( |
| 69 |
'description' => esc_html__( |
| 70 |
'Specifies how often to refresh the cache, in seconds.', |
| 71 |
'vimeography' |
| 72 |
), |
| 73 |
'default' => 3600, |
| 74 |
'type' => 'integer', |
| 75 |
'sanitize_callback' => 'absint' |
| 76 |
), |
| 77 |
'direction' => array( |
| 78 |
'description' => esc_html__( |
| 79 |
'Specifies whether the results should be returned in ascending or descending order.', |
| 80 |
'vimeography' |
| 81 |
), |
| 82 |
'default' => "desc", |
| 83 |
'type' => 'string', |
| 84 |
'validate_callback' => function ($value, $request, $param) { |
| 85 |
if ($value !== "asc" && $value !== "desc") { |
| 86 |
return false; |
| 87 |
} |
| 88 |
} |
| 89 |
), |
| 90 |
'enable_playlist' => array( |
| 91 |
'description' => esc_html__( |
| 92 |
'Enable auto-advance playlists for a gallery. Requires Vimeography Pro.', |
| 93 |
'vimeography' |
| 94 |
), |
| 95 |
'default' => false, |
| 96 |
'type' => 'boolean' |
| 97 |
), |
| 98 |
'enable_search' => array( |
| 99 |
'description' => esc_html__( |
| 100 |
'Enable search capabilities for a gallery. Requires Vimeography Pro.', |
| 101 |
'vimeography' |
| 102 |
), |
| 103 |
'default' => false, |
| 104 |
'type' => 'boolean' |
| 105 |
), |
| 106 |
'featured_video' => array( |
| 107 |
'description' => esc_html__( |
| 108 |
'Link to the video which should appear first in the gallery.', |
| 109 |
'vimeography' |
| 110 |
), |
| 111 |
"default" => '' |
| 112 |
), |
| 113 |
'gallery_width' => array( |
| 114 |
'description' => esc_html__( |
| 115 |
'Maximum width of the rendered DOM container of the gallery.', |
| 116 |
'vimeography' |
| 117 |
), |
| 118 |
'type' => 'string', |
| 119 |
'default' => '', |
| 120 |
'sanitize_callback' => function ($value, $request, $param) { |
| 121 |
preg_match('/(\d*)(px|%?)/', $value, $matches); |
| 122 |
// If a number value is set... |
| 123 |
if (!empty($matches[1])) { |
| 124 |
// If a '%' or 'px' is set... |
| 125 |
if (!empty($matches[2])) { |
| 126 |
// Accept the valid matching string |
| 127 |
$value = $matches[0]; |
| 128 |
} else { |
| 129 |
// Append a 'px' value to the matching number |
| 130 |
$value = $matches[1] . 'px'; |
| 131 |
} |
| 132 |
} else { |
| 133 |
// Not a valid width |
| 134 |
$value = ''; |
| 135 |
} |
| 136 |
|
| 137 |
return $value; |
| 138 |
} |
| 139 |
), |
| 140 |
'source_url' => array( |
| 141 |
'description' => esc_html__( |
| 142 |
'Vimeo source for the gallery. Usually a showcase link.', |
| 143 |
'vimeography' |
| 144 |
), |
| 145 |
'type' => 'string' |
| 146 |
), |
| 147 |
'sort' => array( |
| 148 |
'description' => esc_html__( |
| 149 |
'Sorting method to use for the gallery.', |
| 150 |
'vimeography' |
| 151 |
), |
| 152 |
'type' => 'string' |
| 153 |
), |
| 154 |
'theme_name' => array( |
| 155 |
'description' => esc_html__( |
| 156 |
'Gallery theme which is applied to the current gallery.', |
| 157 |
'vimeography' |
| 158 |
), |
| 159 |
'type' => 'string' |
| 160 |
), |
| 161 |
'title' => array( |
| 162 |
'description' => esc_html__( |
| 163 |
'Gallery title. Only appears in the admin panel.', |
| 164 |
'vimeography' |
| 165 |
), |
| 166 |
'type' => 'string' |
| 167 |
), |
| 168 |
'video_limit' => array( |
| 169 |
'description' => esc_html__( |
| 170 |
'Suppresses the number of videos which appear in the gallery. Set to 0 for no suppression.', |
| 171 |
'vimeography' |
| 172 |
), |
| 173 |
'type' => 'integer', |
| 174 |
'sanitize_callback' => 'absint', |
| 175 |
'validate_callback' => function ($value, $request, $param) { |
| 176 |
if ($value > 25) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
} |
| 180 |
), |
| 181 |
'videos_per_page' => array( |
| 182 |
'description' => esc_html__( |
| 183 |
'Sets the number of videos that should be fetched on each pagination request to Vimeo.', |
| 184 |
'vimeography' |
| 185 |
), |
| 186 |
'type' => 'integer', |
| 187 |
'sanitize_callback' => 'absint', |
| 188 |
'validate_callback' => function ($value, $request, $param) { |
| 189 |
if ($value > 100) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
} |
| 193 |
) |
| 194 |
) |
| 195 |
), |
| 196 |
array( |
| 197 |
'methods' => \WP_REST_Server::DELETABLE, |
| 198 |
'callback' => array($this, 'delete_item'), |
| 199 |
'permission_callback' => array($this, 'delete_item_permissions_check'), |
| 200 |
'args' => array( |
| 201 |
'force' => array( |
| 202 |
'default' => false |
| 203 |
) |
| 204 |
) |
| 205 |
) |
| 206 |
)); |
| 207 |
register_rest_route( |
| 208 |
$namespace, |
| 209 |
'/' . $base . '/(?P<id>[\d]+)/duplicate', |
| 210 |
array( |
| 211 |
array( |
| 212 |
'methods' => \WP_REST_Server::EDITABLE, |
| 213 |
'callback' => array($this, 'duplicate_gallery'), |
| 214 |
'permission_callback' => array( |
| 215 |
$this, |
| 216 |
'update_item_permissions_check' |
| 217 |
), |
| 218 |
'args' => $this->get_endpoint_args_for_item_schema(false) |
| 219 |
) |
| 220 |
) |
| 221 |
); |
| 222 |
|
| 223 |
register_rest_route( |
| 224 |
$namespace, |
| 225 |
'/' . $base . '/(?P<id>[\d]+)/appearance', |
| 226 |
array( |
| 227 |
array( |
| 228 |
'methods' => \WP_REST_Server::EDITABLE, |
| 229 |
'callback' => array($this, 'update_gallery_appearance'), |
| 230 |
'permission_callback' => array( |
| 231 |
$this, |
| 232 |
'update_item_permissions_check' |
| 233 |
), |
| 234 |
'args' => array( |
| 235 |
'css' => array( |
| 236 |
'default' => '', |
| 237 |
'required' => true, |
| 238 |
'validate_callback' => function ($param, $request, $key) { |
| 239 |
// Markup is not allowed in CSS. |
| 240 |
$has_invalid_tags = preg_match('#</?\w+#', $param); |
| 241 |
return !$has_invalid_tags; |
| 242 |
} |
| 243 |
) |
| 244 |
) |
| 245 |
), |
| 246 |
array( |
| 247 |
'methods' => \WP_REST_Server::DELETABLE, |
| 248 |
'callback' => array($this, 'delete_gallery_appearance'), |
| 249 |
'permission_callback' => array( |
| 250 |
$this, |
| 251 |
'delete_item_permissions_check' |
| 252 |
), |
| 253 |
'args' => array( |
| 254 |
'force' => array( |
| 255 |
'default' => false |
| 256 |
) |
| 257 |
) |
| 258 |
) |
| 259 |
) |
| 260 |
); |
| 261 |
|
| 262 |
register_rest_route($namespace, '/' . $base . '/schema', array( |
| 263 |
'methods' => \WP_REST_Server::READABLE, |
| 264 |
'callback' => array($this, 'get_public_item_schema') |
| 265 |
)); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Get a collection of items |
| 270 |
* |
| 271 |
* @param WP_REST_Request $request Full data about the request. |
| 272 |
* @return WP_Error|WP_REST_Response |
| 273 |
*/ |
| 274 |
public function get_items($request) |
| 275 |
{ |
| 276 |
$items = array(); //do a query, call another class, etc |
| 277 |
$data = array(); |
| 278 |
|
| 279 |
global $wpdb; |
| 280 |
|
| 281 |
if (isset($_GET['s']) && !empty($_GET['s'])) { |
| 282 |
$term = sanitize_text_field($_GET['s']); |
| 283 |
|
| 284 |
if (intval($term) == 0) { |
| 285 |
$filter = 'WHERE gallery.title LIKE "%' . $term . '%" '; |
| 286 |
} else { |
| 287 |
$filter = 'WHERE gallery.id = "' . $term . '" '; |
| 288 |
} |
| 289 |
} else { |
| 290 |
$filter = ''; |
| 291 |
} |
| 292 |
|
| 293 |
$sort = $this->_get_sort(); |
| 294 |
$result = $wpdb->get_results( |
| 295 |
'SELECT * from ' . |
| 296 |
$wpdb->vimeography_gallery_meta . |
| 297 |
' AS meta JOIN ' . |
| 298 |
$wpdb->vimeography_gallery . |
| 299 |
' AS gallery ON meta.gallery_id = gallery.id ' . |
| 300 |
$filter . |
| 301 |
$sort . |
| 302 |
';', |
| 303 |
ARRAY_A |
| 304 |
); |
| 305 |
// echo '<pre>'; |
| 306 |
// var_dump($result); |
| 307 |
// echo '</pre>'; |
| 308 |
// die; |
| 309 |
return $result; |
| 310 |
|
| 311 |
foreach ($items as $item) { |
| 312 |
$itemdata = $this->prepare_item_for_response($item, $request); |
| 313 |
$data[] = $this->prepare_response_for_collection($itemdata); |
| 314 |
} |
| 315 |
|
| 316 |
return new WP_REST_Response($data, 200); |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Get one item from the collection |
| 321 |
* |
| 322 |
* @param WP_REST_Request $request Full data about the request. |
| 323 |
* @return WP_Error|WP_REST_Response |
| 324 |
*/ |
| 325 |
public function get_item($request) |
| 326 |
{ |
| 327 |
$params = $request->get_params(); |
| 328 |
$gallery_id = intval($params['id']); |
| 329 |
|
| 330 |
global $wpdb; |
| 331 |
|
| 332 |
$result = $wpdb->get_results( |
| 333 |
' |
| 334 |
SELECT * FROM ' . |
| 335 |
$wpdb->vimeography_gallery_meta . |
| 336 |
' AS meta |
| 337 |
JOIN ' . |
| 338 |
$wpdb->vimeography_gallery . |
| 339 |
' AS gallery |
| 340 |
ON meta.gallery_id = gallery.id |
| 341 |
WHERE meta.gallery_id = ' . |
| 342 |
$gallery_id . |
| 343 |
' |
| 344 |
LIMIT 1; |
| 345 |
' |
| 346 |
); |
| 347 |
|
| 348 |
$settings = $result[0]; |
| 349 |
|
| 350 |
$settings = new \stdClass(); |
| 351 |
$settings->id = intval($result[0]->gallery_id); |
| 352 |
$settings->date_created = $result[0]->date_created; |
| 353 |
$settings->cache_timeout = intval($result[0]->cache_timeout); |
| 354 |
$settings->featured_video = $result[0]->featured_video; |
| 355 |
$settings->gallery_width = $result[0]->gallery_width; |
| 356 |
$settings->resource_uri = $result[0]->resource_uri; |
| 357 |
$settings->source_url = $result[0]->source_url; |
| 358 |
$settings->theme_name = $result[0]->theme_name; |
| 359 |
$settings->title = $result[0]->title; |
| 360 |
$settings->video_limit = intval($result[0]->video_limit); |
| 361 |
|
| 362 |
// need to add pro settings like this |
| 363 |
// apply_filters('vimeography/gallery-settings', $this->_gallery); |
| 364 |
|
| 365 |
if (is_plugin_active('vimeography-pro/vimeography-pro.php')) { |
| 366 |
$pro_settings = $wpdb->get_results( |
| 367 |
' |
| 368 |
SELECT * |
| 369 |
FROM ' . |
| 370 |
$wpdb->vimeography_pro_meta . |
| 371 |
' AS pro |
| 372 |
WHERE pro.gallery_id = ' . |
| 373 |
$gallery_id . |
| 374 |
' |
| 375 |
LIMIT 1; |
| 376 |
' |
| 377 |
); |
| 378 |
|
| 379 |
if (empty($pro_settings)) { |
| 380 |
$pro_settings = \Vimeography_Pro()->database->add_default_settings( |
| 381 |
$gallery_id |
| 382 |
); |
| 383 |
} |
| 384 |
|
| 385 |
$settings->allow_downloads = (bool) $pro_settings[0]->allow_downloads; |
| 386 |
$settings->sort = $pro_settings[0]->sort; |
| 387 |
$settings->direction = $pro_settings[0]->direction; |
| 388 |
$settings->enable_search = (bool) $pro_settings[0]->enable_search; |
| 389 |
$settings->enable_tags = (bool) $pro_settings[0]->enable_tags; |
| 390 |
$settings->enable_playlist = (bool) $pro_settings[0]->playlist; |
| 391 |
$settings->videos_per_page = intval($pro_settings[0]->per_page); |
| 392 |
} |
| 393 |
|
| 394 |
return $settings; |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Create one item from the collection |
| 399 |
* |
| 400 |
* @param WP_REST_Request $request Full data about the request. |
| 401 |
* @return WP_Error|WP_REST_Response |
| 402 |
*/ |
| 403 |
public function create_item($request) |
| 404 |
{ |
| 405 |
$item = $this->prepare_item_for_database($request); |
| 406 |
|
| 407 |
if (function_exists('slug_some_function_to_create_item')) { |
| 408 |
$data = slug_some_function_to_create_item($item); |
| 409 |
if (is_array($data)) { |
| 410 |
return new WP_REST_Response($data, 200); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
return new WP_Error('cant-create', __('message', 'text-domain'), array( |
| 415 |
'status' => 500 |
| 416 |
)); |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Update one item from the collection |
| 421 |
* |
| 422 |
* @param WP_REST_Request $request Full data about the request. |
| 423 |
* @return WP_Error|WP_REST_Response |
| 424 |
*/ |
| 425 |
public function update_item($request) |
| 426 |
{ |
| 427 |
$params = $request->get_params(); |
| 428 |
$data = array(); |
| 429 |
$format = array(); |
| 430 |
|
| 431 |
if (isset($params['cache_timeout'])) { |
| 432 |
$data['cache_timeout'] = $params['cache_timeout']; |
| 433 |
$format[] = '%d'; |
| 434 |
} |
| 435 |
|
| 436 |
if (isset($params['video_limit'])) { |
| 437 |
$data['video_limit'] = $params['video_limit']; |
| 438 |
$format[] = '%d'; |
| 439 |
} |
| 440 |
|
| 441 |
if (isset($params['featured_video'])) { |
| 442 |
$data['featured_video'] = $params['featured_video']; |
| 443 |
$format[] = '%s'; |
| 444 |
} |
| 445 |
|
| 446 |
if (isset($params['gallery_width'])) { |
| 447 |
$data['gallery_width'] = $params['gallery_width']; |
| 448 |
$format[] = '%s'; |
| 449 |
} |
| 450 |
|
| 451 |
global $wpdb; |
| 452 |
|
| 453 |
$result = $wpdb->update( |
| 454 |
$wpdb->vimeography_gallery_meta, |
| 455 |
$data, |
| 456 |
array('gallery_id' => $params['id']), |
| 457 |
$format, |
| 458 |
array('%d') |
| 459 |
); |
| 460 |
|
| 461 |
if ($result === false) { |
| 462 |
return new \WP_Error('cant-update', __('message', 'text-domain'), array( |
| 463 |
'status' => 500 |
| 464 |
)); |
| 465 |
} |
| 466 |
|
| 467 |
if (is_plugin_active('vimeography-pro/vimeography-pro.php')) { |
| 468 |
try { |
| 469 |
$data = array(); |
| 470 |
$format = array(); |
| 471 |
|
| 472 |
if (isset($params['videos_per_page'])) { |
| 473 |
$data['per_page'] = $params['videos_per_page']; |
| 474 |
$format[] = '%d'; |
| 475 |
} |
| 476 |
|
| 477 |
if (isset($params['sort'])) { |
| 478 |
$data['sort'] = $params['sort']; |
| 479 |
$format[] = '%s'; |
| 480 |
} |
| 481 |
|
| 482 |
if (isset($params['direction'])) { |
| 483 |
$data['direction'] = $params['direction']; |
| 484 |
$format[] = '%s'; |
| 485 |
} |
| 486 |
|
| 487 |
if (isset($params['enable_playlist'])) { |
| 488 |
$data['playlist'] = $params['enable_playlist'] === true ? 1 : 0; |
| 489 |
$format[] = '%d'; |
| 490 |
} |
| 491 |
|
| 492 |
if (isset($params['allow_downloads'])) { |
| 493 |
$data['allow_downloads'] = |
| 494 |
$params['allow_downloads'] === true ? 1 : 0; |
| 495 |
$format[] = '%d'; |
| 496 |
} |
| 497 |
|
| 498 |
if (isset($params['enable_search'])) { |
| 499 |
$data['enable_search'] = $params['enable_search'] === true ? 1 : 0; |
| 500 |
$format[] = '%d'; |
| 501 |
} |
| 502 |
|
| 503 |
if (isset($params['enable_tags'])) { |
| 504 |
$data['enable_tags'] = $params['enable_tags'] === true ? 1 : 0; |
| 505 |
$format[] = '%d'; |
| 506 |
} |
| 507 |
|
| 508 |
$result = $wpdb->update( |
| 509 |
$wpdb->vimeography_pro_meta, |
| 510 |
$data, |
| 511 |
array('gallery_id' => $params['id']), |
| 512 |
$format, |
| 513 |
array('%d') |
| 514 |
); |
| 515 |
|
| 516 |
if ($result === false) { |
| 517 |
return new \WP_Error( |
| 518 |
'cant-update', |
| 519 |
__('message', 'text-domain'), |
| 520 |
array( |
| 521 |
'status' => 500 |
| 522 |
) |
| 523 |
); |
| 524 |
} |
| 525 |
} catch (Exception $e) { |
| 526 |
return new \WP_Error('cant-update', __('message', 'text-domain'), array( |
| 527 |
'status' => 500 |
| 528 |
)); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
require_once VIMEOGRAPHY_PATH . 'lib/deprecated/cache.php'; |
| 533 |
$cache = new \Vimeography_Cache($params['id']); |
| 534 |
|
| 535 |
if ($cache->exists()) { |
| 536 |
$cache->delete(); |
| 537 |
} |
| 538 |
|
| 539 |
return new \WP_REST_Response($result, 200); |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* Delete one item from the collection |
| 544 |
* |
| 545 |
* @param WP_REST_Request $request Full data about the request. |
| 546 |
* @return WP_Error|WP_REST_Response |
| 547 |
*/ |
| 548 |
public function delete_item($request) |
| 549 |
{ |
| 550 |
$params = $request->get_params(); |
| 551 |
$id = intval($params['id']); |
| 552 |
|
| 553 |
try { |
| 554 |
global $wpdb; |
| 555 |
$result = $wpdb->query( |
| 556 |
'DELETE gallery, meta FROM ' . |
| 557 |
$wpdb->vimeography_gallery . |
| 558 |
' gallery, ' . |
| 559 |
$wpdb->vimeography_gallery_meta . |
| 560 |
' meta WHERE gallery.id = ' . |
| 561 |
$id . |
| 562 |
' AND meta.gallery_id = ' . |
| 563 |
$id . |
| 564 |
';' |
| 565 |
); |
| 566 |
|
| 567 |
if ($result === false) { |
| 568 |
return new \WP_Error( |
| 569 |
'cant-delete', |
| 570 |
__('Your gallery could not be deleted.', 'vimeography'), |
| 571 |
array( |
| 572 |
'status' => 500 |
| 573 |
) |
| 574 |
); |
| 575 |
} |
| 576 |
|
| 577 |
do_action('vimeography-pro/delete-gallery', $id); |
| 578 |
|
| 579 |
require_once VIMEOGRAPHY_PATH . 'lib/deprecated/cache.php'; |
| 580 |
$cache = new \Vimeography_Cache($id); |
| 581 |
if ($cache->exists()) { |
| 582 |
$cache->delete(); |
| 583 |
} |
| 584 |
|
| 585 |
return new \WP_REST_Response(true, 204); |
| 586 |
} catch (Exception $e) { |
| 587 |
return new \WP_Error( |
| 588 |
'cant-delete', |
| 589 |
__('Your gallery could not be deleted.', 'vimeography'), |
| 590 |
array( |
| 591 |
'status' => 500 |
| 592 |
) |
| 593 |
); |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* Check if a given request has access to get items |
| 599 |
* |
| 600 |
* @param WP_REST_Request $request Full data about the request. |
| 601 |
* @return WP_Error|bool |
| 602 |
*/ |
| 603 |
public function get_items_permissions_check($request) |
| 604 |
{ |
| 605 |
return true; |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Check if a given request has access to get a specific item |
| 610 |
* |
| 611 |
* @param WP_REST_Request $request Full data about the request. |
| 612 |
* @return WP_Error|bool |
| 613 |
*/ |
| 614 |
public function get_item_permissions_check($request) |
| 615 |
{ |
| 616 |
return $this->get_items_permissions_check($request); |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* Check if a given request has access to create items |
| 621 |
* |
| 622 |
* @param WP_REST_Request $request Full data about the request. |
| 623 |
* @return WP_Error|bool |
| 624 |
*/ |
| 625 |
public function create_item_permissions_check($request) |
| 626 |
{ |
| 627 |
return current_user_can('edit_posts'); // admin |
| 628 |
} |
| 629 |
|
| 630 |
/** |
| 631 |
* Check if a given request has access to update a specific item |
| 632 |
* |
| 633 |
* @param WP_REST_Request $request Full data about the request. |
| 634 |
* @return WP_Error|bool |
| 635 |
*/ |
| 636 |
public function update_item_permissions_check($request) |
| 637 |
{ |
| 638 |
return $this->create_item_permissions_check($request); |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* Check if a given request has access to delete a specific item |
| 643 |
* |
| 644 |
* @param WP_REST_Request $request Full data about the request. |
| 645 |
* @return WP_Error|bool |
| 646 |
*/ |
| 647 |
public function delete_item_permissions_check($request) |
| 648 |
{ |
| 649 |
return $this->create_item_permissions_check($request); |
| 650 |
} |
| 651 |
|
| 652 |
/** |
| 653 |
* Prepare the item for create or update operation |
| 654 |
* |
| 655 |
* @param WP_REST_Request $request Request object |
| 656 |
* @return WP_Error|object $prepared_item |
| 657 |
*/ |
| 658 |
protected function prepare_item_for_database($request) |
| 659 |
{ |
| 660 |
return array(); |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* Prepare the item for the REST response |
| 665 |
* |
| 666 |
* @param mixed $item WordPress representation of the item. |
| 667 |
* @param WP_REST_Request $request Request object. |
| 668 |
* @return mixed |
| 669 |
*/ |
| 670 |
public function prepare_item_for_response($item, $request) |
| 671 |
{ |
| 672 |
return array(); |
| 673 |
} |
| 674 |
|
| 675 |
/** |
| 676 |
* Get the query params for collections |
| 677 |
* |
| 678 |
* @return array |
| 679 |
*/ |
| 680 |
public function get_collection_params() |
| 681 |
{ |
| 682 |
return array( |
| 683 |
'page' => array( |
| 684 |
'description' => 'Current page of the collection.', |
| 685 |
'type' => 'integer', |
| 686 |
'default' => 1, |
| 687 |
'sanitize_callback' => 'absint' |
| 688 |
), |
| 689 |
'per_page' => array( |
| 690 |
'description' => |
| 691 |
'Maximum number of items to be returned in result set.', |
| 692 |
'type' => 'integer', |
| 693 |
'default' => 10, |
| 694 |
'sanitize_callback' => 'absint' |
| 695 |
), |
| 696 |
'search' => array( |
| 697 |
'description' => 'Limit results to those matching a string.', |
| 698 |
'type' => 'string', |
| 699 |
'sanitize_callback' => 'sanitize_text_field' |
| 700 |
) |
| 701 |
); |
| 702 |
} |
| 703 |
|
| 704 |
/** |
| 705 |
* Returns whitelisted the ORDERBY string to use in the SQL query. |
| 706 |
* |
| 707 |
* @return string |
| 708 |
*/ |
| 709 |
private function _get_sort() |
| 710 |
{ |
| 711 |
if (!empty($_GET['orderby'])) { |
| 712 |
switch ($_GET['orderby']) { |
| 713 |
case 'id': |
| 714 |
case 'title': |
| 715 |
case 'date_created': |
| 716 |
$orderby = 'ORDER BY gallery.' . $_GET['orderby']; |
| 717 |
break; |
| 718 |
case 'theme_name': |
| 719 |
$orderby = 'ORDER BY meta.' . $_GET['orderby']; |
| 720 |
break; |
| 721 |
default: |
| 722 |
$orderby = 'ORDER BY gallery.id'; |
| 723 |
break; |
| 724 |
} |
| 725 |
} else { |
| 726 |
$orderby = 'ORDER BY gallery.id'; |
| 727 |
} |
| 728 |
|
| 729 |
if (!empty($_GET['order'])) { |
| 730 |
if ($_GET['order'] == 'asc' or $_GET['order'] == 'desc') { |
| 731 |
$order = strtoupper($_GET['order']); |
| 732 |
} |
| 733 |
} else { |
| 734 |
$order = 'ASC'; |
| 735 |
} |
| 736 |
return $orderby . ' ' . $order; |
| 737 |
} |
| 738 |
|
| 739 |
/** |
| 740 |
* Stores custom CSS for a gallery in the db using |
| 741 |
* a key that matches the incoming gallery id |
| 742 |
*/ |
| 743 |
public function update_gallery_appearance($request) |
| 744 |
{ |
| 745 |
$params = $request->get_params(); |
| 746 |
$gallery_id = intval($params['id']); |
| 747 |
|
| 748 |
$stylesheet_key = "vimeography_gallery_" . $gallery_id; |
| 749 |
|
| 750 |
// Requires WordPress 4.7 |
| 751 |
// https://github.com/WordPress/wordpress-develop/blob/6aca60d33a6f4c1e7e38dacdfcc6fb171af81831/tests/phpunit/tests/customize/custom-css-setting.php |
| 752 |
|
| 753 |
// this is an upsert operation. |
| 754 |
// https://github.com/WordPress/WordPress/blob/7ced0efbf4afa3c0eab3289596bcea9a4e367fca/wp-includes/theme.php#L1939 |
| 755 |
if (function_exists('wp_update_custom_css_post')) { |
| 756 |
$r = wp_update_custom_css_post($params['css'], array( |
| 757 |
'stylesheet' => $stylesheet_key // should be unique per gallery, copy on galleryduplicate |
| 758 |
)); |
| 759 |
|
| 760 |
return true; |
| 761 |
} else { |
| 762 |
return new \WP_Error( |
| 763 |
'cant-update', |
| 764 |
__('Your css could not be saved.', 'vimeography'), |
| 765 |
array( |
| 766 |
'status' => 500 |
| 767 |
) |
| 768 |
); |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
public function delete_gallery_appearance($request) |
| 773 |
{ |
| 774 |
$params = $request->get_params(); |
| 775 |
$gallery_id = intval($params['id']); |
| 776 |
$stylesheet_key = "vimeography_gallery_" + $gallery_id; |
| 777 |
|
| 778 |
$css_post = wp_get_custom_css_post($stylesheet_key); |
| 779 |
wp_delete_post($css_post->post_id); |
| 780 |
return true; |
| 781 |
} |
| 782 |
|
| 783 |
/** |
| 784 |
* Duplicate gallery via REST API |
| 785 |
*/ |
| 786 |
public function duplicate_gallery($request) |
| 787 |
{ |
| 788 |
$params = json_decode($request->get_body()); |
| 789 |
|
| 790 |
if ( |
| 791 |
isset($_POST['vimeography_duplicate_gallery_serialized']) && |
| 792 |
!empty($_POST['vimeography_duplicate_gallery_serialized']) |
| 793 |
) { |
| 794 |
$params = unserialize( |
| 795 |
stripslashes($_POST['vimeography_duplicate_gallery_serialized']) |
| 796 |
); |
| 797 |
} |
| 798 |
|
| 799 |
if ($params->copy_appearance === true) { |
| 800 |
// Do filesystem stuffs here… a simple file_put_contents would be nice. |
| 801 |
$_POST['vimeography_duplicate_gallery_serialized'] = serialize($params); |
| 802 |
|
| 803 |
$url = wp_nonce_url( |
| 804 |
network_admin_url( |
| 805 |
add_query_arg( |
| 806 |
array('page' => 'vimeography-edit-galleries'), |
| 807 |
'admin.php' |
| 808 |
) |
| 809 |
), |
| 810 |
'vimeography-duplicate-gallery-action', |
| 811 |
'vimeography-duplicate-gallery-verification' |
| 812 |
); |
| 813 |
|
| 814 |
$filesystem = new \Vimeography_Filesystem($url, array( |
| 815 |
'vimeography_duplicate_gallery_serialized', |
| 816 |
'vimeography-action' |
| 817 |
)); |
| 818 |
|
| 819 |
if ($filesystem->connect()) { |
| 820 |
$filesystem_connection = true; |
| 821 |
} else { |
| 822 |
exit(); |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
try { |
| 827 |
$id = intval($params->id); |
| 828 |
$title = sanitize_text_field($params->title); |
| 829 |
$source_url = sanitize_text_field($params->source_url); |
| 830 |
$resource_uri = \Vimeography::validate_vimeo_source($params->source_url); |
| 831 |
|
| 832 |
if (empty($title)) { |
| 833 |
return new \WP_Error( |
| 834 |
'cant-update', |
| 835 |
__('Make sure to give your new gallery a name!', 'vimeography'), |
| 836 |
array( |
| 837 |
'status' => 500 |
| 838 |
) |
| 839 |
); |
| 840 |
} |
| 841 |
|
| 842 |
global $wpdb; |
| 843 |
$duplicate = $wpdb->get_results( |
| 844 |
'SELECT * from ' . |
| 845 |
$wpdb->vimeography_gallery_meta . |
| 846 |
' AS meta JOIN ' . |
| 847 |
$wpdb->vimeography_gallery . |
| 848 |
' AS gallery ON meta.gallery_id = gallery.id WHERE meta.gallery_id = ' . |
| 849 |
$id . |
| 850 |
' LIMIT 1;' |
| 851 |
); |
| 852 |
|
| 853 |
$result = $wpdb->insert($wpdb->vimeography_gallery, array( |
| 854 |
'title' => $title, |
| 855 |
'date_created' => current_time('mysql'), |
| 856 |
'is_active' => 1 |
| 857 |
)); |
| 858 |
|
| 859 |
if ($result === false) { |
| 860 |
return new \WP_Error( |
| 861 |
'cant-update', |
| 862 |
__('Your gallery could not be duplicated.', 'vimeography'), |
| 863 |
array( |
| 864 |
'status' => 500 |
| 865 |
) |
| 866 |
); |
| 867 |
} |
| 868 |
|
| 869 |
$gallery_id = $wpdb->insert_id; |
| 870 |
$result = $wpdb->insert($wpdb->vimeography_gallery_meta, array( |
| 871 |
'gallery_id' => $gallery_id, |
| 872 |
'source_url' => $source_url, |
| 873 |
'video_limit' => $duplicate[0]->video_limit, |
| 874 |
'featured_video' => $duplicate[0]->featured_video, |
| 875 |
'cache_timeout' => $duplicate[0]->cache_timeout, |
| 876 |
'theme_name' => $duplicate[0]->theme_name, |
| 877 |
'resource_uri' => $resource_uri |
| 878 |
)); |
| 879 |
|
| 880 |
if ($result === false) { |
| 881 |
return new \WP_Error( |
| 882 |
'cant-update', |
| 883 |
__('Your gallery could not be duplicated.', 'vimeography'), |
| 884 |
array( |
| 885 |
'status' => 500 |
| 886 |
) |
| 887 |
); |
| 888 |
} |
| 889 |
|
| 890 |
if (isset($filesystem_connection)) { |
| 891 |
$old_filename = 'vimeography-gallery-' . $id . '-custom.css'; |
| 892 |
$old_filepath = VIMEOGRAPHY_CUSTOMIZATIONS_PATH . $old_filename; |
| 893 |
$search_string = '#vimeography-gallery-' . $id; |
| 894 |
|
| 895 |
$new_filename = 'vimeography-gallery-' . $gallery_id . '-custom.css'; |
| 896 |
$new_filepath = VIMEOGRAPHY_CUSTOMIZATIONS_PATH . $new_filename; |
| 897 |
$replace_string = '#vimeography-gallery-' . $gallery_id; |
| 898 |
|
| 899 |
global $wp_filesystem; |
| 900 |
|
| 901 |
if ($wp_filesystem->exists(VIMEOGRAPHY_CUSTOMIZATIONS_PATH)) { |
| 902 |
if ( |
| 903 |
$wp_filesystem->exists($old_filepath) and |
| 904 |
$wp_filesystem->is_file($old_filepath) |
| 905 |
) { |
| 906 |
$old_css = $wp_filesystem->get_contents($old_filepath); |
| 907 |
$new_css = str_ireplace($search_string, $replace_string, $old_css); |
| 908 |
|
| 909 |
// If there is an error, output a message for the user to see |
| 910 |
if ( |
| 911 |
!$wp_filesystem->put_contents( |
| 912 |
$new_filepath, |
| 913 |
$new_css, |
| 914 |
FS_CHMOD_FILE |
| 915 |
) |
| 916 |
) { |
| 917 |
return new \WP_Error( |
| 918 |
'cant-update', |
| 919 |
__( |
| 920 |
'There was an error writing your file. Please try again!', |
| 921 |
'vimeography' |
| 922 |
), |
| 923 |
array( |
| 924 |
'status' => 500 |
| 925 |
) |
| 926 |
); |
| 927 |
} |
| 928 |
} |
| 929 |
} elseif (function_exists('wp_update_custom_css_post')) { |
| 930 |
// if 4.7+, find styles in db and copy to new gallery. |
| 931 |
$styles = wp_get_custom_css('vimeography_gallery_' . $id); // returns css string |
| 932 |
|
| 933 |
if ($styles) { |
| 934 |
$search_string = '#vimeography-gallery-' . $id; |
| 935 |
$replace_string = '#vimeography-gallery-' . $gallery_id; |
| 936 |
|
| 937 |
$new_css = str_ireplace($search_string, $replace_string, $styles); |
| 938 |
|
| 939 |
$stylesheet_key = "vimeography_gallery_" . $gallery_id; |
| 940 |
|
| 941 |
$r = wp_update_custom_css_post($new_css, array( |
| 942 |
'stylesheet' => $stylesheet_key |
| 943 |
)); |
| 944 |
} |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
do_action('vimeography-pro/duplicate-gallery', $id, $gallery_id); |
| 949 |
do_action('vimeography/reload-galleries'); |
| 950 |
|
| 951 |
return new \WP_REST_Response(null, 201); |
| 952 |
} catch (\Vimeography_Exception $e) { |
| 953 |
return new \WP_Error( |
| 954 |
'cant-update', |
| 955 |
__( |
| 956 |
'Your gallery could not be duplicated. ' . $e->getMessage(), |
| 957 |
'vimeography' |
| 958 |
), |
| 959 |
array( |
| 960 |
'status' => 500 |
| 961 |
) |
| 962 |
); |
| 963 |
} |
| 964 |
} |
| 965 |
} |
| 966 |
|