<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[2251] 2013/codebykat/post-by-email/trunk: Use wp_cron to schedule hourly mail checks</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://gsoc.trac.wordpress.org/changeset/2251">2251</a></dd>
<dt>Author</dt> <dd>codebykat</dd>
<dt>Date</dt> <dd>2013-08-29 07:52:09 +0000 (Thu, 29 Aug 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Use wp_cron to schedule hourly mail checks</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#2013codebykatpostbyemailtrunkclasspostbyemailphp">2013/codebykat/post-by-email/trunk/class-post-by-email.php</a></li>
<li><a href="#2013codebykatpostbyemailtrunkpostbyemailphp">2013/codebykat/post-by-email/trunk/post-by-email.php</a></li>
<li><a href="#2013codebykatpostbyemailtrunkreadmemd">2013/codebykat/post-by-email/trunk/readme.md</a></li>
<li><a href="#2013codebykatpostbyemailtrunkreadmetxt">2013/codebykat/post-by-email/trunk/readme.txt</a></li>
<li><a href="#2013codebykatpostbyemailtrunkteststestpostbyemailphp">2013/codebykat/post-by-email/trunk/tests/test-post-by-email.php</a></li>
<li><a href="#2013codebykatpostbyemailtrunkviewsadminphp">2013/codebykat/post-by-email/trunk/views/admin.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="2013codebykatpostbyemailtrunkclasspostbyemailphp"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/class-post-by-email.php (2250 => 2251)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/class-post-by-email.php       2013-08-28 23:11:18 UTC (rev 2250)
+++ 2013/codebykat/post-by-email/trunk/class-post-by-email.php  2013-08-29 07:52:09 UTC (rev 2251)
</span><span class="lines">@@ -52,11 +52,26 @@
</span><span class="cx">   *
</span><span class="cx">   * @since    0.9.7
</span><span class="cx">   *
</span><del>-        * @var      object
</del><ins>+         * @var      string
</ins><span class="cx">    */
</span><span class="cx">  public static $path;
</span><span class="cx"> 
</span><span class="cx">  /**
</span><ins>+        * Default settings.
+        *
+        * @since    0.9.8
+        *
+        * @var      array
+        */
+       protected $default_options = array(
+               'mailserver_url' => 'mail.example.com',
+               'mailserver_login' => 'login@example.com',
+               'mailserver_pass' => 'password',
+               'mailserver_port' => 110,
+               'default_email_category' => '');
+
+
+       /**
</ins><span class="cx">    * Initialize the plugin by setting localization, filters, and administration functions.
</span><span class="cx">   *
</span><span class="cx">   * @since     0.9.0
</span><span class="lines">@@ -68,8 +83,9 @@
</span><span class="cx">          // Enable autoloading
</span><span class="cx">          add_action( 'plugins_loaded', array( $this, 'load' ) );
</span><span class="cx"> 
</span><del>-               // add hook to check for mail
-               add_action( 'wp-mail.php', array( 'Post_By_Email', 'check_email' ) );
</del><ins>+                // add hooks to check for mail
+               add_action( 'wp-mail.php', array( $this, 'check_email' ) );
+               add_action( 'post-by-email-wp-mail.php', array( $this, 'check_email' ) );
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><span class="lines">@@ -101,35 +117,40 @@
</span><span class="cx">          $options = get_option( 'post_by_email_options' );
</span><span class="cx"> 
</span><span class="cx">          if( ! $options ) {
</span><del>-                       $options = array();
</del><ins>+                        $options = $this->default_options;
</ins><span class="cx"> 
</span><span class="cx">                  // if old global options exist, copy them into plugin options
</span><del>-                       // WP_MAIL_INTERVAL - interval to check new messages
-
-                       $plugin_options = array(
-                               'mailserver_url',
-                               'mailserver_port',
-                               'mailserver_login',
-                               'mailserver_pass',
-                               'default_email_category'
-                       );
-
-                       foreach( $plugin_options as $optname ) {
-                               $options[ $optname ] = get_option( $optname );
-                               //delete_option( $optname );                    
</del><ins>+                        foreach( array_keys( $this->default_options ) as $optname ) {
+                               if( get_option( $optname ) ) {
+                                       $options[ $optname ] = get_option( $optname );
+                                       //delete_option( $optname );                    
+                               }
</ins><span class="cx">                   }
</span><span class="cx"> 
</span><span class="cx">                  update_option( 'post_by_email_options', $options );
</span><span class="cx">          }
</span><ins>+
+               // schedule hourly mail checks with wp_cron
+               if( ! wp_next_scheduled( 'post-by-email-wp-mail.php' ) ) {
+                       wp_schedule_event( current_time( 'timestamp', 1 ), 'hourly', 'post-by-email-wp-mail.php' );
+               }
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><ins>+       * Fired when the plugin is deactivated.
+       *
+       * @since    0.9.8
+       */
+       function deactivate() {
+               wp_clear_scheduled_hook( 'post-by-email-wp-mail.php' );
+       }
+
+       /**
</ins><span class="cx">    * Load the plugin text domain for translation.
</span><span class="cx">   *
</span><span class="cx">   * @since    0.9.0
</span><span class="cx">   */
</span><span class="cx">  public function load_plugin_textdomain() {
</span><del>-
</del><span class="cx">           $domain = $this->plugin_slug;
</span><span class="cx">          $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
</span><span class="cx"> 
</span><span class="lines">@@ -137,18 +158,13 @@
</span><span class="cx">          load_plugin_textdomain( $domain, FALSE, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-
</del><span class="cx">   /**
</span><span class="cx">   * Check for new messages and post them to the blog.
</span><span class="cx">   *
</span><span class="cx">   * @since    0.9.0
</span><span class="cx">   */
</span><span class="cx">  public function check_email() {
</span><del>-
-               /** include the Horde IMAP client class */
-               // require_once( plugin_dir_path( __FILE__ ) . 'include/horde-wrapper.php' );
-
-               /** Only check at this interval for new messages. */
</del><ins>+                // Only check at this interval for new messages.
</ins><span class="cx">           if ( ! defined( 'WP_MAIL_INTERVAL' ) )
</span><span class="cx">                  define( 'WP_MAIL_INTERVAL', 5 * MINUTE_IN_SECONDS );
</span><span class="cx"> 
</span><span class="lines">@@ -160,10 +176,17 @@
</span><span class="cx">          set_transient( 'mailserver_last_checked', true, WP_MAIL_INTERVAL );
</span><span class="cx"> 
</span><span class="cx">          $options = get_option( 'post_by_email_options' );
</span><del>-               /* TODO validate that options are set */
</del><span class="cx"> 
</span><ins>+               // if options aren't set, there's nothing to do, move along
+               foreach( array( 'mailserver_url', 'mailserver_login', 'mailserver_pass' ) as $optname ) {
+                       if( ! $options[$optname] || $options[$optname] == $this->default_options[$optname] ) {
+                               // TODO logging
+                               return;
+                       }
+               }
+
</ins><span class="cx">           $log = array();
</span><del>-               $log['last_checked'] = current_time( 'mysql' );
</del><ins>+                $log['last_checked'] = current_time( 'timestamp' );
</ins><span class="cx">           $log['messages'] = array();
</span><span class="cx"> 
</span><span class="cx">          $time_difference = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
</span></span></pre></div>
<a id="2013codebykatpostbyemailtrunkpostbyemailphp"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/post-by-email.php (2250 => 2251)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/post-by-email.php     2013-08-28 23:11:18 UTC (rev 2250)
+++ 2013/codebykat/post-by-email/trunk/post-by-email.php        2013-08-29 07:52:09 UTC (rev 2251)
</span><span class="lines">@@ -11,8 +11,8 @@
</span><span class="cx">  * @wordpress-plugin
</span><span class="cx">  * Plugin Name: Post By Email
</span><span class="cx">  * Plugin URI:  http://codebykat.wordpress.com
</span><del>- * Description: Gets email messages from the user's mailbox to add as WordPress posts.
- * Version:     0.9.7
</del><ins>+ * Description: Create new posts on your WordPress blog by sending email to a specific email address.
+ * Version:     0.9.8
</ins><span class="cx">  * Author:      Kat Hagan
</span><span class="cx">  * Author URI:  http://profiles.wordpress.org/codebykat
</span><span class="cx">  * Text Domain: post-by-email
</span><span class="lines">@@ -40,6 +40,7 @@
</span><span class="cx"> require_once( plugin_dir_path( __FILE__ ) . 'class-post-by-email.php' );
</span><span class="cx"> 
</span><span class="cx"> register_activation_hook( __FILE__, array( 'Post_By_Email', 'activate' ) );
</span><ins>+register_deactivation_hook( __FILE__, array( 'Post_By_Email', 'deactivate' ) );
</ins><span class="cx"> 
</span><span class="cx"> if( is_admin() ) {
</span><span class="cx">     require_once( plugin_dir_path( __FILE__ ) . 'class-post-by-email-admin.php' );
</span></span></pre></div>
<a id="2013codebykatpostbyemailtrunkreadmemd"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/readme.md (2250 => 2251)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/readme.md     2013-08-28 23:11:18 UTC (rev 2250)
+++ 2013/codebykat/post-by-email/trunk/readme.md        2013-08-29 07:52:09 UTC (rev 2251)
</span><span class="lines">@@ -29,8 +29,21 @@
</span><span class="cx"> 1. Activate the plugin through the 'Plugins' menu.
</span><span class="cx"> 1. Configure mailbox information under plugin settings.
</span><span class="cx"> 
</span><ins>+
+## Tests ##
+
+Like WordPress Core, this plugin includes unit tests written in PHPUnit.
+
+To run the tests:
+
+**1. Set up the WordPress testing library as described in [Handbook:** Automated Testing](http://make.wordpress.org/core/handbook/automated-testing/).  You can also do this [using WP-CLI](http://wp-cli.org/blog/plugin-unit-tests.html).  
+1. From the plugin directory, run `WP_TESTS_DIR=/path/to/WordPress/test/install phpunit`
+
</ins><span class="cx"> ## Changelog ##
</span><span class="cx"> 
</span><ins>+### 0.9.8 ###
+* Use wp_cron to schedule hourly mail checks.
+
</ins><span class="cx"> ### 0.9.7 ###
</span><span class="cx"> * Refactored Horde includes to autoload class files as needed.
</span><span class="cx"> 
</span></span></pre></div>
<a id="2013codebykatpostbyemailtrunkreadmetxt"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/readme.txt (2250 => 2251)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/readme.txt    2013-08-28 23:11:18 UTC (rev 2250)
+++ 2013/codebykat/post-by-email/trunk/readme.txt       2013-08-29 07:52:09 UTC (rev 2251)
</span><span class="lines">@@ -29,8 +29,21 @@
</span><span class="cx"> 1. Activate the plugin through the 'Plugins' menu.
</span><span class="cx"> 1. Configure mailbox information under plugin settings.
</span><span class="cx"> 
</span><ins>+
+== Tests ==
+
+Like WordPress Core, this plugin includes unit tests written in PHPUnit.
+
+To run the tests:
+
+1. Set up the WordPress testing library as described in [Handbook: Automated Testing](http://make.wordpress.org/core/handbook/automated-testing/).  You can also do this [using WP-CLI](http://wp-cli.org/blog/plugin-unit-tests.html).
+1. From the plugin directory, run `WP_TESTS_DIR=/path/to/WordPress/test/install phpunit`
+
</ins><span class="cx"> == Changelog ==
</span><span class="cx"> 
</span><ins>+= 0.9.8 =
+* Use wp_cron to schedule hourly mail checks.
+
</ins><span class="cx"> = 0.9.7 =
</span><span class="cx"> * Refactored Horde includes to autoload class files as needed.
</span><span class="cx"> 
</span></span></pre></div>
<a id="2013codebykatpostbyemailtrunkteststestpostbyemailphp"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/tests/test-post-by-email.php (2250 => 2251)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/tests/test-post-by-email.php  2013-08-28 23:11:18 UTC (rev 2250)
+++ 2013/codebykat/post-by-email/trunk/tests/test-post-by-email.php     2013-08-29 07:52:09 UTC (rev 2251)
</span><span class="lines">@@ -10,8 +10,15 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  function test_plugin_activation() {
</span><ins>+               // with no preexisting options and no global ones, use defaults
+               delete_option( 'post_by_email_options' );
+               delete_option( 'mailserver_url' );
+               $this->plugin->activate( false );
</ins><span class="cx"> 
</span><del>-               // with no preexisting options, we should copy over the global ones
</del><ins>+                $options = get_option( 'post_by_email_options' );
+               $this->assertEquals( 'mail.example.com', $options['mailserver_url'] );
+
+               // copy over the global options if they exist
</ins><span class="cx">           delete_option( 'post_by_email_options' );
</span><span class="cx">          update_option( 'mailserver_url', 'testing.example.com' );
</span><span class="cx">          $this->plugin->activate( false );
</span><span class="lines">@@ -20,13 +27,23 @@
</span><span class="cx">          $options = get_option( 'post_by_email_options' );
</span><span class="cx">          $this->assertEquals( 'testing.example.com', $options['mailserver_url'] );
</span><span class="cx"> 
</span><del>-               // when we have preexisting options, we should use those
</del><ins>+                // when we have preexisting options, those should take precedence
</ins><span class="cx">           update_option( 'mailserver_url', 'another.example.com' );
</span><span class="cx">          $this->plugin->activate( false );
</span><span class="cx">          $options = get_option( 'post_by_email_options' );
</span><del>-               $this->assertNotEquals( 'another.example.com', $options['mailserver_url'] );         
</del><ins>+                $this->assertNotEquals( 'another.example.com', $options['mailserver_url'] );
</ins><span class="cx">   }
</span><span class="cx"> 
</span><ins>+       function test_wp_cron_setup() {
+               // plugin activation should schedule an event with wp_cron
+               $this->plugin->activate( false );
+               $this->assertNotEquals( false, wp_next_scheduled( 'post-by-email-wp-mail.php' ) );
+
+               // plugin deactivation should remove wp_cron scheduled event
+               $this->plugin->deactivate();
+               $this->assertFalse( wp_next_scheduled( 'post-by-email-wp-mail.php' ) );
+       }
+
</ins><span class="cx">   function test_check_email() {
</span><span class="cx">          $this->markTestIncomplete();
</span><span class="cx">  }
</span></span></pre></div>
<a id="2013codebykatpostbyemailtrunkviewsadminphp"></a>
<div class="modfile"><h4>Modified: 2013/codebykat/post-by-email/trunk/views/admin.php (2250 => 2251)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/codebykat/post-by-email/trunk/views/admin.php       2013-08-28 23:11:18 UTC (rev 2250)
+++ 2013/codebykat/post-by-email/trunk/views/admin.php  2013-08-29 07:52:09 UTC (rev 2251)
</span><span class="lines">@@ -102,14 +102,28 @@
</span><span class="cx">          }
</span><span class="cx">  ?>
</span><span class="cx">  <p>
</span><del>-               <?php _e( 'Last checked:', 'post-by-email' ) ?>
-               <?php echo $log ? $log['last_checked'] : __( 'Never', 'post-by-email' ); ?>
</del><ins>+                <?php _e( 'Last checked for new mail:', 'post-by-email' ) ?>
+               <?php
+                       $date_format = get_option( 'date_format' );
+                       $time_format = get_option( 'time_format' );
+               ?>
+               <?php if($log) : ?>
+                       <?php echo date_i18n( "$date_format, $time_format", $log['last_checked'] ); ?>
+               <?php else: ?>
+                       <?php _e( 'Never', 'post-by-email' ); ?>
+               <?php endif; ?>
+               <br />
+               <?php _e( 'Next scheduled check:', 'post-by-email' ) ?>
+               <?php
+                       $next = wp_next_scheduled( 'post-by-email-wp-mail.php' );
+                       echo get_date_from_gmt( date( 'Y-m-d H:i:s', $next ) , "$date_format, $time_format");
+               ?>
</ins><span class="cx">   </p>
</span><ins>+       <p><a href="<?php echo site_url('wp-mail.php'); ?>"><?php _e( 'Check now', 'post-by-email' ) ?></a></p>
</ins><span class="cx">   <?php if( $log['messages'] ) : ?>
</span><del>-               <?php _e( 'And the plugin had this to say about it:', 'post-by-email' ); ?>
</del><ins>+                <h3><?php _e( 'Log Messages', 'post-by-email' ); ?></h3>
</ins><span class="cx">           <?php foreach($log['messages'] as $message) : ?>
</span><span class="cx">                  <li><?php echo $message; ?></li>
</span><span class="cx">          <?php endforeach; ?>
</span><span class="cx">  <?php endif; ?>
</span><del>-       <p><a href="<?php echo site_url('wp-mail.php'); ?>"><?php _e( 'Check now', 'post-by-email' ) ?></a></p>
</del><span class="cx"> </div>
</span><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>

</body>
</html>