diff --git a/classes/class-admin.php b/classes/class-admin.php index d137c3f6f..951bde081 100644 --- a/classes/class-admin.php +++ b/classes/class-admin.php @@ -125,11 +125,6 @@ public function __construct( $plugin ) { add_action( 'init', array( $this, 'init' ) ); - // Ensure function used in various methods is pre-loaded. - if ( ! function_exists( 'is_plugin_active_for_network' ) ) { - require_once ABSPATH . '/wp-admin/includes/plugin.php'; - } - // User and role caps. add_filter( 'user_has_cap', array( $this, 'filter_user_caps' ), 10, 4 ); add_filter( 'role_has_cap', array( $this, 'filter_role_caps' ), 10, 3 ); @@ -686,36 +681,17 @@ public function purge_schedule_setup() { public function purge_scheduled_action() { global $wpdb; - // Don't purge when in Network Admin unless Stream is network activated - if ( - is_multisite() - && - is_network_admin() - && - ! $this->plugin->is_network_activated() - ) { - return; - } - - if ( is_multisite() && $this->plugin->is_network_activated() ) { - $options = (array) get_site_option( 'wp_stream_network', array() ); - } else { - $options = (array) get_option( 'wp_stream', array() ); - } + $days = $this->plugin->settings->records_ttl(); - if ( ! empty( $options['general_keep_records_indefinitely'] ) || ! isset( $options['general_records_ttl'] ) ) { + // Ensure we don't want to keep the records indefinitely. + if ( $this->plugin->settings->keep_records_indefinitely() || empty( $days ) ) { return; } - $days = $options['general_records_ttl']; - $timezone = new DateTimeZone( 'UTC' ); - $date = new DateTime( 'now', $timezone ); - - $date->sub( DateInterval::createFromDateString( "$days days" ) ); - - $where = $wpdb->prepare( ' AND `stream`.`created` < %s', $date->format( 'Y-m-d H:i:s' ) ); + $timestamp = strtotime( sprintf( '%d days ago', $days ) ); + $where = $wpdb->prepare( ' AND `stream`.`created` < %s', gmdate( 'Y-m-d H:i:s', $timestamp ) ); - // Multisite but NOT network activated, only purge the current blog + // Multisite but NOT network activated, only purge the current blog. if ( is_multisite() && ! $this->plugin->is_network_activated() ) { $where .= $wpdb->prepare( ' AND `blog_id` = %d', get_current_blog_id() ); } diff --git a/classes/class-settings.php b/classes/class-settings.php index fa00b7b20..fe6197247 100644 --- a/classes/class-settings.php +++ b/classes/class-settings.php @@ -270,13 +270,7 @@ public function add_display_name_search_columns( $search_columns, $search, $quer public function get_option_key() { $option_key = $this->option_key; - $current_page = wp_stream_filter_input( INPUT_GET, 'page' ); - - if ( ! $current_page ) { - $current_page = wp_stream_filter_input( INPUT_GET, 'action' ); - } - - if ( 'wp_stream_network_settings' === $current_page ) { + if ( $this->plugin->is_network_activated() ) { $option_key = $this->network_options_key; } @@ -1167,4 +1161,38 @@ public function get_settings_translations( $labels ) { return $labels; } + + /** + * Get the record TTL sanitized. + * + * @return integer|null Returns the time-to-live in seconds or null if empty or not set. + */ + public function records_ttl() { + $options = $this->get_options(); + + if ( isset( $options['general_records_ttl'] ) ) { + $ttl = abs( $options['general_records_ttl'] ); + + if ( $ttl > 0 ) { + return $ttl; + } + } + + return null; + } + + /** + * Should the records be stored indefinitely. + * + * @return boolean + */ + public function keep_records_indefinitely() { + $options = $this->get_options(); + + if ( ! empty( $options['general_keep_records_indefinitely'] ) ) { + return true; + } + + return false; + } } diff --git a/tests/tests/test-class-admin.php b/tests/tests/test-class-admin.php index 8dce314c7..38c27a971 100644 --- a/tests/tests/test-class-admin.php +++ b/tests/tests/test-class-admin.php @@ -31,9 +31,7 @@ public function test_construct() { $this->assertNotEmpty( $this->admin->plugin ); $this->assertInstanceOf( '\WP_Stream\Plugin', $this->admin->plugin ); - $this->assertTrue( function_exists( 'is_plugin_active_for_network' ) ); - - if ( is_multisite() && is_plugin_active_for_network( $this->plugin->locations['plugin'] ) && ! is_network_admin() ) { + if ( is_multisite() && $this->admin->plugin->is_network_activated() && ! is_network_admin() ) { $this->assertTrue( $this->admin->disable_access ); } else { $this->assertFalse( $this->admin->disable_access ); @@ -268,8 +266,8 @@ public function test_purge_schedule_setup() { } public function test_purge_scheduled_action() { - // Set the TTL to one day - if ( is_multisite() && is_plugin_active_for_network( $this->plugin->locations['plugin'] ) ) { + // Set the TTL to one day. + if ( $this->admin->plugin->is_network_activated() ) { $options = (array) get_site_option( 'wp_stream_network', array() ); $options['general_records_ttl'] = '1'; update_site_option( 'wp_stream_network', $options ); @@ -281,29 +279,37 @@ public function test_purge_scheduled_action() { global $wpdb; - // Create (two day old) dummy records + // Create a fresh stream record that shouldn't be deleted. + $stream_data = $this->dummy_stream_data(); + $stream_data['created'] = date( 'Y-m-d h:i:s' ); + $wpdb->insert( $wpdb->stream, $stream_data ); + $fresh_stream_id = $wpdb->insert_id; + $this->assertNotFalse( $fresh_stream_id ); + + // Create (two day old) dummy records. $stream_data = $this->dummy_stream_data(); $stream_data['created'] = date( 'Y-m-d h:i:s', strtotime( '2 days ago' ) ); $wpdb->insert( $wpdb->stream, $stream_data ); $stream_id = $wpdb->insert_id; $this->assertNotFalse( $stream_id ); - // Create dummy meta + // Create dummy meta. $meta_data = $this->dummy_meta_data( $stream_id ); $wpdb->insert( $wpdb->streammeta, $meta_data ); $meta_id = $wpdb->insert_id; $this->assertNotFalse( $meta_id ); - // Purge old records and meta + // Purge old records and meta. $this->admin->purge_scheduled_action(); - // Check if the old records have been cleared + $fresh_stream_results = $wpdb->get_row( "SELECT * FROM {$wpdb->stream} WHERE ID = $fresh_stream_id" ); + $this->assertNotEmpty( $fresh_stream_results, 'fresh record from now is still there' ); + $stream_results = $wpdb->get_row( "SELECT * FROM {$wpdb->stream} WHERE ID = $stream_id" ); - $this->assertEmpty( $stream_results ); + $this->assertEmpty( $stream_results, 'Old records have been cleared' ); - // Check if the old meta has been cleared $meta_results = $wpdb->get_row( "SELECT * FROM {$wpdb->streammeta} WHERE meta_id = $meta_id" ); - $this->assertEmpty( $meta_results ); + $this->assertEmpty( $meta_results, 'Old meta has been cleared' ); } public function test_plugin_action_links() {