| 1 |
<?php |
| 2 |
/** |
| 3 |
* Determine which ElasticPress screen we are viewing |
| 4 |
* |
| 5 |
* @since 3.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress; |
| 10 |
|
| 11 |
use ElasticPress\Utils; |
| 12 |
use ElasticPress\Installer; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Screen class |
| 20 |
*/ |
| 21 |
class Screen { |
| 22 |
/** |
| 23 |
* Current screen |
| 24 |
* |
| 25 |
* @var string |
| 26 |
* @since 3.0 |
| 27 |
*/ |
| 28 |
protected $screen = null; |
| 29 |
|
| 30 |
/** |
| 31 |
* Sync screen instance |
| 32 |
* |
| 33 |
* @var Screen\Sync |
| 34 |
* @since 3.6.0 |
| 35 |
*/ |
| 36 |
public $sync_screen; |
| 37 |
|
| 38 |
/** |
| 39 |
* Info screen instance |
| 40 |
* |
| 41 |
* @var Screen\HealthInfo |
| 42 |
* @since 4.3.0 |
| 43 |
*/ |
| 44 |
public $health_info_screen; |
| 45 |
|
| 46 |
/** |
| 47 |
* Status report instance |
| 48 |
* |
| 49 |
* @var Screen\StatusReport |
| 50 |
* @since 4.5.0 |
| 51 |
*/ |
| 52 |
public $status_report; |
| 53 |
|
| 54 |
/** |
| 55 |
* Features instance |
| 56 |
* |
| 57 |
* @var Screen\Features |
| 58 |
* @since 5.0.0 |
| 59 |
*/ |
| 60 |
public $features; |
| 61 |
|
| 62 |
/** |
| 63 |
* Settings instance |
| 64 |
* |
| 65 |
* @var Screen\Settings |
| 66 |
* @since 5.0.0 |
| 67 |
*/ |
| 68 |
public $settings; |
| 69 |
|
| 70 |
/** |
| 71 |
* Initialize class |
| 72 |
* |
| 73 |
* @since 3.0 |
| 74 |
*/ |
| 75 |
public function setup() { |
| 76 |
add_action( 'admin_init', [ $this, 'determine_screen' ] ); |
| 77 |
|
| 78 |
$this->sync_screen = new Screen\Sync(); |
| 79 |
$this->health_info_screen = new Screen\HealthInfo(); |
| 80 |
$this->status_report = new Screen\StatusReport(); |
| 81 |
$this->features = new Screen\Features(); |
| 82 |
$this->settings = new Screen\Settings(); |
| 83 |
|
| 84 |
$this->sync_screen->setup(); |
| 85 |
$this->health_info_screen->setup(); |
| 86 |
$this->status_report->setup(); |
| 87 |
$this->features->setup(); |
| 88 |
$this->settings->setup(); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Determine current ElasticPress screen. null means not EP screen. |
| 93 |
* |
| 94 |
* @since 3.0 |
| 95 |
*/ |
| 96 |
public function determine_screen() { |
| 97 |
$page = ! empty( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification |
| 98 |
if ( ! $page || false === strpos( $page, 'elasticpress' ) ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
$install_status = Installer::factory()->get_install_status(); |
| 103 |
$install_complete = ! empty( $_GET['install_complete'] ) ? sanitize_key( $_GET['install_complete'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification |
| 104 |
|
| 105 |
$this->screen = 'install'; |
| 106 |
|
| 107 |
// Handle main dashboard page with special logic |
| 108 |
if ( 'elasticpress' === $page ) { |
| 109 |
$can_access = ! $install_complete && ( true === $install_status || Utils\isset_do_sync_parameter() ); |
| 110 |
if ( $can_access ) { |
| 111 |
$this->screen = Utils\is_top_level_admin_context() ? 'dashboard' : 'weighting'; |
| 112 |
} |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
// Map page slugs to screen names with their access conditions |
| 117 |
$page_screen_map = [ |
| 118 |
'elasticpress-settings' => 'settings', |
| 119 |
'elasticpress-health' => 'health', |
| 120 |
'elasticpress-weighting' => 'weighting', |
| 121 |
'elasticpress-synonyms' => 'synonyms', |
| 122 |
'elasticpress-sync' => 'sync', |
| 123 |
'elasticpress-status-report' => 'status-report', |
| 124 |
]; |
| 125 |
|
| 126 |
if ( ! isset( $page_screen_map[ $page ] ) ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
$screen_name = $page_screen_map[ $page ]; |
| 131 |
$is_settings = 'settings' === $screen_name; |
| 132 |
|
| 133 |
// Settings screen allows install status 2, others require completed install or sync parameter |
| 134 |
$can_access = $is_settings |
| 135 |
? ( true === $install_status || 2 === $install_status || Utils\isset_do_sync_parameter() ) |
| 136 |
: ( ! $install_complete && ( true === $install_status || Utils\isset_do_sync_parameter() ) ); |
| 137 |
|
| 138 |
if ( $can_access ) { |
| 139 |
$this->screen = $screen_name; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Output template for current screen |
| 145 |
* |
| 146 |
* @since 3.0 |
| 147 |
*/ |
| 148 |
public function output() { |
| 149 |
$page_screen_map = [ |
| 150 |
'dashboard' => 'dashboard', |
| 151 |
'settings' => 'settings', |
| 152 |
'install' => 'install', |
| 153 |
'health' => 'stats', |
| 154 |
'sync' => 'sync', |
| 155 |
'status-report' => 'status-report', |
| 156 |
]; |
| 157 |
|
| 158 |
if ( ! isset( $page_screen_map[ $this->screen ] ) ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
require_once __DIR__ . '/../partials/' . $page_screen_map[ $this->screen ] . '-page.php'; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get current screen |
| 167 |
* |
| 168 |
* @since 3.0 |
| 169 |
* @return string |
| 170 |
*/ |
| 171 |
public function get_current_screen() { |
| 172 |
return $this->screen; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Set current screen |
| 177 |
* |
| 178 |
* @since 3.0 |
| 179 |
* @param string $screen Screen to set |
| 180 |
*/ |
| 181 |
public function set_current_screen( $screen ) { |
| 182 |
$this->screen = $screen; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Return singleton instance of class |
| 187 |
* |
| 188 |
* @return self |
| 189 |
* @since 3.0 |
| 190 |
*/ |
| 191 |
public static function factory() { |
| 192 |
static $instance = false; |
| 193 |
|
| 194 |
if ( ! $instance ) { |
| 195 |
$instance = new self(); |
| 196 |
$instance->setup(); |
| 197 |
} |
| 198 |
|
| 199 |
return $instance; |
| 200 |
} |
| 201 |
} |
| 202 |
|