Module.php
181 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Integrations\BeaverBuilder\ReusableVideoModule; |
| 4 | |
| 5 | use PrestoPlayer\Services\Scripts; |
| 6 | use PrestoPlayer\Models\ReusableVideo; |
| 7 | |
| 8 | class Module extends \FLBuilderModule |
| 9 | { |
| 10 | |
| 11 | public function __construct() |
| 12 | { |
| 13 | parent::__construct([ |
| 14 | 'name' => __('Presto Player Media', 'presto-player'), |
| 15 | 'description' => __('Presto Player Media', 'presto-player'), |
| 16 | 'group' => __('Presto Player', 'presto-player'), |
| 17 | 'category' => __('Presto Player', 'presto-player'), |
| 18 | 'dir' => PRESTO_PLAYER_BB_DIR . 'ReusableVideoModule/', |
| 19 | 'url' => PRESTO_PLAYER_BB_URL . 'ReusableVideoModule/', |
| 20 | 'icon' => 'button.svg', |
| 21 | 'editor_export' => true, // Defaults to true and can be omitted. |
| 22 | 'enabled' => true, // Defaults to true and can be omitted. |
| 23 | 'partial_refresh' => false, // Defaults to false and can be omitted. |
| 24 | ]); |
| 25 | |
| 26 | $this->slug = 'presto-player'; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Should be overridden by subclasses to enqueue |
| 31 | * additional css/js using the add_css and add_js methods. |
| 32 | * |
| 33 | * @since 1.0 |
| 34 | * @return void |
| 35 | */ |
| 36 | public function enqueue_scripts() |
| 37 | { |
| 38 | // always enqueue the css |
| 39 | $assets = include trailingslashit(PRESTO_PLAYER_PLUGIN_DIR) . 'dist/beaver-builder.asset.php'; |
| 40 | $this->add_css('surecart/beaver-builder/admin', trailingslashit(PRESTO_PLAYER_PLUGIN_URL) . 'dist/beaver-builder.css', [], $assets['version']); |
| 41 | |
| 42 | // Responsive iframe ui is enabled, enqueue the script for the parent window. |
| 43 | if (class_exists('\FLBuilderUIIFrame') && \FLBuilderUIIFrame::is_enabled()) { |
| 44 | add_action('fl_builder_ui_enqueue_scripts', [$this, 'enqueue_parent_window_script']); |
| 45 | } else { |
| 46 | $this->add_js( |
| 47 | 'surecart/beaver-builder/admin', |
| 48 | trailingslashit(PRESTO_PLAYER_PLUGIN_URL) . 'dist/beaver-builder.js', |
| 49 | array_merge(['jquery'], $assets['dependencies']), |
| 50 | $assets['version'], |
| 51 | true |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Enqueue script for the parent window. |
| 58 | * |
| 59 | * @since 2.0.13 |
| 60 | * @return void |
| 61 | */ |
| 62 | public function enqueue_parent_window_script() |
| 63 | { |
| 64 | $assets = include trailingslashit(PRESTO_PLAYER_PLUGIN_DIR) . 'dist/beaver-builder.asset.php'; |
| 65 | wp_enqueue_script( |
| 66 | 'surecart/beaver-builder/admin', |
| 67 | trailingslashit(PRESTO_PLAYER_PLUGIN_URL) . 'dist/beaver-builder.js', |
| 68 | array_merge(['jquery'], $assets['dependencies']), |
| 69 | $assets['version'], |
| 70 | true |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | public static function getSettings() |
| 75 | { |
| 76 | return [ |
| 77 | 'settings' => [ |
| 78 | 'title' => __('Settings', 'presto-player'), |
| 79 | 'sections' => [ |
| 80 | 'video_select' => [ |
| 81 | 'title' => 'Media', |
| 82 | 'fields' => [ |
| 83 | 'video_select_ajax' => [ |
| 84 | 'type' => 'raw', |
| 85 | 'label' => __('Select Media Hub Item', 'fl-builder'), |
| 86 | 'content' => self::dynamic_dropdown() |
| 87 | ], |
| 88 | 'video_id' => [ |
| 89 | 'type' => 'text', |
| 90 | ], |
| 91 | 'video_name' => [ |
| 92 | 'type' => 'text', |
| 93 | ], |
| 94 | 'url_override' => array( |
| 95 | 'type' => 'text', |
| 96 | 'label' => 'URL Override', |
| 97 | 'connections' => array('url') |
| 98 | ) |
| 99 | ] |
| 100 | ] |
| 101 | ] |
| 102 | ], |
| 103 | ]; |
| 104 | } |
| 105 | |
| 106 | public static function dynamic_dropdown() |
| 107 | { |
| 108 | ob_start(); |
| 109 | ?> |
| 110 | <div class="presto-builder--custom-video-controls"> |
| 111 | <div class="fl-builder--category-select" x-data="window.prestoBBDropdown({nonce: '<?php echo wp_create_nonce('wp_rest'); ?>'})" x-init="init"> |
| 112 | <div class="fl-builder--selector-display" x-on:click="open"> |
| 113 | <button class="fl-builder--selector-display-label"> |
| 114 | <span class="fl-builder--group-label"><?php esc_html_e('Media', 'presto-player'); ?></span> |
| 115 | <span class="fl-builder--current-view-name" x-text="video.name || 'Select media'"></span> |
| 116 | </button> |
| 117 | </div> |
| 118 | |
| 119 | <div class="presto-builder--selector-menu"> |
| 120 | <div class="presto-builder--menu" x-show="isOpen()" x-on:click.away="close"> |
| 121 | <input class="presto-builder--dropdown-search" x-ref="searchbox" type="text" x-model="search" placeholder="<?php _e('Search Media Hub', 'presto-player'); ?>" /> |
| 122 | <template x-if="loading"> |
| 123 | <svg width='14px' height='14px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" class="uil-ring"> |
| 124 | <rect x="0" y="0" width="100" height="100" fill="none" class="bk"></rect> |
| 125 | <circle cx="50" cy="50" r="44" stroke-dasharray="179.69909978533616 96.7610537305656" stroke="#2ea2cc" fill="none" stroke-width="12"> |
| 126 | <animateTransform attributeName="transform" type="rotate" values="0 50 50;180 50 50;360 50 50;" keyTimes="0;0.5;1" dur="1s" repeatCount="indefinite" begin="0s"></animateTransform> |
| 127 | </circle> |
| 128 | </svg> |
| 129 | </template> |
| 130 | |
| 131 | <template x-if="!loading" x-for="item in items" :key="item.ID"> |
| 132 | <button class="presto-builder--menu-item" x-text="item.post_title" x-on:click="setVideo(item)"></button> |
| 133 | </template> |
| 134 | </div> |
| 135 | |
| 136 | </div> |
| 137 | <div class="presto-builder--video-edit-buttons"> |
| 138 | <a href="/wp-admin/post-new.php?post_type=pp_video_block" class="fl-builder-button presto-create-bb-video" target="_blank"> |
| 139 | <?php _e('Create', 'presto-player'); ?> |
| 140 | </a> |
| 141 | <template x-if="video.id"> |
| 142 | <a x-bind:href="video.editLink" class="fl-builder-button presto-create-bb-video" target="_blank"> |
| 143 | <?php _e('Edit', 'presto-player'); ?> |
| 144 | </a> |
| 145 | </template> |
| 146 | </div> |
| 147 | </div> |
| 148 | <?php |
| 149 | return ob_get_clean(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Display the block |
| 154 | * |
| 155 | * @return void |
| 156 | */ |
| 157 | public function display() |
| 158 | { |
| 159 | if (!$this->settings->video_id) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | // global is the most reliable between page builders |
| 164 | global $load_presto_js; |
| 165 | $load_presto_js = true; |
| 166 | (new Scripts())->blockAssets(); // enqueue block assets |
| 167 | |
| 168 | $video = new ReusableVideo($this->settings->video_id); |
| 169 | $overrides = []; |
| 170 | if ($this->settings->url_override) { |
| 171 | $overrides['src'] = $this->settings->url_override; |
| 172 | } |
| 173 | $render = $video->renderBlock($overrides); |
| 174 | if ($render) { |
| 175 | echo $render; |
| 176 | return; |
| 177 | } |
| 178 | return; |
| 179 | } |
| 180 | } |
| 181 |