PluginProbe
ElasticPress / 4.4.0
ElasticPress v4.4.0
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.0, at includes/classes/Feature.php

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