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 |