| 1 |
<?php |
| 2 |
namespace FileBird\Controller; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
use FileBird\Model\Folder as FolderModel; |
| 7 |
|
| 8 |
class FolderUser extends Controller { |
| 9 |
protected static $instance = null; |
| 10 |
|
| 11 |
private $is_enabled; |
| 12 |
private $current_user_id; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
$this->is_enabled = $this->isEnabled(); |
| 16 |
$this->current_user_id = get_current_user_id(); |
| 17 |
|
| 18 |
if($this->is_enabled) { |
| 19 |
add_filter('fbv_data_before_inserting_folder', array($this, 'filterDataBeforeInsertingFolder')); |
| 20 |
add_filter('fbv_getting_folders_where', array($this, 'filterGettingFoldersWhere')); |
| 21 |
add_filter('fbv_can_get_in_not_in', array($this, 'filterCanGetInNotIn'), 10, 2); |
| 22 |
add_filter('fbv_in_not_in_uncategorized_where', array($this, 'filterUncategorizedWhere'), 10, 2); |
| 23 |
} |
| 24 |
add_action('fbv_before_setting_folder', array($this, 'actionBeforeSettingFolder'), 10, 2); |
| 25 |
} |
| 26 |
|
| 27 |
public function filterDataBeforeInsertingFolder($data) { |
| 28 |
$data['created_by'] = $this->current_user_id; |
| 29 |
return $data; |
| 30 |
} |
| 31 |
public function filterGettingFoldersWhere($where) { |
| 32 |
$where['created_by'] = $this->current_user_id; |
| 33 |
return $where; |
| 34 |
} |
| 35 |
public function filterCanGetInNotIn($can, $folder) { |
| 36 |
if ( false === ( $author = get_transient( 'fbv_transient_' . $folder ) ) ) { |
| 37 |
$author = FolderModel::getAuthor($folder); |
| 38 |
set_transient( 'fbv_transient_' . $folder, $author, (3600 * 365) );//1 year |
| 39 |
} |
| 40 |
$author = FolderModel::getAuthor($folder); |
| 41 |
return (int)$author === $this->current_user_id; |
| 42 |
} |
| 43 |
public function filterUncategorizedWhere($where, $folder_table) { |
| 44 |
return sprintf('`folder_id` IN (SELECT `id` FROM %1$s WHERE `created_by` = %2$d)', $folder_table, $this->current_user_id); |
| 45 |
} |
| 46 |
|
| 47 |
public function actionBeforeSettingFolder($post_id, $folder_id) { |
| 48 |
global $wpdb; |
| 49 |
if($this->is_enabled) { |
| 50 |
$query = sprintf('DELETE FROM %1$sfbv_attachment_folder WHERE `attachment_id` = %2$d AND `folder_id` IN (SELECT `id` FROM %1$sfbv WHERE `created_by` = %3$d)', $wpdb->prefix, $post_id, $this->current_user_id); |
| 51 |
$wpdb->query($query); |
| 52 |
} else { |
| 53 |
$wpdb->query(sprintf('DELETE FROM %1$sfbv_attachment_folder WHERE `attachment_id` = %2$d AND `folder_id` IN (SELECT `id` FROM %1$sfbv WHERE `created_by` = 0)', $wpdb->prefix, $post_id)); |
| 54 |
} |
| 55 |
} |
| 56 |
private function isEnabled() { |
| 57 |
return get_option('njt_fbv_folder_per_user', '0') === '1'; |
| 58 |
} |
| 59 |
public static function getInstance() { |
| 60 |
if (null == self::$instance) { |
| 61 |
self::$instance = new self; |
| 62 |
} |
| 63 |
return self::$instance; |
| 64 |
} |
| 65 |
} |
| 66 |
|