| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentSupport\App\App; |
| 6 |
use FluentSupport\Framework\Support\Arr; |
| 7 |
use FluentSupport\App\Services\Blocks\BlockAttributes; |
| 8 |
|
| 9 |
class BlockEditorHandler |
| 10 |
{ |
| 11 |
public function init() |
| 12 |
{ |
| 13 |
$app = App::getInstance(); |
| 14 |
|
| 15 |
$assets = $app['url.assets']; |
| 16 |
|
| 17 |
wp_register_script( |
| 18 |
'fluent-support/customer-portal', |
| 19 |
$assets . 'block-editor/js/fs_block.js', |
| 20 |
array('wp-blocks', 'wp-components', 'wp-block-editor', 'wp-element'), |
| 21 |
FLUENT_SUPPORT_VERSION, |
| 22 |
true |
| 23 |
); |
| 24 |
|
| 25 |
wp_localize_script('fluent-support/customer-portal', 'fluent_support_vars', [ |
| 26 |
'rest' => $this->getRestInfo(), |
| 27 |
]); |
| 28 |
|
| 29 |
register_block_type('fluent-support/customer-portal', array( |
| 30 |
'editor_script' => 'fluent-support/customer-portal', |
| 31 |
'render_callback' => array($this, 'renderBlock'), |
| 32 |
'attributes' => BlockAttributes::CustomerPortalAttributes(), |
| 33 |
)); |
| 34 |
} |
| 35 |
|
| 36 |
protected function getRestInfo() |
| 37 |
{ |
| 38 |
$app = App::getInstance(); |
| 39 |
|
| 40 |
$ns = $app->config->get('app.rest_namespace'); |
| 41 |
$v = $app->config->get('app.rest_version'); |
| 42 |
|
| 43 |
return [ |
| 44 |
'base_url' => esc_url_raw(rest_url()), |
| 45 |
'url' => rest_url($ns . '/' . $v), |
| 46 |
'nonce' => wp_create_nonce('wp_rest'), |
| 47 |
'namespace' => $ns, |
| 48 |
'version' => $v |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
public function renderBlock($attributes) |
| 53 |
{ |
| 54 |
$param = ''; |
| 55 |
|
| 56 |
if (Arr::get($attributes, 'showLogoutButton')) { |
| 57 |
$param = "show_logout=yes"; |
| 58 |
} |
| 59 |
|
| 60 |
if ($selectedMailbox = Arr::get($attributes, 'selectedMailbox')) { |
| 61 |
$param .= " business_box_id='" . esc_attr($selectedMailbox) . "'"; |
| 62 |
} |
| 63 |
|
| 64 |
$param .= " attributes='" . esc_attr(json_encode($attributes)) . "'"; |
| 65 |
|
| 66 |
return do_shortcode("[fluent_support_portal $param]"); |
| 67 |
} |
| 68 |
} |
| 69 |
|