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.0.4 7.0.5 7.0.6 7.0.7
media-cleaner / scan.php
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