Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/seo-by-rank-math/includes/class-helper.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Helper Functions.
4 + *
5 + * @since 0.9.0
6 + * @package RankMath
7 + * @subpackage RankMath\Core
8 + * @author Rank Math <support@rankmath.com>
9 + */
10 +
11 + namespace RankMath;
12 +
13 + use RankMath\Helpers\Api;
14 + use RankMath\Helpers\Conditional;
15 + use RankMath\Helpers\Choices;
16 + use RankMath\Helpers\Post_Type;
17 + use RankMath\Helpers\Options;
18 + use RankMath\Helpers\Taxonomy;
19 + use RankMath\Helpers\WordPress;
20 + use RankMath\Helpers\Schema;
21 + use RankMath\Helpers\Analytics;
22 + use RankMath\Helpers\Content_AI;
23 + use RankMath\Helpers\HTML;
24 + use RankMath\Replace_Variables\Replacer;
25 + use RankMath\Helpers\Param;
26 + use RankMath\Helpers\DB as DB_Helper;
27 +
28 + defined( 'ABSPATH' ) || exit;
29 +
30 + /**
31 + * Helper class.
32 + */
33 + class Helper {
34 +
35 + use Api;
36 + use Conditional;
37 + use Choices;
38 + use Post_Type;
39 + use Options;
40 + use Taxonomy;
41 + use WordPress;
42 + use Schema;
43 + use Analytics;
44 + use Content_AI;
45 +
46 + /**
47 + * Replace `%variables%` with context-dependent value.
48 + *
49 + * @param string $content The string containing the %variables%.
50 + * @param array $args Context object, can be post, taxonomy or term.
51 + * @param array $exclude Excluded variables won't be replaced.
52 + *
53 + * @return string
54 + */
55 + public static function replace_vars( $content, $args = [], $exclude = [] ) {
56 + return ( new Replacer() )->replace( $content, $args, $exclude );
57 + }
58 +
59 + /**
60 + * Replace `%variables%` with context-dependent value in SEO fields.
61 + *
62 + * @param string $content The string containing the %variables%.
63 + * @param object $post Context object, can be post, taxonomy or term.
64 + *
65 + * @return string
66 + */
67 + public static function replace_seo_fields( $content, $post ) {
68 + if ( empty( $post ) || ! in_array( $content, [ '%seo_title%', '%seo_description%', '%url%' ], true ) ) {
69 + return self::replace_vars( $content, $post );
70 + }
71 +
72 + if ( '%seo_title%' === $content ) {
73 + $default = self::get_settings( "titles.pt_{$post->post_type}_title", '%title% %sep% %sitename%' );
74 + $title = self::get_post_meta( 'title', $post->ID, $default );
75 +
76 + return self::replace_vars( $title, $post );
77 + }
78 +
79 + if ( '%seo_description%' === $content ) {
80 + $default = self::get_settings( "titles.pt_{$post->post_type}_description", '%excerpt%' );
81 + $desc = self::get_post_meta( 'description', $post->ID, $default );
82 +
83 + return self::replace_vars( $desc, $post );
84 + }
85 +
86 + return self::get_post_meta( 'canonical', $post->ID, get_the_permalink( $post->ID ) );
87 + }
88 +
89 + /**
90 + * Register extra %variables%. For developers.
91 + *
92 + * @codeCoverageIgnore
93 + *
94 + * @deprecated 1.0.34 Use rank_math_register_var_replacement()
95 + * @see rank_math_register_var_replacement()
96 + *
97 + * @param string $variable Variable name, for example %custom%. '%' signs are optional.
98 + * @param mixed $callback Replacement callback. Should return value, not output it.
99 + * @param array $args Array with additional title, description and example values for the variable.
100 + *
101 + * @return bool Replacement was registered successfully or not.
102 + */
103 + public static function register_var_replacement( $variable, $callback, $args = [] ) {
104 + _deprecated_function( 'RankMath\Helper::register_var_replacement()', '1.0.34', 'rank_math_register_var_replacement()' );
105 + $args['description'] = isset( $args['desc'] ) ? $args['desc'] : '';
106 + $args['variable'] = $variable;
107 + return rank_math_register_var_replacement( $variable, $args, $callback );
108 + }
109 +
110 + /**
111 + * Get midnight time for the date variables.
112 + *
113 + * @param int $time Timestamp of date.
114 + * @return int
115 + */
116 + public static function get_midnight( $time ) {
117 + $org_time = $time;
118 + if ( is_numeric( $time ) ) {
119 + $time = self::get_date( 'Y-m-d H:i:s', $time, false, true );
120 + }
121 +
122 + // Early bail if time format is invalid.
123 + if ( false === strtotime( $time ) ) {
124 + return $org_time;
125 + }
126 +
127 + $date = new \DateTime( $time );
128 + $date->setTime( 0, 0, 0 );
129 +
130 + return $date->getTimestamp();
131 + }
132 +
133 + /**
134 + * Extract URL part.
135 + *
136 + * @param string $url The URL to parse.
137 + * @param string $part The URL part to retrieve.
138 + *
139 + * @return string The extracted URL part.
140 + */
141 + public static function get_url_part( $url, $part ) {
142 + $url_parts = wp_parse_url( $url );
143 +
144 + return $url_parts[ $part ] ?? '';
145 + }
146 +
147 + /**
148 + * Get current page URL.
149 + *
150 + * @param bool $ignore_qs Ignore query string.
151 + *
152 + * @return string
153 + */
154 + public static function get_current_page_url( $ignore_qs = false ) {
155 + $link = ( is_ssl() ? 'https' : 'http' ) . '://' . Param::server( 'HTTP_HOST' ) . Param::server( 'REQUEST_URI' );
156 +
157 + if ( $ignore_qs ) {
158 + $link = explode( '?', $link );
159 + $link = $link[0];
160 + }
161 +
162 + return $link;
163 + }
164 +
165 + /**
166 + * Get module by ID.
167 + *
168 + * @param string $id ID to get module.
169 + *
170 + * @return object Module class object.
171 + */
172 + public static function get_module( $id ) {
173 + return rank_math()->manager->get_module( $id );
174 + }
175 +
176 + /**
177 + * Modify module status.
178 + *
179 + * @param array $modules Modules to modify.
180 + */
181 + public static function update_modules( $modules ) {
182 + $stored = get_option( 'rank_math_modules', [] );
183 +
184 + foreach ( $modules as $module => $action ) {
185 + if ( 'off' === $action ) {
186 + if ( in_array( $module, $stored, true ) ) {
187 + $stored = array_diff( $stored, [ $module ] );
188 + }
189 + continue;
190 + }
191 +
192 + $stored[] = $module;
193 + Installer::create_tables( [ $module ] );
194 + }
195 +
196 + update_option( 'rank_math_modules', array_unique( $stored ) );
197 + }
198 +
199 + /**
200 + * Get list of currently active modules.
201 + *
202 + * @return array
203 + */
204 + public static function get_active_modules() {
205 + $registered_modules = rank_math()->manager->modules;
206 + $stored = array_values( get_option( 'rank_math_modules', [] ) );
207 + foreach ( $stored as $key => $value ) {
208 + if (
209 + ! isset( $registered_modules[ $value ] )
210 + || ! is_object( $registered_modules[ $value ] )
211 + || ! method_exists( $registered_modules[ $value ], 'is_disabled' )
212 + || $registered_modules[ $value ]->is_disabled()
213 + ) {
214 + unset( $stored[ $key ] );
215 + }
216 + }
217 +
218 + return $stored;
219 + }
220 +
221 + /**
222 + * Clear cache from:
223 + * - WordPress Total Cache
224 + * - W3 Total Cache
225 + * - WP Super Cache
226 + * - SG CachePress
227 + * - WPEngine
228 + * - Varnish
229 + *
230 + * @param string $context Context for cache to clear.
231 + */
232 + public static function clear_cache( $context = '' ) {
233 +
234 + /**
235 + * Filter: 'rank_math/pre_clear_cache' - Allow developers to extend/override cache clearing.
236 + * Pass a truthy value to override the cache clearing.
237 + */
238 + if ( apply_filters( 'rank_math/pre_clear_cache', false, $context ) ) {
239 + return;
240 + }
241 +
242 + // Clean WordPress cache.
243 + if ( function_exists( 'wp_cache_clear_cache' ) ) {
244 + wp_cache_clear_cache();
245 + }
246 +
247 + // If W3 Total Cache is being used, clear the cache.
248 + if ( function_exists( 'w3tc_pgcache_flush' ) ) {
249 + w3tc_pgcache_flush();
250 + }
251 +
252 + // If WP Super Cache is being used, clear the cache.
253 + if ( function_exists( 'wp_cache_clean_cache' ) ) {
254 + global $file_prefix;
255 + wp_cache_clean_cache( $file_prefix );
256 + }
257 +
258 + // If SG CachePress is installed, reset its caches.
259 + if ( class_exists( 'SG_CachePress_Supercacher' ) && is_callable( [ 'SG_CachePress_Supercacher', 'purge_cache' ] ) ) {
260 + \SG_CachePress_Supercacher::purge_cache();
261 + }
262 +
263 + // Clear caches on WPEngine-hosted sites.
264 + if ( class_exists( 'WpeCommon' ) ) {
265 + \WpeCommon::purge_memcached();
266 + \WpeCommon::purge_varnish_cache();
267 +
268 + // Clear WPEngine CDN cache. Added this condition to avoid PHP error as we are not sure when the new clear_cdn_cache method was added.
269 + if ( method_exists( 'WpeCommon', 'clear_cdn_cache' ) ) {
270 + \WpeCommon::clear_cdn_cache();
271 + } else {
272 + \WpeCommon::clear_maxcdn_cache();
273 + }
274 + }
275 +
276 + // Clear Varnish caches.
277 + self::clear_varnish_cache();
278 + }
279 +
280 + /**
281 + * Clear varnish cache for the dynamic files.
282 + * Credit @davidbarratt: https://github.com/davidbarratt/varnish-http-purge
283 + */
284 + private static function clear_varnish_cache() {
285 + // Early bail if Varnish cache is not enabled on the site.
286 + if ( ! isset( $_SERVER['HTTP_X_VARNISH'] ) ) {
287 + return;
288 + }
289 +
290 + // Parse the URL for proxy proxies.
291 + $parsed_url = wp_parse_url( home_url() );
292 +
293 + // Build a varniship.
294 + $varniship = get_option( 'vhp_varnish_ip' );
295 + if ( defined( 'VHP_VARNISH_IP' ) && false !== VHP_VARNISH_IP ) {
296 + $varniship = VHP_VARNISH_IP;
297 + }
298 +
299 + // If we made varniship, let it sail.
300 + $purgeme = ( isset( $varniship ) && null !== $varniship ) ? $varniship : $parsed_url['host'];
301 + wp_remote_request(
302 + $parsed_url['scheme'] . '://' . $purgeme,
303 + [
304 + 'method' => 'PURGE',
305 + 'blocking' => false,
306 + 'headers' => [
307 + 'host' => $parsed_url['host'],
308 + 'X-Purge-Method' => 'default',
309 + ],
310 + ]
311 + );
312 + }
313 +
314 + /**
315 + * Check if current environment is a localhost.
316 + *
317 + * @return boolean
318 + */
319 + public static function is_localhost() {
320 + $whitelist = [
321 + '127.0.0.1', // IPv4 address.
322 + '::1', // IPv6 address.
323 + ];
324 +
325 + return in_array( self::get_remote_addr(), $whitelist, true );
326 + }
327 +
328 + /**
329 + * Get IP address from which the user is viewing the current page.
330 + *
331 + * @return string
332 + */
333 + public static function get_remote_addr() {
334 + $ip = filter_input( INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP );
335 +
336 + // INPUT_SERVER is not available on some hosts, let's try INPUT_ENV.
337 + if ( ! $ip ) {
338 + $ip = filter_input( INPUT_ENV, 'REMOTE_ADDR', FILTER_VALIDATE_IP );
339 + }
340 +
341 + // If we still don't have it, try to get it from the $_SERVER global.
342 + if ( ! $ip && isset( $_SERVER['REMOTE_ADDR'] ) ) {
343 + $ip = filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP );
344 + }
345 +
346 + return $ip ? $ip : '';
347 + }
348 +
349 + /**
350 + * Get date using date_i18n() or date().
351 + *
352 + * @param string $format Format to display the date.
353 + * @param int|boolean $timestamp_with_offset A sum of Unix timestamp and timezone offset in seconds.
354 + * @param boolean $gmt Whether to use GMT timezone. Only applies if timestamp is not provided.
355 + * @param boolean $mode Whether to use date() or date_i18n().
356 + * @return mixin
357 + */
358 + public static function get_date( $format, $timestamp_with_offset = false, $gmt = false, $mode = false ) {
359 + if ( true === $mode ) {
360 + return date( $format, $timestamp_with_offset ); // phpcs:ignore
361 + }
362 + return date_i18n( $format, $timestamp_with_offset, $gmt );
363 + }
364 +
365 + /**
366 + * Check for valid image url.
367 + *
368 + * @param string $image_url The image url.
369 + * @return boolean
370 + */
371 + public static function is_image_url( $image_url ) {
372 + return filter_var( $image_url, FILTER_VALIDATE_URL ) && preg_match( '/\.(jpg|jpeg|png|gif|webp)$/i', $image_url );
373 + }
374 +
375 + /**
376 + * Check if plugin auto update is disabled.
377 + *
378 + * @return bool
379 + */
380 + public static function is_plugin_update_disabled() {
381 + return ! apply_filters_ref_array( 'auto_update_plugin', [ true, (object) [] ] )
382 + || apply_filters_ref_array( 'automatic_updater_disabled', [ false ] )
383 + || ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
384 + || ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED );
385 + }
386 +
387 + /**
388 + * Enable big selects.
389 + */
390 + public static function enable_big_selects_for_queries() {
391 + static $rank_math_enable_big_select;
392 +
393 + if ( $rank_math_enable_big_select || ! apply_filters( 'rank_math/enable_big_selects', true ) ) {
394 + return;
395 + }
396 +
397 + $rank_math_enable_big_select = DB_Helper::query( 'SET SESSION SQL_BIG_SELECTS=1' );
398 + }
399 +
400 + /**
401 + * Used for Backward compatibility to prevent site from showing undefined method error. (PRO v3.0.49-beta)
402 + *
403 + * @param string $name Method name.
404 + * @param array $argument Parameters passed to the function.
405 + *
406 + * @return string
407 + */
408 + public static function __callStatic( $name, $argument ) {
409 + if ( 'extract_attributes' === $name ) {
410 + return HTML::extract_attributes( current( $argument ) );
411 + }
412 +
413 + return '';
414 + }
415 + }
416 +