| 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 && !is_plugin_active('vimeography-pro/vimeography-pro.php')) { |
| 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 |
'permission_callback' => '__return_true' |
| 266 |
)); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Get a collection of items |
| 271 |
* |
| 272 |
* @param WP_REST_Request $request Full data about the request. |
| 273 |
* @return WP_Error|WP_REST_Response |
| 274 |
*/ |
| 275 |
public function get_items($request) |
| 276 |
{ |
| 277 |
$items = array(); //do a query, call another class, etc |
| 278 |
$data = array(); |
| 279 |
|
| 280 |
global $wpdb; |
| 281 |
|
| 282 |
if (isset($_GET['s']) && !empty($_GET['s'])) { |
| 283 |
$term = sanitize_text_field($_GET['s']); |
| 284 |
|
| 285 |
if (intval($term) == 0) { |
| 286 |
$filter = $wpdb->prepare('WHERE gallery.title LIKE %s ', '%' . $term . '%'); |
| 287 |
} else { |
| 288 |
$filter = $wpdb->prepare('WHERE gallery.id = %d ', $term); |
| 289 |
} |
| 290 |
} else { |
| 291 |
$filter = ''; |
| 292 |
} |
| 293 |
|
| 294 |
$sort = $this->_get_sort(); |
| 295 |
$result = $wpdb->get_results( |
| 296 |
'SELECT * FROM ' . $wpdb->vimeography_gallery_meta . ' AS meta JOIN ' . $wpdb->vimeography_gallery . ' AS gallery ON meta.gallery_id = gallery.id ' . $filter . $sort . ';', |
| 297 |
ARRAY_A |
| 298 |
); |
| 299 |
// echo '<pre>'; |
| 300 |
// var_dump($result); |
| 301 |
// echo '</pre>'; |
| 302 |
// die; |
| 303 |
return $result; |
| 304 |
|
| 305 |
foreach ($items as $item) { |
| 306 |
$itemdata = $this->prepare_item_for_response($item, $request); |
| 307 |
$data[] = $this->prepare_response_for_collection($itemdata); |
| 308 |
} |
| 309 |
|
| 310 |
return new WP_REST_Response($data, 200); |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Get one item from the collection |
| 315 |
* |
| 316 |
* @param WP_REST_Request $request Full data about the request. |
| 317 |
* @return WP_Error|WP_REST_Response |
| 318 |
*/ |
| 319 |
public function get_item($request) |
| 320 |
{ |
| 321 |
$params = $request->get_params(); |
| 322 |
$gallery_id = intval($params['id']); |
| 323 |
|
| 324 |
global $wpdb; |
| 325 |
|
| 326 |
$result = $wpdb->get_results( |
| 327 |
$wpdb->prepare( |
| 328 |
' |
| 329 |
SELECT * |
| 330 |
FROM ' . $wpdb->vimeography_gallery_meta . ' AS meta |
| 331 |
JOIN ' . $wpdb->vimeography_gallery . ' AS gallery |
| 332 |
ON meta.gallery_id = gallery.id |
| 333 |
WHERE meta.gallery_id = %d |
| 334 |
LIMIT 1; |
| 335 |
', |
| 336 |
$gallery_id |
| 337 |
) |
| 338 |
); |
| 339 |
|
| 340 |
$settings = $result[0]; |
| 341 |
|
| 342 |
$settings = new \stdClass(); |
| 343 |
$settings->id = intval($result[0]->gallery_id); |
| 344 |
$settings->date_created = $result[0]->date_created; |
| 345 |
$settings->cache_timeout = intval($result[0]->cache_timeout); |
| 346 |
$settings->featured_video = $result[0]->featured_video; |
| 347 |
$settings->gallery_width = $result[0]->gallery_width; |
| 348 |
$settings->resource_uri = $result[0]->resource_uri; |
| 349 |
$settings->source_url = $result[0]->source_url; |
| 350 |
$settings->theme_name = $result[0]->theme_name; |
| 351 |
$settings->title = $result[0]->title; |
| 352 |
$settings->video_limit = intval($result[0]->video_limit); |
| 353 |
|
| 354 |
// need to add pro settings like this |
| 355 |
// apply_filters('vimeography/gallery-settings', $this->_gallery); |
| 356 |
|
| 357 |
if (is_plugin_active('vimeography-pro/vimeography-pro.php')) { |
| 358 |
$pro_settings = $wpdb->get_results( |
| 359 |
$wpdb->prepare( |
| 360 |
' |
| 361 |
SELECT * |
| 362 |
FROM ' . $wpdb->vimeography_pro_meta . ' AS pro |
| 363 |
WHERE pro.gallery_id = %d |
| 364 |
LIMIT 1; |
| 365 |
', |
| 366 |
$gallery_id |
| 367 |
) |
| 368 |
); |
| 369 |
|
| 370 |
if (empty($pro_settings)) { |
| 371 |
$pro_settings = \Vimeography_Pro()->database->add_default_settings( |
| 372 |
$gallery_id |
| 373 |
); |
| 374 |
} |
| 375 |
|
| 376 |
$settings->allow_downloads = (bool) $pro_settings[0]->allow_downloads; |
| 377 |
$settings->sort = $pro_settings[0]->sort; |
| 378 |
$settings->direction = $pro_settings[0]->direction; |
| 379 |
$settings->enable_search = (bool) $pro_settings[0]->enable_search; |
| 380 |
$settings->enable_tags = (bool) $pro_settings[0]->enable_tags; |
| 381 |
$settings->enable_playlist = (bool) $pro_settings[0]->playlist; |
| 382 |
$settings->videos_per_page = intval($pro_settings[0]->per_page); |
| 383 |
} |
| 384 |
|
| 385 |
return $settings; |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Create one item from the collection |
| 390 |
* |
| 391 |
* @param WP_REST_Request $request Full data about the request. |
| 392 |
* @return WP_Error|WP_REST_Response |
| 393 |
*/ |
| 394 |
public function create_item($request) |
| 395 |
{ |
| 396 |
$item = $this->prepare_item_for_database($request); |
| 397 |
|
| 398 |
if (function_exists('slug_some_function_to_create_item')) { |
| 399 |
$data = slug_some_function_to_create_item($item); |
| 400 |
if (is_array($data)) { |
| 401 |
return new WP_REST_Response($data, 200); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
return new WP_Error('cant-create', __('message', 'text-domain'), array( |
| 406 |
'status' => 500 |
| 407 |
)); |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Update one item from the collection |
| 412 |
* |
| 413 |
* @param WP_REST_Request $request Full data about the request. |
| 414 |
* @return WP_Error|WP_REST_Response |
| 415 |
*/ |
| 416 |
public function update_item($request) |
| 417 |
{ |
| 418 |
$params = $request->get_params(); |
| 419 |
$data = array(); |
| 420 |
$format = array(); |
| 421 |
global $wpdb; |
| 422 |
|
| 423 |
if (isset($params['title']) && $params['title'] !== "") { |
| 424 |
$result = $wpdb->update( |
| 425 |
$wpdb->vimeography_gallery, |
| 426 |
array('title' => $params['title']), |
| 427 |
array('id' => $params['id']), |
| 428 |
array('%s'), |
| 429 |
array('%d') |
| 430 |
); |
| 431 |
|
| 432 |
if ($result === false) { |
| 433 |
return new \WP_Error('cant-update', __('Could not update gallery title', 'vimeography'), array( |
| 434 |
'status' => 500 |
| 435 |
)); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
if (isset($params['cache_timeout'])) { |
| 440 |
$data['cache_timeout'] = $params['cache_timeout']; |
| 441 |
$format[] = '%d'; |
| 442 |
} |
| 443 |
|
| 444 |
if (isset($params['video_limit'])) { |
| 445 |
$data['video_limit'] = $params['video_limit']; |
| 446 |
$format[] = '%d'; |
| 447 |
} |
| 448 |
|
| 449 |
if (isset($params['featured_video'])) { |
| 450 |
$data['featured_video'] = $params['featured_video']; |
| 451 |
$format[] = '%s'; |
| 452 |
} |
| 453 |
|
| 454 |
if (isset($params['gallery_width'])) { |
| 455 |
$data['gallery_width'] = $params['gallery_width']; |
| 456 |
$format[] = '%s'; |
| 457 |
} |
| 458 |
|
| 459 |
if (isset($params['source_url']) && $params['source_url'] !== "") { |
| 460 |
try { |
| 461 |
$data['resource_uri'] = \Vimeography::validate_vimeo_source( $params['source_url'] ); |
| 462 |
$format[] = '%s'; |
| 463 |
|
| 464 |
$data['source_url'] = $params['source_url']; |
| 465 |
$format[] = '%s'; |
| 466 |
} catch (Error $e) { |
| 467 |
return new \WP_Error('cant-update', __('Invalid Vimeo collection URL.', 'vimeography'), array( |
| 468 |
'status' => 500 |
| 469 |
)); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
$result = $wpdb->update( |
| 474 |
$wpdb->vimeography_gallery_meta, |
| 475 |
$data, |
| 476 |
array('gallery_id' => $params['id']), |
| 477 |
$format, |
| 478 |
array('%d') |
| 479 |
); |
| 480 |
|
| 481 |
if ($result === false) { |
| 482 |
return new \WP_Error('cant-update', __('Could not update gallery metadata', 'vimeography'), array( |
| 483 |
'status' => 500 |
| 484 |
)); |
| 485 |
} |
| 486 |
|
| 487 |
if (is_plugin_active('vimeography-pro/vimeography-pro.php')) { |
| 488 |
try { |
| 489 |
$data = array(); |
| 490 |
$format = array(); |
| 491 |
|
| 492 |
if (isset($params['videos_per_page'])) { |
| 493 |
$data['per_page'] = $params['videos_per_page']; |
| 494 |
$format[] = '%d'; |
| 495 |
} |
| 496 |
|
| 497 |
if (isset($params['sort'])) { |
| 498 |
$data['sort'] = $params['sort']; |
| 499 |
$format[] = '%s'; |
| 500 |
} |
| 501 |
|
| 502 |
if (isset($params['direction'])) { |
| 503 |
$data['direction'] = $params['direction']; |
| 504 |
$format[] = '%s'; |
| 505 |
} |
| 506 |
|
| 507 |
if (isset($params['enable_playlist'])) { |
| 508 |
$data['playlist'] = $params['enable_playlist'] === true ? 1 : 0; |
| 509 |
$format[] = '%d'; |
| 510 |
} |
| 511 |
|
| 512 |
if (isset($params['allow_downloads'])) { |
| 513 |
$data['allow_downloads'] = |
| 514 |
$params['allow_downloads'] === true ? 1 : 0; |
| 515 |
$format[] = '%d'; |
| 516 |
} |
| 517 |
|
| 518 |
if (isset($params['enable_search'])) { |
| 519 |
$data['enable_search'] = $params['enable_search'] === true ? 1 : 0; |
| 520 |
$format[] = '%d'; |
| 521 |
} |
| 522 |
|
| 523 |
if (isset($params['enable_tags'])) { |
| 524 |
$data['enable_tags'] = $params['enable_tags'] === true ? 1 : 0; |
| 525 |
$format[] = '%d'; |
| 526 |
} |
| 527 |
|
| 528 |
$result = $wpdb->update( |
| 529 |
$wpdb->vimeography_pro_meta, |
| 530 |
$data, |
| 531 |
array('gallery_id' => $params['id']), |
| 532 |
$format, |
| 533 |
array('%d') |
| 534 |
); |
| 535 |
|
| 536 |
if ($result === false) { |
| 537 |
return new \WP_Error( |
| 538 |
'cant-update', |
| 539 |
__('message', 'text-domain'), |
| 540 |
array( |
| 541 |
'status' => 500 |
| 542 |
) |
| 543 |
); |
| 544 |
} |
| 545 |
} catch (Exception $e) { |
| 546 |
return new \WP_Error('cant-update', __('message', 'text-domain'), array( |
| 547 |
'status' => 500 |
| 548 |
)); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
require_once VIMEOGRAPHY_PATH . 'lib/deprecated/cache.php'; |
| 553 |
$cache = new \Vimeography_Cache($params['id']); |
| 554 |
|
| 555 |
if ($cache->exists()) { |
| 556 |
$cache->delete(); |
| 557 |
} |
| 558 |
|
| 559 |
return new \WP_REST_Response($result, 200); |
| 560 |
} |
| 561 |
|
| 562 |
/** |
| 563 |
* Delete one item from the collection |
| 564 |
* |
| 565 |
* @param WP_REST_Request $request Full data about the request. |
| 566 |
* @return WP_Error|WP_REST_Response |
| 567 |
*/ |
| 568 |
public function delete_item($request) |
| 569 |
{ |
| 570 |
$params = $request->get_params(); |
| 571 |
$id = intval($params['id']); |
| 572 |
|
| 573 |
try { |
| 574 |
global $wpdb; |
| 575 |
$result = $wpdb->query( |
| 576 |
$wpdb->prepare( |
| 577 |
'DELETE gallery, meta FROM ' . $wpdb->vimeography_gallery . ' gallery, ' . $wpdb->vimeography_gallery_meta . ' meta WHERE gallery.id = %d AND meta.gallery_id = %d;', |
| 578 |
$id, |
| 579 |
$id |
| 580 |
) |
| 581 |
); |
| 582 |
|
| 583 |
if ($result === false) { |
| 584 |
return new \WP_Error( |
| 585 |
'cant-delete', |
| 586 |
__('Your gallery could not be deleted.', 'vimeography'), |
| 587 |
array( |
| 588 |
'status' => 500 |
| 589 |
) |
| 590 |
); |
| 591 |
} |
| 592 |
|
| 593 |
do_action('vimeography-pro/delete-gallery', $id); |
| 594 |
|
| 595 |
require_once VIMEOGRAPHY_PATH . 'lib/deprecated/cache.php'; |
| 596 |
$cache = new \Vimeography_Cache($id); |
| 597 |
if ($cache->exists()) { |
| 598 |
$cache->delete(); |
| 599 |
} |
| 600 |
|
| 601 |
return new \WP_REST_Response(true, 204); |
| 602 |
} catch (Exception $e) { |
| 603 |
return new \WP_Error( |
| 604 |
'cant-delete', |
| 605 |
__('Your gallery could not be deleted.', 'vimeography'), |
| 606 |
array( |
| 607 |
'status' => 500 |
| 608 |
) |
| 609 |
); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Check if a given request has access to get items |
| 615 |
* |
| 616 |
* @param WP_REST_Request $request Full data about the request. |
| 617 |
* @return WP_Error|bool |
| 618 |
*/ |
| 619 |
public function get_items_permissions_check($request) |
| 620 |
{ |
| 621 |
return true; |
| 622 |
} |
| 623 |
|
| 624 |
/** |
| 625 |
* Check if a given request has access to get a specific item |
| 626 |
* |
| 627 |
* @param WP_REST_Request $request Full data about the request. |
| 628 |
* @return WP_Error|bool |
| 629 |
*/ |
| 630 |
public function get_item_permissions_check($request) |
| 631 |
{ |
| 632 |
return $this->get_items_permissions_check($request); |
| 633 |
} |
| 634 |
|
| 635 |
/** |
| 636 |
* Check if a given request has access to create items |
| 637 |
* |
| 638 |
* @param WP_REST_Request $request Full data about the request. |
| 639 |
* @return WP_Error|bool |
| 640 |
*/ |
| 641 |
public function create_item_permissions_check($request) |
| 642 |
{ |
| 643 |
return current_user_can('manage_options'); // admin |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* Check if a given request has access to update a specific item |
| 648 |
* |
| 649 |
* @param WP_REST_Request $request Full data about the request. |
| 650 |
* @return WP_Error|bool |
| 651 |
*/ |
| 652 |
public function update_item_permissions_check($request) |
| 653 |
{ |
| 654 |
return $this->create_item_permissions_check($request); |
| 655 |
} |
| 656 |
|
| 657 |
/** |
| 658 |
* Check if a given request has access to delete a specific item |
| 659 |
* |
| 660 |
* @param WP_REST_Request $request Full data about the request. |
| 661 |
* @return WP_Error|bool |
| 662 |
*/ |
| 663 |
public function delete_item_permissions_check($request) |
| 664 |
{ |
| 665 |
return $this->create_item_permissions_check($request); |
| 666 |
} |
| 667 |
|
| 668 |
/** |
| 669 |
* Prepare the item for create or update operation |
| 670 |
* |
| 671 |
* @param WP_REST_Request $request Request object |
| 672 |
* @return WP_Error|object $prepared_item |
| 673 |
*/ |
| 674 |
protected function prepare_item_for_database($request) |
| 675 |
{ |
| 676 |
return array(); |
| 677 |
} |
| 678 |
|
| 679 |
/** |
| 680 |
* Prepare the item for the REST response |
| 681 |
* |
| 682 |
* @param mixed $item WordPress representation of the item. |
| 683 |
* @param WP_REST_Request $request Request object. |
| 684 |
* @return mixed |
| 685 |
*/ |
| 686 |
public function prepare_item_for_response($item, $request) |
| 687 |
{ |
| 688 |
return array(); |
| 689 |
} |
| 690 |
|
| 691 |
/** |
| 692 |
* Get the query params for collections |
| 693 |
* |
| 694 |
* @return array |
| 695 |
*/ |
| 696 |
public function get_collection_params() |
| 697 |
{ |
| 698 |
return array( |
| 699 |
'page' => array( |
| 700 |
'description' => 'Current page of the collection.', |
| 701 |
'type' => 'integer', |
| 702 |
'default' => 1, |
| 703 |
'sanitize_callback' => 'absint' |
| 704 |
), |
| 705 |
'per_page' => array( |
| 706 |
'description' => |
| 707 |
'Maximum number of items to be returned in result set.', |
| 708 |
'type' => 'integer', |
| 709 |
'default' => 10, |
| 710 |
'sanitize_callback' => 'absint' |
| 711 |
), |
| 712 |
'search' => array( |
| 713 |
'description' => 'Limit results to those matching a string.', |
| 714 |
'type' => 'string', |
| 715 |
'sanitize_callback' => 'sanitize_text_field' |
| 716 |
) |
| 717 |
); |
| 718 |
} |
| 719 |
|
| 720 |
/** |
| 721 |
* Returns whitelisted the ORDERBY string to use in the SQL query. |
| 722 |
* |
| 723 |
* @return string |
| 724 |
*/ |
| 725 |
private function _get_sort() |
| 726 |
{ |
| 727 |
if (!empty($_GET['orderby'])) { |
| 728 |
switch ($_GET['orderby']) { |
| 729 |
case 'id': |
| 730 |
case 'title': |
| 731 |
case 'date_created': |
| 732 |
$orderby = 'ORDER BY gallery.' . $_GET['orderby']; |
| 733 |
break; |
| 734 |
case 'theme_name': |
| 735 |
$orderby = 'ORDER BY meta.' . $_GET['orderby']; |
| 736 |
break; |
| 737 |
default: |
| 738 |
$orderby = 'ORDER BY gallery.id'; |
| 739 |
break; |
| 740 |
} |
| 741 |
} else { |
| 742 |
$orderby = 'ORDER BY gallery.id'; |
| 743 |
} |
| 744 |
|
| 745 |
if (!empty($_GET['order'])) { |
| 746 |
if ($_GET['order'] == 'asc' or $_GET['order'] == 'desc') { |
| 747 |
$order = strtoupper($_GET['order']); |
| 748 |
} |
| 749 |
} else { |
| 750 |
$order = 'ASC'; |
| 751 |
} |
| 752 |
return $orderby . ' ' . $order; |
| 753 |
} |
| 754 |
|
| 755 |
/** |
| 756 |
* Stores custom CSS for a gallery in the db using |
| 757 |
* a key that matches the incoming gallery id |
| 758 |
*/ |
| 759 |
public function update_gallery_appearance($request) |
| 760 |
{ |
| 761 |
$params = $request->get_params(); |
| 762 |
$gallery_id = intval($params['id']); |
| 763 |
|
| 764 |
$stylesheet_key = "vimeography_gallery_" . $gallery_id; |
| 765 |
|
| 766 |
// Requires WordPress 4.7 |
| 767 |
// https://github.com/WordPress/wordpress-develop/blob/6aca60d33a6f4c1e7e38dacdfcc6fb171af81831/tests/phpunit/tests/customize/custom-css-setting.php |
| 768 |
|
| 769 |
// this is an upsert operation. |
| 770 |
// https://github.com/WordPress/WordPress/blob/7ced0efbf4afa3c0eab3289596bcea9a4e367fca/wp-includes/theme.php#L1939 |
| 771 |
if (function_exists('wp_update_custom_css_post')) { |
| 772 |
$r = wp_update_custom_css_post($params['css'], array( |
| 773 |
'stylesheet' => $stylesheet_key // should be unique per gallery, copy on galleryduplicate |
| 774 |
)); |
| 775 |
|
| 776 |
return true; |
| 777 |
} else { |
| 778 |
return new \WP_Error( |
| 779 |
'cant-update', |
| 780 |
__('Your css could not be saved.', 'vimeography'), |
| 781 |
array( |
| 782 |
'status' => 500 |
| 783 |
) |
| 784 |
); |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
public function delete_gallery_appearance($request) |
| 789 |
{ |
| 790 |
$params = $request->get_params(); |
| 791 |
$gallery_id = intval($params['id']); |
| 792 |
$stylesheet_key = "vimeography_gallery_" + $gallery_id; |
| 793 |
|
| 794 |
$css_post = wp_get_custom_css_post($stylesheet_key); |
| 795 |
wp_delete_post($css_post->post_id); |
| 796 |
return true; |
| 797 |
} |
| 798 |
|
| 799 |
/** |
| 800 |
* Duplicate gallery via REST API |
| 801 |
*/ |
| 802 |
public function duplicate_gallery($request) |
| 803 |
{ |
| 804 |
$params = json_decode($request->get_body()); |
| 805 |
|
| 806 |
if ( !empty( $request->get_param( 'vimeography_duplicate_gallery_serialized' ) ) ) { |
| 807 |
$params = json_decode( $request->get_param( 'vimeography_duplicate_gallery_serialized' ) ); |
| 808 |
$params = ( is_object( $params ) || is_array( $params ) ) ? (array) $params : []; |
| 809 |
} |
| 810 |
|
| 811 |
if ($params->copy_appearance === true) { |
| 812 |
// Do filesystem stuffs here… a simple file_put_contents would be nice. |
| 813 |
$_POST['vimeography_duplicate_gallery_serialized'] = json_encode($params); |
| 814 |
|
| 815 |
$url = wp_nonce_url( |
| 816 |
network_admin_url( |
| 817 |
add_query_arg( |
| 818 |
array('page' => 'vimeography-edit-galleries'), |
| 819 |
'admin.php' |
| 820 |
) |
| 821 |
), |
| 822 |
'vimeography-duplicate-gallery-action', |
| 823 |
'vimeography-duplicate-gallery-verification' |
| 824 |
); |
| 825 |
|
| 826 |
$filesystem = new \Vimeography_Filesystem($url, array( |
| 827 |
'vimeography_duplicate_gallery_serialized', |
| 828 |
'vimeography-action' |
| 829 |
)); |
| 830 |
|
| 831 |
if ($filesystem->connect()) { |
| 832 |
$filesystem_connection = true; |
| 833 |
} else { |
| 834 |
exit(); |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
try { |
| 839 |
$id = intval($params->id); |
| 840 |
$title = sanitize_text_field($params->title); |
| 841 |
$source_url = sanitize_text_field($params->source_url); |
| 842 |
$resource_uri = \Vimeography::validate_vimeo_source($params->source_url); |
| 843 |
|
| 844 |
if (empty($title)) { |
| 845 |
return new \WP_Error( |
| 846 |
'cant-update', |
| 847 |
__('Make sure to give your new gallery a name!', 'vimeography'), |
| 848 |
array( |
| 849 |
'status' => 500 |
| 850 |
) |
| 851 |
); |
| 852 |
} |
| 853 |
|
| 854 |
global $wpdb; |
| 855 |
$duplicate = $wpdb->get_results( |
| 856 |
$wpdb->prepare( |
| 857 |
' |
| 858 |
SELECT * |
| 859 |
FROM ' . $wpdb->vimeography_gallery_meta . ' AS meta |
| 860 |
JOIN ' . $wpdb->vimeography_gallery . ' AS gallery |
| 861 |
ON meta.gallery_id = gallery.id |
| 862 |
WHERE meta.gallery_id = %d |
| 863 |
LIMIT 1; |
| 864 |
', |
| 865 |
$id |
| 866 |
) |
| 867 |
); |
| 868 |
|
| 869 |
$result = $wpdb->insert($wpdb->vimeography_gallery, array( |
| 870 |
'title' => $title, |
| 871 |
'date_created' => current_time('mysql'), |
| 872 |
'is_active' => 1 |
| 873 |
)); |
| 874 |
|
| 875 |
if ($result === false) { |
| 876 |
return new \WP_Error( |
| 877 |
'cant-update', |
| 878 |
__('Your gallery could not be duplicated.', 'vimeography'), |
| 879 |
array( |
| 880 |
'status' => 500 |
| 881 |
) |
| 882 |
); |
| 883 |
} |
| 884 |
|
| 885 |
$gallery_id = $wpdb->insert_id; |
| 886 |
$result = $wpdb->insert($wpdb->vimeography_gallery_meta, array( |
| 887 |
'gallery_id' => $gallery_id, |
| 888 |
'source_url' => $source_url, |
| 889 |
'video_limit' => $duplicate[0]->video_limit, |
| 890 |
'featured_video' => $duplicate[0]->featured_video, |
| 891 |
'cache_timeout' => $duplicate[0]->cache_timeout, |
| 892 |
'theme_name' => $duplicate[0]->theme_name, |
| 893 |
'resource_uri' => $resource_uri |
| 894 |
)); |
| 895 |
|
| 896 |
if ($result === false) { |
| 897 |
return new \WP_Error( |
| 898 |
'cant-update', |
| 899 |
__('Your gallery could not be duplicated.', 'vimeography'), |
| 900 |
array( |
| 901 |
'status' => 500 |
| 902 |
) |
| 903 |
); |
| 904 |
} |
| 905 |
|
| 906 |
if (isset($filesystem_connection)) { |
| 907 |
$old_filename = 'vimeography-gallery-' . $id . '-custom.css'; |
| 908 |
$old_filepath = VIMEOGRAPHY_CUSTOMIZATIONS_PATH . $old_filename; |
| 909 |
$search_string = '#vimeography-gallery-' . $id; |
| 910 |
|
| 911 |
$new_filename = 'vimeography-gallery-' . $gallery_id . '-custom.css'; |
| 912 |
$new_filepath = VIMEOGRAPHY_CUSTOMIZATIONS_PATH . $new_filename; |
| 913 |
$replace_string = '#vimeography-gallery-' . $gallery_id; |
| 914 |
|
| 915 |
global $wp_filesystem; |
| 916 |
|
| 917 |
if ($wp_filesystem->exists(VIMEOGRAPHY_CUSTOMIZATIONS_PATH)) { |
| 918 |
if ( |
| 919 |
$wp_filesystem->exists($old_filepath) and |
| 920 |
$wp_filesystem->is_file($old_filepath) |
| 921 |
) { |
| 922 |
$old_css = $wp_filesystem->get_contents($old_filepath); |
| 923 |
$new_css = str_ireplace($search_string, $replace_string, $old_css); |
| 924 |
|
| 925 |
// If there is an error, output a message for the user to see |
| 926 |
if ( |
| 927 |
!$wp_filesystem->put_contents( |
| 928 |
$new_filepath, |
| 929 |
$new_css, |
| 930 |
FS_CHMOD_FILE |
| 931 |
) |
| 932 |
) { |
| 933 |
return new \WP_Error( |
| 934 |
'cant-update', |
| 935 |
__( |
| 936 |
'There was an error writing your file. Please try again!', |
| 937 |
'vimeography' |
| 938 |
), |
| 939 |
array( |
| 940 |
'status' => 500 |
| 941 |
) |
| 942 |
); |
| 943 |
} |
| 944 |
} |
| 945 |
} elseif (function_exists('wp_update_custom_css_post')) { |
| 946 |
// if 4.7+, find styles in db and copy to new gallery. |
| 947 |
// These are saved in the `wp_posts` table with a `post_title` |
| 948 |
// of vimeography_gallery_{n} |
| 949 |
$styles = wp_get_custom_css('vimeography_gallery_' . $id); // returns css string |
| 950 |
|
| 951 |
if ($styles) { |
| 952 |
$search_string = '#vimeography-gallery-' . $id; |
| 953 |
$replace_string = '#vimeography-gallery-' . $gallery_id; |
| 954 |
|
| 955 |
$new_css = str_ireplace($search_string, $replace_string, $styles); |
| 956 |
|
| 957 |
$stylesheet_key = "vimeography_gallery_" . $gallery_id; |
| 958 |
|
| 959 |
$r = wp_update_custom_css_post($new_css, array( |
| 960 |
'stylesheet' => $stylesheet_key |
| 961 |
)); |
| 962 |
} |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
do_action('vimeography-pro/duplicate-gallery', $id, $gallery_id); |
| 967 |
do_action('vimeography/reload-galleries'); |
| 968 |
|
| 969 |
return new \WP_REST_Response(null, 201); |
| 970 |
} catch (\Vimeography_Exception $e) { |
| 971 |
$message = $e->getMessage(); |
| 972 |
return new \WP_Error( |
| 973 |
'cant-update', |
| 974 |
__( |
| 975 |
"Your gallery could not be duplicated. $message", |
| 976 |
'vimeography' |
| 977 |
), |
| 978 |
array( |
| 979 |
'status' => 500 |
| 980 |
) |
| 981 |
); |
| 982 |
} |
| 983 |
} |
| 984 |
} |
| 985 |
|