Plugin Directory

Changeset 1047426


Ignore:
Timestamp:
12/17/2014 07:24:35 PM (11 years ago)
Author:
freakingid
Message:

Store datetime in UTC

Location:
content-scheduler/trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • content-scheduler/trunk/content-scheduler-settings.php

    r1047425 r1047426  
    55    exit();
    66}
     7
     8define( 'PEK_CONTENT_SCHEDULER_VERSION', '2.0.0' );
    79
    810if ( !class_exists( "Content_Scheduler_Settings" ) ) {
     
    588590        function draw_plugin_version()
    589591        {
    590             $this->options = get_option('ContentScheduler_Options');
    591             echo "<p>$this->options[version]</p>\n";
     592            echo "<p>" . $this->options['version'] . "</p>\n";
    592593        } // end draw_plugin_version()
    593594
  • content-scheduler/trunk/content-scheduler.php

    r1047425 r1047426  
    185185            if( is_array( $this->options ) )
    186186            {
     187                // If version is older than 2.0, we need to change the way we store expiration date metadata
     188                if( !isset( $this->options['version'] ) || $this->options['version'] < '2.0.0' )
     189                {
     190                    include 'includes/update-postmeta-expiration-values.php';
     191                }
    187192                // If version newer than 0.9.7, we need to alter the name of our postmeta variables if there are earlier version settings in options
    188193                if( !isset( $this->options['version'] ) || $this->options['version'] < '0.9.7' )
     
    238243                // We previously used content_scheduler_(blogid)
    239244                // http://codex.wordpress.org/Function_Reference/wp_schedule_event
     245                // TODO time() is UTC for right now, and that is okay
    240246                wp_schedule_event( time(), 'contsched_usertime', 'contentscheduler' );
    241247                // wp_schedule_event( time(), 'hourly', 'content_scheduler_'.$current_blog_id );
     
    393399            echo "<br />\n<br />\n";
    394400            // Field for datetime of expiration
    395             $datestring = ( get_post_meta( $post->ID, '_cs-expire-date', true) );
     401            // TODO datetime conversion
     402            // should be unix timestamp at this point, in UTC
     403            // for display, we need to convert this to local time and then format
     404           
     405            // datestring is the original human-readable form
     406            // $datestring = ( get_post_meta( $post->ID, '_cs-expire-date', true) );
     407            // timestamp should just be a unix timestamp
     408            $timestamp = ( get_post_meta( $post->ID, '_cs-expire-date', true) );
     409            if( !empty( $timestamp ) ) {
     410                // we need to convert that into human readable so we can put it into our field
     411                $datestring = $this->getReadableDateFromTimestamp( $timestamp );
     412            } else {
     413                $datestring = '';
     414            }
    396415            // Should we check for format of the date string? (not doing that presently)
    397416            echo '<label for="cs-expire-date">' . __("Expiration date and hour", 'contentscheduler' ) . '</label><br />';
     
    435454            // OK, we're authenticated: we need to find and save the data
    436455            // First, let's make sure we'll do date operations in the right timezone for this blog
    437             $this->setup_timezone();
     456            // $this->setup_timezone();
    438457            // Checkbox for "enable scheduling"
    439458            $enabled = ( empty( $_POST['_cs-enable-schedule'] ) ? 'Disable' : $_POST['_cs-enable-schedule'] );
     
    448467            }
    449468            // Textbox for "expiration date"
    450             $date = $_POST['_cs-expire-date'];
     469            $dateString = $_POST['_cs-expire-date'];
     470            $offsetHours = 0;
     471            // if it is empty then set it to tomorrow
     472            // we just want to pass an offset into getTimestampFromReadableDate since that is where our DateTime is made
     473            if( empty( $dateString ) ) {
     474                // set it to now + 24 hours
     475                $offsetHours = 24;
     476            }
     477            // TODO handle datemath if field reads "default"
    451478            if( strtolower( $date ) == 'default' )
    452479            {
     
    506533            else
    507534            {
    508                 // How can we check a myriad of date formats??
    509                 // Right now we are mm/dd/yyyy
    510                 if( ! $this->check_date_format( $date ) )
    511                 {
    512                     // It was not a valid date format
    513                     // Normally, we would set to ''
    514                     $date = '';
    515                     // For debug, we will set to 'INVALID'
    516                     // $date = 'INVALID';
    517                 }
     535                // a. Take human-readable date and time from the field,
     536                // b. turn it into a local timestamp
     537                // c. turn that into a utc timestamp
     538                // d. store it. into $date before saving
     539                $date = $this->getTimestampFromReadableDate( $dateString, $offsetHours );
    518540            }
    519541            // We probably need to store the date differently,
     
    758780                        $query = "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = \"_cs-expire-date\" AND post_id=$id";
    759781                        // get the single returned value (can do this better?)
    760                         $ed = $wpdb->get_var($query);
     782                        // $ed = $wpdb->get_var($query);
     783                        $timestamp = $wpdb->get_var($query);
     784                        if( !empty( $timestamp ) ) {
     785                            // convert
     786                            $ed = $this->getReadableDateFromTimestamp( $timestamp );
     787                        } else {
     788                            $ed = "Date misunderstood";
     789                        }
    761790                        // determine whether expiration is enabled or disabled
    762791                        if( get_post_meta( $post->ID, '_cs-enable-schedule', true) != 'Enable' )
     
    801830            // else - continue
    802831            // get the expiration timestamp
    803             $expirationdt = get_post_meta( $post->ID, '_cs-expire-date', true );
    804             if ( empty( $expirationdt ) )
     832            $timestamp = get_post_meta( $post->ID, '_cs-expire-date', true );
     833            if ( empty( $timestamp ) )
    805834            {
    806835                return false;
    807             }
     836            } else {
     837                $expirationdt = $this->getReadableDateFromTimestamp( $timestamp );
     838            }
     839
    808840            // We'll need the following if / when we allow formatting of the timestamp
    809841            /*
     
    867899        // handle timezones
    868900        function setup_timezone() {
     901            error_log( __FILE__ . " :: " . __FUNCTION__ . " was called, but we want to stop using it." );
     902            /*
    869903            if ( ! $wp_timezone = get_option( 'timezone_string' ) )
    870904            {
     
    874908            // Set the default timezone used by Content Scheduler
    875909            date_default_timezone_set( $wp_timezone );
     910            */
    876911        }
    877     } // end ContentScheduler Class
     912
     913        // TODO: Pull these out into a static class so they can be used in multiple places
     914        /*
     915            unixTimestamp       timestamp NOT adjusted for WordPress local time
     916            return something date and time as one string following WP site formatting settings
     917        */
     918        function getReadableDateFromTimestamp( $unixTimestamp ) {
     919            // get datetime object from unix timestamp
     920            $datetime = new DateTime( "@$unixTimestamp", new DateTimeZone( 'UTC' ) );
     921            // set the timezone to the site timezone
     922            $datetime->setTimezone( new DateTimeZone( $this->wp_get_timezone_string() ) );
     923            // return the unix timestamp adjusted to reflect the site's timezone
     924            // return $timestamp + $datetime->getOffset();
     925            $localTimestamp = $unixTimestamp + $datetime->getOffset();
     926            $dateString = date_i18n( get_option( 'date_format' ), $localTimestamp );
     927            $timeString = date( get_option( 'time_format' ), $localTimestamp );
     928            // put together and return
     929            return $dateString . " " . $timeString;
     930        }
     931        /*
     932            dateSTring      readalbe date / time string from user input field
     933            offsetHours     hours to add / remove from dateString-generated DateTime
     934            return          unit timestamp in UTC time (i.e., not 'local' time)
     935        */
     936        function getTimestampFromReadableDate( $dateString, $offsetHours = 0 ) {
     937            // get datetime object from site timezone
     938            $datetime = new DateTime( $dateString, new DateTimeZone( $this->wp_get_timezone_string() ) );
     939            // add the offsetHours
     940            // $date->add(new DateInterval('P10D'));
     941            $datetime->add( new DateInterval( "PT".$offsetHours."H" ) );
     942            // get the unix timestamp (adjusted for the site's timezone already)
     943            $timestamp = $datetime->format( 'U' );
     944            return $timestamp;   
     945        }
     946        /**
     947         * Returns the timezone string for a site, even if it's set to a UTC offset
     948         *
     949         * Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#89155
     950         *
     951         * @return string valid PHP timezone string
     952         */
     953        function wp_get_timezone_string() {
     954            // if site timezone string exists, return it
     955            if ( $timezone = get_option( 'timezone_string' ) )
     956                return $timezone;
     957            // get UTC offset, if it isn't set then return UTC
     958            if ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) )
     959                return 'UTC';
     960            // adjust UTC offset from hours to seconds
     961            $utc_offset *= 3600;
     962            // attempt to guess the timezone string from the UTC offset
     963            if ( $timezone = timezone_name_from_abbr( '', $utc_offset, 0 ) ) {
     964                return $timezone;
     965            }
     966            // last try, guess timezone string manually
     967            $is_dst = date( 'I' );
     968            foreach ( timezone_abbreviations_list() as $abbr ) {
     969                foreach ( $abbr as $city ) {
     970                    if ( $city['dst'] == $is_dst && $city['offset'] == $utc_offset )
     971                        return $city['timezone_id'];
     972                }
     973            }
     974            // fallback to UTC
     975            return 'UTC';
     976        }
     977} // end ContentScheduler Class
    878978} // End IF Class ContentScheduler
    879979
  • content-scheduler/trunk/includes/process-expirations.php

    r1047425 r1047426  
    22// find posts that need to take some expiration action
    33            global $wpdb;
    4             // $options = get_option('ContentScheduler_Options');
    5             // tell PHP to use wp-settings timezone
    6             $this->setup_timezone();
    74            // select all Posts / Pages that have "enable-expiration" set and have expiration date older than right now
    8             // 12/8/2010 7:18:08 PM
    9             // Original has expiration date in results -- differing from process_notifications and causing problems.
    10             // $querystring = 'SELECT postmetadate.post_id, postmetadate.meta_value AS expiration
    115            $querystring = 'SELECT postmetadate.post_id
    126                FROM
     
    1711                AND postmetadoit.meta_value = "Enable"
    1812                AND postmetadate.meta_key = "_cs-expire-date"
    19                 AND postmetadate.meta_value <= "' . date("Y-m-d H:i:s") . '"
     13                AND postmetadate.meta_value <= "' . time() . '"
    2014                AND postmetadate.post_id = postmetadoit.post_id
    2115                AND postmetadate.post_id = posts.ID
  • content-scheduler/trunk/includes/process-notifications.php

    r1047425 r1047426  
    55            // get days of notification offset
    66            $notice_days = $this->options['notify-before'];
    7             // setup timezone
    8             $this->setup_timezone();
    97            // find posts that need to send notifications
     8            // this doesn't seem right. unix timestamp plus noticedays in seconds? NO
     9            // I would think it would be ... hmm, I don't know
     10            // anyway
     11            // TODO
     12            // dates need to be relavant to unix timestamp (UTC)
     13            //              AND postmetadate.meta_value <= "' . time() . '"
     14
    1015            $notification_timestamp = time() + ($notice_days * 24 * 60 * 60);
    1116            $notification_string = date( "Y-m-d H:i:s", $notification_timestamp );
  • content-scheduler/trunk/includes/send-notifications.php

    r1047425 r1047426  
    6868                // $post_edit_url = "Fake Post Editing URL";
    6969                // get the post expiration date
    70                 $post_expiration_date = ( get_post_meta( $post_data['ID'], '_cs-expire-date', true) );
     70                // $post_expiration_date = ( get_post_meta( $post_data['ID'], '_cs-expire-date', true) );
     71                $post_expiration_date_timestamp = ( get_post_meta( $post_data['ID'], '_cs-expire-date', true) );
     72                $post_expiration_date = $this->getReadableDateFromTimestamp( $post_expiration_date_timestamp );
     73               
    7174                // pack it up into our array
    7275                // make a new item array
Note: See TracChangeset for help on using the changeset viewer.