| 1 |
<?php |
| 2 |
/** |
| 3 |
* Content Intelligence: Base class for all Content Intelligence features |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.9.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\Content_Helper; |
| 12 |
|
| 13 |
use Parsely\Parsely; |
| 14 |
use Parsely\Permissions; |
| 15 |
use WP_REST_Request; |
| 16 |
|
| 17 |
/** |
| 18 |
* Base class for all Content Intelligence features. |
| 19 |
* |
| 20 |
* @since 3.9.0 |
| 21 |
*/ |
| 22 |
abstract class Content_Helper_Feature { |
| 23 |
/** |
| 24 |
* Instance of Parsely class. |
| 25 |
* |
| 26 |
* @since 3.9.0 |
| 27 |
* @var Parsely |
| 28 |
*/ |
| 29 |
protected $parsely; |
| 30 |
|
| 31 |
/** |
| 32 |
* Returns the global Content Intelligence filter name. The global filter |
| 33 |
* controls the enabled/disabled state of all Content Intelligence features. |
| 34 |
* |
| 35 |
* @since 3.9.0 |
| 36 |
* |
| 37 |
* @return string The filter name. |
| 38 |
*/ |
| 39 |
final public static function get_global_filter_name(): string { |
| 40 |
return 'wp_parsely_enable_content_helper'; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Returns the feature's filter name. The feature filter controls the |
| 45 |
* enabled/disabled state of a particular Content Intelligence feature. |
| 46 |
* |
| 47 |
* @since 3.9.0 |
| 48 |
* |
| 49 |
* @return string The filter name. |
| 50 |
*/ |
| 51 |
abstract public static function get_feature_filter_name(): string; |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the feature's script ID. |
| 55 |
* |
| 56 |
* @since 3.9.0 |
| 57 |
* |
| 58 |
* @return string The script ID. |
| 59 |
*/ |
| 60 |
abstract public static function get_script_id(): string; |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the feature's style ID. |
| 64 |
* |
| 65 |
* @since 3.9.0 |
| 66 |
* |
| 67 |
* @return string The style ID. |
| 68 |
*/ |
| 69 |
abstract public static function get_style_id(): string; |
| 70 |
|
| 71 |
/** |
| 72 |
* Runs the feature's initialization process. |
| 73 |
*/ |
| 74 |
abstract public function run(): void; |
| 75 |
|
| 76 |
/** |
| 77 |
* Examines filters and conditions to determine whether the feature can be |
| 78 |
* enabled. |
| 79 |
* |
| 80 |
* - By default (no filters are explicitly set), the value returns true. |
| 81 |
* - If not set, the feature filter will take the global filter's value. |
| 82 |
* - When explicitly set, the feature filter overrides the global filter. |
| 83 |
* - Possible invalid filter values will resolve to false. |
| 84 |
* |
| 85 |
* @since 3.9.0 |
| 86 |
* |
| 87 |
* @param bool ...$conditions Conditions that need to be met besides filters |
| 88 |
* for the function to return true. |
| 89 |
* @return bool Whether the feature can be enabled. |
| 90 |
*/ |
| 91 |
protected function can_enable_feature( bool ...$conditions ): bool { |
| 92 |
// Get filter values. |
| 93 |
$global = null; |
| 94 |
$feature = null; |
| 95 |
|
| 96 |
if ( '' !== self::get_global_filter_name() ) { |
| 97 |
$global = apply_filters( self::get_global_filter_name(), null ); // phpcs:ignore |
| 98 |
} |
| 99 |
|
| 100 |
if ( '' !== static::get_feature_filter_name() ) { |
| 101 |
$feature = apply_filters( static::get_feature_filter_name(), null ); // phpcs:ignore |
| 102 |
} |
| 103 |
|
| 104 |
// If not set, the feature filter will get its value from the global |
| 105 |
// filter. |
| 106 |
$global_filter_is_false = null !== $global && true !== $global; |
| 107 |
if ( null === $feature && $global_filter_is_false ) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
// Feature filter has explicitly been set to a value different than true. |
| 112 |
$feature_filter_is_false = null !== $feature && true !== $feature; |
| 113 |
if ( $feature_filter_is_false ) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
// Return false if any of the passed conditions are false. |
| 118 |
if ( in_array( false, $conditions, true ) ) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Injects any required inline scripts. |
| 127 |
* |
| 128 |
* @since 3.9.0 |
| 129 |
* |
| 130 |
* @param string|null $settings_route Optional. The settings route if the |
| 131 |
* feature uses settings. Defaults to null. |
| 132 |
*/ |
| 133 |
protected function inject_inline_scripts( |
| 134 |
?string $settings_route = null |
| 135 |
): void { |
| 136 |
$are_credentials_set = $this->parsely->site_id_is_set() && |
| 137 |
$this->parsely->api_secret_is_set(); |
| 138 |
|
| 139 |
// Inject Content Intelligence permissions. |
| 140 |
$permissions_json = Permissions::get_pch_permissions_json( |
| 141 |
$this->parsely->get_options()['content_helper'] |
| 142 |
); |
| 143 |
wp_add_inline_script( |
| 144 |
static::get_script_id(), |
| 145 |
"window.wpParselyContentHelperPermissions = '$permissions_json';", |
| 146 |
'before' |
| 147 |
); |
| 148 |
|
| 149 |
// Inject a message if the required credentials are not set. |
| 150 |
if ( ! $are_credentials_set ) { |
| 151 |
$message = $this->get_credentials_not_set_message(); |
| 152 |
|
| 153 |
wp_add_inline_script( |
| 154 |
static::get_script_id(), |
| 155 |
"window.wpParselyEmptyCredentialsMessage = '{$message}';", |
| 156 |
'before' |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
// If the feature has settings, inject them. |
| 161 |
if ( null !== $settings_route ) { |
| 162 |
$settings = ''; |
| 163 |
|
| 164 |
if ( ! defined( 'INTEGRATION_TESTS_RUNNING' ) ) { |
| 165 |
$settings = rest_do_request( |
| 166 |
new WP_REST_Request( |
| 167 |
'GET', |
| 168 |
'/wp-parsely/v2/settings/' . $settings_route |
| 169 |
) |
| 170 |
)->get_data(); |
| 171 |
} |
| 172 |
|
| 173 |
if ( ! is_array( $settings ) ) { |
| 174 |
$settings = array(); |
| 175 |
} |
| 176 |
|
| 177 |
$settings = wp_json_encode( $settings ); |
| 178 |
|
| 179 |
wp_add_inline_script( |
| 180 |
static::get_script_id(), |
| 181 |
"window.wpParselyContentHelperSettings = '$settings';", |
| 182 |
'before' |
| 183 |
); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Returns the message to be shown when required credentials are not set. |
| 189 |
* |
| 190 |
* HTML is allowed within the message. The message can be overridden using |
| 191 |
* the wp_parsely_message_credentials_not_set filter. |
| 192 |
* |
| 193 |
* @since 3.9.0 |
| 194 |
* |
| 195 |
* @return string The sanitized message. |
| 196 |
*/ |
| 197 |
protected function get_credentials_not_set_message(): string { |
| 198 |
$default_message = ' |
| 199 |
<p> |
| 200 |
<a href="https://www.parse.ly/contact" target="_blank" rel="noopener">' . |
| 201 |
__( 'Contact us', 'wp-parsely' ) . |
| 202 |
'</a>' . |
| 203 |
__( ' about advanced plugin features and the Parse.ly dashboard.', 'wp-parsely' ) . ' |
| 204 |
</p> |
| 205 |
<p>' . |
| 206 |
__( |
| 207 |
'Existing Parse.ly customers can enable this feature by setting their Site ID and API Secret in ', |
| 208 |
'wp-parsely' |
| 209 |
) . ' |
| 210 |
<a href="' . esc_url( Parsely::get_settings_url() ) . '" target="_blank" rel="noopener">' . |
| 211 |
__( 'wp-parsely options.', 'wp-parsely' ) . ' |
| 212 |
</a> |
| 213 |
</p> |
| 214 |
'; |
| 215 |
|
| 216 |
// Override default message if the respective filter is set. |
| 217 |
$message = apply_filters( |
| 218 |
'wp_parsely_message_credentials_not_set', |
| 219 |
$default_message |
| 220 |
); |
| 221 |
|
| 222 |
// Remove unnecessary whitespace to avoid broken output. |
| 223 |
$message = str_replace( array( "\r", "\n", "\t" ), '', $message ); |
| 224 |
|
| 225 |
return wp_kses_post( $message ); |
| 226 |
} |
| 227 |
} |
| 228 |
|