0.2 - adding flickr

This commit is contained in:
Peter Molnar 2015-03-13 13:44:59 +00:00
parent 3238e90a5c
commit 6e9342aec2
3 changed files with 278 additions and 8 deletions

View file

@ -0,0 +1,255 @@
<?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 = 'import';
}
}
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 = 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' )
);
} );

View file

@ -3,7 +3,7 @@
Plugin Name: Keyring Reactions Importer Plugin Name: Keyring Reactions Importer
Plugin URI: https://github.com/petermolnar/keyring-reactions-importer Plugin URI: https://github.com/petermolnar/keyring-reactions-importer
Description: A recations (comments, favs, etc. ) importer based on Keyring Description: A recations (comments, favs, etc. ) importer based on Keyring
Version: 0.1 Version: 0.2
Author: Peter Molnar <hello@petermolnar.eu> Author: Peter Molnar <hello@petermolnar.eu>
Author URI: http://petermolnar.eu/ Author URI: http://petermolnar.eu/
License: GPLv3 License: GPLv3
@ -844,9 +844,8 @@ abstract class Keyring_Reactions_Base {
$this->posts = $posts; $this->posts = $posts;
return true; return true;
} }
/*
* load this for test, in case you need it for a specific post only //load this for test, in case you need it for a specific post only
*/
//$raw = array ( get_post( 8180 )); //$raw = array ( get_post( 8180 ));
$args = array ( $args = array (
@ -969,7 +968,7 @@ abstract class Keyring_Reactions_Base {
Keyring_Util::debug($r); Keyring_Util::debug($r);
return true; return $comment_id;
} }
} }

View file

@ -1,20 +1,36 @@
=== Keyring Reactions Importer === === Keyring Reactions Importer ===
Contributors: cadeyrn Contributors: cadeyrn
Tags: 500px, backfeed, indieweb, comments, likes, favorites Tags: flickr, 500px, backfeed, indieweb, comments, likes, favorites
Requires at least: 3.0 Requires at least: 3.0
Tested up to: 4.1 Tested up to: 4.1
Stable tag: 0.1 Stable tag: 0.2
License: GPLv3 License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html License URI: http://www.gnu.org/licenses/gpl-3.0.html
A social reactions ( comments, like, favs, etc. ) importer based on [Keyring Social Importers](https://wordpress.org/plugins/keyring-social-importers/) and [Keyring](https://wordpress.org/plugins/keyring/) from [Beau Lebens](http://dentedreality.com.au/). A social reactions ( comments, like, favs, etc. ) importer.
== Description == == Description ==
A [backfeed](http://indiewebcamp.com/backfeed) to have all the reaction from all the social networks you have a copy of your post at!
Using the `syndication_urls` post meta field populated by either the [Syndication Links](https://wordpress.org/plugins/syndication-links/) plugin or by hand the Keyring Reactions Importer will pull all the comments, favs, likes, votes, etc. from any supported networks to comments in WordPress.
Currently supported networks:
* [500px](http://500px.com/) - comments, favs, likes
* [Flickr](http://flickr.com/) - comments, favs
The plugin uses the brilliant [Keyring](https://wordpress.org/plugins/keyring/) for handling networks and us based on [Keyring Social Importers](https://wordpress.org/plugins/keyring-social-importers/); both from [Beau Lebens](http://dentedreality.com.au/).
== Installation == == Installation ==
== Changelog == == Changelog ==
= 0.2 =
*2015-03-13*
* adding Flickr
= 0.1 = = 0.1 =
*2015-03-12* *2015-03-12*