PageRenderTime 95ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/upload.php

https://gitlab.com/WPonEB/WPonEB
PHP | 331 lines | 260 code | 54 blank | 17 comment | 54 complexity | 07fd427d371d52fb67c1c0868a17ebf1 MD5 | raw file
  1. <?php
  2. /**
  3. * Media Library administration panel.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /** WordPress Administration Bootstrap */
  9. require_once( dirname( __FILE__ ) . '/admin.php' );
  10. if ( !current_user_can('upload_files') )
  11. wp_die( __( 'Sorry, you are not allowed to upload files.' ) );
  12. $mode = get_user_option( 'media_library_mode', get_current_user_id() ) ? get_user_option( 'media_library_mode', get_current_user_id() ) : 'grid';
  13. $modes = array( 'grid', 'list' );
  14. if ( isset( $_GET['mode'] ) && in_array( $_GET['mode'], $modes ) ) {
  15. $mode = $_GET['mode'];
  16. update_user_option( get_current_user_id(), 'media_library_mode', $mode );
  17. }
  18. if ( 'grid' === $mode ) {
  19. wp_enqueue_media();
  20. wp_enqueue_script( 'media-grid' );
  21. wp_enqueue_script( 'media' );
  22. remove_action( 'admin_head', 'wp_admin_canonical_url' );
  23. $q = $_GET;
  24. // let JS handle this
  25. unset( $q['s'] );
  26. $vars = wp_edit_attachments_query_vars( $q );
  27. $ignore = array( 'mode', 'post_type', 'post_status', 'posts_per_page' );
  28. foreach ( $vars as $key => $value ) {
  29. if ( ! $value || in_array( $key, $ignore ) ) {
  30. unset( $vars[ $key ] );
  31. }
  32. }
  33. wp_localize_script( 'media-grid', '_wpMediaGridSettings', array(
  34. 'adminUrl' => parse_url( self_admin_url(), PHP_URL_PATH ),
  35. 'queryVars' => (object) $vars
  36. ) );
  37. get_current_screen()->add_help_tab( array(
  38. 'id' => 'overview',
  39. 'title' => __( 'Overview' ),
  40. 'content' =>
  41. '<p>' . __( 'All the files you&#8217;ve uploaded are listed in the Media Library, with the most recent uploads listed first.' ) . '</p>' .
  42. '<p>' . __( 'You can view your media in a simple visual grid or a list with columns. Switch between these views using the icons to the left above the media.' ) . '</p>' .
  43. '<p>' . __( 'To delete media items, click the Bulk Select button at the top of the screen. Select any items you wish to delete, then click the Delete Selected button. Clicking the Cancel Selection button takes you back to viewing your media.' ) . '</p>'
  44. ) );
  45. get_current_screen()->add_help_tab( array(
  46. 'id' => 'attachment-details',
  47. 'title' => __( 'Attachment Details' ),
  48. 'content' =>
  49. '<p>' . __( 'Clicking an item will display an Attachment Details dialog, which allows you to preview media and make quick edits. Any changes you make to the attachment details will be automatically saved.' ) . '</p>' .
  50. '<p>' . __( 'Use the arrow buttons at the top of the dialog, or the left and right arrow keys on your keyboard, to navigate between media items quickly.' ) . '</p>' .
  51. '<p>' . __( 'You can also delete individual items and access the extended edit screen from the details dialog.' ) . '</p>'
  52. ) );
  53. get_current_screen()->set_help_sidebar(
  54. '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  55. '<p>' . __( '<a href="https://codex.wordpress.org/Media_Library_Screen">Documentation on Media Library</a>' ) . '</p>' .
  56. '<p>' . __( '<a href="https://wordpress.org/support/">Support Forums</a>' ) . '</p>'
  57. );
  58. $title = __('Media Library');
  59. $parent_file = 'upload.php';
  60. require_once( ABSPATH . 'wp-admin/admin-header.php' );
  61. ?>
  62. <div class="wrap" id="wp-media-grid" data-search="<?php _admin_search_query() ?>">
  63. <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
  64. <?php
  65. if ( current_user_can( 'upload_files' ) ) { ?>
  66. <a href="<?php echo admin_url( 'media-new.php' ); ?>" class="page-title-action aria-button-if-js"><?php echo esc_html_x( 'Add New', 'file' ); ?></a><?php
  67. }
  68. ?>
  69. <hr class="wp-header-end">
  70. <div class="error hide-if-js">
  71. <p><?php printf(
  72. /* translators: %s: list view URL */
  73. __( 'The grid view for the Media Library requires JavaScript. <a href="%s">Switch to the list view</a>.' ),
  74. 'upload.php?mode=list'
  75. ); ?></p>
  76. </div>
  77. </div>
  78. <?php
  79. include( ABSPATH . 'wp-admin/admin-footer.php' );
  80. exit;
  81. }
  82. $wp_list_table = _get_list_table('WP_Media_List_Table');
  83. $pagenum = $wp_list_table->get_pagenum();
  84. // Handle bulk actions
  85. $doaction = $wp_list_table->current_action();
  86. if ( $doaction ) {
  87. check_admin_referer('bulk-media');
  88. if ( 'delete_all' == $doaction ) {
  89. $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type='attachment' AND post_status = 'trash'" );
  90. $doaction = 'delete';
  91. } elseif ( isset( $_REQUEST['media'] ) ) {
  92. $post_ids = $_REQUEST['media'];
  93. } elseif ( isset( $_REQUEST['ids'] ) ) {
  94. $post_ids = explode( ',', $_REQUEST['ids'] );
  95. }
  96. $location = 'upload.php';
  97. if ( $referer = wp_get_referer() ) {
  98. if ( false !== strpos( $referer, 'upload.php' ) )
  99. $location = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'message', 'ids', 'posted' ), $referer );
  100. }
  101. switch ( $doaction ) {
  102. case 'detach':
  103. wp_media_attach_action( $_REQUEST['parent_post_id'], 'detach' );
  104. break;
  105. case 'attach':
  106. wp_media_attach_action( $_REQUEST['found_post_id'] );
  107. break;
  108. case 'trash':
  109. if ( !isset( $post_ids ) )
  110. break;
  111. foreach ( (array) $post_ids as $post_id ) {
  112. if ( !current_user_can( 'delete_post', $post_id ) )
  113. wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
  114. if ( !wp_trash_post( $post_id ) )
  115. wp_die( __( 'Error in moving to Trash.' ) );
  116. }
  117. $location = add_query_arg( array( 'trashed' => count( $post_ids ), 'ids' => join( ',', $post_ids ) ), $location );
  118. break;
  119. case 'untrash':
  120. if ( !isset( $post_ids ) )
  121. break;
  122. foreach ( (array) $post_ids as $post_id ) {
  123. if ( !current_user_can( 'delete_post', $post_id ) )
  124. wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
  125. if ( !wp_untrash_post( $post_id ) )
  126. wp_die( __( 'Error in restoring from Trash.' ) );
  127. }
  128. $location = add_query_arg( 'untrashed', count( $post_ids ), $location );
  129. break;
  130. case 'delete':
  131. if ( !isset( $post_ids ) )
  132. break;
  133. foreach ( (array) $post_ids as $post_id_del ) {
  134. if ( !current_user_can( 'delete_post', $post_id_del ) )
  135. wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
  136. if ( !wp_delete_attachment( $post_id_del ) )
  137. wp_die( __( 'Error in deleting.' ) );
  138. }
  139. $location = add_query_arg( 'deleted', count( $post_ids ), $location );
  140. break;
  141. default:
  142. /** This action is documented in wp-admin/edit-comments.php */
  143. $location = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $location, $doaction, $post_ids );
  144. }
  145. wp_redirect( $location );
  146. exit;
  147. } elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
  148. wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
  149. exit;
  150. }
  151. $wp_list_table->prepare_items();
  152. $title = __('Media Library');
  153. $parent_file = 'upload.php';
  154. wp_enqueue_script( 'media' );
  155. add_screen_option( 'per_page' );
  156. get_current_screen()->add_help_tab( array(
  157. 'id' => 'overview',
  158. 'title' => __('Overview'),
  159. 'content' =>
  160. '<p>' . __( 'All the files you&#8217;ve uploaded are listed in the Media Library, with the most recent uploads listed first. You can use the Screen Options tab to customize the display of this screen.' ) . '</p>' .
  161. '<p>' . __( 'You can narrow the list by file type/status or by date using the dropdown menus above the media table.' ) . '</p>' .
  162. '<p>' . __( 'You can view your media in a simple visual grid or a list with columns. Switch between these views using the icons to the left above the media.' ) . '</p>'
  163. ) );
  164. get_current_screen()->add_help_tab( array(
  165. 'id' => 'actions-links',
  166. 'title' => __('Available Actions'),
  167. 'content' =>
  168. '<p>' . __( 'Hovering over a row reveals action links: Edit, Delete Permanently, and View. Clicking Edit or on the media file&#8217;s name displays a simple screen to edit that individual file&#8217;s metadata. Clicking Delete Permanently will delete the file from the media library (as well as from any posts to which it is currently attached). View will take you to the display page for that file.' ) . '</p>'
  169. ) );
  170. get_current_screen()->add_help_tab( array(
  171. 'id' => 'attaching-files',
  172. 'title' => __('Attaching Files'),
  173. 'content' =>
  174. '<p>' . __( 'If a media file has not been attached to any content, you will see that in the Uploaded To column, and can click on Attach to launch a small popup that will allow you to search for existing content and attach the file.' ) . '</p>'
  175. ) );
  176. get_current_screen()->set_help_sidebar(
  177. '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  178. '<p>' . __( '<a href="https://codex.wordpress.org/Media_Library_Screen">Documentation on Media Library</a>' ) . '</p>' .
  179. '<p>' . __( '<a href="https://wordpress.org/support/">Support Forums</a>' ) . '</p>'
  180. );
  181. get_current_screen()->set_screen_reader_content( array(
  182. 'heading_views' => __( 'Filter media items list' ),
  183. 'heading_pagination' => __( 'Media items list navigation' ),
  184. 'heading_list' => __( 'Media items list' ),
  185. ) );
  186. require_once( ABSPATH . 'wp-admin/admin-header.php' );
  187. ?>
  188. <div class="wrap">
  189. <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
  190. <?php
  191. if ( current_user_can( 'upload_files' ) ) { ?>
  192. <a href="<?php echo admin_url( 'media-new.php' ); ?>" class="page-title-action"><?php echo esc_html_x( 'Add New', 'file' ); ?></a><?php
  193. }
  194. if ( isset( $_REQUEST['s'] ) && strlen( $_REQUEST['s'] ) ) {
  195. /* translators: %s: search keywords */
  196. printf( '<span class="subtitle">' . __( 'Search results for &#8220;%s&#8221;' ) . '</span>', get_search_query() );
  197. }
  198. ?>
  199. <hr class="wp-header-end">
  200. <?php
  201. $message = '';
  202. if ( ! empty( $_GET['posted'] ) ) {
  203. $message = __( 'Media file updated.' );
  204. $_SERVER['REQUEST_URI'] = remove_query_arg(array('posted'), $_SERVER['REQUEST_URI']);
  205. }
  206. if ( ! empty( $_GET['attached'] ) && $attached = absint( $_GET['attached'] ) ) {
  207. if ( 1 == $attached ) {
  208. $message = __( 'Media file attached.' );
  209. } else {
  210. /* translators: %s: number of media files */
  211. $message = _n( '%s media file attached.', '%s media files attached.', $attached );
  212. }
  213. $message = sprintf( $message, number_format_i18n( $attached ) );
  214. $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
  215. }
  216. if ( ! empty( $_GET['detach'] ) && $detached = absint( $_GET['detach'] ) ) {
  217. if ( 1 == $detached ) {
  218. $message = __( 'Media file detached.' );
  219. } else {
  220. /* translators: %s: number of media files */
  221. $message = _n( '%s media file detached.', '%s media files detached.', $detached );
  222. }
  223. $message = sprintf( $message, number_format_i18n( $detached ) );
  224. $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
  225. }
  226. if ( ! empty( $_GET['deleted'] ) && $deleted = absint( $_GET['deleted'] ) ) {
  227. if ( 1 == $deleted ) {
  228. $message = __( 'Media file permanently deleted.' );
  229. } else {
  230. /* translators: %s: number of media files */
  231. $message = _n( '%s media file permanently deleted.', '%s media files permanently deleted.', $deleted );
  232. }
  233. $message = sprintf( $message, number_format_i18n( $deleted ) );
  234. $_SERVER['REQUEST_URI'] = remove_query_arg(array('deleted'), $_SERVER['REQUEST_URI']);
  235. }
  236. if ( ! empty( $_GET['trashed'] ) && $trashed = absint( $_GET['trashed'] ) ) {
  237. if ( 1 == $trashed ) {
  238. $message = __( 'Media file moved to the trash.' );
  239. } else {
  240. /* translators: %s: number of media files */
  241. $message = _n( '%s media file moved to the trash.', '%s media files moved to the trash.', $trashed );
  242. }
  243. $message = sprintf( $message, number_format_i18n( $trashed ) );
  244. $message .= ' <a href="' . esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids='.(isset($_GET['ids']) ? $_GET['ids'] : ''), "bulk-media" ) ) . '">' . __('Undo') . '</a>';
  245. $_SERVER['REQUEST_URI'] = remove_query_arg(array('trashed'), $_SERVER['REQUEST_URI']);
  246. }
  247. if ( ! empty( $_GET['untrashed'] ) && $untrashed = absint( $_GET['untrashed'] ) ) {
  248. if ( 1 == $untrashed ) {
  249. $message = __( 'Media file restored from the trash.' );
  250. } else {
  251. /* translators: %s: number of media files */
  252. $message = _n( '%s media file restored from the trash.', '%s media files restored from the trash.', $untrashed );
  253. }
  254. $message = sprintf( $message, number_format_i18n( $untrashed ) );
  255. $_SERVER['REQUEST_URI'] = remove_query_arg(array('untrashed'), $_SERVER['REQUEST_URI']);
  256. }
  257. $messages[1] = __( 'Media file updated.' );
  258. $messages[2] = __( 'Media file permanently deleted.' );
  259. $messages[3] = __( 'Error saving media file.' );
  260. $messages[4] = __( 'Media file moved to the trash.' ) . ' <a href="' . esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids='.(isset($_GET['ids']) ? $_GET['ids'] : ''), "bulk-media" ) ) . '">' . __( 'Undo' ) . '</a>';
  261. $messages[5] = __( 'Media file restored from the trash.' );
  262. if ( ! empty( $_GET['message'] ) && isset( $messages[ $_GET['message'] ] ) ) {
  263. $message = $messages[ $_GET['message'] ];
  264. $_SERVER['REQUEST_URI'] = remove_query_arg(array('message'), $_SERVER['REQUEST_URI']);
  265. }
  266. if ( !empty($message) ) { ?>
  267. <div id="message" class="updated notice is-dismissible"><p><?php echo $message; ?></p></div>
  268. <?php } ?>
  269. <form id="posts-filter" method="get">
  270. <?php $wp_list_table->views(); ?>
  271. <?php $wp_list_table->display(); ?>
  272. <div id="ajax-response"></div>
  273. <?php find_posts_div(); ?>
  274. </form>
  275. </div>
  276. <?php
  277. include( ABSPATH . 'wp-admin/admin-footer.php' );