PluginProbe ʕ •ᴥ•ʔ
Presto Player / 4.3.3
Presto Player v4.3.3
4.3.3 4.3.2 4.3.1 4.3.0 4.2.4 4.2.3 4.2.2 4.2.0 4.2.1 trunk 1.10.0 1.10.1 1.10.2 1.11.0 1.12.0 1.13.0 1.14.0 1.14.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.13 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3-beta1 2.3.0 2.3.1 2.3.2 2.3.3 3.0.0 3.0.0-beta1 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1.0 3.1.1 3.1.2 3.1.3 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4
presto-player / inc / Integrations / Divi / Divi.php
presto-player / inc / Integrations / Divi Last commit date
D5 3 weeks ago includes 1 month ago Divi.php 6 days ago
Divi.php
206 lines
1 <?php
2 /**
3 * Handles divi integration related functionality.
4 *
5 * @package presto-player
6 */
7
8 namespace PrestoPlayer\Integrations\Divi;
9
10 use PrestoPlayer\Models\ReusableVideo;
11 use PrestoPlayer\Models\Post;
12
13 /**
14 * Handles divi-related functionality
15 */
16 class Divi {
17
18 /**
19 * Registers the Divi integration.
20 */
21 public function register() {
22 add_action( 'divi_module_library_modules_dependency_tree', array( $this, 'register_d5_modules' ) );
23 add_action( 'et_fb_framework_loaded', array( $this, 'register_d5_builder_assets' ) );
24 add_action( 'divi_extensions_init', array( $this, 'init' ) );
25 add_action( 'wp_ajax_presto_get_media_attributes', array( $this, 'getMediaItemAttributes' ) );
26 }
27
28 /**
29 * Registers D5 module dependencies with the Divi module library.
30 *
31 * @param object $tree The Divi module dependency tree.
32 * @return void
33 */
34 public function register_d5_modules( $tree ) {
35 $tree->add_dependency( new D5\PrestoVideoModule() );
36 }
37
38 /**
39 * Registers and enqueues the D5 builder script package.
40 *
41 * @return void
42 */
43 public function register_d5_builder_assets() {
44 if ( ! class_exists( 'ET\\Builder\\VisualBuilder\\Assets\\PackageBuildManager' ) ) {
45 return;
46 }
47 $asset_file = PRESTO_PLAYER_PLUGIN_DIR . 'dist/divi-d5.asset.php';
48 if ( ! file_exists( $asset_file ) ) {
49 return;
50 }
51 $asset = include $asset_file;
52 if ( ! is_array( $asset ) || empty( $asset['version'] ) || ! isset( $asset['dependencies'] ) ) {
53 return;
54 }
55 $data = array(
56 'ajaxurl' => admin_url( 'admin-ajax.php' ),
57 'nonce' => wp_create_nonce( 'wp_rest' ),
58 );
59 // PackageBuildManager localizes data_top_window/data_app_window under a global named
60 // pascal_case( name . 'Data' ) — so 'presto-player-divi-d5' exposes window.PrestoPlayerDiviD5Data,
61 // which is exactly what MediaHubField.jsx / PlayerEdit.jsx read. Keep the name in sync with that.
62 \ET\Builder\VisualBuilder\Assets\PackageBuildManager::register_package_build(
63 array(
64 'name' => 'presto-player-divi-d5',
65 'version' => $asset['version'],
66 'script' => array(
67 'src' => PRESTO_PLAYER_PLUGIN_URL . 'dist/divi-d5.js',
68 'deps' => array_merge( $asset['dependencies'], array( 'divi-module-library' ) ),
69 'args' => array( 'in_footer' => true ),
70 'enqueue_top_window' => true,
71 'enqueue_app_window' => true,
72 'data_top_window' => $data,
73 'data_app_window' => $data,
74 ),
75 )
76 );
77 }
78
79 /**
80 * Initializes the Divi integration.
81 */
82 public function init() {
83 // require our module.
84 include_once 'includes/PrestoDiviExtension.php';
85
86 // enqueue the scripts.
87 add_action( 'wp_enqueue_scripts', array( $this, 'enqueueScripts' ) );
88
89 // fix rankmath conflict.
90 add_filter( 'script_loader_tag', array( $this, 'rankMathFix' ), 11, 3 );
91
92 // parse the divi block to get the inner media hub block.
93 add_filter( 'presto_player_get_block_from_content', array( $this, 'getInnerBlockFromDiviContent' ) );
94 }
95
96 /**
97 * Gets the inner media hub block from the divi content for lessons and topics.
98 *
99 * @param array $block the Divi block array.
100 *
101 * @return array $block the filtered media hub inner block array.
102 */
103 public function getInnerBlockFromDiviContent( $block ) {
104 // bail if block is empty.
105 if ( empty( $block ) ) {
106 return $block;
107 }
108
109 $pattern = get_shortcode_regex( array( 'prpl_presto_player' ) );
110 $block_id = false;
111 if ( $block['innerHTML'] && preg_match( "/$pattern/", $block['innerHTML'], $matches ) ) {
112 $shortcode = $matches[0] ?? '';
113 if ( ! empty( $shortcode ) ) {
114 $atts = shortcode_parse_atts( $shortcode );
115 $block_id = isset( $atts['video_id'] ) ? (int) $atts['video_id'] : false;
116 }
117 }
118 if ( ! empty( $block_id ) ) {
119 // getMediaHubBlockFromPost() resolves the post itself, so don't build a Post
120 // model here - get_post() returns null for a deleted media hub item and the
121 // constructor is typed \WP_Post, which fataled the whole page.
122 $inner_block = Post::getMediaHubBlockFromPost( $block_id );
123 if ( ! empty( $inner_block ) ) {
124 return $inner_block;
125 }
126 }
127 return $block;
128 }
129
130 /**
131 * Fixes rankmath excluding wp-i18n script from iframe.
132 *
133 * @param string $tag The <script> tag for the enqueued script.
134 * @param string $handle The script's registered handle.
135 * @param string $src The script's source URL.
136 *
137 * @return string
138 */
139 public function rankMathFix( $tag, $handle, $src ) {
140 if ( 'wp-i18n' === $handle ) {
141 return '<script type="text/javascript" src="' . $src . '"></script>' . "\n"; // phpcs:ignore
142 }
143 return $tag;
144 }
145
146 /**
147 * Get attributes to inject into JSX component.
148 *
149 * AJAX callback (wp_ajax_presto_get_media_attributes); reads the id from $_POST
150 * and always responds via wp_send_json_*.
151 *
152 * @return void
153 */
154 public function getMediaItemAttributes() {
155 // D4 sends et_admin_load_nonce; D5 sends wp_rest nonce via prestoPlayer.nonce.
156 // wp_rest is a broad, shared nonce, so the current_user_can() check below is the real
157 // authorization gate here — don't drop it assuming the nonce alone scopes access.
158 $nonce = isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '';
159 $valid = wp_verify_nonce( $nonce, 'et_admin_load_nonce' )
160 || wp_verify_nonce( $nonce, 'wp_rest' );
161 if ( ! $valid ) {
162 wp_send_json_error( array( 'message' => __( 'Invalid nonce.', 'presto-player' ) ), 403 );
163 }
164
165 if ( ! current_user_can( 'edit_posts' ) ) {
166 wp_send_json_error( array( 'message' => __( 'You are not allowed to do this.', 'presto-player' ) ), 403 );
167 }
168
169 $id = isset( $_POST['id'] ) ? absint( wp_unslash( $_POST['id'] ) ) : 0;
170
171 if ( ! $id ) {
172 wp_send_json_error( array( 'message' => __( 'You must provide an id.', 'presto-player' ) ), 400 );
173 }
174
175 $video = new ReusableVideo( $id );
176 if ( ! $video->post || 'pp_video_block' !== $video->post->post_type ) {
177 wp_send_json_error( array( 'message' => __( 'Video not found.', 'presto-player' ) ), 404 );
178 }
179
180 wp_send_json_success( $video->getAttributes() );
181 }
182
183 /**
184 * Enqueues scripts for Divi integration.
185 */
186 public function enqueueScripts() {
187 if ( ! et_core_is_fb_enabled() ) {
188 return;
189 }
190
191 $assets = include trailingslashit( PRESTO_PLAYER_PLUGIN_DIR ) . 'dist/divi.asset.php';
192 wp_enqueue_script(
193 'surecart/divi/admin',
194 trailingslashit( PRESTO_PLAYER_PLUGIN_URL ) . 'dist/divi.js',
195 array_merge( array( 'react-dom', 'jquery', 'hls.js' ), $assets['dependencies'] ),
196 $assets['version'],
197 true
198 );
199 wp_enqueue_style( 'surecart/divi/admin', trailingslashit( PRESTO_PLAYER_PLUGIN_URL ) . 'dist/divi.css', array(), $assets['version'] );
200
201 if ( function_exists( 'wp_set_script_translations' ) ) {
202 wp_set_script_translations( 'surecart/divi/admin', 'presto-player' );
203 }
204 }
205 }
206