function wpmc_check_media($attachmentId) { // Is it an image? $meta = wp_get_attachment_metadata($attachmentId); $isImage = isset($meta, $meta['width'], $meta['height']); // Get the main file global $wpdb; $fullpath = get_attached_file($attachmentId); $mainfile = wpmc_clean_uploaded_filename($fullpath); $baseUp = pathinfo($mainfile); $baseUp = $baseUp['dirname']; $size = 0; $countfiles = 0; $issue = 'NO_CONTENT'; if (file_exists($fullpath)) { $size = filesize($fullpath); if (wpmc_check_is_ignore($mainfile) || wpmc_check_in_gallery($mainfile) || wpmc_check_db_has_featured($mainfile) || wpmc_check_db_has_background_or_header($mainfile) || wpmc_check_db_has_meta($mainfile, $attachmentId) || wpmc_check_db_has_content($mainfile)) { return true; } // If images, check the other files as well $countfiles = 0; $sizes = wpmc_get_image_sizes(); if ($isImage && isset($meta['sizes'])) { foreach ($meta['sizes'] as $name => $attr) { if (isset($attr['file'])) { $filepath = wp_upload_dir(); $filepath = $filepath['basedir']; $filepath = trailingslashit($filepath) . trailingslashit($baseUp) . $attr['file']; if (file_exists($filepath)) { $size += filesize($filepath); } $file = wpmc_clean_uploaded_filename($attr['file']); $countfiles++; //error_log("checking MEDIA-IMAGE {$filepath}"); if (wpmc_check_in_gallery($filepath) || wpmc_check_db_has_featured($filepath) || wpmc_check_db_has_background_or_header($filepath) || wpmc_check_db_has_meta($filepath, $attachmentId) || wpmc_check_db_has_content($filepath)) { return true; } } } } } else { $issue = 'ORPHAN_MEDIA'; } $table_name = $wpdb->prefix . "wpmcleaner"; $wpdb->insert($table_name, array('time' => current_time('mysql'), 'type' => 1, 'size' => $size, 'path' => $mainfile . ($countfiles > 0 ? " (+ " . $countfiles . " files)" : ""), 'postId' => $attachmentId, 'issue' => $issue)); return false; }
function wpmc_check_file($path) { global $wpdb; $filepath = wp_upload_dir(); $filepath = $filepath['basedir']; $filepath = trailingslashit($filepath) . stripslashes($path); // Retina support if (strpos($path, '@2x.') !== false) { $originalfile = str_replace('@2x.', '.', $filepath); if (file_exists($originalfile)) { return true; } else { $table_name = $wpdb->prefix . "wpmcleaner"; $wpdb->insert($table_name, array('time' => current_time('mysql'), 'type' => 0, 'path' => $path, 'size' => filesize($filepath), 'issue' => 'ORPHAN_RETINA')); return false; } } $issue = "NONE"; $path_parts = pathinfo($path); if (wpmc_check_is_ignore($path_parts['basename']) || wpmc_check_in_gallery($path_parts['basename']) || wpmc_check_db_has_featured($path_parts['basename']) || wpmc_check_db_has_content($path_parts['basename']) || wpmc_check_db_has_background_or_header($path_parts['basename'])) { return true; } else { $issue = "NO_CONTENT"; } if (wpmc_check_db_has_meta($path_parts['basename'])) { return true; } else { $issue = "NO_MEDIA"; } $table_name = $wpdb->prefix . "wpmcleaner"; $filesize = file_exists($filepath) ? filesize($filepath) : 0; $wpdb->insert($table_name, array('time' => current_time('mysql'), 'type' => 0, 'path' => $path, 'size' => $filesize, 'issue' => $issue)); return false; }