media-cleaner
Last commit date
common
7 years ago
parsers
7 years ago
scripts
7 years ago
views
7 years ago
admin.php
7 years ago
api.php
7 years ago
core.php
7 years ago
engine.php
7 years ago
media-cleaner.php
7 years ago
parsers.php
7 years ago
readme.txt
7 years ago
ui.php
7 years ago
engine.php
149 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_WPMC_Engine { |
| 4 | |
| 5 | function __construct( $core, $admin ) { |
| 6 | $this->core = $core; |
| 7 | $this->admin = $admin; |
| 8 | } |
| 9 | |
| 10 | /* |
| 11 | STEP 1: Parse the content, and look for references |
| 12 | */ |
| 13 | |
| 14 | // Parse the posts for references (based on $limit and $limitsize for paging the scan) |
| 15 | function parse( $limit, $limitsize, &$message = '' ) { |
| 16 | if ( empty( $limit ) ) |
| 17 | $this->core->reset_issues(); |
| 18 | |
| 19 | $method = get_option( 'wpmc_method', 'media' ); |
| 20 | $check_library = get_option(' wpmc_media_library', true ); |
| 21 | $check_content = get_option( 'wpmc_content', true ); |
| 22 | $check_postmeta = get_option( 'wpmc_postmeta', false ); |
| 23 | $check_posts = get_option( 'wpmc_posts', false ); |
| 24 | $check_widgets = get_option( 'wpmc_widgets', false ); |
| 25 | |
| 26 | if ( $method == 'media' && $check_posts && !$check_content && !$check_postmeta && !$check_widgets ) { |
| 27 | $message = __( "Posts, Meta and Widgets analysis are all off. Done.", 'media-cleaner' ); |
| 28 | return true; |
| 29 | } |
| 30 | if ( $method == 'files' && $check_library && !$check_content && !$check_posts && !$check_postmeta && !$check_widgets ) { |
| 31 | $message = __( "Posts, Meta and Widgets analysis are all off. Done.", 'media-cleaner' ); |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | // Initialize the parsers |
| 36 | do_action( 'wpmc_initialize_parsers' ); |
| 37 | |
| 38 | global $wpdb; |
| 39 | // Maybe we could avoid to check more post_types. |
| 40 | // SELECT post_type, COUNT(*) FROM `wp_posts` GROUP BY post_type |
| 41 | $posts = $wpdb->get_col( $wpdb->prepare( "SELECT p.ID FROM $wpdb->posts p |
| 42 | WHERE p.post_status != 'inherit' |
| 43 | AND p.post_status != 'trash' |
| 44 | AND p.post_status != 'auto-draft' |
| 45 | AND p.post_type != 'attachment' |
| 46 | AND p.post_type != 'shop_order' |
| 47 | AND p.post_type != 'shop_order_refund' |
| 48 | AND p.post_type != 'nav_menu_item' |
| 49 | AND p.post_type != 'revision' |
| 50 | AND p.post_type != 'auto-draft' |
| 51 | AND p.post_type != 'wphb_minify_group' |
| 52 | AND p.post_type != 'customize_changeset' |
| 53 | AND p.post_type != 'oembed_cache' |
| 54 | AND p.post_type NOT LIKE 'ml-slide%' |
| 55 | AND p.post_type NOT LIKE '%acf-%' |
| 56 | AND p.post_type NOT LIKE '%edd_%' |
| 57 | LIMIT %d, %d", $limit, $limitsize |
| 58 | ) |
| 59 | ); |
| 60 | |
| 61 | // Only at the beginning |
| 62 | if ( empty( $limit ) ) { |
| 63 | $this->core->log( "Parsed references:" ); |
| 64 | if ( get_option( 'wpmc_widgets', false ) ) { |
| 65 | |
| 66 | global $wp_registered_widgets; |
| 67 | $syswidgets = $wp_registered_widgets; |
| 68 | $active_widgets = get_option( 'sidebars_widgets' ); |
| 69 | foreach ( $active_widgets as $sidebar_name => $widgets ) { |
| 70 | if ( $sidebar_name != 'wp_inactive_widgets' && !empty( $widgets ) && is_array( $widgets ) ) { |
| 71 | foreach ( $widgets as $key => $widget ) { |
| 72 | do_action( 'wpmc_scan_widget', $syswidgets[$widget] ); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | do_action( 'wpmc_scan_widgets' ); |
| 78 | } |
| 79 | do_action( 'wpmc_scan_once' ); |
| 80 | } |
| 81 | |
| 82 | $this->core->timeout_check_start( count( $posts ) ); |
| 83 | |
| 84 | foreach ( $posts as $post ) { |
| 85 | $this->core->timeout_check(); |
| 86 | |
| 87 | // Check Meta |
| 88 | if ( $check_content || $check_postmeta ) |
| 89 | do_action( 'wpmc_scan_postmeta', $post ); |
| 90 | |
| 91 | // Check Posts |
| 92 | if ( $check_content || $check_posts ) { |
| 93 | // Get HTML for this post |
| 94 | $html = get_post_field( 'post_content', $post ); |
| 95 | do_action( 'wpmc_scan_post', $html, $post ); |
| 96 | } |
| 97 | |
| 98 | // Extra scanning methods |
| 99 | do_action( 'wpmc_scan_extra', $post ); |
| 100 | |
| 101 | $this->core->timeout_check_additem(); |
| 102 | } |
| 103 | |
| 104 | // Write the references found (and cached) by the parsers |
| 105 | $this->core->write_references(); |
| 106 | |
| 107 | $finished = count( $posts ) < $limitsize; |
| 108 | if ( $finished ) |
| 109 | $this->core->log(); |
| 110 | $message = __( "Posts checked.", 'media-cleaner' ); |
| 111 | return $finished; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | STEP 2: List the media entries (or files) |
| 116 | */ |
| 117 | |
| 118 | // Get files in /uploads (if path is null, the root of /uploads is returned) |
| 119 | function get_files( $path = null ) { |
| 120 | $files = apply_filters( 'wpmc_list_uploaded_files', null, $path ); |
| 121 | return $files; |
| 122 | } |
| 123 | |
| 124 | function get_media_entries( $limit, $limitsize ) { |
| 125 | global $wpdb; |
| 126 | $results = $wpdb->get_col( $wpdb->prepare( "SELECT p.ID FROM $wpdb->posts p |
| 127 | WHERE p.post_status = 'inherit' |
| 128 | AND p.post_type = 'attachment' |
| 129 | LIMIT %d, %d", $limit, $limitsize |
| 130 | ) |
| 131 | ); |
| 132 | return $results; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | STEP 3: Check the media entries (or files) against the references |
| 137 | */ |
| 138 | |
| 139 | function check_media( $media ) { |
| 140 | return $this->core->check_media( $media ); |
| 141 | } |
| 142 | |
| 143 | function check_file( $file ) { |
| 144 | return apply_filters( 'wpmc_check_file', true, $file ); |
| 145 | } |
| 146 | |
| 147 | } |
| 148 | |
| 149 | ?> |