PluginProbe
CatFolders – WordPress Media Library Folders & Categories / trunk
CatFolders – WordPress Media Library Folders & Categories vtrunk
2.5.6 2.5.5 trunk 1.6.1 1.8 1.9 2.0 2.2 2.3.1 2.3.2 2.4.4 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4
catfolders / includes / Rest / Controllers / FolderController.php

FolderController.php in CatFolders – WordPress Media Library Folders & Categories trunk, at includes/Rest/Controllers/FolderController.php

310 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace CatFolders\Rest\Controllers;
3
4 use CatFolders\Models\FolderModel;
5 use CatFolders\Core\Base;
6 use CatFolders\Classes\Helpers;
7
8 class FolderController extends Base {
9 public function __construct() {
10 parent::initialize();
11 }
12
13 public function register_routes() {
14 register_rest_route(
15 CATF_ROUTE_NAMESPACE,
16 '/folders',
17 array(
18 array(
19 'methods' => \WP_REST_Server::READABLE,
20 'callback' => array( $this, 'get_folders' ),
21 'permission_callback' => array( $this, 'permission_callback' ),
22 ),
23 array(
24 'methods' => \WP_REST_Server::CREATABLE,
25 'callback' => array( $this, 'new_folder' ),
26 'permission_callback' => array( $this, 'permission_callback' ),
27 ),
28 )
29 );
30
31 register_rest_route(
32 CATF_ROUTE_NAMESPACE,
33 '/folders/(?P<folderId>\d+)',
34 array(
35 array(
36 'methods' => \WP_REST_Server::CREATABLE,
37 'callback' => array( $this, 'update_folder' ),
38 'permission_callback' => array( $this, 'permission_callback' ),
39 ),
40 )
41 );
42
43 register_rest_route(
44 CATF_ROUTE_NAMESPACE,
45 '/folder-position/(?P<folderId>\d+)',
46 array(
47 array(
48 'methods' => \WP_REST_Server::CREATABLE,
49 'callback' => array( $this, 'update_folder_position' ),
50 'permission_callback' => array( $this, 'permission_callback' ),
51 ),
52 )
53 );
54
55 register_rest_route(
56 CATF_ROUTE_NAMESPACE,
57 '/delete-folders',
58 array(
59 array(
60 'methods' => \WP_REST_Server::CREATABLE,
61 'callback' => array( $this, 'delete_folder' ),
62 'permission_callback' => array( $this, 'permission_callback' ),
63 ),
64 )
65 );
66
67 register_rest_route(
68 CATF_ROUTE_NAMESPACE,
69 '/attachment-to-folder',
70 array(
71 array(
72 'methods' => \WP_REST_Server::CREATABLE,
73 'callback' => array( $this, 'set_attachment_to_folder' ),
74 'permission_callback' => array( $this, 'permission_callback' ),
75 ),
76 )
77 );
78
79 register_rest_route(
80 CATF_ROUTE_NAMESPACE,
81 '/sort-folders',
82 array(
83 array(
84 'methods' => \WP_REST_Server::CREATABLE,
85 'callback' => array( $this, 'sort_folders' ),
86 'permission_callback' => array( $this, 'permission_callback' ),
87 ),
88 )
89 );
90
91 register_rest_route(
92 CATF_ROUTE_NAMESPACE,
93 '/set-settings',
94 array(
95 array(
96 'methods' => \WP_REST_Server::CREATABLE,
97 'callback' => array( $this, 'set_settings' ),
98 'permission_callback' => array( $this, 'permission_callback' ),
99 ),
100 )
101 );
102
103 register_rest_route(
104 CATF_ROUTE_NAMESPACE,
105 '/folder-property/(?P<folderId>\d+)',
106 array(
107 array(
108 'methods' => \WP_REST_Server::READABLE,
109 'callback' => array( $this, 'get_folder_property' ),
110 'permission_callback' => array( $this, 'permission_callback' ),
111 ),
112 )
113 );
114 register_rest_route(
115 CATF_ROUTE_NAMESPACE,
116 '/folder-counter',
117 array(
118 array(
119 'methods' => \WP_REST_Server::READABLE,
120 'callback' => array( $this, 'get_folder_counter' ),
121 'permission_callback' => array( $this, 'permission_callback' ),
122 ),
123 )
124 );
125 }
126
127 public function permission_callback() {
128 return current_user_can( 'upload_files' );
129 }
130
131 public function get_folders( \WP_REST_Request $request ) {
132 $orderBy = sanitize_key( $request->get_param( 'orderby' ) );
133 $orderType = sanitize_key( $request->get_param( 'ordertype' ) );
134
135 //Get all folders
136 $result = FolderModel::get_all(
137 array(
138 'orderBy' => $orderBy,
139 'orderType' => $orderType,
140 )
141 );
142
143 //Return the response as Json format
144 return new \WP_REST_Response( $result );
145 }
146
147 public function sort_folders( \WP_REST_Request $request ) {
148 global $wpdb;
149
150 $parentId = \intval( $request->get_param( 'toParentId' ) );
151 $nodeId = \intval( $request->get_param( 'nodeId' ) );
152 $nextSiblingId = $request->get_param( 'nextSiblingId' );
153 $nextSiblingId = isset( $nextSiblingId ) ? \intval( $nextSiblingId ) : null;
154
155 FolderModel::updateFolder( $nodeId, array( 'parent' => $parentId ) );
156
157 if ( is_null( $nextSiblingId ) ) {
158 FolderModel::updateToMaxOrd( $nodeId, $parentId );
159 } else {
160 //TODO test this case
161 $oldList = FolderModel::builder()
162 ->select( 'id, ord' )
163 ->where( array( 'parent' => $parentId ) )
164 ->order_by( 'ord+0' )
165 ->get();
166
167 $ord = 0;
168 $q = '';
169 foreach ( $oldList as $folder ) {
170 if ( $folder->id == $nodeId ) {
171 continue;
172 }
173 if ( $folder->id == $nextSiblingId ) {
174 $q .= "($nodeId,$ord),(" . $folder->id . ',' . ( ++$ord ) . '),';
175 } else {
176 $q .= "($folder->id,$ord),";
177 }
178 $ord++;
179 }
180 $q = rtrim( $q, ',' );
181
182 FolderModel::builder()->query( 'INSERT INTO ' . $wpdb->prefix . self::CAT_FOLDERS_TABLE . ' (id, ord) VALUES ' . $q . ' ON DUPLICATE KEY UPDATE ord=VALUES(ord)' );
183 }
184 return new \WP_REST_Response( array( 'success' => true ) );
185 }
186
187 public function new_folder( \WP_REST_Request $request ) {
188 $title = Helpers::sanitize_array( $request->get_param( 'title' ) );
189 $parent = \intval( $request->get_param( 'parent' ) );
190 $type = Helpers::sanitize_array( $request->get_param( 'type' ) ) ?? 'attachment';
191
192 if ( '' === trim( $title ) || '' === $parent ) {
193 return new \WP_Error( 500, __( 'Validation failed', 'catfolders' ) );
194 }
195
196 if ( FolderModel::isExistingFolderName( 0, $title, $parent ) ) {
197 return new \WP_Error( 500, __( 'A folder with this name already exists. Please choose another one.', 'catfolders' ) );
198 }
199
200 $result = FolderModel::createFolder(
201 array(
202 'title' => $title,
203 'parent' => $parent,
204 'type' => $type,
205 )
206 );
207 return new \WP_REST_Response( $result );
208 }
209
210 public function update_folder( \WP_REST_Request $request ) {
211 $folderId = \intval( $request->get_param( 'folderId' ) );
212 $parent = \intval( $request->get_param( 'parent' ) );
213 $title = Helpers::sanitize_array( $request->get_param( 'title' ) );
214
215 if ( '' === trim( $title ) ) {
216 return new \WP_Error( 500, __( 'Validation failed', 'catfolders' ) );
217 }
218
219 if ( FolderModel::isExistingFolderName( $folderId, $title, $parent ) ) {
220 return new \WP_Error( 500, __( 'A folder with this name already exists. Please choose another one.', 'catfolders' ) );
221 }
222
223 FolderModel::updateFolder(
224 $folderId,
225 array(
226 'title' => $title,
227 )
228 );
229
230 $folder = FolderModel::find( $folderId );
231
232 if ( is_null( $folder ) ) {
233 return new \WP_Error( 500, __( 'Something is wrong! Please try again', 'catfolders' ) );
234 }
235
236 return new \WP_REST_Response( $folder->attributes );
237 }
238
239 public function update_folder_position( \WP_REST_Request $request ) {
240 $folderId = \intval( $request->get_param( 'folderId' ) );
241 $position = \intval( $request->get_param( 'position' ) );
242 $parent = \intval( $request->get_param( 'parent' ) );
243
244 FolderModel::updatePositionAndParent( $folderId, $position, $parent );
245 return new \WP_REST_Response();
246 }
247
248 public function delete_folder( \WP_REST_Request $request ) {
249 $ids = $request->get_param( 'ids' );
250 $ids = is_array( $ids ) ? array_map( 'intval', $ids ) : intval( $ids );
251 $result = FolderModel::deleteFolder( $ids );
252 return new \WP_REST_Response( $result );
253 }
254
255 public function set_attachment_to_folder( \WP_REST_Request $request ) {
256 $folderId = intval( $request->get_param( 'folderId' ) );
257 $imgIds = array_map( 'intval', $request->get_param( 'imgIds' ) );
258
259 $result = FolderModel::set_attachments( $folderId, $imgIds );
260 return new \WP_REST_Response( $result );
261 }
262
263 public function get_inside_folder_detail( $folderId ) {
264 $children = FolderModel::get_children_ids( array( $folderId ) );
265
266 $totalChildren = count( $children );
267 $totalItems = count( FolderModel::getPostIdsFromFolder( array_merge( $children, array( $folderId ) ) ) );
268
269 return array(
270 'totalChildren' => $totalChildren,
271 'totalItems' => $totalItems,
272 );
273 }
274
275 public function get_folder_property( \WP_REST_Request $request ) {
276 $folderId = \intval( $request->get_param( 'folderId' ) );
277 $folder = FolderModel::find_where( array( 'id' => $folderId ) );
278
279 $counters = $this->get_inside_folder_detail( $folderId );
280
281 if ( ! empty( $folder ) ) {
282 $postTypeName = get_post_type_object( $folder->attributes['type'] )->labels->singular_name;
283
284 $result = array(
285 'type_name' => $postTypeName,
286 'folder_name' => $folder->attributes['title'],
287 'total_children' => $counters['totalChildren'],
288 'total_items' => $counters['totalItems'],
289 );
290 return new \WP_REST_Response( $result );
291 }
292 return new \WP_Error( '400', __( 'Folder doesn\'t exist!', 'catfolders' ) );
293 }
294
295 public function set_settings( \WP_REST_Request $request ) {
296 $userSettings = array(
297 'sortFolder' => Helpers::sanitize_array( $request->get_param( 'sortFolder' ) ),
298 'sortFile' => Helpers::sanitize_array( $request->get_param( 'sortFile' ) ),
299 'startupFolder' => Helpers::sanitize_array( $request->get_param( 'startupMode' ) ),
300 );
301
302 $this->updateUserSettings( $userSettings );
303
304 wp_send_json_success();
305 }
306 public function get_folder_counter( \WP_REST_Request $request ) {
307 return new \WP_REST_Response( FolderModel::get_folders_counter( true ) );
308 }
309 }
310