Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Brute_Force_Protection_Transient_Cleanup
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 jp_purge_transients
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
42
 jp_purge_transients_activation
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Adapted from Purge Transients by Seebz
4 * https://github.com/Seebz/Snippets/tree/master/Wordpress/plugins/purge-transients
5 *
6 * @package automattic/jetpack-waf
7 */
8
9namespace Automattic\Jetpack\Waf\Brute_Force_Protection;
10
11/**
12 * Transient Cleanup class.
13 */
14class Brute_Force_Protection_Transient_Cleanup {
15    /**
16     * Jetpack Purge Transients.
17     *
18     * @access public
19     * @param string $older_than (default: '1 hour') Older Than.
20     */
21    public static function jp_purge_transients( $older_than = '1 hour' ) {
22        global $wpdb;
23        $older_than_time = strtotime( '-' . $older_than );
24        if ( $older_than_time > time() || $older_than_time < 1 ) {
25            return false;
26        }
27        $sql = $wpdb->prepare(
28            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.LikeWildcardsInQuery
29            "SELECT REPLACE(option_name, '_transient_timeout_jpp_', '') AS transient_name FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_timeout\_jpp\__%%' AND option_value < %d",
30            $older_than_time
31        );
32        $transients    = $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB -- $sql is prepared above.
33        $options_names = array();
34        foreach ( $transients as $transient ) {
35            $options_names[] = '_transient_jpp_' . $transient;
36            $options_names[] = '_transient_timeout_jpp_' . $transient;
37        }
38        if ( $options_names ) {
39            $option_names_string = implode( ', ', array_fill( 0, count( $options_names ), '%s' ) );
40            $result              = $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
41                $wpdb->prepare(
42                    "DELETE FROM {$wpdb->options} WHERE option_name IN ($option_names_string)", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- the placeholders are set above.
43                    $options_names
44                )
45            );
46            if ( ! $result ) {
47                return false;
48            }
49        }
50    }
51
52    /**
53     * Jetpack Purge Transients Activation.
54     *
55     * @access public
56     * @return void
57     */
58    public static function jp_purge_transients_activation() {
59        if ( ! wp_next_scheduled( 'jp_purge_transients_cron' ) ) {
60            wp_schedule_event( time(), 'daily', 'jp_purge_transients_cron' );
61        }
62    }
63}