PluginProbe
Embed Any Document – Embed PDF, Word, PowerPoint and Excel Files / trunk
Embed Any Document – Embed PDF, Word, PowerPoint and Excel Files vtrunk
2.7.13 2.7.6 trunk 1.0.1 1.1 1.1.1 2.1 2.2 2.2.1 2.2.2 2.2.4 2.2.5 2.3 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.3 2.7.4 2.7.5 2.7.7 2.7.8
embed-any-document / blocks / document.php

document.php in Embed Any Document – Embed PDF, Word, PowerPoint and Excel Files trunk, at blocks/document.php

163 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Enqueue JS and CSS files for the Guten Blocks
4 *
5 * @package embed-any-document
6 */
7
8 // Exit if accessed directly.
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * Used for blocks registration and rendering.
15 */
16 class Awsm_embed_Guten_blocks {
17 /**
18 * The instance of the class.
19 *
20 * @var Awsm_embed_Guten_blocks
21 */
22 private static $instance = null;
23
24 /**
25 * Constructor
26 */
27 public function __construct() {
28 add_action( 'init', array( $this, 'register_dynamic_block' ) );
29
30 // Hook: Assets for both editor and front-end.
31 add_action( 'enqueue_block_assets', array( $this, 'block_assets' ) );
32 // Hook: Editor assets.
33 add_action( 'enqueue_block_editor_assets', array( $this, 'block_editor_assets' ) );
34 }
35
36 /**
37 * Creates or returns an instance of this class.
38 */
39 public static function get_instance() {
40 // If an instance hasn't been created and set to $instance create an instance and set it to $instance.
41 if ( is_null( self::$instance ) ) {
42 self::$instance = new self();
43 }
44 return self::$instance;
45 }
46
47 /**
48 * Register dynamic block
49 */
50 public function register_dynamic_block() {
51 if ( ! function_exists( 'register_block_type' ) ) {
52 return;
53 }
54
55 // Register the editor stylesheet early so it can be referenced via editor_style_handles.
56 // This ensures the style is injected into the iframe editor used by API version 3.
57 wp_register_style(
58 'ead-block-editor-css',
59 plugins_url( 'blocks/document/editor.css', dirname( __FILE__ ) ),
60 array(),
61 AWSM_EMBED_VERSION,
62 'all'
63 );
64
65 register_block_type(
66 'embed-any-document/document',
67 array(
68 'attributes' => array(
69 'className' => array(
70 'type' => 'string',
71 ),
72 'shortcode' => array(
73 'type' => 'string',
74 ),
75 'url' => array(
76 'type' => 'string',
77 ),
78 'width' => array(
79 'type' => 'string',
80 ),
81 'height' => array(
82 'type' => 'string',
83 ),
84 'cache' => array(
85 'type' => 'boolean',
86 'default' => true,
87 ),
88 'download' => array(
89 'type' => 'string',
90 ),
91 'text' => array(
92 'type' => 'string',
93 ),
94 'viewer' => array(
95 'type' => 'string',
96 ),
97 ),
98 'editor_style_handles' => array( 'ead-block-editor-css' ),
99 'render_callback' => array( $this, 'block_render_callback' ),
100 )
101 );
102 }
103
104 /**
105 * Server side rendering
106 *
107 * @param array $atts Attributes array.
108 * @return string
109 */
110 public function block_render_callback( $atts ) {
111 $embed = '';
112 $class_name = isset( $atts['className'] ) ? $atts['className'] : '';
113 $shortcode = isset( $atts['shortcode'] ) ? $atts['shortcode'] : '';
114 $atts['cache'] = isset( $atts['cache'] ) && $atts['cache'] == false ? 'off' : 'on';
115
116 if ( isset( $atts['text'] ) ) {
117 $atts['text'] = sanitize_text_field( wp_unslash( $atts['text'] ) );
118 }
119
120 if ( ! empty( $shortcode ) ) {
121 $parsed_atts = Awsm_embed::get_shortcode_attrs( $shortcode );
122 $atts['url'] = isset( $parsed_atts['url'] ) ? $parsed_atts['url'] : ''; // url remains static.
123 $ead_atts = array_replace( $parsed_atts, $atts );
124 $ead = Awsm_embed::get_instance();
125 $embed = $ead->embed_shortcode( $ead_atts );
126 if ( ! empty( $embed ) && ! empty( $class_name ) ) {
127 $embed = sprintf( '<div class="%2$s">%1$s</div>', $embed, esc_attr( $class_name ) );
128 }
129 }
130 return $embed;
131 }
132
133 /**
134 * Enqueue Gutenberg block assets for both editor and front-end.
135 */
136 public function block_assets() {
137 wp_enqueue_style( 'awsm-ead-public' );
138 wp_enqueue_script( 'awsm-ead-public' );
139 }
140
141 /**
142 * Enqueue Gutenberg block assets for backend editor.
143 *
144 * `wp-blocks`: includes block type registration and related functions.
145 * `wp-element`: includes the WordPress Element abstraction for describing the structure of your blocks.
146 * `wp-i18n`: To internationalize the block's text.
147 */
148 public function block_editor_assets() {
149 // Styles. The handle is already registered on init; just enqueue it for the main editor page.
150 wp_enqueue_style( 'ead-block-editor-css' );
151 // Scripts.
152 wp_enqueue_script(
153 'ead-block-editor-js',
154 plugins_url( 'blocks/document/document-block.js', dirname( __FILE__ ) ),
155 array( 'wp-blocks', 'wp-components', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-url', 'wp-api-fetch', 'lodash', 'ead_media_button', 'awsm-ead-pdf-object' ),
156 AWSM_EMBED_VERSION,
157 true
158 );
159 }
160 }
161
162 Awsm_embed_Guten_blocks::get_instance();
163