| 1 |
<?php |
| 2 |
/************************************************************************************************ |
| 3 |
* |
| 4 |
* UTILITIES |
| 5 |
* |
| 6 |
************************************************************************************************/ |
| 7 |
?> |
| 8 |
<?php |
| 9 |
class ODB_Utilities { |
| 10 |
/******************************************************************************************** |
| 11 |
* CONSTRUCTOR |
| 12 |
********************************************************************************************/ |
| 13 |
function __construct() { |
| 14 |
} // __construct() |
| 15 |
|
| 16 |
|
| 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 |
/******************************************************************************************** |
| 41 |
* FORMAT SIZES FROM BYTES TO KB OR MB |
| 42 |
********************************************************************************************/ |
| 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'; |
| 47 |
} // odb_format_size() |
| 48 |
|
| 49 |
|
| 50 |
/******************************************************************************************** |
| 51 |
* CALCULATE THE SIZE OF THE WORDPRESS DATABASE (IN BYTES) |
| 52 |
********************************************************************************************/ |
| 53 |
function odb_get_db_size() { |
| 54 |
global $wpdb; |
| 55 |
|
| 56 |
$sql = $wpdb->prepare(" |
| 57 |
SELECT SUM(data_length + index_length) AS size |
| 58 |
FROM information_schema.TABLES |
| 59 |
WHERE table_schema = %s |
| 60 |
GROUP BY table_schema |
| 61 |
", DB_NAME); |
| 62 |
|
| 63 |
$res = $wpdb->get_results($sql); |
| 64 |
|
| 65 |
return $res[0]->size; |
| 66 |
} // odb_get_db_size() |
| 67 |
|
| 68 |
|
| 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 |
/******************************************************************************************** |
| 80 |
* GET DATABASE TABLES |
| 81 |
********************************************************************************************/ |
| 82 |
function odb_get_tables() { |
| 83 |
global $wpdb; |
| 84 |
|
| 85 |
// GET THE DATABASE BASE TABLES |
| 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); |
| 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 |
} |
| 102 |
} // ODB_Utilities |