Adthrive.php
2.45 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace WP_Rocket\ThirdParty\Plugins\Ads;
use WP_Rocket\Event_Management\Subscriber_Interface;
class Adthrive implements Subscriber_Interface {
/**
* Returns an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events(): array {
return [
'wp_rocket_upgrade' => [ 'add_delay_js_exclusion_on_plugin_update', 20, 2 ],
'activate_adthrive-ads/adthrive-ads.php' => 'add_delay_js_exclusion',
'pre_update_option_wp_rocket_settings' => [ 'maybe_add_delay_js_exclusion', 10, 2 ],
];
}
/**
* Adds adthrive to delay JS exclusion field
*
* @since 3.9.3
*
* @return void
*/
public function add_delay_js_exclusion() {
$options = get_option( 'wp_rocket_settings', [] );
if (
! isset( $options['delay_js'] )
||
empty( $options['delay_js'] )
) {
return;
}
$exclusions = $options['delay_js_exclusions'] ?? [];
if ( in_array( 'adthrive', $exclusions, true ) ) {
return;
}
$exclusions[] = 'adthrive';
$options['delay_js_exclusions'] = $exclusions;
update_option( 'wp_rocket_settings', $options );
}
/**
* Adds adthrive to delay JS exclusion field on update to 3.9.3
*
* @since 3.9.3
*
* @param string $new_version Plugin new version.
* @param string $old_version Plugin old version.
*
* @return void
*/
public function add_delay_js_exclusion_on_plugin_update( $new_version, $old_version ) {
if ( version_compare( $old_version, '3.9.3', '>' ) ) {
return;
}
if ( ! is_plugin_active( 'adthrive-ads/adthrive-ads.php' ) ) {
return;
}
$this->add_delay_js_exclusion();
}
/**
* Adds Adthrive pattern when saving WPR options and Adthrive is enabled
*
* @since 3.9.3
*
* @param array $value The new, unserialized option value.
* @param array $old_value The old option value.
*
* @return array
*/
public function maybe_add_delay_js_exclusion( $value, $old_value ): array {
if ( ! is_plugin_active( 'adthrive-ads/adthrive-ads.php' ) ) {
return $value;
}
if ( empty( $value['delay_js'] ) ) {
return $value;
}
if (
isset( $old_value['delay_js'] )
&&
$old_value['delay_js'] === $value['delay_js']
) {
return $value;
}
if (
isset( $value['delay_js_exclusions'] )
&&
in_array( 'adthrive', $value['delay_js_exclusions'], true )
) {
return $value;
}
$value['delay_js_exclusions'][] = 'adthrive';
return $value;
}
}