PluginProbe
ElasticPress / 4.3.1
ElasticPress v4.3.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.3.1, at includes/classes/Feature.php

355 lines 8.7 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
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 $sync_url = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK )
286 ? network_admin_url( 'admin.php?page=elasticpress-sync' )
287 : admin_url( 'admin.php?page=elasticpress-sync' );
288 ?>
289
290 <form>
291 <?php
292 if ( ! empty( $requirements_status->message ) ) :
293 $messages = (array) $requirements_status->message;
294 ?>
295 <?php foreach ( $messages as $message ) : ?>
296 <div class="requirements-status-notice">
297 <?php echo wp_kses_post( $message ); ?>
298 </div>
299 <?php endforeach; ?>
300 <?php endif; ?>
301
302 <?php if ( $this->requires_install_reindex ) : ?>
303 <div class="requirements-status-notice requirements-status-notice--reindex" role="status">
304 <?php esc_html_e( 'Enabling this feature will require re-indexing your content.', 'elasticpress' ); ?>
305 </div>
306 <?php endif; ?>
307
308 <div class="requirements-status-notice requirements-status-notice--syncing" role="alert">
309 <?php
310 printf(
311 '%1$s <a href="%2$s">%3$s</a>',
312 esc_html__( 'Settings not saved. Cannot save settings while a sync is in progress.', 'elasticpress' ),
313 esc_url( $sync_url ),
314 esc_html__( 'View sync status.', 'elasticpress' )
315 );
316 ?>
317 </div>
318
319 <h3><?php esc_html_e( 'Settings', 'elasticpress' ); ?></h3>
320
321 <div class="feature-fields">
322 <div class="field js-toggle-feature">
323 <div class="field-name status"><?php esc_html_e( 'Status', 'elasticpress' ); ?></div>
324 <div class="input-wrap <?php if ( 2 === $requirements_status->code ) : ?>disabled<?php endif; ?>">
325 <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>
326 <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>
327 </div>
328 </div>
329
330 <?php
331 $this->output_feature_box_settings();
332 ?>
333 </div>
334
335 <div class="action-wrap">
336 <span class="no-dash-sync">
337 <?php esc_html_e( 'Setting adjustments to this feature require a re-sync. Use WP-CLI.', 'elasticpress' ); ?>
338 </span>
339
340 <input type="hidden" name="action" value="ep_save_feature">
341 <input type="hidden" name="feature" value="<?php echo esc_attr( $this->slug ); ?>">
342 <input type="hidden" name="requires_reindex" value="<?php echo $this->requires_install_reindex ? '1' : '0'; ?>">
343 <input type="hidden" name="was_active" value="<?php echo $this->is_active() ? '1' : '0'; ?>">
344 <?php wp_nonce_field( 'ep_dashboard_nonce', 'nonce' ); ?>
345
346 <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">
347 <?php esc_html_e( 'Save', 'elasticpress' ); ?>
348 </button>
349 </div>
350 </form>
351
352 <?php
353 }
354 }
355