fix(bootstrap): Add standalone WP-CLI package support#48
Open
faisalahammad wants to merge 1 commit intogravityforms:masterfrom
Open
fix(bootstrap): Add standalone WP-CLI package support#48faisalahammad wants to merge 1 commit intogravityforms:masterfrom
faisalahammad wants to merge 1 commit intogravityforms:masterfrom
Conversation
- 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 gravityforms#39
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🎯 Summary
Adds standalone WP-CLI package support so
gravityformsclican be installed viawp package installwithout requiring it to be a WordPress plugin, while maintaining full backward compatibility as a plugin.📋 Issue Reference
Fixes #39
🔍 Problem Description
Current Behavior
When installing as a standalone WP-CLI package (
wp package install gravityforms/gravityformscli), the bootstrap process fails because:add_action()is not available during WP-CLI's Composer autoload phaseplugin_dir_path()is a WordPress function not available outside the WP contextGFForms::include_addon_framework()fatal errors when Gravity Forms isn't loadedclass GF_CLI extends GFAddOnfatal errors when the parent class doesn't existExpected Behavior
The CLI commands should register and work when installed as a standalone WP-CLI package. The
before_invokecallback already handles the case where Gravity Forms isn't installed — it shows a helpful error message. The bootstrap just needs to get that far without fataling.Root Cause
The bootstrap code assumes it will always run in a WordPress plugin context where
add_action(),plugin_dir_path(), and Gravity Forms classes are available. When Composer autoloadscli.phpduringwp package install, none of these exist yet — WordPress hasn't bootstrapped.✨ Solution Overview
Approach Taken
Make the WordPress-specific code paths conditional while keeping the WP-CLI command registration unconditional. This is a minimal, surgical change — only the parts that fail in standalone mode are wrapped in availability checks.
Why This Approach
defined('ABSPATH') || defined('WP_CLI')guardsAlternatives Considered
🔧 Changes Made
Files Modified
cli.php— Conditionaladd_action(),dirname()replacement, safegf_cli()helperclass-gf-cli.php— Conditional addon framework loading and class definitionDetailed Changes
1.
cli.php— Bootstrap CompatibilityChange 1: Conditional
add_action()Before:
After:
Change 2:
dirname()replacementBefore:
After:
Change 3: Safe
gf_cli()helperBefore:
After:
Why These Work:
function_exists('add_action')is the standard way to check for WordPress function availabilitydirname(__FILE__)is functionally identical toplugin_dir_path(__FILE__)(which is just a wrapper aroundtrailingslashit(dirname()))gf_cli()guard prevents fatal errors whenGF_CLIclass isn't defined (standalone mode)2.
class-gf-cli.php— Conditional Addon RegistrationBefore:
After:
Why This Works:
GFFormsandGFAddOnare available → addon registers normally ✅GF_CLI(the addon class) — they use their own classes inincludes/which are loaded byload_cli()Impact:
wp package installusagebefore_invokecallback still validates Gravity Forms is available before running commands🧪 Testing Performed
Verification
Code Path Analysis
ABSPATHWP_CLIadd_actionGFFormsbefore_invokevalidates GFdie()— blocked by security guard* Until WordPress bootstraps, then these become available
Expected Usage
After these changes, users can install as a standalone WP-CLI package:
📊 Performance Impact
Analysis: Zero performance impact. The added
function_exists()andclass_exists()checks are negligible (microseconds) and only run once during bootstrap.🔒 Security Considerations
defined('ABSPATH') || defined('WP_CLI') || die()guards remain in all filesbefore_invokecallback still validates Gravity Forms availability before any data-touching command runs♿ Accessibility
N/A — Backend/CLI changes only, no UI modifications.
🌍 Internationalization
N/A — No new user-facing strings added.
✅ No breaking changes — Fully backward compatible.
The plugin continues to work identically when installed as a WordPress plugin. The only difference is that it also works when installed as a standalone WP-CLI package.
✅ PR Checklist
🤝 Additional Context
composer.jsonalready has"type": "wp-cli-package"and the correct autoload configuration ("files": ["cli.php"]), so no changes needed thereincludes/*.php) already have the dualABSPATH || WP_CLIguard pattern — no changes needed there eitherReady for Review ✨