Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/ThirdParty/Hostings/Godaddy.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + declare(strict_types=1);
3 +
4 + namespace WP_Rocket\ThirdParty\Hostings;
5 +
6 + use WP_Rocket\Event_Management\Subscriber_Interface;
7 + use WP_Rocket\ThirdParty\ReturnTypesTrait;
8 +
9 + class Godaddy implements Subscriber_Interface {
10 + use ReturnTypesTrait;
11 +
12 + /**
13 + * Godaddy vip url
14 + *
15 + * @var string
16 + */
17 + private $vip_url = '';
18 +
19 + /**
20 + * Godaddy constructor.
21 + *
22 + * @param string $vip_url Godaddy vip url.
23 + */
24 + public function __construct( $vip_url = '' ) {
25 + $this->vip_url = method_exists( '\WPaas\Plugin', 'vip' ) ? \WPaas\Plugin::vip() : $vip_url; // @phpstan-ignore-line
26 + }
27 +
28 + /**
29 + * Returns an array of events that this subscriber wants to listen to.
30 + *
31 + * @see Subscriber_Interface.
32 + *
33 + * @since 3.9.1
34 + *
35 + * @return array
36 + */
37 + public static function get_subscribed_events() {
38 + return [
39 + 'rocket_varnish_field_settings' => 'varnish_field',
40 + 'rocket_display_input_varnish_auto_purge' => [ 'return_false' ],
41 + 'set_rocket_wp_cache_define' => [ 'return_true' ],
42 + 'rocket_cache_mandatory_cookies' => [ 'return_empty_array', PHP_INT_MAX ],
43 + 'rocket_htaccess_mod_rewrite' => [ 'return_false' ],
44 + 'rocket_htaccess_mod_expires' => [ 'remove_html_expire', 5 ],
45 + 'before_rocket_clean_domain' => 'clean_domain',
46 + 'before_rocket_clean_file' => 'clean_file',
47 + 'before_rocket_clean_home' => [ 'clean_home', 10, 2 ],
48 + ];
49 + }
50 +
51 + /**
52 + * Changes the text on the Varnish one-click block.
53 + *
54 + * @since 3.9.1
55 + *
56 + * @param array $settings Field settings data.
57 + *
58 + * @return array modified field settings data.
59 + */
60 + public function varnish_field( $settings ): array {
61 + $settings['varnish_auto_purge']['title'] = sprintf(
62 + // Translators: %s = Hosting name.
63 + __( 'Your site is hosted on %s, we have enabled Varnish auto-purge for compatibility.', 'rocket' ),
64 + 'GoDaddy'
65 + );
66 +
67 + return $settings;
68 + }
69 +
70 + /**
71 + * Remove expiration on HTML to prevent issue with Varnish cache.
72 + *
73 + * @since 3.9.1
74 + *
75 + * @param string $rules htaccess rules.
76 + *
77 + * @return string
78 + */
79 + public function remove_html_expire( $rules ): string {
80 + $rules = preg_replace( '@\s*#\s*Your document html@', '', $rules );
81 + $rules = preg_replace( '@\s*ExpiresByType text/html\s*"access plus \d+ (seconds|minutes|hour|week|month|year)"@', '', $rules );
82 +
83 + return $rules;
84 + }
85 +
86 + /**
87 + * Call the Varnish server to purge the cache with GoDaddy.
88 + *
89 + * @since 3.9.1
90 + *
91 + * @return void
92 + */
93 + public function clean_domain() {
94 + $this->purge_request( 'BAN' );
95 + }
96 +
97 + /**
98 + * Call the Varnish server to purge a specific URL with GoDaddy.
99 + *
100 + * @since 3.9.1
101 + *
102 + * @param string $url URL to purge.
103 + *
104 + * @return void
105 + */
106 + public function clean_file( $url ) {
107 + $this->purge_request( 'BAN', $url );
108 + }
109 +
110 + /**
111 + * Call the Varnish server to purge the home with GoDaddy.
112 + *
113 + * @since 3.9.1
114 + *
115 + * @param string $root root URL.
116 + * @param string $lang language code.
117 + *
118 + * @return void
119 + */
120 + public function clean_home( $root, $lang ) {
121 + $home_url = trailingslashit( get_rocket_i18n_home_url( $lang ) );
122 + $home_pagination_url = $home_url . trailingslashit( $GLOBALS['wp_rewrite']->pagination_base );
123 +
124 + $this->purge_request( 'BAN', $home_url );
125 + $this->purge_request( 'BAN', $home_pagination_url );
126 + }
127 +
128 +
129 + /**
130 + * Perform the call to the Varnish server to purge
131 + *
132 + * @since 3.9.1
133 + * @source WPaaS\Cache
134 + *
135 + * @param string $method can be BAN or PURGE.
136 + * @param string $url URL to purge.
137 + *
138 + * @return void
139 + */
140 + private function purge_request( string $method, string $url = '' ) {
141 +
142 + if ( empty( $this->vip_url ) ) {
143 + return;
144 + }
145 +
146 + if ( empty( $url ) ) {
147 + $url = home_url();
148 + }
149 +
150 + $host = wp_parse_url( $url, PHP_URL_HOST );
151 +
152 + $url = untrailingslashit( set_url_scheme( str_replace( $host, $this->vip_url, $url ), 'http' ) );
153 +
154 + wp_cache_flush();
155 +
156 + // This forces the APC cache to flush across the server.
157 + update_option( 'gd_system_last_cache_flush', time() );
158 +
159 + wp_remote_request(
160 + esc_url_raw( $url ),
161 + [
162 + 'method' => $method,
163 + 'blocking' => false,
164 + 'headers' => [
165 + 'Host' => $host,
166 + ],
167 + ]
168 + );
169 + }
170 +
171 + /**
172 + * Performs these actions during the plugin activation
173 + *
174 + * @since 3.9.1
175 + *
176 + * @return void
177 + */
178 + public function activate() {
179 + add_action( 'rocket_activation', [ $this, 'activate_no_htaccess_html_expire' ] );
180 + }
181 +
182 + /**
183 + * Remove expiration on HTML on activation to prevent issue with Varnish cache.
184 + *
185 + * @since 3.9.1
186 + *
187 + * @return void
188 + */
189 + public function activate_no_htaccess_html_expire() {
190 + add_filter( 'rocket_htaccess_mod_expires', [ $this, 'remove_htaccess_html_expire' ] );
191 + }
192 + }
193 +