| 1 |
<?php |
| 2 |
namespace FileBird\Classes; |
| 3 |
|
| 4 |
use FileBird\Admin\Settings; |
| 5 |
use FileBird\Utils\Singleton; |
| 6 |
use FileBird\Model\Folder as FolderModel; |
| 7 |
use FileBird\Utils\Vite; |
| 8 |
use FileBird\Classes\Helpers as Helpers; |
| 9 |
use FileBird\Controller\Import\ImportController; |
| 10 |
use FileBird\I18n as I18n; |
| 11 |
use FileBird\Model\SettingModel; |
| 12 |
use FileBird\Controller\Convert as ConvertController; |
| 13 |
use FileBird\Classes\FolderStateManager; |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
class Core { |
| 18 |
private $settingModel = null; |
| 19 |
|
| 20 |
use Singleton; |
| 21 |
|
| 22 |
public function __construct() { |
| 23 |
$this->settingModel = SettingModel::getInstance(); |
| 24 |
$this->loadModules(); |
| 25 |
$this->checkUpdateDatabase(); |
| 26 |
} |
| 27 |
|
| 28 |
private function doHooks() { |
| 29 |
add_filter( 'media_library_infinite_scrolling', '__return_true' ); |
| 30 |
add_filter( 'ajax_query_attachments_args', array( $this, 'ajaxQueryAttachmentsArgs' ), 20 ); |
| 31 |
add_filter( 'mla_media_modal_query_final_terms', array( $this, 'ajaxQueryAttachmentsArgs' ), 20 ); |
| 32 |
add_filter( 'restrict_manage_posts', array( $this, 'restrictManagePosts' ) ); |
| 33 |
add_filter( 'posts_clauses', array( $this, 'postsClauses' ), 10, 2 ); |
| 34 |
add_filter( 'attachment_fields_to_save', array( $this, 'attachment_fields_to_save' ), 10, 2 ); |
| 35 |
|
| 36 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueueAdminScripts' ), PHP_INT_MAX ); |
| 37 |
add_action( 'add_attachment', array( $this, 'addAttachment' ) ); |
| 38 |
add_action( 'delete_attachment', array( $this, 'deleteAttachment' ) ); |
| 39 |
add_action( 'pre-upload-ui', array( $this, 'actionPluploadUi' ) ); |
| 40 |
add_action( 'wp_ajax_fbv_first_folder_notice', array( $this, 'ajax_first_folder_notice' ) ); |
| 41 |
add_action( 'admin_notices', array( $this, 'adminNotices' ) ); |
| 42 |
add_action( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 ); |
| 43 |
add_filter( 'wp_edited_image_metadata', array( $this, 'edited_image_metadata' ), 10, 3 ); |
| 44 |
|
| 45 |
add_filter( 'users_have_additional_content', array( $this, 'users_have_additional_content' ), 10, 2 ); |
| 46 |
add_action( 'deleted_user', array( $this, 'deleted_user' ), 10, 3 ); |
| 47 |
} |
| 48 |
|
| 49 |
public function checkUpdateDatabase() { |
| 50 |
if ( is_admin() ) { |
| 51 |
$is_converted = get_option( 'fbv_old_data_updated_to_v4', '0' ); |
| 52 |
if ( $is_converted !== '1' ) { |
| 53 |
if ( ConvertController::countOldFolders() > 0 && ! isset( $_GET['autorun'] ) ) { |
| 54 |
add_filter( 'fbv_update_database_notice', '__return_true' ); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
private function loadModules() { |
| 61 |
new Attachment\AttachmentSize(); |
| 62 |
new Modules\ModuleExclude(); |
| 63 |
new Modules\ModuleUser(); |
| 64 |
new Modules\ModuleCompatibility(); |
| 65 |
new Modules\ModuleSvg(); |
| 66 |
} |
| 67 |
|
| 68 |
public function adminNotices() { |
| 69 |
global $pagenow; |
| 70 |
|
| 71 |
$optionFirstFolder = get_option( 'fbv_first_folder_notice' ); |
| 72 |
if ( FolderModel::countFolder() === 0 && $pagenow !== 'upload.php' && |
| 73 |
( $optionFirstFolder === false || time() >= intval( $optionFirstFolder ) ) ) { |
| 74 |
include NJFB_PLUGIN_PATH . '/views/notices/html-notice-first-folder.php'; |
| 75 |
} |
| 76 |
|
| 77 |
if ( $pagenow !== 'upload.php' && apply_filters( 'fbv_update_database_notice', false ) ) { |
| 78 |
include NJFB_PLUGIN_PATH . '/views/notices/html-notice-update-database.php'; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public function ajax_first_folder_notice() { |
| 83 |
check_ajax_referer( 'fbv_nonce', 'nonce', true ); |
| 84 |
update_option( 'fbv_first_folder_notice', time() + 30 * 60 * 60 * 24 ); //After 3 months show |
| 85 |
wp_send_json_success(); |
| 86 |
} |
| 87 |
|
| 88 |
public function enqueueAdminScripts( $screenId ) { |
| 89 |
$script_handle = Vite::enqueue_vite( 'main.tsx' ); |
| 90 |
|
| 91 |
wp_enqueue_script( 'jquery-ui-draggable' ); |
| 92 |
wp_enqueue_script( 'jquery-ui-droppable' ); |
| 93 |
|
| 94 |
if ( wp_is_mobile() ) { |
| 95 |
wp_enqueue_script( 'jquery-touch-punch-fixed', NJFB_PLUGIN_URL . 'assets/js/jquery.ui.touch-punch.js', array( 'jquery-ui-widget', 'jquery-ui-mouse' ), NJFB_VERSION, false ); |
| 96 |
} |
| 97 |
|
| 98 |
wp_localize_script( |
| 99 |
$script_handle, |
| 100 |
'fbv_data', |
| 101 |
apply_filters( |
| 102 |
'fbv_data', |
| 103 |
array( |
| 104 |
'nonce' => wp_create_nonce( 'fbv_nonce' ), |
| 105 |
'rest_nonce' => wp_create_nonce( 'wp_rest' ), |
| 106 |
'is_upload_screen' => 'upload.php' === $screenId ? '1' : '0', |
| 107 |
'i18n' => I18n::getTranslation(), |
| 108 |
'media_mode' => get_user_option( 'media_library_mode', get_current_user_id() ), |
| 109 |
'json_url' => apply_filters( 'filebird_json_url', get_rest_url( null, NJFB_REST_URL ) ), |
| 110 |
'media_url' => admin_url( 'upload.php' ), |
| 111 |
'asset_url' => NJFB_PLUGIN_URL . 'assets/', |
| 112 |
'data_import' => ImportController::get_notice_import( $screenId ), |
| 113 |
'data_import_url' => esc_url( |
| 114 |
add_query_arg( |
| 115 |
array( |
| 116 |
'page' => Settings::SETTING_PAGE_SLUG . '#/import-export', |
| 117 |
), |
| 118 |
admin_url( 'admin.php' ) |
| 119 |
) |
| 120 |
), |
| 121 |
'is_new_user' => get_option( 'fbv_is_new_user', false ), |
| 122 |
'update_database_notice' => apply_filters( 'fbv_update_database_notice', false ), |
| 123 |
'is_rtl' => is_rtl(), |
| 124 |
'tree_mode' => 'attachment', |
| 125 |
'folder_search_api' => apply_filters( 'fbv_folder_api_search', false ), |
| 126 |
) |
| 127 |
) |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
public function restrictManagePosts() { |
| 132 |
$screen = get_current_screen(); |
| 133 |
if ( $screen->id == 'upload' ) { |
| 134 |
$fbv = ( ( isset( $_GET['fbv'] ) ) ? (int) sanitize_text_field( $_GET['fbv'] ) : FolderModel::ALL_CATEGORIES ); |
| 135 |
$folders = FolderModel::allFolders(); |
| 136 |
|
| 137 |
$all = new \stdClass(); |
| 138 |
$all->id = -1; |
| 139 |
$all->name = __( 'All Folders', 'filebird' ); |
| 140 |
|
| 141 |
$uncategorized = new \stdClass(); |
| 142 |
$uncategorized->id = 0; |
| 143 |
$uncategorized->name = __( 'Uncategorized', 'filebird' ); |
| 144 |
|
| 145 |
array_unshift( $folders, $all, $uncategorized ); |
| 146 |
echo '<select name="fbv" id="filter-by-fbv" class="fbv-filter attachment-filters fbv">'; |
| 147 |
foreach ( $folders as $k => $folder ) { |
| 148 |
echo sprintf( '<option value="%1$d" %3$s>%2$s</option>', esc_html( $folder->id ), esc_html( $folder->name ), selected( intval( $folder->id ), $fbv, false ) ); |
| 149 |
} |
| 150 |
echo '</select>'; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public function postsClauses( $clauses, $query ) { |
| 155 |
global $wpdb; |
| 156 |
|
| 157 |
if ( $query->get( 'post_type' ) !== 'attachment' ) { |
| 158 |
return $clauses; |
| 159 |
} |
| 160 |
|
| 161 |
$fb_folder = ( new FolderStateManager( $query, $this->settingModel ) )->getFbFolder(); |
| 162 |
|
| 163 |
if ( ! \is_null( $fb_folder ) ) { |
| 164 |
if ( FolderModel::ALL_CATEGORIES === $fb_folder ) { |
| 165 |
return $clauses; |
| 166 |
} elseif ( FolderModel::UN_CATEGORIZED === $fb_folder ) { |
| 167 |
$clauses = FolderModel::getRelationsWithFolderUser( $clauses ); |
| 168 |
} else { |
| 169 |
$clauses['join'] .= $wpdb->prepare( " LEFT JOIN {$wpdb->prefix}fbv_attachment_folder AS fbva ON fbva.attachment_id = {$wpdb->posts}.ID AND fbva.folder_id = %d ", $fb_folder ); |
| 170 |
$clauses['where'] .= ' AND fbva.folder_id IS NOT NULL'; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return $clauses; |
| 175 |
} |
| 176 |
|
| 177 |
public function addAttachment( $post_id ) { |
| 178 |
$fbv = ( ( isset( $_REQUEST['fbv'] ) ) ? sanitize_text_field( $_REQUEST['fbv'] ) : '' ); |
| 179 |
if ( $fbv != '' ) { |
| 180 |
if ( is_numeric( $fbv ) ) { |
| 181 |
$parent = $fbv; |
| 182 |
} else { |
| 183 |
$fbv = explode( '/', ltrim( rtrim( $fbv, '/' ), '/' ) ); |
| 184 |
$parent = (int) $fbv[0]; |
| 185 |
if ( $parent < 0 ) { |
| 186 |
$parent = 0; //important |
| 187 |
} |
| 188 |
if ( apply_filters( 'fbv_auto_create_folders', true ) ) { |
| 189 |
unset( $fbv[0] ); |
| 190 |
foreach ( $fbv as $k => $v ) { |
| 191 |
$folder = FolderModel::newOrGet( $v, $parent ); |
| 192 |
$parent = $folder['id']; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
FolderModel::setFoldersForPosts( $post_id, $parent ); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
public function deleteAttachment( $post_id ) { |
| 201 |
FolderModel::deleteFoldersOfPost( $post_id ); |
| 202 |
} |
| 203 |
|
| 204 |
public function ajaxQueryAttachmentsArgs( $query ) { |
| 205 |
// phpcs::ignore WordPress.Security.NonceVerification.Recommended |
| 206 |
if ( isset( $_REQUEST['query']['fbv'] ) ) { |
| 207 |
$fbv = Helpers::sanitize_intval_array( $_REQUEST['query']['fbv'] ); |
| 208 |
$query['fbv'] = ( new FolderStateManager( $query, $this->settingModel ) )->getState( $fbv ); |
| 209 |
} |
| 210 |
return $query; |
| 211 |
} |
| 212 |
|
| 213 |
public function attachment_fields_to_edit( $form_fields, $post ) { |
| 214 |
$screen = null; |
| 215 |
if ( function_exists( 'get_current_screen' ) ) { |
| 216 |
$screen = get_current_screen(); |
| 217 |
|
| 218 |
if ( ! is_null( $screen ) && 'attachment' === $screen->id ) { |
| 219 |
return $form_fields; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
$fbv_folder = FolderModel::getFolderFromPostId( $post->ID ); |
| 224 |
$fbv_folder = count( $fbv_folder ) > 0 ? $fbv_folder[0] : (object) array( |
| 225 |
'folder_id' => 0, |
| 226 |
'name' => __( 'Uncategorized', 'filebird' ), |
| 227 |
); |
| 228 |
$folder_name = esc_attr( $fbv_folder->name ); |
| 229 |
$folder_id = (int) $fbv_folder->folder_id; |
| 230 |
$post_id = (int) $post->ID; |
| 231 |
|
| 232 |
$form_fields['fbv'] = array( |
| 233 |
'html' => "<div class='fbv-attachment-edit-wrapper' data-folder-id='{$folder_id}' data-attachment-id='{$post_id}'><input readonly type='text' value='{$folder_name}'/></div>", |
| 234 |
'label' => esc_html__( 'FileBird folder:', 'filebird' ), |
| 235 |
'helps' => esc_html__( 'Click on the folder name to move this file to another folder', 'filebird' ), |
| 236 |
'input' => 'html', |
| 237 |
); |
| 238 |
|
| 239 |
return $form_fields; |
| 240 |
} |
| 241 |
|
| 242 |
public function edited_image_metadata( $new_image_meta, $new_attachment_id, $attachment_id ) { |
| 243 |
$folder = FolderModel::getFolderFromPostId( $attachment_id ); |
| 244 |
if ( is_array( $folder ) && count( $folder ) > 0 ) { |
| 245 |
if ( (int) $folder[0]->folder_id > 0 ) { |
| 246 |
FolderModel::setFoldersForPosts( $new_attachment_id, (int) $folder[0]->folder_id ); |
| 247 |
} |
| 248 |
} |
| 249 |
return $new_image_meta; |
| 250 |
} |
| 251 |
|
| 252 |
public function attachment_fields_to_save( $post, $attachment ) { |
| 253 |
if ( isset( $attachment['fbv'] ) ) { |
| 254 |
FolderModel::setFoldersForPosts( $post['ID'], $attachment['fbv'] ); |
| 255 |
} |
| 256 |
return $post; |
| 257 |
} |
| 258 |
|
| 259 |
public function actionPluploadUi() { |
| 260 |
?> |
| 261 |
<div class="fbv-upload-inline"> |
| 262 |
<label for="fbv"><?php esc_html_e( 'Choose folder: ', 'filebird' ); ?></label> |
| 263 |
<span id="fbv-folder-selector" class="fbv-folder-selector" name="fbv"></span> |
| 264 |
</div> |
| 265 |
<?php |
| 266 |
} |
| 267 |
|
| 268 |
public function getFlatTree( $data = array(), $parent = 0, $default = null, $level = 0 ) { |
| 269 |
$tree = is_null( $default ) ? array() : $default; |
| 270 |
foreach ( $data as $k => $v ) { |
| 271 |
if ( $v->parent == $parent ) { |
| 272 |
$node = array( |
| 273 |
'title' => str_repeat( '-', $level ) . $v->name, |
| 274 |
'value' => $v->id, |
| 275 |
); |
| 276 |
$tree[] = $node; |
| 277 |
$children = $this->getFlatTree( $data, $v->id, null, $level + 1 ); |
| 278 |
foreach ( $children as $k2 => $child ) { |
| 279 |
$tree[] = $child; |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
return $tree; |
| 284 |
} |
| 285 |
|
| 286 |
public function deleted_user( $id, $reassign, $user ) { |
| 287 |
if ( $reassign === null ) { |
| 288 |
FolderModel::deleteByAuthor( $id ); |
| 289 |
} else { |
| 290 |
FolderModel::updateAuthor( $id, (int) $reassign ); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
public function users_have_additional_content( $users_have_content, $userids ) { |
| 295 |
global $wpdb; |
| 296 |
if ( $userids && ! $users_have_content ) { |
| 297 |
if ( $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}fbv WHERE created_by IN( " . implode( ',', $userids ) . ' ) LIMIT 1' ) ) { |
| 298 |
$users_have_content = true; |
| 299 |
} |
| 300 |
} |
| 301 |
return $users_have_content; |
| 302 |
} |
| 303 |
} |