Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Remove old records #1075

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
/.vagrant/
/console.log
/phpcs.xml
/stream.zip
/stream-*.zip
npm-debug.log
package.lock

Expand Down
25 changes: 14 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
dist: xenial

language:
- php
- node_js
language: php

php:
- "5.6"
Expand All @@ -21,6 +19,12 @@ env:
- WP_VERSION=trunk WP_MULTISITE=0
- WP_VERSION=trunk WP_MULTISITE=1

jobs:
include:
- name: Release Package
php: "7.3"
env: WP_VERSION=latest WP_MULTISITE=0 WP_RELEASE=1

services:
- mysql

Expand All @@ -38,22 +42,21 @@ after_script:
- source $DEV_LIB_PATH/travis.after_script.sh

before_deploy:
- npm run build
- npm run release

deploy:
provider: releases
api_key:
secure: HheYiv6c8ipHzMZBTH7xcKrOwCllvJTtfiTffAPK6XubWe3Kudn6IJUv0p1gmRhWXxZ5ciJQ/sgiCRGTRm/bubHs4tS7JOmpmoTdkrXajTxyyDCKpxhtT43nie0vNF+pWqVu2yOjhDR4pwtWjpQdzEKOz0kn0XSMT+vGsKQD50w=
overwrite: true
skip_cleanup: true
file_glob: true
file: build/**/*
file:
- stream.zip
- stream-$TRAVIS_TAG.zip
on:
tags: true

# Pull requests are built by default.
branches:
only:
- master
- develop
condition: "$WP_RELEASE = 1"

notifications:
email: false
Expand Down
25 changes: 23 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-env node */
/* eslint-env node, es6 */

module.exports = function( grunt ) {
'use strict';
Expand Down Expand Up @@ -73,6 +73,25 @@ module.exports = function( grunt ) {
},
},

compress: {
release: {
options: {
archive: function() {
if ( process.env.TRAVIS_TAG ) {
return `stream-${process.env.TRAVIS_TAG}.zip`;
}

return 'stream.zip';
},
},
cwd: 'build',
dest: 'stream',
src: [
'**/*',
],
},
},

// Clean up the build
clean: {
build: {
Expand All @@ -97,11 +116,13 @@ module.exports = function( grunt ) {
grunt.loadNpmTasks( 'grunt-contrib-clean' );
grunt.loadNpmTasks( 'grunt-contrib-copy' );
grunt.loadNpmTasks( 'grunt-contrib-cssmin' );
grunt.loadNpmTasks( 'grunt-contrib-compress' );
grunt.loadNpmTasks( 'grunt-contrib-uglify' );
grunt.loadNpmTasks( 'grunt-wp-deploy' );

// Register tasks
grunt.registerTask( 'default', [ 'uglify', 'cssmin' ] );
grunt.registerTask( 'default', [ 'clean', 'uglify', 'cssmin' ] );
grunt.registerTask( 'build', [ 'default', 'copy' ] );
grunt.registerTask( 'release', [ 'build', 'compress' ] );
grunt.registerTask( 'deploy', [ 'build', 'wp_deploy', 'clean' ] );
};
47 changes: 15 additions & 32 deletions classes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -186,8 +181,15 @@ public function __construct( $plugin ) {
// Uninstall Streams and Deactivate plugin.
$uninstall = $this->plugin->db->driver->purge_storage( $this->plugin );

// Auto purge setup.
add_action( 'wp_loaded', array( $this, 'purge_schedule_setup' ) );
// Setup the cron task to purge old records.
add_action(
'admin_init',
array(
$this,
'purge_schedule_setup'
)
);

add_action(
'wp_stream_auto_purge',
array(
Expand Down Expand Up @@ -686,36 +688,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() );
}
Expand Down
82 changes: 37 additions & 45 deletions classes/class-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -718,55 +718,46 @@ public function filters_form() {
}

public function filter_select( $name, $title, $items, $ajax = false ) {
if ( $ajax ) {
$out = sprintf(
'<input type="hidden" name="%s" class="chosen-select" value="%s" data-placeholder="%s" />',
esc_attr( $name ),
esc_attr( wp_stream_filter_input( INPUT_GET, $name ) ),
esc_attr( $title )
$options = array( '<option value=""></option>' );
$selected = wp_stream_filter_input( INPUT_GET, $name );

foreach ( $items as $key => $item ) {
$value = isset( $item['children'] ) ? 'group-' . $key : $key;
$option_args = array(
'value' => $value,
'selected' => selected( $value, $selected, false ),
'disabled' => isset( $item['disabled'] ) ? $item['disabled'] : null,
'icon' => isset( $item['icon'] ) ? $item['icon'] : null,
'group' => isset( $item['children'] ) ? $key : null,
'tooltip' => isset( $item['tooltip'] ) ? $item['tooltip'] : null,
'class' => isset( $item['children'] ) ? 'level-1' : null,
'label' => isset( $item['label'] ) ? $item['label'] : null,
);
} else {
$options = array( '<option value=""></option>' );
$selected = wp_stream_filter_input( INPUT_GET, $name );

foreach ( $items as $key => $item ) {
$value = isset( $item['children'] ) ? 'group-' . $key : $key;
$option_args = array(
'value' => $value,
'selected' => selected( $value, $selected, false ),
'disabled' => isset( $item['disabled'] ) ? $item['disabled'] : null,
'icon' => isset( $item['icon'] ) ? $item['icon'] : null,
'group' => isset( $item['children'] ) ? $key : null,
'tooltip' => isset( $item['tooltip'] ) ? $item['tooltip'] : null,
'class' => isset( $item['children'] ) ? 'level-1' : null,
'label' => isset( $item['label'] ) ? $item['label'] : null,
);
$options[] = $this->filter_option( $option_args );

if ( isset( $item['children'] ) ) {
foreach ( $item['children'] as $child_value => $child_item ) {
$option_args = array(
'value' => $child_value,
'selected' => selected( $child_value, $selected, false ),
'disabled' => isset( $child_item['disabled'] ) ? $child_item['disabled'] : null,
'icon' => isset( $child_item['icon'] ) ? $child_item['icon'] : null,
'group' => $key,
'tooltip' => isset( $child_item['tooltip'] ) ? $child_item['tooltip'] : null,
'class' => 'level-2',
'label' => isset( $child_item['label'] ) ? '- ' . $child_item['label'] : null,
);
$options[] = $this->filter_option( $option_args );
}
$options[] = $this->filter_option( $option_args );

if ( isset( $item['children'] ) ) {
foreach ( $item['children'] as $child_value => $child_item ) {
$option_args = array(
'value' => $child_value,
'selected' => selected( $child_value, $selected, false ),
'disabled' => isset( $child_item['disabled'] ) ? $child_item['disabled'] : null,
'icon' => isset( $child_item['icon'] ) ? $child_item['icon'] : null,
'group' => $key,
'tooltip' => isset( $child_item['tooltip'] ) ? $child_item['tooltip'] : null,
'class' => 'level-2',
'label' => isset( $child_item['label'] ) ? '- ' . $child_item['label'] : null,
);
$options[] = $this->filter_option( $option_args );
}
}
$out = sprintf(
'<select name="%s" class="chosen-select" data-placeholder="%s">%s</select>',
esc_attr( $name ),
// translators: Placeholder refers to the title of the dropdown menu (e.g. "users")
sprintf( esc_attr__( 'Show all %s', 'stream' ), $title ),
implode( '', $options )
);
}
$out = sprintf(
'<select name="%s" class="chosen-select" data-placeholder="%s">%s</select>',
esc_attr( $name ),
// translators: Placeholder refers to the title of the dropdown menu (e.g. "users")
sprintf( esc_attr__( 'Show all %s', 'stream' ), $title ),
implode( '', $options )
);

return $out;
}
Expand Down Expand Up @@ -895,6 +886,7 @@ public function record_actions_form() {
}
echo '</select></div>';
wp_nonce_field( 'stream_record_actions_nonce', 'stream_record_actions_nonce' );
wp_nonce_field( 'stream_filters_user_search_nonce', 'stream_filters_user_search_nonce' );

printf( '<input type="hidden" name="page" value="%s">', esc_attr( wp_stream_filter_input( INPUT_GET, 'page' ) ) );
printf( '<input type="hidden" name="date_predefined" value="%s">', esc_attr( wp_stream_filter_input( INPUT_GET, 'date_predefined' ) ) );
Expand Down
2 changes: 1 addition & 1 deletion classes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Plugin {
*
* @const string
*/
const VERSION = '3.4.2';
const VERSION = '3.4.3';

/**
* WP-CLI command
Expand Down
42 changes: 35 additions & 7 deletions classes/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Loading