| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Pdf Embed |
| 4 |
* Plugin URI: https://formello.net/ |
| 5 |
* Description: PDF embedded with official Adobe API. |
| 6 |
* Version: 0.1.1 |
| 7 |
* Author: Formello |
| 8 |
* Author URI: https://formello.net |
| 9 |
* License: GPL2 |
| 10 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 11 |
* Text Domain: formello |
| 12 |
* @package Formello/PdfEmbed |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
/** |
| 17 |
* Registers the block using the metadata loaded from the `block.json` file. |
| 18 |
* Behind the scenes, it registers also all assets so they can be enqueued |
| 19 |
* through the block editor in the corresponding context. |
| 20 |
* |
| 21 |
* @see https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/writing-your-first-block-type/ |
| 22 |
*/ |
| 23 |
function pdf_embed_block_init() { |
| 24 |
|
| 25 |
$script_asset_path = __DIR__ . '/build/frontend.asset.php'; |
| 26 |
if ( ! file_exists( $script_asset_path ) ) { |
| 27 |
throw new \Error( |
| 28 |
'You need to run `npm start` or `npm run build` for the "tropicalista/pdfembed" block first.' |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
$script_asset = require $script_asset_path; |
| 33 |
|
| 34 |
wp_register_script( |
| 35 |
'adobe-pdf-block-frontend', |
| 36 |
plugins_url( 'build/frontend.js', __FILE__ ), |
| 37 |
array(), |
| 38 |
$script_asset['version'], |
| 39 |
true |
| 40 |
); |
| 41 |
|
| 42 |
register_block_type( |
| 43 |
__DIR__, |
| 44 |
array( |
| 45 |
'render_callback' => function ( $attributes, $content ) { |
| 46 |
|
| 47 |
if ( ! is_admin() ) { |
| 48 |
wp_enqueue_script( 'adobe-pdf-block-frontend' ); |
| 49 |
|
| 50 |
wp_add_inline_script( 'adobe-pdf-block-frontend', 'var embedConfig =' . wp_json_encode( $attributes ) ); |
| 51 |
} |
| 52 |
|
| 53 |
return $content; |
| 54 |
}, |
| 55 |
) |
| 56 |
); |
| 57 |
} |
| 58 |
add_action( 'init', 'pdf_embed_block_init' ); |
| 59 |
|