Skip to content

Conversation

@GaryJones
Copy link
Collaborator

Problem

When the ACM Ad Zones widget was configured for an ad zone without any matching ad codes, the widget would still render its wrapper HTML elements. This resulted in broken output containing unreplaced tokens such as %width%, %height%, and %tag_id% in the page source. Beyond being visually problematic, this cluttered the DOM with meaningless elements and exposed implementation details that should remain hidden when no ad content exists.

The root cause was that ACM_Ad_Zones::widget() called do_action('acm_tag', ...) directly within the widget wrapper output, without first checking whether any ad content would actually be rendered. The action hook would fire, find no matching ad code, and return nothing—but the surrounding wrapper HTML had already been echoed.

Solution

This change modifies ACM_Ad_Zones::widget() to use output buffering to capture the ad content before deciding whether to output the widget wrapper. The method now:

  1. Captures output from the acm_tag action using ob_start() and ob_get_clean()
  2. Checks whether the captured content is empty
  3. Returns early (outputting nothing) if no content exists and the new acm_display_empty_widget filter doesn't override this behaviour
  4. Only outputs the complete widget wrapper (including title) when actual ad content is present

A new filter, acm_display_empty_widget, has been introduced to allow themes or plugins to override this behaviour and force display of empty widget wrappers if desired. The filter receives the ad zone identifier, widget arguments, and widget instance for contextual decision-making.

Additional changes include excluding the short prefix warning from PHPCS for the established "acm" prefix, which has been consistently used throughout this plugin's history.

The fix includes comprehensive test coverage: seven integration tests verify widget empty output behaviour across various scenarios (including filter interaction and backwards compatibility), whilst three unit tests confirm that get_acm_tag() correctly returns empty strings in appropriate circumstances.

Fixes #72

Resolves issue #72 where the ACM_Ad_Zones widget would output empty wrapper HTML (before_widget/after_widget) even when no valid ad codes were found for the specified tag. This created unnecessary empty markup in the page output.

The widget now uses output buffering to capture the ad content first, checks if any content was generated, and only outputs the wrapper HTML if there's actual ad content to display.

A new filter 'acm_display_empty_widget' has been added to allow themes to override this behaviour if they need the wrapper HTML to display even when empty (defaults to false).

Changes:
- Modified ACM_Ad_Zones::widget() to buffer ad output and conditionally render wrapper
- Added 'acm_display_empty_widget' filter with ad zone, args, and instance parameters
- Excluded short prefix warning from PHPCS for established 'acm' prefix
- Added 7 integration tests covering empty output behaviour and filter functionality
- Added unit tests verifying get_acm_tag() returns empty string when no ad codes found

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@GaryJones GaryJones requested a review from a team as a code owner December 14, 2025 17:02
@GaryJones GaryJones added this to the 0.8.0 milestone Dec 14, 2025
@GaryJones GaryJones merged commit 8cefa2e into develop Dec 14, 2025
25 checks passed
@GaryJones GaryJones deleted the fix/72-no-ad-code-empty-output branch December 14, 2025 17:05
@GaryJones GaryJones added the type: bug Something isn't working label Dec 14, 2025
@GaryJones GaryJones self-assigned this Dec 14, 2025
@GaryJones GaryJones mentioned this pull request Jan 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ad code tag still rendered if no valid ad codes found

2 participants