| 1 |
<?php |
| 2 |
namespace FileBird\Controller; |
| 3 |
|
| 4 |
use FileBird\Controller\Convert as ConvertController; |
| 5 |
|
| 6 |
use FileBird\Model\Folder as FolderModel; |
| 7 |
use FileBird\Classes\Helpers as Helpers; |
| 8 |
use FileBird\Classes\Tree; |
| 9 |
|
| 10 |
use FileBird\I18n as I18n; |
| 11 |
|
| 12 |
defined('ABSPATH') || exit; |
| 13 |
|
| 14 |
/** |
| 15 |
* Folder Controller |
| 16 |
*/ |
| 17 |
|
| 18 |
class Folder extends Controller { |
| 19 |
|
| 20 |
protected static $instance = null; |
| 21 |
|
| 22 |
public function __construct() { |
| 23 |
add_action('admin_enqueue_scripts', array($this, 'enqueueAdminScripts')); |
| 24 |
|
| 25 |
add_action('rest_api_init', array($this, 'registerRestFields')); |
| 26 |
|
| 27 |
add_action('add_attachment', array($this, 'addAttachment')); |
| 28 |
// add_action('edit_attachment', array($this, 'filebird_set_attachment_category')); |
| 29 |
add_action('delete_attachment', array($this, 'deleteAttachment')); |
| 30 |
|
| 31 |
add_filter('ajax_query_attachments_args', array($this, 'ajaxQueryAttachmentsArgs'), 20); |
| 32 |
add_filter('restrict_manage_posts', array($this, 'restrictManagePosts')); |
| 33 |
//add_action('pre_get_posts', array($this, 'preGetPosts')); |
| 34 |
add_filter( 'posts_clauses', array($this, 'postsClauses'), 10, 2 ); |
| 35 |
add_action( 'pre-upload-ui', array($this, 'actionPluploadUi') ); |
| 36 |
add_action( 'wp_ajax_fbv_first_folder_notice', array($this, 'ajax_first_folder_notice')); |
| 37 |
add_action( 'admin_notices', array($this, 'adminNotices') ); |
| 38 |
} |
| 39 |
public function adminNotices() |
| 40 |
{ |
| 41 |
global $pagenow; |
| 42 |
//welcome to new filebird message |
| 43 |
$notShownInPages = array('upload.php'); |
| 44 |
$optionFirstFolder = get_option('fbv_first_folder_notice'); |
| 45 |
if ((int)FolderModel::countFolder() == 0 |
| 46 |
&& !in_array($pagenow, $notShownInPages) |
| 47 |
&& ($optionFirstFolder === false || time() >= (int)$optionFirstFolder)) { |
| 48 |
?> |
| 49 |
<div class="notice notice-info is-dismissible" id="filebird-empty-folder-notice"> |
| 50 |
<p> |
| 51 |
<?php _e('Create your first folder for media library now.', 'filebird')?> |
| 52 |
<a href="<?php echo esc_url(admin_url('/upload.php')) ?>"> |
| 53 |
<strong><?php _e('Get Started', 'filebird')?></strong> |
| 54 |
</a> |
| 55 |
</p> |
| 56 |
</div> |
| 57 |
<?php |
| 58 |
} |
| 59 |
//import from old folders message |
| 60 |
$is_converted = get_option('fbv_old_data_updated_to_v4', '0'); |
| 61 |
if($is_converted !== '1') { |
| 62 |
$old_folder_count = count(ConvertController::getOldFolers()); |
| 63 |
$style = ''; |
| 64 |
if ($pagenow === 'upload.php') $style = 'display: none'; |
| 65 |
if ($pagenow === 'options-general.php') { |
| 66 |
if ( isset($_GET['page']) && isset($_GET['tab']) && |
| 67 |
sanitize_text_field($_GET['page']) === 'filebird-settings' && |
| 68 |
sanitize_text_field($_GET['tab']) === 'update-db' |
| 69 |
) $style = 'display: none'; |
| 70 |
} |
| 71 |
if($old_folder_count > 0 && !isset($_GET['autorun'])) { |
| 72 |
?> |
| 73 |
<div style="<?php echo esc_attr($style) ?>" class="notice notice-warning is-dismissible njt-fb-update-db-noti" id="njt-fb-update-db-noti"> |
| 74 |
<div class="njt-fb-update-db-noti-item"> |
| 75 |
<h3><?php _e('FileBird 4 Update Required', 'filebird'); ?></h3> |
| 76 |
</div> |
| 77 |
<div class="njt-fb-update-db-noti-item"> |
| 78 |
<p> |
| 79 |
<?php _e('You\'re using the new FileBird 4. Please update database to view your folders correctly.', 'filebird'); ?> |
| 80 |
</p> |
| 81 |
</div> |
| 82 |
<div class="njt-fb-update-db-noti-item"> |
| 83 |
<p> |
| 84 |
<a class="button button-primary" href="<?php echo esc_url(add_query_arg(array('page' => 'filebird-settings', 'tab' => 'update-db', 'autorun' => 'true'), admin_url('/options-general.php'))); ?>"> |
| 85 |
<strong><?php _e('Update now', 'filebird')?></strong> |
| 86 |
</a> |
| 87 |
</p> |
| 88 |
</div> |
| 89 |
</div> |
| 90 |
<?php |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
public function ajax_first_folder_notice(){ |
| 98 |
check_ajax_referer('fbv_nonce', 'nonce', true); |
| 99 |
update_option('fbv_first_folder_notice', time() + 30*60*60*24); //After 3 months show |
| 100 |
wp_send_json_success(); |
| 101 |
} |
| 102 |
|
| 103 |
public function registerRestFields() { |
| 104 |
register_rest_route(NJFB_REST_URL, |
| 105 |
'get-folders', |
| 106 |
array( |
| 107 |
'methods' => 'GET', |
| 108 |
'callback' => array($this, 'ajaxGetFolder'), |
| 109 |
'permission_callback' => array($this, 'resPermissionsCheck'), |
| 110 |
) |
| 111 |
); |
| 112 |
register_rest_route(NJFB_REST_URL, |
| 113 |
'gutenberg-get-folders', |
| 114 |
array( |
| 115 |
'methods' => 'GET', |
| 116 |
'callback' => array($this, 'ajaxGutenbergGetFolder'), |
| 117 |
'permission_callback' => array($this, 'resPermissionsCheck'), |
| 118 |
) |
| 119 |
); |
| 120 |
|
| 121 |
register_rest_route(NJFB_REST_URL, |
| 122 |
'new-folder', |
| 123 |
array( |
| 124 |
'methods' => 'POST', |
| 125 |
'callback' => array($this, 'ajaxNewFolder'), |
| 126 |
'permission_callback' => array($this, 'resPermissionsCheck'), |
| 127 |
) |
| 128 |
); |
| 129 |
register_rest_route(NJFB_REST_URL, |
| 130 |
'update-folder', |
| 131 |
array( |
| 132 |
'methods' => 'POST', |
| 133 |
'callback' => array($this, 'ajaxUpdateFolder'), |
| 134 |
'permission_callback' => array($this, 'resPermissionsCheck'), |
| 135 |
) |
| 136 |
); |
| 137 |
register_rest_route(NJFB_REST_URL, |
| 138 |
'update-folder-ord', |
| 139 |
array( |
| 140 |
'methods' => 'POST', |
| 141 |
'callback' => array($this, 'ajaxUpdateFolderOrd'), |
| 142 |
'permission_callback' => array($this, 'resPermissionsCheck'), |
| 143 |
) |
| 144 |
); |
| 145 |
register_rest_route(NJFB_REST_URL, |
| 146 |
'delete-folder', |
| 147 |
array( |
| 148 |
'methods' => 'POST', |
| 149 |
'callback' => array($this, 'ajaxDeleteFolder'), |
| 150 |
'permission_callback' => array($this, 'resPermissionsCheck'), |
| 151 |
) |
| 152 |
); |
| 153 |
register_rest_route(NJFB_REST_URL, |
| 154 |
'set-folder-attachments', |
| 155 |
array( |
| 156 |
'methods' => 'POST', |
| 157 |
'callback' => array($this, 'ajaxSetFolder'), |
| 158 |
'permission_callback' => array($this, 'resPermissionsCheck'), |
| 159 |
) |
| 160 |
); |
| 161 |
register_rest_route(NJFB_REST_URL, |
| 162 |
'update-tree', |
| 163 |
array( |
| 164 |
'methods' => 'POST', |
| 165 |
'callback' => array($this, 'ajaxUpdateTree'), |
| 166 |
'permission_callback' => array($this, 'resPermissionsCheck'), |
| 167 |
) |
| 168 |
); |
| 169 |
register_rest_route(NJFB_REST_URL, |
| 170 |
'get-relations', |
| 171 |
array( |
| 172 |
'methods' => 'POST', |
| 173 |
'callback' => array($this, 'ajaxGetRelations'), |
| 174 |
'permission_callback' => array($this, 'resPermissionsCheck'), |
| 175 |
) |
| 176 |
); |
| 177 |
register_rest_route(NJFB_REST_URL, |
| 178 |
'set-default-folder', |
| 179 |
array( |
| 180 |
'methods' => 'POST', |
| 181 |
'callback' => array($this, 'ajaxSetDefaultFolder'), |
| 182 |
'permission_callback' => array($this, 'resPermissionsCheck'), |
| 183 |
) |
| 184 |
); |
| 185 |
} |
| 186 |
public function resPermissionsCheck() { |
| 187 |
return current_user_can('upload_files'); |
| 188 |
} |
| 189 |
public static function getInstance() { |
| 190 |
if (null == self::$instance) { |
| 191 |
self::$instance = new self; |
| 192 |
} |
| 193 |
return self::$instance; |
| 194 |
} |
| 195 |
|
| 196 |
public function enqueueAdminScripts($screenId) { |
| 197 |
if (function_exists('get_current_screen')) { |
| 198 |
if ($screenId == "upload.php") { |
| 199 |
wp_register_script('jquery-resizable', NJFB_PLUGIN_URL . 'assets/js/jquery-resizable.min.js' ); |
| 200 |
wp_enqueue_script('jquery-resizable'); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if ( $screenId !== 'pagebuilders' ) { |
| 205 |
wp_enqueue_script('fbv-import', NJFB_PLUGIN_URL . 'assets/js/import.js', array('jquery'), NJFB_VERSION, false); |
| 206 |
wp_enqueue_script('fbv-active', NJFB_PLUGIN_URL . 'assets/js/active.js', array('jquery'), NJFB_VERSION, false); |
| 207 |
} |
| 208 |
|
| 209 |
if ( $screenId === 'settings_page_filebird-settings' ) { |
| 210 |
wp_enqueue_script('toastr', NJFB_PLUGIN_URL . 'assets/js/toastr/toastr.min.js', array(), '2.1.3', false); |
| 211 |
wp_enqueue_style('toastr', NJFB_PLUGIN_URL . 'assets/js/toastr/toastr.min.css', array(), '2.1.3'); |
| 212 |
} |
| 213 |
|
| 214 |
wp_enqueue_script('jquery-ui-draggable'); |
| 215 |
wp_enqueue_script('jquery-ui-droppable'); |
| 216 |
|
| 217 |
wp_enqueue_script('fbv-folder', NJFB_PLUGIN_URL . 'assets/dist/app.js', array(), NJFB_VERSION, false); |
| 218 |
wp_enqueue_script('fbv-lib', NJFB_PLUGIN_URL . 'assets/js/jstree/jstree.min.js', array(), NJFB_VERSION, false); |
| 219 |
|
| 220 |
wp_enqueue_style('fbv-folder', NJFB_PLUGIN_URL . 'assets/dist/app.css', array(), NJFB_VERSION); |
| 221 |
wp_style_add_data('fbv-folder', 'rtl', 'replace'); |
| 222 |
|
| 223 |
wp_localize_script('fbv-folder', 'fbv_data', apply_filters('fbv_data', array( |
| 224 |
'nonce' => wp_create_nonce('fbv_nonce'), |
| 225 |
'rest_nonce' => wp_create_nonce('wp_rest'), |
| 226 |
'nonce_error' => __('Your request can\'t be processed.', 'filebird'), |
| 227 |
'current_folder' => ((isset($_GET['fbv'])) ? (int)sanitize_text_field($_GET['fbv']) : -1), //-1: all files. 0: uncategorized |
| 228 |
'default_folder' => Helpers::getDefaultSelectedFolder(), |
| 229 |
'folders' => FolderModel::allFolders('id as term_id, name as term_name', array('term_id', 'term_name')), |
| 230 |
'relations' => FolderModel::getRelations(), |
| 231 |
// 'is_upload' => $current_screen != null && $current_screen->id === 'upload' ? 1 : 0, |
| 232 |
'i18n' => i18n::getTranslation(), |
| 233 |
'media_mode' => get_user_option('media_library_mode', get_current_user_id()), |
| 234 |
'json_url' => apply_filters('filebird_json_url', rtrim(rest_url(NJFB_REST_URL),"/")), |
| 235 |
'media_url' => admin_url('upload.php'), |
| 236 |
'auto_import_url' => esc_url(add_query_arg(array('page' => 'filebird-settings', 'tab' => 'update-db', 'autorun' => 'true'), admin_url('/options-general.php'))) |
| 237 |
))); |
| 238 |
} |
| 239 |
|
| 240 |
public function restrictManagePosts() { |
| 241 |
$screen = get_current_screen(); |
| 242 |
if ($screen->id == "upload") { |
| 243 |
$fbv = ((isset($_GET['fbv'])) ? (int)sanitize_text_field($_GET['fbv']) : -1); |
| 244 |
$folders = FolderModel::allFolders(); |
| 245 |
|
| 246 |
$all = new \stdClass(); |
| 247 |
$all->id = -1; |
| 248 |
$all->name = __('All Folders', 'filebird'); |
| 249 |
|
| 250 |
$uncategorized = new \stdClass(); |
| 251 |
$uncategorized->id = 0; |
| 252 |
$uncategorized->name = __('Uncategorized', 'filebird'); |
| 253 |
|
| 254 |
array_unshift($folders, $all, $uncategorized); |
| 255 |
echo '<select name="fbv" id="filter-by-fbv" class="fbv-filter attachment-filters fbv">'; |
| 256 |
foreach($folders as $k => $folder) { |
| 257 |
echo sprintf('<option value="%1$d" %3$s>%2$s</option>', $folder->id, $folder->name, selected($folder->id, $fbv, false)); |
| 258 |
} |
| 259 |
echo '</select>'; |
| 260 |
} |
| 261 |
} |
| 262 |
// public function preGetPosts($query) { |
| 263 |
// if ( is_admin() && $query->is_main_query() ) { |
| 264 |
// if (isset($_GET['fbv'])) { |
| 265 |
// $query->set('fbv', (int)$_GET['fbv']); |
| 266 |
// } |
| 267 |
// } |
| 268 |
// } |
| 269 |
public function postsClauses($clauses, $query) { |
| 270 |
if ($query->get("post_type") !== "attachment") { |
| 271 |
return $clauses; |
| 272 |
} |
| 273 |
|
| 274 |
if( isset($_GET['fbv']) || !empty($query->get('fbv')) ) { |
| 275 |
$fbv = isset($_GET['fbv']) ? (int)sanitize_text_field($_GET['fbv']) : (int)$query->get('fbv'); |
| 276 |
$in_not_in = FolderModel::getInAndNotInIds($fbv, true); |
| 277 |
if(count($in_not_in['post__not_in']) > 0) { |
| 278 |
$clauses['where'] .= "AND ID NOT IN (".implode(',', $in_not_in['post__not_in']).")"; |
| 279 |
} elseif(count($in_not_in['post__in']) > 0) { |
| 280 |
$clauses['where'] .= "AND ID IN (".implode(',', $in_not_in['post__in']).")"; |
| 281 |
} |
| 282 |
} |
| 283 |
return $clauses; |
| 284 |
} |
| 285 |
public function addAttachment($post_id) { |
| 286 |
$fbv = ((isset($_REQUEST['fbv'])) ? sanitize_text_field($_REQUEST['fbv']) : ''); |
| 287 |
if($fbv != '') { |
| 288 |
if(is_numeric($fbv)) { |
| 289 |
$parent = $fbv; |
| 290 |
} else { |
| 291 |
$fbv = explode('/', ltrim(rtrim($fbv, '/'), '/')); |
| 292 |
$parent = (int)$fbv[0]; |
| 293 |
if($parent < 0) $parent = 0;//important |
| 294 |
unset($fbv[0]); |
| 295 |
foreach($fbv as $k => $v) { |
| 296 |
$parent = FolderModel::newOrGet($v, $parent); |
| 297 |
} |
| 298 |
} |
| 299 |
FolderModel::setFoldersForPosts($post_id, $parent); |
| 300 |
} |
| 301 |
} |
| 302 |
public function deleteAttachment($post_id) { |
| 303 |
FolderModel::deleteFoldersOfPost($post_id); |
| 304 |
} |
| 305 |
|
| 306 |
public function ajaxQueryAttachmentsArgs($query) { |
| 307 |
if(isset($_REQUEST['query']['fbv'])) { |
| 308 |
$fbv = $_REQUEST['query']['fbv']; |
| 309 |
if(is_array($fbv)) { |
| 310 |
$fbv = array_map('intval', $fbv); |
| 311 |
} else { |
| 312 |
$fbv = intval($fbv); |
| 313 |
} |
| 314 |
|
| 315 |
$in_not_in = FolderModel::getInAndNotInIds($fbv); |
| 316 |
if(!isset($query['post__not_in'])) { |
| 317 |
$query['post__not_in'] = array(); |
| 318 |
} |
| 319 |
if(!isset($query['post__in'])) { |
| 320 |
$query['post__in'] = array(); |
| 321 |
} |
| 322 |
$query['post__not_in'] += $in_not_in['post__not_in']; |
| 323 |
$query['post__in'] += $in_not_in['post__in']; |
| 324 |
} |
| 325 |
return $query; |
| 326 |
} |
| 327 |
public function ajaxGetFolder() { |
| 328 |
|
| 329 |
// if(get_option('fbv_old_data_updated_to_v4', '0') !== '1') { |
| 330 |
// Convert::insertToNewTable(); |
| 331 |
// update_option('fbv_old_data_updated_to_v4', '1'); |
| 332 |
// } |
| 333 |
|
| 334 |
$order_by = null; |
| 335 |
$sort_option = 'reset'; |
| 336 |
|
| 337 |
if(isset($_GET['sort']) && \in_array(sanitize_text_field($_GET['sort']), array('name_asc', 'name_desc', 'reset'))) { |
| 338 |
if(sanitize_text_field($_GET['sort']) == 'name_asc') { |
| 339 |
$order_by = 'name asc'; |
| 340 |
$sort_option = sanitize_text_field($_GET['sort']); |
| 341 |
} elseif(sanitize_text_field($_GET['sort']) == 'name_desc') { |
| 342 |
$order_by = 'name desc'; |
| 343 |
$sort_option = sanitize_text_field($_GET['sort']); |
| 344 |
} |
| 345 |
update_option('njt_fb_sort_folder', $sort_option); |
| 346 |
} else { |
| 347 |
$njt_fb_sort_folder = get_option('njt_fb_sort_folder', 'reset'); |
| 348 |
if($njt_fb_sort_folder == 'reset') { |
| 349 |
$order_by = null; |
| 350 |
} elseif($njt_fb_sort_folder == 'name_asc') { |
| 351 |
$order_by = 'name asc'; |
| 352 |
} elseif($njt_fb_sort_folder == 'name_desc') { |
| 353 |
$order_by = 'name desc'; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
$tree = Tree::getFolders($order_by, false); |
| 358 |
|
| 359 |
wp_send_json_success(array( |
| 360 |
'tree' => $tree, |
| 361 |
'folder_count' => array( |
| 362 |
'-1' => Tree::getCount(-1), |
| 363 |
'0' => Tree::getCount(0) |
| 364 |
) |
| 365 |
)); |
| 366 |
} |
| 367 |
public function ajaxGutenbergGetFolder() { |
| 368 |
$_folders = Tree::getFolders(null, true, 0, true); |
| 369 |
$folders = array( |
| 370 |
array( |
| 371 |
'value' => 0, |
| 372 |
'label' => __('Please choose folder', 'filebird'), |
| 373 |
'disabled' => true |
| 374 |
) |
| 375 |
); |
| 376 |
foreach($_folders as $k => $v) { |
| 377 |
$folders[] = array( |
| 378 |
'value' => $v['id'], |
| 379 |
'label' => $v['text'] |
| 380 |
); |
| 381 |
} |
| 382 |
|
| 383 |
wp_send_json_success($folders); |
| 384 |
} |
| 385 |
public function ajaxNewFolder() { |
| 386 |
//check_ajax_referer('fbv_nonce', 'nonce', true); |
| 387 |
$name = ((isset($_POST['name'])) ? sanitize_text_field(wp_unslash($_POST['name'])) : ''); |
| 388 |
$parent = ((isset($_POST['parent'])) ? sanitize_text_field($_POST['parent']) : ''); |
| 389 |
$id = null; |
| 390 |
if($name != '' && $parent != '') { |
| 391 |
// if(!is_array($name)) { |
| 392 |
// $name = array($name); |
| 393 |
// } |
| 394 |
// if($parent < 0) $parent = 0; |
| 395 |
|
| 396 |
// $paths = array(); |
| 397 |
// foreach($name as $k => $v) { |
| 398 |
// $parent2 = $parent; |
| 399 |
// $sub_folders = explode('/', ltrim(rtrim($v, '/'), '/')); |
| 400 |
// foreach($sub_folders as $k2 => $v2) { |
| 401 |
// $parent2 = FolderModel::newOrGet($v2, $parent2); |
| 402 |
// } |
| 403 |
// $paths[$v] = $parent2; |
| 404 |
// $id = $parent2; //ID Added |
| 405 |
// } |
| 406 |
// wp_send_json_success(array('id' => $id)); |
| 407 |
$insert = FolderModel::newOrGet($name, $parent, false); |
| 408 |
if($insert !== false) { |
| 409 |
wp_send_json_success(array('id' => $insert)); |
| 410 |
} else { |
| 411 |
wp_send_json_error(array('mess' => __('A folder with this name already exists. Please choose another one.', 'filebird'))); |
| 412 |
} |
| 413 |
} else { |
| 414 |
wp_send_json_error(array( |
| 415 |
'mess' => __('Validation failed', 'filebird') |
| 416 |
)); |
| 417 |
} |
| 418 |
} |
| 419 |
public function ajaxUpdateFolder() { |
| 420 |
//check_ajax_referer('fbv_nonce', 'nonce', true); |
| 421 |
$id = ((isset($_POST['id'])) ? sanitize_text_field($_POST['id']) : ''); |
| 422 |
$parent = ((isset($_POST['parent'])) ? intval(sanitize_text_field($_POST['parent'])) : ''); |
| 423 |
$name = ((isset($_POST['name'])) ? sanitize_text_field(wp_unslash($_POST['name'])) : ''); |
| 424 |
if(is_numeric($id) && is_numeric($parent) && $name != '') { |
| 425 |
$update = FolderModel::updateFolderName($name, $parent, $id); |
| 426 |
if($update === true) { |
| 427 |
wp_send_json_success(); |
| 428 |
} else { |
| 429 |
wp_send_json_error(array('mess' => __('A folder with this name already exists. Please choose another one.', 'filebird'))); |
| 430 |
} |
| 431 |
|
| 432 |
} |
| 433 |
wp_send_json_error(); |
| 434 |
} |
| 435 |
public function ajaxUpdateFolderOrd() { |
| 436 |
$id = ((isset($_POST['id'])) ? sanitize_text_field($_POST['id']) : ''); |
| 437 |
$parent = ((isset($_POST['parent'])) ? sanitize_text_field(wp_unslash($_POST['parent'])) : ''); |
| 438 |
$ord = ((isset($_POST['ord'])) ? sanitize_text_field(wp_unslash($_POST['ord'])) : ''); |
| 439 |
if(is_numeric($id) && is_numeric($parent) && is_numeric($ord)) { |
| 440 |
FolderModel::updateOrdAndParent($id, $ord, $parent); |
| 441 |
wp_send_json_success(); |
| 442 |
} |
| 443 |
wp_send_json_error(); |
| 444 |
} |
| 445 |
public function ajaxDeleteFolder() { |
| 446 |
//check_ajax_referer('fbv_nonce', 'nonce', true); |
| 447 |
$ids = ((isset($_POST['ids'])) ? Helpers::sanitize_array($_POST['ids']) : ''); |
| 448 |
if($ids != '') { |
| 449 |
if(!is_array($ids)) $ids = array($ids); |
| 450 |
$ids = array_map('intval', $ids); |
| 451 |
|
| 452 |
foreach($ids as $k => $v) { |
| 453 |
if($v > 0) FolderModel::deleteFolderAndItsChildren($v); |
| 454 |
} |
| 455 |
wp_send_json_success(); |
| 456 |
} |
| 457 |
wp_send_json_error(array( |
| 458 |
'mess' => __('Can\'t delete folder, please try again later', 'filebird') |
| 459 |
)); |
| 460 |
} |
| 461 |
public function ajaxSetFolder() { |
| 462 |
//check_ajax_referer('fbv_nonce', 'nonce', true); |
| 463 |
$ids = ((isset($_POST['ids'])) ? Helpers::sanitize_array($_POST['ids']) : ''); |
| 464 |
$folder = ((isset($_POST['folder'])) ? sanitize_text_field($_POST['folder']) : ''); |
| 465 |
if($ids != '' && is_array($ids) && is_numeric($folder)) { |
| 466 |
FolderModel::setFoldersForPosts($ids, $folder); |
| 467 |
wp_send_json_success(); |
| 468 |
} |
| 469 |
wp_send_json_error(array( |
| 470 |
'mess' => __('Validation failed', 'filebird') |
| 471 |
)); |
| 472 |
} |
| 473 |
public function ajaxUpdateTree() { |
| 474 |
//check_ajax_referer('fbv_nonce', 'nonce', true); |
| 475 |
$tree = ((isset($_POST['tree'])) ? sanitize_text_field($_POST['tree']) : ''); |
| 476 |
if($tree != '') { |
| 477 |
FolderModel::rawInsert('(id, ord, parent) VALUES ' . $tree . ' ON DUPLICATE KEY UPDATE ord=VALUES(ord),parent=VALUES(parent)'); |
| 478 |
wp_send_json_success(array( |
| 479 |
'mess' => __('Folder tree has been updated.', 'filebird') |
| 480 |
)); |
| 481 |
} |
| 482 |
wp_send_json_error(array( |
| 483 |
'mess' => __('Validation failed', 'filebird') |
| 484 |
)); |
| 485 |
} |
| 486 |
public function ajaxGetRelations() { |
| 487 |
//check_ajax_referer('fbv_nonce', 'nonce', true); |
| 488 |
wp_send_json_success(array( |
| 489 |
'relations' => FolderModel::getRelations() |
| 490 |
)); |
| 491 |
} |
| 492 |
public function ajaxSetDefaultFolder() { |
| 493 |
$folder_id = ((isset($_POST['folder_id'])) ? intval($_POST['folder_id']) : -1); |
| 494 |
Helpers::setDefaultSelectedFolder($folder_id); |
| 495 |
wp_send_json_success(); |
| 496 |
} |
| 497 |
public function actionPluploadUi() { |
| 498 |
global $pagenow; |
| 499 |
$folders = FolderModel::allFolders(); |
| 500 |
$default = array( |
| 501 |
array( |
| 502 |
'title' => __('Uncategorized', 'filebird'), |
| 503 |
'value' => '0' |
| 504 |
) |
| 505 |
); |
| 506 |
$data = array( |
| 507 |
'tree' => $this->getFlatTree($folders, 0, $default) |
| 508 |
); |
| 509 |
$this->loadView('particle/folder_dropdown', $data); |
| 510 |
} |
| 511 |
private function _buildQuery($tree, $parent) { |
| 512 |
$results = array(); |
| 513 |
$ord = 0; |
| 514 |
foreach($tree as $k => $v) { |
| 515 |
// if($v['key'] < 1) continue; |
| 516 |
$results[] = sprintf('(%1$d, %2$d, %3$d)', $v['id'], $ord, $parent); |
| 517 |
if(isset($v['children']) && is_array($v['children']) && count($v['children']) > 0) { |
| 518 |
$children = $this->_buildQuery($v['children'], $v['id']); |
| 519 |
foreach ($children as $k2 => $v2) { |
| 520 |
$results[] = $v2; |
| 521 |
} |
| 522 |
} |
| 523 |
$ord++; |
| 524 |
} |
| 525 |
return $results; |
| 526 |
} |
| 527 |
|
| 528 |
private function getFlatTree($data, $parent = 0, $default = null, $level = 0) { |
| 529 |
$tree = is_null($default) ? array() : $default; |
| 530 |
foreach($data as $k => $v) { |
| 531 |
if($v->parent == $parent) { |
| 532 |
$node = array( |
| 533 |
'title' => str_repeat('-', $level) . $v->name, |
| 534 |
'value' => $v->id, |
| 535 |
); |
| 536 |
$tree[] = $node; |
| 537 |
$children = $this->getFlatTree($data, $v->id, null, $level + 1); |
| 538 |
foreach($children as $k2 => $child) { |
| 539 |
$tree[] = $child; |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
return $tree; |
| 544 |
} |
| 545 |
|
| 546 |
} |