| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bricks SureForms service provider. |
| 4 |
* |
| 5 |
* @package sureforms. |
| 6 |
* @since 0.0.5 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SRFM\Inc\Page_Builders\Bricks; |
| 10 |
|
| 11 |
use SRFM\Inc\Payments\Payment_History_Shortcode; |
| 12 |
use SRFM\Inc\Traits\Get_Instance; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* SureForms Bricks service provider. |
| 20 |
*/ |
| 21 |
class Service_Provider { |
| 22 |
use Get_Instance; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor. |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
add_action( 'init', [ $this, 'widget' ], 11 ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Register SureForms widget. |
| 33 |
* |
| 34 |
* @since 0.0.5 |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function widget() { |
| 38 |
if ( ! class_exists( '\Bricks\Elements' ) ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
add_filter( |
| 42 |
'bricks/builder/i18n', |
| 43 |
[ $this, 'bricks_translatable_strings' ] |
| 44 |
); |
| 45 |
\Bricks\Elements::register_element( __DIR__ . '/elements/form-widget.php' ); |
| 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 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Filter to add translatable string to the builder. |
| 87 |
* |
| 88 |
* @param array<string> $i18n Array of translatable strings. |
| 89 |
* @since 0.0.5 |
| 90 |
* @return array<string> $i18n |
| 91 |
*/ |
| 92 |
public function bricks_translatable_strings( $i18n ) { |
| 93 |
$i18n['sureforms'] = __( 'SureForms', 'sureforms' ); |
| 94 |
return $i18n; |
| 95 |
} |
| 96 |
} |
| 97 |
|