function wpinstaroll_checkInstagramPhotoDataPresenceFromInstaID($insta_id) { global $wpdb; $table = WP_ROLL_INSTAGRAM_PICS_TRACK_TABLE; if (empty($insta_id)) { return false; } $result = wpinstaroll_getInstagramPhotoDataFromInstaID($insta_id); if ($result) { return true; } else { return false; } }
function wpinstaroll_createpostfromphoto($insta_id, $insta_url, $insta_link = '', $insta_caption = '', $insta_author_username = '', $insta_author_id = '') { // mandatory parameters missing if (empty($insta_id) || empty($insta_url)) { return array('error' => true, 'error_description' => WP_ROLL_INSTAGRAM_ERROR_MISSING_PARAMETERS_MESSAGE, 'error_code' => WP_ROLL_INSTAGRAM_ERROR_MISSING_PARAMETERS_CODE); } /* // not actually needed, here! $search_tag = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX.'_instagram_search_tag'); if (empty($search_tag)) { return array( 'error' => true, 'error_description' => WP_ROLL_INSTAGRAM_ERROR_INSTAGRAM_ACCESS_NOT_CONFIGURED_MESSAGE, 'error_code' => WP_ROLL_INSTAGRAM_ERROR_INSTAGRAM_ACCESS_NOT_CONFIGURED_CODE ); } */ $title_placeholder = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_post_title_placeholder'); $category_for_post = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_post_category'); if (empty($category_for_post)) { $category_for_post = 'Uncategorized'; update_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_post_category', $category_for_post); } // a. if the category corresponding to the Instagram search tags // doesn't exist, we create it $cat_id = category_exists($category_for_post); if (!$cat_id) { $cat_id = wp_create_category($category_for_post); } // b. post creation $created_post_status = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_created_post_status'); if ($created_post_status != 'publish') { $created_post_status = 'draft'; } $insert_photo_mode = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_insert_photo_mode'); if ($insert_photo_mode !== 'featured') { $insert_photo_mode = 'post_content'; } $post_type = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_insert_post_type', 'post'); $post_args = array('post_author' => 0, 'post_category' => array($cat_id), 'post_content' => $insta_caption, 'post_status' => 'draft', 'post_title' => $title_placeholder, 'post_type' => $post_type); // add comma separated tags to post, if specified $tag_to_add_to_post = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_tag_to_add_to_posts'); if (!empty($tag_to_add_to_post)) { $post_args['tags_input'] = $tag_to_add_to_post; } // INSERT checks about correct format... $created_post_ID = wp_insert_post($post_args); if (!$created_post_ID) { return array('error' => true, 'error_description' => WP_ROLL_INSTAGRAM_ERROR_INSTAGRAM_POST_CREATION_PROBLEM_MESSAGE, 'error_code' => WP_ROLL_INSTAGRAM_ERROR_INSTAGRAM_POST_CREATION_PROBLEM_CODE); } // c. add Instagram pic metadata to the just created post update_post_meta($created_post_ID, '_' . WP_ROLL_INSTAGRAM_PLUGIN_METADATA_PREFIX . '_insta_id', $insta_id); update_post_meta($created_post_ID, '_' . WP_ROLL_INSTAGRAM_PLUGIN_METADATA_PREFIX . '_insta_link', $insta_link); update_post_meta($created_post_ID, '_' . WP_ROLL_INSTAGRAM_PLUGIN_METADATA_PREFIX . '_insta_authorusername', $insta_author_username); update_post_meta($created_post_ID, '_' . WP_ROLL_INSTAGRAM_PLUGIN_METADATA_PREFIX . '_insta_authorid', $insta_author_id); // d. download image from Instagram and associate to post $photo_data = wpinstaroll_getInstagramPhotoDataFromInstaID($insta_id); // if we the image is already inside the media library, we get it from there, without actually downloading it from Instagram $image_info = null; if ($photo_data && $photo_data->media_id) { $image_info = wp_get_attachment_image_src($photo_data->media_id, 'full'); } if (!$image_info) { $tmp = download_url($insta_url); preg_match('/[^\\?]+\\.(jpg|jpe|jpeg|gif|png)/i', $insta_url, $insta_matches); $file_array = array('name' => basename($insta_matches[0]), 'tmp_name' => $tmp); if (is_wp_error($tmp)) { @unlink($file_array['tmp_name']); // delete just created post wp_delete_post($created_post_ID, true); return array('error' => true, 'error_description' => WP_ROLL_INSTAGRAM_ERROR_INSTAGRAM_IMAGE_DOWNLOAD_PROBLEM_MESSAGE, 'error_code' => WP_ROLL_INSTAGRAM_ERROR_INSTAGRAM_IMAGE_DOWNLOAD_PROBLEM_CODE); } $attach_id = media_handle_sideload($file_array, $created_post_ID); // see: what in case the same media file is added to multiple posts (as post content, but also as featured image) // http://core.trac.wordpress.org/browser/tags/3.3.2/wp-admin/includes/media.php (media_handle_sideload() code) // http://www.trovster.com/blog/2011/07/wordpress-custom-file-upload if (is_wp_error($attach_id)) { // remove uploaded temporary file @unlink($file_array['tmp_name']); // delete just created post wp_delete_post($created_post_ID, true); return array('error' => true, 'error_description' => WP_ROLL_INSTAGRAM_ERROR_INSTAGRAM_IMAGE_ADD_TO_POST_PROBLEM_MESSAGE, 'error_code' => WP_ROLL_INSTAGRAM_ERROR_INSTAGRAM_IMAGE_ADD_TO_POST_PROBLEM_CODE); } @unlink($file_array['tmp_name']); } else { $attach_id = $photo_data->media_id; } // update Instagram photo local data (better doing it as soon as we are sure we have the image, so // if next scheduled event occurs and the same image is present, it is less likely to be added again) wpinstaroll_updateInstagramPhotoStatus($insta_id, true, $attach_id); // SEE: another solution could be: update the status as soon as we get the image id and, only in case // there are problems getting the image and put it inside the media library, we set the status back to // non-published if ($insert_photo_mode === 'featured') { // attach to image as featured image (post thumbnail) add_post_meta($created_post_ID, '_thumbnail_id', $attach_id, true); } else { if (!$image_info) { $image_info = wp_get_attachment_image_src($attach_id, 'full'); } // insert the image inside the post, followed by post caption $update_post_data = array(); $update_post_data['ID'] = $created_post_ID; $update_post_data['post_content'] = '<img src="' . $image_info[0] . '" alt="' . strip_tags($insta_caption) . '" width="' . $image_info[1] . '" height="' . $image_info[2] . '"/><br/>' . $insta_caption; wp_update_post($update_post_data); } // the post is always created as draft and, if after post creation the image could actually be added and settings say the // post must be directly published, it is moved from 'draft' to 'published' if ($created_post_status == 'publish') { $update_post_data = array(); $update_post_data['ID'] = $created_post_ID; $update_post_data['post_status'] = 'publish'; wp_update_post($update_post_data); } return array('error' => false, 'post_id' => $created_post_ID); }