| 1 |
<?php |
| 2 |
|
| 3 |
require_once __DIR__ . '/includes/class-obTargeting.php'; |
| 4 |
require_once __DIR__ . '/includes/class-obAnalytics.php'; |
| 5 |
require_once __DIR__ . '/includes/Templates/Templates.php'; |
| 6 |
|
| 7 |
if ( !class_exists( 'OCBOffCanvas' ) ) { |
| 8 |
class OCBOffCanvas |
| 9 |
{ |
| 10 |
public function __construct() { |
| 11 |
add_action( 'init', [ $this, 'onInit' ] ); |
| 12 |
add_action( 'enqueue_block_editor_assets', [ $this, 'obEnqueueEditorAssets' ] ); |
| 13 |
add_action( 'wp_enqueue_scripts', [ $this, 'obEnqueueViewAssets' ] ); |
| 14 |
} |
| 15 |
|
| 16 |
public function onInit() |
| 17 |
{ |
| 18 |
register_block_type( __DIR__ . '/build' ); |
| 19 |
|
| 20 |
} |
| 21 |
|
| 22 |
public function obEnqueueEditorAssets() { |
| 23 |
// The template library talks to admin-ajax through wp.ajax, which |
| 24 |
// ships with wp-util. That handle is not part of the block's |
| 25 |
// generated dependency list, so request it explicitly. |
| 26 |
wp_enqueue_script( 'wp-util' ); |
| 27 |
|
| 28 |
wp_add_inline_script( |
| 29 |
'ocb-offcanvas-editor-script', |
| 30 |
'const obIsPipeChecker = ' . wp_json_encode( obIsPremium() ) . ';', |
| 31 |
'before' |
| 32 |
); |
| 33 |
|
| 34 |
// Template Library configuration — printed before the editor script |
| 35 |
// so TemplateLibrary.js can read it synchronously on mount. |
| 36 |
// Assigned onto `window` on purpose: a top-level `const` in a |
| 37 |
// classic script lands in the global lexical scope, which a bundled |
| 38 |
// module cannot reach as `window.<name>`. |
| 39 |
wp_add_inline_script( |
| 40 |
'ocb-offcanvas-editor-script', |
| 41 |
'window.ocbTemplateConfig = ' . wp_json_encode( [ |
| 42 |
'nonce' => wp_create_nonce( OCBTemplates::NONCE_ACTION ), |
| 43 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 44 |
'adminUrl' => admin_url(), |
| 45 |
'pricingUrl' => 'https://bplugins.com/products/offcanvas-block/pricing', |
| 46 |
'isPremium' => obIsPremium(), |
| 47 |
] ) . ';', |
| 48 |
'before' |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
public function obEnqueueViewAssets() { |
| 53 |
// Current page/post ID, used only to label analytics rows in the admin. |
| 54 |
$current_page_id = is_singular() ? get_queried_object_id() : 0; |
| 55 |
|
| 56 |
wp_add_inline_script( |
| 57 |
'ocb-offcanvas-view-script', |
| 58 |
'const obIsPipeChecker = ' . wp_json_encode( obIsPremium() ) . ';' . |
| 59 |
'var obRestUrl = ' . wp_json_encode( esc_url_raw( rest_url() ) ) . ';' . |
| 60 |
'var obCurrentPageId = ' . wp_json_encode( $current_page_id ) . ';', |
| 61 |
'before' |
| 62 |
); |
| 63 |
|
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
new OCBOffCanvas(); |
| 68 |
} |