From ed933ec642f8b0bc5a785bcfe83496dc0f148cdc Mon Sep 17 00:00:00 2001 From: Joe Fusco Date: Mon, 10 Jun 2024 14:41:21 -0400 Subject: [PATCH 01/21] Polish up root PHP file for release --- wpgraphql-ide.php | 297 ++++++++++++++++++++-------------------------- 1 file changed, 128 insertions(+), 169 deletions(-) diff --git a/wpgraphql-ide.php b/wpgraphql-ide.php index 82f2d7d..407910b 100644 --- a/wpgraphql-ide.php +++ b/wpgraphql-ide.php @@ -38,28 +38,28 @@ */ function graphql_logo_svg(): string { return << - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + XML; } @@ -77,11 +77,6 @@ function user_lacks_capability(): bool { /** * Determines if the current admin page is a dedicated WPGraphQL IDE page. * - * This function checks whether the current admin page is exclusively for either the new GraphQL IDE - * or the legacy GraphiQL IDE, distinguishing them from other admin pages where the IDE might be present - * in a drawer. It is designed to help in tailoring the admin interface or enqueuing specific scripts - * and styles on these dedicated IDE pages. - * * @return bool True if the current page is a dedicated WPGraphQL IDE page, false otherwise. */ function current_screen_is_dedicated_ide_page(): bool { @@ -169,12 +164,6 @@ function register_wpadminbar_menus(): void { /** * Registers a submenu page for the dedicated GraphQL IDE. * - * This function checks if the current user has the necessary capabilities - * to access the GraphQL IDE. If the user has the required capabilities, - * it adds a submenu page under a specified parent menu. The submenu page - * is intended to provide access to a dedicated GraphQL IDE within the WordPress - * admin area. - * * @see add_submenu_page() For more information on adding submenu pages. * @link https://developer.wordpress.org/reference/functions/add_submenu_page/ */ @@ -218,18 +207,18 @@ function enqueue_graphql_ide_menu_icon_css(): void { } $custom_css = ' - #wp-admin-bar-wpgraphql-ide .ab-icon::before, - #wp-admin-bar-wpgraphql-ide .ab-icon::before { - background-image: url("data:image/svg+xml;base64,' . base64_encode( graphql_logo_svg() ) . '"); - background-size: 100%; - border-radius: 12px; - box-sizing: border-box; - content: ""; - display: inline-block; - height: 24px; - width: 24px; - } - '; + #wp-admin-bar-wpgraphql-ide .ab-icon::before, + #wp-admin-bar-wpgraphql-ide .ab-icon::before { + background-image: url("data:image/svg+xml;base64,' . base64_encode( graphql_logo_svg() ) . '"); + background-size: 100%; + border-radius: 12px; + box-sizing: border-box; + content: ""; + display: inline-block; + height: 24px; + width: 24px; + } + '; wp_add_inline_style( 'admin-bar', $custom_css ); } @@ -331,7 +320,7 @@ function get_dedicated_ide_base_url(): string { * Retrieves the specific header of this plugin. * * @param string $key The plugin data key. - * @return string|null The version number of the plugin. Returns an empty string if the version is not found. + * @return string|null The version number of the plugin. Returns null if the version is not found. */ function get_plugin_header( string $key = '' ): ?string { if ( ! function_exists( 'get_plugin_data' ) ) { @@ -350,7 +339,7 @@ function get_plugin_header( string $key = '' ): ?string { /** * Retrieves app context. * - * @return array The possibly filtered app context array. + * @return array The possibly filtered app context array. */ function get_app_context(): array { $current_user = wp_get_current_user(); @@ -371,118 +360,92 @@ function get_app_context(): array { } /** - * Add styles to hide generic admin notices on the graphiql-ide page - * and play nice with notices added via register_graphql_admin_notice + * Adds styles to hide generic admin notices on the GraphQL IDE page. * - * @param array $notices The array of notices to render + * @param array $notices The array of notices to render. */ -add_action( - 'graphql_admin_notices_render_notices', - static function ( array $notices ) { - - echo ' - - '; - }, - 10, - 1 -); +function graphql_admin_notices_render_notices( array $notices ) { + echo ' + + '; +} +add_action( 'graphql_admin_notices_render_notices', __NAMESPACE__ . '\\graphql_admin_notices_render_notices', 10, 1 ); /** - * Add styles to apply top margin to notices added via register_graphql_admin_notice + * Adds styles to apply top margin to notices added via register_graphql_admin_notice. * - * @param string $notice_slug The slug of the notice - * @param array $notice The notice data - * @param bool $is_dismissable Whether the notice is dismissable - * @param int $count The count of notices + * @param string $notice_slug The slug of the notice. + * @param array $notice The notice data. + * @param bool $is_dismissable Whether the notice is dismissable. + * @param int $count The count of notices. */ -add_action( - 'graphql_admin_notices_render_notice', - static function ( string $notice_slug, array $notice, bool $is_dismissable, int $count ) { - - echo ' - - '; - }, - 10, - 4 -); +function graphql_admin_notices_render_notice( string $notice_slug, array $notice, bool $is_dismissable, int $count ) { + echo ' + + '; +} +add_action( 'graphql_admin_notices_render_notice', __NAMESPACE__ . '\\graphql_admin_notices_render_notice', 10, 4 ); /** - * Filter to allow graphql admin notices to be displayed on the dedicated IDE page. + * Filters to allow GraphQL admin notices to be displayed on the dedicated IDE page. * - * @param bool $is_plugin_scoped_page True if the current page is within scope of the plugin's pages. + * @param bool $is_plugin_scoped_page True if the current page is within scope of the plugin's pages. * @param string $current_page_id The ID of the current admin page. - * @param array $allowed_pages The list of allowed pages. + * @param array $allowed_pages The list of allowed pages. + * @return bool Whether the admin notice is allowed on the current page. */ -add_filter( - 'graphql_admin_notices_is_allowed_admin_page', - static function ( bool $is_plugin_scoped_page, string $current_page_id, array $allowed_pages ) { - - // If the current page is the dedicated IDE page, we want to allow notices to be displayed. - if ( 'graphql_page_graphql-ide' === $current_page_id ) { - return true; - } +function graphql_admin_notices_is_allowed_admin_page( bool $is_plugin_scoped_page, string $current_page_id, array $allowed_pages ): bool { + // If the current page is the dedicated IDE page, we want to allow notices to be displayed. + if ( 'graphql_page_graphql-ide' === $current_page_id ) { + return true; + } - return $is_plugin_scoped_page; - }, - 10, - 3 -); + return $is_plugin_scoped_page; +} +add_filter( 'graphql_admin_notices_is_allowed_admin_page', __NAMESPACE__ . '\\graphql_admin_notices_is_allowed_admin_page', 10, 3 ); /** * Modifies the script tag for specific scripts to add the 'defer' attribute. * - * This function checks if the script handle matches 'wpgraphql-ide' and, if so, - * adds the 'defer' attribute to the script tag. This allows the script to be executed - * after the HTML document is parsed but before the DOMContentLoaded event. - * - * @param string $tag The HTML `