Last active
November 29, 2024 05:02
-
-
Save jordymeow/8e980328fc80c70d262ebae40fbd9a48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Adding action hooks for Media Cleaner plugin | |
add_action( 'wpmc_scan_once', 'wpmc_scan_once_PLUGIN_NAME', 10, 0 ); | |
add_action( 'wpmc_scan_post', 'wpmc_scan_html_PLUGIN_NAME', 10, 2 ); | |
add_action( 'wpmc_scan_postmeta', 'wpmc_scan_postmeta_PLUGIN_NAME', 10, 2 ); | |
/** | |
* Runs once at the beginning of the scan. | |
* Can be used to check images usage in general settings, in a theme, like a favicon, etc. | |
*/ | |
function wpmc_scan_once_PLUGIN_NAME() { | |
global $wpmc; | |
// Implement your logic here | |
} | |
/** | |
* Runs for each postmeta of any post type. | |
* Scans and collects image IDs and URLs from post meta. | |
* | |
* @param int $id The post ID. | |
*/ | |
function wpmc_scan_postmeta_PLUGIN_NAME( $id ) { | |
global $wpmc; | |
$postmeta_images_ids = array(); | |
$postmeta_images_urls = array(); | |
// Fetch data from post meta with key '_fusion' | |
$data = get_post_meta( $id, '_fusion' ); | |
$attributes = [ 'id', 'url', 'thumbnail' ]; | |
// Get images from post meta data | |
$wpmc->get_from_meta( $data, $attributes, $postmeta_images_ids, $postmeta_images_urls ); | |
// Add image references to the Media Cleaner | |
$wpmc->add_reference_id( $postmeta_images_ids, 'PLUGIN_NAME (ID)', $id ); | |
$wpmc->add_reference_url( $postmeta_images_urls, 'PLUGIN_NAME (URL)', $id ); | |
} | |
/** | |
* Runs for each post of any post type. | |
* Scans and collects image URLs from the post content (HTML). | |
* | |
* @param string $html The post content HTML. | |
* @param int $id The post ID. | |
*/ | |
function wpmc_scan_html_PLUGIN_NAME( $html, $id ) { | |
global $wpmc; | |
$posts_images_urls = array(); | |
$galleries_images = array(); | |
// Example patterns to find image URLs in the post content | |
// Uncomment and modify these patterns based on your needs | |
// Images between brackets | |
// preg_match_all( "/\]((https?:\/\/)?[^\\&\#\[\] \"\?]+\.(" . $wpmc->types . "))\[\//", $html, $res ); | |
// if ( !empty( $res ) && isset( $res[1] ) && count( $res[1] ) > 0 ) { | |
// foreach ( $res[1] as $url ) { | |
// array_push( $posts_images_urls, $wpmc->clean_url( $url ) ); | |
// } | |
// } | |
// Background Image | |
// preg_match_all( "/background_image=\"((https?:\/\/)?[^\\&\#\[\] \"\?]+\.(" . $wpmc->types . "))\"/", $html, $res ); | |
// if ( !empty( $res ) && isset( $res[1] ) && count( $res[1] ) > 0 ) { | |
// foreach ( $res[1] as $url ) { | |
// array_push( $posts_images_urls, $wpmc->clean_url( $url ) ); | |
// } | |
// } | |
// Background Image IDs | |
// preg_match_all( "/background_image_id=\"([0-9,]+)/", $html, $res ); | |
// if ( !empty( $res ) && isset( $res[1] ) ) { | |
// foreach ( $res[1] as $r ) { | |
// $ids = explode( ',', $r ); | |
// $galleries_images = array_merge( $galleries_images, $ids ); | |
// } | |
// } | |
// Galleries | |
// preg_match_all( "/image_ids=\"([0-9,]+)/", $html, $res ); | |
// if ( !empty( $res ) && isset( $res[1] ) ) { | |
// foreach ( $res[1] as $r ) { | |
// $ids = explode( ',', $r ); | |
// $galleries_images = array_merge( $galleries_images, $ids ); | |
// } | |
// } | |
// Add image references to the Media Cleaner | |
$wpmc->add_reference_url( $posts_images_urls, 'PLUGIN_NAME (URL)', $id ); | |
$wpmc->add_reference_id( $galleries_images, 'PLUGIN_NAME (ID)', $id ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment