@@ -42,7 +42,7 @@ class WP_Logger extends wpdb {
42
42
* Adds all of the filters and hooks and enforces singleton pattern
43
43
*/
44
44
function __construct () {
45
- global $ wbdb ;
45
+ global $ wpdb ;
46
46
47
47
// Enforces a single instance of this class.
48
48
if ( isset ( self ::$ instance ) ) {
@@ -51,6 +51,9 @@ function __construct() {
51
51
52
52
self ::$ instance = $ this ;
53
53
54
+ // Check of $wpdb object type
55
+ if ($ wpdb instanceOf wpdb) $ this ->cast ($ this , $ wpdb );
56
+
54
57
// These actions setup the plugin and inject scripts and styles on our logs page.
55
58
add_action ( 'init ' , array ( $ this , 'init ' ), 1 );
56
59
add_action ( 'admin_menu ' , array ( $ this , 'add_menu_page ' ) );
@@ -85,6 +88,36 @@ function __construct() {
85
88
}
86
89
}
87
90
91
+ /**
92
+ * Class casting
93
+ *
94
+ * @param string|object $destination
95
+ * @param object $sourceObject
96
+ * @return object
97
+ */
98
+ function cast ($ destination , $ sourceObject )
99
+ {
100
+ if (is_string ($ destination )) {
101
+ $ destination = new $ destination ();
102
+ }
103
+ $ sourceReflection = new ReflectionObject ($ sourceObject );
104
+ $ destinationReflection = new ReflectionObject ($ destination );
105
+ $ sourceProperties = $ sourceReflection ->getProperties ();
106
+ foreach ($ sourceProperties as $ sourceProperty ) {
107
+ $ sourceProperty ->setAccessible (true );
108
+ $ name = $ sourceProperty ->getName ();
109
+ $ value = $ sourceProperty ->getValue ($ sourceObject );
110
+ if ($ destinationReflection ->hasProperty ($ name )) {
111
+ $ propDest = $ destinationReflection ->getProperty ($ name );
112
+ $ propDest ->setAccessible (true );
113
+ $ propDest ->setValue ($ destination ,$ value );
114
+ } else {
115
+ $ destination ->$ name = $ value ;
116
+ }
117
+ }
118
+ return $ destination ;
119
+ }
120
+
88
121
/**
89
122
* Will return the current version of WP Logger to plugins that call the `wp_logger_version` filter.
90
123
*
0 commit comments