| 1 |
<?php |
| 2 |
namespace FileBird\Controller; |
| 3 |
|
| 4 |
use FileBird\Model\Folder as FolderModel; |
| 5 |
use FileBird\Classes\Helpers as Helpers; |
| 6 |
use FileBird\Classes\Tree; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
class Api { |
| 11 |
public function restApi( $request ) { |
| 12 |
$act = $request->get_param( 'act' ); |
| 13 |
$act = isset( $act ) ? sanitize_text_field( $act ) : ''; |
| 14 |
if ( $act == 'generate-key' ) { |
| 15 |
$key = $this->generateRandomString( 40 ); |
| 16 |
update_option( 'fbv_rest_api_key', $key ); |
| 17 |
wp_send_json_success( array( 'key' => $key ) ); |
| 18 |
} |
| 19 |
wp_send_json_error( |
| 20 |
array( |
| 21 |
'mess' => __( 'Invalid action', 'filebird' ), |
| 22 |
) |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
private function addFolderCounter( $folders, $counter ) { |
| 27 |
if ( is_array( $folders ) ) { |
| 28 |
foreach ( $folders as $k => $v ) { |
| 29 |
$folders[ $k ]['data-count'] = isset( $counter[ $v['id'] ] ) ? $counter[ $v['id'] ] : 0; |
| 30 |
if ( isset( $v['children'] ) ) { |
| 31 |
$folders[ $k ]['children'] = $this->addFolderCounter( $v['children'], $counter ); |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
return $folders; |
| 36 |
} |
| 37 |
|
| 38 |
public function publicRestApiGetFolders( $request ) { |
| 39 |
$lang = sanitize_key( $request->get_param( 'language' ) ); |
| 40 |
$lang = ! empty( $lang ) ? $lang : null; |
| 41 |
|
| 42 |
$counter = FolderModel::countAttachments( $lang )['display']; |
| 43 |
$result['folders'] = $this->addFolderCounter( Tree::getFolders(), $counter ); |
| 44 |
|
| 45 |
wp_send_json_success( $result ); |
| 46 |
} |
| 47 |
|
| 48 |
public function publicRestApiGetFolderDetail( $request ) { |
| 49 |
$folder_id = $request->get_param( 'folder_id' ); |
| 50 |
wp_send_json_success( |
| 51 |
array( |
| 52 |
'folder' => FolderModel::findById( $folder_id, 'id, name, parent' ), |
| 53 |
) |
| 54 |
); |
| 55 |
} |
| 56 |
public function publicRestApiGetAttachmentIds( $request ) { |
| 57 |
$folder_id = $request->get_param( 'folder_id' ); |
| 58 |
|
| 59 |
if ( $folder_id != '' ) { |
| 60 |
wp_send_json_success( |
| 61 |
array( |
| 62 |
'attachment_ids' => Helpers::getAttachmentIdsByFolderId( $folder_id ), |
| 63 |
) |
| 64 |
); |
| 65 |
} |
| 66 |
wp_send_json_error( |
| 67 |
array( |
| 68 |
'mess' => __( 'folder_id is missing.', 'filebird' ), |
| 69 |
) |
| 70 |
); |
| 71 |
} |
| 72 |
public function publicRestApiGetAttachmentCount( $request ) { |
| 73 |
$folder_id = $request->get_param( 'folder_id' ); |
| 74 |
$icl_lang = $request->get_param( 'icl_lang' ); |
| 75 |
if ( $folder_id !== '' ) { |
| 76 |
$count = 0; |
| 77 |
if ( $folder_id > 0 ) { |
| 78 |
$count = Helpers::getAttachmentCountByFolderId( $folder_id ); |
| 79 |
} elseif ( $folder_id == -1 ) { |
| 80 |
$count = is_null( $icl_lang ) ? Tree::getCount( -1 ) : Tree::getCount( -1, $icl_lang ); |
| 81 |
} else { |
| 82 |
//Uncategorized |
| 83 |
$total = is_null( $icl_lang ) ? Tree::getCount( -1 ) : Tree::getCount( -1, $icl_lang ); |
| 84 |
$folders = is_null( $icl_lang ) ? Tree::getAllFoldersAndCount() : Tree::getAllFoldersAndCount( $icl_lang ); |
| 85 |
$count_of_folders = 0; |
| 86 |
foreach ( $folders as $k => $v ) { |
| 87 |
$count_of_folders += $v; |
| 88 |
} |
| 89 |
$count = $total - $count_of_folders; |
| 90 |
} |
| 91 |
wp_send_json_success( |
| 92 |
array( |
| 93 |
'count' => $count, |
| 94 |
) |
| 95 |
); |
| 96 |
} |
| 97 |
wp_send_json_error( |
| 98 |
array( |
| 99 |
'mess' => __( 'folder_id is missing.', 'filebird' ), |
| 100 |
) |
| 101 |
); |
| 102 |
} |
| 103 |
public function publicRestApiNewFolder( $request ) { |
| 104 |
$parent_id = (int) ( $request->get_param( 'parent_id' ) ); |
| 105 |
$name = sanitize_text_field( $request->get_param( 'name' ) ); |
| 106 |
|
| 107 |
if ( $name != '' ) { |
| 108 |
$folder = FolderModel::newOrGet( $name, $parent_id ); |
| 109 |
wp_send_json_success( array( 'id' => $folder['id'] ) ); |
| 110 |
} |
| 111 |
wp_send_json_error( |
| 112 |
array( |
| 113 |
'mess' => __( 'Required fields are missing.', 'filebird' ), |
| 114 |
) |
| 115 |
); |
| 116 |
} |
| 117 |
public function publicRestApiSetAttachment( $request ) { |
| 118 |
$ids = $request->get_param( 'ids' ); |
| 119 |
$folder = $request->get_param( 'folder' ); |
| 120 |
|
| 121 |
$ids = ! empty( $ids ) ? Helpers::sanitize_array( $ids ) : ''; |
| 122 |
$folder = sanitize_text_field( $folder ); |
| 123 |
|
| 124 |
if ( \is_numeric( $ids ) ) { |
| 125 |
$ids = array( $ids ); |
| 126 |
} |
| 127 |
|
| 128 |
if ( $ids != '' && is_numeric( $folder ) ) { |
| 129 |
FolderModel::setFoldersForPosts( $ids, $folder ); |
| 130 |
wp_send_json_success(); |
| 131 |
} |
| 132 |
wp_send_json_error( |
| 133 |
array( |
| 134 |
'mess' => __( 'Validation failed', 'filebird' ), |
| 135 |
) |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
private function generateRandomString( $length = 10 ) { |
| 140 |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 141 |
$charactersLength = strlen( $characters ); |
| 142 |
$randomString = ''; |
| 143 |
for ( $i = 0; $i < $length; $i++ ) { |
| 144 |
$randomString .= $characters[ wp_rand( 0, $charactersLength - 1 ) ]; |
| 145 |
} |
| 146 |
return $randomString; |
| 147 |
} |
| 148 |
} |