PluginProbe
ElasticPress / 5.0.1
ElasticPress v5.0.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 / Installer.php

Installer.php in ElasticPress 5.0.1, at includes/classes/Installer.php

155 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ElasticPress installer handler
4 *
5 * @since 3.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress;
10
11 use ElasticPress\Utils;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly.
15 }
16
17 /**
18 * Installer class
19 */
20 class Installer {
21 /**
22 * Current install status
23 *
24 * @var int|bool
25 * @since 3.0
26 */
27 protected $install_status;
28
29 /**
30 * Initialize class
31 *
32 * @since 3.0
33 */
34 public function setup() {
35 add_action( 'admin_init', [ $this, 'calculate_install_status' ], 9 );
36 add_filter( 'admin_title', [ $this, 'filter_admin_title' ], 10, 2 );
37 }
38
39 /**
40 * Filter admin title for install page
41 *
42 * @param string $admin_title Current title
43 * @param string $title Original title
44 * @since 3.0
45 * @return string
46 */
47 public function filter_admin_title( $admin_title, $title ) {
48 if ( 'install' === Screen::factory()->get_current_screen() ) {
49 // translators: Site Name
50 return sprintf( esc_html__( 'ElasticPress Setup &lsaquo; %s &#8212; WordPress', 'elasticpress' ), esc_html( get_bloginfo( 'name' ) ) );
51 }
52
53 return $admin_title;
54 }
55
56 /**
57 * Determine current install status
58 *
59 * @since 3.0
60 */
61 public function calculate_install_status() {
62 $skip_install = Utils\get_option( 'ep_skip_install', false );
63
64 if ( $skip_install ) {
65 $this->install_status = true;
66
67 return;
68 }
69
70 $last_sync = Utils\get_option( 'ep_last_sync', false );
71 if ( ! empty( $last_sync ) ) {
72 $this->install_status = true;
73
74 return;
75 }
76
77 $host = Utils\get_host();
78
79 if ( empty( $host ) && empty( $_POST['ep_host'] ) ) { // phpcs:ignore
80 $this->install_status = 2;
81
82 return;
83 }
84
85 $this->install_status = 3;
86
87 $this->maybe_set_features();
88 }
89
90 /**
91 * Get installation status
92 *
93 * false - not installed
94 * 2 - On step two of install
95 * 3 - On step three of install
96 * true - Install complete
97 *
98 * @return bool|int
99 */
100 public function get_install_status() {
101 /**
102 * Filter install status
103 *
104 * @hook ep_install_status
105 * @param {string} $install_status Current install status
106 * @return {string} New install status
107 * @since 3.0
108 */
109 return apply_filters( 'ep_install_status', $this->install_status );
110 }
111
112 /**
113 * Check if it should use the features selected during the install to update the settings.
114 */
115 public function maybe_set_features() {
116 if ( empty( $_POST['ep_install_page_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['ep_install_page_nonce'] ), 'ep_install_page' ) ) {
117 return;
118 }
119
120 if ( ! isset( $_POST['features'] ) || ! is_array( $_POST['features'] ) ) {
121 return;
122 }
123
124 $registered_features = \ElasticPress\Features::factory()->registered_features;
125 $activation_features = wp_list_filter( $registered_features, array( 'available_during_installation' => true ) );
126
127 foreach ( $activation_features as $slug => $feature ) {
128 if ( in_array( $slug, $_POST['features'], true ) ) {
129 \ElasticPress\Features::factory()->activate_feature( $slug );
130 } else {
131 \ElasticPress\Features::factory()->deactivate_feature( $slug );
132 }
133 }
134
135 $this->install_status = 4;
136 }
137
138 /**
139 * Return singleton instance of class
140 *
141 * @return self
142 * @since 3.0
143 */
144 public static function factory() {
145 static $instance = false;
146
147 if ( ! $instance ) {
148 $instance = new self();
149 $instance->setup();
150 }
151
152 return $instance;
153 }
154 }
155