summaryrefslogtreecommitdiff
blob: 7d50a51468f08c7aa9e67c582a8906aa4556f0b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
 * Action Hooks for Jetpack WAF module.
 *
 * @package automattic/jetpack-waf
 */

namespace Automattic\Jetpack\Waf;

use Automattic\Jetpack\Status\Host;

// We don't want to be anything in here outside WP context.
if ( ! function_exists( 'add_action' ) ) {
	return;
}

/**
 * Check if killswitch is defined as true
 */
if ( defined( 'DISABLE_JETPACK_WAF' ) && DISABLE_JETPACK_WAF ) {
	return;
}

if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
	return;
}

if ( ( new Host() )->is_atomic_platform() ) {
	add_filter(
		'jetpack_get_available_modules',
		function ( $modules ) {
			unset( $modules['waf'] );

			return $modules;
		}
	);
}

/**
 * Triggers when the Jetpack plugin is updated
 */
add_action(
	'upgrader_process_complete',
	array( __NAMESPACE__ . '\Waf_Runner', 'update_rules_if_changed' )
);

/**
 * Cron to update the rules periodically.
 */
if ( Waf_Runner::is_enabled() ) {
	add_action( 'jetpack_waf_rules_update_cron', array( __NAMESPACE__ . '\Waf_Runner', 'update_rules_cron' ) );

	if ( ! wp_next_scheduled( 'jetpack_waf_rules_update_cron' ) ) {
		wp_schedule_event( time(), 'twicedaily', 'jetpack_waf_rules_update_cron' );
	}
}

/**
 * Runs the WAF in the WP context.
 *
 * @return void
 */
add_action(
	'plugins_loaded',
	function () {
		require_once __DIR__ . '/run.php';
	}
);

/**
 * Adds the REST API endpoints used by the WAF in the WP context.
 *
 * @return void
 */
add_action(
	'rest_api_init',
	function () {
		require_once __DIR__ . '/src/class-waf-endpoints.php';
		Waf_Endpoints::register_endpoints();
	}
);

add_action( 'update_option_' . Waf_Runner::IP_ALLOW_LIST_OPTION_NAME, array( Waf_Runner::class, 'activate' ), 10, 0 );
add_action( 'update_option_' . Waf_Runner::IP_BLOCK_LIST_OPTION_NAME, array( Waf_Runner::class, 'activate' ), 10, 0 );
add_action( 'update_option_' . Waf_Runner::IP_LISTS_ENABLED_OPTION_NAME, array( Waf_Runner::class, 'activate' ), 10, 0 );