| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Modules\Gutenberg; |
| 4 |
|
| 5 |
class Module |
| 6 |
{ |
| 7 |
public function __construct() { |
| 8 |
$this->initGutenbergBlock(); |
| 9 |
add_action( 'admin_enqueue_scripts', [ $this, 'loadGutenbergAdminWidgetScripts'] ); |
| 10 |
add_action( 'wp_enqueue_scripts', [$this, 'loadGutenbergWidgetScripts']); |
| 11 |
} |
| 12 |
|
| 13 |
public function loadGutenbergWidgetScripts() { |
| 14 |
if ( $this->hasDepicter() ) { |
| 15 |
wp_enqueue_style( |
| 16 |
'depicter-gutenberg', |
| 17 |
\Depicter::core()->assets()->getUrl() . '/app/src/Modules/Gutenberg/build/index.css', |
| 18 |
[], |
| 19 |
'1.0.0' |
| 20 |
); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
public function loadGutenbergAdminWidgetScripts() { |
| 25 |
|
| 26 |
$current_screen = get_current_screen(); |
| 27 |
if ( !$current_screen->is_block_editor() ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
$list = [ |
| 32 |
[ |
| 33 |
'id' => "0", |
| 34 |
'name' => __( 'Select Slider', 'depicter' ) |
| 35 |
] |
| 36 |
]; |
| 37 |
$documents = \Depicter::documentRepository()->select( ['id', 'name'] )->where('type', 'not in', ['popup', 'banner-bar'])->orderBy('modified_at', 'DESC')->findAll()->get(); |
| 38 |
$list = $documents ? array_merge( $list, $documents->toArray() ) : $list; |
| 39 |
if ( !empty( $list ) ) { |
| 40 |
foreach ( $list as $key => $item ) { |
| 41 |
$list[ $key ]['label'] = $item['name']; |
| 42 |
unset( $list[ $key ]['name'] ); |
| 43 |
|
| 44 |
$list[ $key ]['value'] = $item['id']; |
| 45 |
unset( $list[ $key ]['id'] ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// load common assets |
| 50 |
\Depicter::front()->assets()->enqueueStyles(); |
| 51 |
\Depicter::front()->assets()->enqueueScripts(['player', 'iframe-resizer']); |
| 52 |
|
| 53 |
wp_localize_script( 'wp-block-editor', 'depicterSliders',[ |
| 54 |
'list' => $list, |
| 55 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 56 |
'editor_url' => \Depicter::editor()->getEditUrl('1'), |
| 57 |
'token' => \Depicter::csrf()->getToken( \Depicter\Security\CSRF::EDITOR_ACTION ), |
| 58 |
'publish_text' => esc_html__( 'Publish Slider', 'depicter' ), |
| 59 |
'edit_text' => esc_html__( 'Edit Slider', 'depicter' ) |
| 60 |
]); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
public function initGutenbergBlock() { |
| 65 |
register_block_type( __DIR__ . '/build', [ |
| 66 |
'render_callback' => [ $this, 'renderGutenbergBlock' ] |
| 67 |
] ); |
| 68 |
} |
| 69 |
|
| 70 |
public function renderGutenbergBlock( $blockAttributes ) { |
| 71 |
|
| 72 |
if ( !empty( $blockAttributes['id'] ) ) { |
| 73 |
$id = (int) $blockAttributes['id']; |
| 74 |
return depicter( $id, ['echo' => false ] ); |
| 75 |
} else { |
| 76 |
echo esc_html__( 'Slider ID required', 'depicter' ); |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
public function hasDepicter($post_id = null) { |
| 82 |
if (!$post_id) { |
| 83 |
global $post; |
| 84 |
$post_id = $post->ID ?? null; |
| 85 |
} |
| 86 |
|
| 87 |
if (!$post_id) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
$post_content = get_post_field('post_content', $post_id); |
| 92 |
|
| 93 |
// Check for Gutenberg block comments |
| 94 |
return strpos($post_content, '<!-- wp:depicter/slider') !== false; |
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
new Module(); |