ldc-poule-et-toque/content/plugins/elasticpress/includes/classes/Screen.php
2021-04-12 15:54:29 +02:00

151 lines
3.5 KiB
PHP

<?php
/**
* Determine which ElasticPress screen we are viewing
*
* @since 3.0
* @package elasticpress
*/
namespace ElasticPress;
use ElasticPress\Utils;
use ElasticPress\Installer;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Screen class
*/
class Screen {
/**
* Current screen
*
* @var string
* @since 3.0
*/
protected $screen = null;
/**
* Initialize class
*
* @since 3.0
*/
public function setup() {
add_action( 'admin_init', [ $this, 'determine_screen' ] );
}
/**
* Determine current ElasticPress screen. null means not EP screen.
*
* @since 3.0
*/
public function determine_screen() {
// If in network mode, don't output notice in admin and vice-versa.
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
if ( ! is_network_admin() ) {
return false;
}
} else {
if ( is_network_admin() ) {
return false;
}
}
// phpcs:disable WordPress.Security.NonceVerification
if ( ! empty( $_GET['page'] ) && false !== strpos( $_GET['page'], 'elasticpress' ) ) {
$install_status = Installer::factory()->get_install_status();
$this->screen = 'install';
if ( 'elasticpress' === $_GET['page'] ) {
if ( ! isset( $_GET['install_complete'] ) && ( true === $install_status || isset( $_GET['do_sync'] ) ) ) {
$this->screen = 'dashboard';
}
} elseif ( 'elasticpress-settings' === $_GET['page'] ) {
if ( true === $install_status || 2 === $install_status || isset( $_GET['do_sync'] ) ) {
$this->screen = 'settings';
}
} elseif ( 'elasticpress-health' === $_GET['page'] ) {
if ( ! isset( $_GET['install_complete'] ) && ( true === $install_status || isset( $_GET['do_sync'] ) ) ) {
$this->screen = 'health';
}
} elseif ( 'elasticpress-highlighting' === $_GET['page'] ) {
if ( ! isset( $_GET['install_complete'] ) && ( true === $install_status || isset( $_GET['do_sync'] ) ) ) {
$this->screen = 'highlighting';
}
} elseif ( 'elasticpress-weighting' === $_GET['page'] ) {
if ( ! isset( $_GET['install_complete'] ) && ( true === $install_status || isset( $_GET['do_sync'] ) ) ) {
$this->screen = 'weighting';
}
} elseif ( 'elasticpress-synonyms' === $_GET['page'] ) {
if ( ! isset( $_GET['install_complete'] ) && ( true === $install_status || isset( $_GET['do_sync'] ) ) ) {
$this->screen = 'synonyms';
}
}
}
// phpcs:enable WordPress.Security.NonceVerification
}
/**
* Output template for current screen
*
* @since 3.0
*/
public function output() {
$install_status = Installer::factory()->get_install_status();
switch ( $this->screen ) {
case 'dashboard':
require_once __DIR__ . '/../partials/dashboard-page.php';
break;
case 'settings':
require_once __DIR__ . '/../partials/settings-page.php';
break;
case 'install':
require_once __DIR__ . '/../partials/install-page.php';
break;
case 'health':
require_once __DIR__ . '/../partials/stats-page.php';
break;
}
}
/**
* Get current screen
*
* @since 3.0
* @return string
*/
public function get_current_screen() {
return $this->screen;
}
/**
* Set current screen
*
* @since 3.0
* @param string $screen Screen to set
*/
public function set_current_screen( $screen ) {
$this->screen = $screen;
}
/**
* Return singleton instance of class
*
* @return self
* @since 3.0
*/
public static function factory() {
static $instance = false;
if ( ! $instance ) {
$instance = new self();
$instance->setup();
}
return $instance;
}
}