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.9.7 6.9.8 6.9.9 7.0.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7
media-cleaner / classes / parsers / common.php
media-cleaner / classes / parsers Last commit date
attachments.php 6 years ago common.php 4 months ago maxmegamenu.php 1 month ago meow_gallery.php 3 months ago metaslider.php 1 month ago my-calendar.php 1 month ago woocommerce.php 1 month ago wpseo.php 3 years ago
common.php
195 lines
1 <?php
2
3 class MeowApps_WPMC_Parser_Common {
4
5 private $metakeys = array( '%gallery%', '%ids%' );
6
7 public function __construct() {
8
9 // Check theme and favicon
10 add_action( 'wpmc_scan_once', array( $this, 'scan_once' ), 10, 0 );
11
12 // Check widgets for IDs and URLs
13 add_action( 'wpmc_scan_widget', array( $this, 'scan_widget' ), 10, 1 );
14
15 // Detect values in the general (known, based on %like%) Meta Keys
16 add_action( 'wpmc_scan_postmeta', array( $this, 'scan_postmeta' ), 10, 1 );
17
18 // Check URLs, IDs, WP Gallery
19 add_action( 'wpmc_scan_post', array( $this, 'scan_post' ), 10, 2 );
20 }
21
22 public function scan_once() {
23 global $wpmc;
24 $theme_ids = array();
25 $theme_urls = array();
26 $wpmc->get_images_from_themes( $theme_ids, $theme_urls );
27 $wpmc->add_reference_id( $theme_ids, 'THEME' );
28 $wpmc->add_reference_url( $theme_urls, 'THEME' );
29 $favicon = $wpmc->get_favicon();
30 if ( !empty( $favicon ) ) {
31 $wpmc->add_reference_url( $favicon, 'SITE ICON' );
32 }
33 }
34
35 function get_images_from_widget( $widget, &$ids, &$urls ) {
36 global $wpmc;
37 // TODO: We should test with widgets
38 if ( !isset( $widget['callback'] ) || !isset( $widget['callback'][0] ) ) {
39 return;
40 }
41 $widget_class = $widget['callback'][0]->option_name;
42 $instance_id = $widget['params'][0]['number'];
43 $widget_data = get_option( $widget_class );
44 if ( !empty( $widget_data[$instance_id]['text'] ) ) {
45 $html = $widget_data[$instance_id]['text']; // mm change
46 $urls = array_merge( $urls, $wpmc->get_urls_from_html( $html ) );
47 }
48 if ( !empty( $widget_data[$instance_id]['attachment_id'] ) ) {
49 $id = $widget_data[$instance_id]['attachment_id'];
50 array_push( $ids, $id );
51 }
52 if ( !empty( $widget_data[$instance_id]['url'] ) ) {
53 $url = $widget_data[$instance_id]['url'];
54 if ( $wpmc->is_url( $url ) ) {
55 $url = $wpmc->clean_url( $url );
56 if ( !empty($url) )
57 array_push( $urls, $url );
58 }
59 }
60 if ( !empty( $widget_data[$instance_id]['ids'] ) ) {
61 $newIds = $widget_data[$instance_id]['ids'];
62 if ( is_array( $newIds ) ) {
63 $ids = array_merge( $ids, $newIds );
64 }
65 }
66 // Recent Blog Posts
67 if ( !empty( $widget_data[$instance_id]['thumbnail'] ) ) {
68 $id = $widget_data[$instance_id]['thumbnail'];
69 array_push( $ids, $id );
70 }
71 }
72
73 public function scan_widget( $widget ) {
74 global $wpmc;
75 $widgets_ids = array();
76 $widgets_urls = array();
77 $this->get_images_from_widget( $widget, $widgets_ids, $widgets_urls );
78 $wpmc->add_reference_id( $widgets_ids, 'WIDGET' );
79 $wpmc->add_reference_url( $widgets_urls, 'WIDGET' );
80 }
81
82 public function get_post_galleries_ids( $id ) {
83 global $post;
84 $content_post = get_post( $id );
85 $content = $content_post->post_content;
86 $ids = array();
87 if ( preg_match_all('/\[gallery.*ids=.(.*).\]/', $content, $foundArrayIds ) ) {
88 if ( $foundArrayIds ) {
89 foreach ( $foundArrayIds[1] as $foundIds ) {
90 $newIds = explode( ',', $foundIds );
91 $ids = array_merge( $ids, $newIds );
92 }
93 }
94 }
95 return $ids;
96 }
97
98 public function scan_post( $html, $id ) {
99 global $wpmc;
100 $posts_images_urls = array();
101 $posts_images_ids = array();
102 $galleries_images = array();
103
104 // Check URLs in HTML
105 $new_urls = $wpmc->get_urls_from_html( $html );
106 $posts_images_urls = array_merge( $posts_images_urls, $new_urls );
107
108 // Check URLs in the Excerpt
109 $excerpt = get_post_field( 'post_excerpt', $id );
110 if ( !empty( $excerpt ) ) {
111 $new_urls = $wpmc->get_urls_from_html( $excerpt );
112 $posts_images_urls = array_merge( $posts_images_urls, $new_urls );
113 }
114
115 // Check for images IDs through classes in in posts
116 preg_match_all( "/wp-image-([0-9]+)/", $html, $res );
117 if ( !empty( $res ) && isset( $res[1] ) && count( $res[1] ) > 0 )
118 $posts_images_ids = array_merge( $posts_images_ids, $res[1] );
119
120 // Standard WP Gallery
121 $ids = $this->get_post_galleries_ids( $id );
122 $posts_images_ids = array_merge( $posts_images_ids, $ids );
123 $galleries = get_post_galleries_images( $id );
124 foreach ( $galleries as $gallery ) {
125 foreach ( $gallery as $image ) {
126 array_push( $galleries_images, $wpmc->clean_url( $image ) );
127 }
128 }
129
130 // Check all the shortcodes attributes
131 $shortcodes = array();
132 $shortcodes = $wpmc->get_all_shortcodes_attributes( $html, [ "id", "ids" ], [ "url", "link" ] );
133
134 $posts_images_ids = array_merge( $posts_images_ids, $shortcodes['ids'] );
135 $posts_images_urls = array_merge( $posts_images_urls, $shortcodes['urls'] );
136
137 $wpmc->add_reference_id( $posts_images_ids, "Common Content", $id );
138 $wpmc->add_reference_url( $posts_images_urls, "Common Content", $id );
139 $wpmc->add_reference_url( $galleries_images, "Common Gallery", $id );
140 }
141
142 public function scan_postmeta( $id ) {
143 global $wpdb, $wpmc;
144
145 $likes = array();
146 foreach ($this->metakeys as $metakey) {
147 $likes[] = "OR meta_key LIKE '{$metakey}'";
148 }
149 $likes = implode( ' ', $likes );
150 $q = "SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = %d";
151 // Since WordPress 6.2, $wpdb->prepare seems fail with the AND/OR.
152 $sql = $wpdb->prepare( $q, $id ) . " AND (meta_key = '_thumbnail_id' {$likes})";
153 $metas = $wpdb->get_col( $sql );
154 if ( count( $metas ) > 0 ) {
155 $postmeta_images_ids = array();
156 $postmeta_images_urls = array();
157 foreach ( $metas as $meta ) {
158 // Just a number, let's assume it's a Media ID
159 if ( is_numeric( $meta ) ) {
160 if ( $meta > 0 )
161 array_push( $postmeta_images_ids, $meta );
162 continue;
163 }
164 else if ( is_serialized( $meta ) ) {
165 $decoded = @unserialize( $meta );
166 if ( is_array( $decoded ) ) {
167 $wpmc->array_to_ids_or_urls( $decoded, $postmeta_images_ids, $postmeta_images_urls );
168 continue;
169 }
170 }
171 else {
172 $exploded = $meta !== null ? explode(',', $meta) : [];
173 if ( is_array( $exploded ) && count( $exploded ) > 0 ) {
174 $wpmc->array_to_ids_or_urls( $exploded, $postmeta_images_ids, $postmeta_images_urls );
175 continue;
176 }
177 }
178 }
179 $wpmc->add_reference_id( $postmeta_images_ids, 'Post Meta', $id );
180 $wpmc->add_reference_url( $postmeta_images_urls, 'Post Meta', $id );
181 }
182
183
184 // WordPress does not tell which sizes are used for the Featured Image, so to be safe, we just get them all.
185 // * Add the SAFE tag for 'FEATURED IMAGE (URL)' in the dashboard
186 $thumbnail_id = get_post_thumbnail_id( $id );
187 $urls = $wpmc->get_thumbnails_urls( $thumbnail_id );
188
189 $wpmc->add_reference_url( $urls, 'Featured Image {SAFE}', $id );
190
191 }
192 }
193
194 new MeowApps_WPMC_Parser_Common();
195