PluginProbe
FV Player 8 / 8.0.21
FV Player 8 v8.0.21
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 8.0.21, at models/gutenberg.php

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