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 / learndash.php

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

258 lines 9.5 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 class FV_Player_Learndash_LMS {
8
9 function __construct() {
10 add_filter( 'plugins_loaded', array( $this, 'plugin_load' ) );
11 }
12
13 function plugin_load() {
14 if( !defined('LEARNDASH_VERSION') ) {
15 return;
16 }
17
18 // Register FV Player Custom Video field for LearnDash lesson settings
19 add_filter( 'init', array( $this, 'register_fv_player_field' ) );
20 // Make sure it does not appear as a standard meta box
21 add_action( 'add_meta_boxes', array( $this, 'remove_fv_player_meta_box' ), PHP_INT_MAX );
22
23 // Field for Learndash
24 // FV Player display
25 add_filter( 'learndash_settings_field', array( $this, 'display_field' ), 10, 2 );
26 // Register "Use FV Player" and "FV Player"
27 add_filter( 'learndash_settings_fields', array( $this, 'editing_field' ), 10, 2 );
28 add_filter( 'ld_video_provider', array( $this, 'set_provider' ), 10, 2 );
29
30 // TODO: Only load where needed
31 add_action( 'admin_init', array( $this, 'admin_load_assets' ), 10, 2 );
32
33 // We need to save our custom fields
34 // Here we also adjust the Video URL field of Learndash
35 add_action( 'save_post', array( $this, 'save_field' ), PHP_INT_MAX );
36 }
37
38 function admin_load_assets() {
39 global $fv_wp_flowplayer_ver;
40 wp_enqueue_script('fvplayer-learndash-lms-admin', plugins_url('js/learndash-lms-admin.js', dirname(__FILE__) ), array('jquery'), $fv_wp_flowplayer_ver, true );
41 }
42
43 function display_field( $field_args ) {
44 if( $field_args['name'] == 'lesson_fv_player' ) {
45 global $FV_Player_Custom_Videos_form_instances;
46 if( !empty($FV_Player_Custom_Videos_form_instances['fv_player_custom_videos-field_lesson_fv_player']) && method_exists($FV_Player_Custom_Videos_form_instances['fv_player_custom_videos-field_lesson_fv_player'], 'get_form') ) {
47 $objVideos = $FV_Player_Custom_Videos_form_instances['fv_player_custom_videos-field_lesson_fv_player'];
48 $field_args['html'] = $objVideos->get_form();
49 } else {
50 $field_args['html'] = 'Failed to load FV Player Editor.';
51 }
52 }
53 return $field_args;
54 }
55
56 function editing_field( $setting_option_fields, $settings_metabox_key ) {
57 if( in_array($settings_metabox_key, array('learndash-lesson-display-content-settings', 'learndash-topic-display-content-settings')) ) {
58
59 $new = array();
60 foreach( $setting_option_fields AS $k => $v ) {
61 $new[$k] = $v;
62
63 // Add new settings after "Video URL"
64 if( $k == 'lesson_video_url' ) {
65
66 // We have to load the field value ourselves: https://developers.learndash.com/hook/learndash_settings_fields/
67 $post_id = get_the_ID();
68 $settings_value = get_post_meta( $post_id, 'lesson_use_fvplayer_video', true );
69
70 $new['lesson_use_fvplayer_video'] = array(
71 'name' => 'lesson_use_fvplayer_video',
72 'label' => esc_html__('Use FV Player', 'learndash'),
73 'type' => 'checkbox-switch',
74 'value' => $settings_value,
75 'help_text' => esc_html__('Use the FV Player video in your post content for video progression.', 'learndash'),
76 'default' => '',
77 'options' => array(
78 'on' => esc_html__('Use FV Player for video progression.', 'learndash'),
79 '' => '',
80 ),
81 'parent_setting' => 'lesson_video_enabled',
82 );
83
84 $new['lesson_fv_player'] = array(
85 'name' => 'lesson_fv_player',
86 'label' => esc_html__( 'FV Player', 'learndash' ),
87 'type' => 'custom',
88 'class' => 'full-text',
89 'default' => '',
90 'placeholder' => esc_html__( 'FV Player', 'learndash' ),
91 'attrs' => array(
92 'rows' => '1',
93 'cols' => '57',
94 ),
95 'parent_setting' => 'lesson_video_enabled',
96 'rest' => array(
97 'show_in_rest' => LearnDash_REST_API::enabled(),
98 'rest_args' => array(
99 'schema' => array(
100 'field_key' => 'fv_player',
101 // translators: placeholder: Lesson.
102 'description' => sprintf( esc_html_x( '%s FV Player', 'placeholder: Lesson', 'learndash' ), learndash_get_custom_label( 'lesson' ) ),
103 'type' => 'text',
104 'default' => '',
105 ),
106 ),
107 ),
108 );
109 }
110 }
111
112 $setting_option_fields = $new;
113 }
114 return $setting_option_fields;
115 }
116
117 function register_fv_player_field() {
118 if( class_exists('FV_Player_MetaBox') ) {
119 new FV_Player_MetaBox( array(
120 'name' => 'FV Player',
121 'meta_key' => 'lesson_fv_player',
122 'post_type' => 'sfwd-lessons',
123 'display' => false,
124 'multiple' => false
125 )
126 );
127 new FV_Player_MetaBox( array(
128 'name' => 'FV Player',
129 'meta_key' => 'lesson_fv_player',
130 'post_type' => 'sfwd-topic',
131 'display' => false,
132 'multiple' => false
133 )
134 );
135 }
136 }
137
138 function remove_fv_player_meta_box() {
139 remove_meta_box('fv_player_custom_videos-field_lesson_fv_player', null, 'normal' );
140 }
141
142 // https://developers.learndash.com/hook/learndash_settings_fields/
143 function save_field( $post_id ) {
144
145 $saving_lesson =
146 isset( $_POST['learndash-lesson-access-settings']['nonce'] ) &&
147 wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['learndash-lesson-access-settings']['nonce'] ) ), 'learndash-lesson-access-settings' );
148
149 $saving_topic =
150 isset( $_POST['learndash-topic-access-settings']['nonce'] ) &&
151 wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['learndash-topic-access-settings']['nonce'] ) ), 'learndash-topic-access-settings' );
152
153 if ( ! $saving_lesson && ! $saving_topic ) {
154 return;
155 }
156
157 // Is it saving LearnDash lesson or a topic?
158 $post_key = false;
159 foreach( array(
160 'learndash-lesson-display-content-settings',
161 'learndash-topic-display-content-settings'
162 ) AS $key ) {
163 if( !empty($_POST[$key]) ) {
164 $post_key = $key;
165 }
166 }
167
168 if( !$post_key ) {
169 return false;
170 }
171
172 $lesson_use_fvplayer_video = false;
173
174 // We need to save our custom field for Use FV Player
175 if ( isset( $_POST[$post_key]['lesson_use_fvplayer_video'] ) ) {
176 $lesson_use_fvplayer_video = true;
177
178 $my_settings_value = sanitize_key( $_POST[$post_key]['lesson_use_fvplayer_video'] );
179 update_post_meta( $post_id, 'lesson_use_fvplayer_video', $my_settings_value );
180
181 } else {
182 delete_post_meta( $post_id, 'lesson_use_fvplayer_video' );
183 }
184
185 if( !empty($_POST[$post_key]['lesson_video_enabled']) && sanitize_key( $_POST[$post_key]['lesson_video_enabled'] ) == 'on' ) {
186
187 // Adjusting the Video URL field based on "Use FV Player"
188 foreach( array(
189 '_sfwd-lessons' => 'sfwd-lessons',
190 '_sfwd-topic' => 'sfwd-topic'
191 ) AS $meta_key => $prefix ) {
192 $video_url_key = $prefix."_lesson_video_url";
193 $backup_key = '_backup_'.$prefix.'_lesson_video_url';
194
195 // Adjust the Video URL stored by LearnDash
196 $meta = get_post_meta( $post_id, $meta_key, true );
197 if( $meta ) {
198
199 // If we detect [fvplayer] shortcode was used as Video URL we enable FV Player
200 if( stripos($meta[$video_url_key],'[fvplayer ') !== false ) {
201
202 // If the FV Player is not already in
203 // ...or if Use FV Player is not on
204 $objVideos = new FV_Player_Custom_Videos( array('id' => $post_id, 'meta' => 'lesson_fv_player', 'type' => 'post' ) );
205 if( !$objVideos->have_videos() || !$lesson_use_fvplayer_video ) {
206 $lesson_use_fvplayer_video = true;
207 update_post_meta( $post_id, 'lesson_use_fvplayer_video', 'on' );
208 update_post_meta( $post_id, 'lesson_fv_player', $meta[$video_url_key] );
209 }
210 }
211
212 $backup = get_post_meta( $post_id, $backup_key, true );
213 if( $lesson_use_fvplayer_video ) {
214 if( !$backup ) {
215 update_post_meta( $post_id, $backup_key, $meta[$video_url_key] );
216 }
217
218 $lesson_fv_player = '';
219
220 $objVideos = new FV_Player_Custom_Videos( array('id' => $post_id, 'meta' => 'lesson_fv_player', 'type' => 'post' ) );
221 if( $objVideos->have_videos() ) {
222 foreach( $objVideos->get_videos() AS $video ) {
223 $lesson_fv_player .= $video;
224 }
225 }
226
227 // We need to put in that [embed][/embed] shortcode to ensure Learndash detects that as a shortcode to show
228 $meta[$video_url_key] = '[embed][/embed]'.$lesson_fv_player;
229 } else if( $backup ) {
230 $meta[$video_url_key] = $backup;
231
232 delete_post_meta( $post_id, $backup_key );
233 }
234
235 // If Video URL is empty, LearnDash would just turn this off
236 $meta[$prefix."_lesson_video_enabled"] = 'on';
237
238 update_post_meta( $post_id, $meta_key, $meta );
239 }
240 }
241 }
242 }
243
244 /*
245 If we see [fvplayer ...] shortcode in Learndash Video URL we need to persuade it it's the local provider, otherwise it would not parse the shortcode
246 */
247 function set_provider($video_data, $step_settings) {
248 if (strpos($step_settings['lesson_video_url'], '[fvplayer ') !== false) {
249 return 'local';
250 }
251
252 return $video_data;
253 }
254
255 }
256
257 new FV_Player_Learndash_LMS;
258