From 5987053c5f03cd0cd47c029848b856d5f39bb6b2 Mon Sep 17 00:00:00 2001 From: "DESKTOP-QEP0TND\\Javmah" Date: Wed, 7 Dec 2022 12:01:24 +0600 Subject: [PATCH] Fix thumbnail not being updated issue #253 Thumbnail is not changing from the WordPress media # issue 253 --- admin/rt-transcoder-admin.php | 434 ++++++++++++++++------------------ 1 file changed, 204 insertions(+), 230 deletions(-) diff --git a/admin/rt-transcoder-admin.php b/admin/rt-transcoder-admin.php index b216a39..e3fd99d 100755 --- a/admin/rt-transcoder-admin.php +++ b/admin/rt-transcoder-admin.php @@ -1,9 +1,8 @@ api_key = get_site_option( 'rt-transcoding-api-key' ); $this->stored_api_key = get_site_option( 'rt-transcoding-api-key-stored' ); - + // Plugin translation files $this->load_translation(); if ( ! class_exists( 'RT_Progress' ) ) { include_once RT_TRANSCODER_PATH . 'admin/rt-transcoder-progressbar.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant } - + // Including handler file include_once RT_TRANSCODER_PATH . 'admin/rt-transcoder-handler.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant include_once RT_TRANSCODER_PATH . 'admin/rt-transcoder-actions.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant - + // Enqueueing the scripts add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts_styles' ) ); - - add_filter( 'attachment_fields_to_edit', array( $this, 'edit_video_thumbnail' ), 11, 2 ); + // Below Hooks Will Display thumbnails in the attachment video edit page bottom + add_filter( 'attachment_fields_to_edit', array( $this, 'show_video_thumbnail_in_attachment_edit_page' ), 11, 2 ); + // Hook for Processing the data that send by show_video_thumbnail_in_attachment_edit_page() function add_filter( 'attachment_fields_to_save', array( $this, 'save_video_thumbnail' ), 11, 1 ); + // Admin Notice HOOK add_action( 'admin_notices', array( $this, 'add_settings_errors' ) ); - + // This Plugin HTTP request and Repose handler $this->transcoder_handler = new RT_Transcoder_Handler(); - if ( is_admin() ) { - add_action( 'admin_menu', array( $this, 'menu' ) ); + if(is_admin()){ + // Admin menu Hook for creating admin page || + add_action('admin_menu', array( $this, 'menu' )); + // Admin init HOOK add_action( 'admin_init', array( $this, 'register_transcoder_settings' ) ); if ( class_exists( 'RTMediaEncoding' ) ) { $old_rtmedia_encoding_key = get_site_option( 'rtmedia-encoding-api-key' ); @@ -89,18 +86,21 @@ public function __construct() { if ( is_multisite() ) { add_action( 'network_admin_notices', array( $this, 'subscribe_transcoder_admin_notice' ) ); } + // Adding admin notice hook add_action( 'admin_notices', array( $this, 'subscribe_transcoder_admin_notice' ) ); + // Adding footer function for JS uncheck all checkboxes except one clicked + add_filter( 'admin_footer', array( $this,'admin_footer_for_attachment_edit_page')); } if ( class_exists( 'RTMedia' ) ) { - if ( ! function_exists( 'get_plugin_data' ) ) { + if(! function_exists( 'get_plugin_data' )){ include_once ABSPATH . 'wp-admin/includes/plugin.php'; } $rtmedia_plugin_info = get_plugin_data( RTMEDIA_PATH . 'index.php' ); // Show admin notice when Transcoder pluign active and user using rtMedia version 4.0.7. - if ( version_compare( $rtmedia_plugin_info['Version'], '4.0.7', '<=' ) ) { - if ( is_multisite() ) { + if(version_compare( $rtmedia_plugin_info['Version'], '4.0.7', '<=' )){ + if(is_multisite()){ add_action( 'network_admin_notices', array( $this, 'transcoder_admin_notice' ) ); } add_action( 'admin_notices', array( $this, 'transcoder_admin_notice' ) ); @@ -120,17 +120,15 @@ public function add_settings_errors() { } /** - * Create menu. - * + * Create admin menu for this Plugin. *** * @since 1.0.0 */ public function menu() { - add_menu_page( 'Transcoder', 'Transcoder', 'manage_options', 'rt-transcoder', array( $this, 'settings_page' ), RT_TRANSCODER_URL . 'admin/images/menu-icon.png', '40.2222' ); + add_menu_page( 'Transcoder', 'Transcoder', 'manage_options', 'rt-transcoder', array($this, 'settings_page'), RT_TRANSCODER_URL . 'admin/images/menu-icon.png', '40.2222' ); } /** * Register transcoder settings. - * * @since 1.0.0 */ public function register_transcoder_settings() { @@ -141,7 +139,6 @@ public function register_transcoder_settings() { /** * Display settings page. - * * @since 1.0.0 */ public function settings_page() { @@ -150,7 +147,6 @@ public function settings_page() { /** * Load language translation. - * * @since 1.0.0 */ public function load_translation() { @@ -159,7 +155,6 @@ public function load_translation() { /** * Remove actions and filters from old rtMedia (v4.0.2) plugin. - * * @since 1.0.0 */ public function disable_encoding() { @@ -169,6 +164,7 @@ public function disable_encoding() { remove_filter( 'media_row_actions', array( $rtmedia_admin, 'add_reencode_link' ) ); remove_action( 'admin_head-upload.php', array( $rtmedia_admin, 'add_bulk_actions_regenerate' ) ); } + if ( isset( $rtmedia_admin->rtmedia_encoding ) ) { $rtmedia_encoding = $rtmedia_admin->rtmedia_encoding; remove_action( 'rtmedia_after_add_media', array( $rtmedia_encoding, 'encoding' ) ); @@ -179,7 +175,6 @@ public function disable_encoding() { /** * Load styles and scripts - * * @since 1.0.0 */ public function enqueue_scripts_styles() { @@ -187,7 +182,7 @@ public function enqueue_scripts_styles() { $page = transcoder_filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING ); - if ( 'admin.php' !== $pagenow || 'rt-transcoder' !== $page ) { + if ( 'admin.php' !== $pagenow OR 'rt-transcoder' !== $page ) { return; } $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; @@ -205,203 +200,164 @@ public function enqueue_scripts_styles() { ); wp_localize_script( 'rt-transcoder-main', 'rt_transcoder_script', $localize_script_data ); - wp_enqueue_script( 'rt-transcoder-main' ); } /** * Create subscription form for various subscription plans. - * - * @since 1.0.0 - * - * @param string $name The name of subscription plan. - * @param float $price The price of subscription plan. - * @param bool $force If true then it always show subscriobe form. - * @return string + * @since 1.0.0 + * @param string $name The name of subscription plan. + * @param float $price The price of subscription plan. + * @param bool $force If true then it always show subscriobe form. + * @return string */ public function transcoding_subscription_button( $name = 'No Name', $price = '0', $force = false ) { - if ( $this->api_key ) { + if($this->api_key){ $this->transcoder_handler->update_usage( $this->api_key ); } - + // Getting uses data from site option $usage_details = get_site_option( 'rt-transcoding-usage' ); - - if ( isset( $usage_details[ $this->api_key ]->plan->name ) && ( strtolower( $usage_details[ $this->api_key ]->plan->name ) === strtolower( $name ) ) && $usage_details[ $this->api_key ]->sub_status && ! $force ) { - $form = ''; + // + if (isset($usage_details[ $this->api_key ]->plan->name ) && (strtolower( $usage_details[ $this->api_key ]->plan->name ) === strtolower( $name )) && $usage_details[ $this->api_key ]->sub_status && ! $force ) { + $form = ''; } else { $plan_name = 'free' === $name ? 'Try Now' : 'Subscribe'; - $form = ' - ' . esc_html( $plan_name ) . ' - '; + $form = '' . esc_html( $plan_name ) . ''; } - return $form; } /** * Display all video thumbnails on attachment edit page. - * * @since 1.0.0 - * - * @param array $form_fields An array of attachment form fields. - * @param WP_Post $post The WP_Post attachment object. - * @return array $form_fields + * @param array $form_fields An array of attachment form fields. + * @param WP_Post $post The WP_Post attachment object. + * @return array $form_fields */ - public function edit_video_thumbnail( $form_fields, $post ) { - - if ( isset( $post->post_mime_type ) ) { - $media_type = explode( '/', $post->post_mime_type ); - if ( is_array( $media_type ) && 'video' === $media_type[0] ) { - $media_id = $post->ID; - $thumbnail_array = get_post_meta( $media_id, '_rt_media_thumbnails', true ); - - if ( empty( $thumbnail_array ) ) { - $thumbnail_array = get_post_meta( $media_id, 'rtmedia_media_thumbnails', true ); - } - - $wp_video_thumbnail = get_post_meta( $media_id, '_rt_media_video_thumbnail', true ); - - $video_thumb_html = ''; - if ( is_array( $thumbnail_array ) ) { - $video_thumb_html .= ''; - $form_fields['rtmedia_video_thumbnail'] = array( - 'label' => 'Video Thumbnails', - 'input' => 'html', - 'html' => $video_thumb_html, - ); - } - } + public function show_video_thumbnail_in_attachment_edit_page($form_fields, $post) { + // If post mime type is not video, this feature is only for videos. + if(! isset(explode( '/', $post->post_mime_type)[0]) OR explode('/', $post->post_mime_type)[0] !== 'video'){ + return $form_fields; + } + // Getting attachment thumbnail list that was created by our Plugin. + $thumbnailArray = get_post_meta($post->ID, '_rt_media_thumbnails', true); + // Getting option. + $thumbnailArray = empty($thumbnailArray) ? get_post_meta($post->ID, 'rtmedia_media_thumbnails', true) : $thumbnailArray; + // Saved or selected thumbnail is that was saved in attachment option. + $preSelectedThumbnail = get_post_meta($post->ID, '_rt_media_video_thumbnail', true); + // checking thumbnail Array is array and not empty + if(! is_array($thumbnailArray) OR empty($thumbnailArray)){ + return $form_fields; } - return $form_fields; - } - - /** - * Display all video thumbnails on attachment edit page. - * - * @since 1.0.0 - * - * @param array $form_fields An array of attachment form fields. - * @param WP_Post $post The WP_Post attachment object. - * @return array $form_fields - */ - public function edit_video_thumbnail_( $form_fields, $post ) { - if ( isset( $post->post_mime_type ) ) { - $media_type = explode( '/', $post->post_mime_type ); - if ( is_array( $media_type ) && 'video' === $media_type[0] ) { - $media_id = $post->ID; - $thumbnail_array = get_post_meta( $media_id, '_rt_media_thumbnails', true ); - - if ( empty( $thumbnail_array ) ) { - $thumbnail_array = get_post_meta( $media_id, 'rtmedia_media_thumbnails', true ); - } - - $rtmedia_model = new RTMediaModel(); - $rtmedia_media = $rtmedia_model->get( array( 'media_id' => $media_id ) ); - $video_thumb_html = ''; - if ( is_array( $thumbnail_array ) ) { - - /* for WordPress backward compatibility */ - if ( function_exists( 'wp_get_upload_dir' ) ) { - $uploads = wp_get_upload_dir(); - } else { - $uploads = wp_upload_dir(); - } - $base_url = $uploads['baseurl']; - - $video_thumb_html .= ''; - $form_fields['rtmedia_video_thumbnail'] = array( - 'label' => 'Video Thumbnails', - 'input' => 'html', - 'html' => $video_thumb_html, - ); - } - } + // Backward compatibility. + $uploads = function_exists('wp_get_upload_dir') ? wp_get_upload_dir() : wp_upload_dir(); + // HTML buffer holder. + $htmlString = ""; + // Creating HTML output buffering, starts. + $htmlString .= "'; + // Custom HTML output. + $form_fields['rtmedia_video_thumbnail'] = array( + 'label' => 'Video Thumbnails', + 'input' => 'html', + 'html' => $htmlString, + ); + // Return parameter array value return $form_fields; } /** - * Save selected video thumbnail in attachment meta. - * Selected thumbnail use as cover art for buddypress activity if video was uploaded in activity. + * this is a process function of show_video_thumbnail_in_attachment_edit_page() function. + * This function will process show_video_thumbnail_in_attachment_edit_page() selections and create thumbnail if necessary. + * This Function also Save selected video thumbnail in attachment meta in attached video file. + * This Function will also connected to rtMedia Plugin. + * Selected thumbnail use as cover art for buddyPress activity if video was uploaded in activity. * * @since 1.0.0 - * - * @param array $post An array of post data. - * @return array $form_fields + * @param array $post An array of post data. + * @return array $form_fields */ public function save_video_thumbnail( $post ) { - - $rtmedia_thumbnail = transcoder_filter_input( INPUT_POST, 'rtmedia-thumbnail', FILTER_SANITIZE_STRING ); - $id = ( ! empty( $post['ID'] ) && 0 < intval( $post['ID'] ) ) ? intval( $post['ID'] ) : 0; - - if ( isset( $rtmedia_thumbnail ) ) { - if ( class_exists( 'rtMedia' ) ) { - $file_url = $rtmedia_thumbnail; - /* for WordPress backward compatibility */ - if ( function_exists( 'wp_get_upload_dir' ) ) { - $uploads = wp_get_upload_dir(); - } else { - $uploads = wp_upload_dir(); - } - if ( 0 === strpos( $file_url, $uploads['baseurl'] ) ) { - $final_file_url = $file_url; - } else { - $final_file_url = $uploads['baseurl'] . '/' . $file_url; + // Attachment edit page selected Thumbnail file name. + $rtMediaSelectedThumbnail = (isset($post['rtmedia-thumbnail']) AND !empty($post['rtmedia-thumbnail'])) ? sanitize_text_field($post['rtmedia-thumbnail']) : NULL; + // Video attachment ID. + $postId = (isset($post['ID']) AND !empty($post['ID'])) ? intval( sanitize_text_field($post['ID']) ) : NULL; + // Empty check for thumbnail image file name and post id + if(! $rtMediaSelectedThumbnail OR ! $postId){ + return $post; + } + // === This is old code [legacy code starts] === + // if rtMedia Plugin is exist Do this block + if (class_exists( 'rtMedia' )){ + $uploads = function_exists('wp_get_upload_dir') ? wp_get_upload_dir() : wp_upload_dir(); + $final_file_url = (strpos($rtMediaSelectedThumbnail, $uploads['baseurl']) === false) ? $rtMediaSelectedThumbnail : $uploads['baseurl'] . '/' . $rtMediaSelectedThumbnail; + $rtmedia_model = new RTMediaModel(); + $media = $rtmedia_model->get(array('media_id' => $postId)); + $media_id = $media[0]->id; + $rtmedia_model->update(array('cover_art' => $final_file_url ), array('media_id' => $postId)); + rtt_update_activity_after_thumb_set( $media_id ); + } + // Updating post meta. + update_post_meta($postId, '_rt_media_video_thumbnail', $rtMediaSelectedThumbnail); + // === This is old code [legacy code ends] === + # I am creating thumbnail entry because when transcoder sent the data it create only first thumbnail of the video + // Getting all meta File name that was created by this Plugin + $thumbnailListArray = get_post_meta($postId, '_rt_media_thumbnails', true); + // Check is empty or not array. + if(! is_array($thumbnailListArray) OR empty($thumbnailListArray)){ + return $post; + } + // Global database object + global $wpdb; + // Looping the thumbnails array + foreach ($thumbnailListArray as $fileLastHalfPath){ + // Running database query to see thumbnail already exist in the database + $firstPreviousEntryID = $wpdb->get_var("SELECT * FROM ".$wpdb->prefix."posts WHERE post_type = 'attachment' AND post_title = '". pathinfo($fileLastHalfPath, PATHINFO_FILENAME) ."'"); + // if Thumbnail is not in the database than insert the thubnail to the database + if(! $firstPreviousEntryID){ + // Getting upload directory details. + $uploadsDir = function_exists('wp_get_upload_dir') ? wp_get_upload_dir() : wp_upload_dir(); + // File Upload path + $filePath = (isset($uploadsDir['basedir']) AND !empty($uploadsDir['basedir'])) ? $uploadsDir['basedir'] .'/'. $fileLastHalfPath : ""; + // sCheck to see File exist in the path if exist than create a thumbnail with that file, this file was uploaded by transcode but database was not updated + if(file_exists($filePath)){ + // Prepare an array of post data for the attachment. + $attachment = array( + 'guid' => $uploadsDir['url'] . '/' . basename( $filePath ), + 'post_mime_type' => (isset(wp_check_filetype(basename($filePath), null)['type']) AND ! empty(wp_check_filetype(basename($filePath), null)['type'])) ? wp_check_filetype(basename($filePath), null)['type'] : "image/jpeg", + 'post_title' => pathinfo($filePath, PATHINFO_FILENAME), + 'post_content' => '', + 'post_status' => 'inherit' + ); + // Insert the attachment to the database entry. + $new_attachment_id = wp_insert_attachment($attachment, $filePath, $postId); + // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. + require_once( ABSPATH . 'wp-admin/includes/image.php' ); + // Generate the metadata for the attachment, and update the database record. + $attach_data = wp_generate_attachment_metadata($new_attachment_id, $filePath); + // Updating attachment Information + wp_update_attachment_metadata($new_attachment_id, $attach_data); } - - $rtmedia_model = new RTMediaModel(); - $media = $rtmedia_model->get( array( 'media_id' => $id ) ); - $media_id = $media[0]->id; - $rtmedia_model->update( array( 'cover_art' => $final_file_url ), array( 'media_id' => $id ) ); - rtt_update_activity_after_thumb_set( $media_id ); - } - update_post_meta( $id, '_rt_media_video_thumbnail', $rtmedia_thumbnail ); + } + } + // getting the selected image thumbnail database id for setting the thumbnail image + $selectedThumbnailsId = $wpdb->get_var("SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type = 'attachment' AND post_title = '". pathinfo($rtMediaSelectedThumbnail, PATHINFO_FILENAME) ."'"); + // Setting selected image to the Parent file featured image. + if( $selectedThumbnailsId AND ! is_float($selectedThumbnailsId)){ + $post['_thumbnail_id'] = $selectedThumbnailsId; } return $post; @@ -409,42 +365,40 @@ public function save_video_thumbnail( $post ) { /** * Display admin notice. - * * @since 1.0.0 - */ + */ public function transcoder_admin_notice() { + // Getting site option data thats was saved before $show_notice = get_site_option( 'transcoder_admin_notice', 1 ); - - if ( '1' === $show_notice || 1 === $show_notice ) : + // + if ( '1' === $show_notice OR 1 === $show_notice ) : ?> -
- -

- -

-
- + api_key ) ) { + if(! empty( $this->api_key )){ return false; } $settings_page_link = 'admin.php?page=rt-transcoder'; @@ -461,11 +415,10 @@ public function subscribe_transcoder_admin_notice() { /** * Set option to hide admin notice when user click on dismiss button. - * * @since 1.0.0 */ public function transcoder_hide_admin_notice() { - if ( check_ajax_referer( '_transcoder_hide_notice_', 'transcoder_notice_nonce' ) ) { + if(check_ajax_referer( '_transcoder_hide_notice_', 'transcoder_notice_nonce' )){ update_site_option( 'transcoder_admin_notice', '0' ); } die(); @@ -473,7 +426,6 @@ public function transcoder_hide_admin_notice() { /** * Hide encoding tab in old rtMedia plugin. - * * @since 1.0.0 */ public function rtmedia_hide_encoding_tab() { @@ -487,16 +439,38 @@ public function rtmedia_hide_encoding_tab() { } /** - * Filters the Mediaelement fallback output to add class. - * - * @since 1.0.0 - * - * @param type $output Fallback output for no-JS. - * @param type $url Media file URL. - * - * @return string return fallback output. + * Filters the MediaElement fallback output to add class + * @since 1.0.0 + * @param type $output Fallback output for no-JS. + * @param type $url Media file URL. + * @return string return fallback output. */ public function mediaelement_add_class( $output, $url ) { return sprintf( '%1$s', esc_url( $url ) ); } + + + /** + * This function will add JS to the admin Footer, if Thumbnail is selection is clicked then select the clicked one and deselect rest of the checkboxes. + * @since 1.0.0 + * @param type $output Fallback output for no-JS. + * @param type $url Media file URL. + * @return string return fallback output. + */ + public function admin_footer_for_attachment_edit_page(){ + ?> + +