wp/wp-admin/upload.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     9 /** WordPress Administration Bootstrap */
     9 /** WordPress Administration Bootstrap */
    10 require_once( dirname( __FILE__ ) . '/admin.php' );
    10 require_once( dirname( __FILE__ ) . '/admin.php' );
    11 
    11 
    12 if ( !current_user_can('upload_files') )
    12 if ( !current_user_can('upload_files') )
    13 	wp_die( __( 'You do not have permission to upload files.' ) );
    13 	wp_die( __( 'You do not have permission to upload files.' ) );
       
    14 
       
    15 $mode = get_user_option( 'media_library_mode', get_current_user_id() ) ? get_user_option( 'media_library_mode', get_current_user_id() ) : 'grid';
       
    16 $modes = array( 'grid', 'list' );
       
    17 
       
    18 if ( isset( $_GET['mode'] ) && in_array( $_GET['mode'], $modes ) ) {
       
    19 	$mode = $_GET['mode'];
       
    20 	update_user_option( get_current_user_id(), 'media_library_mode', $mode );
       
    21 }
       
    22 
       
    23 if ( 'grid' === $mode ) {
       
    24 	wp_enqueue_media();
       
    25 	wp_enqueue_script( 'media-grid' );
       
    26 	wp_enqueue_script( 'media' );
       
    27 
       
    28 	$q = $_GET;
       
    29 	// let JS handle this
       
    30 	unset( $q['s'] );
       
    31 	$vars = wp_edit_attachments_query_vars( $q );
       
    32 	$ignore = array( 'mode', 'post_type', 'post_status', 'posts_per_page' );
       
    33 	foreach ( $vars as $key => $value ) {
       
    34 		if ( ! $value || in_array( $key, $ignore ) ) {
       
    35 			unset( $vars[ $key ] );
       
    36 		}
       
    37 	}
       
    38 
       
    39 	wp_localize_script( 'media-grid', '_wpMediaGridSettings', array(
       
    40 		'adminUrl' => parse_url( self_admin_url(), PHP_URL_PATH ),
       
    41 		'queryVars' => (object) $vars
       
    42 	) );
       
    43 
       
    44 	get_current_screen()->add_help_tab( array(
       
    45 		'id'		=> 'overview',
       
    46 		'title'		=> __( 'Overview' ),
       
    47 		'content'	=>
       
    48 			'<p>' . __( 'All the files you&#8217;ve uploaded are listed in the Media Library, with the most recent uploads listed first.' ) . '</p>' .
       
    49 			'<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>' .
       
    50 			'<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>'
       
    51 	) );
       
    52 
       
    53 	get_current_screen()->add_help_tab( array(
       
    54 		'id'		=> 'attachment-details',
       
    55 		'title'		=> __( 'Attachment Details' ),
       
    56 		'content'	=>
       
    57 			'<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>' .
       
    58 			'<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>' .
       
    59 			'<p>' . __( 'You can also delete individual items and access the extended edit screen from the details dialog.' ) . '</p>'
       
    60 	) );
       
    61 
       
    62 	get_current_screen()->set_help_sidebar(
       
    63 		'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
       
    64 		'<p>' . __( '<a href="https://codex.wordpress.org/Media_Library_Screen" target="_blank">Documentation on Media Library</a>' ) . '</p>' .
       
    65 		'<p>' . __( '<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
       
    66 	);
       
    67 
       
    68 	$title = __('Media Library');
       
    69 	$parent_file = 'upload.php';
       
    70 
       
    71 	require_once( ABSPATH . 'wp-admin/admin-header.php' );
       
    72 	?>
       
    73 	<div class="wrap" id="wp-media-grid" data-search="<?php _admin_search_query() ?>">
       
    74 		<h2>
       
    75 		<?php
       
    76 		echo esc_html( $title );
       
    77 		if ( current_user_can( 'upload_files' ) ) { ?>
       
    78 			<a href="media-new.php" class="add-new-h2"><?php echo esc_html_x( 'Add New', 'file' ); ?></a><?php
       
    79 		}
       
    80 		?>
       
    81 		</h2>
       
    82 		<div class="error hide-if-js">
       
    83 			<p><?php _e( 'The grid view for the Media Library requires JavaScript. <a href="upload.php?mode=list">Switch to the list view</a>.' ); ?></p>
       
    84 		</div>
       
    85 	</div>
       
    86 	<?php
       
    87 	include( ABSPATH . 'wp-admin/admin-footer.php' );
       
    88 	exit;
       
    89 }
    14 
    90 
    15 $wp_list_table = _get_list_table('WP_Media_List_Table');
    91 $wp_list_table = _get_list_table('WP_Media_List_Table');
    16 $pagenum = $wp_list_table->get_pagenum();
    92 $pagenum = $wp_list_table->get_pagenum();
    17 
    93 
    18 // Handle bulk actions
    94 // Handle bulk actions
    35 		if ( false !== strpos( $referer, 'upload.php' ) )
   111 		if ( false !== strpos( $referer, 'upload.php' ) )
    36 			$location = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'message', 'ids', 'posted' ), $referer );
   112 			$location = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'message', 'ids', 'posted' ), $referer );
    37 	}
   113 	}
    38 
   114 
    39 	switch ( $doaction ) {
   115 	switch ( $doaction ) {
    40 		case 'find_detached':
   116 		case 'detach':
    41 			if ( !current_user_can('edit_posts') )
   117 			wp_media_attach_action( $_REQUEST['parent_post_id'], 'detach' );
    42 				wp_die( __('You are not allowed to scan for lost attachments.') );
   118 			break;
    43 
   119 
    44 			$lost = $wpdb->get_col( "
       
    45 				SELECT ID FROM $wpdb->posts
       
    46 				WHERE post_type = 'attachment' AND post_parent > '0'
       
    47 				AND post_parent NOT IN (
       
    48 					SELECT ID FROM $wpdb->posts
       
    49 					WHERE post_type NOT IN ( 'attachment', '" . join( "', '", get_post_types( array( 'public' => false ) ) ) . "' )
       
    50 				)
       
    51 			" );
       
    52 
       
    53 			$_REQUEST['detached'] = 1;
       
    54 			break;
       
    55 		case 'attach':
   120 		case 'attach':
    56 			$parent_id = (int) $_REQUEST['found_post_id'];
   121 			wp_media_attach_action( $_REQUEST['found_post_id'] );
    57 			if ( !$parent_id )
   122 			break;
    58 				return;
   123 
    59 
       
    60 			$parent = get_post( $parent_id );
       
    61 			if ( !current_user_can( 'edit_post', $parent_id ) )
       
    62 				wp_die( __( 'You are not allowed to edit this post.' ) );
       
    63 
       
    64 			$attach = array();
       
    65 			foreach ( (array) $_REQUEST['media'] as $att_id ) {
       
    66 				$att_id = (int) $att_id;
       
    67 
       
    68 				if ( !current_user_can( 'edit_post', $att_id ) )
       
    69 					continue;
       
    70 
       
    71 				$attach[] = $att_id;
       
    72 			}
       
    73 
       
    74 			if ( ! empty( $attach ) ) {
       
    75 				$attach_string = implode( ',', $attach );
       
    76 				$attached = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $attach_string )", $parent_id ) );
       
    77 				foreach ( $attach as $att_id ) {
       
    78 					clean_attachment_cache( $att_id );
       
    79 				}
       
    80 			}
       
    81 
       
    82 			if ( isset( $attached ) ) {
       
    83 				$location = 'upload.php';
       
    84 				if ( $referer = wp_get_referer() ) {
       
    85 					if ( false !== strpos( $referer, 'upload.php' ) )
       
    86 						$location = $referer;
       
    87 				}
       
    88 
       
    89 				$location = add_query_arg( array( 'attached' => $attached ) , $location );
       
    90 				wp_redirect( $location );
       
    91 				exit;
       
    92 			}
       
    93 			break;
       
    94 		case 'trash':
   124 		case 'trash':
    95 			if ( !isset( $post_ids ) )
   125 			if ( !isset( $post_ids ) )
    96 				break;
   126 				break;
    97 			foreach ( (array) $post_ids as $post_id ) {
   127 			foreach ( (array) $post_ids as $post_id ) {
    98 				if ( !current_user_can( 'delete_post', $post_id ) )
   128 				if ( !current_user_can( 'delete_post', $post_id ) )
   139 $wp_list_table->prepare_items();
   169 $wp_list_table->prepare_items();
   140 
   170 
   141 $title = __('Media Library');
   171 $title = __('Media Library');
   142 $parent_file = 'upload.php';
   172 $parent_file = 'upload.php';
   143 
   173 
   144 wp_enqueue_script( 'wp-ajax-response' );
       
   145 wp_enqueue_script( 'jquery-ui-draggable' );
       
   146 wp_enqueue_script( 'media' );
   174 wp_enqueue_script( 'media' );
   147 
   175 
   148 add_screen_option( 'per_page', array('label' => _x( 'Media items', 'items per page (screen options)' )) );
   176 add_screen_option( 'per_page' );
   149 
   177 
   150 get_current_screen()->add_help_tab( array(
   178 get_current_screen()->add_help_tab( array(
   151 'id'		=> 'overview',
   179 'id'		=> 'overview',
   152 'title'		=> __('Overview'),
   180 'title'		=> __('Overview'),
   153 'content'	=>
   181 'content'	=>
   154 	'<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>' .
   182 	'<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>' .
   155 	'<p>' . __( 'You can narrow the list by file type/status using the text link filters at the top of the screen. You also can refine the list by date using the dropdown menu above the media table.' ) . '</p>'
   183 	'<p>' . __( 'You can narrow the list by file type/status using the text link filters at the top of the screen. You also can refine the list by date using the dropdown menu above the media table.' ) . '</p>' .
       
   184 	'<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>'
   156 ) );
   185 ) );
   157 get_current_screen()->add_help_tab( array(
   186 get_current_screen()->add_help_tab( array(
   158 'id'		=> 'actions-links',
   187 'id'		=> 'actions-links',
   159 'title'		=> __('Available Actions'),
   188 'title'		=> __('Available Actions'),
   160 'content'	=>
   189 'content'	=>
   167 	'<p>' . __( 'If a media file has not been attached to any post, you will see that in the Attached To column, and can click on Attach File to launch a small popup that will allow you to search for a post and attach the file.' ) . '</p>'
   196 	'<p>' . __( 'If a media file has not been attached to any post, you will see that in the Attached To column, and can click on Attach File to launch a small popup that will allow you to search for a post and attach the file.' ) . '</p>'
   168 ) );
   197 ) );
   169 
   198 
   170 get_current_screen()->set_help_sidebar(
   199 get_current_screen()->set_help_sidebar(
   171 	'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
   200 	'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
   172 	'<p>' . __( '<a href="http://codex.wordpress.org/Media_Library_Screen" target="_blank">Documentation on Media Library</a>' ) . '</p>' .
   201 	'<p>' . __( '<a href="https://codex.wordpress.org/Media_Library_Screen" target="_blank">Documentation on Media Library</a>' ) . '</p>' .
   173 	'<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
   202 	'<p>' . __( '<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
   174 );
   203 );
   175 
   204 
   176 require_once( ABSPATH . 'wp-admin/admin-header.php' );
   205 require_once( ABSPATH . 'wp-admin/admin-header.php' );
   177 ?>
   206 ?>
   178 
   207 
   179 <div class="wrap">
   208 <div class="wrap">
   180 <?php screen_icon(); ?>
       
   181 <h2>
   209 <h2>
   182 <?php
   210 <?php
   183 echo esc_html( $title );
   211 echo esc_html( $title );
   184 if ( current_user_can( 'upload_files' ) ) { ?>
   212 if ( current_user_can( 'upload_files' ) ) { ?>
   185 	<a href="media-new.php" class="add-new-h2"><?php echo esc_html_x('Add New', 'file'); ?></a><?php
   213 	<a href="media-new.php" class="add-new-h2"><?php echo esc_html_x('Add New', 'file'); ?></a><?php
   189 </h2>
   217 </h2>
   190 
   218 
   191 <?php
   219 <?php
   192 $message = '';
   220 $message = '';
   193 if ( ! empty( $_GET['posted'] ) ) {
   221 if ( ! empty( $_GET['posted'] ) ) {
   194 	$message = __('Media attachment updated.');
   222 	$message = __( 'Media attachment updated.' );
   195 	$_SERVER['REQUEST_URI'] = remove_query_arg(array('posted'), $_SERVER['REQUEST_URI']);
   223 	$_SERVER['REQUEST_URI'] = remove_query_arg(array('posted'), $_SERVER['REQUEST_URI']);
   196 }
   224 }
   197 
   225 
   198 if ( ! empty( $_GET['attached'] ) && $attached = absint( $_GET['attached'] ) ) {
   226 if ( ! empty( $_GET['attached'] ) && $attached = absint( $_GET['attached'] ) ) {
   199 	$message = sprintf( _n('Reattached %d attachment.', 'Reattached %d attachments.', $attached), $attached );
   227 	$message = sprintf( _n( 'Reattached %d attachment.', 'Reattached %d attachments.', $attached ), $attached );
   200 	$_SERVER['REQUEST_URI'] = remove_query_arg(array('attached'), $_SERVER['REQUEST_URI']);
   228 	$_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
       
   229 }
       
   230 
       
   231 if ( ! empty( $_GET['detach'] ) && $detached = absint( $_GET['detach'] ) ) {
       
   232 	$message = sprintf( _n( 'Detached %d attachment.', 'Detached %d attachments.', $detached ), $detached );
       
   233 	$_SERVER['REQUEST_URI'] = remove_query_arg( array( 'detach', 'attached' ), $_SERVER['REQUEST_URI'] );
   201 }
   234 }
   202 
   235 
   203 if ( ! empty( $_GET['deleted'] ) && $deleted = absint( $_GET['deleted'] ) ) {
   236 if ( ! empty( $_GET['deleted'] ) && $deleted = absint( $_GET['deleted'] ) ) {
   204 	$message = sprintf( _n( 'Media attachment permanently deleted.', '%d media attachments permanently deleted.', $deleted ), number_format_i18n( $_GET['deleted'] ) );
   237 	if ( 1 == $deleted ) {
       
   238 		$message = __( 'Media attachment permanently deleted.' );
       
   239 	} else {
       
   240 		$message = _n( '%d media attachment permanently deleted.', '%d media attachments permanently deleted.', $deleted );
       
   241 	}
       
   242 	$message = sprintf( $message, number_format_i18n( $deleted ) );
   205 	$_SERVER['REQUEST_URI'] = remove_query_arg(array('deleted'), $_SERVER['REQUEST_URI']);
   243 	$_SERVER['REQUEST_URI'] = remove_query_arg(array('deleted'), $_SERVER['REQUEST_URI']);
   206 }
   244 }
   207 
   245 
   208 if ( ! empty( $_GET['trashed'] ) && $trashed = absint( $_GET['trashed'] ) ) {
   246 if ( ! empty( $_GET['trashed'] ) && $trashed = absint( $_GET['trashed'] ) ) {
   209 	$message = sprintf( _n( 'Media attachment moved to the trash.', '%d media attachments moved to the trash.', $trashed ), number_format_i18n( $_GET['trashed'] ) );
   247 	if ( 1 == $trashed ) {
       
   248 		$message = __( 'Media attachment moved to the trash.' );
       
   249 	} else {
       
   250 		$message = _n( '%d media attachment moved to the trash.', '%d media attachments moved to the trash.', $trashed );
       
   251 	}
       
   252 	$message = sprintf( $message, number_format_i18n( $trashed ) );
   210 	$message .= ' <a href="' . esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids='.(isset($_GET['ids']) ? $_GET['ids'] : ''), "bulk-media" ) ) . '">' . __('Undo') . '</a>';
   253 	$message .= ' <a href="' . esc_url( wp_nonce_url( 'upload.php?doaction=undo&action=untrash&ids='.(isset($_GET['ids']) ? $_GET['ids'] : ''), "bulk-media" ) ) . '">' . __('Undo') . '</a>';
   211 	$_SERVER['REQUEST_URI'] = remove_query_arg(array('trashed'), $_SERVER['REQUEST_URI']);
   254 	$_SERVER['REQUEST_URI'] = remove_query_arg(array('trashed'), $_SERVER['REQUEST_URI']);
   212 }
   255 }
   213 
   256 
   214 if ( ! empty( $_GET['untrashed'] ) && $untrashed = absint( $_GET['untrashed'] ) ) {
   257 if ( ! empty( $_GET['untrashed'] ) && $untrashed = absint( $_GET['untrashed'] ) ) {
   215 	$message = sprintf( _n( 'Media attachment restored from the trash.', '%d media attachments restored from the trash.', $untrashed ), number_format_i18n( $_GET['untrashed'] ) );
   258 	if ( 1 == $untrashed ) {
       
   259 		$message = __( 'Media attachment restored from the trash.' );
       
   260 	} else {
       
   261 		$message = _n( '%d media attachment restored from the trash.', '%d media attachments restored from the trash.', $untrashed );
       
   262 	}
       
   263 	$message = sprintf( $message, number_format_i18n( $untrashed ) );
   216 	$_SERVER['REQUEST_URI'] = remove_query_arg(array('untrashed'), $_SERVER['REQUEST_URI']);
   264 	$_SERVER['REQUEST_URI'] = remove_query_arg(array('untrashed'), $_SERVER['REQUEST_URI']);
   217 }
   265 }
   218 
   266 
   219 $messages[1] = __('Media attachment updated.');
   267 $messages[1] = __('Media attachment updated.');
   220 $messages[2] = __('Media permanently deleted.');
   268 $messages[2] = __('Media permanently deleted.');
   226 	$message = $messages[ $_GET['message'] ];
   274 	$message = $messages[ $_GET['message'] ];
   227 	$_SERVER['REQUEST_URI'] = remove_query_arg(array('message'), $_SERVER['REQUEST_URI']);
   275 	$_SERVER['REQUEST_URI'] = remove_query_arg(array('message'), $_SERVER['REQUEST_URI']);
   228 }
   276 }
   229 
   277 
   230 if ( !empty($message) ) { ?>
   278 if ( !empty($message) ) { ?>
   231 <div id="message" class="updated"><p><?php echo $message; ?></p></div>
   279 <div id="message" class="updated notice is-dismissible"><p><?php echo $message; ?></p></div>
   232 <?php } ?>
   280 <?php } ?>
   233 
   281 
       
   282 <form id="posts-filter" method="get">
       
   283 
   234 <?php $wp_list_table->views(); ?>
   284 <?php $wp_list_table->views(); ?>
   235 
       
   236 <form id="posts-filter" action="" method="get">
       
   237 
       
   238 <?php $wp_list_table->search_box( __( 'Search Media' ), 'media' ); ?>
       
   239 
   285 
   240 <?php $wp_list_table->display(); ?>
   286 <?php $wp_list_table->display(); ?>
   241 
   287 
   242 <div id="ajax-response"></div>
   288 <div id="ajax-response"></div>
   243 <?php find_posts_div(); ?>
   289 <?php find_posts_div(); ?>
   244 <br class="clear" />
       
   245 
       
   246 </form>
   290 </form>
   247 </div>
   291 </div>
   248 
   292 
   249 <?php
   293 <?php
   250 include( ABSPATH . 'wp-admin/admin-footer.php' );
   294 include( ABSPATH . 'wp-admin/admin-footer.php' );