PluginProbe
Embed Any Document – Embed PDF, Word, PowerPoint and Excel Files / 2.7.3
Embed Any Document – Embed PDF, Word, PowerPoint and Excel Files v2.7.3
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 2.7.3, at blocks/document.php

153 lines 3.9 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_block_type(
56 'embed-any-document/document',
57 array(
58 'attributes' => array(
59 'className' => array(
60 'type' => 'string',
61 ),
62 'shortcode' => array(
63 'type' => 'string',
64 ),
65 'url' => array(
66 'type' => 'string',
67 ),
68 'width' => array(
69 'type' => 'string',
70 ),
71 'height' => array(
72 'type' => 'string',
73 ),
74 'cache' => array(
75 'type' => 'boolean',
76 'default' => true,
77 ),
78 'download' => array(
79 'type' => 'string',
80 ),
81 'text' => array(
82 'type' => 'string',
83 ),
84 'viewer' => array(
85 'type' => 'string',
86 ),
87 ),
88 'render_callback' => array( $this, 'block_render_callback' ),
89 )
90 );
91 }
92
93 /**
94 * Server side rendering
95 *
96 * @param array $atts Attributes array.
97 * @return string
98 */
99 public function block_render_callback( $atts ) {
100 $embed = '';
101 $class_name = isset( $atts['className'] ) ? $atts['className'] : '';
102 $shortcode = isset( $atts['shortcode'] ) ? $atts['shortcode'] : '';
103 $atts['cache'] = isset( $atts['cache'] ) && $atts['cache'] == false ? 'off' : 'on';
104 if ( ! empty( $shortcode ) ) {
105 $parsed_atts = Awsm_embed::get_shortcode_attrs( $shortcode );
106 $atts['url'] = isset( $parsed_atts['url'] ) ? $parsed_atts['url'] : ''; // url remains static.
107 $ead_atts = array_replace( $parsed_atts, $atts );
108 $ead = Awsm_embed::get_instance();
109 $embed = $ead->embed_shortcode( $ead_atts );
110 if ( ! empty( $embed ) && ! empty( $class_name ) ) {
111 $embed = sprintf( '<div class="%2$s">%1$s</div>', $embed, esc_attr( $class_name ) );
112 }
113 }
114 return $embed;
115 }
116
117 /**
118 * Enqueue Gutenberg block assets for both editor and front-end.
119 */
120 public function block_assets() {
121 wp_enqueue_style( 'awsm-ead-public' );
122 wp_enqueue_script( 'awsm-ead-public' );
123 }
124
125 /**
126 * Enqueue Gutenberg block assets for backend editor.
127 *
128 * `wp-blocks`: includes block type registration and related functions.
129 * `wp-element`: includes the WordPress Element abstraction for describing the structure of your blocks.
130 * `wp-i18n`: To internationalize the block's text.
131 */
132 public function block_editor_assets() {
133 // Styles.
134 wp_enqueue_style(
135 'ead-block-editor-css',
136 plugins_url( 'blocks/document/editor.css', dirname( __FILE__ ) ),
137 array( 'ead_media_button' ),
138 AWSM_EMBED_VERSION,
139 'all'
140 );
141 // Scripts.
142 wp_enqueue_script(
143 'ead-block-editor-js',
144 plugins_url( 'blocks/document/document-block.js', dirname( __FILE__ ) ),
145 array( 'wp-blocks', 'wp-components', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-url', 'wp-api-fetch', 'lodash', 'ead_media_button', 'awsm-ead-pdf-object' ),
146 AWSM_EMBED_VERSION,
147 true
148 );
149 }
150 }
151
152 Awsm_embed_Guten_blocks::get_instance();
153