plugin.php
447 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Blocks Initializer |
| 5 | * |
| 6 | * Enqueue CSS/JS of all the blocks. |
| 7 | * |
| 8 | * @since 1.0.0 |
| 9 | * @package CGB |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | use EmbedPress\Includes\Classes\Helper; |
| 14 | |
| 15 | if (!defined('ABSPATH')) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * Enqueue Gutenberg block assets for both frontend + backend. |
| 22 | * |
| 23 | * @uses {wp-editor} for WP editor styles. |
| 24 | * @since 1.0.0 |
| 25 | */ |
| 26 | function embedpress_blocks_cgb_block_assets() |
| 27 | { // phpcs:ignore |
| 28 | // Styles. |
| 29 | wp_enqueue_style( |
| 30 | 'embedpress_blocks-cgb-style-css', // Handle. |
| 31 | EMBEDPRESS_GUTENBERG_DIR_URL . 'dist/blocks.style.build.css', // Block style CSS. |
| 32 | is_admin() ? array('wp-editor') : null, // Dependency to include the CSS after it. |
| 33 | filemtime(EMBEDPRESS_GUTENBERG_DIR_PATH . 'dist/blocks.style.build.css') // Version: File modification time. |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | // Hook: Frontend assets. |
| 38 | add_action('enqueue_block_assets', 'embedpress_blocks_cgb_block_assets'); |
| 39 | |
| 40 | /** |
| 41 | * Enqueue Gutenberg block assets for backend editor. |
| 42 | * |
| 43 | * @uses {wp-blocks} for block type registration & related functions. |
| 44 | * @uses {wp-element} for WP Element abstraction — structure of blocks. |
| 45 | * @uses {wp-i18n} to internationalize the block's text. |
| 46 | * @uses {wp-editor} for WP editor styles. |
| 47 | * @since 1.0.0 |
| 48 | */ |
| 49 | function embedpress_blocks_cgb_editor_assets() |
| 50 | { // phpcs:ignore |
| 51 | // Scripts. |
| 52 | if (!wp_script_is('embedpress-pdfobject')) { |
| 53 | wp_enqueue_script( |
| 54 | 'embedpress-pdfobject', |
| 55 | EMBEDPRESS_URL_ASSETS . 'js/pdfobject.min.js', |
| 56 | [], |
| 57 | EMBEDPRESS_VERSION |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | wp_enqueue_script( |
| 62 | 'embedpress_blocks-cgb-block-js', // Handle. |
| 63 | EMBEDPRESS_GUTENBERG_DIR_URL . 'dist/blocks.build.js', // Block.build.js: We register the block here. Built with Webpack. |
| 64 | array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-api-fetch', 'wp-is-shallow-equal', 'wp-editor', 'wp-components', 'embedpress-pdfobject'), // Dependencies, defined above. |
| 65 | filemtime(EMBEDPRESS_GUTENBERG_DIR_PATH . 'dist/blocks.build.js'), // Version: File modification time. |
| 66 | true // Enqueue the script in the footer. |
| 67 | ); |
| 68 | $wistia_labels = array( |
| 69 | 'watch_from_beginning' => __('Watch from the beginning', 'embedpress'), |
| 70 | 'skip_to_where_you_left_off' => __('Skip to where you left off', 'embedpress'), |
| 71 | 'you_have_watched_it_before' => __('It looks like you\'ve watched<br />part of this video before!', 'embedpress'), |
| 72 | ); |
| 73 | $elements = (array) get_option(EMBEDPRESS_PLG_NAME . ":elements", []); |
| 74 | $active_blocks = isset($elements['gutenberg']) ? (array) $elements['gutenberg'] : []; |
| 75 | |
| 76 | $wistia_labels = json_encode($wistia_labels); |
| 77 | $wistia_options = null; |
| 78 | if (function_exists('embedpress_wisita_pro_get_options')) : |
| 79 | $wistia_options = embedpress_wisita_pro_get_options(); |
| 80 | endif; |
| 81 | $pars_url = wp_parse_url(get_site_url()); |
| 82 | $documents_cta_options = (array) get_option(EMBEDPRESS_PLG_NAME . ':document'); |
| 83 | wp_localize_script('embedpress_blocks-cgb-block-js', 'embedpressObj', array( |
| 84 | 'wistia_labels' => $wistia_labels, |
| 85 | 'wisita_options' => $wistia_options, |
| 86 | 'embedpress_powered_by' => apply_filters('embedpress_document_block_powered_by', true), |
| 87 | 'embedpress_pro' => defined('EMBEDPRESS_PRO_PLUGIN_FILE'), |
| 88 | 'twitch_host' => !empty($pars_url['host']) ? $pars_url['host'] : '', |
| 89 | 'site_url' => site_url(), |
| 90 | 'active_blocks' => $active_blocks, |
| 91 | 'document_cta' => $documents_cta_options, |
| 92 | 'pdf_renderer' => Helper::get_pdf_renderer(), |
| 93 | 'is_pro_plugin_active' => defined('EMBEDPRESS_SL_ITEM_SLUG'), |
| 94 | )); |
| 95 | |
| 96 | // Styles. |
| 97 | wp_enqueue_style( |
| 98 | 'embedpress_blocks-cgb-block-editor-css', // Handle. |
| 99 | EMBEDPRESS_GUTENBERG_DIR_URL . 'dist/blocks.editor.build.css', // Block editor CSS. |
| 100 | array('wp-edit-blocks'), // Dependency to include the CSS after it. |
| 101 | filemtime(EMBEDPRESS_GUTENBERG_DIR_PATH . 'dist/blocks.editor.build.css') // Version: File modification time. |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | // Hook: Editor assets. |
| 106 | add_action('enqueue_block_editor_assets', 'embedpress_blocks_cgb_editor_assets'); |
| 107 | |
| 108 | |
| 109 | function embedpress_block_category($categories, $post) |
| 110 | { |
| 111 | return array_merge( |
| 112 | $categories, |
| 113 | array( |
| 114 | array( |
| 115 | 'slug' => 'embedpress', |
| 116 | 'title' => 'EmbedPress', |
| 117 | 'icon' => '', |
| 118 | ), |
| 119 | ) |
| 120 | ); |
| 121 | } |
| 122 | $wp_version = get_bloginfo('version', 'display'); |
| 123 | if (version_compare($wp_version, '5.8', '>=')) { |
| 124 | add_filter('block_categories_all', 'embedpress_block_category', 10, 2); |
| 125 | } else { |
| 126 | add_filter('block_categories', 'embedpress_block_category', 10, 2); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | foreach (glob(EMBEDPRESS_GUTENBERG_DIR_PATH . 'block-backend/*.php') as $block_logic) { |
| 133 | require_once $block_logic; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Registers the embedpress gutneberg block on server. |
| 138 | */ |
| 139 | |
| 140 | function embedpress_gutenberg_register_all_block() |
| 141 | { |
| 142 | if (function_exists('register_block_type')) : |
| 143 | |
| 144 | $elements = (array) get_option(EMBEDPRESS_PLG_NAME . ":elements", []); |
| 145 | $g_blocks = isset($elements['gutenberg']) ? (array) $elements['gutenberg'] : []; |
| 146 | $blocks_to_registers = ['twitch-block', 'google-slides-block', 'google-sheets-block', 'google-maps-block', 'google-forms-block', 'google-drawings-block', 'google-docs-block', 'embedpress', 'embedpress-pdf', 'embedpress-calendar', 'document']; |
| 147 | |
| 148 | foreach ($blocks_to_registers as $blocks_to_register) { |
| 149 | if (!empty($g_blocks[$blocks_to_register])) { |
| 150 | if ('embedpress' === $blocks_to_register) { |
| 151 | register_block_type('embedpress/embedpress', [ |
| 152 | 'render_callback' => 'embedpress_render_block', |
| 153 | 'attributes' => array( |
| 154 | 'height' => [ |
| 155 | 'type' => 'string', |
| 156 | 'default' => '450' |
| 157 | ], |
| 158 | 'width' => [ |
| 159 | 'type' => 'string', |
| 160 | 'default' => '600' |
| 161 | ], |
| 162 | |
| 163 | 'isGutenberg' => [ |
| 164 | 'type' => 'boolean', |
| 165 | 'default' => false |
| 166 | ], |
| 167 | //Youtube Attributes |
| 168 | 'starttime' => [ |
| 169 | 'type' => 'string', |
| 170 | ], |
| 171 | 'endtime' => [ |
| 172 | 'type' => 'string', |
| 173 | ], |
| 174 | 'autoplay' => [ |
| 175 | 'type' => 'boolean', |
| 176 | 'default' => false |
| 177 | ], |
| 178 | 'controls' => [ |
| 179 | 'type' => 'string', |
| 180 | ], |
| 181 | 'progressbarcolor' => [ |
| 182 | 'type' => 'string', |
| 183 | ], |
| 184 | 'videoannotations' => [ |
| 185 | 'type' => 'string', |
| 186 | ], |
| 187 | 'closedcaptions' => [ |
| 188 | 'type' => 'boolean', |
| 189 | 'default' => true |
| 190 | ], |
| 191 | 'relatedvideos' => [ |
| 192 | 'type' => 'boolean', |
| 193 | 'default' => true |
| 194 | ], |
| 195 | 'fullscreen' => [ |
| 196 | 'type' => 'boolean', |
| 197 | 'default' => true |
| 198 | ], |
| 199 | |
| 200 | 'modestbranding' => [ |
| 201 | 'type' => 'string', |
| 202 | ], |
| 203 | //Wistia Attributes |
| 204 | 'wstarttime' => [ |
| 205 | 'type' => 'string', |
| 206 | ], |
| 207 | 'wautoplay' => [ |
| 208 | 'type' => 'boolean', |
| 209 | 'default' => true |
| 210 | ], |
| 211 | 'scheme' => [ |
| 212 | 'type' => 'string', |
| 213 | ], |
| 214 | 'captions' => [ |
| 215 | 'type' => 'boolean', |
| 216 | 'default' => true |
| 217 | ], |
| 218 | 'playbutton' => [ |
| 219 | 'type' => 'boolean', |
| 220 | 'default' => true |
| 221 | ], |
| 222 | 'smallplaybutton' => [ |
| 223 | 'type' => 'boolean', |
| 224 | 'default' => true |
| 225 | ], |
| 226 | 'playbar' => [ |
| 227 | 'type' => 'boolean', |
| 228 | 'default' => true |
| 229 | ], |
| 230 | 'resumable' => [ |
| 231 | 'type' => 'boolean', |
| 232 | 'default' => true |
| 233 | ], |
| 234 | 'wistiafocus' => [ |
| 235 | 'type' => 'boolean', |
| 236 | 'default' => true |
| 237 | ], |
| 238 | 'volumecontrol' => [ |
| 239 | 'type' => 'boolean', |
| 240 | 'default' => true |
| 241 | ], |
| 242 | 'volume' => [ |
| 243 | 'type' => 'number', |
| 244 | 'default' => 100 |
| 245 | ], |
| 246 | 'rewind' => [ |
| 247 | 'type' => 'boolean', |
| 248 | 'default' => false |
| 249 | ], |
| 250 | 'wfullscreen' => [ |
| 251 | 'type' => 'boolean', |
| 252 | 'default' => true |
| 253 | ], |
| 254 | |
| 255 | ), |
| 256 | ]); |
| 257 | } elseif ('embedpress-pdf' === $blocks_to_register) { |
| 258 | register_block_type('embedpress/embedpress-pdf', [ |
| 259 | 'attributes' => array( |
| 260 | 'powered_by' => [ |
| 261 | 'type' => 'boolean', |
| 262 | 'default' => true |
| 263 | ], |
| 264 | |
| 265 | 'presentation' => [ |
| 266 | 'type' => "boolean", |
| 267 | 'default' => true, |
| 268 | ], |
| 269 | |
| 270 | 'position' => [ |
| 271 | 'type' => "string", |
| 272 | 'default' => 'top', |
| 273 | ], |
| 274 | |
| 275 | 'print' => [ |
| 276 | 'type' => "boolean", |
| 277 | 'default' => true, |
| 278 | ], |
| 279 | |
| 280 | 'download' => [ |
| 281 | 'type' => "boolean", |
| 282 | 'default' => true, |
| 283 | ], |
| 284 | 'open' => [ |
| 285 | 'type' => "boolean", |
| 286 | 'default' => true, |
| 287 | ], |
| 288 | 'copy_text' => [ |
| 289 | 'type' => "boolean", |
| 290 | 'default' => true, |
| 291 | ], |
| 292 | 'toolbar' => [ |
| 293 | 'type' => "boolean", |
| 294 | 'default' => true, |
| 295 | ], |
| 296 | 'doc_details' => [ |
| 297 | 'type' => "boolean", |
| 298 | 'default' => true, |
| 299 | ], |
| 300 | 'doc_rotation' => [ |
| 301 | 'type' => "boolean", |
| 302 | 'default' => true, |
| 303 | ], |
| 304 | 'unitoption' => [ |
| 305 | 'type' => "string", |
| 306 | 'default' => 'px', |
| 307 | ], |
| 308 | ), |
| 309 | 'render_callback' => 'embedpress_pdf_render_block', |
| 310 | ]); |
| 311 | } elseif ('embedpress-calendar' === $blocks_to_register) { |
| 312 | register_block_type('embedpress/embedpress-calendar', [ |
| 313 | 'render_callback' => 'embedpress_calendar_render_block', |
| 314 | ]); |
| 315 | } else { |
| 316 | register_block_type('embedpress/' . $blocks_to_register); |
| 317 | } |
| 318 | } else { |
| 319 | |
| 320 | if (WP_Block_Type_Registry::get_instance()->is_registered('embedpress/' . $blocks_to_register)) { |
| 321 | unregister_block_type('embedpress/' . $blocks_to_register); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | endif; |
| 327 | } |
| 328 | |
| 329 | add_action('init', 'embedpress_gutenberg_register_all_block'); |
| 330 | |
| 331 | function getParamData($attributes) |
| 332 | { |
| 333 | |
| 334 | $urlParamData = array( |
| 335 | 'themeMode' => !empty($attributes['themeMode']) ? $attributes['themeMode'] : 'default', |
| 336 | 'toolbar' => !empty($attributes['toolbar']) ? 'true' : 'false', |
| 337 | 'position' => $attributes['position'], |
| 338 | 'presentation' => !empty($attributes['presentation']) ? 'true' : 'false', |
| 339 | 'download' => !empty($attributes['download']) ? 'true' : 'false', |
| 340 | 'copy_text' => !empty($attributes['copy_text']) ? 'true' : 'false', |
| 341 | 'doc_rotation' => !empty($attributes['doc_rotation']) ? 'true' : 'false', |
| 342 | 'doc_details' => !empty($attributes['doc_details']) ? 'true' : 'false', |
| 343 | ); |
| 344 | |
| 345 | return "#" . http_build_query($urlParamData); |
| 346 | } |
| 347 | |
| 348 | function embedpress_pdf_render_block($attributes) |
| 349 | { |
| 350 | |
| 351 | if (!empty($attributes['href'])) { |
| 352 | $renderer = Helper::get_pdf_renderer(); |
| 353 | $pdf_url = $attributes['href']; |
| 354 | $id = !empty($attributes['id']) ? $attributes['id'] : 'embedpress-pdf-' . rand(100, 10000); |
| 355 | |
| 356 | $unitoption = !empty($attributes['unitoption']) ? $attributes['unitoption'] : 'px'; |
| 357 | $width = !empty($attributes['width']) ? $attributes['width'] . $unitoption : '600px'; |
| 358 | |
| 359 | $height = !empty($attributes['height']) ? $attributes['height'] . 'px' : '600px'; |
| 360 | $gen_settings = get_option(EMBEDPRESS_PLG_NAME); |
| 361 | |
| 362 | $powered_by = isset($gen_settings['embedpress_document_powered_by']) && 'yes' === $gen_settings['embedpress_document_powered_by']; |
| 363 | if (isset($attributes['powered_by'])) { |
| 364 | $powered_by = $attributes['powered_by']; |
| 365 | } |
| 366 | |
| 367 | $src = $renderer . ((strpos($renderer, '?') == false) ? '?' : '&') . 'file=' . $attributes['href'] . getParamData($attributes); |
| 368 | |
| 369 | $hash = md5($id); |
| 370 | $aligns = [ |
| 371 | 'left' => 'ep-alignleft', |
| 372 | 'right' => 'ep-alignright', |
| 373 | 'center' => 'ep-aligncenter', |
| 374 | 'wide' => 'ep-alignwide', |
| 375 | 'full' => 'ep-alignfull' |
| 376 | ]; |
| 377 | $alignment = isset($attributes['align']) && isset($aligns[$attributes['align']]) ? $aligns[$attributes['align']] : ''; |
| 378 | $dimension = "width:$width;height:$height"; |
| 379 | ob_start(); |
| 380 | ?> |
| 381 | <div class="embedpress-document-embed embedpress-pdf ose-document ep-doc-<?php echo esc_attr($hash) . ' ' . esc_attr($alignment) ?>"> |
| 382 | <div class="embedpress-inner-iframe <?php if($unitoption === '%') echo esc_attr('emebedpress-unit-percent'); ?>" <?php if($unitoption === '%' && !empty($attributes['width'])) {echo 'style="'.esc_attr('width:'.$attributes['width'].'%').'"';}else{echo 'style="'.esc_attr('width:100%').'"';} ?>> |
| 383 | <iframe class="embedpress-embed-document-pdf <?php echo esc_attr($id); ?>" style="<?php echo esc_attr($dimension); ?>; max-width:100%; display: inline-block" src="<?php echo esc_attr($src); ?>" frameborder="0"></iframe> |
| 384 | |
| 385 | <?php do_action('embedpress_pdf_gutenberg_after_embed', $hash, 'pdf', $attributes, $pdf_url); ?> |
| 386 | |
| 387 | <?php |
| 388 | if ($powered_by) { |
| 389 | printf('<p class="embedpress-el-powered">%s</p>', __('Powered By EmbedPress', 'embedpress')); |
| 390 | } ?> |
| 391 | </div> |
| 392 | |
| 393 | </div> |
| 394 | <?php |
| 395 | |
| 396 | return ob_get_clean(); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | function embedpress_calendar_render_block($attributes) |
| 401 | { |
| 402 | |
| 403 | $id = !empty($attributes['id']) ? $attributes['id'] : 'embedpress-calendar-' . rand(100, 10000); |
| 404 | $url = !empty($attributes['url']) ? $attributes['url'] : ''; |
| 405 | $is_private = isset($attributes['is_public']); |
| 406 | $hash = md5($id); |
| 407 | $width = !empty($attributes['width']) ? $attributes['width'] . 'px' : '600px'; |
| 408 | $height = !empty($attributes['height']) ? $attributes['height'] . 'px' : '600px'; |
| 409 | $gen_settings = get_option(EMBEDPRESS_PLG_NAME); |
| 410 | $powered_by = isset($gen_settings['embedpress_document_powered_by']) && 'yes' === $gen_settings['embedpress_document_powered_by']; |
| 411 | if (isset($attributes['powered_by'])) { |
| 412 | $powered_by = $attributes['powered_by']; |
| 413 | } |
| 414 | |
| 415 | $aligns = [ |
| 416 | 'left' => 'alignleft', |
| 417 | 'right' => 'alignright', |
| 418 | 'wide' => 'alignwide', |
| 419 | 'full' => 'alignfull' |
| 420 | ]; |
| 421 | $alignment = isset($attributes['align']) && isset($aligns[$attributes['align']]) ? $aligns[$attributes['align']] : ''; |
| 422 | $dimension = "width:$width;height:$height"; |
| 423 | ob_start(); |
| 424 | ?> |
| 425 | <div class="embedpress-calendar-gutenberg embedpress-calendar ose-calendar <?php echo esc_attr($alignment) ?>" style="<?php echo esc_attr($dimension); ?>; max-width:100%;"> |
| 426 | |
| 427 | <?php |
| 428 | if (!empty($url) && !$is_private) { |
| 429 | ?> |
| 430 | <iframe style="<?php echo esc_attr($dimension); ?>; max-width:100%; display: inline-block" src="<?php echo esc_attr($url); ?>"></iframe> |
| 431 | <?php } else { |
| 432 | if (is_embedpress_pro_active()) { |
| 433 | echo Embedpress_Google_Helper::shortcode(); |
| 434 | } |
| 435 | } ?> |
| 436 | <?php do_action('embedpress_calendar_gutenberg_after_embed', $hash, 'calendar', $attributes); ?> |
| 437 | |
| 438 | <?php |
| 439 | if ($powered_by) { |
| 440 | printf('<p class="embedpress-el-powered" style="width:' . esc_attr($width) . '" >%s</p>', __('Powered By EmbedPress', 'embedpress')); |
| 441 | } ?> |
| 442 | |
| 443 | </div> |
| 444 | <?php |
| 445 | return ob_get_clean(); |
| 446 | } |
| 447 |