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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 / woocommerce.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
woocommerce.php
125 lines
1 <?php
2
3 add_action( 'wpmc_scan_once', 'wpmc_scan_once_woocommerce' );
4 add_action( 'wpmc_scan_postmeta', 'wpmc_scan_postmeta_woocommerce' );
5
6 /**
7 * Kept for third-party compatibility. Media Cleaner must never alter WordPress
8 * core-table indexes during a scan.
9 */
10 function wpmc_ensure_woocommerce_indexes() {
11 return true;
12 }
13
14 function wpmc_scan_once_woocommerce() {
15 global $wpdb, $wpmc;
16
17 // Ensure indexes exist for better performance
18 wpmc_ensure_woocommerce_indexes();
19
20 $wpmc->run_paged_parser( 'woocommerce-category-images', function( $cursor, $limit ) use ( $wpdb ) {
21 return $wpdb->get_results( $wpdb->prepare( "SELECT meta_id, meta_value FROM $wpdb->termmeta WHERE meta_key LIKE '%%thumbnail_id%%' AND meta_value != '' AND meta_value IS NOT NULL AND meta_id > %d ORDER BY meta_id ASC LIMIT %d", $cursor, $limit ) );
22 }, function( $rows ) use ( $wpmc ) {
23 $ids = array();
24 foreach ( $rows as $row ) {
25 if ( is_numeric( $row->meta_value ) && (int) $row->meta_value > 0 ) $ids[] = (int) $row->meta_value;
26 }
27 if ( !empty( $ids ) ) $wpmc->add_reference_id( $ids, 'WOOCOMMERCE (ID)' );
28 }, 250, function( $rows, $cursor ) {
29 $last = end( $rows );
30 return $last ? (int) $last->meta_id : $cursor;
31 } );
32
33 // PlaceHolder Image ID
34 $placeholder_id = get_option( 'woocommerce_placeholder_image', null, true );
35 if ( !empty( $placeholder_id ) ) {
36 $wpmc->add_reference_id( (int)$placeholder_id, 'WOOCOMMERCE (ID)' );
37 }
38
39 $wpmc->run_paged_parser( 'woocommerce-category-descriptions', function( $cursor, $limit ) use ( $wpdb ) {
40 return $wpdb->get_results( $wpdb->prepare( "SELECT term_taxonomy_id, description FROM $wpdb->term_taxonomy WHERE taxonomy = 'product_cat' AND description <> '' AND description IS NOT NULL AND term_taxonomy_id > %d ORDER BY term_taxonomy_id ASC LIMIT %d", $cursor, $limit ) );
41 }, function( $rows ) use ( $wpmc ) {
42 foreach ( $rows as $row ) {
43 $wpmc->add_reference_url( $wpmc->get_urls_from_html( $row->description ), 'WOOCOMMERCE (URL)' );
44 }
45 }, 100, function( $rows, $cursor ) {
46 $last = end( $rows );
47 return $last ? (int) $last->term_taxonomy_id : $cursor;
48 } );
49 }
50
51 function wpmc_scan_postmeta_woocommerce( $id ) {
52 global $wpdb, $wpmc;
53
54 // Downloadable files
55 $downloable_files = get_post_meta( $id, '_downloadable_files', true );
56 if ( !empty( $downloable_files ) ) {
57 foreach ( $downloable_files as $file ) {
58 $wpmc->add_reference_url( $wpmc->clean_url( $file['file'] ), 'WOOCOMMERCE DOWNLOAD (URL)', $id );
59 }
60 }
61
62 // Galleries - check if any exist first
63 $galleries_images_wc = array();
64 $id = (int)$id;
65
66 $gallery_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = '_product_image_gallery' AND meta_value != '' AND meta_value IS NOT NULL", $id ) );
67
68 if ( $gallery_count > 0 ) {
69 $res = $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = '_product_image_gallery' AND meta_value != '' AND meta_value IS NOT NULL", $id ) );
70
71 foreach ( $res as $values ) {
72 $ids = explode( ',', $values );
73 $galleries_images_wc = array_merge( $galleries_images_wc, $ids );
74 }
75
76 foreach ( $galleries_images_wc as $thumbnail_id ) {
77 //* WooCommerce Gallery Images use srcset so the sizes URL are actually used
78 $urls = $wpmc->get_thumbnails_urls_from_srcset( $thumbnail_id );
79 $wpmc->add_reference_url( $urls, 'WooCommerce Gallery {SAFE}', $id );
80 }
81
82 $wpmc->add_reference_id( $galleries_images_wc, 'WooCommerce Gallery', $id );
83 }
84
85 $first_variation = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'product_variation' ORDER BY ID ASC LIMIT 1", $id ) );
86 if ( $wpdb->last_error ) throw new RuntimeException( sprintf( __( 'WooCommerce variation parser database error: %s', 'media-cleaner' ), $wpdb->last_error ) );
87 if ( !$first_variation ) return;
88
89 $wpmc->run_paged_parser( 'woocommerce-variations-' . $id, function( $cursor, $limit ) use ( $wpdb, $id ) {
90 return $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'product_variation' AND ID > %d ORDER BY ID ASC LIMIT %d", $id, $cursor, $limit ) );
91 }, function( $variations ) use ( $wpdb, $wpmc, $id ) {
92 foreach ( $variations as $variation_id ) {
93 $gallery_variations = array();
94
95 // Check if this variation has additional images before querying
96 $variation_images_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = '_wc_additional_variation_images' AND meta_value != '' AND meta_value IS NOT NULL", $variation_id ) );
97
98 if ( $variation_images_count > 0 ) {
99 $res = $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = '_wc_additional_variation_images' AND meta_value != '' AND meta_value IS NOT NULL", $variation_id ) );
100
101 if( !empty( $res ) ) {
102 foreach ( $res as $values ) {
103 $ids = explode( ',', $values );
104 $gallery_variations = array_merge( $gallery_variations, $ids );
105 }
106 }
107
108 // WooCommerce Gallery Images use srcset so the sizes URL are actually used
109 foreach ( $gallery_variations as $thumbnail_id ) {
110 if( empty( $thumbnail_id ) || !is_numeric( $thumbnail_id ) ) continue;
111
112 $urls = $wpmc->get_thumbnails_urls_from_srcset( intval( $thumbnail_id ) );
113 $wpmc->add_reference_url( $urls, 'WooCommerce Variations Gallery {SAFE}', $id );
114 $wpmc->add_reference_id( $thumbnail_id, 'WooCommerce Variations Gallery', $id );
115 }
116 }
117 }
118 }, 25, function( $variations, $cursor ) {
119 $last = end( $variations );
120 return $last ? (int) $last : $cursor;
121 } );
122 }
123
124 ?>
125