| 1 |
<?php |
| 2 |
|
| 3 |
class WooMailerLiteBlocksIntegration implements Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface |
| 4 |
{ |
| 5 |
public function get_name() |
| 6 |
{ |
| 7 |
return 'woo-mailerlite'; |
| 8 |
} |
| 9 |
|
| 10 |
public function initialize() |
| 11 |
{ |
| 12 |
$this->register_mailerlite_block_frontend_scripts(); |
| 13 |
$this->register_mailerlite_block_editor_scripts(); |
| 14 |
} |
| 15 |
|
| 16 |
public function get_script_handles() |
| 17 |
{ |
| 18 |
return ['mailerlite-block-woo-mailerlite-block-frontend']; |
| 19 |
} |
| 20 |
|
| 21 |
public function get_editor_script_handles() |
| 22 |
{ |
| 23 |
return ['mailerlite-block-woo-mailerlite-block-editor']; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_script_data() |
| 27 |
{ |
| 28 |
$mailerLiteSettings = WooMailerLiteOptions::get('settings'); |
| 29 |
if (!isset($mailerLiteSettings['subscribeOnCheckout'])) { |
| 30 |
WooMailerLiteOptions::update('settings', $mailerLiteSettings); |
| 31 |
return true; |
| 32 |
} elseif (!$mailerLiteSettings['subscribeOnCheckout']) { |
| 33 |
return true; |
| 34 |
} |
| 35 |
return [ |
| 36 |
'MailerLiteWooActive' => true, |
| 37 |
'MailerLiteWooLabel' => $mailerLiteSettings['checkoutLabel'], |
| 38 |
'MailerLiteWooPreselect' => $mailerLiteSettings['checkoutPreselect'], |
| 39 |
'MailerLiteWooHidden' => $mailerLiteSettings['checkoutHidden'], |
| 40 |
'MailerLiteNonce' => wp_create_nonce('ml-block-settings-update'), |
| 41 |
'MailerLiteAdminURL' => admin_url('admin.php?page=wc-settings&tab=integration§ion=mailerlite'), |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
public function register_mailerlite_block_editor_scripts() |
| 46 |
{ |
| 47 |
|
| 48 |
$script_path = '../assets/js/blocks-integration/woo-mailerlite-admin-block.js'; |
| 49 |
$script_asset_path = dirname(__FILE__) . $script_path; |
| 50 |
$script_url = plugins_url($script_path, __FILE__); |
| 51 |
|
| 52 |
$script_asset = [ |
| 53 |
'dependencies' => [ |
| 54 |
'wc-blocks-checkout', |
| 55 |
'wc-settings', |
| 56 |
'wp-block-editor', |
| 57 |
'wp-blocks', |
| 58 |
'wp-components', |
| 59 |
'wp-element', |
| 60 |
'wp-i18n', |
| 61 |
], |
| 62 |
'version' => $this->get_file_version($script_asset_path), |
| 63 |
]; |
| 64 |
|
| 65 |
wp_register_script( |
| 66 |
'mailerlite-block-woo-mailerlite-block-editor', |
| 67 |
$script_url, |
| 68 |
$script_asset['dependencies'], |
| 69 |
$script_asset['version'], |
| 70 |
true |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
public function register_mailerlite_block_frontend_scripts() |
| 75 |
{ |
| 76 |
$script_path = '../../public/js/blocks-integration/woo-mailerlite-blocks.js'; |
| 77 |
$script_url = plugins_url($script_path, __FILE__); |
| 78 |
$script_asset_path = dirname(__FILE__) . $script_path; |
| 79 |
|
| 80 |
$script_asset = [ |
| 81 |
'dependencies' => [ |
| 82 |
'wc-blocks-checkout', |
| 83 |
'wc-settings', |
| 84 |
], |
| 85 |
'version' => $this->get_file_version($script_asset_path), |
| 86 |
]; |
| 87 |
|
| 88 |
$result = wp_register_script( |
| 89 |
'mailerlite-block-woo-mailerlite-block-frontend', |
| 90 |
$script_url, |
| 91 |
$script_asset['dependencies'], |
| 92 |
$script_asset['version'], |
| 93 |
true |
| 94 |
); |
| 95 |
|
| 96 |
if ( ! $result) { |
| 97 |
|
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
wp_localize_script( |
| 102 |
'mailerlite-block-woo-mailerlite-block-frontend', |
| 103 |
'wooMailerLiteBlockData', |
| 104 |
[ |
| 105 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 106 |
'nonce' => wp_create_nonce('woo_mailerlite_cart_nonce'), |
| 107 |
] |
| 108 |
); |
| 109 |
|
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
protected function get_file_version($file) |
| 114 |
{ |
| 115 |
if (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG && file_exists($file)) { |
| 116 |
return filemtime($file); |
| 117 |
} |
| 118 |
|
| 119 |
return WOO_MAILERLITE_VERSION; |
| 120 |
} |
| 121 |
} |
| 122 |
|