'attachment', 'default' => 1, 'label' => __('Media', 'azdrive'), ]; return $types; } /** * Check if the current screen is an attachment page * @param string $type */ public function isAttachmentPage($type, $screen) { if( isset($screen->base) && $screen->base == 'upload' && $screen->id == 'upload' && $screen->post_type == 'attachment' ){ $type = 'attachment'; } return $type; } /** * Add a button to choose folder in modal view media detail page. */ public function modalBtnFolder($form_fields, $post) { $screen = null; if ( function_exists( 'get_current_screen' ) ) { $screen = get_current_screen(); if ( ! is_null( $screen ) && 'attachment' === $screen->id ) { return $form_fields; } } $field = [ 'id' => 0, 'title' => __('Choose a folder', 'azdrive') ]; $folder = PostModel::getFolderByPostId($post->ID); if( $folder ){ $field = [ 'id' => $folder->id, 'title' => $folder->title ]; } // get post name $title = $post->post_title; // Add a custom select field $form_fields['azdrive_folder_btn'] = array( 'label' => __('AzDrive Folder:', 'azdrive'), 'input' => 'html', 'html' => '' ); return $form_fields; } /** * Add a button to choose folder in upload media page. (media-new.php) */ public function btnSelectFolder() { $screen = null; if ( function_exists( 'get_current_screen' ) ) { $screen = get_current_screen(); if( $screen && $screen->action == 'add' ) { Helpers::view('app/sidebar', array( 'type' => 'attachment', 'layout' => 'choose' )); } } } /** * Add a sidebar in modal media view */ public function modalSidebar($strings) { $strings['azdrive_sidebar'] = '
'; return $strings; } /** * Filter the media library query to include custom folders * * @param array $query The media query arguments. * @return array The modified query arguments. */ public function ajaxQueryAttachmentsArgs($query) { if ( ! current_user_can( 'read' ) ) { wp_send_json_error(); } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $folder_id = ( isset($_REQUEST['query']) && isset($_REQUEST['query']['folder_id']) ) ? intval($_REQUEST['query']['folder_id']) : null; if ( $folder_id != null) { // phpcs:ignore WordPress.Security $_nonce = ( isset($_REQUEST['query']) && isset($_REQUEST['query']['_nonce']) ) ? wp_unslash($_REQUEST['query']['_nonce']) : null; if ( !wp_verify_nonce($_nonce, 'wp_rest') ) { wp_send_json_error(); } // get all posts if( $folder_id == 0 ) return $query; // get all files with Uncategorized if( $folder_id == -1 ){ $postIds = PostModel::getPostsIdByFolderId(); if($postIds && count($postIds) > 0){ $query['post__not_in'] = $postIds; }else{ $query['post__not_in'] = []; } return $query; } // get all files ids in folder $postIds = is_numeric($folder_id) ? PostModel::getPostsIdByFolderId($folder_id) : []; if (!empty($postIds) && is_array($postIds)) { $query['post__in'] = $postIds; } else { $query['post__in'] = [0]; } } return $query; } /** * Add attachment * @param int $post_id */ public function addAttachment($post_id){ if ( ! current_user_can( 'upload_files' ) ) { return; } // phpcs:ignore WordPress.Security $_wpnonce = ( isset($_REQUEST['_wpnonce']) ) ? wp_unslash($_REQUEST['_wpnonce']) : null; if ( !wp_verify_nonce($_wpnonce, 'media-form') ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $folder_id = isset($_REQUEST['folder_id']) ? intval($_REQUEST['folder_id']) : 0; if( !$folder_id ) return; // add file to folder $result = PostModel::assignPostToFolder('attachment', $post_id, $folder_id); } /** * Delete attachment * @param int $post_id */ public function deleteAttachment($post_id){ PostModel::deletePostFromFolder($post_id); } }