Divi.php
135 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Integrations\Divi; |
| 4 | |
| 5 | use PrestoPlayer\Plugin; |
| 6 | use PrestoPlayer\WPackio\Enqueue; |
| 7 | use PrestoPlayer\Models\ReusableVideo; |
| 8 | use PrestoPlayer\Models\Post; |
| 9 | |
| 10 | /** |
| 11 | * Handles divi-related functionality |
| 12 | */ |
| 13 | class Divi |
| 14 | { |
| 15 | public function register() |
| 16 | { |
| 17 | add_action('divi_extensions_init', [$this, 'init']); |
| 18 | add_action('wp_ajax_presto_get_media_attributes', [$this, 'getMediaItemAttributes']); |
| 19 | } |
| 20 | |
| 21 | public function init() |
| 22 | { |
| 23 | // require our module |
| 24 | include_once 'includes/PrestoDiviExtension.php'; |
| 25 | |
| 26 | // enqueue the scripts |
| 27 | add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']); |
| 28 | |
| 29 | // fix rankmath conflict. |
| 30 | add_filter('script_loader_tag', [$this, 'rankMathFix'], 11, 3); |
| 31 | |
| 32 | // parse the divi block to get the inner media hub block |
| 33 | add_filter('presto_player_get_block_from_content', [$this, 'getInnerBlockFromDiviBlock']); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Gets the inner media hub block from the divi block. |
| 38 | * |
| 39 | * @param array $block the Divi block array. |
| 40 | * |
| 41 | * @return array $block the filtered media hub inner block array. |
| 42 | */ |
| 43 | public function getInnerBlockFromDiviBlock($block) |
| 44 | { |
| 45 | // bail if block is empty |
| 46 | if (empty($block)) { |
| 47 | return $block; |
| 48 | } |
| 49 | |
| 50 | // bail if block is not a divi block |
| 51 | if ("divi/placeholder" !== $block['blockName']) { |
| 52 | return $block; |
| 53 | } |
| 54 | |
| 55 | $pattern = get_shortcode_regex(['prpl_presto_player']); |
| 56 | $block_id = false; |
| 57 | if ($block['innerHTML'] && preg_match("/$pattern/", $block['innerHTML'], $matches)) { |
| 58 | $shortcode = $matches[0] ?? ''; |
| 59 | if (!empty($shortcode)) { |
| 60 | $atts = shortcode_parse_atts($shortcode); |
| 61 | $block_id = isset($atts['video_id']) ? (int) $atts['video_id'] : false; |
| 62 | } |
| 63 | } |
| 64 | if (!empty($block_id)) { |
| 65 | $post_model = new Post(get_post($block_id)); |
| 66 | $block = $post_model->getMediaHubBlockFromPost($block_id); |
| 67 | if (!empty($block)) { |
| 68 | return $block; |
| 69 | } |
| 70 | } |
| 71 | return $block; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Fixes rankmath excluding wp-i18n script from iframe. |
| 76 | * |
| 77 | * @param string $tag The <script> tag for the enqueued script. |
| 78 | * @param string $handle The script's registered handle. |
| 79 | * @param string $src The script's source URL. |
| 80 | * |
| 81 | * @return string |
| 82 | */ |
| 83 | public function rankMathFix($tag, $handle, $src) |
| 84 | { |
| 85 | if ('wp-i18n' === $handle) { |
| 86 | return '<script type="text/javascript" src="' . $src . '"></script>' . "\n"; // phpcs:ignore |
| 87 | } |
| 88 | return $tag; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get attributes to inject into JSX component |
| 93 | * |
| 94 | * @param integer $id |
| 95 | * @return void |
| 96 | */ |
| 97 | public function getMediaItemAttributes($id) |
| 98 | { |
| 99 | \check_ajax_referer('et_admin_load_nonce'); |
| 100 | |
| 101 | $id = (int) $_POST['id'] ?? 0; |
| 102 | |
| 103 | if (!$id) { |
| 104 | return new \WP_Error('invalid', 'You must provide an id', ['status' => 400]); |
| 105 | } |
| 106 | |
| 107 | if (!$video = new ReusableVideo($id)) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | return wp_send_json_success($video->getAttributes()); |
| 112 | } |
| 113 | |
| 114 | public function enqueueScripts() |
| 115 | { |
| 116 | if (!et_core_is_fb_enabled()) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | $assets = include trailingslashit(PRESTO_PLAYER_PLUGIN_DIR) . 'dist/divi.asset.php'; |
| 121 | wp_enqueue_script( |
| 122 | 'surecart/divi/admin', |
| 123 | trailingslashit(PRESTO_PLAYER_PLUGIN_URL) . 'dist/divi.js', |
| 124 | array_merge(['react-dom', 'jquery', 'hls.js'], $assets['dependencies']), |
| 125 | $assets['version'], |
| 126 | true |
| 127 | ); |
| 128 | wp_enqueue_style('surecart/divi/admin', trailingslashit(PRESTO_PLAYER_PLUGIN_URL) . 'dist/divi.css', [], $assets['version']); |
| 129 | |
| 130 | if (function_exists('wp_set_script_translations')) { |
| 131 | wp_set_script_translations('surecart/divi/admin', 'presto-player'); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 |