media-cleaner
Last commit date
common
8 years ago
views
7 years ago
admin.php
7 years ago
checkers.php
8 years ago
core.php
7 years ago
custom_checkers.php
8 years ago
media-cleaner.css
7 years ago
media-cleaner.js
7 years ago
media-cleaner.php
7 years ago
readme.txt
7 years ago
scan.php
7 years ago
ui.php
7 years ago
scan.php
199 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 | $fields = get_field_objects( $id ); |
| 114 | if ( is_array( $fields ) ) { |
| 115 | foreach ( $fields as $field ) |
| 116 | $this->scan_postmeta_acf_field( $field, $id, 8 ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Scans a single ACF field object. |
| 122 | * If the specified field is a repeater or a flexible content, |
| 123 | * scans each subfield recursively. |
| 124 | * |
| 125 | * @param array $field |
| 126 | * An associative array replesenting a single ACF field. |
| 127 | * The actual array must be structured like this: |
| 128 | * array ( |
| 129 | * 'name' => The name of the field |
| 130 | * 'type' => The field type i.e. 'text', 'object', 'repeater' |
| 131 | * 'value' => The value |
| 132 | * ... |
| 133 | * ) |
| 134 | * @param int $id The post ID |
| 135 | * @param int $recursion_limit The max recursion depth. Negative number means unlimited |
| 136 | * |
| 137 | * @since ACF 5.6.10 |
| 138 | */ |
| 139 | public function scan_postmeta_acf_field( $field, $id, $recursion_limit = -1 ) { |
| 140 | if ( !isset( $field['type'] ) ) return; |
| 141 | |
| 142 | /** Multiple Fields (Repeater or Flexible Content) **/ |
| 143 | static $recursives = array ( // Possibly Recursive Types |
| 144 | 'repeater', |
| 145 | 'flexible_content' |
| 146 | ); |
| 147 | if ( in_array( $field['type'], $recursives ) && have_rows( $field['name'], $id ) ) { |
| 148 | if ( $recursion_limit == 0 ) return; // Too much recursion |
| 149 | do { // Iterate over rows |
| 150 | $row = the_row( true ); |
| 151 | foreach ( $row as $col => $value ) { // Iterate over columns (subfields) |
| 152 | $subfield = get_sub_field_object( $col, true, true ); |
| 153 | if ( !is_array( $subfield ) ) continue; |
| 154 | if ( WP_DEBUG ) { // XXX Debug |
| 155 | if ( !isset( $subfield['value'] ) ) trigger_error( 'Unexpected Situation: $subfield[value] is not set', E_USER_ERROR ); |
| 156 | if ( $subfield['value'] != $value ) trigger_error( 'Unexpected Situation: $subfield[value] has unexpected value', E_USER_ERROR ); |
| 157 | } |
| 158 | $this->scan_postmeta_acf_field( $subfield, $id, $recursion_limit - 1 ); // Recursion |
| 159 | } |
| 160 | } while ( have_rows( $field['name'], $id ) ); |
| 161 | return; |
| 162 | } |
| 163 | /** Singular Field **/ |
| 164 | $postmeta_images_acf_ids = array(); |
| 165 | $postmeta_images_acf_urls = array(); |
| 166 | |
| 167 | $format = ""; |
| 168 | if ( isset( $field['return_format'] ) ) |
| 169 | $format = $field['return_format']; |
| 170 | else if ( isset( $field['save_format'] ) ) |
| 171 | $format = $field['save_format']; |
| 172 | |
| 173 | // ACF Image ID and URL |
| 174 | if ( $field['type'] == 'image' && ( $format == 'array' || $format == 'object' ) ) { |
| 175 | if ( !empty( $field['value']['id'] ) ) |
| 176 | array_push( $postmeta_images_acf_ids, $field['value']['id'] ); |
| 177 | if ( !empty( $field['value']['url'] ) ) |
| 178 | array_push( $postmeta_images_acf_urls, $this->core->wpmc_clean_url( $field['value']['url'] ) ); |
| 179 | } |
| 180 | // ACF Image ID |
| 181 | else if ( $field['type'] == 'image' && $format == 'id' && !empty( $field['value'] ) ) { |
| 182 | array_push( $postmeta_images_acf_ids, $field['value'] ); |
| 183 | } |
| 184 | // ACF Image URL |
| 185 | else if ( $field['type'] == 'image' && $format == 'url' && !empty( $field['value'] ) ) { |
| 186 | array_push( $postmeta_images_acf_urls, $this->core->wpmc_clean_url( $field['value'] ) ); |
| 187 | } |
| 188 | // ACF Gallery |
| 189 | else if ( $field['type'] == 'gallery' && !empty( $field['value'] ) ) { |
| 190 | foreach ( $field['value'] as $media ) { |
| 191 | if ( !empty( $media['id'] ) ) |
| 192 | array_push( $postmeta_images_acf_ids, $media['id'] ); |
| 193 | } |
| 194 | } |
| 195 | $this->core->add_reference_id( $postmeta_images_acf_ids, 'ACF (ID)' ); |
| 196 | $this->core->add_reference_url( $postmeta_images_acf_urls, 'ACF (URL)' ); |
| 197 | } |
| 198 | } |
| 199 |