Skip to content

Instantly share code, notes, and snippets.

@Benoti
Created December 17, 2014 13:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Benoti/e188dfe56d4bf4d48bac to your computer and use it in GitHub Desktop.
Save Benoti/e188dfe56d4bf4d48bac to your computer and use it in GitHub Desktop.
Brozzme Squeeze Atts
<?php
/**
* Plugin Name: Brozzme SQUEEZE Atts
* Plugin URL: http://brozzme.com/squeeze-atts/
* Description: Remove post and its attachments
* Version: 0.1
* Author: Benoît Faure
* Author URI: http://brozzme.com
*/
defined( 'ABSPATH' ) OR exit;
ini_set('display_errors', 'off');
// plugin activation
function brozzme_bsa_Setup_Demo_on_activation()
{
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
}
function brozzme_bsa_Setup_Demo_on_deactivation()
{
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "deactivate-plugin_{$plugin}" );
}
function brozzme_bsa_Setup_Demo_on_uninstall()
{
if ( ! current_user_can( 'activate_plugins' ) )
return;
check_admin_referer( 'bulk-plugins' );
if ( __FILE__ != WP_UNINSTALL_PLUGIN )
return;
}
register_activation_hook( __FILE__, 'brozzme_bsa_Setup_Demo_on_activation' );
register_deactivation_hook( __FILE__, 'brozzme_bsa_Setup_Demo_on_deactivation' );
register_uninstall_hook( __FILE__, 'brozzme_bsa_Setup_Demo_on_uninstall' );
// add plugin menu to WordPress administration
add_action( 'admin_menu', 'brozzme_bsa_add_admin_menu' );
function brozzme_bsa_add_admin_menu( ) {
add_menu_page( 'Brozzme Squeeze', 'Squeeze Atts', 'manage_options','squeeze-att-admin', 'brozzme_bsa_welcome_page', 'dashicons-trash');
add_submenu_page('squeeze-att-admin', 'Squeezer', 'Squezzer', 'manage_options', 'squeeze-att-dashboard', 'brozzme_bsa_dashboard_page');
// add_submenu_page('rapid-pub-admin', 'Bloc d\affichage prédéfinis', 'Area presets', 'manage_options', 'rapid-pub-dashboard', 'brozzme_rapid_pub_welcome_page');
//add_options_page('Settings Rapid Pub', 'Rapid Pub Settings', 'manage_options', 'rapid-pub-settings', 'brozzme_rapid_pub_options_page');
}
function brozzme_bsa_welcome_page(){
echo '<h1>SQUEEZE Atts</h1>';
echo '<p><b>SQUEEZE Atts</b> allow you to erase a post and its attachments.<br />
You just have to type the id of the post you want to scan and press "scan" or press Squeezer in the post admin columns.</p>';
echo '<h3>Be carefull when using SQUEEZE Atts</h3>';
echo '<p><b>SQUEEZE Atts</b> doesn\'t check for posts states. When you choose a post, <b>SQUEEZE Atts</b> will not tell you if the post is published, drafted...</p>';
echo '<p><b>SQUEEZE Atts</b> is not responsible for your data and their loss.<br />';
echo 'This software has been designed with a backward compatibility with WordPress 3.3 and it can be run with WordPress 4.0.1 too.</p>';
echo '<p><b>SQUEEZE Atts</b> is a plugin created by Benoît Faure <a href="http://brozzme.com">brozzme.com</a>. Go to <a href="http://brozzme.com/" target=""_blank">Brozzme</a> to search other cool and efficients plugins.</p>';
}
function brozzme_bsa_dashboard_page(){
echo '<h3>SQUEEZE Atts dashboard</h3>';
echo date('d/m/Y H:i:s'). ' | Attachments and old posts to SQUEEZE ?';
echo '<br/>';
echo '<div class="update-nag" style="padding-bottom: 25px;"><h2>This operation can not be undone, use it carefully !</h2></div>';
echo '<br/><br />';
echo '<b>Type the post ID to scan</b><br />';
echo '<form action="admin.php" method="get">
<input type="hidden" name="page" value="squeeze-att-dashboard" />
<input type="text" name="bsa_id" value=""/>
<input name="submit" type="submit" value="scan" class="button button-primary button-large">
</form>';
echo '<br/>';
$version = get_bloginfo('version');
if(isset($_GET['bsa_id'])!=''){
// backward compatibility for get_attached_media function who was introduce in WordPress 3.6
if ($version < 3.6) {
$post_details = get_children( array( 'post_parent' => $_GET['bsa_id'] ) );
} else {
$post_details = get_attached_media( 'image', $_GET['bsa_id'] );
}
$attachments = get_children( array( 'post_parent' => $_GET['bsa_id'] ) );
$count = count( $attachments );
if($count == '1'){
$atts_display = 'attachment';
}
else{
$atts_display='attachments';
}
echo '<h3>'. $count . ' ' .$atts_display. ' for this post</h3>';
foreach($post_details as $attachment){
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail', 'false' );
$image_metadata = wp_get_attachment_metadata( $attachment->ID, 'false' );
if( $image_attributes ) {
echo '<div style ="padding:15px;border-top:1px dashed #ccc;font-size:16px;margin-bottom:15px;">
<div style="float:left;padding-right:25px;"><img src="'.$image_attributes[0].'" width="'.$image_attributes[1].'" height="'.$image_attributes[2].'" class="size-thumbnail"/></div>
<div style="float:left;vertical-align:top line-height:24px;">
'.$attachment->ID . ' / <a href="' .$attachment->guid . '" target="_blank">Open attachment in a new tab</a><br / >
File : '.$image_metadata['file'].'<br />
</div>
<div style="clear:left;padding-bottom:15px;"></div>
</div>';
}
}
echo '<div style="padding-top:20px;"><form action="admin.php" method="get">
<input type="hidden" name="page" value="squeeze-att-dashboard" />
<input type="hidden" name="bsa_postid" value="'.$_GET['bsa_id'].'"/>
<input type="hidden" name="bsa_auth" value="true"/>
<h3 style="padding-bottom:10px;">Press if you want to erase ' .$atts_display.'</h3> <input name="submit" type="submit" value="Erase ' .$atts_display.'" class="button button-primary button-large">
</form></div>';
// var_dump($post_details);
}
if(isset($_GET['bsa_postid'])!='' && isset($_GET['bsa_auth'])== 'true' ){
if ($version < 3.6) {
$post_details = get_children( array( 'post_parent' => $_GET['bsa_postid'] ) );
} else {
$post_details = get_attached_media( 'image', $_GET['bsa_postid'] );
}
foreach($post_details as $attachment){
wp_delete_attachment( $attachment->ID );
}
echo '<h3>Do you want to erase this post ?</h3>';
echo '<form action="admin.php" method="get">
<input type="hidden" name="page" value="squeeze-att-dashboard" />
<input type="hidden" name="bsa_gbpostid" value="'.$_GET['bsa_postid'].'"/>
<input type="hidden" name="bsa_auth_post" value="true"/>
<b>Press if you want to squeeze this post</b> ('.$_GET['bsa_postid'].') <input name="submit" type="submit" class="button button-primary button-large" value="Delete post">
</form>';
}
if(isset($_GET['bsa_gbpostid'])!='' && isset( $_GET['bsa_auth_post'])== 'true'){
// deleting post datas
$deleting_process = wp_delete_post($_GET['bsa_gbpostid'], true);
if($deleting_process == true){
echo '<div class="updated">OK, post squeezed...</div>';
}
}
}
add_filter('manage_posts_columns', 'posts_columns_attachment_count', 5);
add_action('manage_posts_custom_column', 'posts_custom_columns_attachment_count', 5, 2);
function posts_columns_attachment_count($defaults){
$defaults['wps_post_attachments'] = __('Att');
$defaults['wps_squeeze_attachments'] = __('Squeeze');
$defaults['wps_squeeze_thumb'] = __('Thumbnail');
return $defaults;
}
function posts_custom_columns_attachment_count($column_name, $id){
if($column_name === 'wps_post_attachments'){
$attachments = get_children(array('post_parent'=>$id));
$count = count($attachments);
if($count !=0){echo $count;}
}
if($column_name === 'wps_squeeze_attachments'){
$attachments = get_children(array('post_parent'=>$id));
$count = count($attachments);
if($count !=0){echo '<a href="admin.php?page=squeeze-att-dashboard&bsa_id='.$id.'&submit=scan" target="_blank">Squeeze atts</a>';}
}
if($column_name === 'wps_squeeze_thumb'){
$img_id = get_the_post_thumbnail( $id, 'thumbnail' );
echo '<a href="post.php?post='.$id.'&action=edit">'.$img_id.'</a>';
}
}
=== Squeeze Atts Plugin ===
Contributors: Benoît Faure
Tags: management, image, media, admin
Donate link: http://brozzme.com/
Requires at least: 4.0
Tested up to: 4.0
Stable tag: 0.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Delete medias attached to posts or pages.
== Description ==
Squeeze Atts allow you to delete medias attached to a post or a page.
Choose the post you want to scan, Squeeze Atts will list all of them. Press Erase and these medias will be delete of the ftp server (all image size embedded in functions.php will be treat) and in the media gallery. You can optionnally, delete the main post too.
Featured list:
1. Add number of attachment for all post and pages in WordPress admin columns (only display if exists).
2. Add link in admin to squeeze attachments.
3. Delete all media in ftp server as long as the image size is register in the theme.
4. Possibility to open medias in a new tab.
== Installation ==
1. Upload Squeeze Atts by Brozzme in the plugin folder.
2. Activate the plugin through the plugin panel menu in WordPress.
== Changelog ==
= 0.1 =
* Initial release.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment