Skip to content

Commit

Permalink
Add wpdb class casting to the main class of WP_Logging for properly w…
Browse files Browse the repository at this point in the history
…orking with new version of Wordpress (>4.0)
  • Loading branch information
allyshka committed Apr 14, 2015
1 parent b80e1c3 commit 9e558ff
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion wp-logger.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class WP_Logger extends wpdb {
* Adds all of the filters and hooks and enforces singleton pattern * Adds all of the filters and hooks and enforces singleton pattern
*/ */
function __construct() { function __construct() {
global $wbdb; global $wpdb;


// Enforces a single instance of this class. // Enforces a single instance of this class.
if ( isset( self::$instance ) ) { if ( isset( self::$instance ) ) {
Expand All @@ -51,6 +51,9 @@ function __construct() {


self::$instance = $this; self::$instance = $this;


// Check of $wpdb object type
if($wpdb instanceOf wpdb) $this->cast($this, $wpdb);

// These actions setup the plugin and inject scripts and styles on our logs page. // These actions setup the plugin and inject scripts and styles on our logs page.
add_action( 'init', array( $this, 'init' ), 1 ); add_action( 'init', array( $this, 'init' ), 1 );
add_action( 'admin_menu', array( $this, 'add_menu_page' ) ); add_action( 'admin_menu', array( $this, 'add_menu_page' ) );
Expand Down Expand Up @@ -85,6 +88,36 @@ function __construct() {
} }
} }


/**
* Class casting
*
* @param string|object $destination
* @param object $sourceObject
* @return object
*/
function cast($destination, $sourceObject)
{
if (is_string($destination)) {
$destination = new $destination();
}
$sourceReflection = new ReflectionObject($sourceObject);
$destinationReflection = new ReflectionObject($destination);
$sourceProperties = $sourceReflection->getProperties();
foreach ($sourceProperties as $sourceProperty) {
$sourceProperty->setAccessible(true);
$name = $sourceProperty->getName();
$value = $sourceProperty->getValue($sourceObject);
if ($destinationReflection->hasProperty($name)) {
$propDest = $destinationReflection->getProperty($name);
$propDest->setAccessible(true);
$propDest->setValue($destination,$value);
} else {
$destination->$name = $value;
}
}
return $destination;
}

/** /**
* Will return the current version of WP Logger to plugins that call the `wp_logger_version` filter. * Will return the current version of WP Logger to plugins that call the `wp_logger_version` filter.
* *
Expand Down

0 comments on commit 9e558ff

Please sign in to comment.