| 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 ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Filter admin title for install page |
| 41 |
* |
| 42 |
* @param string $admin_title Current title |
| 43 |
* @since 3.0 |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
public function filter_admin_title( $admin_title ) { |
| 47 |
if ( 'install' === Screen::factory()->get_current_screen() ) { |
| 48 |
// translators: Site Name |
| 49 |
return sprintf( esc_html__( 'ElasticPress Setup ‹ %s — WordPress', 'elasticpress' ), esc_html( get_bloginfo( 'name' ) ) ); |
| 50 |
} |
| 51 |
|
| 52 |
return $admin_title; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Determine current install status |
| 57 |
* |
| 58 |
* @since 3.0 |
| 59 |
*/ |
| 60 |
public function calculate_install_status() { |
| 61 |
$skip_install = Utils\get_option( 'ep_skip_install', false ); |
| 62 |
|
| 63 |
if ( $skip_install ) { |
| 64 |
$this->install_status = true; |
| 65 |
|
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
$last_sync = Utils\get_option( 'ep_last_sync', false ); |
| 70 |
if ( ! empty( $last_sync ) ) { |
| 71 |
$this->install_status = true; |
| 72 |
|
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
$host = Utils\get_host(); |
| 77 |
|
| 78 |
if ( empty( $host ) && empty( $_POST['ep_host'] ) ) { |
| 79 |
$this->install_status = 2; |
| 80 |
|
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$this->install_status = 3; |
| 85 |
|
| 86 |
$this->maybe_set_features(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get installation status |
| 91 |
* |
| 92 |
* false - not installed |
| 93 |
* 2 - On step two of install |
| 94 |
* 3 - On step three of install |
| 95 |
* true - Install complete |
| 96 |
* |
| 97 |
* @return bool|int |
| 98 |
*/ |
| 99 |
public function get_install_status() { |
| 100 |
/** |
| 101 |
* Filter install status |
| 102 |
* |
| 103 |
* @hook ep_install_status |
| 104 |
* @param {string} $install_status Current install status |
| 105 |
* @return {string} New install status |
| 106 |
* @since 3.0 |
| 107 |
*/ |
| 108 |
return apply_filters( 'ep_install_status', $this->install_status ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Check if it should use the features selected during the install to update the settings. |
| 113 |
*/ |
| 114 |
public function maybe_set_features() { |
| 115 |
if ( empty( $_POST['ep_install_page_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['ep_install_page_nonce'] ), 'ep_install_page' ) ) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
if ( ! isset( $_POST['features'] ) || ! is_array( $_POST['features'] ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
$registered_features = \ElasticPress\Features::factory()->registered_features; |
| 124 |
$activation_features = wp_list_filter( $registered_features, array( 'available_during_installation' => true ) ); |
| 125 |
|
| 126 |
foreach ( $activation_features as $slug => $feature ) { |
| 127 |
if ( in_array( $slug, $_POST['features'], true ) ) { |
| 128 |
\ElasticPress\Features::factory()->activate_feature( $slug ); |
| 129 |
} else { |
| 130 |
\ElasticPress\Features::factory()->deactivate_feature( $slug ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
$this->install_status = 4; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Return singleton instance of class |
| 139 |
* |
| 140 |
* @return self |
| 141 |
* @since 3.0 |
| 142 |
*/ |
| 143 |
public static function factory() { |
| 144 |
static $instance = false; |
| 145 |
|
| 146 |
if ( ! $instance ) { |
| 147 |
$instance = new self(); |
| 148 |
$instance->setup(); |
| 149 |
} |
| 150 |
|
| 151 |
return $instance; |
| 152 |
} |
| 153 |
} |
| 154 |
|