diff --git a/includes/admin.php b/includes/admin.php index 62c8f27ea..8e2d49eb8 100644 --- a/includes/admin.php +++ b/includes/admin.php @@ -49,7 +49,7 @@ public static function load() { add_action( 'wp_ajax_wp_stream_uninstall', array( __CLASS__, 'uninstall_plugin' ) ); // Auto purge setup - add_action( 'init', array( __CLASS__, 'purge_schedule_setup' ) ); + add_action( 'wp', array( __CLASS__, 'purge_schedule_setup' ) ); add_action( 'wp_stream_auto_purge', array( __CLASS__, 'purge_scheduled_action' ) ); // Admin notices @@ -171,6 +171,7 @@ public static function admin_enqueue_scripts( $hook ) { 'confirm_purge' => __( 'Are you sure you want to delete all Stream activity records from the database? This cannot be undone.', 'stream' ), 'confirm_uninstall' => __( 'Are you sure you want to uninstall and deactivate Stream? This will delete all Stream tables from the database and cannot be undone.', 'stream' ), ), + 'gmt_offset' => get_option( 'gmt_offset' ), 'current_screen' => $hook, 'current_page' => isset( $_GET['paged'] ) ? esc_js( $_GET['paged'] ) : '1', 'current_order' => isset( $_GET['order'] ) ? esc_js( $_GET['order'] ) : 'desc', @@ -416,30 +417,31 @@ public static function uninstall_plugin(){ } public static function purge_schedule_setup() { - if ( ! wp_next_scheduled( 'stream_auto_purge' ) ) { - wp_schedule_event( time(), 'daily', 'stream_auto_purge' ); + if ( ! wp_next_scheduled( 'wp_stream_auto_purge' ) ) { + wp_schedule_event( time(), 'daily', 'wp_stream_auto_purge' ); } } public static function purge_scheduled_action() { global $wpdb; - $days = WP_Stream_Settings::$options['general_records_ttl']; + $options = WP_Stream_Settings::get_options(); + + $days = $options['general_records_ttl']; $date = new DateTime( 'now', $timezone = new DateTimeZone( 'UTC' ) ); $date->sub( DateInterval::createFromDateString( "$days days" ) ); $wpdb->query( $wpdb->prepare( - " - DELETE t1, t2, t3 - FROM {$wpdb->stream} as t1 - INNER JOIN {$wpdb->streamcontext} as t2 - INNER JOIN {$wpdb->streammeta} as t3 - WHERE t1.type = 'stream' - AND t1.created < %s - AND t1.ID = t2.record_id - AND t1.ID = t3.record_id; - ", + "DELETE `stream`, `context`, `meta` + FROM {$wpdb->stream} AS `stream` + LEFT JOIN {$wpdb->streamcontext} AS `context` + ON `context`.`record_id` = `stream`.`ID` + LEFT JOIN {$wpdb->streammeta} AS `meta` + ON `meta`.`record_id` = `stream`.`ID` + WHERE `stream`.`type` = %s + AND `stream`.`created` < %s;", + 'stream', $date->format( 'Y-m-d H:i:s' ) ) ); diff --git a/includes/date-interval.php b/includes/date-interval.php index 8d3be01d0..3663ac413 100644 --- a/includes/date-interval.php +++ b/includes/date-interval.php @@ -31,67 +31,82 @@ public function __construct() { * @return mixed|void */ public function get_predefined_intervals() { + $timezone = get_option( 'timezone_string' ); + + if ( empty( $timezone ) ) { + $gmt_offset = (int) get_option( 'gmt_offset' ); + $timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, true ); + if ( false === $timezone ) { + $timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, false ); + } + if ( false === $timezone ) { + $timezone = null; + } + } + return apply_filters( 'wp_stream_predefined_date_intervals', array( 'today' => array( 'label' => esc_html__( 'Today', 'stream' ), - 'start' => Carbon::today(), + 'start' => Carbon::today( $timezone )->startOfDay(), + 'end' => Carbon::today( $timezone )->startOfDay(), ), 'yesterday' => array( 'label' => esc_html__( 'Yesterday', 'stream' ), - 'start' => Carbon::today()->subDay(), - 'end' => Carbon::today()->subSecond(), + 'start' => Carbon::today( $timezone )->startOfDay()->subDay(), + 'end' => Carbon::today( $timezone )->startOfDay()->subSecond(), ), 'last-7-days' => array( 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ), - 'start' => Carbon::today()->subDays( 7 ), - 'end' => Carbon::today(), + 'start' => Carbon::today( $timezone )->subDays( 7 ), + 'end' => Carbon::today( $timezone ), ), 'last-14-days' => array( 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ), - 'start' => Carbon::today()->subDays( 14 ), - 'end' => Carbon::today(), + 'start' => Carbon::today( $timezone )->subDays( 14 ), + 'end' => Carbon::today( $timezone ), ), 'last-30-days' => array( 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ), - 'start' => Carbon::today()->subDays( 30 ), - 'end' => Carbon::today(), + 'start' => Carbon::today( $timezone )->subDays( 30 ), + 'end' => Carbon::today( $timezone ), ), 'this-month' => array( 'label' => esc_html__( 'This Month', 'stream' ), - 'start' => Carbon::today()->day( 1 ), + 'start' => Carbon::today( $timezone )->day( 1 ), ), 'last-month' => array( 'label' => esc_html__( 'Last Month', 'stream' ), - 'start' => Carbon::today()->day( 1 )->subMonth(), - 'end' => Carbon::today()->day( 1 )->subSecond(), + 'start' => Carbon::today( $timezone )->day( 1 )->subMonth(), + 'end' => Carbon::today( $timezone )->day( 1 )->subSecond(), ), 'last-3-months' => array( 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ), - 'start' => Carbon::today()->subMonths( 3 ), - 'end' => Carbon::today(), + 'start' => Carbon::today( $timezone )->subMonths( 3 ), + 'end' => Carbon::today( $timezone ), ), 'last-6-months' => array( 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ), - 'start' => Carbon::today()->subMonths( 6 ), - 'end' => Carbon::today(), + 'start' => Carbon::today( $timezone )->subMonths( 6 ), + 'end' => Carbon::today( $timezone ), ), 'last-12-months' => array( 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ), - 'start' => Carbon::today()->subMonths( 12 ), - 'end' => Carbon::today(), + 'start' => Carbon::today( $timezone )->subMonths( 12 ), + 'end' => Carbon::today( $timezone ), ), 'this-year' => array( 'label' => esc_html__( 'This Year', 'stream' ), - 'start' => Carbon::today()->day( 1 )->month( 1 ), + 'start' => Carbon::today( $timezone )->day( 1 )->month( 1 ), ), 'last-year' => array( 'label' => esc_html__( 'Last Year', 'stream' ), - 'start' => Carbon::today()->day( 1 )->month( 1 )->subYear(), - 'end' => Carbon::today()->day( 1 )->month( 1 )->subSecond(), - ) - ) + 'start' => Carbon::today( $timezone )->day( 1 )->month( 1 )->subYear(), + 'end' => Carbon::today( $timezone )->day( 1 )->month( 1 )->subSecond(), + ), + ), + $timezone ); } diff --git a/includes/filters.php b/includes/filters.php index 027a84185..774b0a890 100644 --- a/includes/filters.php +++ b/includes/filters.php @@ -18,7 +18,6 @@ class WP_Stream_Filter_Input { FILTER_SANITIZE_NUMBER_FLOAT => 'floatval', FILTER_SANITIZE_NUMBER_INT => 'intval', FILTER_SANITIZE_SPECIAL_CHARS => 'htmlspecialchars', - FILTER_SANITIZE_FULL_SPECIAL_CHARS => 'htmlspecialchars', FILTER_SANITIZE_STRING => 'sanitize_text_field', FILTER_SANITIZE_URL => 'esc_url_raw', // Other diff --git a/includes/settings.php b/includes/settings.php index 5935d2cf7..f2e2ee3ed 100644 --- a/includes/settings.php +++ b/includes/settings.php @@ -26,29 +26,30 @@ class WP_Stream_Settings { */ public static $fields = array(); - /** - * Public constructor - * - * @return \WP_Stream_Settings - */ - public static function load() { - - // Parse field information gathering default values - $defaults = self::get_defaults(); - + public static function get_options() { /** * Filter allows for modification of options * * @param array array of options * @return array updated array of options */ - self::$options = apply_filters( + return apply_filters( 'wp_stream_options', wp_parse_args( (array) get_option( self::KEY, array() ), - $defaults + self::get_defaults() ) ); + } + + /** + * Public constructor + * + * @return \WP_Stream_Settings + */ + public static function load() { + + self::$options = self::get_options(); // Register settings, and fields add_action( 'admin_init', array( __CLASS__, 'register_settings' ) ); diff --git a/tests/tests/test-admin.php b/tests/tests/test-admin.php index ff7d04c2d..8ae8a417a 100644 --- a/tests/tests/test-admin.php +++ b/tests/tests/test-admin.php @@ -48,7 +48,7 @@ public function test_constructor() { array( 'admin_enqueue_scripts', self::CLASSNAME, 'admin_enqueue_scripts' ), array( 'admin_enqueue_scripts', self::CLASSNAME, 'admin_menu_css' ), array( 'wp_ajax_wp_stream_reset', self::CLASSNAME, 'wp_ajax_reset' ), - array( 'init', self::CLASSNAME, 'purge_schedule_setup' ), + array( 'wp', self::CLASSNAME, 'purge_schedule_setup' ), array( 'wp_stream_auto_purge', self::CLASSNAME, 'purge_scheduled_action' ), ); diff --git a/ui/admin.js b/ui/admin.js index f24f6220e..e78a3cd73 100644 --- a/ui/admin.js +++ b/ui/admin.js @@ -440,9 +440,28 @@ jQuery(function($){ if ( jQuery.datepicker ) { + // Apply a GMT offset due to Date() using the visitor's local time + var siteGMTOffsetHours = parseFloat( wp_stream.gmt_offset ); + var localGMTOffsetHours = new Date().getTimezoneOffset() / 60 * -1; + var totalGMTOffsetHours = siteGMTOffsetHours - localGMTOffsetHours; + + var localTime = new Date(); + var siteTime = new Date( localTime.getTime() + ( totalGMTOffsetHours * 60 * 60 * 1000 ) ); + var dayOffset = '0'; + + // check if the site date is different from the local date, and set a day offset + if ( localTime.getDate() !== siteTime.getDate() || localTime.getMonth() !== siteTime.getMonth() ) { + if ( localTime.getTime() < siteTime.getTime() ) { + dayOffset = '+1d'; + } else { + dayOffset = '-1d'; + } + } + datepickers.datepicker({ dateFormat: 'yy/mm/dd', - maxDate: 0, + maxDate: dayOffset, + defaultDate: siteTime, beforeShow: function() { $(this).prop( 'disabled', true ); },