| 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 |
if ( |
| 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 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
// Is it saving LearnDash lesson or a topic? |
| 153 |
$post_key = false; |
| 154 |
foreach( array( |
| 155 |
'learndash-lesson-display-content-settings', |
| 156 |
'learndash-topic-display-content-settings' |
| 157 |
) AS $key ) { |
| 158 |
if( !empty($_POST[$key]) ) { |
| 159 |
$post_key = $key; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if( !$post_key ) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
$lesson_use_fvplayer_video = false; |
| 168 |
|
| 169 |
// We need to save our custom field for Use FV Player |
| 170 |
if ( isset( $_POST[$post_key]['lesson_use_fvplayer_video'] ) ) { |
| 171 |
$lesson_use_fvplayer_video = true; |
| 172 |
|
| 173 |
$my_settings_value = sanitize_key( $_POST[$post_key]['lesson_use_fvplayer_video'] ); |
| 174 |
update_post_meta( $post_id, 'lesson_use_fvplayer_video', $my_settings_value ); |
| 175 |
|
| 176 |
} else { |
| 177 |
delete_post_meta( $post_id, 'lesson_use_fvplayer_video' ); |
| 178 |
} |
| 179 |
|
| 180 |
if( !empty($_POST[$post_key]['lesson_video_enabled']) && sanitize_key( $_POST[$post_key]['lesson_video_enabled'] ) == 'on' ) { |
| 181 |
|
| 182 |
// Adjusting the Video URL field based on "Use FV Player" |
| 183 |
foreach( array( |
| 184 |
'_sfwd-lessons' => 'sfwd-lessons', |
| 185 |
'_sfwd-topic' => 'sfwd-topic' |
| 186 |
) AS $meta_key => $prefix ) { |
| 187 |
$video_url_key = $prefix."_lesson_video_url"; |
| 188 |
$backup_key = '_backup_'.$prefix.'_lesson_video_url'; |
| 189 |
|
| 190 |
// Adjust the Video URL stored by LearnDash |
| 191 |
$meta = get_post_meta( $post_id, $meta_key, true ); |
| 192 |
if( $meta ) { |
| 193 |
|
| 194 |
// If we detect [fvplayer] shortcode was used as Video URL we enable FV Player |
| 195 |
if( stripos($meta[$video_url_key],'[fvplayer ') !== false ) { |
| 196 |
|
| 197 |
// If the FV Player is not already in |
| 198 |
// ...or if Use FV Player is not on |
| 199 |
$objVideos = new FV_Player_Custom_Videos( array('id' => $post_id, 'meta' => 'lesson_fv_player', 'type' => 'post' ) ); |
| 200 |
if( !$objVideos->have_videos() || !$lesson_use_fvplayer_video ) { |
| 201 |
$lesson_use_fvplayer_video = true; |
| 202 |
update_post_meta( $post_id, 'lesson_use_fvplayer_video', 'on' ); |
| 203 |
update_post_meta( $post_id, 'lesson_fv_player', $meta[$video_url_key] ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
$backup = get_post_meta( $post_id, $backup_key, true ); |
| 208 |
if( $lesson_use_fvplayer_video ) { |
| 209 |
if( !$backup ) { |
| 210 |
update_post_meta( $post_id, $backup_key, $meta[$video_url_key] ); |
| 211 |
} |
| 212 |
|
| 213 |
$lesson_fv_player = ''; |
| 214 |
|
| 215 |
$objVideos = new FV_Player_Custom_Videos( array('id' => $post_id, 'meta' => 'lesson_fv_player', 'type' => 'post' ) ); |
| 216 |
if( $objVideos->have_videos() ) { |
| 217 |
foreach( $objVideos->get_videos() AS $video ) { |
| 218 |
$lesson_fv_player .= $video; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
// We need to put in that [embed][/embed] shortcode to ensure Learndash detects that as a shortcode to show |
| 223 |
$meta[$video_url_key] = '[embed][/embed]'.$lesson_fv_player; |
| 224 |
} else if( $backup ) { |
| 225 |
$meta[$video_url_key] = $backup; |
| 226 |
|
| 227 |
delete_post_meta( $post_id, $backup_key ); |
| 228 |
} |
| 229 |
|
| 230 |
// If Video URL is empty, LearnDash would just turn this off |
| 231 |
$meta[$prefix."_lesson_video_enabled"] = 'on'; |
| 232 |
|
| 233 |
update_post_meta( $post_id, $meta_key, $meta ); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/* |
| 240 |
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 |
| 241 |
*/ |
| 242 |
function set_provider($video_data, $step_settings) { |
| 243 |
if (strpos($step_settings['lesson_video_url'], '[fvplayer ') !== false) { |
| 244 |
return 'local'; |
| 245 |
} |
| 246 |
|
| 247 |
return $video_data; |
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
new FV_Player_Learndash_LMS; |
| 253 |
|