Making WordPress.org

Ticket #212: 212.diff

File 212.diff, 3.1 KB (added by iandunn, 12 years ago)
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-organizer-reminders/wordcamp-organizer-reminders.php

     
     1<?php
     2
     3/*
     4 * Plugin Name: WordCamp Organizer Reminders
     5 * Description: Automatically e-mail WordCamp organizers with various reminders at specified intervals.
     6 * Version:     0.1
     7 * Author:      Ian Dunn
     8 */
     9
     10class WordCampOrganizerReminders {
     11        var $emails = array(
     12                // The index is the number of days before the event that the e-mail should be sent
     13                3 => array(
     14                        'subject' => 'Camera Kits',
     15                        'body'    => 'Did you remember to do something with the camera kits?'
     16                ),
     17               
     18                4 => array(
     19                        'subject' => 'Venue Insurance',
     20                        'body'    => 'Did you remember to get venue insurance?'
     21                )
     22        );
     23
     24        /**
     25         * Constructor
     26         */
     27        public function __construct() {
     28                $this->emails = apply_filters( 'wcor_emails', $this->emails );
     29                add_action( 'wcor_send_emails', array( $this, 'send_emails' ) );
     30        }
     31
     32        /**
     33         * Schedule cron job when plugin is activated 
     34         */
     35        public function activate() {
     36                if ( wp_next_scheduled( 'wcor_send_emails' ) === false ) {
     37                        wp_schedule_event(
     38                                current_time( 'timestamp' ),
     39                                'daily',
     40                                'wcor_send_emails'
     41                        );
     42                }
     43        }
     44       
     45        /**
     46         * Clear cron job when plugin is deactivated
     47         */
     48        public function deactivate() {
     49                wp_clear_scheduled_hook( 'wcor_send_emails' );
     50        }
     51
     52        /**
     53         * Send the email reminders
     54         */
     55        public function send_emails() {
     56                $upcoming_wordcamps = get_posts( array(
     57                        'posts_per_page'  => -1,
     58                        'post_type'       => 'wordcamp',
     59                        'meta_query'      => array(
     60                                array(
     61                                        'key'     => 'Start Date (YYYY-mm-dd)',
     62                                        'value'   => current_time( 'timestamp' ),
     63                                        'compare' => '>=',
     64                                )
     65                        )
     66                ) );
     67               
     68                foreach( $upcoming_wordcamps as $wordcamp ) {
     69                        $organizers_email          = get_post_meta( $wordcamp->ID, 'E-mail Address', true );
     70                        $number_of_days_until_camp = floor( ( get_post_meta( $wordcamp->ID, 'Start Date (YYYY-mm-dd)', true ) - current_time( 'timestamp' ) ) / DAY_IN_SECONDS );
     71                       
     72                        if ( isset( $this->emails[ $number_of_days_until_camp ] ) && is_email( $organizers_email ) ) {
     73                                wp_mail(
     74                                        $organizers_email,
     75                                        $wordcamp->post_title .' Reminder: ' . $this->emails[ $number_of_days_until_camp ]['subject'],
     76                                        $this->emails[ $number_of_days_until_camp ]['body']
     77                                );
     78                               
     79                                sleep( 1 ); // don't send e-mails too fast, or it might increase the risk of being flagged as spam
     80                        }
     81                }
     82        }
     83}
     84
     85$GLOBALS['WordCampOrganizerReminders'] = new WordCampOrganizerReminders();
     86register_activation_hook(   __FILE__, array( $GLOBALS['WordCampOrganizerReminders'], 'activate' ) );
     87register_deactivation_hook( __FILE__, array( $GLOBALS['WordCampOrganizerReminders'], 'deactivate' ) );
     88 No newline at end of file