Skip to content

Commit

Permalink
chore!: Refactor plugin entrypoint to use WPGraphQL\FacetWP namespa…
Browse files Browse the repository at this point in the history
…ce. (#69)
  • Loading branch information
justlevine authored Apr 6, 2024
1 parent 4915292 commit 3d7a197
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 111 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- feat: Add WPGraphQL to Plugin Dependencies header.
- chore!: Refactor plugin entrypoint to use `WPGraphQL\FacetWP` namespace.
- chore: Implement strict phpstan rules and lint.
- chore: Update Composer dependencies and lint.
- ci: Test against WP 6.5.
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@
"/docker-compose.yml",
"/phpstan.neon.dist",
"/phpunit.xml.dist",
"/README.md",
"/strauss.phar"
"/README.md"
]
}
}
88 changes: 88 additions & 0 deletions src/Autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Includes the composer Autoloader used for packages and classes in the src/ directory.
*
* @package WPGraphQL\FacetWP
*/

declare( strict_types = 1 );

namespace WPGraphQL\FacetWP;

/**
* Class - Autoloader
*
* @internal
*/
class Autoloader {
/**
* Whether the autoloader has been loaded.
*
* @var bool
*/
protected static bool $is_loaded = false;

/**
* Attempts to autoload the Composer dependencies.
*/
public static function autoload(): bool {
// If we're not *supposed* to autoload anything, then return true.
if ( defined( 'WPGRAPHQL_FACETWP_AUTOLOAD' ) && false === WPGRAPHQL_FACETWP_AUTOLOAD ) {
return true;
}

if ( self::$is_loaded ) {
return self::$is_loaded;
}

$autoloader = dirname( __DIR__ ) . '/vendor/autoload.php';
self::$is_loaded = self::require_autoloader( $autoloader );

return self::$is_loaded;
}

/**
* Attempts to load the autoloader file, if it exists.
*
* @param string $autoloader_file The path to the autoloader file.
*/
protected static function require_autoloader( string $autoloader_file ): bool {
if ( ! is_readable( $autoloader_file ) ) {
self::missing_autoloader_notice();
return false;
}

return (bool) require_once $autoloader_file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable -- Autoloader is a Composer file.
}

/**
* Displays a notice if the autoloader is missing.
*/
protected static function missing_autoloader_notice(): void {
$error_message = __( 'WPGraphQL for FacetWP: The Composer autoloader was not found. If you installed the plugin from the GitHub source code, make sure to run `composer install`.', 'wpgraphql-facetwp' );

if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( esc_html( $error_message ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- This is a development notice.
}

$hooks = [
'admin_notices',
'network_admin_notices',
];

foreach ( $hooks as $hook ) {
add_action(
$hook,
static function () use ( $error_message ) {
?>
<div class="error notice">
<p>
<?php echo esc_html( $error_message ); ?>
</p>
</div>
<?php
}
);
}
}
}
19 changes: 1 addition & 18 deletions src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
final class Main {
/**
* Class instances.
* Class instance.
*
* @var ?self $instance
*/
Expand All @@ -29,13 +29,7 @@ final class Main {
*/
public static function instance(): self {
if ( ! isset( self::$instance ) || ! self::$instance instanceof self ) {
// @codeCoverageIgnoreStart
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// @codeCoverageIgnoreEnd
self::$instance = new self();
self::$instance->includes();
self::$instance->setup();
}

Expand All @@ -49,17 +43,6 @@ public static function instance(): self {
return self::$instance;
}

/**
* Includes the required files with Composer's autoload.
*
* @codeCoverageIgnore
*/
private function includes(): void {
if ( defined( 'WPGRAPHQL_FACETWP_AUTOLOAD' ) && false !== WPGRAPHQL_FACETWP_AUTOLOAD && defined( 'WPGRAPHQL_FACETWP_PLUGIN_DIR' ) ) {
require_once WPGRAPHQL_FACETWP_PLUGIN_DIR . 'vendor/autoload.php';
}
}

/**
* Sets up the schema.
*
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'AxeWP\\GraphQL\\Traits\\TypeNameTrait' => $vendorDir . '/axepress/wp-graphql-plugin-boilerplate/src/Traits/TypeNameTrait.php',
'AxeWP\\GraphQL\\Traits\\TypeResolverTrait' => $vendorDir . '/axepress/wp-graphql-plugin-boilerplate/src/Traits/TypeResolverTrait.php',
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
'WPGraphQL\\FacetWP\\Autoloader' => $baseDir . '/src/Autoloader.php',
'WPGraphQL\\FacetWP\\CoreSchemaFilters' => $baseDir . '/src/CoreSchemaFilters.php',
'WPGraphQL\\FacetWP\\Main' => $baseDir . '/src/Main.php',
'WPGraphQL\\FacetWP\\Registry\\FacetRegistry' => $baseDir . '/src/Registry/FacetRegistry.php',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ComposerStaticInita808aacd30a48beb897daf0a9480d602
'AxeWP\\GraphQL\\Traits\\TypeNameTrait' => __DIR__ . '/..' . '/axepress/wp-graphql-plugin-boilerplate/src/Traits/TypeNameTrait.php',
'AxeWP\\GraphQL\\Traits\\TypeResolverTrait' => __DIR__ . '/..' . '/axepress/wp-graphql-plugin-boilerplate/src/Traits/TypeResolverTrait.php',
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
'WPGraphQL\\FacetWP\\Autoloader' => __DIR__ . '/../..' . '/src/Autoloader.php',
'WPGraphQL\\FacetWP\\CoreSchemaFilters' => __DIR__ . '/../..' . '/src/CoreSchemaFilters.php',
'WPGraphQL\\FacetWP\\Main' => __DIR__ . '/../..' . '/src/Main.php',
'WPGraphQL\\FacetWP\\Registry\\FacetRegistry' => __DIR__ . '/../..' . '/src/Registry/FacetRegistry.php',
Expand Down
4 changes: 2 additions & 2 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => 'hsimah-services/wp-graphql-facetwp',
'pretty_version' => 'dev-develop',
'version' => 'dev-develop',
'reference' => '0336cd85aac00c493ae69101045b80bb844ef51f',
'reference' => '24fb12a59cd0cbe7e6e9c6581c8380ac252fe096',
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -22,7 +22,7 @@
'hsimah-services/wp-graphql-facetwp' => array(
'pretty_version' => 'dev-develop',
'version' => 'dev-develop',
'reference' => '0336cd85aac00c493ae69101045b80bb844ef51f',
'reference' => '24fb12a59cd0cbe7e6e9c6581c8380ac252fe096',
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down
171 changes: 82 additions & 89 deletions wp-graphql-facetwp.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
* License: GPL-3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*
* @package WPGraphQL_FacetWP
* @package WPGraphQL\FacetWP
* @author hsimah
* @license GPL-3
* @version 0.4.4
*/

namespace WPGraphQL\FacetWP;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
Expand All @@ -33,119 +35,110 @@
require_once __DIR__ . '/c3.php';
}

if ( ! function_exists( 'graphql_facetwp_constants' ) ) {
// Load the autoloader.
require_once __DIR__ . '/src/Autoloader.php';
if ( ! \WPGraphQL\FacetWP\Autoloader::autoload() ) {
return;
}

/**
* Define plugin constants.
*/
function graphql_facetwp_constants(): void {
// Plugin version.
if ( ! defined( 'WPGRAPHQL_FACETWP_VERSION' ) ) {
define( 'WPGRAPHQL_FACETWP_VERSION', '0.4.4' );
}

// Plugin Folder Path.
if ( ! defined( 'WPGRAPHQL_FACETWP_PLUGIN_DIR' ) ) {
define( 'WPGRAPHQL_FACETWP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}

// Plugin Folder URL.
if ( ! defined( 'WPGRAPHQL_FACETWP_PLUGIN_URL' ) ) {
define( 'WPGRAPHQL_FACETWP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}

// Plugin Root File.
if ( ! defined( 'WPGRAPHQL_FACETWP_PLUGIN_FILE' ) ) {
define( 'WPGRAPHQL_FACETWP_PLUGIN_FILE', __FILE__ );
}

// Whether to autoload the files or not.
if ( ! defined( 'WPGRAPHQL_FACETWP_AUTOLOAD' ) ) {
define( 'WPGRAPHQL_FACETWP_AUTOLOAD', true );
}
function constants(): void {
// Plugin version.
if ( ! defined( 'WPGRAPHQL_FACETWP_VERSION' ) ) {
define( 'WPGRAPHQL_FACETWP_VERSION', '0.4.4' );
}

// Plugin Folder Path.
if ( ! defined( 'WPGRAPHQL_FACETWP_PLUGIN_DIR' ) ) {
define( 'WPGRAPHQL_FACETWP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}

// Plugin Folder URL.
if ( ! defined( 'WPGRAPHQL_FACETWP_PLUGIN_URL' ) ) {
define( 'WPGRAPHQL_FACETWP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}

// Plugin Root File.
if ( ! defined( 'WPGRAPHQL_FACETWP_PLUGIN_FILE' ) ) {
define( 'WPGRAPHQL_FACETWP_PLUGIN_FILE', __FILE__ );
}

// Whether to autoload the files or not.
if ( ! defined( 'WPGRAPHQL_FACETWP_AUTOLOAD' ) ) {
define( 'WPGRAPHQL_FACETWP_AUTOLOAD', true );
}
}

if ( ! function_exists( 'graphql_facetwp_deps_not_ready' ) ) {
/**
* Checks if all the the required plugins are installed and activated.
*
* @return array<class-string,string> The list of missing dependencies.
* @return array<string,string> The list of missing dependencies.
*/
function graphql_facetwp_deps_not_ready(): array {
$wpgraphql_version = '1.6.0';
$facetwp_version = '4.0';

$deps = [];
function dependencies_not_ready(): array {
$wpgraphql_version = '1.6.0';
$facetwp_version = '4.0';

if ( ! class_exists( 'WPGraphQL' ) || ( defined( 'WPGRAPHQL_VERSION' ) && version_compare( WPGRAPHQL_VERSION, $wpgraphql_version, '<' ) ) ) {
$deps['WPGraphQL'] = $wpgraphql_version;
}
$deps = [];

if ( ! class_exists( 'FacetWP' ) || ( defined( 'FACETWP_VERSION' ) && version_compare( FACETWP_VERSION, $facetwp_version, '<' ) ) ) {
$deps['FacetWP'] = $facetwp_version;
}
if ( ! class_exists( 'WPGraphQL' ) || ( defined( 'WPGRAPHQL_VERSION' ) && version_compare( WPGRAPHQL_VERSION, $wpgraphql_version, '<' ) ) ) {
$deps['WPGraphQL'] = $wpgraphql_version;
}

return $deps;
if ( ! class_exists( 'FacetWP' ) || ( defined( 'FACETWP_VERSION' ) && version_compare( FACETWP_VERSION, $facetwp_version, '<' ) ) ) {
$deps['FacetWP'] = $facetwp_version;
}

return $deps;
}

if ( ! function_exists( 'graphql_facetwp_init' ) ) {
/**
* Initializes the plugin.
*/
function graphql_facetwp_init(): void {
graphql_facetwp_constants();

$not_ready = graphql_facetwp_deps_not_ready();

if ( empty( $not_ready ) && defined( 'WPGRAPHQL_FACETWP_PLUGIN_DIR' ) ) {
require_once WPGRAPHQL_FACETWP_PLUGIN_DIR . 'src/Main.php';
\WPGraphQL\FacetWP\Main::instance();

return;
}

/**
* For users with lower capabilities, don't show the notice.
*
* @todo Are we sure we don't what to tell all users with backend access that the plugin isnt working?
*/
if ( ! current_user_can( 'manage_options' ) ) {
return;
}

foreach ( $not_ready as $dep => $version ) {
add_action(
'admin_notices',
static function () use ( $dep, $version ) {
?>
function init(): void {
constants();

$not_ready = dependencies_not_ready();

if ( empty( $not_ready ) && defined( 'WPGRAPHQL_FACETWP_PLUGIN_DIR' ) ) {
require_once WPGRAPHQL_FACETWP_PLUGIN_DIR . 'src/Main.php';
\WPGraphQL\FacetWP\Main::instance();

return;
}

/**
* For users with lower capabilities, don't show the notice.
*
* @todo Are we sure we don't what to tell all users with backend access that the plugin isnt working?
*/
if ( ! current_user_can( 'manage_options' ) ) {
return;
}

foreach ( $not_ready as $dep => $version ) {
add_action(
'admin_notices',
static function () use ( $dep, $version ) {
?>
<div class="error notice">
<p>
<?php
printf(
/* translators: dependency not ready error message */
esc_html__( '%1$s (v%2$s) must be active for WPGraphQL Plugin Name to work.', 'wpgraphql-facetwp' ),
esc_attr( $dep ),
esc_attr( $version )
);
?>
<?php
printf(
/* translators: dependency not ready error message */
esc_html__( '%1$s (v%2$s) must be active for WPGraphQL Plugin Name to work.', 'wpgraphql-facetwp' ),
esc_attr( $dep ),
esc_attr( $version )
);
?>
</p>
</div>

<?php
}
);
}
}
);
}
}

add_action( 'facetwp_init', 'graphql_facetwp_init' );

add_filter(
'facetwp_graphql_facet_connection_config',
static function ( array $default_graphql_config ) {
return $default_graphql_config;
},
10,
1
);
add_action( 'facetwp_init', 'WPGraphQL\FacetWP\init' );

0 comments on commit 3d7a197

Please sign in to comment.