media-cleaner
Last commit date
common
8 years ago
admin.php
8 years ago
checkers.php
8 years ago
core.php
8 years ago
custom_checkers.php
8 years ago
media-cleaner.css
9 years ago
media-cleaner.js
8 years ago
media-cleaner.php
8 years ago
readme.txt
8 years ago
scan.php
8 years ago
wpmc_admin.php
8 years ago
wpmc_checkers.php
8 years ago
wpmc_custom_checkers.php
9 years ago
wpmc_scan.php
8 years ago
scan.php
168 lines
| 1 | <?php |
| 2 | |
| 3 | class MeowApps_WPMC_Scan { |
| 4 | |
| 5 | private $core = null; |
| 6 | private $likes = ""; |
| 7 | private $metakeys = array( '%gallery%', '%ids%' ); |
| 8 | |
| 9 | public function __construct( $core ) { |
| 10 | $this->core = $core; |
| 11 | |
| 12 | // Prepare likes for SQL |
| 13 | foreach ( $this->metakeys as $metakey ) |
| 14 | $this->likes .= "OR meta_key LIKE '$metakey' "; |
| 15 | |
| 16 | // Detect values in the general (known, based on %like%) Meta Keys |
| 17 | add_action( 'wpmc_scan_postmeta', array( $this, 'scan_postmeta' ) ); |
| 18 | |
| 19 | if ( class_exists( 'WooCommerce' ) ) |
| 20 | add_action( 'wpmc_scan_postmeta', array( $this, 'scan_postmeta_woocommerce' ) ); |
| 21 | |
| 22 | // Check URLs, IDs, WP Gallery, WooCommerce |
| 23 | add_action( 'wpmc_scan_post', array( $this, 'scan_post' ), 10, 2 ); |
| 24 | |
| 25 | // Advanced Custom Fields |
| 26 | if ( class_exists( 'acf' ) ) |
| 27 | add_action( 'wpmc_scan_postmeta', array( $this, 'scan_postmeta_acf' ) ); |
| 28 | |
| 29 | } |
| 30 | |
| 31 | public function scan_post( $html, $id ) { |
| 32 | $posts_images_urls = array(); |
| 33 | $posts_images_ids = array(); |
| 34 | $galleries_images = array(); |
| 35 | |
| 36 | // Check URLs in HTML |
| 37 | $new_urls = $this->core->get_urls_from_html( $html ); |
| 38 | $posts_images_urls = array_merge( $posts_images_urls, $new_urls ); |
| 39 | |
| 40 | // Check Excerpt for WooCommerce (= Product Short Description) |
| 41 | if ( class_exists( 'WooCommerce' ) ) { |
| 42 | $excerpt = get_post_field( 'post_excerpt', $id ); |
| 43 | if ( !empty( $excerpt ) ) { |
| 44 | $new_urls = $this->core->get_urls_from_html( $excerpt ); |
| 45 | $posts_images_urls = array_merge( $posts_images_urls, $new_urls ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Check for images IDs through classes in in posts |
| 50 | preg_match_all( "/wp-image-([0-9]+)/", $html, $res ); |
| 51 | if ( !empty( $res ) && isset( $res[1] ) && count( $res[1] ) > 0 ) |
| 52 | $posts_images_ids = array_merge( $posts_images_ids, $res[1] ); |
| 53 | |
| 54 | |
| 55 | // Standard WP Gallery |
| 56 | $galleries = get_post_galleries_images( $id ); |
| 57 | foreach ( $galleries as $gallery ) { |
| 58 | foreach ( $gallery as $image ) { |
| 59 | array_push( $galleries_images, $this->core->wpmc_clean_url( $image ) ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | $this->core->add_reference_id( $posts_images_ids, 'CONTENT (ID)' ); |
| 64 | $this->core->add_reference_url( $posts_images_urls, 'CONTENT (URL)' ); |
| 65 | $this->core->add_reference_url( $galleries_images, 'GALLERY (URL)' ); |
| 66 | } |
| 67 | |
| 68 | public function scan_postmeta( $id ) { |
| 69 | global $wpdb; |
| 70 | $query = $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta |
| 71 | WHERE post_id = %d |
| 72 | AND meta_key = '_thumbnail_id' ", $id ) . $this->likes; |
| 73 | $metas = $wpdb->get_col( $query ); |
| 74 | if ( count( $metas ) > 0 ) { |
| 75 | $postmeta_images_ids = array(); |
| 76 | $postmeta_images_urls = array(); |
| 77 | foreach ( $metas as $meta ) { |
| 78 | // Just a number, let's assume it's a Media ID |
| 79 | if ( is_numeric( $meta ) ) { |
| 80 | if ( $meta > 0 ) |
| 81 | array_push( $postmeta_images_ids, $meta ); |
| 82 | continue; |
| 83 | } |
| 84 | $decoded = @unserialize( $meta ); |
| 85 | if ( is_array( $decoded ) ) { |
| 86 | $this->core->array_to_ids_or_urls( $decoded, $postmeta_images_ids, $postmeta_images_urls ); |
| 87 | continue; |
| 88 | } |
| 89 | $exploded = explode( ',', $meta ); |
| 90 | if ( is_array( $exploded ) ) { |
| 91 | $this->core->array_to_ids_or_urls( $exploded, $postmeta_images_ids, $postmeta_images_urls ); |
| 92 | continue; |
| 93 | } |
| 94 | } |
| 95 | $this->core->add_reference_id( $postmeta_images_ids, 'META (ID)' ); |
| 96 | $this->core->add_reference_id( $postmeta_images_urls, 'META (URL)' ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | function scan_postmeta_woocommerce( $id ) { |
| 101 | global $wpdb; |
| 102 | $galleries_images_wc = array(); |
| 103 | $res = $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $id |
| 104 | AND meta_key = '_product_image_gallery'" ); |
| 105 | foreach ( $res as $values ) { |
| 106 | $ids = explode( ',', $values ); |
| 107 | $galleries_images_wc = array_merge( $galleries_images_wc, $ids ); |
| 108 | } |
| 109 | $this->core->add_reference_id( $galleries_images_wc, 'WOOCOOMMERCE (ID)' ); |
| 110 | } |
| 111 | |
| 112 | public function scan_postmeta_acf( $id ) { |
| 113 | $postmeta_images_acf_ids = array(); |
| 114 | $postmeta_images_acf_urls = array(); |
| 115 | $fields = get_field_objects( $id ); |
| 116 | if ( is_array( $fields ) ) { |
| 117 | foreach ( $fields as $field ) { |
| 118 | $format = ""; |
| 119 | if ( isset( $field['return_format'] ) ) |
| 120 | $format = $field['return_format']; |
| 121 | else if ( isset( $field['save_format'] ) ) |
| 122 | $format = $field['save_format']; |
| 123 | |
| 124 | // ACF Repeater |
| 125 | if ( $field['type'] == 'repeater' ) { |
| 126 | if ( !empty( $field['value'] ) ) { |
| 127 | foreach ( $field['value'] as $subfields ) { |
| 128 | foreach ( $subfields as $subfield ) { |
| 129 | if ( $subfield['type'] == 'image' ) { |
| 130 | if ( !empty( $subfield['id'] ) ) |
| 131 | array_push( $postmeta_images_acf_ids, $subfield['id'] ); |
| 132 | if ( !empty( $subfield['url'] ) ) |
| 133 | array_push( $postmeta_images_acf_urls, $this->core->wpmc_clean_url( $subfield['url'] ) ); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | // ACF Image ID and URL |
| 140 | else if ( $field['type'] == 'image' && ( $format == 'array' || $format == 'object' ) ) { |
| 141 | if ( !empty( $field['value']['id'] ) ) |
| 142 | array_push( $postmeta_images_acf_ids, $field['value']['id'] ); |
| 143 | if ( !empty( $field['value']['url'] ) ) |
| 144 | array_push( $postmeta_images_acf_urls, $this->core->wpmc_clean_url( $field['value']['url'] ) ); |
| 145 | } |
| 146 | // ACF Image ID |
| 147 | else if ( $field['type'] == 'image' && $format == 'id' && !empty( $field['value'] ) ) { |
| 148 | array_push( $postmeta_images_acf_ids, $field['value'] ); |
| 149 | } |
| 150 | // ACF Image URL |
| 151 | else if ( $field['type'] == 'image' && $format == 'url' && !empty( $field['value'] ) ) { |
| 152 | array_push( $postmeta_images_acf_urls, $this->core->wpmc_clean_url( $field['value'] ) ); |
| 153 | } |
| 154 | // ACF Gallery |
| 155 | else if ( $field['type'] == 'gallery' && !empty( $field['value'] ) ) { |
| 156 | foreach ( $field['value'] as $media ) { |
| 157 | if ( !empty( $media['id'] ) ) |
| 158 | array_push( $postmeta_images_acf_ids, $media['id'] ); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | $this->core->add_reference_id( $postmeta_images_acf_ids, 'ACF (ID)' ); |
| 163 | $this->core->add_reference_url( $postmeta_images_acf_urls, 'ACF (URL)' ); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | } |
| 168 |