Skip to content

Commit

Permalink
Import carousels and their content
Browse files Browse the repository at this point in the history
* Adds support for importing carousel-type posts
* downloads all images and video
* adds them to the post metadata (call get_post_meta() with  $single = false to get an array of all)
* sets the featured image to match the what instagram shows (either the first picture, or the first frame if it's a movie)

This doesn't automatically insert the extra content into the post (yet). I'm doing it with a hook, but I think we need to figure out a better way to make that a built-in feature.

Partially Fixes jtsternberg#43, and superseds / fixes jtsternberg#42
  • Loading branch information
nfriedly committed Sep 19, 2018
1 parent e04cf13 commit e9c9377
Showing 1 changed file with 68 additions and 44 deletions.
112 changes: 68 additions & 44 deletions dsgnwrks-instagram-importer.php
Expand Up @@ -60,6 +60,13 @@ class DsgnWrksInstagram extends DsgnWrksInstagram_Debug {
*/
public $manual_import = false;

/**
* Plugin prefix
*
* @var string
*/
public $pre = 'dsgnwrks_instagram_';

/**
* Instagram API URL
*
Expand Down Expand Up @@ -182,7 +189,7 @@ protected function hooks() {
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'admin_init', array( $this, 'schedule_frequency' ) );
add_action( 'wp_ajax_dsgnwrks_instagram_import', array( $this, 'ajax_import' ) );
add_action( 'dsgnwrks_instagram_cron', array( $this, 'cron_import' ) );
add_action( $this->pre .'cron', array( $this, 'cron_import' ) );
add_action( 'init', array( $this->embed, 'hook_shortcode' ) );
add_action( 'admin_menu', array( $this->settings, 'settings' ) );
register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );
Expand Down Expand Up @@ -212,13 +219,13 @@ public function register_settings() {

// A pseudo setting. redirects to instagram oauth
register_setting(
'dsgnwrks_instagram_importer_users',
$this->pre .'importer_users',
'dsgnwrks_insta_registration',
array( $this, 'instagram_oauth_redirect' )
);
// validate user options settings
register_setting(
'dsgnwrks_instagram_importer_settings',
$this->pre .'importer_settings',
'dsgnwrks_insta_options',
array( $this, 'settings_validate' )
);
Expand All @@ -234,9 +241,9 @@ public function schedule_frequency() {
$frequency = $this->get_option( 'frequency' );

// if a auto-import frequency interval was saved,
if ( $frequency && 'never' !== $frequency && ! wp_next_scheduled( 'dsgnwrks_instagram_cron' ) ) {
if ( $frequency && 'never' !== $frequency && ! wp_next_scheduled( $this->pre .'cron' ) ) {
// schedule a cron to pull updates from instagram
wp_schedule_event( time(), $frequency, 'dsgnwrks_instagram_cron' );
wp_schedule_event( time(), $frequency, $this->pre .'cron' );
}
}

Expand Down Expand Up @@ -638,11 +645,6 @@ protected function import_messages( $api_url, $prevmessages = array() ) {
// merge previous messages
$messages = ( isset( $messages['messages'] ) ) ? array_merge( $prevmessages, $messages['messages'] ) : $prevmessages;

// This is a (temporary) hack to keep going back in time searching for posts to import.
// if ( empty( $messages ) ) {
// $messages[] = '<p>Nope, nothing, try again...</p>';
// }

// Remove our max quality filter
remove_filter( 'wp_editor_set_quality', array( $this, 'max_quality' ) );
remove_filter( 'jpeg_quality', array( $this, 'max_quality' ) );
Expand All @@ -669,7 +671,7 @@ protected function import_messages( $api_url, $prevmessages = array() ) {
protected function pic_loop( $data = array() ) {

// 'Type' to be imported (images/video)
$this->settings->all_opts[ $this->settings->userid ]['types'] = (array) apply_filters( 'dsgnwrks_instagram_import_types', array( 'video', 'image' ), $this->settings->userid );
$this->settings->all_opts[ $this->settings->userid ]['types'] = (array) apply_filters( 'dsgnwrks_instagram_import_types', array( 'video', 'image', 'carousel' ), $this->settings->userid );

// if we have invalid data, bail here
if ( !isset( $data->data ) || !is_array( $data->data ) )
Expand Down Expand Up @@ -764,7 +766,8 @@ protected function save_img_post() {

$this->type = 'image';
// sideload image
$message = $this->upload_media( array( $p->images->thumbnail->url, $p->images->standard_resolution->url ), $p->created_time );
$message = $this->upload_media( array( $p->images->thumbnail->url, $p->images->standard_resolution->url ), $p->created_time, '', '', true );


// sideload videos
if ( $this->pic->type == 'video' ) {
Expand All @@ -781,6 +784,36 @@ protected function save_img_post() {
);
}
}

// sideload content from carousels

if ( $this->pic->type == 'carousel' ) {
$this->type = 'carousel';
$is_primary = false;
// loop through the carousel and add all images as non-primary images
foreach ( $p->carousel_media as $item ) {
// upload images unless they were already uploaded
// (if the carousel starts with an image, it will have been captured above)
if ($item->type == 'image' && $item->images->thumbnail->url !== $p->images->thumbnail->url) {
$message .= $this->upload_media( array( $item->images->thumbnail->url, $item->images->standard_resolution->url ), $p->created_time, '', '', $is_primary );
}
if ($item->type == 'video') {
// grab both video sizes and upload them
foreach ( array( 'low_resolution', 'standard_resolution' ) as $size ) {
$vid_size = (int) $item->videos->$size->width;
$title = sprintf( __( '%s Video', 'dsgnwrks' ), $vid_size .'x'. $vid_size );
$message .= $this->upload_media(
$item->videos->$size->url,
$title . ' ' . $item->created_time,
$title,
$size,
$is_primary
);
}
}
}
}


// Update post content with our modified post content that replaces the custom tags.
$this->update_post_content();
Expand Down Expand Up @@ -1053,80 +1086,71 @@ protected function savePostmeta() {
* @param string $filename What to name the file. File extension will be added automatically.
* @param string $attach_title Optional title for uploaded media attachement
* @param string $size Optional size of media
* @param boolean $is_primary Is this the primary image or part of a carousel?
* @return string Error|Success message
*/
protected function upload_media( $media_url = '', $filename = '', $attach_title = '', $size = '' ) {

protected function upload_media( $media_url = '', $filename = '', $attach_title = '', $size = '', $is_primary = true ) {
// get our import data
$import = &$this->import;
// image or video?
$is_image = is_array( $media_url );

// bail here if we don't have a media url
if ( empty( $media_url ) ) {
return $this->upload_error(__LINE__);
}

if ( $this->doing_cron ) {
require_once( ABSPATH .'/wp-admin/includes/file.php' );
require_once( ABSPATH .'/wp-admin/includes/media.php' );
require_once( ABSPATH .'/wp-admin/includes/image.php' );
}

$tmp = $this->download_media_url( $media_url );

$media_url = is_array( $media_url ) ? $media_url[1] : $media_url;

// check for file extensions
$pattern = $is_image
? '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i'
: '/[^\?]+\.(mp4|MP4)/';

preg_match( $pattern, $media_url, $matches );

$file_array['tmp_name'] = $tmp;
$file_array['name'] = $filename
? sanitize_title_with_dashes( $filename ) . '.' . $matches[1]
: basename( $matches[0] );

if ( is_wp_error( $tmp ) ) {
@unlink( $file_array['tmp_name'] );
$file_array['tmp_name'] = '';
}

// post title or custom title
$attach_title = $attach_title ? $attach_title : $import['post_title'];

$attach_id = media_handle_sideload( $file_array, $import['post_id'], $attach_title );

if ( is_wp_error( $attach_id ) ) {
@unlink( $file_array['tmp_name'] );
// may return an error if they're on multisite and don't have mp4 enabled
return $this->upload_error( __LINE__, $media_url, $attach_id->get_error_message() );
}

if ( ! $is_image ) {

// Save our video attachement ID's and their urls as post-meta
update_post_meta( $import['post_id'], 'instagram_video_id_'. $size, $attach_id );
update_post_meta( $import['post_id'], 'instagram_video_url_'. $size, wp_get_attachment_url( $attach_id ) );

add_post_meta( $import['post_id'], 'instagram_video_id_'. $size, $attach_id );
add_post_meta( $import['post_id'], 'instagram_video_url_'. $size, wp_get_attachment_url( $attach_id ) );
return '<em> '. sprintf( __( '%s imported.', 'dsgnwrks' ), '<b>'. $attach_title .'</b>' ) .'</em>';
} else {
// Save our photo attachement ID as post-meta
update_post_meta( $import['post_id'], 'instagram_image_id', $attach_id );
}

if ( $import['featured'] ) {
set_post_thumbnail( $import['post_id'], $attach_id );
add_post_meta( $import['post_id'], 'instagram_image_id', $attach_id );
}

// Get image markup
$img_element = $this->get_image_el( $attach_id );

// Replace URLs in post with uploaded image
if ( $img_element ) {


if ( ! $img_element ) {
return $this->upload_error( __LINE__, $media_url );
}

if ( $is_primary ) {

if ( $import['featured'] ) {
set_post_thumbnail( $import['post_id'], $attach_id );
}

// Replace URLs in post with uploaded image

/**
* Filters the image element.
*
Expand All @@ -1136,13 +1160,12 @@ protected function upload_media( $media_url = '', $filename = '', $attach_title
*/
$this->insta_image = (string) apply_filters( 'dsgnwrks_instagram_insta_image', $img_element, $attach_id, $import['post_id'] );

// return a success message
return dw_get_instagram_image( $import['post_id'], array( 50, 50 ) ) .'<strong>&ldquo;'. $import['post_title'] .'&rdquo;</strong> <em> '. __( 'imported and created successfully.', 'dsgnwrks' ) .'</em>';

} else {
return $this->upload_error( __LINE__, $media_url );
return $img_element;
}

// return a success message
return dw_get_instagram_image( $import['post_id'], array( 50, 50 ) ) .'<strong>&ldquo;'. $import['post_title'] .'&rdquo;</strong> <em> '. __( 'imported and created successfully.', 'dsgnwrks' ) .'</em>';

}

/**
Expand Down Expand Up @@ -1646,3 +1669,4 @@ public function debugsend( $line, $title = false, $data = false ) {
wp_mail( 'justin@dsgnwrks.pro', 'Instagram Debug - '. $title .' - line '. $line, $data );
}
}

0 comments on commit e9c9377

Please sign in to comment.