PluginProbe
Optimize Database after Deleting Revisions / 5.2
Optimize Database after Deleting Revisions v5.2
3.4.7 3.4.8 3.4.9 3.5 3.5.1 4.0 4.0.1 4.0.2 4.0.3 4.1 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.3 All 144 releases
rvg-optimize-database / classes / odb-utilities.php

odb-utilities.php in Optimize Database after Deleting Revisions 5.2, at classes/odb-utilities.php

102 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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