Enables the supervision of your attachement, detects duplicates, detects unused files.
You may also create a list of all attached file in the page or in the child pages by using the following shorcode [attach child=1 only_admin=1 title='Title you want' extension='pdf,doc,png'].
wp_enqueue_script( 'jsapi', 'https://www.google.com/jsapi'); or
* wp_enqueue_script('attachments_handler_script', plugins_url('/script.js', __FILE__));
* $this->add_inline_js($js_text);
* $this->add_js($js_url_file);
*
* @return void
*/
function _public_js_load() {
ob_start() ;
?>
function checkIfAttachmentsHandlerNeeded() {
var arguments = {
action: 'checkIfAttachmentsHandlerNeeded'
}
var ajaxurl2 = "" ;
jQuery.post(ajaxurl2, arguments, function(response) {
// We do nothing as the process should be as silent as possible
});
}
// We launch the callback
if (window.attachEvent) {window.attachEvent('onload', checkIfAttachmentsHandlerNeeded);}
else if (window.addEventListener) {window.addEventListener('load', checkIfAttachmentsHandlerNeeded, false);}
else {document.addEventListener('load', checkIfAttachmentsHandlerNeeded, false);}
add_inline_js($java) ;
}
/** ====================================================================================================================================================
* Init css for the public side
* If you want to load a style sheet, please type :
* $this->add_inline_css($css_text);
* $this->add_css($css_url_file);
*
* @return void
*/
function _public_css_load() {
$css = $this->get_param('css') ;
$this->add_inline_css($css) ;
}
/** ====================================================================================================================================================
* Init javascript for the admin side
* If you want to load a script, please type :
* wp_enqueue_script( 'jsapi', 'https://www.google.com/jsapi'); or
* wp_enqueue_script('attachments_handler_script', plugins_url('/script.js', __FILE__));
* $this->add_inline_js($js_text);
* $this->add_js($js_url_file);
*
* @return void
*/
function _admin_js_load() {
}
/** ====================================================================================================================================================
* Init css for the admin side
* If you want to load a style sheet, please type :
* $this->add_inline_css($css_text);
* $this->add_css($css_url_file);
*
* @return void
*/
function _admin_css_load() {
}
/** ====================================================================================================================================================
* Called when the content is displayed
*
* @param string $content the content which will be displayed
* @param string $type the type of the article (e.g. post, page, custom_type1, etc.)
* @param boolean $excerpt if the display is performed during the loop
* @return string the new content
*/
function _modify_content($content, $type, $excerpt) {
return $content;
}
/** ====================================================================================================================================================
* Add a button in the TinyMCE Editor
*
* To add a new button, copy the commented lines a plurality of times (and uncomment them)
*
* @return array of buttons
*/
function add_tinymce_buttons() {
$buttons = array() ;
$buttons[] = array(__('Attachments Handler', $this->pluginID), '[attach child=1 only_admin=1 title="Title you want" extension="pdf,doc,png"]', '', plugin_dir_url("/").'/'.str_replace(basename( __FILE__),"",plugin_basename( __FILE__)).'img/attach_button.png') ;
return $buttons ;
}
/**====================================================================================================================================================
* Function to instantiate the class and make it a singleton
* This function is not supposed to be modified or called (the only call is declared at the end of this file)
*
* @return void
*/
public static function getInstance() {
if ( !self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/** ====================================================================================================================================================
* Define the default option values of the plugin
* This function is called when the $this->get_param function do not find any value fo the given option
* Please note that the default return value will define the type of input form: if the default return value is a:
* - string, the input form will be an input text
* - integer, the input form will be an input text accepting only integer
* - string beggining with a '*', the input form will be a textarea
* - boolean, the input form will be a checkbox
*
* @param string $option the name of the option
* @return variant of the option
*/
public function get_default_option($option) {
switch ($option) {
// Alternative default return values (Please modify)
case 'type_page' : return "page,post" ; break ;
case 'list_post_id_to_check': return array() ; break ;
case 'nb_post_to_check' : return 0 ; break ;
case 'list_img_id_to_regenerate': return array() ; break ;
case 'nb_img_to_regenerate' : return 0 ; break ;
case 'info_img_to_regenerate' : return "" ; break ;
case 'max_page_to_check' : return 200 ; break ;
case 'titre' : return "List of all attached files" ; break ;
case 'html' : return "*%link%
%description%
" ; break ; case 'css' : return "*div.attach_div{ border: 1px solid #AAAAAA; padding: 5px; padding-left: 20px; padding-right: 20px; padding-bottom: 10px; min-width:100px; } div.attach_div h2 { text-align: center; font-size: 13px; line-height: 15px ; font-weight: bold; margin : 3px ; padding-top : 5px ; padding-bottom : 5px ; } div.attach_list p{ margin : 2px ; padding : 2px ; font-size: 11px; line-height: 11px ; } div.attach_list p.description{ padding-left:30px ; }" ; break ; case 'last_request' : return 0 ; break ; case 'between_two_requests' : return 5 ; break ; } return null ; } /** ==================================================================================================================================================== * The admin configuration page * This function will be called when you select the plugin in the admin backend * * @return void */ public function configuration_page() { global $wpdb; global $blog_id ; $this->set_param('info_img_to_regenerate', "") ; SLFramework_Debug::log(get_class(), "Print the configuration page." , 4) ; ?>" ; echo "" ; echo "pluginID)."' />" ; echo "" ; echo " pluginID)."' />" ; echo "" ; echo " pluginID)."' />" ; echo "
" ; $tabs->add_tab(__('Attachments issues', $this->pluginID), ob_get_clean()) ; ob_start() ; echo "".$tit_pos." (".__('Edit', $this->pluginID).")
" ; } else { $post_used .= "" ; } } } if ($post_used == "") { $post_used = "".__("(Not used)", $this->pluginID)."
" ; } if ($r->id==0) { $cel1 = new adminCell("") ; } else { if (get_edit_post_link($r->id)!="") { $cel1 = new adminCell("url)."\">".$r->titre." (id))."\">".__('Edit', $this->pluginID).")
") ; } else { $cel1 = new adminCell("") ; } } $cel2 = new adminCell("".$r->description."
") ; $cel3 = new adminCell($post_used) ; $table->add_line(array($cel1, $cel2, $cel3), $ligne) ; } echo $table->flush() ; $tabs->add_tab(__('All links', $this->pluginID), ob_get_clean()) ; ob_start() ; $maxnb = 20 ; $table = new SLFramework_Table(0, $maxnb, true, true) ; $table->title(array(__('Title of the image', $this->pluginID),__('Image', $this->pluginID),__('Metadata', $this->pluginID), __('Image in...', $this->pluginID))) ; // We order the posts page according to the choice of the user $order = " ORDER BY " ; if ($table->current_ordercolumn()==1) { $order .= "description" ; } else { $order .= "titre" ; } if ($table->current_orderdir()=="DESC") { $order .= " DESC" ; } else { $order .= " ASC" ; } $limit_to_img = "(url LIKE '%.png' OR url LIKE '%.jpg' OR url LIKE '%.bmp' OR url LIKE '%.gif')" ; $nb = $wpdb->get_var("SELECT COUNT(*) FROM ".$this->table_name." WHERE ".$limit_to_img." AND (titre like '%".str_replace("'","",$table->current_filter())."%' OR description like '%".str_replace("'","",$table->current_filter())."%')".$order) ; $table->set_nb_all_Items($nb) ; $limit = "" ; if ($nb>$maxnb) { $limit = " LIMIT ".(($table->current_page()-1)*$maxnb).",".$maxnb ; } $results = $wpdb->get_results("SELECT * FROM ".$this->table_name." WHERE ".$limit_to_img." AND (titre like '%".str_replace("'","",$table->current_filter())."%' )".$order.$limit) ; $ligne=0 ; foreach ($results as $r) { $ligne++ ; if ($r->titre=="") { $r->titre = $r->url ; } $all_id = explode(',',$r->attach_used_in) ; $post_used = "" ; foreach ($all_id as $ai) { if ($ai!="") { $tit_pos = get_the_title($ai); if ($tit_pos=="") { $tit_pos = __("No title", $this->pluginID) ; } if (get_edit_post_link($ai)!="") { $post_used .= "".$tit_pos." (".__('Edit', $this->pluginID).")
" ; } else { $post_used .= "" ; } } } if ($post_used == "") { $post_used = "".__("(Not used)", $this->pluginID)."
" ; } if ($r->id==0) { $cel1 = new adminCell("") ; } else { if (get_edit_post_link($r->id)!="") { $cel1 = new adminCell("url)."\">".$r->titre." (id))."\">".__('Edit', $this->pluginID).")
") ; } else { $cel1 = new adminCell("") ; } } $cel2 = new adminCell("".wp_get_attachment_image( $r->id, "thumbnail")."
") ; $meta = wp_get_attachment_metadata($r->id) ; $meta_toprint = "" ; if (($meta!==false)&&(isset($meta['width']))) { $meta_toprint .= "".sprintf(__("Dimension: %s", $this->pluginID), $meta['width']."x".$meta['height'])."
" ; $meta_toprint .= "".__("Sizes:", $this->pluginID)."
" ; foreach ($meta['sizes'] as $nms => $ms) { $meta_toprint .= "".$nms.": ".$ms['width']."x".$ms['height']."
" ; } $meta_toprint .= "".__("Meta data:", $this->pluginID)."
" ; foreach ($meta['image_meta'] as $nim => $im) { if (($im!="")&&($im!=0)) { if ($nim=="created_timestamp") { $meta_toprint .= "".$nim.": ".date_i18n( get_option( 'date_format' ), $im)."
" ; } else { $meta_toprint .= "".$nim.": ".$im."
" ; } } } } $cel3 = new adminCell($meta_toprint) ; $cel4 = new adminCell($post_used) ; $table->add_line(array($cel1, $cel2, $cel3, $cel4), $ligne) ; } echo $table->flush() ; echo "" ; echo "" ; echo "" ; echo "pluginID)."' />" ; echo "" ; echo " pluginID)."' />" ; echo "" ; echo "
" ; $tabs->add_tab(__('Images', $this->pluginID), ob_get_clean()) ; ob_start() ; $params = new SLFramework_Parameters($this, "tab-parameters") ; $params->add_title(__('Analysis of', $this->pluginID)) ; $params->add_param('type_page', __('Type of page to be analysed:', $this->pluginID)) ; $params->add_title(__('Appearence', $this->pluginID)) ; $params->add_param('titre', __('Default title:', $this->pluginID)) ; $params->add_param('html', __('The HTML displayed for the shortcode:', $this->pluginID)) ; $params->add_comment(__("The default value is:", $this->pluginID)) ; $params->add_comment_default_value('html') ; $params->add_param('html_entry', __('The HTML displayed for each entry of the list:', $this->pluginID)) ; $params->add_comment(__("The default value is:", $this->pluginID)) ; $params->add_comment_default_value('html_entry') ; $params->add_param('css', __('The CSS used for the shortcode:', $this->pluginID)) ; $params->add_comment(__("The default value is:", $this->pluginID)) ; $params->add_comment_default_value('css') ; $params->add_title(__('Advanced', $this->pluginID)) ; $params->add_param('max_page_to_check', __('Max number of post to be checked when an analysis is forced:', $this->pluginID)) ; $params->add_param('between_two_requests', __('Number of minutes between two background check:', $this->pluginID)) ; $params->flush() ; $tabs->add_tab(__('Parameters', $this->pluginID), ob_get_clean() , plugin_dir_url("/").'/'.str_replace(basename(__FILE__),"",plugin_basename(__FILE__))."core/img/tab_param.png") ; // HOW To ob_start() ; echo "".__("There is two different ways to analyze the attachments:", $this->pluginID)."
" ; echo "".__("an automatic process (namely background process):", $this->pluginID)."
".__("Every time a user visits a page of the frontside of your website, a verification of the attachments related to this page is performed;", $this->pluginID)."
".__("In order to limit the load of the website, you may configure in the parameter tab the minimum number of minutes bewteen to background check;", $this->pluginID)."
".__("Note that if you have very few visits, the complete check of all attachments may be quite long.", $this->pluginID)."
".__("a forced process:", $this->pluginID)."
".__("The button that triggers this forced process may be found in the Attachments issues tabs;", $this->pluginID)."
".__("You have to stay on that page for processing all attachments: if you go on another page (or if you reload the page), the process will be stopped.", $this->pluginID)."
".__("Some attachments may be listed in the Attachments issues tab but you are sure that the attachments are used somewhere.", $this->pluginID)."
" ; echo "".__("This could happen if the attachments are not used in a page or a post but, for instance, in your theme or in a plugin.", $this->pluginID)."
" ; echo "".__("If so, just ignore the warning with the appropriate link.", $this->pluginID)."
" ; $howto2 = new SLFramework_Box (__("False positive?", $this->pluginID), ob_get_clean()) ; ob_start() ; echo "".__("This plugin enables the supervision of your attachements.", $this->pluginID)."
" ; echo "".__("You may supervize your attachements, detect duplicates files with different names, detect unused files...", $this->pluginID)."
" ; $howto3 = new SLFramework_Box (__("Purpose of that plugin", $this->pluginID), ob_get_clean()) ; ob_start() ; echo "".sprintf(__("To add the list of used attachments in the post/page or in the child posts/pages, you may used a shortcode like %s.", $this->pluginID), "[attach child=1 only_admin=1 title='Title you want' extension='pdf,doc,png']")."
".__("A button that add this code is available in the post/page editor.", $this->pluginID)."
" ; echo "".sprintf(__("%s: set this option to 1 if you want to display all attachments used in this page and in all child pages, otherwise set this option to 0.", $this->pluginID), "child")."
".sprintf(__("%s: set this option to 1 if you want to display the list only for the admin (this can be used for configuring the plugin without any impact on the users).", $this->pluginID), "only_admin")."
".sprintf(__("%s: list the attachments extension you want to list.", $this->pluginID), "extension")."
".sprintf(__('%s posts/articles have been analysed while there is %s posts/articles to be analysed (%s links found).', $this->pluginID), "".$verified."", "".$total."" , "".$nb_links."" )."
" ; echo "".__('If all posts/articles have not been analysed, the results cannot be displayed ... Thus, wait or force the verification!', $this->pluginID)."
" ; } else { if ($ignored!=1) { echo "".sprintf(__('Each of the %s posts/articles has been analysed (%s links found).', $this->pluginID), "".$total."" , "".$nb_links."" )."
" ; } // DETECT MISSING FILES ON HARD DISK //---------------------------------------- echo "".__('The following files have been deleted on the hard disk but is still registered by Wordpress.', $this->pluginID)."
" ; echo "".__('You have to modify the post/page and thus to remove this file from the media manager.', $this->pluginID)."
" ; } $table = new SLFramework_Table() ; $table->title(array(__('Title of the file', $this->pluginID), __('This file is used in', $this->pluginID))) ; $ligne = 0 ; foreach ($res as $r) { $ligne++ ; if ($r->titre=="") { $r->titre = $r->url ; } $all_id = explode(',',$r->attach_used_in) ; $post_used = "" ; foreach ($all_id as $ai) { if ($ai!="") { $tit_pos = get_the_title($ai); if ($tit_pos=="") { $tit_pos = __("No title", $this->pluginID) ; } if (get_edit_post_link($ai)!="") { $post_used .= "".$tit_pos." (".__('Edit', $this->pluginID).")
" ; } else { $post_used .= "" ; } } } if ($post_used == "") { $post_used = "".__("(Not used)", $this->pluginID)."
" ; } if ($r->id!=0){ if (get_edit_post_link($r->id)!="") { $cel1 = new adminCell("url)."\">".$r->titre." (id))."\">".__('Edit', $this->pluginID).")
") ; } else { $cel1 = new adminCell("") ; } } else { $cel1 = new adminCell("") ; } $cel2 = new adminCell($post_used) ; if ($ignored!=1) { $cel1->add_action(__("Ignore", $this->pluginID), "ignoreAttachmentIssue('".$r->id_media."','".$r->sha1."', '".addslashes(__("Do you want to ignore this entry?", $this->pluginID))."')") ; } else { $cel1->add_action(__("Do not Ignore", $this->pluginID), "doNotignoreAttachmentIssue('".$r->id_media."', '".$r->sha1."', '".addslashes(__("Do you want not to ignore this entry?", $this->pluginID))."')") ; } $table->add_line(array($cel1, $cel2), $r->id_media) ; } echo $table->flush() ; } else { if ($ignored!=1) { echo "".__('No missing files.', $this->pluginID)."
" ; } else { echo "".__('No missing files ignored.', $this->pluginID)."
" ; } } // DETECT DUPLICATE FILES // ------------------------------------- echo "".__('The following files are duplicated on the hard disk.', $this->pluginID)."
" ; echo "".__('You have to modify the post/page and thus to remove the extras files from the media manager.', $this->pluginID)."
" ; } $ligne = 0 ; $old_sha1 = "" ; $nb_entr = 0 ; foreach ($res as $r) { if ($r->sha1!=$old_sha1) { if ($nb_entr!=0) { echo $table->flush() ; echo "" ; } $table = new SLFramework_Table() ; $table->title(array(__('Title of the file', $this->pluginID), __('This file is used in', $this->pluginID))) ; $nb_entr = 0 ; $old_sha1 = $r->sha1 ; } $nb_entr ++ ; $ligne++ ; if ($r->titre=="") { $r->titre = $r->url ; } $all_id = explode(',',$r->attach_used_in) ; $post_used = "" ; foreach ($all_id as $ai) { if ($ai!="") { $tit_pos = get_the_title($ai); if ($tit_pos=="") { $tit_pos = __("No title", $this->pluginID) ; } if (get_edit_post_link($ai)!="") { $post_used .= "
".$tit_pos." (".__('Edit', $this->pluginID).")
" ; } else { $post_used .= "" ; } } } if ($post_used == "") { $post_used = "".__("(Not used)", $this->pluginID)."
" ; } if ($r->id!=0){ if (get_edit_post_link($r->id)!="") { $cel1 = new adminCell("url)."\">".$r->titre." (id))."\">".__('Edit', $this->pluginID).")
") ; } else { $cel1 = new adminCell("") ; } } else { $cel1 = new adminCell("") ; } $cel2 = new adminCell($post_used) ; if ($ignored!=1) { $cel1->add_action(__("Ignore", $this->pluginID), "ignoreAttachmentIssue('".$r->id_media."','".$r->sha1."', '".addslashes(__("Do you want to ignore this entry?", $this->pluginID))."')") ; } else { $cel1->add_action(__("Do not Ignore", $this->pluginID), "doNotignoreAttachmentIssue('".$r->id_media."', '".$r->sha1."', '".addslashes(__("Do you want not to ignore this entry?", $this->pluginID))."')") ; } $table->add_line(array($cel1, $cel2), $r->id_media) ; } if ($nb_entr!=0) { echo $table->flush() ; } } else { if ($ignored!=1) { echo "".__('No duplicate files.', $this->pluginID)."
" ; } else { echo "".__('No duplicate files ignored.', $this->pluginID)."
" ; } } // DETECT FILES THAT ARE NOT USED IN ANY PAGES //------------------------------------------------------- echo "".__('The following files exists on the disk but does not seems to be used ...', $this->pluginID)."
" ; echo "".__('You may remove them from the media manager.', $this->pluginID)."
" ; } $table = new SLFramework_Table() ; $table->title(array(__('Title of the file', $this->pluginID))) ; $ligne = 0 ; foreach ($res as $r) { $ligne++ ; if ($r->titre=="") { $r->titre = $r->url ; } if (get_edit_post_link($r->id)!="") { $cel1 = new adminCell("url)."\">".$r->titre." (id))."\">".__('Edit', $this->pluginID).")
") ; } else { $cel1 = new adminCell("") ; } if ($ignored!=1) { $cel1->add_action(__("Ignore", $this->pluginID), "ignoreAttachmentIssue('".$r->id_media."','".$r->sha1."', '".addslashes(__("Do you want to ignore this entry?", $this->pluginID))."')") ; } else { $cel1->add_action(__("Do not Ignore", $this->pluginID), "doNotignoreAttachmentIssue('".$r->id_media."', '".$r->sha1."', '".addslashes(__("Do you want not to ignore this entry?", $this->pluginID))."')") ; } $table->add_line(array($cel1), $r->id_media) ; } echo $table->flush() ; } else { if ($ignored!=1) { echo "".__('No unused files.', $this->pluginID)."
" ; } else { echo "".__('No unused files ignored.', $this->pluginID)."
" ; } } // DETECT FILES THAT ARE ON THE DISK BUT NOT HANDLED BY THE MEDIA MANAGER } } /** ==================================================================================================================================================== * Ajax Callback to force attachment anaysis * @return void */ function forceAnalysisAttachments() { global $post, $wpdb ; // Initialize the list $at = $this->get_param('list_post_id_to_check') ; if (empty($at)) { // We get the post $args = array( 'posts_per_page' => intval($this->get_param('max_page_to_check')), 'orderby' => 'rand', 'post_type' => explode(",",$this->get_param('type_page')), 'fields' => 'ids', 'post_status' => 'publish' ); $myQuery = new WP_Query( $args ); //Looping through the posts $post_temp = array() ; while ( $myQuery->have_posts() ) { $myQuery->the_post(); $post_temp[] = $post; } // Reset Post Data wp_reset_postdata(); $this->set_param('list_post_id_to_check', $post_temp) ; $this->set_param('nb_post_to_check', count($post_temp)) ; } // Get the first post of the list $post_temp = $this->get_param('list_post_id_to_check') ; $pid = array_pop($post_temp) ; $this->set_param('list_post_id_to_check', $post_temp) ; $this->check_post_for_attachments($pid,false) ; $this->displayTable() ; $at = $this->get_param('list_post_id_to_check') ; if (empty($at)) { $this->set_param('nb_post_to_check', 0) ; } else { $pc = floor(100*($this->get_param('nb_post_to_check')-count($this->get_param('list_post_id_to_check')))/$this->get_param('nb_post_to_check')) ; $pb = new SLFramework_Progressbar(500, 20, $pc, "PROGRESS - ".($this->get_param('nb_post_to_check')-count($this->get_param('list_post_id_to_check')))." / ".$this->get_param('nb_post_to_check')) ; echo "".esc_html( $fullsizepath )."","".esc_html( $iid )."")."".esc_html( $fullsizepath )."","".esc_html( $metadata->get_error_message() )."")."".esc_html( $fullsizepath )."")."".esc_html( $fullsizepath )."")."