| @@ -5,67 +5,98 @@ | ||
| 5 | 5 | * |
| 6 | 6 | ************************************************************************************************/ |
| 7 | 7 | ?> |
| 8 | 8 | <?php |
| 9 | -class ODB_Utilities | |
| 10 | -{ | |
| 9 | +class ODB_Utilities { | |
| 11 | 10 | /******************************************************************************************** |
| 12 | 11 | * CONSTRUCTOR |
| 13 | - ********************************************************************************************/ | |
| 14 | - function __construct() | |
| 15 | - { | |
| 12 | + ********************************************************************************************/ | |
| 13 | + function __construct() { | |
| 16 | 14 | } // __construct() |
| 17 | 15 | |
| 18 | 16 | |
| 19 | 17 | /******************************************************************************************** |
| 18 | + * GET THE (FOR REVISIONS) RELEVANT POST TYPES, INCLUDING CUSTOM POST TYPES (from v4.4) | |
| 19 | + ********************************************************************************************/ | |
| 20 | + function odb_get_relevant_post_types() { | |
| 21 | + $relevant_pts = array(); | |
| 22 | + $posttypes = get_post_types(); | |
| 23 | + foreach ($posttypes as $posttype) { | |
| 24 | + // SKIP THE DEFAULT POST TYPES (EXCEPT FOR 'post' AND 'page') | |
| 25 | + if ($posttype != 'attachment' && | |
| 26 | + $posttype != 'revision' && | |
| 27 | + $posttype != 'nav_menu_item' && | |
| 28 | + $posttype != 'custom_css' && | |
| 29 | + $posttype != 'customize_changeset' && | |
| 30 | + // v4.4.2 | |
| 31 | + $posttype != 'oembed_cache') { | |
| 32 | + array_push($relevant_pts, $posttype); | |
| 33 | + } | |
| 34 | + } // foreach ($posttypes as $posttype) | |
| 35 | + | |
| 36 | + return $relevant_pts; | |
| 37 | + } // odb_get_relevant_post_types() | |
| 38 | + | |
| 39 | + | |
| 40 | + /******************************************************************************************** | |
| 20 | 41 | * FORMAT SIZES FROM BYTES TO KB OR MB |
| 21 | 42 | ********************************************************************************************/ |
| 22 | - function odb_format_size($size, $precision=1) | |
| 23 | - { | |
| 24 | - if($size>1024*1024) | |
| 25 | - $table_size = (round($size/(1024*1024),$precision)).' MB'; | |
| 26 | - else | |
| 27 | - $table_size = (round($size/1024,$precision)).' KB'; | |
| 28 | - | |
| 29 | - return $table_size; | |
| 43 | + function odb_format_size($size, $precision=1) { | |
| 44 | + if($size > 1024*1024) return (round($size/(1024*1024),$precision)).' MB'; | |
| 45 | + | |
| 46 | + return (round($size/1024,$precision)).' KB'; | |
| 30 | 47 | } // odb_format_size() |
| 31 | - | |
| 32 | 48 | |
| 49 | + | |
| 33 | 50 | /******************************************************************************************** |
| 34 | 51 | * CALCULATE THE SIZE OF THE WORDPRESS DATABASE (IN BYTES) |
| 35 | 52 | ********************************************************************************************/ |
| 36 | - function odb_get_db_size() | |
| 37 | - { | |
| 53 | + function odb_get_db_size() { | |
| 38 | 54 | global $wpdb; |
| 39 | - | |
| 40 | - $sql = " | |
| 55 | + | |
| 56 | + $sql = $wpdb->prepare(" | |
| 41 | 57 | SELECT SUM(data_length + index_length) AS size |
| 42 | 58 | FROM information_schema.TABLES |
| 43 | - WHERE table_schema = '".DB_NAME."' | |
| 59 | + WHERE table_schema = %s | |
| 44 | 60 | GROUP BY table_schema |
| 45 | - "; | |
| 46 | - | |
| 61 | + ", DB_NAME); | |
| 62 | + | |
| 47 | 63 | $res = $wpdb->get_results($sql); |
| 48 | - | |
| 64 | + | |
| 49 | 65 | return $res[0]->size; |
| 50 | 66 | } // odb_get_db_size() |
| 51 | 67 | |
| 52 | - | |
| 68 | + | |
| 53 | 69 | /******************************************************************************************** |
| 70 | + * PARSE A TIMESTAMP - v4.6 | |
| 71 | + ********************************************************************************************/ | |
| 72 | + function odb_parse_timestamp($timestamp) { | |
| 73 | + $d = substr($timestamp, 4, 2).'/'.substr($timestamp, 6, 2).'/'.substr($timestamp, 0, 4); | |
| 74 | + $d .= ' ' . substr($timestamp, 8, 2).':'.substr($timestamp, 10, 2).':'.substr($timestamp, 12, 2); | |
| 75 | + return $d; | |
| 76 | + } // odb_parse_timestamp($timestamp) | |
| 77 | + | |
| 78 | + | |
| 79 | + /******************************************************************************************** | |
| 54 | 80 | * GET DATABASE TABLES |
| 55 | 81 | ********************************************************************************************/ |
| 56 | - function odb_get_tables() | |
| 57 | - { | |
| 82 | + function odb_get_tables() { | |
| 58 | 83 | global $wpdb; |
| 59 | 84 | |
| 60 | - $sql = " | |
| 61 | - SHOW FULL TABLES | |
| 62 | - FROM `".DB_NAME."` | |
| 63 | - WHERE table_type = 'BASE TABLE' | |
| 64 | - "; | |
| 65 | - | |
| 66 | 85 | // GET THE DATABASE BASE TABLES |
| 67 | - // $odb_class->odb_tables = $wpdb->get_results($sql, ARRAY_N); | |
| 68 | - // var_dump($wpdb->get_results($sql, ARRAY_N)); | |
| 69 | - return $wpdb->get_results($sql, ARRAY_N); | |
| 86 | + return $wpdb->get_results(" | |
| 87 | + SHOW FULL TABLES | |
| 88 | + FROM `" . $this->odb_sanitize_key( DB_NAME ) . "` | |
| 89 | + WHERE table_type = 'BASE TABLE' | |
| 90 | + ", ARRAY_N); | |
| 70 | 91 | } // odb_get_tables() |
| 92 | + | |
| 93 | + /** | |
| 94 | + * Key sanitization. Matches sanitize_key() but does not strtolower. | |
| 95 | + * | |
| 96 | + * @param string $key | |
| 97 | + * @return string|string[]|null | |
| 98 | + */ | |
| 99 | + function odb_sanitize_key( string $key ) { | |
| 100 | + return preg_replace( '/[^a-z0-9_\-]/', '', $key ); | |
| 101 | + } | |
| 71 | 102 | } // ODB_Utilities |