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 / ExportController.php

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

62 lines 1.6 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 defined( 'ABSPATH' ) || exit;
5
6 class ExportController {
7 public function register_routes() {
8 register_rest_route(
9 CATF_ROUTE_NAMESPACE,
10 '/export-csv',
11 array(
12 'methods' => \WP_REST_Server::READABLE,
13 'callback' => array( $this, 'get_csv' ),
14 'permission_callback' => array( $this, 'permission_callback' ),
15 )
16 );
17 }
18
19 public function permission_callback() {
20 return current_user_can( 'upload_files' );
21 }
22
23 public function get_csv() {
24 global $wpdb;
25 if( ! current_user_can( 'manage_options' ) ) {
26 return new \WP_Error( 403, __( 'You are not allowed to export CSV.', 'catfolders' ) );
27 }
28 $folders = $wpdb->get_results( "Select * from {$wpdb->prefix}catfolders", ARRAY_A );
29 $tree = $this->get_nested_tree( $folders, 0, true );
30 return new \WP_REST_Response( array( 'folders' => $tree ) );
31 }
32
33 public function get_attachments( $folderId ) {
34 global $wpdb;
35
36 $attachments = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->prefix}catfolders_posts WHERE folder_id = %d", $folderId ) );
37
38 return $attachments;
39 }
40
41 public function get_nested_tree( $folders, $parent = 0, $flat = false ) {
42 $tree = array();
43
44 if ( $flat ) {
45 foreach ( $folders as $node ) {
46 $node['attachments'] = $this->get_attachments( $node['id'] );
47 $tree[] = $node;
48 }
49 } else {
50 foreach ( $folders as $node ) {
51 if ( $node['parent'] == $parent ) {
52 $node['children'] = $this->get_nested_tree( $folders, $node['id'] );
53 $node['attachments'] = $this->get_attachments( $node['id'] );
54 $tree[] = $node;
55 }
56 }
57 }
58
59 return $tree;
60 }
61 }
62