class-adbc-common-model.php
7 months ago
class-adbc-cron-jobs.php
2 months ago
class-adbc-database.php
5 months ago
class-adbc-options.php
4 months ago
class-adbc-post-types.php
3 months ago
class-adbc-posts-meta.php
4 months ago
class-adbc-sites.php
7 months ago
class-adbc-tables.php
3 months ago
class-adbc-transients.php
4 months ago
class-adbc-users-meta.php
4 months ago
class-adbc-common-model.php
162 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | /** |
| 8 | * ADBC common model class. |
| 9 | * |
| 10 | * This class provides common functions used across the plugin. |
| 11 | */ |
| 12 | class ADBC_Common_Model { |
| 13 | |
| 14 | /** |
| 15 | * Filter item by the given belongs_to filter. |
| 16 | * |
| 17 | * @param array $filters The filters. |
| 18 | * @param object $belongs_to The belongs_to data. |
| 19 | * @return bool True if the table satisfies the belongs_to filter, false otherwise. |
| 20 | */ |
| 21 | public static function is_item_satisfies_belongs_to( $filters, $belongs_to ) { |
| 22 | |
| 23 | // Check the belongs_to filter |
| 24 | if ( ! empty( $filters['belongs_to'] ) && $filters['belongs_to'] !== 'all' ) { |
| 25 | |
| 26 | $type_map = [ |
| 27 | 'not_scanned' => 'u', |
| 28 | 'plugins' => 'p', |
| 29 | 'themes' => 't', |
| 30 | 'wordpress' => 'w', |
| 31 | 'orphans' => 'o', |
| 32 | 'unknown' => 'unk' |
| 33 | ]; |
| 34 | |
| 35 | // Return false if the type does not match the expected type |
| 36 | if ( ! isset( $type_map[ $filters['belongs_to'] ] ) || $type_map[ $filters['belongs_to'] ] !== $belongs_to['type'] ) |
| 37 | return false; |
| 38 | |
| 39 | } |
| 40 | |
| 41 | // Check 'belongs_to_plugin_slug' filter. |
| 42 | if ( ! empty( $filters['belongs_to_plugin_slug'] ) ) { |
| 43 | // For 'u', the slug is not set, so we need to check if it's not empty before comparing |
| 44 | if ( ! isset( $belongs_to['slug'] ) || $belongs_to['slug'] !== $filters['belongs_to_plugin_slug'] ) |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | // Check 'belongs_to_theme_slug' filter. |
| 49 | if ( ! empty( $filters['belongs_to_theme_slug'] ) ) { |
| 50 | // For 'u', the slug is not set, so we need to check if it's not empty before comparing |
| 51 | if ( ! isset( $belongs_to['slug'] ) || $belongs_to['slug'] !== $filters['belongs_to_theme_slug'] ) |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | // Check 'show_manual_corrections_only' filter. |
| 56 | if ( ! empty( $filters['show_manual_corrections_only'] ) && $filters['show_manual_corrections_only'] === true ) { |
| 57 | // For 'u', the slug is not set, so we need to check if it's not empty before comparing |
| 58 | if ( ! isset( $belongs_to['slug'] ) || $belongs_to['by'] !== 'm' ) |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get rows from a specific column in a table for a specific site based on IDs. |
| 67 | * |
| 68 | * @param int $site_id The site ID. |
| 69 | * @param string $table_name The name of the table. |
| 70 | * @param string $column_name The name of the column to get items from. |
| 71 | * @param string $id_column_name The name of the ID column. |
| 72 | * @param array $ids An array of IDs to filter the results. |
| 73 | * @return array An array of items from the specified column, or an empty array if no items are found. |
| 74 | */ |
| 75 | public static function get_column_rows_from_table( $site_id, $table_name, $column_name, $id_column_name, $ids = [] ) { |
| 76 | |
| 77 | global $wpdb; |
| 78 | |
| 79 | $prefix = ADBC_Sites::instance()->get_prefix_from_site_id( $site_id ); |
| 80 | |
| 81 | if ( $prefix === null || empty( $ids ) ) |
| 82 | return []; |
| 83 | |
| 84 | $tbl = '`' . esc_sql( $prefix . $table_name ) . '`'; |
| 85 | $col = '`' . esc_sql( $column_name ) . '`'; |
| 86 | $idCol = '`' . esc_sql( $id_column_name ) . '`'; |
| 87 | |
| 88 | // Prepare the SQL query to get the items names from the database. |
| 89 | $ids_placeholder = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); |
| 90 | $sql = $wpdb->prepare( |
| 91 | "SELECT $col FROM $tbl WHERE $idCol IN ($ids_placeholder)", |
| 92 | ...$ids |
| 93 | ); |
| 94 | |
| 95 | // Execute the query and get the results. |
| 96 | $results = $wpdb->get_col( $sql ); |
| 97 | |
| 98 | return $results; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Load scan results to items for free version. |
| 103 | * |
| 104 | * @param array $items The items to load scan results into. |
| 105 | * @return void |
| 106 | */ |
| 107 | public static function load_scan_results_to_items_for_free_version( &$items ) { |
| 108 | |
| 109 | // First of all, add some properties to each item to prevent any undefined index error. |
| 110 | foreach ( $items as $item ) { |
| 111 | $item->belongs_to = [ |
| 112 | 'type' => 'u', |
| 113 | 'slug' => 'u', |
| 114 | 'name' => '', |
| 115 | 'by' => '', |
| 116 | 'percent' => '', |
| 117 | 'status' => '', |
| 118 | ]; |
| 119 | $item->known_plugins = []; // known plugins that are related to the item |
| 120 | $item->known_themes = []; // known themes that are related to the item |
| 121 | } |
| 122 | |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Count the number of items that were not scanned in the given list. |
| 127 | * |
| 128 | * @param string $items_type The type of items to count. "tables", "options", "cron_jobs", etc. |
| 129 | * @param array $items_list The list of items to count. ["name1" => true, "name2" => true, ...] |
| 130 | * |
| 131 | * @return int The number of items that were not scanned. |
| 132 | */ |
| 133 | public static function count_not_scanned_items_in_list_for_free( $items_type, &$items_list ) { |
| 134 | |
| 135 | if ( empty( $items_list ) ) |
| 136 | return 0; // Nothing to count. |
| 137 | |
| 138 | // ADBC hardcoded items (exact matches only). |
| 139 | $adbc_items = ADBC_Hardcoded_Items::instance()->get_adbc_items( $items_type ); |
| 140 | $wp_hardcoded_items = ADBC_Hardcoded_Items::instance()->get_wordpress_items( $items_type ); |
| 141 | |
| 142 | foreach ( $items_list as $index => $item ) { |
| 143 | |
| 144 | // Check WordPress core hardcoded items (exact + rule-based for transients). |
| 145 | if ( ADBC_Hardcoded_Items::instance()->is_item_belongs_to_wp_core( $item, $items_type, $wp_hardcoded_items ) ) { |
| 146 | unset( $items_list[ $index ] ); |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | // Check ADBC hardcoded items (exact matches). |
| 151 | if ( isset( $adbc_items[ $item ] ) ) { |
| 152 | unset( $items_list[ $index ] ); |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | return count( $items_list ); |
| 159 | |
| 160 | } |
| 161 | |
| 162 | } |