Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Akeda Bagus
committed
Jul 29, 2013
1 parent
2a58dc6
commit 978100f
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/* | ||
Plugin Name: Post Logger | ||
Description: Logs when post is updated (Abstract factory design pattern example). | ||
Version: 0.1.0 | ||
Author: Akeda Bagus | ||
Author URI: http://gedex.web.id/ | ||
Plugin URI: http://github.com/gedex/WP_DPP | ||
*/ | ||
|
||
namespace WP_DPP\Post_Logger; | ||
|
||
|
||
interface Abstract_Factory { | ||
public static function get_instance(); | ||
public static function get_loggers(); | ||
public static function set_loggers(); | ||
} | ||
|
||
|
||
class Factory { | ||
/** | ||
* Logger products | ||
* | ||
* @var array | ||
*/ | ||
private static $loggers; | ||
|
||
public static function get_instance( $logger ) { | ||
$classname = 'WP_DPP\Post_Logger\Logger_' . ucfirst( strtolower( $logger ) ); | ||
if ( in_array( $logger, self::$products ) && class_exists( $classname ) ) { | ||
return new $classname; | ||
} | ||
|
||
throw new Exception( 'Undefined Logger ' . $logger ); | ||
} | ||
|
||
public static function set_loggers(array $loggers) { | ||
self::$loggers = $loggers; | ||
} | ||
|
||
public static function get_loggers() { | ||
return self::$loggers; | ||
} | ||
} | ||
|
||
|
||
interface Logger { | ||
public function do_logging(\WP_Post $post); | ||
} | ||
|
||
|
||
class Logger_File implements Logger { | ||
public function do_logging(\WP_Post $post) { | ||
error_log( sprintf( 'Logger message for post "%s."', $post->post_title ) ); | ||
} | ||
} | ||
|
||
|
||
class Logger_Email implements Logger { | ||
public function do_logging(\WP_Post $post) { | ||
extract( array( | ||
'to' => 'admin@gedex.web.id', | ||
'subject' => 'Logger subject', | ||
'message' => 'Logger message for post ' . $post->post_title, | ||
) ); | ||
wp_mail( $to, $subject, $message ); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Class that bootstrap the plugin | ||
*/ | ||
class WP_Plugin { | ||
|
||
public function __construct() { | ||
Factory::set_products( array('file', 'email') ); | ||
|
||
add_action( 'save_post', array( $this, 'logger_in_action' ) ); | ||
} | ||
|
||
public function logger_in_action( $post_id ) { | ||
if ( ! wp_is_post_revision( $post_id ) ) | ||
return; | ||
|
||
foreach ( Factory::get_loggers() as $logger ) { | ||
try { | ||
$instance = Factory::get_instance( $logger ); | ||
$instance->do_logging( get_post( $post_id ) ); | ||
} catch (\Exception $e) { | ||
error_log( $e->getMessage() ); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
add_action( 'plugins_loaded', function() { | ||
new WP_Plugin(); | ||
} ); |