PluginProbe
ElasticPress / 4.4.1
ElasticPress v4.4.1
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / Feature.php

Feature.php in ElasticPress 4.4.1, at includes/classes/Feature.php

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