Skip to:
Content

bbPress.org

Ticket #3395: 3395.4.diff

File 3395.4.diff, 6.3 KB (added by johnjamesjacoby, 4 years ago)
  • src/includes/extend/akismet.php

     
    966966        }
    967967
    968968        /**
    969          * Deletes old spam topics & replies from the queue after 15 days
    970          * (determined by `_bbp_akismet_delete_spam_interval` filter).
     969         * Get the number of rows to delete in a single clean-up query.
    971970         *
    972          * @since 2.6.7 bbPress (r7203)
     971         * @since 2.6.9 bbPress (r7225)
    973972         *
    974          * @global wpdb $wpdb
     973         * @param string $filter The name of the filter to run
     974         * @return int
    975975         */
    976         public function delete_old_spam() {
    977                 global $wpdb;
     976        public function get_delete_limit( $filter = '' ) {
    978977
     978                // Default filter
     979                if ( empty( $filter ) ) {
     980                        $filter = '_bbp_akismet_delete_spam_limit';
     981                }
     982
    979983                /**
    980                  * Determines how many posts will be deleted in each batch.
     984                 * Determines how many rows will be deleted in each batch.
    981985                 *
    982986                 * @param int The default as defined by AKISMET_DELETE_LIMIT (also used
    983                  *            in Akismet WordPress plugin).
     987                 *            in Akismet WordPress plugin). Default 1000.
    984988                 */
    985                 $delete_limit = (int) apply_filters( '_bbp_akismet_delete_spam_limit',
     989                $delete_limit = (int) apply_filters( $filter,
    986990                        defined( 'AKISMET_DELETE_LIMIT' )
    987991                                ? AKISMET_DELETE_LIMIT
    988                                 : 10000
     992                                : 1000
    989993                );
    990994
    991                 // Validate the deletion limit
    992                 $delete_limit = max( 1, intval( $delete_limit ) );
     995                // Validate and return the deletion limit
     996                return max( 1, $delete_limit );
     997        }
    993998
     999        /**
     1000         * Get the interval (in days) for spam to remain in the queue.
     1001         *
     1002         * @since 2.6.9 bbPress (r7225)
     1003         *
     1004         * @param string $filter The name of the filter to run
     1005         * @return int
     1006         */
     1007        public function get_delete_interval( $filter = '' ) {
     1008
     1009                // Default filter
     1010                if ( empty( $filter ) ) {
     1011                        $filter = '_bbp_akismet_delete_spam_interval';
     1012                }
     1013
    9941014                /**
    9951015                 * Determines how many days a piece of spam will be left in the Spam
    9961016                 * queue before being deleted.
    9971017                 *
    998                  * @param int The default number of days.
     1018                 * @param int The number of days. Default 15.
    9991019                 */
    1000                 $delete_interval = (int) apply_filters( '_bbp_akismet_delete_spam_interval', 15 );
     1020                $delete_interval = (int) apply_filters( $filter, 15 );
    10011021
    1002                 // Validate the deletion interval
    1003                 $delete_interval = max( 1, intval( $delete_interval ) );
     1022                // Validate and return the deletion interval
     1023                return max( 1, $delete_interval );
     1024        }
    10041025
     1026        /**
     1027         * Deletes old spam topics & replies from the queue after 15 days
     1028         * (determined by `_bbp_akismet_delete_spam_interval` filter).
     1029         *
     1030         * @since 2.6.7 bbPress (r7203)
     1031         *
     1032         * @global wpdb $wpdb
     1033         */
     1034        public function delete_old_spam() {
     1035                global $wpdb;
     1036
     1037                // Get the deletion limit & interval
     1038                $delete_limit    = $this->get_delete_limit( '_bbp_akismet_delete_spam_limit' );
     1039                $delete_interval = $this->get_delete_interval( '_bbp_akismet_delete_spam_interval' );
     1040
    10051041                // Setup the query
    10061042                $sql = "SELECT id FROM {$wpdb->posts} WHERE post_type IN ('topic', 'reply') AND post_status = 'spam' AND DATE_SUB(NOW(), INTERVAL %d DAY) > post_date_gmt LIMIT %d";
    10071043
     
    10341070
    10351071                        // Prepared as strings since id is an unsigned BIGINT, and using %
    10361072                        // will constrain the value to the maximum signed BIGINT.
    1037                         $format_string = implode( ", ", array_fill( 0, count( $spam_ids ), '%s' ) );
     1073                        $format_string = implode( ', ', array_fill( 0, count( $spam_ids ), '%s' ) );
    10381074
    10391075                        // Run the delete queries
    1040                         $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->posts} WHERE post_id IN ( " . $format_string . " )", $spam_ids ) );
    1041                         $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN ( " . $format_string . " )", $spam_ids ) );
     1076                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->posts} WHERE ID IN ( {$format_string} )", $spam_ids ) );
     1077                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN ( {$format_string} )", $spam_ids ) );
    10421078
    10431079                        // Clean the post cache for these topics & replies
    10441080                        clean_post_cache( $spam_ids );
     
    10781114        public function delete_old_spam_meta() {
    10791115                global $wpdb;
    10801116
    1081                 /**
    1082                  * Determines how many days a piece of spam will be left in the Spam
    1083                  * queue before being deleted.
    1084                  *
    1085                  * @param int The default number of days.
    1086                  */
    1087                 $interval = (int) apply_filters( '_bbp_akismet_delete_spam_meta_interval', 15 );
     1117                // Get the deletion limit & interval
     1118                $delete_limit    = $this->get_delete_limit( '_bbp_akismet_delete_spam_meta_limit' );
     1119                $delete_interval = $this->get_delete_interval( '_bbp_akismet_delete_spam_meta_interval' );
    10881120
    1089                 // Validate the deletion interval
    1090                 $interval = max( 1, intval( $interval ) );
    1091 
    10921121                // Setup the query
    1093                 $sql = "SELECT m.post_id FROM {$wpdb->postmeta} as m INNER JOIN {$wpdb->posts} as p ON m.post_id = p.id WHERE m.meta_key = '_bbp_akismet_as_submitted' AND DATE_SUB(NOW(), INTERVAL %d DAY) > p.post_date_gmt LIMIT 10000";
     1122                $sql = "SELECT m.post_id FROM {$wpdb->postmeta} as m INNER JOIN {$wpdb->posts} as p ON m.post_id = p.ID WHERE m.meta_key = '_bbp_akismet_as_submitted' AND DATE_SUB(NOW(), INTERVAL %d DAY) > p.post_date_gmt LIMIT %d";
    10941123
    10951124                // Query loop of topic & reply IDs
    1096                 while ( $spam_ids = $wpdb->get_col( $wpdb->prepare( $sql, $interval ) ) ) {
     1125                while ( $spam_ids = $wpdb->get_col( $wpdb->prepare( $sql, $delete_interval, $delete_limit ) ) ) {
    10971126
    10981127                        // Exit loop if no spam IDs
    10991128                        if ( empty( $spam_ids ) ) {
     
    11431172        public function delete_orphaned_spam_meta() {
    11441173                global $wpdb;
    11451174
     1175                // Get the deletion limit
     1176                $delete_limit = $this->get_delete_limit( '_bbp_akismet_delete_spam_orphaned_limit' );
     1177
     1178                // Default last meta ID
    11461179                $last_meta_id = 0;
    11471180
    11481181                // Start time (float)
     
    11541187                $max_exec_time = (float) max( ini_get( 'max_execution_time' ) - 5, 3 );
    11551188
    11561189                // Setup the query
    1157                 $sql = "SELECT m.meta_id, m.post_id, m.meta_key FROM {$wpdb->postmeta} as m LEFT JOIN {$wpdb->posts} as p ON m.post_id = p.id WHERE p.id IS NULL AND m.meta_id > %d ORDER BY m.meta_id LIMIT 1000";
     1190                $sql = "SELECT m.meta_id, m.post_id, m.meta_key FROM {$wpdb->postmeta} as m LEFT JOIN {$wpdb->posts} as p ON m.post_id = p.ID WHERE p.ID IS NULL AND m.meta_id > %d ORDER BY m.meta_id LIMIT %d";
    11581191
    11591192                // Query loop of topic & reply IDs
    1160                 while ( $spam_meta_results = $wpdb->get_results( $wpdb->prepare( $sql, $last_meta_id ) ) ) {
     1193                while ( $spam_meta_results = $wpdb->get_results( $wpdb->prepare( $sql, $last_meta_id, $delete_limit ) ) ) {
    11611194
    11621195                        // Exit loop if no spam IDs
    11631196                        if ( empty( $spam_meta_results ) ) {