Skip to content

Instantly share code, notes, and snippets.

@danieliser
Created October 25, 2024 07:19
Show Gist options
  • Save danieliser/95edc48ac6febdfa7ecddbf7e2088c14 to your computer and use it in GitHub Desktop.
Save danieliser/95edc48ac6febdfa7ecddbf7e2088c14 to your computer and use it in GitHub Desktop.
Attempts at enabling Bricks Builder for Popup Maker, needed safe place to store the code outside Popup Maker core files for now.
<?php
/**
* Check if the current page is the Bricks editor preview.
*
* @return bool
*/
function pm_is_bricks_editor_preview() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
return isset( $_GET['bricks_preview'] );
}
/**
* Get the current post ID.
*
* @return int|null
*/
function pm_bricks_get_current_post_id() {
global $post;
$post_id = isset( $post ) ? $post->ID : null;
if ( ( bricks_is_builder_iframe() || pm_is_bricks_editor_preview() ) && class_exists( '\Bricks\Helpers' ) ) {
$preview_post_id = \Bricks\Helpers::get_template_setting( 'templatePreviewPostId', $post_id );
if ( $preview_post_id ) {
$post_id = $preview_post_id;
}
}
return $post_id;
}
function pm_bricks_should_output_custom_vars() {
if ( ! function_exists( 'get_field' ) ) {
return false;
}
$checks = [
'is_singular' => is_singular(),
'bricks_is_builder' => bricks_is_builder(),
'bricks_previews' => bricks_is_builder_iframe() || pm_is_bricks_editor_preview(),
];
return in_array( true, $checks, true );
}
/**
* Retrieve custom header variables based on ACF fields.
*
* @return array<string,string>
*/
function pm_get_template_header_variables() {
if ( ! function_exists( 'get_field' ) ) {
return [];
}
// Ensure we're on a single post or page
$post_id = pm_bricks_get_current_post_id();
if ( ! $post_id ) {
return [];
}
// Retrieve the color scheme field
$color_scheme = get_field( 'header_color_scheme', $post_id );
$variables = [];
// Proceed only if the color scheme is set to 'custom'
// Adjust the condition if your 'custom' value differs
if ( '' === $color_scheme ) {
// Retrieve individual color fields
$header_background_color = get_field( 'header_background_color', $post_id );
$header_text_color = get_field( 'header_text_color', $post_id );
$header_accent_color = get_field( 'header_accent_color', $post_id );
$header_accent_text_color = get_field( 'header_accent_text_color', $post_id );
$variables = [
// Foreach if its falsy, set to null.
'header-background-color' => $header_background_color ? $header_background_color : null,
'header-text-color' => $header_text_color ? $header_text_color : null,
'header-accent-color' => $header_accent_color ? $header_accent_color : null,
'header-accent-text-color' => $header_accent_text_color ? $header_accent_text_color : null,
];
}
return array_filter( $variables );
}
/**
* Outputs custom header styles based on ACF fields.
*
* @return void
*/
function pm_render_custom_css_variables() {
if ( is_admin() ) {
return;
}
$overrides = pm_get_template_header_variables();
// Check if at least one custom color field is set
if ( ! empty( $overrides ) ) {
// Start building the CSS output
$custom_css = "<style id=\"custom-header-stylings\" type=\"text/css\">\n:root {\n";
$custom_css .= "/* Custom Stylings Based on ACF Template Fields */ \n";
// Loop through each variable and append it
foreach ( $overrides as $name => $value ) {
// Set the override variable
$safe_value = esc_attr( $value );
$custom_css .= " --$name-override: {$safe_value};\n";
}
$custom_css .= "}\n</style>\n";
// Output the custom CSS
echo $custom_css;
}
}
/**
* Add custom global variables to the Bricks global variables option.
*
* @param array{
* id: string,
* name: string,
* value: string,
* }[] $value The global variables.
*
* @return array{
* id: string,
* name: string,
* value: string,
* }[]
*/
function pm_bricks_filter_global_variables_option( $value ) {
if ( ! $value ) {
return $value;
}
if ( ! is_array( $value ) ) {
$value = [];
}
if ( ! did_action( 'wp' ) ) {
return $value;
}
$overrides = pm_get_template_header_variables();
if ( ! empty( $overrides ) ) {
foreach ( $value as $key => $val ) {
$name = $val['name'];
if ( array_key_exists( $name, $overrides ) ) {
$value[ $key ]['value'] = $overrides[ $name ];
}
}
}
return $value;
}
/**
* Modify the global variables used in the Bricks CSS.
*
* @return void
*/
function pm_modify_bricks_css_global_variables() {
if ( class_exists( '\Bricks\Database' ) ) {
$overrides = pm_get_template_header_variables();
if ( ! empty( $overrides ) ) {
/**
* Global variables.
*
* @var array{
* id: string,
* name: string,
* value: string,
* }[] $globalVariables
*/
$global_variables = \Bricks\Database::$global_data['globalVariables'];
foreach ( $global_variables as $key => $val ) {
$name = $val['name'];
if ( array_key_exists( $name, $overrides ) ) {
$global_variables[ $key ]['value'] = $overrides[ $name ];
}
}
\Bricks\Database::$global_data['globalVariables'] = $global_variables;
}
}
}
function pm_merge_global_variables( $global_variables ) {
$overrides = pm_get_template_header_variables();
do_action( 'qm/debug', 'GlobalVariables' );
do_action( 'qm/debug', $overrides );
do_action( 'qm/debug', $global_variables );
if ( ! empty( $overrides ) ) {
foreach ( $global_variables as $key => $val ) {
$name = $val['name'];
if ( array_key_exists( $name, $overrides ) ) {
$global_variables[ $key ]['value'] = $overrides[ $name ];
}
}
}
return $global_variables;
}
// add_filter('bricks/load_global_variables', 'pm_merge_global_variables');
// add_filter('bricks/get_global_variables', 'pm_merge_global_variables');
// add_filter(
// 'bricks/api/get_templates_data', function ( $templates_data ) {
// $overrides = pm_get_template_header_variables();
// if (! empty($overrides) && is_array($templates_data['globalVariables']) ) {
// foreach ( $templates_data['globalVariables'] as $key => $val ) {
// $name = $val['name'];
// if (array_key_exists($name, $overrides) ) {
// $templates_data['globalVariables'][ $key ]['value'] = $overrides[ $name ];
// }
// }
// }
// return $templates_data;
// }
// );
add_action( 'wp', 'pm_modify_bricks_css_global_variables', 0 );
add_action( 'wp_head', 'pm_render_custom_css_variables', 2 );
add_filter( 'option_bricks_global_variables', 'pm_bricks_filter_global_variables_option' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment