From 41fbe5f3d74fceb8d6eec7c2cc05d59603ad79f6 Mon Sep 17 00:00:00 2001 From: Faisal Ahammad Date: Tue, 17 Feb 2026 23:59:30 +0600 Subject: [PATCH] fix(bootstrap): add standalone WP-CLI package support - Wrap add_action() call conditionally for WP-CLI bootstrap compatibility - Replace plugin_dir_path() with dirname() for standalone usage - Wrap GFForms/GFAddOn class loading conditionally in class-gf-cli.php - Make gf_cli() helper function safe when GF_CLI class is unavailable When installed as a standalone WP-CLI package via 'wp package install', the bootstrap runs before WordPress loads. Functions like add_action() and plugin_dir_path() are not available, and GFForms/GFAddOn classes don't exist yet. These changes ensure the CLI commands register correctly in both contexts. Fixes #39 --- class-gf-cli.php | 11 +++++++++-- cli.php | 18 +++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) 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; }