all repos — keyring-reactions-importer @ 46e6d74d6748cb60d44fd4defabf06c6acc69a7f

importers/keyring-reactions-instagram.php (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
<?php

function Keyring_Instagram_Reactions() {

class Keyring_Instagram_Reactions extends Keyring_Reactions_Base {
	const SLUG              = 'instagram_reactions';
	const LABEL             = 'Instagram - comments and likes';
	const KEYRING_SERVICE   = 'Keyring_Service_Instagram';
	const KEYRING_NAME      = 'instagram';
	const REQUESTS_PER_LOAD = 3;     // How many remote requests should be made before reloading the page?
	const NUM_PER_REQUEST   = 50;     // Number of images per request to ask for

	const SILONAME           = 'instagram.com';

	function __construct() {
		$this->methods = array (
			// method name => comment type
			'likes'  => 'like',
			'comments' => 'comment'
		);

		//$this->methods = array ('votes', 'favs', 'comments');
		parent::__construct();
	}

	function make_all_requests( $method, $post ) {
		extract($post);

		if (empty($post_id))
			return new Keyring_Error(
				'keyring-instagram-reactions-missing-post-id',
				__( 'Missing post ID to make request for.', 'keyring')
			);

		if (empty($syndication_url))
			return new Keyring_Error(
				'keyring-instagram-reactions-missing-syndication-url',
				__( 'Missing syndication URL.', 'keyring')
			);

		// get media id here
		$api = file_get_contents(sprintf('https://api.instagram.com/oembed?url=%s', $syndication_url));
		$apiObj = json_decode($api,true);
		if ( !isset($apiObj['media_id']) || empty($apiObj['media_id']))
			return new Keyring_Error(
				'keyring-instagram-reactions-photo-id-not-found',
				__( 'Cannot get photo ID out of syndication URL.', 'keyring' )
			);

		$silo_id = $apiObj['media_id'];

		$func = 'get_' . $method;
		if ( !method_exists( $this, $func ) )
			return new Keyring_Error(
				'keyring-instagram-reactions-missing-func',
				sprintf(__( 'Function is missing for this method (%s), cannot proceed!', 'keyring'), $method)
			);

		return $this->$func ( $post_id, $silo_id );
	}

	/**
	 * LIKES
	 */

	function get_likes ( $post_id, $silo_id ) {
		$baseurl = sprintf ('https://api.instagram.com/v1/media/%s/likes?', $silo_id );

		$params = array(
			'access_token'   => $this->service->token->token,
		);

		$url = $baseurl . http_build_query( $params );
		$data = $this->service->request( $url, array( 'method' => $this->request_method, 'timeout' => 10 ) );

		if ( Keyring_Util::is_error( $data ) )
			Keyring_Util::Debug (json_encode($data));

		if ($data->data && is_array($data->data) && !empty($data->data)) {

			$auto = ( $this->get_option( 'auto_approve' ) == 1 ) ? 1 : 0;
			$type = $this->methods[ 'likes' ];
			$tpl = __( '<a href="%s" rel="nofollow">%s</a> liked this entry on <a href="https://instagram.com" rel="nofollow">instagram</a>','keyring');

			foreach ( $data->data as $element ) {

				$name = empty($element->full_name) ? $element->username : $element->full_name;
				$email =  $element->username .'@'. self::SILONAME;
				$avatar = empty ($element->profile_picture) ? '' : $element->profile_picture;
				$author_url = 'https://instagram.com/' . $element->username;

				$c = array (
					'comment_author'        => $name,
					'comment_author_url'    => $author_url,
					'comment_author_email'  => $email,
					'comment_post_ID'       => $post_id,
					'comment_type'          => $type,
					//'comment_date'          => date("Y-m-d H:i:s", $element->favedate ),
					//'comment_date_gmt'      => date("Y-m-d H:i:s", $element->favedate ),
					'comment_agent'         => get_class($this),
					'comment_approved'      => $auto,
					'comment_content'       => sprintf( $tpl, $author_url, $name ),
				);

				$this->insert_comment ( $post_id, $c, $element, $avatar);
			}
		}
	}

	/**
	 * COMMENTS
	 */

	function get_comments ( $post_id, $silo_id ) {
		$baseurl = sprintf ('https://api.instagram.com/v1/media/%s/comments?', $silo_id );

		$params = array(
			'access_token'   => $this->service->token->token,
		);

		$url = $baseurl . http_build_query( $params );
		$data = $this->service->request( $url, array( 'method' => $this->request_method, 'timeout' => 10 ) );

		if ( Keyring_Util::is_error( $data ) )
			Keyring_Util::Debug (json_encode($data));


		if ($data->data && is_array($data->data) && !empty($data->data)) {

			$auto = ( $this->get_option( 'auto_approve' ) == 1 ) ? 1 : 0;
			$type = $this->methods[ 'comments' ];

			foreach ( $data->data as $element ) {

				$name = empty($element->from->full_name) ? $element->from->username : $element->from->full_name;
				$email =  $element->from->username .'@'. self::SILONAME;
				$avatar = empty ($element->from->profile_picture) ? '' : $element->from->profile_picture;
				$author_url = 'https://instagram.com/' . $element->from->username;

				$c = array (
					'comment_author'        => $name,
					'comment_author_url'    => $author_url,
					'comment_author_email'  => $email,
					'comment_post_ID'       => $post_id,
					'comment_type'          => $type,
					'comment_date'          => date("Y-m-d H:i:s", $element->created_time ),
					'comment_date_gmt'      => date("Y-m-d H:i:s", $element->created_time ),
					'comment_agent'         => get_class($this),
					'comment_approved'      => $auto,
					'comment_content'       => $element->text
				);

				$this->insert_comment ( $post_id, $c, $element, $avatar);
			}
		}
	}

}}


add_action( 'init', function() {
	Keyring_Instagram_Reactions(); // Load the class code from above
	keyring_register_reactions(
		Keyring_Instagram_Reactions::SLUG,
		'Keyring_Instagram_Reactions',
		plugin_basename( __FILE__ ),
		__( 'Import comments and likes from Instagram for your syndicated posts.', 'keyring' )
	);
} );