| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* Converts a detected context and settings into an enable/disable decision. |
| 8 |
*/ |
| 9 |
class WProofreader_Context_Evaluator { |
| 10 |
|
| 11 |
/** @var WProofreader */ |
| 12 |
private $plugin; |
| 13 |
|
| 14 |
/** @var array */ |
| 15 |
private $post_type_options; |
| 16 |
|
| 17 |
/** @var array */ |
| 18 |
private $taxonomy_options; |
| 19 |
|
| 20 |
/** |
| 21 |
* @param WProofreader $plugin Plugin facade. |
| 22 |
*/ |
| 23 |
public function __construct( WProofreader $plugin ) { |
| 24 |
$this->plugin = $plugin; |
| 25 |
|
| 26 |
$this->post_type_options = array( |
| 27 |
WProofreader::POST_TYPE_POST => WProofreader::SETTING_ENABLE_POSTS, |
| 28 |
WProofreader::POST_TYPE_PAGE => WProofreader::SETTING_ENABLE_PAGES, |
| 29 |
WProofreader::POST_TYPE_PRODUCT => WProofreader::SETTING_ENABLE_PRODUCTS, |
| 30 |
WProofreader::POST_TYPE_WPSC_PRODUCT => WProofreader::SETTING_ENABLE_PRODUCTS, |
| 31 |
); |
| 32 |
|
| 33 |
$this->taxonomy_options = array( |
| 34 |
WProofreader_Context_Detector::TAXONOMY_CATEGORY => WProofreader::SETTING_ENABLE_CATEGORIES, |
| 35 |
WProofreader_Context_Detector::TAXONOMY_TAG => WProofreader::SETTING_ENABLE_TAGS, |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Decide whether WProofreader should be enabled in current context. |
| 41 |
* |
| 42 |
* @param array $context Detected context. |
| 43 |
* @return bool |
| 44 |
*/ |
| 45 |
public function should_enable( array $context ): bool { |
| 46 |
$place = isset( $context['place'] ) ? (string) $context['place'] : WProofreader_Context_Detector::PLACE_OTHER; |
| 47 |
|
| 48 |
if ( WProofreader_Context_Detector::PLACE_SETTINGS === $place ) { |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
$admin_enabled = $this->is_option_enabled( WProofreader::SETTING_ENABLE_ADMIN, WProofreader::OPTION_OFF ); |
| 53 |
$admin_behavior = apply_filters( 'wproofreader_admin_behavior', 'with_exclusions' ); |
| 54 |
|
| 55 |
if ( $admin_enabled && 'override' === $admin_behavior ) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
if ( WProofreader_Context_Detector::PLACE_TAXONOMY === $place ) { |
| 60 |
return $this->should_enable_for_taxonomy( $context, $admin_enabled ); |
| 61 |
} |
| 62 |
|
| 63 |
if ( WProofreader_Context_Detector::PLACE_POST_TYPE === $place ) { |
| 64 |
return $this->should_enable_for_post_type( $context, $admin_enabled ); |
| 65 |
} |
| 66 |
|
| 67 |
return $admin_enabled; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Apply filters around the raw context decision. |
| 72 |
* |
| 73 |
* @param array $context Detected context. |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
public function should_enable_filtered( array $context ): bool { |
| 77 |
return (bool) apply_filters( |
| 78 |
'wproofreader_should_enable_here', |
| 79 |
$this->should_enable( $context ), |
| 80 |
$context, |
| 81 |
$this->plugin->get_options() |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @param array $context Context. |
| 87 |
* @param bool $admin_enabled Whether broad admin mode is enabled. |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
private function should_enable_for_taxonomy( array $context, bool $admin_enabled ): bool { |
| 91 |
$taxonomy = isset( $context['taxonomy'] ) ? (string) $context['taxonomy'] : ''; |
| 92 |
$option_key = isset( $this->taxonomy_options[ $taxonomy ] ) ? $this->taxonomy_options[ $taxonomy ] : ''; |
| 93 |
|
| 94 |
if ( '' === $option_key ) { |
| 95 |
return $admin_enabled; |
| 96 |
} |
| 97 |
|
| 98 |
return $this->is_option_enabled( $option_key, WProofreader::OPTION_ON ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* @param array $context Context. |
| 103 |
* @param bool $admin_enabled Whether broad admin mode is enabled. |
| 104 |
* @return bool |
| 105 |
*/ |
| 106 |
private function should_enable_for_post_type( array $context, bool $admin_enabled ): bool { |
| 107 |
$post_type = isset( $context['post_type'] ) ? (string) $context['post_type'] : ''; |
| 108 |
$option_key = isset( $this->post_type_options[ $post_type ] ) ? $this->post_type_options[ $post_type ] : ''; |
| 109 |
|
| 110 |
if ( '' !== $option_key ) { |
| 111 |
return $this->is_option_enabled( $option_key, WProofreader::OPTION_ON ); |
| 112 |
} |
| 113 |
|
| 114 |
if ( $this->is_additional_post_type( $post_type ) ) { |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
return $admin_enabled; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @param string $post_type Post type. |
| 123 |
* @return bool |
| 124 |
*/ |
| 125 |
private function is_additional_post_type( string $post_type ): bool { |
| 126 |
$additional = apply_filters( 'wproofreader_add_cpt', array() ); |
| 127 |
if ( ! is_array( $additional ) ) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
foreach ( $additional as $cpt ) { |
| 132 |
if ( 0 === strcasecmp( (string) $cpt, $post_type ) ) { |
| 133 |
return true; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @param string $key Option key. |
| 142 |
* @param string $default Default value. |
| 143 |
* @return bool |
| 144 |
*/ |
| 145 |
private function is_option_enabled( string $key, string $default ): bool { |
| 146 |
return WProofreader::OPTION_ON === $this->plugin->get_option( $key, $default ); |
| 147 |
} |
| 148 |
} |
| 149 |
|