Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions includes/classes/Feature/Search/Weighting.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function setup() {
add_filter( 'ep_formatted_args', [ $this, 'do_weighting' ], 20, 2 ); // After date decay, etc are injected
add_filter( 'ep_query_weighting_fields', [ $this, 'adjust_weight_for_cross_fields' ], 10, 5 );
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
}

/**
Expand Down Expand Up @@ -426,6 +427,64 @@ public function register_rest_routes() {
);
}

/**
* Enqueue scripts and styles.
*
* @since 5.3.3
* @return void
*/
public function admin_enqueue_scripts() {
if ( 'weighting' !== \ElasticPress\Screen::factory()->get_current_screen() ) {
return;
}

wp_enqueue_style(
'ep_weighting_styles',
EP_URL . 'dist/css/weighting-script.css',
[ 'wp-components', 'wp-edit-post' ],
Utils\get_asset_info( 'weighting-script', 'version' )
);

wp_enqueue_script(
'ep_weighting_script',
EP_URL . 'dist/js/weighting-script.js',
Utils\get_asset_info( 'weighting-script', 'dependencies' ),
Utils\get_asset_info( 'weighting-script', 'version' ),
true
);

$api_url = esc_url_raw( rest_url( 'elasticpress/v1/weighting' ) );
$meta_mode = $this->get_meta_mode();
$weightable_fields = $this->get_weightable_fields();
$weighting_configuration = $this->get_weighting_configuration_with_defaults();

/**
* Filter weighting dashboard options.
*
* @hook ep_weighting_options
* @param {array} $data Weighting dashboard options
* @return {array} New options array
* @since 5.1.0
*/
$data = apply_filters(
'ep_weighting_options',
[
'apiUrl' => $api_url,
'metaMode' => $meta_mode,
'weightableFields' => $weightable_fields,
'weightingConfiguration' => $weighting_configuration,
]
);

wp_localize_script(
'ep_weighting_script',
'epWeighting',
$data
);

wp_set_script_translations( 'ep_weighting_script', 'elasticpress' );
}

/**
* Handles processing the new weighting values and saving them.
*
Expand Down
115 changes: 55 additions & 60 deletions includes/classes/Screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,47 +94,50 @@ public function setup() {
* @since 3.0
*/
public function determine_screen() {
// phpcs:disable WordPress.Security.NonceVerification
if ( ! empty( $_GET['page'] ) && false !== strpos( sanitize_key( $_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 || Utils\isset_do_sync_parameter() ) ) {
if ( Utils\is_top_level_admin_context() ) {
$this->screen = 'dashboard';
} else {
$this->screen = 'weighting';
}
}
} elseif ( 'elasticpress-settings' === $_GET['page'] ) {
if ( true === $install_status || 2 === $install_status || Utils\isset_do_sync_parameter() ) {
$this->screen = 'settings';
}
} elseif ( 'elasticpress-health' === $_GET['page'] ) {
if ( ! isset( $_GET['install_complete'] ) && ( true === $install_status || Utils\isset_do_sync_parameter() ) ) {
$this->screen = 'health';
}
} elseif ( 'elasticpress-weighting' === $_GET['page'] ) {
if ( ! isset( $_GET['install_complete'] ) && ( true === $install_status || Utils\isset_do_sync_parameter() ) ) {
$this->screen = 'weighting';
}
} elseif ( 'elasticpress-synonyms' === $_GET['page'] ) {
if ( ! isset( $_GET['install_complete'] ) && ( true === $install_status || Utils\isset_do_sync_parameter() ) ) {
$this->screen = 'synonyms';
}
} elseif ( 'elasticpress-sync' === $_GET['page'] ) {
if ( ! isset( $_GET['install_complete'] ) && ( true === $install_status || Utils\isset_do_sync_parameter() ) ) {
$this->screen = 'sync';
}
} elseif ( 'elasticpress-status-report' === $_GET['page'] ) {
if ( ! isset( $_GET['install_complete'] ) && ( true === $install_status || Utils\isset_do_sync_parameter() ) ) {
$this->screen = 'status-report';
}
$page = ! empty( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification
if ( ! $page || false === strpos( $page, 'elasticpress' ) ) {
return;
}

$install_status = Installer::factory()->get_install_status();
$install_complete = ! empty( $_GET['install_complete'] ) ? sanitize_key( $_GET['install_complete'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification

$this->screen = 'install';

// Handle main dashboard page with special logic
if ( 'elasticpress' === $page ) {
$can_access = ! $install_complete && ( true === $install_status || Utils\isset_do_sync_parameter() );
if ( $can_access ) {
$this->screen = Utils\is_top_level_admin_context() ? 'dashboard' : 'weighting';
}
return;
}

// Map page slugs to screen names with their access conditions
$page_screen_map = [
'elasticpress-settings' => 'settings',
'elasticpress-health' => 'health',
'elasticpress-weighting' => 'weighting',
'elasticpress-synonyms' => 'synonyms',
'elasticpress-sync' => 'sync',
'elasticpress-status-report' => 'status-report',
];

if ( ! isset( $page_screen_map[ $page ] ) ) {
return;
}

$screen_name = $page_screen_map[ $page ];
$is_settings = 'settings' === $screen_name;

// Settings screen allows install status 2, others require completed install or sync parameter
$can_access = $is_settings
? ( true === $install_status || 2 === $install_status || Utils\isset_do_sync_parameter() )
: ( ! $install_complete && ( true === $install_status || Utils\isset_do_sync_parameter() ) );

if ( $can_access ) {
$this->screen = $screen_name;
}
// phpcs:enable WordPress.Security.NonceVerification
}

/**
Expand All @@ -143,28 +146,20 @@ public function determine_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;
case 'sync':
require_once __DIR__ . '/../partials/sync-page.php';
break;
case 'status-report':
require_once __DIR__ . '/../partials/status-report-page.php';
break;
$page_screen_map = [
'dashboard' => 'dashboard',
'settings' => 'settings',
'install' => 'install',
'health' => 'stats',
'sync' => 'sync',
'status-report' => 'status-report',
];

if ( ! isset( $page_screen_map[ $this->screen ] ) ) {
return;
}

require_once __DIR__ . '/../partials/' . $page_screen_map[ $this->screen ] . '-page.php';
}

/**
Expand Down
63 changes: 11 additions & 52 deletions includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,17 @@ function action_admin_enqueue_dashboard_scripts() {
wp_localize_script( 'ep_admin_sites_scripts', 'epsa', $data );
}

if ( in_array( Screen::factory()->get_current_screen(), [ 'dashboard', 'settings', 'install', 'health', 'weighting', 'synonyms', 'sync', 'status-report' ], true ) ) {
$general_ep_screens = [ 'dashboard', 'settings', 'install', 'health', 'weighting', 'synonyms', 'sync', 'status-report' ];
/**
* Filter the query logger object
*
* @since 5.3.3
* @hook elasticpress_general_ep_screens
* @param {array} $general_ep_screens The general ElasticPress screens.
* @return {array} The filtered general ElasticPress screens.
*/
$general_ep_screens = apply_filters( 'elasticpress_general_ep_screens', $general_ep_screens );
if ( in_array( Screen::factory()->get_current_screen(), $general_ep_screens, true ) ) {
wp_enqueue_style(
'ep_admin_styles',
EP_URL . 'dist/css/dashboard-styles.css',
Expand All @@ -467,57 +477,6 @@ function action_admin_enqueue_dashboard_scripts() {
wp_set_script_translations( 'ep_admin_script', 'elasticpress' );
}

if ( 'weighting' === Screen::factory()->get_current_screen() ) {

wp_enqueue_style(
'ep_weighting_styles',
EP_URL . 'dist/css/weighting-script.css',
[ 'wp-components', 'wp-edit-post' ],
Utils\get_asset_info( 'weighting-script', 'version' )
);

wp_enqueue_script(
'ep_weighting_script',
EP_URL . 'dist/js/weighting-script.js',
Utils\get_asset_info( 'weighting-script', 'dependencies' ),
Utils\get_asset_info( 'weighting-script', 'version' ),
true
);

$weighting = Features::factory()->get_registered_feature( 'search' )->weighting;

$api_url = esc_url_raw( rest_url( 'elasticpress/v1/weighting' ) );
$meta_mode = $weighting->get_meta_mode();
$weightable_fields = $weighting->get_weightable_fields();
$weighting_configuration = $weighting->get_weighting_configuration_with_defaults();

/**
* Filter weighting dashboard options.
*
* @hook ep_weighting_options
* @param {array} $data Weighting dashboard options
* @return {array} New options array
* @since 5.1.0
*/
$data = apply_filters(
'ep_weighting_options',
[
'apiUrl' => $api_url,
'metaMode' => $meta_mode,
'weightableFields' => $weightable_fields,
'weightingConfiguration' => $weighting_configuration,
]
);

wp_localize_script(
'ep_weighting_script',
'epWeighting',
$data
);

wp_set_script_translations( 'ep_weighting_script', 'elasticpress' );
}

if ( in_array( Screen::factory()->get_current_screen(), [ 'dashboard', 'install' ], true ) ) {
wp_enqueue_script(
'ep_dashboard_scripts',
Expand Down
2 changes: 2 additions & 0 deletions includes/partials/install-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
exit; // Exit if accessed directly.
}

$install_status = \ElasticPress\Installer::factory()->get_install_status();

if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
$setup_url = admin_url( 'network/admin.php?page=elasticpress-settings' );
$dashboard_url = admin_url( 'network/admin.php?page=elasticpress' );
Expand Down
Loading