| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace SRFM\Inc\Page_Builders\Bricks; |
| 10 | 10 | |
| 11 | +use SRFM\Inc\Payments\Payment_History_Shortcode; | |
| 11 | 12 | use SRFM\Inc\Traits\Get_Instance; |
| 12 | 13 | |
| 13 | 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | 15 | exit; // Exit if accessed directly. |
| @@ -42,8 +43,44 @@ | ||
| 42 | 43 | [ $this, 'bricks_translatable_strings' ] |
| 43 | 44 | ); |
| 44 | 45 | \Bricks\Elements::register_element( __DIR__ . '/elements/form-widget.php' ); |
| 45 | 46 | \Bricks\Elements::register_element( __DIR__ . '/elements/payment-history-widget.php' ); |
| 47 | + | |
| 48 | + // Head-load the Payment History stylesheet when the element is on the page. | |
| 49 | + // Priority 5 runs after Payment_History_Shortcode::register_assets() (priority 1) | |
| 50 | + // so the handle exists, and before wp_head prints styles. | |
| 51 | + add_action( 'wp_enqueue_scripts', [ $this, 'maybe_enqueue_payment_history_assets' ], 5 ); | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Enqueue the Payment History assets in the <head> when a Bricks Payment History | |
| 56 | + * element is present on the current page. | |
| 57 | + * | |
| 58 | + * Bricks instantiates elements — and so fires their `enqueue_scripts()` — only | |
| 59 | + * during render (`Frontend::render_element()` → `Element::init()`), which runs | |
| 60 | + * after `wp_head`. An element-side enqueue therefore lands in the footer and the | |
| 61 | + * dashboard flashes unstyled. Bricks stores element data in postmeta, so detect our | |
| 62 | + * element in the head the same way Bricks detects its own setting-specific assets: | |
| 63 | + * sniff the serialised template data of each area for the element name. | |
| 64 | + * | |
| 65 | + * @since 2.12.3 | |
| 66 | + * @return void | |
| 67 | + */ | |
| 68 | + public function maybe_enqueue_payment_history_assets() { | |
| 69 | + if ( ! class_exists( '\Bricks\Database' ) ) { | |
| 70 | + return; | |
| 71 | + } | |
| 72 | + | |
| 73 | + foreach ( [ 'header', 'content', 'footer' ] as $area ) { | |
| 74 | + $data = \Bricks\Database::get_template_data( $area ); | |
| 75 | + if ( ! is_array( $data ) ) { | |
| 76 | + continue; | |
| 77 | + } | |
| 78 | + if ( false !== strpos( (string) wp_json_encode( $data ), '"sureforms-payment-history"' ) ) { | |
| 79 | + Payment_History_Shortcode::get_instance()->enqueue_assets( true ); | |
| 80 | + return; | |
| 81 | + } | |
| 82 | + } | |
| 46 | 83 | } |
| 47 | 84 | |
| 48 | 85 | /** |
| 49 | 86 | * Filter to add translatable string to the builder. |