1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
<?php
namespace MatthiasWeb\RealMediaLibrary\rest;
use MatthiasWeb\RealMediaLibrary\base;
use MatthiasWeb\RealMediaLibrary\general;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
class Attachment extends base\Base {
public function rest_api_init() {
register_rest_route(Service::SERVICE_NAMESPACE, '/attachments/(?P<id>\d+)', array(
'methods' => 'PUT',
'callback' => array($this, 'updateItem')
));
register_rest_route(Service::SERVICE_NAMESPACE, '/attachments/(?P<id>\d+)/shortcutInfo', array(
'methods' => 'GET',
'callback' => array($this, 'routeShortcutInfo')
));
register_rest_route(Service::SERVICE_NAMESPACE, '/attachments/bulk/move', array(
'methods' => 'PUT',
'callback' => array($this, 'routeBulkMove')
));
}
public function updateItem($request) {
$folderId = $request->get_param('folderId');
$attachmentId = $request->get_param('id');
$nextId = $request->get_param('nextId');
$lastIdInView = $request->get_param('lastId');
if (!empty($folderId) && !empty($nextId)) {
wp_attachment_order_update($folderId, $attachmentId, $nextId, $lastIdInView);
}
}
public function routeShortcutInfo($request) {
$id = $request->get_param('id');
return new \WP_REST_Response(array('html' => \MatthiasWeb\RealMediaLibrary\attachment\CustomField::getInstance()->getShortcutInfoContainer($id)));
}
public function routeBulkMove($request) {
$ids = $request->get_param('ids');
$to = intval($request->get_param('to'));
$isCopy = $request->get_param('isCopy');
$isCopy = gettype($isCopy) === 'string' ? $isCopy === 'true' : $isCopy;
if (!is_array($ids) || count($ids) == 0 || $to == null) {
return new \WP_Error('rest_rcl_posts_bulk_move_failed', __('Something went wrong.', RCL_TD), array('status' => 500));
}
$result = wp_rml_move($to, $ids, false, $isCopy);
if (is_array($result)) {
return new \WP_Error('rest_rml_attachment_bulk_move_failed', implode(' ', $result), array('status' => 500));
}else{
wp_rml_structure_reset();
return new \WP_REST_Response(array(
'counts' => \MatthiasWeb\RealMediaLibrary\attachment\Structure::getInstance()->getFolderCounts()
));
}
}
}