diff --git a/class-gf-cli.php b/class-gf-cli.php index 7845ecd..0bc4ada 100644 --- a/class-gf-cli.php +++ b/class-gf-cli.php @@ -2,8 +2,13 @@ defined( 'ABSPATH' ) || defined( 'WP_CLI' ) || die(); -// Include the Gravity Forms add-on framework -GFForms::include_addon_framework(); +// Include the Gravity Forms add-on framework if available. +// When running as a standalone WP-CLI package, GFForms is not loaded. +if ( class_exists( 'GFForms' ) ) { + GFForms::include_addon_framework(); +} + +if ( class_exists( 'GFAddOn' ) ) { class GF_CLI extends GFAddOn { /** @@ -100,5 +105,7 @@ private function __clone() { } +} // End class_exists( 'GFAddOn' ) check. + diff --git a/cli.php b/cli.php index ac069d9..6bdb3d4 100644 --- a/cli.php +++ b/cli.php @@ -34,8 +34,11 @@ define( 'GF_CLI_MIN_GF_VERSION', '1.9.17.8' ); -// After GF is loaded, load the CLI add-on -defined( 'ABSPATH' ) && add_action( 'gform_loaded', array( 'GF_CLI_Bootstrap', 'load_addon' ), 1 ); +// After GF is loaded, load the CLI add-on. +// When running as a standalone WP-CLI package, add_action() is not available. +if ( defined( 'ABSPATH' ) && function_exists( 'add_action' ) ) { + add_action( 'gform_loaded', array( 'GF_CLI_Bootstrap', 'load_addon' ), 1 ); +} @@ -58,7 +61,8 @@ class GF_CLI_Bootstrap { public static function load_addon() { // Requires the class file - require_once( plugin_dir_path( __FILE__ ) . '/class-gf-cli.php' ); + // Use dirname() instead of plugin_dir_path() for standalone WP-CLI package compatibility. + require_once( dirname( __FILE__ ) . '/class-gf-cli.php' ); // Registers the class name with GFAddOn GFAddOn::register( 'GF_CLI' ); @@ -105,9 +109,13 @@ public static function before_invoke() { * Returns an instance of the GF_CLI class * * @since 1.0-beta-1 - * @return object An instance of the GF_CLI class + * @return object|null An instance of the GF_CLI class, or null if not available. */ function gf_cli() { - return GF_CLI::get_instance(); + if ( class_exists( 'GF_CLI' ) ) { + return GF_CLI::get_instance(); + } + + return null; }