| 1 |
<?php |
| 2 |
/** |
| 3 |
* Feature class to be initiated for all features. |
| 4 |
* |
| 5 |
* All features extend this class. |
| 6 |
* |
| 7 |
* @since 2.1 |
| 8 |
* @package elasticpress |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace ElasticPress; |
| 12 |
|
| 13 |
use ElasticPress\FeatureRequirementsStatus as FeatureRequirementsStatus; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Feature abstract class |
| 21 |
*/ |
| 22 |
abstract class Feature { |
| 23 |
/** |
| 24 |
* Feature slug |
| 25 |
* |
| 26 |
* @var string |
| 27 |
* @since 2.1 |
| 28 |
*/ |
| 29 |
public $slug; |
| 30 |
|
| 31 |
/** |
| 32 |
* Feature pretty title |
| 33 |
* |
| 34 |
* @var string |
| 35 |
* @since 2.1 |
| 36 |
*/ |
| 37 |
public $title; |
| 38 |
|
| 39 |
/** |
| 40 |
* Feature summary |
| 41 |
* |
| 42 |
* @var string |
| 43 |
* @since 4.0.0 |
| 44 |
*/ |
| 45 |
public $summary; |
| 46 |
|
| 47 |
/** |
| 48 |
* URL to feature documentation. |
| 49 |
* |
| 50 |
* @var string |
| 51 |
* @since 4.0.0 |
| 52 |
*/ |
| 53 |
public $docs_url; |
| 54 |
|
| 55 |
/** |
| 56 |
* Optional feature default settings |
| 57 |
* |
| 58 |
* @since 2.2 |
| 59 |
* @var array |
| 60 |
*/ |
| 61 |
public $default_settings = []; |
| 62 |
|
| 63 |
/** |
| 64 |
* True if the feature requires content reindexing after activating |
| 65 |
* |
| 66 |
* @since 2.1 |
| 67 |
* @var [type] |
| 68 |
*/ |
| 69 |
public $requires_install_reindex; |
| 70 |
|
| 71 |
/** |
| 72 |
* The order in the features screen |
| 73 |
* |
| 74 |
* @var int |
| 75 |
* @since 3.6.0 |
| 76 |
*/ |
| 77 |
public $order; |
| 78 |
|
| 79 |
/** |
| 80 |
* Set if a feature should be on the left or right side |
| 81 |
* |
| 82 |
* @var string |
| 83 |
* @since 3.6.0 |
| 84 |
*/ |
| 85 |
public $group_order; |
| 86 |
|
| 87 |
/** |
| 88 |
* True if activation of this feature should be available during |
| 89 |
* installation. |
| 90 |
* |
| 91 |
* @since 4.0.0 |
| 92 |
* @var boolean |
| 93 |
*/ |
| 94 |
public $available_during_installation = false; |
| 95 |
|
| 96 |
/** |
| 97 |
* Run on every page load for feature to set itself up |
| 98 |
* |
| 99 |
* @since 2.1 |
| 100 |
*/ |
| 101 |
abstract public function setup(); |
| 102 |
|
| 103 |
/** |
| 104 |
* Output feature box summary |
| 105 |
* |
| 106 |
* @since 2.1 |
| 107 |
*/ |
| 108 |
public function output_feature_box_summary() { |
| 109 |
if ( $this->summary ) { |
| 110 |
echo '<p>' . esc_html( $this->summary ) . '</p>'; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Implement to output feature box long text |
| 116 |
* |
| 117 |
* @since 3.0 |
| 118 |
*/ |
| 119 |
abstract public function output_feature_box_long(); |
| 120 |
|
| 121 |
/** |
| 122 |
* Create feature |
| 123 |
* |
| 124 |
* @since 3.0 |
| 125 |
*/ |
| 126 |
public function __construct() { |
| 127 |
/** |
| 128 |
* Fires when Feature object is created |
| 129 |
* |
| 130 |
* @hook ep_feature_create |
| 131 |
* @param {Feature} $feature Current feature |
| 132 |
* @since 3.0 |
| 133 |
*/ |
| 134 |
do_action( 'ep_feature_create', $this ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Returns requirements status of feature |
| 139 |
* |
| 140 |
* @since 2.2 |
| 141 |
* @return FeatureRequirementsStatus |
| 142 |
*/ |
| 143 |
public function requirements_status() { |
| 144 |
$status = new FeatureRequirementsStatus( 0 ); |
| 145 |
|
| 146 |
/** |
| 147 |
* Filter feature requirement status |
| 148 |
* |
| 149 |
* @hook ep_{indexable_slug}_index_kill |
| 150 |
* @param {FeatureRequirementStatus} $status Current feature requirement status |
| 151 |
* @param {Feature} $feature Current feature |
| 152 |
* @since 2.2 |
| 153 |
* @return {FeatureRequirementStatus} New status |
| 154 |
*/ |
| 155 |
return apply_filters( 'ep_feature_requirements_status', $status, $this ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Return feature settings |
| 160 |
* |
| 161 |
* @since 2.2.1 |
| 162 |
* @return array|bool |
| 163 |
*/ |
| 164 |
public function get_settings() { |
| 165 |
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { |
| 166 |
$feature_settings = get_site_option( 'ep_feature_settings', [] ); |
| 167 |
} else { |
| 168 |
$feature_settings = get_option( 'ep_feature_settings', [] ); |
| 169 |
} |
| 170 |
|
| 171 |
return ( ! empty( $feature_settings[ $this->slug ] ) ) ? $feature_settings[ $this->slug ] : false; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Returns true if feature is active |
| 176 |
* |
| 177 |
* @since 2.2 |
| 178 |
* @return boolean |
| 179 |
*/ |
| 180 |
public function is_active() { |
| 181 |
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { |
| 182 |
$feature_settings = get_site_option( 'ep_feature_settings', [] ); |
| 183 |
} else { |
| 184 |
$feature_settings = get_option( 'ep_feature_settings', [] ); |
| 185 |
} |
| 186 |
|
| 187 |
$active = false; |
| 188 |
|
| 189 |
if ( ! empty( $feature_settings[ $this->slug ] ) && $feature_settings[ $this->slug ]['active'] ) { |
| 190 |
$active = true; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Filter whether a feature is active or not |
| 195 |
* |
| 196 |
* @hook ep_feature_active |
| 197 |
* @param {bool} $active Whether feature is active or not |
| 198 |
* @param {array} $feature_settings Current feature settings |
| 199 |
* @param {Feature} $feature Current feature |
| 200 |
* @since 2.2 |
| 201 |
* @return {bool} New active value |
| 202 |
*/ |
| 203 |
return apply_filters( 'ep_feature_active', $active, $feature_settings, $this ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* To be run after initial feature activation |
| 208 |
* |
| 209 |
* @since 2.1 |
| 210 |
*/ |
| 211 |
public function post_activation() { |
| 212 |
/** |
| 213 |
* Fires after feature is activated |
| 214 |
* |
| 215 |
* @hook ep_feature_post_activation |
| 216 |
* @param {string} $slug Feature slug |
| 217 |
* @param {Feature} $feature Current feature |
| 218 |
* @since 2.1 |
| 219 |
*/ |
| 220 |
do_action( 'ep_feature_post_activation', $this->slug, $this ); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Outputs feature box |
| 225 |
* |
| 226 |
* @since 2.1 |
| 227 |
*/ |
| 228 |
public function output_feature_box() { |
| 229 |
$this->output_feature_box_summary(); |
| 230 |
|
| 231 |
/** |
| 232 |
* Fires before feature box summary is shown |
| 233 |
* |
| 234 |
* @hook ep_feature_box_summary |
| 235 |
* @param {string} $slug Feature slug |
| 236 |
* @param {Feature} $feature Current feature |
| 237 |
* @since 2.1 |
| 238 |
*/ |
| 239 |
do_action( 'ep_feature_box_summary', $this->slug, $this ); |
| 240 |
?> |
| 241 |
|
| 242 |
<button aria-expanded="false" class="learn-more button button-secondary button-small" type="button"><?php esc_html_e( 'Learn more', 'elasticpress' ); ?></button> |
| 243 |
|
| 244 |
<div class="long"> |
| 245 |
<?php $this->output_feature_box_long(); ?> |
| 246 |
|
| 247 |
<p><button aria-expanded="true" class="collapse button button-secondary button-small" type="button"><?php esc_html_e( 'Collapse', 'elasticpress' ); ?></button></p> |
| 248 |
|
| 249 |
<?php |
| 250 |
/** |
| 251 |
* Fires after feature long description |
| 252 |
* |
| 253 |
* @hook ep_feature_box_long |
| 254 |
* @param {string} $slug Feature slug |
| 255 |
* @param {Feature} $feature Current feature |
| 256 |
* @since 2.1 |
| 257 |
*/ |
| 258 |
do_action( 'ep_feature_box_long', $this->slug, $this ); |
| 259 |
?> |
| 260 |
|
| 261 |
</div> |
| 262 |
<?php |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Output extra feature box settings. |
| 267 |
* |
| 268 |
* By default this does nothing. Override to add additional settings. |
| 269 |
* |
| 270 |
* @since 3.0 |
| 271 |
*/ |
| 272 |
public function output_feature_box_settings() { |
| 273 |
/** |
| 274 |
* Optionally override |
| 275 |
*/ |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Output feature settings |
| 280 |
* |
| 281 |
* @since 3.0 |
| 282 |
*/ |
| 283 |
public function output_settings_box() { |
| 284 |
$requirements_status = $this->requirements_status(); |
| 285 |
?> |
| 286 |
|
| 287 |
<form> |
| 288 |
<?php |
| 289 |
if ( ! empty( $requirements_status->message ) ) : |
| 290 |
$messages = (array) $requirements_status->message; |
| 291 |
?> |
| 292 |
<?php foreach ( $messages as $message ) : ?> |
| 293 |
<div class="requirements-status-notice"> |
| 294 |
<?php echo wp_kses_post( $message ); ?> |
| 295 |
</div> |
| 296 |
<?php endforeach; ?> |
| 297 |
<?php endif; ?> |
| 298 |
|
| 299 |
<?php if ( $this->requires_install_reindex ) : ?> |
| 300 |
<div class="requirements-status-notice requirements-status-notice--reindex" role="status"> |
| 301 |
<?php esc_html_e( 'Enabling this feature will require re-indexing your content.', 'elasticpress' ); ?> |
| 302 |
</div> |
| 303 |
<?php endif; ?> |
| 304 |
|
| 305 |
<h3><?php esc_html_e( 'Settings', 'elasticpress' ); ?></h3> |
| 306 |
|
| 307 |
<div class="feature-fields"> |
| 308 |
<div class="field js-toggle-feature"> |
| 309 |
<div class="field-name status"><?php esc_html_e( 'Status', 'elasticpress' ); ?></div> |
| 310 |
<div class="input-wrap <?php if ( 2 === $requirements_status->code ) : ?>disabled<?php endif; ?>"> |
| 311 |
<label><input name="settings[active]" <?php disabled( 2 === $requirements_status->code ); ?> type="radio" <?php checked( $this->is_active() ); ?> value="1"><?php esc_html_e( 'Enabled', 'elasticpress' ); ?></label><br> |
| 312 |
<label><input name="settings[active]" <?php disabled( 2 === $requirements_status->code ); ?> type="radio" <?php checked( ! $this->is_active() ); ?> value="0"><?php esc_html_e( 'Disabled', 'elasticpress' ); ?></label> |
| 313 |
</div> |
| 314 |
</div> |
| 315 |
|
| 316 |
<?php |
| 317 |
$this->output_feature_box_settings(); |
| 318 |
?> |
| 319 |
</div> |
| 320 |
|
| 321 |
<div class="action-wrap"> |
| 322 |
<span class="no-dash-sync"> |
| 323 |
<?php esc_html_e( 'Setting adjustments to this feature require a re-sync. Use WP-CLI.', 'elasticpress' ); ?> |
| 324 |
</span> |
| 325 |
|
| 326 |
<input type="hidden" name="action" value="ep_save_feature"> |
| 327 |
<input type="hidden" name="feature" value="<?php echo esc_attr( $this->slug ); ?>"> |
| 328 |
<input type="hidden" name="requires_reindex" value="<?php echo $this->requires_install_reindex ? '1' : '0'; ?>"> |
| 329 |
<input type="hidden" name="was_active" value="<?php echo $this->is_active() ? '1' : '0'; ?>"> |
| 330 |
<?php wp_nonce_field( 'ep_dashboard_nonce', 'nonce' ); ?> |
| 331 |
|
| 332 |
<button name="submit" <?php disabled( 2 === $requirements_status->code || ( $this->requires_install_reindex && defined( 'EP_DASHBOARD_SYNC' ) && ! EP_DASHBOARD_SYNC ) ); ?> class="button button-primary" type="submit"> |
| 333 |
<?php esc_html_e( 'Save', 'elasticpress' ); ?> |
| 334 |
</button> |
| 335 |
</div> |
| 336 |
</form> |
| 337 |
|
| 338 |
<?php |
| 339 |
} |
| 340 |
} |
| 341 |
|