Exemplo n.º 1
0
/**
 * Helper function to prepare the metadata for an image in a slider.
 *
 * @since 1.0.0
 *
 * @param array $slider_data  Array of data for the slider.
 * @param int $id             The Post ID to prepare data for.
 * @param string $type        The type of slide to prepare (defaults to image).
 * @param array $data         Data to be used for the slide.
 * @return array $slider_data Amended slider data with updated image metadata.
 */
function soliloquy_ajax_prepare_slider_data($slider_data, $id, $type = 'image', $data = array())
{
    // Get global option for slide status
    $publishingDefault = get_option('soliloquy-publishing-default', 'pending');
    switch ($type) {
        case 'image':
            $attachment = get_post($id);
            $url = wp_get_attachment_image_src($id, 'full');
            $alt_text = get_post_meta($id, '_wp_attachment_image_alt', true);
            $slider_data['slider'][$id] = array('status' => $publishingDefault, 'id' => $id, 'attachment_id' => $id, 'src' => isset($url[0]) ? esc_url($url[0]) : '', 'title' => get_the_title($id), 'link' => '', 'alt' => !empty($alt_text) ? $alt_text : get_the_title($id), 'caption' => !empty($attachment->post_excerpt) ? $attachment->post_excerpt : '', 'type' => $type);
            break;
        case 'video':
            $slider_data['slider'][$id] = array('status' => $publishingDefault, 'id' => $id, 'src' => isset($data['src']) ? esc_url($data['src']) : '', 'title' => isset($data['title']) ? esc_html($data['title']) : '', 'url' => isset($data['url']) ? esc_url($data['url']) : '', 'caption' => isset($data['caption']) ? trim($data['caption']) : '', 'type' => $type);
            // If no thumbnail specified, attempt to get it from the video
            if (empty($data['src'])) {
                // Get Video Thumbnail
                if (preg_match('#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\\/)[^&\\n]+(?=\\?)|(?<=v=)[^&\\n]+|(?<=youtu.be/)[^&\\n]+#', $data['url'], $y_matches)) {
                    // YouTube
                    $videoID = $y_matches[0];
                    // Get HD or SD thumbnail
                    $data['src'] = soliloquy_ajax_get_youtube_thumbnail_url($videoID);
                } elseif (preg_match('#(?:https?:\\/\\/(?:[\\w]+\\.)*vimeo\\.com(?:[\\/\\w]*\\/videos?)?\\/([0-9]+)[^\\s]*)#i', $data['url'], $v_matches)) {
                    // Vimeo
                    $videoID = $v_matches[1];
                    // Get highest resolution thumbnail
                    $data['src'] = soliloquy_ajax_get_vimeo_thumbnail_url($videoID);
                } elseif (preg_match('/https?:\\/\\/(.+)?(wistia.com|wi.st)\\/.*/i', $data['url'], $w_matches)) {
                    // Wistia
                    $parts = explode('/', $w_matches[0]);
                    $videoID = array_pop($parts);
                    // Get image from API
                    $res = wp_remote_get('http://fast.wistia.net/oembed?url=' . urlencode($item['url']));
                    $bod = wp_remote_retrieve_body($res);
                    $api = json_decode($bod, true);
                    if (!empty($api['thumbnail_url'])) {
                        $data['src'] = remove_query_arg('image_crop_resized', $api['thumbnail_url']);
                    }
                } else {
                    // Unknown
                    $videoID = false;
                }
                // If a thumbnail was found, import it to the local filesystem
                $stream = Soliloquy_Import::get_instance()->import_remote_image($data['src'], $data, $id, 0, true);
                if (empty($stream['error']) || isset($stream['error']) && !$stream['error']) {
                    $slider_data['slider'][$id]['attachment_id'] = $stream['attachment_id'];
                    $slider_data['slider'][$id]['src'] = $stream['url'];
                }
            }
            break;
        case 'html':
            $slider_data['slider'][$id] = array('status' => $publishingDefault, 'id' => $id, 'title' => isset($data['title']) ? esc_html($data['title']) : '', 'code' => isset($data['code']) ? trim($data['code']) : '', 'type' => $type);
            break;
    }
    $slider_data = apply_filters('soliloquy_ajax_item_data', $slider_data, $id, $type);
    return $slider_data;
}
Exemplo n.º 2
0
 /**
  * Helper method for grabbing the default video thumbnail from the video provider.
  *
  * @since 2.2.0
  *
  * @param int|string $id The ID for the slide.
  * @param array $item    Array of data for the slide.
  * @param array $data    Array of slider data.
  * @return bool|string   False if unsuccessful, otherwise the video thumbnail.
  */
 public function get_video_thumbnail($id, $item, $data)
 {
     $thumb = false;
     $id = $this->get_video_data($id, $item, $data);
     $type = $this->get_video_data($id, $item, $data, 'type');
     switch ($type) {
         case 'youtube':
             $thumb = 'http://img.youtube.com/vi/' . $id . '/maxresdefault.jpg';
             break;
         case 'vimeo':
             $res = wp_remote_get('http://vimeo.com/api/v2/video/' . $id . '.json');
             $bod = wp_remote_retrieve_body($res);
             $api = json_decode($bod, true);
             if (!empty($api[0]) && !empty($api[0]['thumbnail_large'])) {
                 $thumb = $api[0]['thumbnail_large'];
             }
             break;
         case 'wistia':
             $res = wp_remote_get('http://fast.wistia.net/oembed?url=' . urlencode($item['url']));
             $bod = wp_remote_retrieve_body($res);
             $api = json_decode($bod, true);
             if (!empty($api['thumbnail_url'])) {
                 $thumb = remove_query_arg('image_crop_resized', $api['thumbnail_url']);
             }
             break;
     }
     // If we have reached this point, we need to stream and save the image into the database to prevent multiple stream lookups.
     if ($thumb) {
         if (!class_exists('Soliloquy_Import')) {
             require plugin_dir_path($this->base->file) . 'includes/admin/import.php';
         }
         $stream = Soliloquy_Import::get_instance()->import_remote_image($thumb, $data, $data['id'], 0, true);
         if (empty($stream['error']) || isset($stream['error']) && !$stream['error']) {
             $thumb = $stream['url'];
             $data = get_post_meta($data['id'], '_sol_slider_data', true);
             $data['slider'][$item['id']]['src'] = $thumb;
             update_post_meta($data['id'], '_sol_slider_data', $data);
             Soliloquy_Common::get_instance()->flush_slider_caches($data['id'], $this->get_config('slug', $data));
         }
     }
     return apply_filters('soliloquy_video_thumbnail', $thumb, $id, $item, $data);
 }
Exemplo n.º 3
0
 /**
  * Returns the singleton instance of the class.
  *
  * @since 1.0.0
  *
  * @return object The Soliloquy_Import object.
  */
 public static function get_instance()
 {
     if (!isset(self::$instance) && !self::$instance instanceof Soliloquy_Import) {
         self::$instance = new Soliloquy_Import();
     }
     return self::$instance;
 }