PluginProbe
FV Player 8 / trunk
FV Player 8 vtrunk
trunk 8.0.18 8.0.19 8.0.20 8.0.21 8.0.25 8.0.27 8.1 8.1.3
fv-player / models / gutenberg.php

gutenberg.php in FV Player 8 trunk, at models/gutenberg.php

230 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 function fv_player_gutenberg() {
8 wp_register_script( 'fv-player-gutenberg', flowplayer::get_plugin_url().'/blocks/build/index.js', array( 'wp-blocks', 'wp-element','wp-editor', 'wp-components', 'wp-i18n', 'wp-dom-ready'), filemtime( __DIR__.'/../blocks/build/index.js' ) );
9
10 wp_localize_script( 'fv-player-gutenberg', 'fv_player_gutenberg', array(
11 'nonce' => wp_create_nonce( 'fv_player_gutenberg' ),
12 ));
13
14 if( function_exists('register_block_type') ) {
15 register_block_type( 'fv-player-gutenberg/basic', array(
16 'api_version' => 3,
17 'editor_script' => 'fv-player-gutenberg',
18 'render_callback' => 'fv_player_block_render',
19 'attributes' => array(
20 'align' => array( // block alignment in popover
21 'type' => 'string',
22 'default' => '',
23 ),
24 'className' => array( // set in advanced block settings
25 'type' => 'string',
26 'default' => '',
27 ),
28 'src' => array(
29 'type' => 'string',
30 'default' => '',
31 ),
32 'splash' => array(
33 'type' => 'string',
34 'default' => '',
35 ),
36 'timeline_previews' => array(
37 'type' => 'string',
38 'default' => '',
39 ),
40 'hls_hlskey' => array(
41 'type' => 'string',
42 'default' => '',
43 ),
44 'title' => array(
45 'type' => 'string',
46 'default' => '',
47 ),
48 'shortcodeContent' => array(
49 'type' => 'string',
50 'default' => '',
51 'source' => 'text'
52 ),
53 'player_id' => array(
54 'type' => 'string',
55 'default' => '0',
56 ),
57 'splash_attachment_id' => array(
58 'type' => 'string',
59 'default' => '0',
60 ),
61 'forceUpdate' => array(
62 'type' => 'string',
63 'default' => '0',
64 )
65 ),
66 ));
67 }
68 }
69
70 function fv_player_block_render($attributes, $content, $block) {
71 ob_start();
72
73 // Runs if the block was created with FV Player 8
74 if( !empty( $attributes['player_id'] ) && !empty( $attributes['src'] ) ) {
75 $shortcode_dimensions = '';
76
77 if( $attributes['align'] == 'wide' || $attributes['align'] == 'full' ) {
78 $shortcode_dimensions = 'width="100%" height="100%"';
79 } else if( $attributes['align'] == 'left' || $attributes['align'] == 'right' ) {
80 $shortcode_dimensions = 'align="left|right"';
81 }
82
83 echo '<div class="' . esc_attr( $attributes['className'] ) . ' align' . esc_attr( $attributes['align'] ) .'">' . do_shortcode( '[fvplayer id="' . intval( $attributes['player_id'] ) . '" ' . esc_html( $shortcode_dimensions ) . ']' ) . '</div>';
84
85 /**
86 * Runs if the block was created with FV Player 7.
87 * Perhaps a better solution would be to use the render_block filter, but that would require a lot of code
88 * changes and testing.
89 */
90 } else if ( ! is_admin() && preg_match( '~\[fvplayer.*?\]~', $content, $shortcode ) ) {
91 $shortcode_dimensions = '';
92
93 if ( $attributes['align'] == 'wide' || $attributes['align'] == 'full' ) {
94 $shortcode_dimensions = 'width="100%" height="100%"';
95 } else if( $attributes['align'] == 'left' || $attributes['align'] == 'right' ) {
96 $shortcode_dimensions = 'align="left|right"';
97 }
98
99 echo '<div class="' . esc_attr( $attributes['className'] ) . ' align' . esc_attr( $attributes['align'] ) .'">' . do_shortcode( $shortcode[0] ) . '</div>';
100
101 } else if ( empty( $attributes['player_id']) && is_admin() ) {
102 echo 'No player created yet.';
103 } else if ( empty( $attributes['src']) && is_admin() ) {
104 echo 'No video added yet.';
105 }
106
107 $output = ob_get_clean();
108 return $output;
109 }
110
111 add_action( 'init', 'fv_player_gutenberg' );
112
113 function fv_player_block_add_missing_attributes_callback($matches) {
114 $player_id = preg_match('/id="(\d+)"/', $matches[0], $player_id_matches) ? $player_id_matches[1] : 0;
115
116 // bail out if no player id
117 if ( ! $player_id ) {
118 return '<!-- wp:fv-player-gutenberg/basic --><!-- /wp:fv-player-gutenberg/basic -->';
119 }
120
121 $player = new FV_Player_Db_Player( $player_id );
122
123 $attributes = array(
124 'align' => '',
125 'className' => '',
126 'src' => '',
127 'splash' => '',
128 'timeline_previews' => '',
129 'hls_hlskey' => '',
130 'title' => '',
131 'player_id' => '',
132 'splash_attachment_id' => '',
133 'cover' => '',
134 'forceUpdate' => 0
135 );
136
137 $attributes['player_id'] = $player_id;
138 $content = '[fvplayer id="' . $player_id . '"]';
139
140 // get data from first video
141 foreach( $player->getVideos() AS $video ) {
142 $attributes['src'] = $video->getSrc() ? $video->getSrc() : '';
143 $attributes['splash'] = $video->getSplash() ? $video->getSplash() : '';
144 $attributes['title'] = $video->getTitle() ? $video->getTitle() : '';
145 $attributes['splash_attachment_id'] = $video->getSplashAttachmentId() ? $video->getSplashAttachmentId() : '0';
146 $attributes['timeline_previews'] = $video->getMetaValue('timeline_previews',true) ? $video->getMetaValue('timeline_previews', true) : '';
147 $attributes['hls_hlskey'] = $video->getMetaValue('hls_hlskey',true) ? $video->getMetaValue('hls_hlskey', true) : '';
148 break;
149 }
150
151 return '<!-- wp:fv-player-gutenberg/basic ' . wp_json_encode($attributes) . ' -->'. PHP_EOL . $content . PHP_EOL . '<!-- /wp:fv-player-gutenberg/basic -->';
152 }
153
154 /**
155 * Update block attributes from FV Player 7 block to FV Player 8 block.
156 * Somehow this does not run for patterns as it only sees block markup like <!-- wp:block {"ref":86} /-->.
157 * Perhaps a better solution would be to use the render_block filter, but that would require a lot of code
158 * changes and testing.
159 *
160 * @param string $content Post content
161 * @return string
162 */
163 function fv_player_update_block_attributes($content) {
164 $content = preg_replace_callback('/<!-- wp:fv-player-gutenberg\/basic -->(.*?)<!-- \/wp:fv-player-gutenberg\/basic -->/s', 'fv_player_block_add_missing_attributes_callback', $content);
165
166 return $content;
167 }
168
169 function fv_player_handle_post_content($content) {
170 // get post object
171 global $post;
172
173 if ( ! $post || !$content ) {
174 return $content;
175 }
176
177 $updated_content = fv_player_update_block_attributes($content);
178
179 return $updated_content;
180 }
181
182 // frontend, before block generates HTML
183 add_filter( 'the_content', 'fv_player_handle_post_content', 8 );
184
185 function fv_player_handle_rest_content($response) {
186 if ( ! empty( $response->data['content'] ) && ! empty( $response->data['content']['raw'] ) ) {
187 $updated_content = fv_player_update_block_attributes($response->data['content']['raw']);
188
189 // check if changed
190 if ( $updated_content !== $response->data['content']['raw'] ) {
191 $response->data['content']['raw'] = $updated_content;
192 }
193 }
194
195 return $response;
196 }
197
198 // Register the rest_prepare_post filter for all post types once they got a chance to be registered
199 add_action( 'init', 'fv_player_handle_rest_content_register', 999 );
200
201 function fv_player_handle_rest_content_register() {
202 if ( function_exists( 'get_post_types' ) ) {
203 $post_types = get_post_types( array( 'show_in_rest' => true ), 'names' );
204 foreach ( $post_types as $post_type ) {
205 add_filter( 'rest_prepare_' . $post_type, 'fv_player_handle_rest_content' );
206 }
207 }
208 }
209
210 // Force load FV Player CSS and JS for Site Editor
211 add_action( 'enqueue_block_assets', 'fv_player_enqueue_block_assets_for_site_editor' );
212
213 function fv_player_enqueue_block_assets_for_site_editor() {
214 if (
215 ! did_action( 'load-site-editor.php' ) &&
216 ! did_action( 'load-post.php' ) &&
217 ! did_action( 'load-post-new.php' )
218 ) {
219 return;
220 }
221
222 // Load all of CSS
223 global $fv_fp;
224 $fv_fp->css_enqueue( true );
225
226 // Load scripts
227 do_action( 'fv_player_force_load_assets' );
228
229 flowplayer_prepare_scripts();
230 }