Skip to content

Commit

Permalink
chore!: refactor plugin instantiation (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
justlevine committed Apr 6, 2024
1 parent 088d49e commit a884694
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 199 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- fix: Plugin versions in dependency check logic is now in sync with the version requirements.
- chore!: Add `WPGraphQL/RankMath` namespace to root-level files ( `activation.php`, `deactivation.php`, `wp-graphql-rank-math.php` ).
- chore: Refactor autoloader logic to `Autoloader` class.
- chore: Update Composer dev-deps and fix newly-surfaced PHPCS smells.
- chore: Implement PHPStan strict rules.
- ci: Update GitHub Actions to latest versions.
Expand Down
22 changes: 11 additions & 11 deletions activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
* @package WPGraphql\RankMath
*/

if ( ! function_exists( 'graphql_seo_activation_callback' ) ) {
/**
* Runs when the plugin is activated.
*/
function graphql_seo_activation_callback(): callable {
return static function (): void {
do_action( 'graphql_seo_activate' );
namespace WPGraphQL\RankMath;

// store the current version of the plugin.
update_option( 'wp_graphql_seo_version', WPGRAPHQL_SEO_VERSION );
};
}
/**
* Runs when the plugin is activated.
*/
function activation_callback(): callable {
return static function (): void {
do_action( 'graphql_seo_activate' );

// store the current version of the plugin.
update_option( 'wp_graphql_seo_version', WPGRAPHQL_SEO_VERSION );
};
}
80 changes: 39 additions & 41 deletions deactivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,53 @@
* @package WPGraphql\RankMath
*/

if ( ! function_exists( 'graphql_seo_deactivation_callback' ) ) {
/**
* Runs when WPGraphQL is de-activated.
*
* This cleans up data that WPGraphQL stores.
*/
function graphql_seo_deactivation_callback(): callable {
return static function (): void {

// Fire an action when WPGraphQL is de-activating.
do_action( 'graphql_seo_deactivate' );

// Delete data during activation.
graphql_seo_delete_data();
};
}
namespace WPGraphQL\RankMath;

/**
* Runs when WPGraphQL is de-activated.
*
* This cleans up data that WPGraphQL stores.
*/
function deactivation_callback(): callable {
return static function (): void {

// Fire an action when WPGraphQL is de-activating.
do_action( 'graphql_seo_deactivate' );

// Delete data during activation.
delete_data();
};
}

if ( ! function_exists( 'graphql_seo_delete_data' ) ) {
/**
* Delete data on deactivation.
*/
function graphql_seo_delete_data(): void {
/**
* Delete data on deactivation.
*/
function delete_data(): void {

// Check if the plugin is set to delete data or not.
$delete_data = graphql_seo_get_setting( 'delete_data_on_deactivate' );
// Check if the plugin is set to delete data or not.
$delete_data = graphql_seo_get_setting( 'delete_data_on_deactivate' );

// Bail if not set to delete.
if ( 'on' !== $delete_data ) {
return;
}
// Bail if not set to delete.
if ( 'on' !== $delete_data ) {
return;
}

// Delete plugin version.
delete_option( 'wp_graphql_seo_version' );
// Delete plugin version.
delete_option( 'wp_graphql_seo_version' );

// Initialize the settings API.
$settings = new \WPGraphQL\RankMath\Admin\Settings\Settings();
$settings::register_settings();
// Initialize the settings API.
$settings = new \WPGraphQL\RankMath\Admin\Settings\Settings();
$settings::register_settings();

// Get all the registered settings fields.
$fields = $settings::get_settings_api()->get_settings_fields();
// Get all the registered settings fields.
$fields = $settings::get_settings_api()->get_settings_fields();

// Loop over the registered settings fields and delete the options.
if ( ! empty( $fields ) && is_array( $fields ) ) {
foreach ( $fields as $group => $fields ) {
delete_option( $group );
}
// Loop over the registered settings fields and delete the options.
if ( ! empty( $fields ) && is_array( $fields ) ) {
foreach ( $fields as $group => $fields ) {
delete_option( $group );
}

do_action( 'graphql_seo_delete_data' );
}

do_action( 'graphql_seo_delete_data' );
}
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\RankMath
*/

declare( strict_types = 1 );

namespace WPGraphQL\RankMath;

/**
* 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_SEO_AUTOLOAD' ) && false === WPGRAPHQL_SEO_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 RankMath: The Composer autoloader was not found. If you installed the plugin from the GitHub source code, make sure to run `composer install`.', 'wp-graphql-rank-math' );

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
}
);
}
}
}
17 changes: 1 addition & 16 deletions src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@ final class Main {
* Constructor
*/
public static function instance(): self {
if ( ! isset( self::$instance ) || ! ( is_a( self::$instance, self::class ) ) ) {
if ( ! isset( self::$instance ) ) {
// You cant test a singleton.
// @codeCoverageIgnoreStart .
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
self::$instance = new self();
self::$instance->includes();
self::$instance->setup();
// @codeCoverageIgnoreEnd
}
Expand All @@ -50,17 +46,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_SEO_AUTOLOAD' ) && false !== WPGRAPHQL_SEO_AUTOLOAD && defined( 'WPGRAPHQL_SEO_PLUGIN_DIR' ) ) {
require_once WPGRAPHQL_SEO_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 @@ -8,6 +8,7 @@
return array(
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
'WPGraphQL\\RankMath\\Admin\\Settings\\Settings' => $baseDir . '/src/Admin/Settings/Settings.php',
'WPGraphQL\\RankMath\\Autoloader' => $baseDir . '/src/Autoloader.php',
'WPGraphQL\\RankMath\\CoreSchemaFilters' => $baseDir . '/src/CoreSchemaFilters.php',
'WPGraphQL\\RankMath\\Fields\\RootQuery' => $baseDir . '/src/Fields/RootQuery.php',
'WPGraphQL\\RankMath\\Main' => $baseDir . '/src/Main.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 @@ -35,6 +35,7 @@ class ComposerStaticInit0e21517c8407dd3be5d5cb3416c889d5
public static $classMap = array (
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
'WPGraphQL\\RankMath\\Admin\\Settings\\Settings' => __DIR__ . '/../..' . '/src/Admin/Settings/Settings.php',
'WPGraphQL\\RankMath\\Autoloader' => __DIR__ . '/../..' . '/src/Autoloader.php',
'WPGraphQL\\RankMath\\CoreSchemaFilters' => __DIR__ . '/../..' . '/src/CoreSchemaFilters.php',
'WPGraphQL\\RankMath\\Fields\\RootQuery' => __DIR__ . '/../..' . '/src/Fields/RootQuery.php',
'WPGraphQL\\RankMath\\Main' => __DIR__ . '/../..' . '/src/Main.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' => 'axepress/wp-graphql-rank-math',
'pretty_version' => 'dev-develop',
'version' => 'dev-develop',
'reference' => '4cfb718e0092a85d92ab13d4d8a5cdd5e270d611',
'reference' => '088d49e35e0caa606761d1734ede065d5b398c29',
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -22,7 +22,7 @@
'axepress/wp-graphql-rank-math' => array(
'pretty_version' => 'dev-develop',
'version' => 'dev-develop',
'reference' => '4cfb718e0092a85d92ab13d4d8a5cdd5e270d611',
'reference' => '088d49e35e0caa606761d1734ede065d5b398c29',
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down

0 comments on commit a884694

Please sign in to comment.