Advertisement
Guest User

Unattach 1.0.3

a guest
Jun 15th, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.31 KB | None | 0 0
  1. <?php
  2. /*************************************************************************
  3. Plugin Name:  Unattach
  4. Plugin URI:   http://outlandishideas.co.uk/blog/2011/03/unattach/
  5. Description:  Allows detaching images and other media from posts, pages and other content types.
  6. Version:      1.0.3
  7. Author:       tamlyn, Ov3rfly (1.0.2 & 1.0.3 changes)
  8. **************************************************************************/
  9.  
  10. //filter to add button to media library UI
  11. function unattach_media_row_action( $actions, $post ) {
  12.     // 1.0.3: only add action if user can edit media and parent
  13.     if ( current_user_can( 'edit_post', $post->ID ) && $post->post_parent && current_user_can( 'edit_post', $post->post_parent ) ) {
  14.         // 1.0.3: options.php as target page
  15.         // 1.0.2: add nonce to fix CSRF
  16.         $url = wp_nonce_url( admin_url('options.php?page=unattach&noheader=true&id=' . $post->ID ), 'unattach' );
  17.         // 1.0.3: add current page and searchterm as param
  18.         if ( !empty( $_REQUEST['paged'] ) ) {
  19.             $pagenum = (int)$_REQUEST['paged'];
  20.             if ( $pagenum > 1 ) {
  21.                 $url = add_query_arg( 'paged', $pagenum, $url );
  22.             }
  23.         }
  24.         if ( !empty( $_REQUEST['s'] ) ) {
  25.             $url = add_query_arg( 's', $_REQUEST['s'], $url );
  26.         }
  27.         // 1.0.2: localized strings
  28.         $actions['unattach'] = '<a href="' . esc_url( $url ) . '" title="' . __( 'Unattach this media item.', 'unattach' ) . '">' . __( 'Unattach', 'unattach' ) . '</a>';
  29.     }
  30.  
  31.     return $actions;
  32. }
  33.  
  34. //action to set post_parent to 0 on attachment
  35. function unattach_do_it() {
  36.     // 1.0.2: check nonce to fix CSRF
  37.     check_admin_referer( 'unattach' );
  38.  
  39.     global $wpdb;
  40.     if ( !empty($_REQUEST['id'] ) ) {
  41.         // 1.0.2: cast id to int
  42.         $id = (int)$_REQUEST['id'];
  43.         // 1.0.3: check if user can edit media
  44.         if ( current_user_can( 'edit_post', $id ) ) {
  45.             $wpdb->update($wpdb->posts, array('post_parent'=>0), array('id'=>$id, 'post_type'=>'attachment'));
  46.         }
  47.     }
  48.  
  49.     $url = 'upload.php';
  50.     // 1.0.3: add current page and searchterm as param
  51.     if ( !empty( $_REQUEST['paged'] ) ) {
  52.         $pagenum = (int)$_REQUEST['paged'];
  53.         if ( $pagenum > 1 ) {
  54.             $url = add_query_arg( 'paged', $pagenum, $url );
  55.         }
  56.     }
  57.     if ( !empty( $_REQUEST['s'] ) ) {
  58.         $url = add_query_arg( 's', $_REQUEST['s'], $url );
  59.     }
  60.  
  61.     wp_redirect( admin_url( $url ) );
  62.     exit;
  63. }
  64.  
  65. //set it up
  66. add_action( 'admin_menu', 'unattach_init' );
  67. function unattach_init() {
  68.     // 1.0.2: add filter for capability
  69.     $capability = apply_filters( 'unattach_capability', 'upload_files' );
  70.     if ( current_user_can( $capability ) ) {
  71.         add_filter('media_row_actions',  'unattach_media_row_action', 10, 2);
  72.         // 1.0.3: use null instead tools.php
  73.         add_submenu_page( null, 'Unattach', 'Unattach', $capability, 'unattach', 'unattach_do_it' );
  74.     }
  75. }
  76.  
  77. /*
  78. // example to restrict access via capability in functions.php
  79. // see also http://codex.wordpress.org/Roles_and_Capabilities
  80. function my_unattach_capability( $capability ) {
  81.     return 'administrator';
  82. }
  83. add_filter( 'unattach_capability', 'my_unattach_capability' );
  84. */
  85.  
  86. // 1.0.2: add translation possibility
  87. function unattach_load_textdomain() {
  88.     load_plugin_textdomain( 'unattach', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
  89. }
  90. add_action( 'plugins_loaded', 'unattach_load_textdomain' );
  91.  
  92. // 1.0.3 complete package with language files: http://www.wikisend.com/download/647230/unattach.zip
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement