Skip to content
Permalink
Browse files

Instagram updates

- Handle video posts (format=video, download media, embed in post)
- Add a reprocessor to go back and handle videos properly as well
- Link up People mentioned in captions using People & Places
- Improve some code syntax
  • Loading branch information...
beaulebens committed Feb 26, 2017
1 parent 44d0b22 commit 465c6c83de6c3c4802a351694392b9150a74e219
Showing with 134 additions and 13 deletions.
  1. +110 −13 importers/keyring-importer-instagram.php
  2. +24 −0 keyring-importers.php
@@ -15,6 +15,17 @@ class Keyring_Instagram_Importer extends Keyring_Importer_Base {
function __construct() {
parent::__construct();
// Allow users to re-process old posts and handle video posts better
add_filter( 'keyring_importer_reprocessors', function( $reprocessors ) {
$reprocessors[ 'instagram-videos' ] = array(
'label' => __( 'Instagram video posts', 'keyring' ),
'description' => __( 'Previously, Instagram video posts were just handled like images. This will download the video, embed it in the post, and mark the post as a video post-format.', 'keyring' ),
'callback' => array( $this, 'reprocess_videos' ),
'service' => $this->taxonomy->slug,
);
return $reprocessors;
} );
// If we have People & Places available, then allow re-processing old posts as well
if ( class_exists( 'People_Places' ) ) {
add_filter( 'keyring_importer_reprocessors', function( $reprocessors ) {
@@ -134,14 +145,26 @@ function extract_posts_from_data( $raw ) {
// Apply selected category
$post_category = array( $this->get_option( 'category' ) );
// Construct a post body. By default we'll just link to the external image.
// In insert_posts() we'll attempt to download/replace that with a local version.
$post_content = '<p class="instagram-image">';
$post_content .= '<a href="' . esc_url( $post->link ) . '" class="instagram-link">';
$post_content .= '<img src="' . esc_url( $post->images->standard_resolution->url ) . '" width="' . esc_attr( $post->images->standard_resolution->width ) . '" height="' . esc_attr( $post->images->standard_resolution->height ) . '" alt="' . esc_attr( $post_title ) . '" class="instagram-img" />';
$post_content .= '</a></p>';
if ( !empty( $post->caption ) )
// Construct a post body.
$instagram_video = false;
if ( ! empty( $post->videos->standard_resolution->url ) ) {
// We've got a video to handle
$instagram_video = $post->videos->standard_resolution->url;
$post_content = '<p class="instagram-video">';
$post_content .= "\n\n" . esc_url( $post->videos->standard_resolution->url ) . "\n\n";
$post_content .= '</p>';
} else {
// Just an image. By default we'll just link to the external image.
// In insert_posts() we'll attempt to download/replace that with a local version.
$post_content = '<p class="instagram-image">';
$post_content .= '<a href="' . esc_url( $post->link ) . '" class="instagram-link">';
$post_content .= '<img src="' . esc_url( $post->images->standard_resolution->url ) . '" width="' . esc_attr( $post->images->standard_resolution->width ) . '" height="' . esc_attr( $post->images->standard_resolution->height ) . '" alt="' . esc_attr( $post_title ) . '" class="instagram-img" />';
$post_content .= '</a></p>';
}
if ( ! empty( $post->caption ) ) {
$post_content .= "\n<p class='instagram-caption'>" . $post->caption->text . '</p>';
}
// Include geo Data
$geo = false;
@@ -171,6 +194,16 @@ function extract_posts_from_data( $raw ) {
}
}
// User mentions in captions
if ( ! empty( $post->caption->text ) && stristr( $post->caption->text, '@' ) ) {
preg_match_all( '/(^|[(\[\s\.])?@(\w+)/', $post->caption->text, $matches );
foreach ( (array) $matches[2] as $match ) {
$people[ trim( $match ) ] = array(
'name' => trim( $match )
);
}
}
// Extract specific details of the place/venue
$place = array();
if ( ! empty( $post->location ) ) {
@@ -203,6 +236,7 @@ function extract_posts_from_data( $raw ) {
'instagram_id',
'instagram_url',
'instagram_img',
'instagram_video',
'instagram_filter',
'instagram_raw',
'people',
@@ -231,17 +265,23 @@ function insert_posts() {
} else {
$post_id = wp_insert_post( $post );
if ( is_wp_error( $post_id ) )
if ( is_wp_error( $post_id ) ) {
return $post_id;
}
if ( !$post_id )
if ( ! $post_id ) {
continue;
}
// Track which Keyring service was used
wp_set_object_terms( $post_id, self::LABEL, 'keyring_services' );
// Mark it as an aside
set_post_format( $post_id, 'image' );
// Mark post format, based on what we actually imported
if ( $instagram_video ) {
set_post_format( $post_id, 'video' );
} else {
set_post_format( $post_id, 'image' );
}
// Update Category
wp_set_post_categories( $post_id, $post_category );
@@ -250,8 +290,13 @@ function insert_posts() {
add_post_meta( $post_id, 'instagram_url', $instagram_url );
add_post_meta( $post_id, 'instagram_filter', $instagram_filter );
if ( count( $tags ) )
if ( $instagram_video ) {
add_post_meta( $post_id, 'instagram_video', $instagram_video );
}
if ( count( $tags ) ) {
wp_set_post_terms( $post_id, implode( ',', $tags ) );
}
// Store geodata if it's available
if ( ! empty( $geo ) ) {
@@ -262,7 +307,16 @@ function insert_posts() {
add_post_meta( $post_id, 'raw_import_data', wp_slash( json_encode( $instagram_raw ) ) );
$this->sideload_media( $instagram_img, $post_id, $post, apply_filters( 'keyring_instagram_importer_image_embed_size', 'full' ) );
if ( $instagram_video ) {
// Sideload thumbnail image just for "completeness"
media_sideload_image( $instagram_img, $post_id );
// Sideload the video itself
$this->sideload_video( $instagram_video, $post_id );
} else {
// Sideload and embed image
$this->sideload_media( $instagram_img, $post_id, $post, apply_filters( 'keyring_instagram_importer_image_embed_size', 'full' ) );
}
// If we found people, and have the People & Places plugin available
// to handle processing/storing, then store references to people against
@@ -388,7 +442,50 @@ function reprocess_places( $post ) {
}
return Keyring_Importer_Reprocessor::PROCESS_SUCCESS;
}
/**
* Now that we know how to handle videos, we can go back over old posts and download them
*/
function reprocess_videos( $post ) {
// Get raw data
$raw = get_post_meta( $post->ID, 'raw_import_data', true );
if ( ! $raw ) {
return Keyring_Importer_Reprocessor::PROCESS_SKIPPED;
}
// Decode it, and bail if that fails for some reason
$raw = json_decode( $raw );
if ( null == $raw ) {
return Keyring_Importer_Reprocessor::PROCESS_FAILED;
}
// Look for video elements and if found, handle it
if ( empty( $raw->videos->standard_resolution->url ) ) {
return Keyring_Importer_Reprocessor::PROCESS_SKIPPED;
} else {
// Change the content, so we're ready to sideload
$post_content = '<p class="instagram-video">';
$post_content .= "\n\n" . esc_url( $raw->videos->standard_resolution->url ) . "\n\n";
$post_content .= '</p>';
if ( ! empty( $raw->caption ) ) {
$post_content .= "\n<p class='instagram-caption'>" . $raw->caption->text . '</p>';
}
$post_data = get_post( $post->ID );
$post_data->post_content = $post_content;
wp_update_post( $post_data );
// Handle the video
$this->sideload_video( $raw->videos->standard_resolution->url, $post->ID );
// Update the post format
set_post_format( $post->ID, 'video' );
}
return Keyring_Importer_Reprocessor::PROCESS_SUCCESS;
}
}
} // end function Keyring_Instagram_Importer
@@ -866,6 +866,30 @@ function sideload_media( $urls, $post_id, $post, $size = 'large', $append = fals
wp_update_post( $post );
}
}
/**
* Similar to sideload_media, but a little simpler. This will download a video
* from a URL, and then embed it into a post by replacing the same URL
*/
function sideload_video( $url, $post_id ) {
$file = array();
$file['tmp_name'] = download_url( $url );
if ( is_wp_error( $file['tmp_name'] ) ) {
// Download failed, leave the post alone
@unlink( $file_array['tmp_name'] );
} else {
// Download worked, now import into Media Library
$file['name'] = basename( $url );
$id = media_handle_sideload( $file, $post_id );
@unlink( $file_array['tmp_name'] );
if ( ! is_wp_error( $id ) ) {
// Update URL in post to point to the local copy
$post_data = get_post( $post_id );
$post_data->post_content = str_replace( $url, wp_get_attachment_url( $id ), $post_data->post_content );
wp_update_post( $post_data );
}
}
}
}
/**

0 comments on commit 465c6c8

Please sign in to comment.
You can’t perform that action at this time.