| | 826 | |
| | 827 | /** |
| | 828 | * Delete old spam topics and replies from the queue after a few days (determined by `_bbp_akismet_delete_spam_interval` filter) |
| | 829 | * |
| | 830 | * @since 2.7.0 bbPress |
| | 831 | */ |
| | 832 | public function delete_old_spam() { |
| | 833 | global $wpdb; |
| | 834 | |
| | 835 | /** |
| | 836 | * Determines how many posts will be deleted in each batch. |
| | 837 | * |
| | 838 | * @param int The default, as defined by AKISMET_DELETE_LIMIT (also used in AK WP plugin). |
| | 839 | */ |
| | 840 | $delete_limit = apply_filters( '_bbp_akismet_delete_spam_limit', defined( 'AKISMET_DELETE_LIMIT' ) ? AKISMET_DELETE_LIMIT : 10000 ); |
| | 841 | $delete_limit = max( 1, intval( $delete_limit ) ); |
| | 842 | |
| | 843 | /** |
| | 844 | * Determines how many days a piece of spam will be left in the Spam queue before being deleted. |
| | 845 | * |
| | 846 | * @param int The default number of days. |
| | 847 | */ |
| | 848 | $delete_interval = apply_filters( '_bbp_akismet_delete_spam_interval', 15 ); |
| | 849 | $delete_interval = max( 1, intval( $delete_interval ) ); |
| | 850 | |
| | 851 | while ( $spam_ids = $wpdb->get_col( $wpdb->prepare( "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", $delete_interval, $delete_limit ) ) ) { |
| | 852 | if ( empty( $spam_ids ) ) |
| | 853 | return; |
| | 854 | |
| | 855 | $wpdb->queries = array(); |
| | 856 | |
| | 857 | foreach ( $spam_ids as $spam_id ) { |
| | 858 | // Maybe we should run the bbp_delete_topic or bbp_delete_reply actions too? |
| | 859 | do_action( '_bbp_akismet_batch_delete_count', __FUNCTION__ ); |
| | 860 | } |
| | 861 | |
| | 862 | // Prepared as strings since id is an unsigned BIGINT, and using %d will constrain the value to the maximum signed BIGINT. |
| | 863 | $format_string = implode( ", ", array_fill( 0, count( $spam_ids ), '%s' ) ); |
| | 864 | |
| | 865 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->posts} WHERE post_id IN ( " . $format_string . " )", $spam_ids ) ); |
| | 866 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN ( " . $format_string . " )", $spam_ids ) ); |
| | 867 | |
| | 868 | clean_post_cache( $spam_ids ); |
| | 869 | do_action( '_bbp_akismet_delete_spam_batch', count( $spam_ids ) ); |
| | 870 | } |
| | 871 | |
| | 872 | if ( apply_filters( '_bbp_akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->posts ) ) // lucky number |
| | 873 | $wpdb->query("OPTIMIZE TABLE {$wpdb->posts}"); |
| | 874 | } |
| | 875 | |
| | 876 | /** |
| | 877 | * Delete the `_bbp_akismet_as_submitted` meta keys after a while, since they are large and not useful in the long term. |
| | 878 | * |
| | 879 | * @since 2.7.0 bbPress |
| | 880 | */ |
| | 881 | public function delete_old_spam_meta() { |
| | 882 | global $wpdb; |
| | 883 | |
| | 884 | $interval = apply_filters( '_bbp_akismet_delete_spam_meta_interval', 15 ); |
| | 885 | |
| | 886 | # enforce a minimum of 1 day |
| | 887 | $interval = absint( $interval ); |
| | 888 | if ( $interval < 1 ) |
| | 889 | $interval = 1; |
| | 890 | |
| | 891 | // akismet_as_submitted meta values are large, so expire them |
| | 892 | // after $interval days regardless of the spam status |
| | 893 | while ( $spam_ids = $wpdb->get_col( $wpdb->prepare( "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", $interval ) ) ) { |
| | 894 | if ( empty( $spam_ids ) ) |
| | 895 | return; |
| | 896 | |
| | 897 | $wpdb->queries = array(); |
| | 898 | |
| | 899 | foreach ( $spam_ids as $spam_id ) { |
| | 900 | delete_post_meta( $spam_id, '_bbp_akismet_as_submitted' ); |
| | 901 | do_action( '_bbp_akismet_batch_delete_count', __FUNCTION__ ); |
| | 902 | } |
| | 903 | |
| | 904 | do_action( '_bbp_akismet_delete_spam_meta_batch', count( $spam_ids ) ); |
| | 905 | } |
| | 906 | |
| | 907 | if ( apply_filters( '_bbp_akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->postmeta ) ) // lucky number |
| | 908 | $wpdb->query("OPTIMIZE TABLE {$wpdb->postmeta}"); |
| | 909 | } |
| | 910 | |
| | 911 | /** |
| | 912 | * Clear out posts meta that no longer have corresponding posts in the database |
| | 913 | * |
| | 914 | * @since 2.7.0 bbPress |
| | 915 | */ |
| | 916 | public function delete_orphaned_spam_meta() { |
| | 917 | global $wpdb; |
| | 918 | |
| | 919 | $last_meta_id = 0; |
| | 920 | $start_time = isset( $_SERVER['REQUEST_TIME_FLOAT'] ) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime( true ); |
| | 921 | $max_exec_time = max( ini_get('max_execution_time') - 5, 3 ); |
| | 922 | |
| | 923 | while ( $spam_meta_results = $wpdb->get_results( $wpdb->prepare( "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", $last_meta_id ) ) ) { |
| | 924 | if ( empty( $spam_meta_results ) ) |
| | 925 | return; |
| | 926 | |
| | 927 | $wpdb->queries = array(); |
| | 928 | |
| | 929 | $spam_meta_deleted = 0; |
| | 930 | |
| | 931 | foreach ( $spam_meta_results as $spam_meta ) { |
| | 932 | if ( 'akismet_' == substr( $spam_meta->meta_key, 0, 8 ) ) { |
| | 933 | delete_post_meta( $spam_meta->post_id, $spam_meta->meta_key ); |
| | 934 | do_action( '_bbp_akismet_batch_delete_count', __FUNCTION__ ); |
| | 935 | $spam_meta_deleted++; |
| | 936 | } |
| | 937 | |
| | 938 | $last_meta_id = $spam_meta->meta_id; |
| | 939 | } |
| | 940 | |
| | 941 | do_action( '_bbp_akismet_delete_spam_meta_batch', $spam_meta_deleted ); |
| | 942 | |
| | 943 | // If we're getting close to max_execution_time, quit for this round. |
| | 944 | if ( microtime(true) - $start_time > $max_exec_time ) |
| | 945 | return; |
| | 946 | } |
| | 947 | |
| | 948 | if ( apply_filters( '_bbp_akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->postmeta ) ) // lucky number |
| | 949 | $wpdb->query("OPTIMIZE TABLE {$wpdb->postmeta}"); |
| | 950 | } |