@@ -42,7 +42,7 @@ class WP_Logger extends wpdb {
4242 * Adds all of the filters and hooks and enforces singleton pattern
4343 */
4444 function __construct () {
45- global $ wbdb ;
45+ global $ wpdb ;
4646
4747 // Enforces a single instance of this class.
4848 if ( isset ( self ::$ instance ) ) {
@@ -51,6 +51,9 @@ function __construct() {
5151
5252 self ::$ instance = $ this ;
5353
54+ // Check of $wpdb object type
55+ if ($ wpdb instanceOf wpdb) $ this ->cast ($ this , $ wpdb );
56+
5457 // These actions setup the plugin and inject scripts and styles on our logs page.
5558 add_action ( 'init ' , array ( $ this , 'init ' ), 1 );
5659 add_action ( 'admin_menu ' , array ( $ this , 'add_menu_page ' ) );
@@ -85,6 +88,36 @@ function __construct() {
8588 }
8689 }
8790
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+
88121 /**
89122 * Will return the current version of WP Logger to plugins that call the `wp_logger_version` filter.
90123 *
0 commit comments