Permalink
Browse files

Add wpdb class casting to the main class of WP_Logging for properly w…

…orking with new version of Wordpress (>4.0)
  • Loading branch information...
allyshka
allyshka committed Apr 14, 2015
1 parent b80e1c3 commit 9e558ff6e283f8662b5f56603fe9274c986fb5d7
Showing with 34 additions and 1 deletion.
  1. +34 −1 wp-logger.php
View
@@ -42,7 +42,7 @@ class WP_Logger extends wpdb {
* Adds all of the filters and hooks and enforces singleton pattern
*/
function __construct() {
global $wbdb;
global $wpdb;
// Enforces a single instance of this class.
if ( isset( self::$instance ) ) {
@@ -51,6 +51,9 @@ function __construct() {
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.
add_action( 'init', array( $this, 'init' ), 1 );
add_action( 'admin_menu', array( $this, 'add_menu_page' ) );
@@ -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.
*

0 comments on commit 9e558ff

Please sign in to comment.