lint fixes

This commit is contained in:
Abhijit Bhatnagar 2025-08-10 00:09:35 +05:30
parent d106c178ac
commit 01a7f967a4
7 changed files with 395 additions and 243 deletions

View file

@ -1,119 +1,172 @@
<?php <?php
/**
* Redirects classic WordPress admin requests into Helix where applicable.
*
* @package Helix
*/
/**
* Check if the Helix build is ready.
*
* @return bool True if the build is ready, false otherwise.
*/
function helix_is_ready(): bool { function helix_is_ready(): bool {
// Ensure the built frontend exists before hijacking admin navigation // Ensure the built frontend exists before hijacking admin navigation.
$plugin_base_path = plugin_dir_path(__DIR__); // points to helix/ $plugin_base_path = plugin_dir_path( __DIR__ ); // points to helix/.
$build_js_path = $plugin_base_path . 'build/index.js'; $build_js_path = $plugin_base_path . 'build/index.js';
return file_exists($build_js_path) && is_readable($build_js_path); return file_exists( $build_js_path ) && is_readable( $build_js_path );
} }
function helix_normalize_route(string $urlOrPath): string { /**
// Convert absolute URLs to a relative admin route * Normalize a route to a relative admin route.
if (preg_match('#^https?://#i', $urlOrPath)) { *
$parts = wp_parse_url($urlOrPath); * @param string $url_or_path The URL or path to normalize.
$path = isset($parts['path']) ? $parts['path'] : '/wp-admin/'; * @return string The normalized route.
$query = isset($parts['query']) && $parts['query'] !== '' ? '?' . $parts['query'] : ''; */
return $path . $query; function helix_normalize_route( string $url_or_path ): string {
} // Convert absolute URLs to a relative admin route.
if ( preg_match( '#^https?://#i', $url_or_path ) ) {
$parts = wp_parse_url( $url_or_path );
$path = isset( $parts['path'] ) ? $parts['path'] : '/wp-admin/';
$query = isset( $parts['query'] ) && '' !== $parts['query'] ? '?' . $parts['query'] : '';
return $path . $query;
}
// Ensure it starts with /wp-admin (allow optional leading slash) // Ensure it starts with /wp-admin (allow optional leading slash).
if (preg_match('#^/?wp-admin#', $urlOrPath)) { if ( preg_match( '#^/?wp-admin#', $url_or_path ) ) {
return str_starts_with($urlOrPath, '/') ? $urlOrPath : '/' . $urlOrPath; return str_starts_with( $url_or_path, '/' ) ? $url_or_path : '/' . $url_or_path;
} }
return '/wp-admin/'; return '/wp-admin/';
} }
/**
* Check if the admin should be hijacked.
*
* @return bool True if the admin should be hijacked, false otherwise.
*/
function helix_should_hijack_admin(): bool { function helix_should_hijack_admin(): bool {
// If user opted into default WP admin, do not hijack // If user opted into default WP admin, do not hijack.
if (get_option('helix_use_default_admin', false)) { if ( get_option( 'helix_use_default_admin', false ) ) {
return false; return false;
} }
// If Helix build is unavailable, do not hijack // If Helix build is unavailable, do not hijack.
if (!helix_is_ready()) { if ( ! helix_is_ready() ) {
return false; return false;
} }
return true; return true;
} }
add_filter('login_redirect', function ($redirect_to, $requested_redirect_to, $user) { /**
// Only modify redirect for successful logins * Redirect the user to the Helix admin page after login.
if (is_wp_error($user)) { *
return $redirect_to; * @param string $redirect_to The redirect URL.
} * @param string $requested_redirect_to The requested redirect URL.
* @param WP_User $user The user object.
* @return string The redirect URL.
*/
add_filter(
'login_redirect',
function ( $redirect_to, $requested_redirect_to, $user ) {
// Only modify redirect for successful logins.
if ( is_wp_error( $user ) ) {
return $redirect_to;
}
// Respect plugin setting to use default WordPress admin or if Helix is not ready // Respect plugin setting to use default WordPress admin or if Helix is not ready.
if (!helix_should_hijack_admin()) { if ( ! helix_should_hijack_admin() ) {
return $redirect_to; return $redirect_to;
} }
// Determine the intended post-login target // Determine the intended post-login target.
$intended_target = (is_string($requested_redirect_to) && $requested_redirect_to !== '') $intended_target = ( is_string( $requested_redirect_to ) && '' !== $requested_redirect_to )
? $requested_redirect_to ? $requested_redirect_to
: $redirect_to; : $redirect_to;
// If the target is not an admin page, do not override // If the target is not an admin page, do not override.
if ($intended_target && !str_contains($intended_target, 'wp-admin')) { if ( $intended_target && ! str_contains( $intended_target, 'wp-admin' ) ) {
return $redirect_to; return $redirect_to;
} }
// Default to the dashboard when no target provided // Default to the dashboard when no target provided.
$helix_route = helix_normalize_route($intended_target ?: '/wp-admin/'); $helix_route = helix_normalize_route( $intended_target ?? '/wp-admin/' );
// Send directly to Helix, preserving the original admin route for the React app // Send directly to Helix, preserving the original admin route for the React app.
// Always use '&' because admin.php already has a query string (page=helix) // Always use '&' because admin.php already has a query string (page=helix).
return admin_url('admin.php?page=helix&helix_route=' . urlencode($helix_route)); return admin_url( 'admin.php?page=helix&helix_route=' . rawurlencode( $helix_route ) );
}, 10, 3); },
10,
3
);
add_action('current_screen', function ($current_screen) { /**
// Don't redirect if not in admin * Redirect the user to the Helix admin page when accessing an admin page.
if (!is_admin()) return; *
* @param WP_Screen $current_screen The current screen object.
*/
add_action(
'current_screen',
function ( $current_screen ) {
// Don't redirect if not in admin.
if ( ! is_admin() ) {
return;
}
// Don't redirect AJAX requests // Don't redirect AJAX requests.
if (wp_doing_ajax()) return; if ( wp_doing_ajax() ) {
return;
}
// Don't redirect REST API requests // Don't redirect REST API requests.
if (defined('REST_REQUEST') && REST_REQUEST) return; if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return;
}
// Don't redirect cron jobs // Don't redirect cron jobs.
if (defined('DOING_CRON') && DOING_CRON) return; if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
return;
}
// Check if we're already on the helix page or wordpress admin page // Check if we're already on the helix page or WordPress admin page.
if ( in_array( $current_screen->id, array( 'toplevel_page_helix', 'toplevel_page_wordpress-admin' ), true ) ) { if ( in_array( $current_screen->id, array( 'toplevel_page_helix', 'toplevel_page_wordpress-admin' ), true ) ) {
return; return;
} }
// Check if user wants to use default WordPress admin // Check if user wants to use default WordPress admin.
$use_default_admin = get_option('helix_use_default_admin', false); $use_default_admin = get_option( 'helix_use_default_admin', false );
if ($use_default_admin) { if ( $use_default_admin ) {
return; return;
} }
// Don't redirect if Helix is not ready or the user opted into default admin // Don't redirect if Helix is not ready or the user opted into default admin.
if (!helix_should_hijack_admin()) { if ( ! helix_should_hijack_admin() ) {
return; return;
} }
// Don't redirect critical WordPress admin functions // Don't redirect critical WordPress admin functions.
global $pagenow; global $pagenow;
$critical_pages = [ $critical_pages = array(
'admin-ajax.php', 'admin-ajax.php',
'admin-post.php', 'admin-post.php',
'async-upload.php', 'async-upload.php',
'update.php', 'update.php',
'update-core.php', 'update-core.php',
'upgrade.php', 'upgrade.php',
]; );
if (in_array($pagenow, $critical_pages)) return; if ( in_array( $pagenow, $critical_pages, true ) ) {
return;
}
// Capture the current admin URL to pass to Helix for routing // Capture the current admin URL to pass to Helix for routing.
$current_url = helix_normalize_route($_SERVER['REQUEST_URI']); $current_url = helix_normalize_route( filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL ) );
// Build the redirect URL with the original path for React routing // Build the redirect URL with the original path for React routing.
// Always use '&' because admin.php already has a query string (page=helix) // Always use '&' because admin.php already has a query string (page=helix).
$redirect_url = admin_url('admin.php?page=helix&helix_route=' . urlencode($current_url)); $redirect_url = admin_url( 'admin.php?page=helix&helix_route=' . rawurlencode( $current_url ) );
// Redirect all other admin pages to Helix // Redirect all other admin pages to Helix.
wp_redirect($redirect_url); wp_safe_redirect( $redirect_url );
exit; exit;
}); }
);

View file

@ -1,6 +1,35 @@
<?php <?php
add_action('admin_menu', function () { /**
add_menu_page('Helix', 'Helix', 'read', 'helix', function () { * Registers the Helix admin top-level menu page.
echo '<div id="helix-root"></div>'; *
}, 'dashicons-admin-generic', 1); * @package Helix
}); */
add_action(
'admin_menu',
function () {
$helix_hook_suffix = add_menu_page(
'Helix',
'Helix',
'read',
'helix',
function () {
echo '<div id="helix-root"></div>';
},
'dashicons-admin-generic',
1
);
if ( $helix_hook_suffix ) {
// Mark when the Helix screen is loading. Used elsewhere to conditionally modify admin UI.
add_action(
"load-$helix_hook_suffix",
function () {
$GLOBALS['helix_is_current_screen'] = true;
// Ensure we are in Helix mode when visiting the Helix page.
update_option( 'helix_use_default_admin', false );
}
);
}
}
);

View file

@ -1,127 +1,156 @@
<?php <?php
/** /**
* Customize the WordPress admin menu for Helix * Customizes the WordPress admin menu for Helix.
* This removes unwanted menu items and keeps only the ones you want * This removes unwanted menu items and keeps only the ones you want
*
* @package Helix
*/ */
add_action('admin_menu', function () { add_action(
// Only customize menu when on Helix page 'current_screen',
if (!isset($_GET['page']) || $_GET['page'] !== 'helix') { function ( $current_screen ) {
return; if ( ! $current_screen || 'toplevel_page_helix' !== $current_screen->id ) {
} return;
}
// Remove all default WordPress admin menus // 1) Remove default WordPress admin menus when viewing Helix.
remove_menu_page('index.php'); // Dashboard remove_menu_page( 'index.php' ); // Dashboard.
remove_menu_page('edit.php'); // Posts remove_menu_page( 'edit.php' ); // Posts.
remove_menu_page('upload.php'); // Media remove_menu_page( 'upload.php' ); // Media.
remove_menu_page('edit.php?post_type=page'); // Pages remove_menu_page( 'edit.php?post_type=page' ); // Pages.
remove_menu_page('edit-comments.php'); // Comments remove_menu_page( 'edit-comments.php' ); // Comments.
remove_menu_page('themes.php'); // Appearance remove_menu_page( 'themes.php' ); // Appearance.
remove_menu_page('plugins.php'); // Plugins remove_menu_page( 'plugins.php' ); // Plugins.
remove_menu_page('users.php'); // Users remove_menu_page( 'users.php' ); // Users.
remove_menu_page('tools.php'); // Tools remove_menu_page( 'tools.php' ); // Tools.
remove_menu_page('options-general.php'); // Settings remove_menu_page( 'options-general.php' ); // Settings.
// Remove all admin menu separators // Remove all admin menu separators.
remove_all_admin_menu_separators(); remove_all_admin_menu_separators();
}, 999); // High priority to run after other plugins
// 2) Ensure the "WordPress Admin" return link is present on first load.
if ( ! get_option( 'helix_use_default_admin', false ) ) {
global $admin_page_hooks;
if ( ! isset( $admin_page_hooks['wordpress-admin'] ) ) {
add_menu_page(
'WordPress Admin',
'WordPress Admin',
'read',
'wordpress-admin',
'wordpress_admin_callback',
'dashicons-wordpress',
2
);
}
}
// 3) Add custom Helix menu items.
add_menu_page(
'Helix Posts',
'Posts',
'edit_posts',
'helix-posts',
'helix_posts_callback',
'dashicons-admin-post',
3
);
add_menu_page(
'Helix Users',
'Users',
'list_users',
'helix-users',
'helix_users_callback',
'dashicons-admin-users',
4
);
},
10
);
/**
* Ensure the "WordPress Admin" return link is present when landing on Helix, even on the first load
* after switching modes in the same request.
*/
/**
* Remove all admin menu separators.
*/
function remove_all_admin_menu_separators() { function remove_all_admin_menu_separators() {
global $menu; global $menu;
if (is_array($menu)) { if ( is_array( $menu ) ) {
foreach ($menu as $key => $item) { foreach ( $menu as $key => $item ) {
// Check if menu item is a separator // Check if menu item is a separator.
if ( false !== strpos( $item[4], 'wp-menu-separator' ) ) { if ( false !== strpos( $item[4], 'wp-menu-separator' ) ) {
unset( $menu[ $key ] ); unset( $menu[ $key ] );
} }
} }
} }
} }
/** /**
* Add custom menu items * Add WordPress Admin menu item conditionally.
*/ */
add_action(
'admin_menu',
function () {
// Only show the "WordPress Admin" link when in Helix mode.
$use_default_admin = get_option( 'helix_use_default_admin', false );
if ( $use_default_admin ) {
return;
}
// Add WordPress Admin menu item conditionally // Add a menu item to go back to default WordPress admin.
add_action('admin_menu', function () { add_menu_page(
// Reset to Helix mode when accessing Helix page (do this before checking the option) 'WordPress Admin', // Page title.
if (isset($_GET['page']) && $_GET['page'] === 'helix') { 'WordPress Admin', // Menu title.
update_option('helix_use_default_admin', false); 'read', // Capability.
} 'wordpress-admin', // Menu slug.
'wordpress_admin_callback', // Callback function.
'dashicons-wordpress', // Icon.
2 // Position (after main Helix menu).
);
},
1000
);
// Only show the "WordPress Admin" link when in Helix mode. /**
$use_default_admin = get_option('helix_use_default_admin', false); * Add custom Helix menu items (only when on Helix page).
if ($use_default_admin) { */
return; /* previous current_screen callbacks combined into the single handler above */
}
// Add a menu item to go back to default WordPress admin /**
add_menu_page( * Callback functions for custom menu items.
'WordPress Admin', // Page title */
'WordPress Admin', // Menu title
'read', // Capability
'wordpress-admin', // Menu slug
'wordpress_admin_callback', // Callback function
'dashicons-wordpress', // Icon
2 // Position (after main Helix menu)
);
}, 1000); // Higher priority to run after menu removal
// Add custom Helix menu items (only when on Helix page)
add_action('admin_menu', function () {
// Only add when on Helix page
if (!isset($_GET['page']) || $_GET['page'] !== 'helix') {
return;
}
// Example: Add a Posts menu item
add_menu_page(
'Helix Posts',
'Posts',
'edit_posts',
'helix-posts',
'helix_posts_callback',
'dashicons-admin-post',
3
);
// Example: Add a Users menu item
add_menu_page(
'Helix Users',
'Users',
'list_users',
'helix-users',
'helix_users_callback',
'dashicons-admin-users',
4
);
}, 1001); // Even higher priority to run after WordPress Admin menu
// Callback functions for custom menu items
function wordpress_admin_callback() { function wordpress_admin_callback() {
// Set the option to use default WordPress admin // Set the option to use default WordPress admin.
update_option('helix_use_default_admin', true); update_option( 'helix_use_default_admin', true );
// Show a clean message and let the user navigate naturally // Show a clean message and let the user navigate naturally.
?> ?>
<div style="text-align: center; padding: 50px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;"> <div style="text-align: center; padding: 50px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;">
<h2> Switched to WordPress Admin</h2> <h2> Switched to WordPress Admin</h2>
<p>You can now navigate to any WordPress admin page normally.</p> <p>You can now navigate to any WordPress admin page normally.</p>
<p><a href="<?php echo admin_url('index.php'); ?>" class="button button-primary">Go to Dashboard</a></p> <p><a href="<?php echo esc_url( admin_url( 'index.php' ) ); ?>" class="button button-primary">Go to Dashboard</a></p>
<p><a href="<?php echo admin_url('edit.php'); ?>" class="button">Posts</a> <p><a href="<?php echo esc_url( admin_url( 'edit.php' ) ); ?>" class="button">Posts</a>
<a href="<?php echo admin_url('users.php'); ?>" class="button">Users</a> <a href="<?php echo esc_url( admin_url( 'users.php' ) ); ?>" class="button">Users</a>
<a href="<?php echo admin_url('options-general.php'); ?>" class="button">Settings</a></p> <a href="<?php echo esc_url( admin_url( 'options-general.php' ) ); ?>" class="button">Settings</a>
</div> </p>
<?php </div>
<?php
} }
/**
* Callback function for Posts menu item.
*/
function helix_posts_callback() { function helix_posts_callback() {
echo '<div id="helix-posts-root"></div>'; echo '<div id="helix-posts-root"></div>';
} }
/**
* Callback function for Users menu item.
*/
function helix_users_callback() { function helix_users_callback() {
echo '<div id="helix-users-root"></div>'; echo '<div id="helix-users-root"></div>';
} }

View file

@ -1,13 +1,26 @@
<?php <?php
add_action('rest_api_init', function () { /**
register_rest_route('helix/v1', '/settings', [ * Registers REST API routes for Helix.
'methods' => 'GET', *
'permission_callback' => '__return_true', * @package Helix
'callback' => function () { */
return [
'siteTitle' => get_option('blogname'), add_action(
'language' => get_option('WPLANG'), 'rest_api_init',
]; function () {
} register_rest_route(
]); 'helix/v1',
}); '/settings',
array(
'methods' => 'GET',
'permission_callback' => '__return_true',
'callback' => function () {
return array(
'siteTitle' => get_option( 'blogname' ),
'language' => get_option( 'WPLANG' ),
);
},
)
);
}
);

View file

@ -1,5 +1,5 @@
{ {
"name": "Helix", "name": "helix/helix",
"description": "Helix Modern WP Admin", "description": "Helix Modern WP Admin",
"type": "project", "type": "project",
"license": "GPL-2.0-or-later", "license": "GPL-2.0-or-later",

View file

@ -1,17 +1,39 @@
<?php <?php
add_action('admin_enqueue_scripts', function ($hook) { /**
if ($hook !== 'toplevel_page_helix') return; * Enqueues Helix assets and exposes configuration to the frontend.
*
* @package Helix
*/
wp_enqueue_script('helix-app', plugin_dir_url(__FILE__) . 'build/index.js', [], null, true); add_action(
'admin_enqueue_scripts',
function ( $hook ) {
if ( 'toplevel_page_helix' !== $hook ) {
return;
}
// Get the original route that was redirected wp_enqueue_script(
$original_route = isset($_GET['helix_route']) ? $_GET['helix_route'] : '/wp-admin/'; 'helix-app',
plugin_dir_url( __FILE__ ) . 'build/index.js',
array(),
'0.1.0',
array()
);
wp_localize_script('helix-app', 'helixData', [ // Get the original route that was redirected.
'restUrl' => esc_url_raw(rest_url('helix/v1/')), $original_route = filter_input( INPUT_GET, 'helix_route', FILTER_SANITIZE_URL );
'nonce' => wp_create_nonce('wp_rest'), $original_route = $original_route ? esc_url_raw( $original_route ) : '/wp-admin/';
'user' => wp_get_current_user(),
'originalRoute' => $original_route, wp_localize_script(
'adminUrl' => admin_url(), 'helix-app',
]); 'helixData',
}); array(
'restUrl' => esc_url_raw( rest_url( 'helix/v1/' ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'user' => wp_get_current_user(),
'originalRoute' => $original_route,
'adminUrl' => admin_url(),
)
);
}
);

View file

@ -1,10 +1,16 @@
<?php <?php
/* /**
Plugin Name: Helix Modern WP Admin * Main plugin file that bootstraps the Helix admin interface.
Description: A React-powered replacement for the WordPress admin UI. *
Version: 0.1.0 * @package Helix
Author: Your Name */
*/
/**
* Plugin Name: Helix Modern WP Admin
* Description: A React-powered replacement for the WordPress admin UI.
* Version: 0.1.0
* Author: Abhijit Bhatnagar
*/
require_once __DIR__ . '/admin/init.php'; require_once __DIR__ . '/admin/init.php';
require_once __DIR__ . '/admin/rest-routes.php'; require_once __DIR__ . '/admin/rest-routes.php';