function rtmedia_update_view_meta($media_id, $action)
 {
     $curr_count = get_rtmedia_meta($media_id, $action);
     if (!$curr_count) {
         $curr_count = 1;
     } else {
         $curr_count++;
     }
     update_rtmedia_meta($media_id, $action, $curr_count, false);
 }
 /**
  *
  * @param type $media_ids
  * @param type $file_object
  * @param type $uploaded
  * @param string $autoformat thumbnails for genrating thumbs only
  */
 function encoding($media_ids, $file_object, $uploaded, $autoformat = true)
 {
     foreach ($file_object as $key => $single) {
         $type_arry = explode(".", $single['url']);
         $type = strtolower($type_arry[sizeof($type_arry) - 1]);
         $not_allowed_type = array("mp3");
         if (preg_match('/video|audio/i', $single['type'], $type_array) && !in_array($single['type'], array('audio/mp3')) && !in_array($type, $not_allowed_type)) {
             $options = rtmedia_get_site_option('rtmedia-options');
             $options_vedio_thumb = $options['general_videothumbs'];
             if ($options_vedio_thumb == "") {
                 $options_vedio_thumb = 3;
             }
             /**  fORMAT * */
             if ($single['type'] == 'video/mp4' || $type == "mp4") {
                 $autoformat = "thumbnails";
             }
             $query_args = array('url' => urlencode($single['url']), 'callbackurl' => urlencode(trailingslashit(home_url()) . "index.php"), 'force' => 0, 'size' => filesize($single['file']), 'formats' => $autoformat === true ? $type_array[0] == 'video' ? 'mp4' : 'mp3' : $autoformat, 'thumbs' => $options_vedio_thumb, 'rt_id' => $media_ids[$key]);
             $encoding_url = $this->api_url . 'job/new/';
             $upload_url = add_query_arg($query_args, $encoding_url . $this->api_key);
             //error_log(var_export($upload_url, true));
             //var_dump($upload_url);
             $upload_page = wp_remote_get($upload_url, array('timeout' => 200));
             //error_log(var_export($upload_page, true));
             if (!is_wp_error($upload_page) && (!isset($upload_page['headers']['status']) || isset($upload_page['headers']['status']) && $upload_page['headers']['status'] == 200)) {
                 $upload_info = json_decode($upload_page['body']);
                 if (isset($upload_info->status) && $upload_info->status && isset($upload_info->job_id) && $upload_info->job_id) {
                     $job_id = $upload_info->job_id;
                     update_rtmedia_meta($media_ids[$key], 'rtmedia-encoding-job-id', $job_id);
                     $model = new RTMediaModel();
                     $model->update(array('cover_art' => '0'), array('id' => $media_ids[$key]));
                 } else {
                     //                        remove_filter('bp_media_plupload_files_filter', array($bp_media_admin->bp_media_encoding, 'allowed_types'));
                     //                        return parent::insertmedia($name, $description, $album_id, $group, $is_multiple, $is_activity, $parent_fallback_files, $author_id, $album_name);
                 }
             }
             $this->update_usage($this->api_key);
         }
     }
 }
 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);
             }
         }
     }
 }