| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Optml_Filters. |
| 5 |
* |
| 6 |
* @package \Optml\Inc |
| 7 |
* @author Optimole <friends@optimole.com> |
| 8 |
*/ |
| 9 |
final class Optml_Filters { |
| 10 |
|
| 11 |
/** |
| 12 |
* Generic method to check if the page is allowed to do the action. |
| 13 |
* |
| 14 |
* @param array $contains_flags Contains flags array. |
| 15 |
* @param array $match_flags Exact path match flags array. |
| 16 |
* @return bool Should do action on page? |
| 17 |
*/ |
| 18 |
public static function should_do_page( $contains_flags, $match_flags ) { |
| 19 |
|
| 20 |
if ( empty( $contains_flags ) && empty( $match_flags ) ) { |
| 21 |
return true; |
| 22 |
} |
| 23 |
|
| 24 |
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { |
| 25 |
return true; |
| 26 |
} |
| 27 |
|
| 28 |
$check_against = [ $_SERVER['REQUEST_URI'] ]; |
| 29 |
// This code is designed to handle ajax requests on pages that are excluded. |
| 30 |
// For ajax requests, the referer is set to the page URL and they use a POST method. |
| 31 |
// If an ajax request uses a GET method, it can be managed using the available exclusion rules. |
| 32 |
if ( isset( $_SERVER['HTTP_REFERER'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' ) { |
| 33 |
$check_against[] = $_SERVER['HTTP_REFERER']; |
| 34 |
} |
| 35 |
foreach ( $check_against as $check ) { |
| 36 |
foreach ( $match_flags as $rule_flag => $status ) { |
| 37 |
if ( $rule_flag === $check ) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
} |
| 41 |
foreach ( $contains_flags as $rule_flag => $status ) { |
| 42 |
if ( strpos( $check, $rule_flag ) !== false ) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
if ( $rule_flag === 'home' && ( empty( $check ) || $check === '/' ) ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Check if image qualifies for processing. |
| 56 |
* |
| 57 |
* @param string $image_url Image url. |
| 58 |
* @param array $flags Flags array. |
| 59 |
* |
| 60 |
* @return bool Should we process the image? |
| 61 |
*/ |
| 62 |
public static function should_do_image( $image_url, $flags ) { |
| 63 |
|
| 64 |
foreach ( $flags as $rule_flag => $status ) { |
| 65 |
if ( strpos( $image_url, $rule_flag ) !== false ) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* |
| 75 |
* Check if the image should be processed based on the extension. |
| 76 |
* |
| 77 |
* @param array $flags Flags array. |
| 78 |
* @param string $ext Extension string. |
| 79 |
* |
| 80 |
* @return bool|string Should we do the processing? |
| 81 |
*/ |
| 82 |
public static function should_do_extension( $flags, $ext ) { |
| 83 |
foreach ( $flags as $rule_flag => $status ) { |
| 84 |
if ( $rule_flag === $ext ) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return $ext; |
| 90 |
} |
| 91 |
} |
| 92 |
|