wp/wp-admin/media.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
     7  */
     7  */
     8 
     8 
     9 /** Load WordPress Administration Bootstrap */
     9 /** Load WordPress Administration Bootstrap */
    10 require_once( dirname( __FILE__ ) . '/admin.php' );
    10 require_once( dirname( __FILE__ ) . '/admin.php' );
    11 
    11 
    12 $parent_file = 'upload.php';
    12 $parent_file  = 'upload.php';
    13 $submenu_file = 'upload.php';
    13 $submenu_file = 'upload.php';
    14 
    14 
    15 wp_reset_vars(array('action'));
    15 wp_reset_vars( array( 'action' ) );
    16 
    16 
    17 switch ( $action ) {
    17 switch ( $action ) {
    18 case 'editattachment' :
    18 	case 'editattachment':
    19 	$attachment_id = (int) $_POST['attachment_id'];
    19 		$attachment_id = (int) $_POST['attachment_id'];
    20 	check_admin_referer('media-form');
    20 		check_admin_referer( 'media-form' );
    21 
    21 
    22 	if ( !current_user_can('edit_post', $attachment_id) )
    22 		if ( ! current_user_can( 'edit_post', $attachment_id ) ) {
    23 		wp_die ( __('Sorry, you are not allowed to edit this attachment.') );
    23 			wp_die( __( 'Sorry, you are not allowed to edit this attachment.' ) );
       
    24 		}
    24 
    25 
    25 	$errors = media_upload_form_handler();
    26 		$errors = media_upload_form_handler();
    26 
    27 
    27 	if ( empty($errors) ) {
    28 		if ( empty( $errors ) ) {
    28 		$location = 'media.php';
    29 			$location = 'media.php';
    29 		if ( $referer = wp_get_original_referer() ) {
    30 			if ( $referer = wp_get_original_referer() ) {
    30 			if ( false !== strpos($referer, 'upload.php') || ( url_to_postid($referer) == $attachment_id )  )
    31 				if ( false !== strpos( $referer, 'upload.php' ) || ( url_to_postid( $referer ) == $attachment_id ) ) {
    31 				$location = $referer;
    32 					$location = $referer;
       
    33 				}
       
    34 			}
       
    35 			if ( false !== strpos( $location, 'upload.php' ) ) {
       
    36 				$location = remove_query_arg( 'message', $location );
       
    37 				$location = add_query_arg( 'posted', $attachment_id, $location );
       
    38 			} elseif ( false !== strpos( $location, 'media.php' ) ) {
       
    39 				$location = add_query_arg( 'message', 'updated', $location );
       
    40 			}
       
    41 			wp_redirect( $location );
       
    42 			exit;
    32 		}
    43 		}
    33 		if ( false !== strpos($location, 'upload.php') ) {
    44 
    34 			$location = remove_query_arg('message', $location);
    45 		// No break.
    35 			$location = add_query_arg('posted',	$attachment_id, $location);
    46 	case 'edit':
    36 		} elseif ( false !== strpos($location, 'media.php') ) {
    47 		$title = __( 'Edit Media' );
    37 			$location = add_query_arg('message', 'updated', $location);
    48 
       
    49 		if ( empty( $errors ) ) {
       
    50 			$errors = null;
    38 		}
    51 		}
    39 		wp_redirect($location);
       
    40 		exit;
       
    41 	}
       
    42 
    52 
    43 	// No break.
    53 		if ( empty( $_GET['attachment_id'] ) ) {
    44 case 'edit' :
    54 			wp_redirect( admin_url( 'upload.php' ) );
    45 	$title = __('Edit Media');
    55 			exit();
       
    56 		}
       
    57 		$att_id = (int) $_GET['attachment_id'];
    46 
    58 
    47 	if ( empty($errors) )
    59 		if ( ! current_user_can( 'edit_post', $att_id ) ) {
    48 		$errors = null;
    60 			wp_die( __( 'Sorry, you are not allowed to edit this attachment.' ) );
       
    61 		}
    49 
    62 
    50 	if ( empty( $_GET['attachment_id'] ) ) {
    63 		$att = get_post( $att_id );
    51 		wp_redirect( admin_url('upload.php') );
       
    52 		exit();
       
    53 	}
       
    54 	$att_id = (int) $_GET['attachment_id'];
       
    55 
    64 
    56 	if ( !current_user_can('edit_post', $att_id) )
    65 		if ( empty( $att->ID ) ) {
    57 		wp_die ( __('Sorry, you are not allowed to edit this attachment.') );
    66 			wp_die( __( 'You attempted to edit an attachment that doesn’t exist. Perhaps it was deleted?' ) );
       
    67 		}
       
    68 		if ( 'attachment' !== $att->post_type ) {
       
    69 			wp_die( __( 'You attempted to edit an item that isn’t an attachment. Please go back and try again.' ) );
       
    70 		}
       
    71 		if ( $att->post_status == 'trash' ) {
       
    72 			wp_die( __( 'You can’t edit this attachment because it is in the Trash. Please move it out of the Trash and try again.' ) );
       
    73 		}
    58 
    74 
    59 	$att = get_post($att_id);
    75 		add_filter( 'attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2 );
    60 
    76 
    61 	if ( empty($att->ID) ) wp_die( __('You attempted to edit an attachment that doesn’t exist. Perhaps it was deleted?') );
    77 		wp_enqueue_script( 'wp-ajax-response' );
    62 	if ( 'attachment' !== $att->post_type ) wp_die( __('You attempted to edit an item that isn’t an attachment. Please go back and try again.') );
    78 		wp_enqueue_script( 'image-edit' );
    63 	if ( $att->post_status == 'trash' ) wp_die( __('You can’t edit this attachment because it is in the Trash. Please move it out of the Trash and try again.') );
    79 		wp_enqueue_style( 'imgareaselect' );
    64 
    80 
    65 	add_filter('attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2);
    81 		get_current_screen()->add_help_tab(
       
    82 			array(
       
    83 				'id'      => 'overview',
       
    84 				'title'   => __( 'Overview' ),
       
    85 				'content' =>
       
    86 					  '<p>' . __( 'This screen allows you to edit five fields for metadata in a file within the media library.' ) . '</p>' .
       
    87 					  '<p>' . __( 'For images only, you can click on Edit Image under the thumbnail to expand out an inline image editor with icons for cropping, rotating, or flipping the image as well as for undoing and redoing. The boxes on the right give you more options for scaling the image, for cropping it, and for cropping the thumbnail in a different way than you crop the original image. You can click on Help in those boxes to get more information.' ) . '</p>' .
       
    88 					  '<p>' . __( 'Note that you crop the image by clicking on it (the Crop icon is already selected) and dragging the cropping frame to select the desired part. Then click Save to retain the cropping.' ) . '</p>' .
       
    89 					  '<p>' . __( 'Remember to click Update Media to save metadata entered or changed.' ) . '</p>',
       
    90 			)
       
    91 		);
    66 
    92 
    67 	wp_enqueue_script( 'wp-ajax-response' );
    93 		get_current_screen()->set_help_sidebar(
    68 	wp_enqueue_script('image-edit');
    94 			'<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
    69 	wp_enqueue_style('imgareaselect');
    95 			'<p>' . __( '<a href="https://codex.wordpress.org/Media_Add_New_Screen#Edit_Media">Documentation on Edit Media</a>' ) . '</p>' .
       
    96 			'<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
       
    97 		);
    70 
    98 
    71 	get_current_screen()->add_help_tab( array(
    99 		require( ABSPATH . 'wp-admin/admin-header.php' );
    72 		'id'      => 'overview',
       
    73 		'title'   => __('Overview'),
       
    74 		'content' =>
       
    75 			'<p>' . __('This screen allows you to edit five fields for metadata in a file within the media library.') . '</p>' .
       
    76 			'<p>' . __('For images only, you can click on Edit Image under the thumbnail to expand out an inline image editor with icons for cropping, rotating, or flipping the image as well as for undoing and redoing. The boxes on the right give you more options for scaling the image, for cropping it, and for cropping the thumbnail in a different way than you crop the original image. You can click on Help in those boxes to get more information.') . '</p>' .
       
    77 			'<p>' . __('Note that you crop the image by clicking on it (the Crop icon is already selected) and dragging the cropping frame to select the desired part. Then click Save to retain the cropping.') . '</p>' .
       
    78 			'<p>' . __('Remember to click Update Media to save metadata entered or changed.') . '</p>'
       
    79 	) );
       
    80 
   100 
    81 	get_current_screen()->set_help_sidebar(
   101 		$parent_file = 'upload.php';
    82 	'<p><strong>' . __('For more information:') . '</strong></p>' .
   102 		$message     = '';
    83 	'<p>' . __('<a href="https://codex.wordpress.org/Media_Add_New_Screen#Edit_Media">Documentation on Edit Media</a>') . '</p>' .
   103 		$class       = '';
    84 	'<p>' . __('<a href="https://wordpress.org/support/">Support Forums</a>') . '</p>'
   104 		if ( isset( $_GET['message'] ) ) {
    85 	);
   105 			switch ( $_GET['message'] ) {
       
   106 				case 'updated':
       
   107 					$message = __( 'Media file updated.' );
       
   108 					$class   = 'updated';
       
   109 					break;
       
   110 			}
       
   111 		}
       
   112 		if ( $message ) {
       
   113 			echo "<div id='message' class='$class'><p>$message</p></div>\n";
       
   114 		}
    86 
   115 
    87 	require( ABSPATH . 'wp-admin/admin-header.php' );
   116 		?>
    88 
   117 
    89 	$parent_file = 'upload.php';
   118 	<div class="wrap">
    90 	$message = '';
   119 	<h1 class="wp-heading-inline">
    91 	$class = '';
   120 		<?php
    92 	if ( isset($_GET['message']) ) {
   121 		echo esc_html( $title );
    93 		switch ( $_GET['message'] ) {
   122 		?>
    94 			case 'updated' :
   123 </h1>
    95 				$message = __('Media file updated.');
       
    96 				$class = 'updated';
       
    97 				break;
       
    98 		}
       
    99 	}
       
   100 	if ( $message )
       
   101 		echo "<div id='message' class='$class'><p>$message</p></div>\n";
       
   102 
   124 
   103 ?>
   125 		<?php
   104 
   126 		if ( current_user_can( 'upload_files' ) ) {
   105 <div class="wrap">
   127 			?>
   106 <h1 class="wp-heading-inline"><?php
   128 	<a href="media-new.php" class="page-title-action"><?php echo esc_html_x( 'Add New', 'file' ); ?></a>
   107 echo esc_html( $title );
       
   108 ?></h1>
       
   109 
       
   110 <?php
       
   111 if ( current_user_can( 'upload_files' ) ) { ?>
       
   112 	<a href="media-new.php" class="page-title-action"><?php echo esc_html_x('Add New', 'file'); ?></a>
       
   113 <?php } ?>
   129 <?php } ?>
   114 
   130 
   115 <hr class="wp-header-end">
   131 	<hr class="wp-header-end">
   116 
   132 
   117 <form method="post" class="media-upload-form" id="media-single-form">
   133 	<form method="post" class="media-upload-form" id="media-single-form">
   118 <p class="submit" style="padding-bottom: 0;">
   134 	<p class="submit" style="padding-bottom: 0;">
   119 <?php submit_button( __( 'Update Media' ), 'primary', 'save', false ); ?>
   135 		<?php submit_button( __( 'Update Media' ), 'primary', 'save', false ); ?>
   120 </p>
   136 	</p>
   121 
   137 
   122 <div class="media-single">
   138 	<div class="media-single">
   123 <div id="media-item-<?php echo $att_id; ?>" class="media-item">
   139 	<div id="media-item-<?php echo $att_id; ?>" class="media-item">
   124 <?php echo get_media_item( $att_id, array( 'toggle' => false, 'send' => false, 'delete' => false, 'show_title' => false, 'errors' => !empty($errors[$att_id]) ? $errors[$att_id] : null ) ); ?>
   140 		<?php
   125 </div>
   141 		echo get_media_item(
   126 </div>
   142 			$att_id,
       
   143 			array(
       
   144 				'toggle'     => false,
       
   145 				'send'       => false,
       
   146 				'delete'     => false,
       
   147 				'show_title' => false,
       
   148 				'errors'     => ! empty( $errors[ $att_id ] ) ? $errors[ $att_id ] : null,
       
   149 			)
       
   150 		);
       
   151 		?>
       
   152 	</div>
       
   153 	</div>
   127 
   154 
   128 <?php submit_button( __( 'Update Media' ), 'primary', 'save' ); ?>
   155 		<?php submit_button( __( 'Update Media' ), 'primary', 'save' ); ?>
   129 <input type="hidden" name="post_id" id="post_id" value="<?php echo isset($post_id) ? esc_attr($post_id) : ''; ?>" />
   156 	<input type="hidden" name="post_id" id="post_id" value="<?php echo isset( $post_id ) ? esc_attr( $post_id ) : ''; ?>" />
   130 <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr($att_id); ?>" />
   157 	<input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $att_id ); ?>" />
   131 <input type="hidden" name="action" value="editattachment" />
   158 	<input type="hidden" name="action" value="editattachment" />
   132 <?php wp_original_referer_field(true, 'previous'); ?>
   159 		<?php wp_original_referer_field( true, 'previous' ); ?>
   133 <?php wp_nonce_field('media-form'); ?>
   160 		<?php wp_nonce_field( 'media-form' ); ?>
   134 
   161 
   135 </form>
   162 	</form>
   136 
   163 
   137 </div>
   164 	</div>
   138 
   165 
   139 <?php
   166 		<?php
   140 
   167 
   141 	require( ABSPATH . 'wp-admin/admin-footer.php' );
   168 		require( ABSPATH . 'wp-admin/admin-footer.php' );
   142 
   169 
   143 	exit;
   170 		exit;
   144 
   171 
   145 default:
   172 	default:
   146 	wp_redirect( admin_url('upload.php') );
   173 		wp_redirect( admin_url( 'upload.php' ) );
   147 	exit;
   174 		exit;
   148 
   175 
   149 }
   176 }