-
Notifications
You must be signed in to change notification settings - Fork 92
gcoai-stream-loading-text-animation.php: Added a new snippet for customizing the Steam field's loading text.
#1187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
45b8256
`gcoai-stream-loading-text-animation.php`: Added a new snippet for cu…
SebastianWiz 2f63853
`gcoai-stream-loading-text-animation.php`: Added a new snippet for cu…
SebastianWiz 4362519
`gcoai-stream-loading-text-animation.php`: Added a new snippet for cu…
SebastianWiz 4e3a704
`gcoai-stream-loading-text-animation.php`: Added a new snippet for cu…
saifsultanc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| <?php | ||
| /** | ||
| * Gravity Connect // OpenAI // Stream Loading Text Animation | ||
| * | ||
| * Adds a customizable shimmer animation and rotating spinner icon to the Stream field's | ||
| * loading placeholders. Replaces the static "Loading..." text with animated text and/or | ||
| * a rotating spinner icon. | ||
| */ | ||
| class GCOAI_Loading_Animation { | ||
|
|
||
| private $args; | ||
|
|
||
| public function __construct( $args = array() ) { | ||
| $this->args = wp_parse_args( $args, array( | ||
| 'text' => 'Thinking...', | ||
| 'base_color' => '#000', | ||
| 'shimmer_color' => '#fff', | ||
| 'shimmer_duration' => '2.2s', | ||
| 'show_shimmer' => true, | ||
| 'show_spinner' => false, | ||
| 'spinner_size' => '24', | ||
| 'form_id' => null, | ||
| 'field_id' => null, | ||
| ) ); | ||
|
|
||
| add_filter( 'gform_gcoai_field_loading_text', array( $this, 'filter_loading_text' ), 10, 3 ); | ||
| add_action( 'gform_register_init_scripts', array( $this, 'register_init_script' ), 10, 2 ); | ||
| } | ||
|
|
||
| public function register_init_script( $form, $is_ajax ) { | ||
| if ( empty( $form['id'] ) ) { | ||
| return; | ||
| } | ||
|
|
||
| // If form_id is specified, only run scripts on those forms | ||
| if ( $this->args['form_id'] !== null ) { | ||
| $form_ids = is_array( $this->args['form_id'] ) ? $this->args['form_id'] : array( $this->args['form_id'] ); | ||
| if ( ! in_array( $form['id'], $form_ids ) ) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| $markup = $this->get_shimmer_markup(); | ||
| $css = $this->get_styles_css(); | ||
|
|
||
| ?> | ||
| <script type="text/javascript"> | ||
| (function($) { | ||
| var shimmerMarkup = <?php echo wp_json_encode( $markup ); ?>; | ||
| var shimmerStyles = <?php echo wp_json_encode( $css ); ?>; | ||
|
|
||
| function addStylesToPage() { | ||
| if ( ! $('style.gw-gcoai-shimmer-style').length ) { | ||
| $('<style>') | ||
| .addClass('gw-gcoai-shimmer-style') | ||
| .text(shimmerStyles) | ||
| .appendTo('head'); | ||
| } | ||
| } | ||
|
|
||
| function applyShimmerToPlaceholders($container) { | ||
| var $searchContext = $container && $container.length ? $container : $(document); | ||
| $searchContext.find('.gcoai-output .gcoai-placeholder').html(shimmerMarkup); | ||
| } | ||
|
|
||
| if ( window.gform && typeof window.gform.addFilter === 'function' ) { | ||
| window.gform.addFilter('gcoai_stream_loading_placeholder', function(current, instance) { | ||
| return shimmerMarkup; | ||
| }); | ||
| } | ||
|
|
||
| $(function() { | ||
| addStylesToPage(); | ||
| applyShimmerToPlaceholders(); | ||
| }); | ||
|
|
||
| // Re-apply after Generate/Regenerate clicks | ||
| $(document).on('click', '.gcoai-trigger, .gcoai-regenerate', function() { | ||
| setTimeout(function() { | ||
| applyShimmerToPlaceholders(); | ||
| }, 50); | ||
| }); | ||
|
|
||
| // Re-apply after AJAX completes | ||
| $(document).ajaxComplete(function() { | ||
| applyShimmerToPlaceholders(); | ||
| }); | ||
| })(jQuery); | ||
| </script> | ||
| <?php | ||
| } | ||
|
|
||
| public function get_shimmer_markup() { | ||
| $spinner = ''; | ||
|
|
||
| if ( $this->args['show_spinner'] ) { | ||
| $spinner = sprintf( | ||
| '<svg class="shimmer-spinner" xmlns="http://www.w3.org/2000/svg" width="%s" height="%s" stroke="%s" viewBox="0 0 24 24"> | ||
| <g class="spinner-rotate"> | ||
| <circle cx="12" cy="12" r="9.5" fill="none" stroke-width="1.5"/> | ||
| </g> | ||
| </svg>', | ||
| esc_attr( $this->args['spinner_size'] ), | ||
| esc_attr( $this->args['spinner_size'] ), | ||
| esc_attr( $this->args['base_color'] ) | ||
| ); | ||
| } | ||
|
|
||
| $text_class = $this->args['show_shimmer'] ? 'shimmer' : 'shimmer-text'; | ||
|
|
||
| return sprintf( | ||
| '<span class="shimmer-wrapper">%s<span class="%s">%s</span></span>', | ||
| $spinner, | ||
| $text_class, | ||
| esc_html( $this->args['text'] ) | ||
| ); | ||
| } | ||
|
|
||
| public function filter_loading_text( $placeholder, $field, $form = null ) { | ||
| if ( ! class_exists( '\\GC_OpenAI\\Fields\\Stream' ) || ! $field instanceof \GC_OpenAI\Fields\Stream ) { | ||
| return $placeholder; | ||
| } | ||
|
|
||
| // If form_id is specified, only apply to those forms | ||
| if ( $this->args['form_id'] !== null ) { | ||
| $form_ids = is_array( $this->args['form_id'] ) ? $this->args['form_id'] : array( $this->args['form_id'] ); | ||
| if ( $form && ! in_array( rgar( $form, 'id' ), $form_ids ) ) { | ||
| return $placeholder; | ||
| } | ||
| } | ||
|
|
||
| // If field_id is specified, only apply to those fields | ||
| if ( $this->args['field_id'] !== null ) { | ||
| $field_ids = is_array( $this->args['field_id'] ) ? $this->args['field_id'] : array( $this->args['field_id'] ); | ||
| if ( ! in_array( rgar( $field, 'id' ), $field_ids ) ) { | ||
| return $placeholder; | ||
| } | ||
| } | ||
|
|
||
| return $this->get_shimmer_markup(); | ||
| } | ||
|
|
||
| private function get_styles_css() { | ||
| $base = esc_attr( $this->args['base_color'] ); | ||
| $shimmer = esc_attr( $this->args['shimmer_color'] ); | ||
| $dur = esc_attr( $this->args['shimmer_duration'] ); | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return | ||
| ".shimmer-wrapper { display: inline-flex; align-items: center; gap: 8px; } " . | ||
| ".shimmer-spinner { flex-shrink: 0; } " . | ||
| ".spinner-rotate { transform-origin: center; animation: spinner-rotation 2s linear infinite; } " . | ||
| ".spinner-rotate circle { stroke-linecap: round; animation: spinner-stroke 1.5s ease-in-out infinite; } " . | ||
| "@keyframes spinner-rotation { 100% { transform: rotate(360deg); } } " . | ||
| "@keyframes spinner-stroke { 0% { stroke-dasharray: 0 150; stroke-dashoffset: 0; } 47.5% { stroke-dasharray: 42 150; stroke-dashoffset: -16; } 95%, 100% { stroke-dasharray: 42 150; stroke-dashoffset: -59; } } " . | ||
| ".shimmer-text { display: inline-block; color: {$base}; line-height: 1.2; } " . | ||
| ".shimmer { display: inline-block; color: {$base}; line-height: 1.2; background: {$base} linear-gradient(to left, {$base}, {$shimmer} 50%, {$base}); background-position: -4rem top; background-repeat: no-repeat; background-size: 4rem 100%; -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; animation: shimmer {$dur} infinite; } " . | ||
| "@keyframes shimmer { 0% { background-position: -4rem top; } 70%, 100% { background-position: 12.5rem top; } }"; | ||
| } | ||
| } | ||
|
|
||
| # Configuration | ||
|
|
||
| new GCOAI_Loading_Animation( array( | ||
| 'text' => 'Thinking...', | ||
| 'base_color' => '#292929', | ||
| 'shimmer_color' => '#fff', | ||
| 'shimmer_duration' => '2.2s', | ||
| 'show_shimmer' => true, | ||
| 'show_spinner' => true, | ||
| 'spinner_size' => '16', | ||
| // 'form_id' => 123, // Uncomment and set to target specific form(s): 123 or array( 18, 22, 35 ) | ||
| // 'field_id' => 4, // Uncomment and set to target specific field(s): 5 or array( 5, 7, 12 ) | ||
| ) ); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
field_idtargeting is broken due to use ofrgaron a field objectIn
filter_loading_text(), the form scoping usesrgar( $form, 'id' ), which is fine because$formis an array. For fields, however, you already check$field instanceof \GC_OpenAI\Fields\Stream, so$fieldis an object. Passing it torgar( $field, 'id' )will always produce an empty string, meaning thefield_idfilter never matches and the shimmer is never applied whenfield_idis configured.You can fix this by reading
$field->iddirectly and normalizing types for comparison:Also, if there are code paths where this filter is called without a
$formvalue,form_idscoping will be skipped entirely; it’s worth confirming the filter’s usage in GC OpenAI to ensure that’s acceptable.I'll verify the review comment by examining the codebase to understand the issue with
rgar()function and the field object handling.I've confirmed from Gravity Forms documentation that the
rgar()function is used to retrieve a value from an array, whilergobj()is used to retrieve a property value from an object. This validates the core of the review comment. Now I need to verify the codebase specifics.Length of output: 279
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 49
Let me examine the file content and find the Stream class definition:
Length of output: 6227
🏁 Script executed:
Length of output: 560
🏁 Script executed:
Length of output: 603
🏁 Script executed:
Length of output: 0
Now I need to verify the behavior of
rgar()when passed an object, and confirm the Stream class structure. Let me search for more context:🏁 Script executed:
Length of output: 170
🏁 Script executed:
Length of output: 1226
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 0
🌐 Web query:
💡 Result:
Short answer: rgar() expects arrays — it checks array keys and will not read object properties. For Gravity Forms objects (fields, etc.) use rgobj() or object property access (->), or cast the object to an array first.
Why and examples:
Examples:
Array (use rgar):
$value = rgar($array, 'key'); // works. [1]
Object (use rgobj or ->):
$value = rgobj($field, 'inputs'); // recommended. [2]
// or
$value = isset($field->inputs) ? $field->inputs : null; // direct object access. [3]
Alternative: cast object to array if you must use rgar:
$value = rgar((array)$obj, 'property');
References:
[1] rgar() docs.
[2] rgobj() docs.
[3] Gravity Forms v1.9 developer notes (object notation / field objects).
Fix the
field_idtargeting by accessing the field object property directly, not viargar()The review comment is accurate. The
rgar()function is designed for arrays and will not properly read object properties. Since$fieldis confirmed to be an instance of\GC_OpenAI\Fields\Stream(an object), usingrgar($field, 'id')will fail to access the id property. This means field_id targeting never matches.The suggested fix is correct:
rgar($field, 'id')with$field->idApply the exact diff from the review comment to fix the bug at lines 131–140.
🧰 Tools
🪛 GitHub Check: PHPCS (Files Changed)
[failure] 139-139:
Whitespace found at end of line
[failure] 131-131:
Whitespace found at end of line
[failure] 123-123:
Whitespace found at end of line
🤖 Prompt for AI Agents