Example #1
0
function wpinstaroll_photo_selection_panel_draw()
{
    // check for plugin requirements
    if (!wpinstaroll_check_requirements(true)) {
        wp_die('');
    }
    global $wpinstaroll_photo_selection_page_title;
    $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);
    }
    $insta_post_title = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_post_title_placeholder');
    if (empty($insta_post_title)) {
        $insta_post_title = WP_ROLL_INSTAGRAM_DEFAULT_TITLE_PLACEHOLDER;
        update_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_post_title_placeholder', $insta_post_title);
    }
    // not the requested access level
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }
    ?>

	<script type="text/javascript">

		var WPInstaroll_ActivePanel = 'user';

			function UserPhotosActivatePanel()
			{
				if (WPInstaroll_ActivePanel == 'tag')
				{
					jQuery('#InstaTagPhotos').removeClass('nav-tab-active');
					jQuery('#InstaUserPhotos').addClass('nav-tab-active');

					jQuery('#InstagramTagPhotosPanel').css('display', 'none');
					jQuery('#InstagramUserPhotosPanel').css('display', 'block');

					WPInstaroll_ActivePanel = 'user';

					AJAXDrawUserPhotosTable();
				}

				return false;
			}
			function TagPhotosActivatePanel()
			{
				if (WPInstaroll_ActivePanel == 'user')
				{
					jQuery('#InstaUserPhotos').removeClass('nav-tab-active');
					jQuery('#InstaTagPhotos').addClass('nav-tab-active');

					jQuery('#InstagramUserPhotosPanel').css('display', 'none');
					jQuery('#InstagramTagPhotosPanel').css('display', 'block');

					WPInstaroll_ActivePanel = 'tag';

					AJAXDrawTagPhotosTable();
				}

				return false;
			}

		function AJAXDrawUserPhotosTable()
		{
			jQuery('#InstagramUserPhotosPanel').html('<p>loading...</p>');

			jQuery.ajax({
				url: ajaxurl + '?action=wpinstaroll_photosbyusertable',
				success: function(data) {

					jQuery('#InstagramUserPhotosPanel').html(data);
				}
			});
		}

		function AJAXDrawTagPhotosTable()
		{
			jQuery('#InstagramTagPhotosPanel').html('<p>loading...</p>');

			jQuery.ajax({
				url: ajaxurl + '?action=wpinstaroll_photosbytagtable',
				success: function(data) {

					jQuery('#InstagramTagPhotosPanel').html(data);
				}
			});
		}

		jQuery(document).ready(function() {

			// open first panel: user stream
			AJAXDrawUserPhotosTable();

		});

	</script>

	<div class="wrap">

		<div id="icon-options-general" class="icon32 instaroll"><br /></div>
		<h2><?php 
    print $wpinstaroll_photo_selection_page_title;
    ?>
</h2>

		<h3 class="nav-tab-wrapper">
			<a id="InstaUserPhotos" class="nav-tab nav-tab-active" href="#" onclick="return UserPhotosActivatePanel()"><?php 
    echo WP_ROLL_INSTAGRAM_PHOTOS_TABS_USER;
    ?>
</a>
			<a id="InstaTagPhotos" class="nav-tab" href="#" onclick="return TagPhotosActivatePanel()"><?php 
    echo WP_ROLL_INSTAGRAM_PHOTOS_TABS_TAG;
    ?>
</a>
		</h3>

		<div id="InstagramUserPhotosPanel"></div>

		<div id="InstagramTagPhotosPanel" style="display: none"></div>

		<script type="text/javascript">

			jQuery('#Instagram_userphotosupdate').live('click', function() {

				AJAXDrawUserPhotosTable();

				return false;
			});

			jQuery('#Instagram_tagphotosupdate').live('click', function() {

				AJAXDrawTagPhotosTable();

				return false;
			});


			jQuery("#show_useronly_userpanel").live('click', function() {

				var status;
				if (jQuery(this).attr('checked') == 'checked')
					status = 'show_useronly';
				else
					status = 'show_userandfriends';

				jQuery.ajax({
					url: ajaxurl + '?action=set_instagram_show_useronly_flag',
					type: 'POST',
					data: {
						show: status,
					},
					success: function() {

						// reload the view after setting the flag
						AJAXDrawUserPhotosTable();
					}
				});

				return true;
			});


			jQuery("#show_useronly_tagpanel").live('click', function() {

				var status;
				if (jQuery(this).attr('checked') == 'checked')
					status = 'show_useronly_by_tag';
				else
					status = 'show_all_by_tag';

				jQuery.ajax({
					url: ajaxurl + '?action=set_instagram_show_useronly_by_tag_flag',
					type: 'POST',
					data: {
						show: status,
					},
					success: function() {

						// reload the view after setting the flag
						AJAXDrawTagPhotosTable();
					}
				});

				return true;
			});


			jQuery("#show_already_published_userpanel").live('click', function() {

				var status;
				if (jQuery(this).attr('checked') == 'checked')
					status = 'show_published';
				else
					status = 'dont_show_published';

				jQuery.ajax({
					url: ajaxurl + '?action=set_instagram_show_published_flag',
					type: 'POST',
					data: {
						show: status,
					},
					success: function() {

						// reload the view after setting the flag
						AJAXDrawUserPhotosTable();
					}
				});

				return true;
			});

			jQuery("#show_already_published_tagpanel").live('click', function() {

				var status;
				if (jQuery(this).attr('checked') == 'checked')
					status = 'show_published';
				else
					status = 'dont_show_published';

				jQuery.ajax({
					url: ajaxurl + '?action=set_instagram_show_published_flag',
					type: 'POST',
					data: {
						show: status,
					},
					success: function() {

						// reload the view after setting the flag
						AJAXDrawTagPhotosTable();
					}
				});

				return true;
			});

			jQuery('.<?php 
    echo WP_ROLL_INSTAGRAM_PLUGIN_PREFIX;
    ?>
_createpost_action').live('click', function() {

				var t_id = jQuery(this).attr('id');
				var pic_id = t_id.substr('create_wp_post_'.length);


				var postCreationString = '<?php 
    echo get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_created_post_status');
    ?>
';
				if (postCreationString != 'publish')
					postCreationString = 'saved as draft.\n\nYou will then be able to edit and actually publish the post.';
				else
					postCreationString = 'directly published.';

				var postCreationModeString = '<?php 
    echo get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_insert_photo_mode');
    ?>
';
				if (postCreationModeString !== 'featured')
					postCreationModeString = '\n\nThe photo will be inserted into the created post.';
				else
					postCreationModeString = '\n\nThe photo will be added as featured image for created post.';

				if (!window.confirm('Do you want to create a post from the Instagram image with ID: ' + pic_id + '?' +
					'\n\nA new post will be created with category \"' + '<?php 
    print $category_for_post;
    ?>
\" and title \"' + '<?php 
    print $insta_post_title;
    ?>
' + '\", and will be ' + postCreationString + postCreationModeString))
					return false;


				jQuery.ajax({
					url: ajaxurl + '?action=create_post_from_instagram_pic',
					type: 'POST',
					dataType: 'json',
					data: {
						url: jQuery(this).closest('tr').find('.insta_image a img').attr('data-fullimageurl'),
						id: pic_id,
						link: jQuery(this).closest('tr').find('.insta_image a').attr('href'),
						caption: jQuery(this).closest('tr').find('.insta_description').html(),
						author_username: jQuery(this).closest('tr').find('.insta_username').html(),
						author_id: jQuery(this).closest('tr').find('.insta_userid').html()
					},
					success: function(data) {

						if (data.error == false)
						{
							alert('Post successfully created, width ID: ' + data.post_id);
						}
						else
							alert('There was a problem creating the post.\n\nReason: ' + data.error_description);
					}
				});

				return false;
			});

		</script>

	</div>

	<?php 
}
Example #2
0
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);
                }
            }
        }
    }
}