function wpinstaroll_automatic_post_creation() { // check for plugin requirements (without echoing error messages, just logging them) if (!wpinstaroll_check_requirements()) { wp_die(''); } $InstagramClientID = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_app_id'); $InstagramClientSecret = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_app_secret'); $user_access_token = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_user_accesstoken'); $search_tag = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_search_tag'); $scheduled_publication_period = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_scheduled_publication_period'); $scheduled_publication_stream = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_scheduled_publication_stream'); // is Instagram properly configured? // // if not, first reset event scheduling settings and removes event schedulation, then simply exits if (empty($InstagramClientID) || empty($InstagramClientSecret) || empty($user_access_token) || empty($scheduled_publication_period) || empty($scheduled_publication_stream)) { wpinstaroll_remove_scheduled_event(); update_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_scheduled_publication_period', 'never'); exit; } // inclusion of functions included only when opening WordPress backend require_once 'wp-admin/includes/admin.php'; // force current user to user with id == 1 (that should be an admin) wp_set_current_user(1); // retrieval of photos and post creation for new ones // user stream if ($scheduled_publication_stream == 'user' || $scheduled_publication_stream == 'user_tag') { $photoStream = wpinstaroll_getInstagramUserStream(); $data = $photoStream->data; if ($data) { // reverse the array, so that oldest photos are processed first $data = array_reverse($data); // ids of already published photos $published_ids = wpinstaroll_getInstagramPublishedPhotosIDs(); // scan the stream and publish new photos foreach ($data as $element) { // if the photo has not been published yet if (!in_array($element->id, $published_ids)) { wpinstaroll_createpostfromphoto($element->id, $element->images->standard_resolution->url, $element->link, $element->caption->text, $element->user->username, $element->user->id); } } } } // tag stream - only in this case, we check for search tag presence if (($scheduled_publication_stream == 'tag' || $scheduled_publication_stream == 'user_tag') && !empty($search_tag)) { $photoStream = wpinstaroll_getInstagramPhotosWithTag($search_tag); $data = $photoStream->data; if ($data) { $data = array_reverse($data); $published_ids = wpinstaroll_getInstagramPublishedPhotosIDs(); foreach ($data as $element) { if (!in_array($element->id, $published_ids)) { wpinstaroll_createpostfromphoto($element->id, $element->images->standard_resolution->url, $element->link, $element->caption->text, $element->user->username, $element->user->id); } } } } }
function wpinstaroll_createpostfromphoto_ajax() { $response = ''; if (!isset($_POST['id']) || !isset($_POST['url']) || !isset($_POST['link'])) { $response = array('error' => true, 'error_description' => WP_ROLL_INSTAGRAM_ERROR_MISSING_PARAMETERS_MESSAGE, 'error_code' => WP_ROLL_INSTAGRAM_ERROR_MISSING_PARAMETERS_CODE); } else { $response = wpinstaroll_createpostfromphoto($_POST['id'], $_POST['url'], $_POST['link'], $_POST['caption'], $_POST['author_username'], $_POST['author_id']); } echo json_encode($response); exit; // accessible with URL: // http://[HOST]/wp-admin/admin-ajax.php?action=create_post_from_instagram_pic }