all repos — keyring-reactions-importer @ 00ac80b615dec4e0ded67d5dd1eaf628dc4a0753

importers/keyring-reactions-flickr.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
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
<?php

// a horrible hack borrowed from Beau Lebens
function Keyring_Flickr_Reactions() {

class Keyring_Flickr_Reactions extends Keyring_Reactions_Base {
	const SLUG              = 'flickr_reactions';
	const LABEL             = 'Flickr - comments and favorites';
	const KEYRING_SERVICE   = 'Keyring_Service_Flickr';
	const KEYRING_NAME      = 'flickr';
	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           = 'flickr.com';

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

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

	/**
	 * Accept the form submission of the Options page and handle all of the values there.
	 * You'll need to validate/santize things, and probably store options in the DB. When you're
	 * done, set $this->step = 'import' to continue, or 'options' to show the options form again.
	 *
 	function handle_request_options() {
		$bools = array('auto_import','auto_approve');

		foreach ( $bools as $bool ) {
			if ( isset( $_POST[$bool] ) )
				$_POST[$bool] = true;
			else
				$_POST[$bool] = false;
		}

		// If there were errors, output them, otherwise store options and start importing
		if ( count( $this->errors ) ) {
			$this->step = 'greet';
		} else {
			$this->set_option( array(
				'auto_import'     => (bool) $_POST['auto_import'],
				'auto_approve'    => (bool) $_POST['auto_approve'],
			) );

			$this->step = 'options';
		}
	}
	*/


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

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

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

		$photo_id = trim(end((explode('/', rtrim($syndication_url, '/')))));
		if (empty($photo_id))
			return new Keyring_Error(
				'keyring-flickr-reactions-photo-id-not-found',
				__( 'Cannot get photo ID out of syndication URL.', 'keyring' )
			);

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

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

	/**
	 * FAVS
	 */

	function get_favs ( $post_id, $photo_id ) {

		$results = $this->query_favs( $photo_id );

		if ($results && is_array($results) && !empty($results)) {

			$auto = ( $this->get_option( 'auto_approve' ) == 1 ) ? 1 : 0;
			$type = $this->methods[ 'favs' ];
			$tpl = __( '<a href="%s" rel="nofollow">%s</a> added this photo to their favorites on <a href="https://flickr.com" rel="nofollow">Flickr.com</a>','keyring');

			foreach ( $results as $element ) {

				$name = empty($element->realname) ? $element->username : $element->realname;
				// flickr is a bastard putting @ in the username...
				$email =  str_replace('@', '+', $element->nsid) .'@'. self::SILONAME;
				//	'http://static.flickr.com/'. $author['iconserver'] .'/buddyicons/'. $author['id'] .'.jpg';
				$avatar = '';
				if ( isset( $element->iconserver ) && $element->iconserver > 0 )
					$avatar = sprintf('http://static.flickr.com/%s/buddyicons/%s.jpg', $element->iconserver, $element->nsid );

				$author_url = 'https://www.flickr.com/people/' . $element->nsid;

				$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);
			}
		}
	}

	/**
	 *
	 */
	function query_favs ( $photo_id ) {

		$page = 1;
		$finished = false;
		$res = array();

		$baseurl = "https://api.flickr.com/services/rest/?";

		while (!$finished) {
			$params = array(
				'method'         => 'flickr.photos.getFavorites',
				'api_key'        => $this->service->key,
				'photo_id'       => $photo_id,
				'per_page'       => self::NUM_PER_REQUEST,
				'page'           => $page,
			);

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

			if ( Keyring_Util::is_error( $data ) )
				print $data;

			if (!empty($data->photo->person))
				foreach ($data->photo->person as $element )
					$res[] = $element;

			// jump to the next page or finish
			if ( ceil($data->photo->total / self::NUM_PER_REQUEST) > $page )
				$page += 1;
			else
				$finished = true;
		}

		return $res;
	}

	/**
	 * COMMENTS
	 */
	function get_comments ( $post_id, $photo_id ) {
		$results = $this->query_comments( $photo_id );

		if ($results && is_array($results) && !empty($results)) {

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

			foreach ( $results as $element ) {

				$name = empty($element->realname) ? $element->authorname : $element->realname;
				// flickr is a bastard putting @ in the username...
				$email =  str_replace('@', '+', $element->author) .'@'. self::SILONAME;
				//	'http://static.flickr.com/'. $author['iconserver'] .'/buddyicons/'. $author['id'] .'.jpg';
				$avatar = '';
				if ( isset( $element->iconserver ) && $element->iconserver > 0 )
					$avatar = sprintf('http://static.flickr.com/%s/buddyicons/%s.jpg', $element->iconserver, $element->nsid );

				$author_url = 'https://www.flickr.com/people/' . $element->author;

				$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->datecreate ),
					'comment_date_gmt'      => date("Y-m-d H:i:s", $element->datecreate ),
					'comment_agent'         => get_class($this),
					'comment_approved'      => $auto,
					'comment_content'       => $element->_content
				);

				if ( $comment_id = $this->insert_comment ( $post_id, $c, $element, $avatar))
					if (isset($element->permalink))
						update_comment_meta( $comment_id, 'permalink', $element->permalink );
			}
		}
	}

	/**
	 *
	 */
	function query_comments ( $photo_id ) {
		$res = array();

		$baseurl = "https://api.flickr.com/services/rest/?";

		// Flickr does not seem to support paged comment requests; easier for me...
		// https://www.flickr.com/services/api/flickr.photos.comments.getList.htm
		$params = array(
			'method'         => 'flickr.photos.comments.getList',
			'api_key'        => $this->service->key,
			'photo_id'       => $photo_id,
		);

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

		if ( Keyring_Util::is_error( $data ) )
			print $data;

		if (!empty($data->comments->comment))
			foreach ($data->comments->comment as $element )
				$res[] = $element;

		return $res;
	}

}}


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