function migrate_single_media($result) { global $wpdb; $rtmedia_model = new RTMediaModel(); $attached_file = get_attached_file($result->media_id); $return = true; if (file_exists($attached_file)) { $file_size = filesize($attached_file); } else { $file_size = '0'; error_log('rtMedia size importer: file not exist. Media ID: ' . $result->id . ', File: ' . $attached_file); $return = false; return false; } $post = get_post($result->media_id); $post_date = $post->post_date; $rtmedia_model->update(array('upload_date' => $post_date, 'file_size' => $file_size), array('id' => $result->id)); return $return; }
function save_video_thumbnail($post, $attachment) { if (isset($post['rtmedia-thumbnail'])) { $rtmedia_model = new RTMediaModel(); $model = new RTMediaModel(); $media = $model->get(array('media_id' => $post['ID'])); $media_id = $media[0]->id; $rtmedia_model->update(array('cover_art' => $post['rtmedia-thumbnail']), array('media_id' => $post['ID'])); update_activity_after_thumb_set($media_id); } return $post; }
public function add_media_thumbnails($post_id) { //global $rtmedia_ffmpeg; $post_info = get_post($post_id); $post_date_string = new DateTime($post_info->post_date); $post_date = $post_date_string->format('Y-m-d G:i:s'); $post_date_thumb_string = new DateTime($post_info->post_date); $post_date_thumb = $post_date_thumb_string->format('Y/m/'); $post_thumbs = get_post_meta($post_id, 'rtmedia_encode_response', TRUE); $post_thumbs_array = maybe_unserialize($post_thumbs); $largest_thumb_size = 0; $model = new RTMediaModel(); $media = $model->get(array("media_id" => $post_id)); $media_id = $media[0]->id; $largest_thumb = false; $upload_thumbnail_array = array(); //var_dump($post_thumbs_array['thumbs']); foreach ($post_thumbs_array['thumbs'] as $thumbs => $thumbnail) { // error_log("Thumb:" + var_export($post_thumbs_array['thumbs'][$thumbnail])); // } // for ($i = 1; $i <= sizeof($post_thumbs_array['thumbs']); $i++) { // $thumbnail = 'thumb_' . $i; // if (isset($post_thumbs_array['thumbs'][$thumbnail])) { $thumbnail_ids = get_rtmedia_meta($post_id, 'rtmedia-thumbnail-ids', true); $thumbresource = wp_remote_get($thumbnail); $thumbinfo = pathinfo($thumbnail); $temp_name = $thumbinfo['basename']; $temp_name = urldecode($temp_name); $temp_name_array = explode("/", $temp_name); $temp_name = $temp_name_array[sizeof($temp_name_array) - 1]; $thumbinfo['basename'] = $temp_name; $thumb_upload_info = wp_upload_bits($thumbinfo['basename'], null, $thumbresource['body']); $upload_thumbnail_array[] = $thumb_upload_info['url']; //var_dump($thumb_upload_info); // $thumb_attachment = array( // 'guid' => $thumb_upload_info['url'], // 'post_mime_type' => 'image/jpeg', // 'post_title' => basename($thumbinfo['basename'], '.jpg'), // 'post_content' => '', // 'post_status' => 'inherit', // 'post_date' => $post_date // ); // //var_dump($thumb_attachment); // $upload_dir = wp_upload_dir($post_date_thumb); // // //insert into attachment // $attach_id = wp_insert_attachment($thumb_attachment, trailingslashit($upload_dir['path']) . $thumbinfo['basename'], 0); // if (!is_wp_error($attach_id) && $attach_id) { // $attach_data = wp_generate_attachment_metadata($attach_id, $thumb_upload_info['file']); // if (wp_update_attachment_metadata($attach_id, $attach_data)) { // $thumbnail_ids[] = $attach_id; // update_rtmedia_meta($post_id, 'rtmedia-thumbnail-ids', $thumbnail_ids); // } // else // wp_delete_attachment($attach_id, true); // } $current_thumb_size = @filesize($thumb_upload_info['url']); if ($current_thumb_size >= $largest_thumb_size) { $largest_thumb_size = $current_thumb_size; $largest_thumb = $thumb_upload_info['url']; $model->update(array('cover_art' => $thumb_upload_info['url']), array('media_id' => $post_id)); } ///} } update_activity_after_thumb_set($media_id); update_post_meta($post_id, 'rtmedia_media_thumbnails', $upload_thumbnail_array); return $largest_thumb; }
function set_video_thumbnail($id) { $media_type = rtmedia_type($id); if ('video' == $media_type && isset($_POST['rtmedia-thumbnail'])) { $model = new RTMediaModel(); $model->update(array('cover_art' => $_POST['rtmedia-thumbnail']), array('id' => $id)); update_activity_after_thumb_set($id); // code to update activity } }
function importmedia($id, $prefix) { $delete = false; $attached_file = get_attached_file($id); $attached_file_option = get_post_meta($id, '_wp_attached_file', true); $basename = wp_basename($attached_file); $file_folder_path = trailingslashit(str_replace($basename, '', $attached_file)); $siteurl = get_option('siteurl'); $upload_path = trim(get_option('upload_path')); if (empty($upload_path) || 'wp-content/uploads' == $upload_path) { $dir = WP_CONTENT_DIR . '/uploads'; } elseif (0 !== strpos($upload_path, ABSPATH)) { // $dir is absolute, $upload_path is (maybe) relative to ABSPATH $dir = path_join(ABSPATH, $upload_path); } else { $dir = $upload_path; } if (!($url = get_option('upload_url_path'))) { if (empty($upload_path) || 'wp-content/uploads' == $upload_path || $upload_path == $dir) { $url = WP_CONTENT_URL . '/uploads'; } else { $url = trailingslashit($siteurl) . $upload_path; } } // Obey the value of UPLOADS. This happens as long as ms-files rewriting is disabled. // We also sometimes obey UPLOADS when rewriting is enabled -- see the next block. if (defined('UPLOADS') && !(is_multisite() && rtmedia_get_site_option('ms_files_rewriting'))) { $dir = ABSPATH . UPLOADS; $url = trailingslashit($siteurl) . UPLOADS; } // If multisite (and if not the main site in a post-MU network) if (is_multisite() && !(is_main_site() && defined('MULTISITE'))) { if (!rtmedia_get_site_option('ms_files_rewriting')) { // If ms-files rewriting is disabled (networks created post-3.5), it is fairly straightforward: // Append sites/%d if we're not on the main site (for post-MU networks). (The extra directory // prevents a four-digit ID from conflicting with a year-based directory for the main site. // But if a MU-era network has disabled ms-files rewriting manually, they don't need the extra // directory, as they never had wp-content/uploads for the main site.) if (defined('MULTISITE')) { $ms_dir = '/sites/' . get_current_blog_id(); } else { $ms_dir = '/' . get_current_blog_id(); } $dir .= $ms_dir; $url .= $ms_dir; } elseif (defined('UPLOADS') && !ms_is_switched()) { // Handle the old-form ms-files.php rewriting if the network still has that enabled. // When ms-files rewriting is enabled, then we only listen to UPLOADS when: // 1) we are not on the main site in a post-MU network, // as wp-content/uploads is used there, and // 2) we are not switched, as ms_upload_constants() hardcodes // these constants to reflect the original blog ID. // // Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute. // (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as // as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files // rewriting in multisite, the resulting URL is /files. (#WP22702 for background.) if (defined('BLOGUPLOADDIR')) { $dir = untrailingslashit(BLOGUPLOADDIR); } else { $dir = ABSPATH . UPLOADS; } $url = trailingslashit($siteurl) . 'files'; } } $basedir = trailingslashit($dir); $baseurl = trailingslashit($url); $new_file_folder_path = trailingslashit(str_replace($basedir, $basedir . "rtMedia/{$prefix}/", $file_folder_path)); $year_month = untrailingslashit(str_replace($basedir, '', $file_folder_path)); $metadata = wp_get_attachment_metadata($id); $backup_metadata = get_post_meta($id, '_wp_attachment_backup_sizes', true); $instagram_thumbs = get_post_meta($id, '_instagram_thumbs', true); $instagram_full_images = get_post_meta($id, '_instagram_full_images', true); $instagram_metadata = get_post_meta($id, '_instagram_metadata', true); $encoding_job_id = get_post_meta($id, 'bp-media-encoding-job-id', true); $ffmpeg_thumbnail_ids = get_post_meta($id, 'bp_media_thumbnail_ids', true); $ffmpeg_thumbnail = get_post_meta($id, 'bp_media_thumbnail', true); $ffmpeg_remote_id = get_post_meta($id, 'bp_media_ffmpeg_remote_id', true); $kaltura_remote_id = get_post_meta($id, 'bp_media_kaltura_remote_id', true); if (wp_mkdir_p($basedir . "rtMedia/{$prefix}/" . $year_month)) { if (copy($attached_file, str_replace($basedir, $basedir . "rtMedia/{$prefix}/", $attached_file))) { $delete = true; if (isset($metadata['sizes'])) { foreach ($metadata['sizes'] as $size) { if (!copy($file_folder_path . $size['file'], $new_file_folder_path . $size['file'])) { $delete = false; } else { $delete_sizes[] = $file_folder_path . $size['file']; $this->search_and_replace(trailingslashit($baseurl . $year_month) . $size['file'], trailingslashit($baseurl . "rtMedia/{$prefix}/" . $year_month) . $size['file']); } } } if ($backup_metadata) { foreach ($backup_metadata as $backup_images) { if (!copy($file_folder_path . $backup_images['file'], $new_file_folder_path . $backup_images['file'])) { $delete = false; } else { $delete_sizes[] = $file_folder_path . $backup_images['file']; $this->search_and_replace(trailingslashit($baseurl . $year_month) . $backup_images['file'], trailingslashit($baseurl . "rtMedia/{$prefix}/" . $year_month) . $backup_images['file']); } } } if ($instagram_thumbs) { foreach ($instagram_thumbs as $key => $insta_thumb) { try { if (!copy(str_replace($baseurl, $basedir, $insta_thumb), str_replace($baseurl, $basedir . "rtMedia/{$prefix}/", $insta_thumb))) { $delete = false; } else { $delete_sizes[] = str_replace($baseurl, $basedir, $insta_thumb); $instagram_thumbs_new[$key] = str_replace($baseurl, $baseurl . "rtMedia/{$prefix}/", $insta_thumb); $this->search_and_replace(trailingslashit($baseurl . $year_month) . $insta_thumb, trailingslashit($baseurl . "rtMedia/{$prefix}/" . $year_month) . $insta_thumb); } } catch (Exceptio $e) { $delete = false; } } } if ($instagram_full_images) { foreach ($instagram_full_images as $key => $insta_full_image) { if (!copy($insta_full_image, str_replace($basedir, $basedir . "rtMedia/{$prefix}/", $insta_full_image))) { $delete = false; } else { $delete_sizes[] = $insta_full_image; $instagram_full_images_new[$key] = str_replace($basedir, $basedir . "rtMedia/{$prefix}", $insta_full_image); $this->search_and_replace(trailingslashit($baseurl . $year_month) . $insta_full_image, trailingslashit($baseurl . "rtMedia/{$prefix}/" . $year_month) . $insta_full_image); } } } if ($instagram_metadata) { $instagram_metadata_new = $instagram_metadata; foreach ($instagram_metadata as $wp_size => $insta_metadata) { if (isset($insta_metadata['file'])) { if (!copy($basedir . $insta_metadata['file'], $basedir . "rtMedia/{$prefix}/" . $insta_metadata['file'])) { $delete = false; } else { $delete_sizes[] = $basedir . $insta_metadata['file']; $instagram_metadata_new[$wp_size]['file'] = "rtMedia/{$prefix}/" . $insta_metadata['file']; if (isset($insta_metadata['sizes'])) { foreach ($insta_metadata['sizes'] as $key => $insta_size) { if (!copy($file_folder_path . $insta_size['file'], $new_file_folder_path . $insta_size['file'])) { $delete = false; } else { $delete_sizes[] = $file_folder_path . $insta_size['file']; $this->search_and_replace(trailingslashit($baseurl . $year_month) . $insta_size['file'], trailingslashit($baseurl . "rtMedia/{$prefix}/" . $year_month) . $insta_size['file']); } } } } } } } if ($delete) { if (file_exists($attached_file)) { unlink($attached_file); } if (isset($delete_sizes)) { foreach ($delete_sizes as $delete_size) { if (file_exists($delete_size)) { unlink($delete_size); } } } update_post_meta($id, '_wp_attached_file', "rtMedia/{$prefix}/" . $attached_file_option); if (isset($metadata['file'])) { $metadata['file'] = "rtMedia/{$prefix}/" . $metadata['file']; wp_update_attachment_metadata($id, $metadata); } if ($instagram_thumbs) { update_rtmedia_meta($id, '_instagram_thumbs', $instagram_thumbs_new); } if ($instagram_full_images) { update_rtmedia_meta($id, '_instagram_full_images', $instagram_full_images_new); } if ($instagram_metadata) { update_rtmedia_meta($id, '_instagram_metadata', $instagram_metadata_new); } if ($encoding_job_id) { update_rtmedia_meta($id, 'rtmedia-encoding-job-id', $encoding_job_id); } if ($ffmpeg_thumbnail_ids) { update_rtmedia_meta($id, 'rtmedia-thumbnail-ids', $ffmpeg_thumbnail_ids); } if ($ffmpeg_thumbnail) { $model = new RTMediaModel(); $model->update(array('cover_art' => $ffmpeg_thumbnail), array('id' => $id)); } if ($ffmpeg_remote_id) { update_rtmedia_meta($id, 'rtmedia-ffmpeg-remote-id', $ffmpeg_remote_id); } if ($kaltura_remote_id) { update_rtmedia_meta($id, 'rtmedia-kaltura-remote-id', $kaltura_remote_id); } $attachment = array(); $attachment['ID'] = $id; $old_guid = get_post_field('guid', $id); $attachment['guid'] = str_replace($baseurl, $baseurl . "rtMedia/{$prefix}/", $old_guid); /** * For Activity */ global $last_baseurl, $last_newurl; $last_baseurl = $baseurl; $last_newurl = $baseurl . "rtMedia/{$prefix}/"; $this->search_and_replace($old_guid, $attachment['guid']); wp_update_post($attachment); } } } }
function rtm_change_activity_privacy() { $data = $_POST; if (wp_verify_nonce($data['nonce'], 'rtmedia_activity_privacy_nonce')) { $rtm_activity_model = new RTMediaActivityModel(); $is_ac_privacy_exist = $rtm_activity_model->check($data['activity_id']); $privacy = intval($data['privacy']); $activity_id = intval($data['activity_id']); if (!$is_ac_privacy_exist) { // Very first privacy entry for this activity $status = $rtm_activity_model->insert(array('privacy' => $privacy, 'activity_id' => $activity_id, 'user_id' => get_current_user_id())); } else { // Just update the existing value $status = $rtm_activity_model->update(array('privacy' => $privacy), array('activity_id' => $activity_id)); } // update privacy of corresponding media $media_model = new RTMediaModel(); $activity_media = $media_model->get(array('activity_id' => $activity_id)); if (!empty($activity_media) && is_array($activity_media)) { foreach ($activity_media as $single_media) { $where = array('id' => $single_media->id); $columns = array('privacy' => $privacy); // update media privacy $media_model->update($columns, $where); } } if ($status === false) { $status = 'false'; } else { $status = 'true'; } echo $status; wp_die(); } }
/** * Method to reassign media to another user while deleting user * * @param type $user_id, $reassign * */ public function reassign_wordpress_user($user_id, $reassign) { if ($reassign != null || $reassign != '') { // Updating media author $rtmedia_model = new RTMediaModel(); $rtmedia_model->update(array('media_author' => $reassign), array('media_author' => $user_id)); // Updating user id from interaction $rtmediainteraction = new RTMediaInteractionModel(); $rtmediainteraction->update(array('user_id' => $reassign), array('user_id' => $user_id)); } }
function rtm_change_activity_privacy() { $nonce = filter_input(INPUT_POST, 'nonce', FILTER_SANITIZE_STRING); $privacy = filter_input(INPUT_POST, 'privacy', FILTER_SANITIZE_NUMBER_INT); $activity_id = filter_input(INPUT_POST, 'activity_id', FILTER_SANITIZE_NUMBER_INT); if (wp_verify_nonce($nonce, 'rtmedia_activity_privacy_nonce')) { $rtm_activity_model = new RTMediaActivityModel(); $is_ac_privacy_exist = $rtm_activity_model->check($activity_id); $privacy = intval($privacy); $activity_id = intval($activity_id); if (!$is_ac_privacy_exist) { // Very first privacy entry for this activity $status = $rtm_activity_model->insert(array('privacy' => $privacy, 'activity_id' => $activity_id, 'user_id' => get_current_user_id())); } else { // Just update the existing value $status = $rtm_activity_model->update(array('privacy' => $privacy), array('activity_id' => $activity_id)); } // update privacy of corresponding media $media_model = new RTMediaModel(); $activity_media = $media_model->get(array('activity_id' => $activity_id)); if (!empty($activity_media) && is_array($activity_media)) { foreach ($activity_media as $single_media) { $where = array('id' => $single_media->id); $columns = array('privacy' => $privacy); // update media privacy $media_model->update($columns, $where); } } if (false === $status) { $status = 'false'; } else { $status = 'true'; } echo esc_html($status); wp_die(); } }