From e798bc8d27144dff674aa05121a2c346841cbab2 Mon Sep 17 00:00:00 2001 From: Joe Fusco Date: Mon, 5 Aug 2024 13:59:20 -0400 Subject: [PATCH 1/4] Reorder GraphQL submenu items --- wpgraphql-ide.php | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/wpgraphql-ide.php b/wpgraphql-ide.php index 5e198a0..01262ca 100644 --- a/wpgraphql-ide.php +++ b/wpgraphql-ide.php @@ -296,6 +296,60 @@ function register_dedicated_ide_menu(): void { 'graphql-ide', __NAMESPACE__ . '\\render_dedicated_ide_page' ); + + add_action( 'admin_menu', __NAMESPACE__ . '\\reorder_graphql_submenu_items', 100 ); +} + +/** + * Reorder the submenu items under the GraphQL menu. + */ +function reorder_graphql_submenu_items(): void { + global $submenu; + + $graphql_ide_settings = get_option( 'graphql_ide_settings', [] ); + $show_legacy_editor = isset( $graphql_ide_settings['graphql_ide_show_legacy_editor'] ) ? $graphql_ide_settings['graphql_ide_show_legacy_editor'] : 'off'; + + if ( isset( $submenu['graphiql-ide'] ) ) { + $ordered_submenu = []; + + // Always add GraphQL IDE first. + foreach ( $submenu['graphiql-ide'] as $item ) { + if ( 'GraphQL IDE' === $item[0] ) { + $ordered_submenu[] = $item; + break; + } + } + + // Conditionally add GraphiQL IDE second. + if ( 'on' === $show_legacy_editor ) { + foreach ( $submenu['graphiql-ide'] as $item ) { + if ( 'GraphiQL IDE' === $item[0] ) { + $item[0] = 'Legacy GraphQL IDE'; + $ordered_submenu[] = $item; + break; + } + } + } + + // Add Extensions next. + foreach ( $submenu['graphiql-ide'] as $item ) { + if ( 'Extensions' === $item[0] ) { + $ordered_submenu[] = $item; + break; + } + } + + // Add Settings last. + foreach ( $submenu['graphiql-ide'] as $item ) { + if ( 'Settings' === $item[0] ) { + $ordered_submenu[] = $item; + break; + } + } + + // Apply the reordered submenu. + $submenu['graphiql-ide'] = $ordered_submenu; + } } /** From cb6eda03e08fff3870f73146336a4255a42aea5c Mon Sep 17 00:00:00 2001 From: Joe Fusco Date: Mon, 5 Aug 2024 14:01:21 -0400 Subject: [PATCH 2/4] Add changeset --- .changeset/little-dancers-raise.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/little-dancers-raise.md diff --git a/.changeset/little-dancers-raise.md b/.changeset/little-dancers-raise.md new file mode 100644 index 0000000..bf1600b --- /dev/null +++ b/.changeset/little-dancers-raise.md @@ -0,0 +1,5 @@ +--- +"wpgraphql-ide": patch +--- + +Reorder sidebar menu to always have the IDE first. From 4427198e8baeb8b1eeb44683002e6746db71141d Mon Sep 17 00:00:00 2001 From: Joe Fusco Date: Mon, 5 Aug 2024 14:12:12 -0400 Subject: [PATCH 3/4] Avoid overriding WordPress globals --- wpgraphql-ide.php | 74 +++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/wpgraphql-ide.php b/wpgraphql-ide.php index 01262ca..404cc7c 100644 --- a/wpgraphql-ide.php +++ b/wpgraphql-ide.php @@ -270,7 +270,7 @@ function register_wpadminbar_menus(): void { } /** - * Registers a submenu page for the dedicated GraphQL IDE. + * Registers a submenu page for the dedicated GraphQL IDE and reorder the items. * * @see add_submenu_page() For more information on adding submenu pages. * @link https://developer.wordpress.org/reference/functions/add_submenu_page/ @@ -297,6 +297,7 @@ function register_dedicated_ide_menu(): void { __NAMESPACE__ . '\\render_dedicated_ide_page' ); + // Reorder the submenu items. add_action( 'admin_menu', __NAMESPACE__ . '\\reorder_graphql_submenu_items', 100 ); } @@ -306,48 +307,51 @@ function register_dedicated_ide_menu(): void { function reorder_graphql_submenu_items(): void { global $submenu; - $graphql_ide_settings = get_option( 'graphql_ide_settings', [] ); - $show_legacy_editor = isset( $graphql_ide_settings['graphql_ide_show_legacy_editor'] ) ? $graphql_ide_settings['graphql_ide_show_legacy_editor'] : 'off'; - if ( isset( $submenu['graphiql-ide'] ) ) { - $ordered_submenu = []; - - // Always add GraphQL IDE first. - foreach ( $submenu['graphiql-ide'] as $item ) { - if ( 'GraphQL IDE' === $item[0] ) { - $ordered_submenu[] = $item; - break; - } - } - - // Conditionally add GraphiQL IDE second. - if ( 'on' === $show_legacy_editor ) { - foreach ( $submenu['graphiql-ide'] as $item ) { - if ( 'GraphiQL IDE' === $item[0] ) { - $item[0] = 'Legacy GraphQL IDE'; - $ordered_submenu[] = $item; + $graphql_ide_settings = get_option( 'graphql_ide_settings', [] ); + $show_legacy_editor = isset( $graphql_ide_settings['graphql_ide_show_legacy_editor'] ) ? $graphql_ide_settings['graphql_ide_show_legacy_editor'] : 'off'; + + // Extract existing submenu items. + $graphql_ide = null; + $graphiql_ide = null; + $extensions = null; + $settings = null; + + foreach ( $submenu['graphiql-ide'] as $key => $item ) { + switch ( $item[0] ) { + case 'GraphQL IDE': + $graphql_ide = $item; + break; + case 'GraphiQL IDE': + $graphiql_ide = $item; + break; + case 'Extensions': + $extensions = $item; + break; + case 'Settings': + $settings = $item; break; - } } } - // Add Extensions next. - foreach ( $submenu['graphiql-ide'] as $item ) { - if ( 'Extensions' === $item[0] ) { - $ordered_submenu[] = $item; - break; - } - } + // Create the reordered submenu array. + $ordered_submenu = []; - // Add Settings last. - foreach ( $submenu['graphiql-ide'] as $item ) { - if ( 'Settings' === $item[0] ) { - $ordered_submenu[] = $item; - break; - } + if ( $graphql_ide ) { + $ordered_submenu[] = $graphql_ide; + } + if ( 'on' === $show_legacy_editor && $graphiql_ide ) { + $graphiql_ide[0] = 'Legacy GraphQL IDE'; + $ordered_submenu[] = $graphiql_ide; + } + if ( $extensions ) { + $ordered_submenu[] = $extensions; + } + if ( $settings ) { + $ordered_submenu[] = $settings; } - // Apply the reordered submenu. + // Merge the reordered submenu back into the global $submenu. $submenu['graphiql-ide'] = $ordered_submenu; } } From 2999b032f5c4270ac4cc3326c598420646740db7 Mon Sep 17 00:00:00 2001 From: Joe Fusco Date: Mon, 5 Aug 2024 14:16:53 -0400 Subject: [PATCH 4/4] Resolve linting errors --- wpgraphql-ide.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/wpgraphql-ide.php b/wpgraphql-ide.php index 404cc7c..d48e972 100644 --- a/wpgraphql-ide.php +++ b/wpgraphql-ide.php @@ -317,12 +317,12 @@ function reorder_graphql_submenu_items(): void { $extensions = null; $settings = null; - foreach ( $submenu['graphiql-ide'] as $key => $item ) { + foreach ( $submenu['graphiql-ide'] as $item ) { switch ( $item[0] ) { case 'GraphQL IDE': $graphql_ide = $item; break; - case 'GraphiQL IDE': + case 'GraphiQL IDE': // Legacy menu item. $graphiql_ide = $item; break; case 'Extensions': @@ -352,6 +352,7 @@ function reorder_graphql_submenu_items(): void { } // Merge the reordered submenu back into the global $submenu. + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $submenu['graphiql-ide'] = $ordered_submenu; } }