From 6e9342aec244e30ceef333712423e551f07a1355 Mon Sep 17 00:00:00 2001 From: Peter Molnar Date: Fri, 13 Mar 2015 13:44:59 +0000 Subject: [PATCH] 0.2 - adding flickr --- importers/keyring-reactions-flickr.php | 255 +++++++++++++++++++++++++ keyring-reactions-importer.php | 9 +- readme.txt | 22 ++- 3 files changed, 278 insertions(+), 8 deletions(-) create mode 100644 importers/keyring-reactions-flickr.php diff --git a/importers/keyring-reactions-flickr.php b/importers/keyring-reactions-flickr.php new file mode 100644 index 0000000..90c9a6c --- /dev/null +++ b/importers/keyring-reactions-flickr.php @@ -0,0 +1,255 @@ +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 = __( '%s added this photo to their favorites on Flickr.com','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' ) + ); +} ); diff --git a/keyring-reactions-importer.php b/keyring-reactions-importer.php index 18c57d6..da5b919 100644 --- a/keyring-reactions-importer.php +++ b/keyring-reactions-importer.php @@ -3,7 +3,7 @@ Plugin Name: Keyring Reactions Importer Plugin URI: https://github.com/petermolnar/keyring-reactions-importer Description: A recations (comments, favs, etc. ) importer based on Keyring -Version: 0.1 +Version: 0.2 Author: Peter Molnar Author URI: http://petermolnar.eu/ License: GPLv3 @@ -844,9 +844,8 @@ abstract class Keyring_Reactions_Base { $this->posts = $posts; 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 )); $args = array ( @@ -969,7 +968,7 @@ abstract class Keyring_Reactions_Base { Keyring_Util::debug($r); - return true; + return $comment_id; } } diff --git a/readme.txt b/readme.txt index 92a9c62..809f28e 100644 --- a/readme.txt +++ b/readme.txt @@ -1,20 +1,36 @@ === Keyring Reactions Importer === Contributors: cadeyrn -Tags: 500px, backfeed, indieweb, comments, likes, favorites +Tags: flickr, 500px, backfeed, indieweb, comments, likes, favorites Requires at least: 3.0 Tested up to: 4.1 -Stable tag: 0.1 +Stable tag: 0.2 License: GPLv3 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 == +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 == == Changelog == += 0.2 = +*2015-03-13* + +* adding Flickr + = 0.1 = *2015-03-12*