Skip to content

Commit

Permalink
Add Dependency checking (#73)
Browse files Browse the repository at this point in the history
* version bump

* helper function to get a cmb2 path

* don't activate if cmb2 wasn't found

* try to get the cmb2 path

* only add the cmb2 path if we found it

* do a similar handling of extended cpts

* composer update

* lint
  • Loading branch information
jazzsequence authored Oct 10, 2024
1 parent d9e6b19 commit 5f016c9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
64 changes: 61 additions & 3 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Games Collector
* Plugin URI: https://github.com/jazzsequence/games-collector
* Description: Catalog all your tabletop (or other) games in your WordPress site and display a list of games in your collection.
* Version: 1.3.4
* Version: 1.3.6
* Author: Chris Reynolds
* Author URI: https://jazzsequence.com
* Donate link: https://paypal.me/jazzsequence
Expand Down Expand Up @@ -43,16 +43,32 @@
* @since 1.1.0
*/
function autoload_init() {
$cmb2_path = maybe_get_cmb2_path();

// Add in some specific includes and vendor libraries.
$files = [
__DIR__ . '/vendor/cmb2/cmb2/init.php',
__DIR__ . '/inc/namespace.php',
__DIR__ . '/inc/functions.php',
];

// Only add CMB2 if we found it.
if ( $cmb2_path ) {
$files[] = $cmb2_path;
}

// Check for extended cpts, load it if it hasn't already been loaded.
if ( ! function_exists( 'register_extended_post_type' ) ) {
$files[] = __DIR__ . '/vendor/johnbillion/extended-cpts/extended-cpts.php';
$plugin_vendor = __DIR__ . '/vendor/johnbillion/extended-cpts/extended-cpts.php';
$root_vendor = ABSPATH . '/vendor/johnbillion/extended-cpts/extended-cpts.php';

// Check if extended cpts exists. If not, deactivate the plugin.
$exists = file_exists( $plugin_vendor ) || file_exists( $root_vendor );
if ( $exists ) {
$files[] = $exists ? $plugin_vendor : $root_vendor;
} else {
// If it's not loaded, deactivate the plugin.
deactivate_plugins( plugin_basename( __FILE__ ) );
}
}

// Autoload the namespaces.
Expand All @@ -67,6 +83,42 @@ function autoload_init() {
}
}

/**
* Try to get the path to the CMB2 library.
*
* @since 1.3.6
*/
function maybe_get_cmb2_path() {
// If the CMB2 library is already loaded, we don't need to load it.
if ( function_exists( 'cmb2_bootstrap' ) ) {
return '';
}

// Maybe load from the vendor directory.
if ( file_exists( __DIR__ . '/vendor/cmb2/init.php' ) ) {
return __DIR__ . '/vendor/cmb2/init.php';
}

// Maybe load from the root /vendor directory.
if ( file_exists( ABSPATH . '/vendor/cmb2/init.php' ) ) {
return ABSPATH . '/vendor/cmb2/init.php';
}

// Was it installed as a plugin?
if ( file_exists( WP_PLUGIN_DIR . '/cmb2/init.php' ) ) {
// Activate the plugin.
activate_plugin( 'cmb2' );
}

// Last chance, maybe it's in the mu-plugins directory. If it's here, it should already be activated.
if ( file_exists( WPMU_PLUGIN_DIR . '/cmb2/init.php' ) ) {
return WPMU_PLUGIN_DIR . '/cmb2/init.php';
}

// If we got here, we couldn't find CMB2.
return '';
}

/**
* Main initialization function.
*
Expand All @@ -76,6 +128,12 @@ function init() {
// Load all the required files.
autoload_init();

// If CMB2 was not loaded, deactivate ourself.
if ( ! function_exists( 'cmb2_bootstrap' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
return;
}

// Register activation hook.
register_activation_hook( __FILE__, __NAMESPACE__ . '\\activate' );

Expand Down
2 changes: 1 addition & 1 deletion tests/test-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GC_Test_Game_Collector_API extends WP_UnitTestCase {
/**
* Kick off the rest api.
*/
public function setUp() : void {
public function setUp(): void {
parent::setUp();
global $wp_rest_server;
$wp_rest_server = new \WP_Rest_Server();
Expand Down
2 changes: 1 addition & 1 deletion tests/test-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public function test_single_game() {
'Shortcode output did not match expected output.'
);

foreach( ['chrononauts', 'hanabi'] as $game ) {
foreach ( [ 'chrononauts', 'hanabi' ] as $game ) {
// Make sure we can get more than one game as an array.
$this->assertStringContainsString(
$games[ $game ]->post_title,
Expand Down
12 changes: 6 additions & 6 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php return array(
'root' => array(
'name' => 'jazzsequence/games-collector',
'pretty_version' => 'dev-develop',
'version' => 'dev-develop',
'reference' => '4deaa55152aa1117c0cf2d4fe3c183f1ee852a68',
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => '0dd5d612c2011e2ce8a248ca927488078970e1e5',
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down Expand Up @@ -65,9 +65,9 @@
'dev_requirement' => false,
),
'jazzsequence/games-collector' => array(
'pretty_version' => 'dev-develop',
'version' => 'dev-develop',
'reference' => '4deaa55152aa1117c0cf2d4fe3c183f1ee852a68',
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => '0dd5d612c2011e2ce8a248ca927488078970e1e5',
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down

0 comments on commit 5f016c9

Please sign in to comment.