| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\DonorDashboards; |
| 4 |
|
| 5 |
use Give\DonorDashboards\App as DonorDashboard; |
| 6 |
|
| 7 |
class Block |
| 8 |
{ |
| 9 |
|
| 10 |
protected $donorDashboard; |
| 11 |
|
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
$this->donorDashboard = give(DonorDashboard::class); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Registers Donor Dashboard block |
| 19 |
* |
| 20 |
* @since 2.10.0 |
| 21 |
**/ |
| 22 |
public function addBlock() |
| 23 |
{ |
| 24 |
register_block_type( |
| 25 |
'give/donor-dashboard', |
| 26 |
[ |
| 27 |
'render_callback' => [$this, 'renderCallback'], |
| 28 |
'attributes' => [ |
| 29 |
'align' => [ |
| 30 |
'type' => 'string', |
| 31 |
'default' => 'wide', |
| 32 |
], |
| 33 |
'accent_color' => [ |
| 34 |
'type' => 'string', |
| 35 |
'default' => '#68bb6c', |
| 36 |
], |
| 37 |
], |
| 38 |
] |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns Donor Profile block markup |
| 44 |
* |
| 45 |
* @since 2.22.1 Add script for iframe onload event to activate gutenberg edit mode. |
| 46 |
* Gutenberg block edit mode activates when focus set to block container. |
| 47 |
* @since 2.10.0 |
| 48 |
**/ |
| 49 |
public function renderCallback($attributes) |
| 50 |
{ |
| 51 |
$output = $this->donorDashboard->getOutput($attributes); |
| 52 |
|
| 53 |
if( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
| 54 |
$output = str_replace( |
| 55 |
'onload="', |
| 56 |
sprintf( |
| 57 |
'onload="%s;', |
| 58 |
'const iframe = this;this.contentWindow.document.addEventListener(\'click\', function(){iframe.closest(\'[data-block]\').focus({preventScroll: true});})' |
| 59 |
), |
| 60 |
$output |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
return $output; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Load Donor Profile frontend assets |
| 69 |
* |
| 70 |
* @since 2.10.0 |
| 71 |
**/ |
| 72 |
public function loadFrontendAssets() |
| 73 |
{ |
| 74 |
if (has_block('give/donor-dashboard')) { |
| 75 |
return $this->donorDashboard->loadAssets(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Load Donor Profile block editor assets |
| 81 |
* |
| 82 |
* @since 2.10.0 |
| 83 |
**/ |
| 84 |
public function loadEditorAssets() |
| 85 |
{ |
| 86 |
wp_enqueue_script( |
| 87 |
'give-donor-dashboards-block', |
| 88 |
GIVE_PLUGIN_URL . 'build/assets/dist/js/donor-dashboards-block.js', |
| 89 |
[], |
| 90 |
GIVE_VERSION, |
| 91 |
true |
| 92 |
); |
| 93 |
wp_set_script_translations( 'give-donor-dashboards-block', 'give' ); |
| 94 |
} |
| 95 |
} |
| 96 |
|