block-embedpress.php
73 lines
| 1 | <?php |
| 2 | // Exit if accessed directly. |
| 3 | |
| 4 | use EmbedPress\Providers\Youtube; |
| 5 | |
| 6 | if (!defined('ABSPATH')) { |
| 7 | exit; |
| 8 | } |
| 9 | /** |
| 10 | * It renders gutenberg block of embedpress on the frontend |
| 11 | * @param array $attributes |
| 12 | */ |
| 13 | |
| 14 | |
| 15 | function embedpress_render_block($attributes) |
| 16 | { |
| 17 | |
| 18 | |
| 19 | |
| 20 | if (!empty($attributes['embedHTML'])) { |
| 21 | $embed = apply_filters('embedpress_gutenberg_embed', $attributes['embedHTML'], $attributes); |
| 22 | $aligns = [ |
| 23 | 'left' => 'alignleft', |
| 24 | 'right' => 'alignright', |
| 25 | 'wide' => 'alignwide', |
| 26 | 'full' => 'alignfull', |
| 27 | 'center' => 'aligncenter', |
| 28 | ]; |
| 29 | if (isset($attributes['align'])) { |
| 30 | $alignment = isset($aligns[$attributes['align']]) ? $aligns[$attributes['align']] . ' clear' : ''; |
| 31 | } else { |
| 32 | $alignment = 'aligncenter'; // default alignment is center in js, so keeping same here |
| 33 | } |
| 34 | |
| 35 | ob_start(); |
| 36 | ?> |
| 37 | <div class="embedpress-gutenberg-wrapper"> |
| 38 | <div class="wp-block-embed__wrapper <?php echo esc_attr($alignment) ?>"> |
| 39 | <?php echo $embed; ?> |
| 40 | </div> |
| 41 | </div> |
| 42 | <?php |
| 43 | |
| 44 | |
| 45 | echo embedpress_render_block_style($attributes); |
| 46 | |
| 47 | return ob_get_clean(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Make style function for embedpress render block |
| 53 | */ |
| 54 | |
| 55 | function embedpress_render_block_style($attributes) |
| 56 | { |
| 57 | $uniqid = '.ose-uid-' . md5($attributes['url']); |
| 58 | $youtubeStyles = '<style> |
| 59 | ' . esc_attr($uniqid) . ' { |
| 60 | width: ' . esc_attr($attributes['width']) . 'px !important; |
| 61 | height: ' . esc_attr($attributes['height']) . 'px; |
| 62 | } |
| 63 | |
| 64 | ' . esc_attr($uniqid) . '>iframe { |
| 65 | height: ' . esc_attr($attributes['height']) . 'px !important; |
| 66 | max-height: ' . esc_attr($attributes['height']) . 'px !important; |
| 67 | width: 100%; |
| 68 | } |
| 69 | </style>'; |
| 70 | |
| 71 | return $youtubeStyles; |
| 72 | } |
| 73 |