diff --git a/controller/settings.php b/controller/settings.php index 3f0916952..eb6b2f9f3 100644 --- a/controller/settings.php +++ b/controller/settings.php @@ -63,10 +63,10 @@ function fv_wp_flowplayer_after_plugin_row( $arg) { if( apply_filters('fv_player_skip_ads',false) ) { return; } - + $args = func_get_args(); - - if( $args[1]['Name'] == 'FV Player' ) { + + if( $args[1]['Name'] == 'FV Player' ) { $options = get_option( 'fvwpflowplayer' ); if( $options['key'] == 'false' || $options['key'] == '' ) : ?> @@ -143,13 +143,13 @@ function fv_player_settings_save() { $_GET['post_mime_type'] = 'image'; } } - + if( isset($_POST['fv-wp-flowplayer-submit']) ) { check_admin_referer('fv_flowplayer_settings_nonce','fv_flowplayer_settings_nonce'); - + global $fv_fp; if( method_exists($fv_fp,'_set_conf') ) { - if( + if( // pro not installed or !function_exists('FV_Player_Pro') || // pro installed and version is at least 7.5.25.728 @@ -189,9 +189,9 @@ function fv_player_handle_secrets($new, $old) { } } } else if($v == '1') { // single value, 1 - keep original, 0 - use new - $new[$key] = $old[$key]; + $new[$key] = $old[$key]; } - } + } unset($new[$k]); // remove _is_secret_ } @@ -206,12 +206,27 @@ function fv_player_handle_secrets($new, $old) { /* * Pointer boxes */ -add_action('admin_init', 'fv_player_admin_pointer_boxes'); +add_action('admin_footer', 'fv_player_admin_pointer_boxes'); function fv_player_admin_pointer_boxes() { global $fv_fp; global $fv_wp_flowplayer_ver, $fv_wp_flowplayer_core_ver; + if( + $fv_fp->_get_option('video_position_save_enable') && + ! $fv_fp->_get_option('notice_user_video_positions_conversion') + ) { + $fv_fp->pointer_boxes['fv_flowplayer_video_positions_conversion'] = array( + 'id' => '#wp-admin-bar-new-content', + 'pointerClass' => 'fv_flowplayer_video_positions_conversion', + 'heading' => __('FV Player Video Position Conversion', 'fv-wordpress-flowplayer'), + 'content' => __("

In this new version of FV Player the user video positions are stored in separate table. This avoids slowing down the user database (wp_usermeta).

Please run the tool to migrate the video positions to the new table.

"), + 'position' => array( 'edge' => 'top', 'align' => 'left' ), + 'button1' => __('Migrate', 'fv-wordpress-flowplayer'), + 'function1' => 'location.href = "' . admin_url('admin.php?page=fv_player_conversion_positions_meta2table') . '"', + ); + } + if( isset($fv_fp->conf['disable_videochecker']) && $fv_fp->conf['disable_videochecker'] == 'false' && ( !isset($fv_fp->conf['video_checker_agreement']) || $fv_fp->conf['video_checker_agreement'] != 'true' ) @@ -226,7 +241,7 @@ function fv_player_admin_pointer_boxes() { 'button2' => __('Disable the video checker', 'fv-wordpress-flowplayer') ); } - + if( !$fv_fp->_get_option('notice_7_5') ) { $fv_fp->pointer_boxes['fv_flowplayer_notice_7_5'] = array( 'id' => '#wp-admin-bar-new-content', @@ -244,7 +259,7 @@ function fv_player_admin_pointer_boxes() { 'button1' => __('Thanks for letting me know!', 'fv-wordpress-flowplayer') ); } - + if( !$fv_fp->_get_option('notice_new_lightbox') ) { $fv_fp->pointer_boxes['fv_flowplayer_new_lightbox'] = array( 'id' => '#wp-admin-bar-new-content', @@ -255,7 +270,7 @@ function fv_player_admin_pointer_boxes() { 'button1' => __('Thanks for letting me know!', 'fv-wordpress-flowplayer') ); } - + if( !$fv_fp->_get_option('notice_db') ) { $fv_fp->pointer_boxes['fv_flowplayer_db'] = array( 'id' => '#wp-admin-bar-new-content', @@ -266,7 +281,7 @@ function fv_player_admin_pointer_boxes() { 'button1' => __('Thanks for letting me know!', 'fv-wordpress-flowplayer') ); } - + if( $fv_fp->_get_option('video_sitemap') && !$fv_fp->_get_option('disableembedding') && !$fv_fp->_get_option('notice_xml_sitemap_iframes') ) { $fv_fp->pointer_boxes['fv_flowplayer_notice_xml_sitemap_iframes'] = array( 'id' => '#wp-admin-bar-new-content', @@ -278,8 +293,8 @@ function fv_player_admin_pointer_boxes() { 'button2' => __('Go to setting', 'fv-wordpress-flowplayer'), 'function2' => 'location.href = "'.admin_url('options-general.php?page=fvplayer').'#fv_flowplayer_seo"', ); - } - + } + if( !$fv_fp->_get_option('notice_db') && !$fv_fp->_get_option('nag_fv_player_7') ) { $fv_fp->pointer_boxes['fv_flowplayer_fv_player_7'] = array( 'id' => '#wp-admin-bar-new-content', @@ -297,11 +312,11 @@ function fv_player_admin_pointer_boxes() { 'button1' => __('Thanks for letting me know!', 'fv-wordpress-flowplayer'), ); } - - if( - (stripos( $_SERVER['REQUEST_URI'], '/plugins.php') !== false ||fv_player_is_admin_screen() ) - && $pnotices = get_option('fv_wordpress_flowplayer_persistent_notices') - ) { + + if( + (stripos( $_SERVER['REQUEST_URI'], '/plugins.php') !== false ||fv_player_is_admin_screen() ) + && $pnotices = get_option('fv_wordpress_flowplayer_persistent_notices') + ) { $fv_fp->pointer_boxes['fv_flowplayer_license_expired'] = array( 'id' => '#wp-admin-bar-new-content', 'pointerClass' => 'fv_flowplayer_license_expired', @@ -311,9 +326,9 @@ function fv_player_admin_pointer_boxes() { 'position' => array( 'edge' => 'top', 'align' => 'center' ), 'button1' => __('Hide this notice', 'fv-wordpress-flowplayer'), 'button2' => __('I\'ll check this later', 'fv-wordpress-flowplayer') - ); + ); } - + /*if( !$fv_fp->_get_option('disable_video_hash_links') && !$fv_fp->_get_option('notification_video_links') ) { $fv_fp->pointer_boxes['fv_player_notification_video_links'] = array( 'id' => '#wp-admin-bar-new-content', @@ -324,7 +339,7 @@ function fv_player_admin_pointer_boxes() { 'button1' => __('Open Settings', 'fv-wordpress-flowplayer'), 'button2' => __('Dismiss', 'fv-wordpress-flowplayer') ); - + add_action( 'admin_print_footer_scripts', 'fv_player_pointer_scripts' ); }*/ } @@ -335,7 +350,7 @@ function fv_player_admin_pointer_boxes() { add_action( 'wp_ajax_fv_foliopress_ajax_pointers', 'fv_wp_flowplayer_pointers_ajax' ); function fv_wp_flowplayer_pointers_ajax() { - + if( isset($_POST['key']) && $_POST['key'] == 'fv_flowplayer_video_checker_service' && isset($_POST['value']) ) { check_ajax_referer('fv_flowplayer_video_checker_service'); $conf = get_option( 'fvwpflowplayer' ); @@ -350,13 +365,23 @@ function fv_wp_flowplayer_pointers_ajax() { } die(); } - + + if( isset($_POST['key']) && $_POST['key'] == 'fv_flowplayer_video_positions_conversion' && isset($_POST['value']) ) { + check_ajax_referer('fv_flowplayer_video_positions_conversion'); + $conf = get_option( 'fvwpflowplayer' ); + if( $conf ) { + $conf['notice_user_video_positions_conversion'] = $_POST['value']; + update_option( 'fvwpflowplayer', $conf ); + } + die(); + } + if( isset($_POST['key']) && $_POST['key'] == 'fv_flowplayer_license_expired' && isset($_POST['value']) && $_POST['value'] === 'true' ) { check_ajax_referer('fv_flowplayer_license_expired'); delete_option("fv_wordpress_flowplayer_persistent_notices"); die(); - } - + } + $notices = array( 'fv_flowplayer_notice_7_5' => 'notice_7_5', 'fv_flowplayer_new_lightbox' => 'notice_new_lightbox', @@ -365,7 +390,7 @@ function fv_wp_flowplayer_pointers_ajax() { 'fv_flowplayer_fv_player_7' => 'nag_fv_player_7', 'fv_player_notification_video_links' => 'notification_video_links', ); - + if( isset($_POST['key']) && isset($_POST['value']) && in_array($_POST['key'], array_keys($notices) ) ) { check_ajax_referer($_POST['key']); $conf = get_option( 'fvwpflowplayer' ); @@ -375,7 +400,7 @@ function fv_wp_flowplayer_pointers_ajax() { } die(); } - + } @@ -390,7 +415,7 @@ function fv_player_pointer_scripts() { window.location = '#playlist_advance'; }); }); - })(jQuery); + })(jQuery); + ?> set_live ; } -} \ No newline at end of file +} diff --git a/models/conversion/positionsMeta2Table.php b/models/conversion/positionsMeta2Table.php new file mode 100644 index 000000000..1b651c526 --- /dev/null +++ b/models/conversion/positionsMeta2Table.php @@ -0,0 +1,384 @@ + 'FV Player PositionsMeta2Table Conversion', + 'slug' => 'positions_meta2table', + 'matchers' => array( + + ), + 'help' => __("This converts position values from usermeta to fv_player_user_video_positions table", 'fv-wordpress-flowplayer') + ) ); + + $this->conversion_limit = 2500; + $this->make_chages_button = false; // disable make changes button + + $this->start_warning_text = __('This will convert positions from usermeta to new tables. Please make sure you have a backup of your database before continuing.', 'fv-wordpress-flowplayer'); + + $this->conversion_done_details = __('The conversion has finished. The usermeta table will be purged of the FV Player video position data in 4 weeks.', 'fv-wordpress-flowplayer'); + + $this->screen_fields = array( + 'User ID', + 'Video ID', + 'Result', + 'Error', + ); + + add_action('admin_init', array($this, 'set_pointer_checked')); + } + + /** + * Override parent method + * + * @return int + */ + function get_count() { + global $wpdb; + return (int) $wpdb->get_var( "SELECT COUNT(*) AS count FROM `$wpdb->usermeta` WHERE meta_key LIKE 'fv_wp_flowplayer_%'" ); + } + + function get_items( $offset, $limit ) { + global $wpdb; + + // select umeta_id to prevent using filesort + $ids = $wpdb->get_results( "SELECT umeta_id FROM `$wpdb->usermeta` WHERE meta_key LIKE 'fv_wp_flowplayer_%' ORDER BY umeta_id ASC LIMIT {$offset},{$limit}" ); + + // select all meta data fields by ids + $meta_data = $wpdb->get_results( "SELECT * FROM `$wpdb->usermeta` WHERE umeta_id IN (". implode(',', wp_list_pluck($ids, 'umeta_id') ) .") ORDER BY umeta_id ASC" ); + + return $meta_data; + } + + function convert_one($meta) { + $output_data = array(); // output for html + $errors = array(); // all errors for export + + // get meta data + $meta_key = $meta->meta_key; + $meta_value = $meta->meta_value; + $user_id = $meta->user_id; + + $type = ''; + + if( strpos($meta_key, 'fv_wp_flowplayer_position') !== false ) { // last + $type = 'last_position'; + } else if ( strpos($meta_key, 'fv_wp_flowplayer_saw') !== false ) { // finished + $type = 'finished'; + } else if ( strpos($meta_key, 'fv_wp_flowplayer_top_position') !== false ) { // top + $type = 'top_position'; + } else if ( strpos($meta_key, 'fv_wp_flowplayer_player_playlist') !== false ) { // playlist + $type = 'playlist'; + } + + if( $type == 'playlist' ) { + preg_match('/fv_wp_flowplayer_player_playlist_(\d+)/', $meta_key, $matches); + if( isset( $matches[1] ) ) { + $playlist_id = $matches[1]; + + // check if its db video or external + $row_exitst = $this->position_row_exists( $user_id, $playlist_id, 'playlist' ); + + $result = $this->insert_update_playlist_row( $user_id, $playlist_id, $meta_value, $row_exitst ); + + if( $result ) { + $output_data[] = array( + 'ID' => $user_id, + 'Name' => $meta_key, + 'output' => $row_exitst ? 'Playlist position updated' : 'Playlist position inserted', + 'error' => '' + ); + } else { // failed to update + $output_data[] = array( + 'ID' => $user_id, + 'Name' => $meta_key, + 'output' => 'Playlist position failed to update', + 'error' => 'Failed to update playlist position' + ); + + $errors[] = array( + 'ID' => $user_id, + 'Name' => $meta_key + ); + } + + } else { + // failed to get playlist id + $output_data[] = array( + 'ID' => $user_id, + 'Name' => $meta_key, + 'output' => 'Playlist position failed to update', + 'error' => 'Cannot get playlist id' + ); + + $errors[] = array( + 'ID' => $user_id, + 'Name' => $meta_key + ); + } + + } else { + preg_match('/fv_wp_flowplayer_\w+_(.*)/', $meta_key, $matches); + if( isset( $matches[1] ) ) { + $video_id = $matches[1]; + + // check if video exitst in db + $video_exists = $this->video_exists( $video_id ); + $row_exitst = $this->position_row_exists( $user_id, $video_id, 'position', $video_exists ); + + $result = $this->insert_update_video_row( $user_id, $video_id, $type, $meta_value, $row_exitst, $video_exists ); + + if( $result ) { + $output_data[] = array( + 'ID' => $user_id, + 'Name' => $meta_key, + 'output' => $row_exitst ? 'Video position updated' : 'Video position inserted', + 'error' => '' + ); + } else { + $output_data[] = array( + 'ID' => $user_id, + 'Name' => $meta_key, + 'output' => 'Video position failed to update', + 'error' => 'Failed to update position' + ); + + $errors[] = array( + 'ID' => $user_id, + 'Name' => $meta_key + ); + } + + } else { + $output_data[] = array( + 'ID' => $user_id, + 'Name' => $meta_key, + 'output' => 'Video position failed to update', + 'error' => 'Cannot get video id' + ); + + // failed to get video id + $errors[] = array( + 'ID' => $user_id, + 'Name' => $meta_key + ); + } + + } + + return array( + 'output_data' => $output_data, + 'errors' => $errors + ); + + } + + /** + * Insert or update video row + * + * @param int $user_id + * @param int $video_id + * @param string $type + * @param int $value + * @param boolean $row_exitst + * @param boolean $video_exists + * + * @return boolean $res result of insert or update + */ + function insert_update_video_row( $user_id, $video_id, $type, $value, $row_exitst, $video_exists ) { + global $wpdb; + + if( !$video_exists ) { // non db video + $legacy_id = $video_id; + $video_id = 0; + } else { // db video + $legacy_id = ''; + $video_id = intval($video_id); + } + + if( $row_exitst ) { + $res = $wpdb->update( + $wpdb->prefix . 'fv_player_user_video_positions', + array( + $type => $value + ), + array( + 'user_id' => $user_id, + 'video_id' => $video_id, + 'legacy_video_id' => $legacy_id + ) + ); + + $res = is_numeric($res); + } else { + $res = $wpdb->insert( + $wpdb->prefix . 'fv_player_user_video_positions', + array( + $type => $value, + 'user_id' => $user_id, + 'video_id' => $video_id, + 'legacy_video_id' => $legacy_id + ) + ); + + $res = !empty($res); + } + + return $res; + } + + /** + * Insert or update playlist row + * + * @param int $user_id + * @param int $playlist_id + * @param int $value + * @param boolean $exitst + * + * @return boolean $res result of insert or update + */ + function insert_update_playlist_row( $user_id, $playlist_id, $value, $exitst ) { + global $wpdb; + + if( $exitst ) { + $res = $wpdb->update( + $wpdb->prefix . 'fv_player_user_playlist_positions', + array( + 'item_index' => $value + ), + array( + 'user_id' => $user_id, + 'player_id' => $playlist_id + ) + ); + + $res = is_numeric($res); + } else { + $res = $wpdb->insert( + $wpdb->prefix . 'fv_player_user_playlist_positions', + array( + 'user_id' => $user_id, + 'player_id' => $playlist_id, + 'item_index' => $value + ) + ); + + $res = !empty($res); + } + + return $res; + } + + /** + * Check if video exitst in db + * + * @param int $video_id + * + * @return object|null + */ + function video_exists( $video_id ) { + global $wpdb; + + $row = $wpdb->get_row( "SELECT * FROM `{$wpdb->prefix}fv_player_videos` WHERE id = {$video_id}" ); + + return $row; + } + + /** + * Check if position or playlist row exitst + * + * @param int $user_id + * @param int|string $id + * @param string $type + * @param boolean $video_exists + * + * @return object|null + */ + function position_row_exists( $user_id, $id, $type, $video_exists = false ) { + global $wpdb; + + if( $type == 'position' ) { + + if( $video_exists ) { // db video + $row = $wpdb->get_row( "SELECT * FROM `{$wpdb->prefix}fv_player_user_video_positions` WHERE user_id = {$user_id} AND video_id = {$id}" ); + } else { // legacy video + $row = $wpdb->get_row( "SELECT * FROM `{$wpdb->prefix}fv_player_user_video_positions` WHERE user_id = {$user_id} AND legacy_video_id = {$id}" ); + } + + } + + if( $type == 'playlist' ) { + $row = $wpdb->get_row( "SELECT * FROM `{$wpdb->prefix}fv_player_user_playlist_positions` WHERE user_id = {$user_id} AND player_id = {$id}" ); + } + + return $row; + } + + function conversion_button() { + ?> + + + +

+ +

+ + + convert_one( $meta ); + + if( !empty($result['errors']) ) { + $convert_error = true; + } + + $conversions_output = array_merge( $conversions_output, $result['output_data'] ); + } + + return array( + 'convert_error' => $convert_error, + 'conversions_output' => $conversions_output + ); + } + + function build_output_html( $data, $percent_done ) { + $html = array(); + + foreach( $data as $output_data ) { + if(!empty( $output_data['error']) ) { // show only errors + $html[] = "". $output_data['ID'] . "". $output_data['Name'] ."". $output_data['output'] ."". $output_data['error'] .""; + } + } + + if( empty($html) && $percent_done == 0 ) { + $html[] = "No matching meta found."; + } + + return $html; + } + + function set_pointer_checked() { + if( isset($_GET['page'] ) && $_GET['page'] == $this->screen ) { + $conf = get_option( 'fvwpflowplayer' ); + if( !isset($conf['notice_user_video_positions_conversion']) ) { + $conf['notice_user_video_positions_conversion'] = true; + update_option( 'fvwpflowplayer', $conf ); + } + } + } + +} + +global $FV_Player_Positions_Meta2Table_Conversion; +$FV_Player_Positions_Meta2Table_Conversion = new FV_Player_Positions_Meta2Table_Conversion; diff --git a/models/conversion/shortcode2DB.class.php b/models/conversion/shortcode2DB.class.php index 84d09d5d4..64a21d92e 100644 --- a/models/conversion/shortcode2DB.class.php +++ b/models/conversion/shortcode2DB.class.php @@ -24,6 +24,10 @@ function __construct() { 'help' => __("This converts the [fvplayer src=...] and [flowplayer src=...] shortcodes into database [fvplayer id=...] shortcodes.", 'fv-wordpress-flowplayer') ) ); + $this->conversion_limit = 1; + + $this->start_warning_text = __('Please make sure you backup your database before continuing. You can use post revisions to get back to previous version of your posts as well.', 'fv-wordpress-flowplayer'); + // atts for video that are supported $this->supported_video_atts = array( 'src', @@ -50,16 +54,19 @@ function __construct() { ); $this->supported_atts = array_merge( $this->supported_video_atts, $this->supported_player_atts ); - } - /** - * Count posts with old shortcode - * - * @return int $count - */ - function get_count() { - global $wpdb; - return $wpdb->get_var( "SELECT FOUND_ROWS()" ); + $this->screen_fields = array( + 'ID', + 'Title', + 'Post Type', + 'Shortcode', + 'Result', + 'Error' + ); + + if( isset($_GET['fv-conversion-export']) && !empty($_GET['page']) && $_GET['page'] === $this->screen ) { + add_action('admin_init', array( $this, 'csv_export' ) ); + } } /** @@ -67,7 +74,7 @@ function get_count() { * * @return object|null $result */ - function get_posts_with_shortcode($offset, $limit) { + function get_items($offset, $limit) { global $wpdb; // Each row is the matching wp_posts row or wp_posts row with matching meta_value @@ -226,7 +233,7 @@ function worker( $shortcode, $post, $new_content, $meta_key = false ) { $video_index = 0; $import_video_atts[$video_index] = array(); // add video atts - foreach( $this->supported_video_atts as $video_att ) { + foreach( $this->supported_video_atts as $video_att ) { if( isset( $import_atts[$video_att] ) ) { $import_video_atts[$video_index][$video_att] = $import_atts[$video_att]; } @@ -334,7 +341,7 @@ function worker( $shortcode, $post, $new_content, $meta_key = false ) { } else { $import_player_atts[$player_att] = $import_atts[$player_att]; } - + } else if(strcmp( $player_att, 'speed' ) == 0) { // subtitles if( $import_atts[$player_att] == 'buttons' ) { $import_player_atts[$player_att] = 'yes'; @@ -349,7 +356,7 @@ function worker( $shortcode, $post, $new_content, $meta_key = false ) { } // add metadata - $import_player_atts['meta'] = array( + $import_player_atts['meta'] = array( array( 'meta_key' => 'post_id', 'meta_value' => $post->ID @@ -406,7 +413,7 @@ function worker( $shortcode, $post, $new_content, $meta_key = false ) { $output_msg = "Would create new FV Player"; } } - + $type = $post->post_type; if( $meta_key ) { $type .= '
meta_key: '.$meta_key.''; @@ -427,7 +434,92 @@ function worker( $shortcode, $post, $new_content, $meta_key = false ) { ); } + function build_output_html($data, $percent_done) { + $html = array(); + + foreach( $data as $output_data ) { + $html[] = " #". $output_data['ID'] . "". $output_data['title'] . "" . $output_data['type'] . "". $output_data['shortcode'] . "" . $output_data['output'] . "" . $output_data['error'] . ""; + } + + if( empty($html) && $percent_done == 0 ) { + $html[] = "No matching players found."; + } + + return $html; + } + + function iterate_data($data) { + $conversions_output = array(); + $convert_error = false; + + foreach( $data AS $post ) { + $result = $this->convert_one($post); + // mark post if conversion failed + if( !empty( $result['errors'] ) ) { + update_post_meta( $post->ID, '_fv_player_' . $this->slug . '_failed', $result['errors'] ); + $convert_error = true; + } else { + if( $result['content_updated'] ) { + // no problem, unmark + delete_post_meta( $post->ID, '_fv_player_' . $this->slug . '_failed' ); + } + } + + $conversions_output = array_merge( $conversions_output, $result['output_data'] ); + + if( $this->is_live() && $result['content_updated'] ) { + wp_update_post( array( 'ID' => $post->ID, 'post_content' => $result['new_content'] ) ); + } + } + + return array( + 'convert_error' => $convert_error, + 'conversions_output' => $conversions_output + ); + } + + function csv_export() { + if( !current_user_can('install_plugins') ) return; + + global $wpdb; + + $filename = $this->slug . '-export-' . date('Y-m-d') . '.csv'; + + header('Content-type: text/csv'); + header("Content-Disposition: attachment; filename=$filename"); + header("Pragma: no-cache"); + header("Expires: 0"); + + $meta_key = '_fv_player_' . $this->slug . '_failed'; + + $sql = $wpdb->prepare( "SELECT {$wpdb->postmeta}.meta_value FROM {$wpdb->postmeta} JOIN {$wpdb->posts} ON {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID WHERE {$wpdb->postmeta}.meta_key = '%s' ORDER BY {$wpdb->posts}.post_date_gmt DESC ", $meta_key ); + + $results = $wpdb->get_col( $sql ); + + if( !empty( $results ) ) { + $fp = fopen('php://output', 'wb'); + + $header = array('ID','Title','Post-Link','Edit-Link','Shortcode','Message'); + + fputcsv($fp, $header); + + foreach( $results as $result ) { + $unserialized = unserialize( $result ); + + foreach( $unserialized as $row ) { + $row['post_link'] = htmlspecialchars_decode( $row['post_link'] ); + $row['post_edit'] = htmlspecialchars_decode( $row['post_edit'] ); + fputcsv($fp, $row); + } + } + + fclose($fp); + } + + die(); + } + } global $FV_Player_Shortcode2Database_Conversion; -$FV_Player_Shortcode2Database_Conversion = new FV_Player_Shortcode2Database_Conversion; \ No newline at end of file +$FV_Player_Shortcode2Database_Conversion = new FV_Player_Shortcode2Database_Conversion; diff --git a/models/player-position-save.php b/models/player-position-save.php index 69a894439..2ad3b531b 100644 --- a/models/player-position-save.php +++ b/models/player-position-save.php @@ -1,6 +1,14 @@ get_charset_collate() . ";"; // create table to store position in playlists @@ -49,6 +58,54 @@ function plugin_update_database() { dbDelta( $sql_playlist_positions ); } + /** + * Set cache to static variable + * + * @param int $user_id + * @param string $type video, playlist + * + * @return array + */ + private function set_cache($user_id, $type) { + global $wpdb; + + if( !isset(self::$cache[$user_id]) ) { + self::$cache[$user_id] = array(); + } + + if( $type == 'video') { + self::$cache[$user_id][$type] = $wpdb->get_results( $wpdb->prepare( + "SELECT video_id, last_position, top_position, finished, legacy_video_id FROM `{$wpdb->prefix}fv_player_user_video_positions` WHERE user_id = %d", + $user_id + ) ); + } + + if( $type == 'playlist' ) { + self::$cache[$user_id][$type] = $wpdb->get_results( $wpdb->prepare( + "SELECT player_id, item_index FROM `{$wpdb->prefix}fv_player_user_playlist_positions` WHERE user_id = %d", + $user_id + ) ); + } + + return self::$cache[$user_id][$type]; + } + + /** + * Get cache from static variable + * + * @param int $user_id + * @param string $type video, playlist + * + * @return array + */ + private function get_cache($user_id, $type) { + if ( ! isset( self::$cache[$user_id][$type] ) ) { + $this->set_cache($user_id, $type); + } + + return self::$cache[$user_id][$type]; + } + /** * Get video position * @@ -59,21 +116,29 @@ function plugin_update_database() { * @return int */ function get_video_position( $user_id, $video_id, $type ) { - global $wpdb; + $cache = self::get_cache($user_id, 'video'); + $value = 0; - if( is_numeric( $video_id) ) { // id + if( is_numeric($video_id) ) { // id $video_id = intval($video_id); - $value = $wpdb->get_var( $wpdb->prepare( - "SELECT $type FROM ".$wpdb->prefix."fv_player_user_video_positions WHERE user_id = %d AND video_id = %d", - $user_id, - $video_id, - ) ); - } else { // legacy - $value = $wpdb->get_var( $wpdb->prepare( - "SELECT $type FROM ".$wpdb->prefix."fv_player_user_video_positions WHERE user_id = %d AND legacy_video_id = %s and video_id = 0", - $user_id, - $video_id, - ) ); + + if(is_array($cache)) { + foreach ($cache as $cache_item) { + if ($cache_item->video_id == $video_id) { + $value = $cache_item->$type; + break; + } + } + } + } else { // legacy_video_id + if( is_array($cache)) { + foreach ($cache as $cache_item) { + if ($cache_item->legacy_video_id == $video_id) { + $value = $cache_item->$type; + break; + } + } + } } if( is_numeric($value) ) { @@ -124,6 +189,9 @@ function delete_video_postion( $user_id, $video_id, $type ) { '%s', ) ); + + // update cache + self::set_cache($user_id, 'video'); } /** @@ -137,11 +205,18 @@ function delete_video_postion( $user_id, $video_id, $type ) { function get_player_position( $user_id, $player_id ) { global $wpdb; - $index = $wpdb->get_var( $wpdb->prepare( - "SELECT item_index FROM ".$wpdb->prefix."fv_player_user_playlist_positions WHERE user_id = %d AND player_id = %d", - $user_id, - $player_id - ) ); + $index = 0; + + $cache = self::get_cache($user_id, 'playlist'); + + if( is_array($cache) ) { + foreach ($cache as $cache_item) { + if ($cache_item->player_id == $player_id) { + $index = $cache_item->item_index; + break; + } + } + } if( is_numeric($index) ) { $index = intval($index); @@ -165,21 +240,30 @@ function get_player_position( $user_id, $player_id ) { function set_video_position( $user_id, $video_id, $type, $value) { global $wpdb; + $cache = self::get_cache($user_id, 'video'); + $exits = false; + if( is_numeric($video_id) ) { // id $video_id = intval($video_id); - $exits = $wpdb->get_var( $wpdb->prepare( - "SELECT id FROM ".$wpdb->prefix."fv_player_user_video_positions WHERE user_id = %d AND video_id = %d AND legacy_video_id = %s", - $user_id, - $video_id, - '' - ) ); - } else { // legacy - $exits = $wpdb->get_var( $wpdb->prepare( - "SELECT id FROM ".$wpdb->prefix."fv_player_user_video_positions WHERE user_id = %d AND video_id = %d AND legacy_video_id = %s", - $user_id, - 0, - $video_id - ) ); + + if( is_array($cache) ) { + foreach ($cache as $cache_item) { + if ($cache_item->video_id == $video_id) { + $exits = true; + break; + } + } + } + } else { // legacy_video_id + if( is_array($cache) ) { + foreach ($cache as $cache_item) { + if ($cache_item->legacy_video_id == $video_id) { + $exits = true; + break; + } + } + } + } // video id and legacy @@ -213,6 +297,9 @@ function set_video_position( $user_id, $video_id, $type, $value) { ) ); } + + // update cache + self::set_cache($user_id, 'video'); } /** @@ -227,12 +314,18 @@ function set_video_position( $user_id, $video_id, $type, $value) { function set_player_position( $user_id, $player_id, $index ) { global $wpdb; - // check if the record already exists - $exits = $wpdb->get_var( $wpdb->prepare( - "SELECT id FROM ".$wpdb->prefix."fv_player_user_playlist_positions WHERE user_id = %d AND player_id = %d", - $user_id, - $player_id - ) ); + $cache = self::get_cache($user_id, 'playlist'); + $exits = false; + + if( is_array($cache) ) { + // check if the record already exists + foreach ($cache as $cache_item) { + if ($cache_item->player_id == $player_id) { + $exits = true; + break; + } + } + } if( $exits ) { // update index $wpdb->update( @@ -255,6 +348,9 @@ function set_player_position( $user_id, $player_id, $index ) { ) ); } + + // update cache + self::set_cache($user_id, 'playlist'); } public static function get_extensionless_file_name($path) { diff --git a/models/related-videos.php b/models/related-videos.php new file mode 100644 index 000000000..ab7d544a3 --- /dev/null +++ b/models/related-videos.php @@ -0,0 +1,98 @@ + $this->max_related_posts, // maximum number of related entries to return + 'order' => 'score DESC', // column on "wp_posts" to order by, then a space, and whether to order in ascending ("ASC") or descending ("DESC") order + 'promote_yarpp' => false, // boolean indicating whether to add 'Powered by YARPP' below related posts + 'post_type' => array('post', 'page'), // post types to include in results + )); + + if( !is_array($related_posts ) ) { + return $attributes; + } + + global $wpdb; + + // get all player meta + $player_meta_all = $wpdb->get_results("SELECT * FROM `{$wpdb->prefix}fv_player_playermeta` WHERE meta_key = 'post_id'"); + + // html for related videos + $related_videos_html = ''; + + // keep track of related videos count + $current_related_videos = 0; + + // loop through related posts + foreach($related_posts as $related_post) { + $post_id = $related_post->ID; + + // loop through player meta + foreach($player_meta_all as $player_meta) { + + // if player meta is for this post + if( $player_meta->meta_value == $post_id ) { + $id_player = $player_meta->id_player; + + // get player data + $player_data = $wpdb->get_row("SELECT p.id, videos, v.id, src, splash FROM `{$wpdb->prefix}fv_player_players` AS p JOIN wp_fv_player_videos AS v ON find_in_set( p.videos, v.id ) WHERE p.id = $id_player"); + + if( !$player_data ) { + continue; + } + + $splash_img = ''; + + // check if there is splash + if( $player_data->splash ) { + $splash_img = $player_data->splash; + } else { + $splash_img = get_the_post_thumbnail($post_id, 'thumbnail'); + } + + $related_videos_html .= ''; + + $current_related_videos++; + + if( $current_related_videos >= $this->max_related_videos ) { + break 2; + } + + } + } + + } + + if( $related_videos_html ) { + $popup = '

'.__('Related videos','fv-player-ppv') .'

' . $related_videos_html . '
'; + + $attributes['data-related-videos'] = $player->json_encode( array( 'html' => $popup ) ); + } + + return $attributes; + } + +} + + +global $FV_Player_Related_Videos; +$FV_Player_Related_Videos = new FV_Player_Related_Videos; + +endif;