Plugin Directory

Changeset 3718572


Ignore:
Timestamp:
09/29/2026 08:48:37 AM (3 weeks ago)
Author:
prondzyn
Message:

1.1: configurable days, SQLite fix, tested up to 7.1

Location:
automatic-spam-eraser
Files:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • automatic-spam-eraser/tags/1.1/automatic-spam-eraser.php

    r833801 r3718572  
    33   * Plugin Name: Automatic SPAM Eraser
    44   * Plugin URI: http://wordpress.org/plugins/automatic-spam-eraser/
    5    * Description: The plugin is adding a new WP-Cron event which automatically removes all <a href="edit-comments.php?comment_status=spam">spam</a> comments older than 7 days.
    6    * Version: 1.0
     5   * Description: The plugin is adding a new WP-Cron event which automatically removes all <a href="edit-comments.php?comment_status=spam">spam</a> comments older than 7 days (configurable in <a href="options-discussion.php#automatic_spam_eraser_days">Settings &rarr; Discussion</a>).
     6   * Version: 1.1
    77   * Author: Piotr Prądzyński
    88   * Author URI: http://prondzyn.com
    99   * License: GPL2
     10   * Text Domain: automatic-spam-eraser
    1011   */
     12
     13  define( 'AUTOMATIC_SPAM_ERASER_DEFAULT_DAYS', 7 );
    1114
    1215  register_activation_hook(__FILE__, 'on_activation');
    … …  
    1518    wp_schedule_event( time(), 'daily', 'automatic_spam_eraser_event' );
    1619  }
    17  
     20
    1821  add_action( 'automatic_spam_eraser_event',  'delete_spam_from_db' );
    19  
     22
     23  function automatic_spam_eraser_get_days() {
     24    return automatic_spam_eraser_sanitize_days( get_option( 'automatic_spam_eraser_days', AUTOMATIC_SPAM_ERASER_DEFAULT_DAYS ) );
     25  }
     26
     27  function automatic_spam_eraser_sanitize_days( $days ) {
     28    $days = intval( $days );
     29    return $days >= 1 ? $days : AUTOMATIC_SPAM_ERASER_DEFAULT_DAYS;
     30  }
     31
    2032  function delete_spam_from_db() {
    2133    global $wpdb;
    22     $wpdb->query(
     34    // spam from N or more calendar days ago, i.e. before midnight N-1 days ago;
     35    // computed in PHP instead of DATEDIFF() to work on non-MySQL databases (e.g. SQLite)
     36    $cutoff = gmdate( 'Y-m-d', strtotime( '-' . ( automatic_spam_eraser_get_days() - 1 ) . ' days' ) );
     37    $wpdb->query(
    2338      $wpdb->prepare(
    24         "DELETE FROM $wpdb->comments WHERE comment_approved = %s AND DATEDIFF(DATE(%s), comment_date) >= %d",
     39        "DELETE FROM $wpdb->comments WHERE comment_approved = %s AND comment_date < %s",
    2540        'spam',
    26         date('Y-m-d'), 7
     41        $cutoff
    2742      )
    2843    );
    2944  }
    3045
     46  add_action( 'admin_init', 'automatic_spam_eraser_register_settings' );
     47
     48  function automatic_spam_eraser_register_settings() {
     49    register_setting( 'discussion', 'automatic_spam_eraser_days', 'automatic_spam_eraser_sanitize_days' );
     50    add_settings_field(
     51      'automatic_spam_eraser_days',
     52      __( 'Automatic SPAM Eraser', 'automatic-spam-eraser' ),
     53      'automatic_spam_eraser_render_days_field',
     54      'discussion'
     55    );
     56  }
     57
     58  function automatic_spam_eraser_render_days_field() {
     59    printf(
     60      '<label for="automatic_spam_eraser_days">%s</label>',
     61      sprintf(
     62        /* translators: %s: number input */
     63        __( 'Delete spam comments older than %s days', 'automatic-spam-eraser' ),
     64        sprintf(
     65          '<input name="automatic_spam_eraser_days" type="number" step="1" min="1" id="automatic_spam_eraser_days" value="%d" class="small-text" />',
     66          automatic_spam_eraser_get_days()
     67        )
     68      )
     69    );
     70  }
     71
     72  add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'automatic_spam_eraser_action_links' );
     73
     74  function automatic_spam_eraser_action_links( $links ) {
     75    array_unshift( $links, sprintf(
     76      '<a href="%s">%s</a>',
     77      esc_url( admin_url( 'options-discussion.php#automatic_spam_eraser_days' ) ),
     78      __( 'Settings', 'automatic-spam-eraser' )
     79    ) );
     80    return $links;
     81  }
     82
    3183  register_deactivation_hook(__FILE__, 'on_deactivation');
    32  
     84
    3385  function on_deactivation() {
    3486    wp_clear_scheduled_hook( 'automatic_spam_eraser_event' );
    3587  }
     88
     89  register_uninstall_hook( __FILE__, 'automatic_spam_eraser_uninstall' );
     90
     91  function automatic_spam_eraser_uninstall() {
     92    delete_option( 'automatic_spam_eraser_days' );
     93  }
    3694?>
  • automatic-spam-eraser/tags/1.1/readme.txt

    r3070454 r3718572  
    33Tags: comments, spam, eraser, cleaner, cron
    44Requires at least: 3.0.1
    5 Tested up to: 6.5.2
    6 Stable tag: 1.0
     5Tested up to: 7.1
     6Stable tag: 1.1
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
    … …  
    1414The plugin is adding a new WP-Cron event which automatically removes all spam comments older than 7 days.
    1515
     16The number of days can be changed in Settings > Discussion (default: 7).
     17
    1618**For the proper functioning requires a properly working WP-Cron. If the plugin does not work please check you have WP-Cron correctly working.**
    1719
    … …  
    20221. Upload `automatic-spam-eraser.php` to the `/wp-content/plugins/` directory
    21232. Activate the plugin through the 'Plugins' menu in WordPress
     24
     25== Changelog ==
     26
     27= 1.1 =
     28* Added: the number of days is now configurable in Settings > Discussion (default: 7).
     29* Fixed: on SQLite databases all spam comments were removed regardless of their age.
     30* Tested up to WordPress 7.1.
     31
     32= 1.0 =
     33* Initial release.
  • automatic-spam-eraser/trunk/automatic-spam-eraser.php

    r833801 r3718572  
    33   * Plugin Name: Automatic SPAM Eraser
    44   * Plugin URI: http://wordpress.org/plugins/automatic-spam-eraser/
    5    * Description: The plugin is adding a new WP-Cron event which automatically removes all <a href="edit-comments.php?comment_status=spam">spam</a> comments older than 7 days.
    6    * Version: 1.0
     5   * Description: The plugin is adding a new WP-Cron event which automatically removes all <a href="edit-comments.php?comment_status=spam">spam</a> comments older than 7 days (configurable in <a href="options-discussion.php#automatic_spam_eraser_days">Settings &rarr; Discussion</a>).
     6   * Version: 1.1
    77   * Author: Piotr Prądzyński
    88   * Author URI: http://prondzyn.com
    99   * License: GPL2
     10   * Text Domain: automatic-spam-eraser
    1011   */
     12
     13  define( 'AUTOMATIC_SPAM_ERASER_DEFAULT_DAYS', 7 );
    1114
    1215  register_activation_hook(__FILE__, 'on_activation');
    … …  
    1518    wp_schedule_event( time(), 'daily', 'automatic_spam_eraser_event' );
    1619  }
    17  
     20
    1821  add_action( 'automatic_spam_eraser_event',  'delete_spam_from_db' );
    19  
     22
     23  function automatic_spam_eraser_get_days() {
     24    return automatic_spam_eraser_sanitize_days( get_option( 'automatic_spam_eraser_days', AUTOMATIC_SPAM_ERASER_DEFAULT_DAYS ) );
     25  }
     26
     27  function automatic_spam_eraser_sanitize_days( $days ) {
     28    $days = intval( $days );
     29    return $days >= 1 ? $days : AUTOMATIC_SPAM_ERASER_DEFAULT_DAYS;
     30  }
     31
    2032  function delete_spam_from_db() {
    2133    global $wpdb;
    22     $wpdb->query(
     34    // spam from N or more calendar days ago, i.e. before midnight N-1 days ago;
     35    // computed in PHP instead of DATEDIFF() to work on non-MySQL databases (e.g. SQLite)
     36    $cutoff = gmdate( 'Y-m-d', strtotime( '-' . ( automatic_spam_eraser_get_days() - 1 ) . ' days' ) );
     37    $wpdb->query(
    2338      $wpdb->prepare(
    24         "DELETE FROM $wpdb->comments WHERE comment_approved = %s AND DATEDIFF(DATE(%s), comment_date) >= %d",
     39        "DELETE FROM $wpdb->comments WHERE comment_approved = %s AND comment_date < %s",
    2540        'spam',
    26         date('Y-m-d'), 7
     41        $cutoff
    2742      )
    2843    );
    2944  }
    3045
     46  add_action( 'admin_init', 'automatic_spam_eraser_register_settings' );
     47
     48  function automatic_spam_eraser_register_settings() {
     49    register_setting( 'discussion', 'automatic_spam_eraser_days', 'automatic_spam_eraser_sanitize_days' );
     50    add_settings_field(
     51      'automatic_spam_eraser_days',
     52      __( 'Automatic SPAM Eraser', 'automatic-spam-eraser' ),
     53      'automatic_spam_eraser_render_days_field',
     54      'discussion'
     55    );
     56  }
     57
     58  function automatic_spam_eraser_render_days_field() {
     59    printf(
     60      '<label for="automatic_spam_eraser_days">%s</label>',
     61      sprintf(
     62        /* translators: %s: number input */
     63        __( 'Delete spam comments older than %s days', 'automatic-spam-eraser' ),
     64        sprintf(
     65          '<input name="automatic_spam_eraser_days" type="number" step="1" min="1" id="automatic_spam_eraser_days" value="%d" class="small-text" />',
     66          automatic_spam_eraser_get_days()
     67        )
     68      )
     69    );
     70  }
     71
     72  add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'automatic_spam_eraser_action_links' );
     73
     74  function automatic_spam_eraser_action_links( $links ) {
     75    array_unshift( $links, sprintf(
     76      '<a href="%s">%s</a>',
     77      esc_url( admin_url( 'options-discussion.php#automatic_spam_eraser_days' ) ),
     78      __( 'Settings', 'automatic-spam-eraser' )
     79    ) );
     80    return $links;
     81  }
     82
    3183  register_deactivation_hook(__FILE__, 'on_deactivation');
    32  
     84
    3385  function on_deactivation() {
    3486    wp_clear_scheduled_hook( 'automatic_spam_eraser_event' );
    3587  }
     88
     89  register_uninstall_hook( __FILE__, 'automatic_spam_eraser_uninstall' );
     90
     91  function automatic_spam_eraser_uninstall() {
     92    delete_option( 'automatic_spam_eraser_days' );
     93  }
    3694?>
  • automatic-spam-eraser/trunk/readme.txt

    r3070454 r3718572  
    33Tags: comments, spam, eraser, cleaner, cron
    44Requires at least: 3.0.1
    5 Tested up to: 6.5.2
    6 Stable tag: 1.0
     5Tested up to: 7.1
     6Stable tag: 1.1
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
    … …  
    1414The plugin is adding a new WP-Cron event which automatically removes all spam comments older than 7 days.
    1515
     16The number of days can be changed in Settings > Discussion (default: 7).
     17
    1618**For the proper functioning requires a properly working WP-Cron. If the plugin does not work please check you have WP-Cron correctly working.**
    1719
    … …  
    20221. Upload `automatic-spam-eraser.php` to the `/wp-content/plugins/` directory
    21232. Activate the plugin through the 'Plugins' menu in WordPress
     24
     25== Changelog ==
     26
     27= 1.1 =
     28* Added: the number of days is now configurable in Settings > Discussion (default: 7).
     29* Fixed: on SQLite databases all spam comments were removed regardless of their age.
     30* Tested up to WordPress 7.1.
     31
     32= 1.0 =
     33* Initial release.
Note: See TracChangeset for help on using the changeset viewer.