Skip to content

Commit a15bf2d

Browse files
ilicfilipclaude
andauthored
Add Branding::enqueue_inline_css() so partner CSS can attach to any handle (#762)
Allows external integrations (e.g. pp-hosts guided tour, which runs on the block editor, site editor, frontend, and non-PP admin screens) to apply the partner branding custom CSS to their own stylesheets, instead of being limited to the PP admin pages where Page::enqueue_styles() runs. The new method is idempotent per style handle, so the per-request dedupe that previously lived on Page (via the $branding_inline_styles_added static — needed because dashboard widgets call enqueue_styles() multiple times per request) now lives with the branding class itself and works for any handle. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent ded2039 commit a15bf2d

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

classes/admin/class-page.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
*/
1313
class Page {
1414

15-
/**
16-
* Whether the branding inline styles have been added.
17-
*
18-
* @var boolean
19-
*/
20-
protected static $branding_inline_styles_added = false;
21-
2215
/**
2316
* Constructor.
2417
*/
@@ -285,10 +278,7 @@ public function enqueue_styles() {
285278

286279
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/variables-color' );
287280
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/admin' );
288-
if ( ! static::$branding_inline_styles_added ) {
289-
\wp_add_inline_style( 'progress-planner/admin', \progress_planner()->get_ui__branding()->get_custom_css() );
290-
static::$branding_inline_styles_added = true;
291-
}
281+
\progress_planner()->get_ui__branding()->enqueue_inline_css( 'progress-planner/admin' );
292282
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/web-components/prpl-tooltip' );
293283
\progress_planner()->get_admin__enqueue()->enqueue_style( 'progress-planner/web-components/prpl-install-plugin' );
294284

classes/ui/class-branding.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ final class Branding {
2121
'default' => 0,
2222
];
2323

24+
/**
25+
* Style handles that have already received the branding inline CSS this request.
26+
*
27+
* @var array<string, bool>
28+
*/
29+
private static $inline_css_attached = [];
30+
2431
/**
2532
* Constructor.
2633
*/
@@ -124,6 +131,30 @@ public function get_custom_css(): string {
124131
: $this->get_api_data()['custom_css'];
125132
}
126133

134+
/**
135+
* Attach the branding custom CSS to a registered/enqueued stylesheet.
136+
*
137+
* Idempotent per handle within a single request, so callers do not need to
138+
* track whether the CSS has already been added.
139+
*
140+
* @param string $handle The style handle to attach the inline CSS to.
141+
*
142+
* @return void
143+
*/
144+
public function enqueue_inline_css( string $handle ): void {
145+
if ( isset( self::$inline_css_attached[ $handle ] ) ) {
146+
return;
147+
}
148+
self::$inline_css_attached[ $handle ] = true;
149+
150+
$css = $this->get_custom_css();
151+
if ( '' === $css ) {
152+
return;
153+
}
154+
155+
\wp_add_inline_style( $handle, $css );
156+
}
157+
127158
/**
128159
* Get the admin-menu icon.
129160
*

0 commit comments

Comments
 (0)