diff --git a/abstract-factory/post-logger/post-logger.php b/abstract-factory/post-logger/post-logger.php new file mode 100644 index 0000000..550b238 --- /dev/null +++ b/abstract-factory/post-logger/post-logger.php @@ -0,0 +1,101 @@ +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(); +} );