From e894933928cf77c74644bb5f38a3d57f150f037d Mon Sep 17 00:00:00 2001 From: Peter Molnar Date: Fri, 13 Mar 2015 22:41:19 +0000 Subject: [PATCH] adding facebook --- importers/keyring-reactions-500px.php | 4 +- importers/keyring-reactions-facebook.php | 244 +++++++++++++++++++++++ importers/keyring-reactions-flickr.php | 2 +- keyring-reactions-importer.php | 14 +- readme.txt | 8 +- 5 files changed, 261 insertions(+), 11 deletions(-) create mode 100644 importers/keyring-reactions-facebook.php diff --git a/importers/keyring-reactions-500px.php b/importers/keyring-reactions-500px.php index bfa31de..7ecfaeb 100644 --- a/importers/keyring-reactions-500px.php +++ b/importers/keyring-reactions-500px.php @@ -74,7 +74,7 @@ class Keyring_500px_Reactions extends Keyring_Reactions_Base { __( 'Missing syndication URL.', 'keyring') ); - $photo_id = end((explode('/', rtrim($syndication_url, '/')))); + $photo_id = trim(end((explode('/', rtrim($syndication_url, '/'))))); if (empty($photo_id)) return new Keyring_Error( 'keyring-500px-reactions-photo-id-not-found', @@ -100,7 +100,7 @@ class Keyring_500px_Reactions extends Keyring_Reactions_Base { $baseurl = sprintf("https://api.500px.com/v1/photos/%s/votes", $photo_id); $res = $this->request ( $baseurl, 'users' ); - $tpl = __( '%s likes this photo on 500px.com','keyring'); + $tpl = __( '%s liked this photo on 500px.com','keyring'); $this->parser_fav_vote ( $post_id, $res, 'votes', $tpl ); diff --git a/importers/keyring-reactions-facebook.php b/importers/keyring-reactions-facebook.php new file mode 100644 index 0000000..e3e7794 --- /dev/null +++ b/importers/keyring-reactions-facebook.php @@ -0,0 +1,244 @@ +methods = array ( + // method name => comment type + 'likes' => 'like', + 'comments' => 'comment', + //'shares' => 'share' + ); + + //$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-facebook-reactions-missing-post-id', + __( 'Missing post ID to make request for.', 'keyring') + ); + + if (empty($syndication_url)) + return new Keyring_Error( + 'keyring-facebook-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-facebook-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-facebook-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_likes ( $post_id, $fb_id ) { + $res = array(); + $baseurl = sprintf( "https://graph.facebook.com/v%s/%s/likes?", static::GRAPHAPI, $fb_id ); + + $params = array( + 'access_token' => $this->service->token->token, + 'limit' => static::NUM_PER_REQUEST, + ); + + $starturl = $baseurl . http_build_query( $params ); + $results = $this->query($starturl); + + if ($results && is_array($results) && !empty($results)) { + + $auto = ( $this->get_option( 'auto_approve' ) == 1 ) ? 1 : 0; + $type = $this->methods[ 'likes' ]; + + foreach ( $results as $element ) { + + $avatar = sprintf('https://graph.facebook.com/%s/picture', $element->id ); + $author_url = 'https://facebook.com/' . $element->id; + $name = $element->name; + $tpl = __( '%s liked this entry on facebook','keyring'); + + $c = array ( + 'comment_author' => $name, + 'comment_author_url' => $author_url, + 'comment_author_email' => $element->id . '@' . static::SILONAME, + 'comment_post_ID' => $post_id, + 'comment_type' => $type, + // DON'T set the date unless it's provided - not with favs & votes + //'comment_date' => date("Y-m-d H:i:s"), + //'comment_date_gmt' => date("Y-m-d H:i:s"), + '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, $fb_id ) { + $res = array(); + $baseurl = sprintf( "https://graph.facebook.com/v%s/%s/comments?", static::GRAPHAPI, $fb_id ); + + $params = array( + 'access_token' => $this->service->token->token, + 'limit' => static::NUM_PER_REQUEST, + ); + + $starturl = $baseurl . http_build_query( $params ); + $results = $this->query($starturl); + + if ($results && is_array($results) && !empty($results)) { + + $auto = ( $this->get_option( 'auto_approve' ) == 1 ) ? 1 : 0; + $type = $this->methods[ 'comments' ]; + + foreach ( $results as $element ) { + $ctime = strtotime($element->created_time); + $author_url = 'https://facebook.com/' . $element->from->id; + $name = $element->from->name; + $avatar = sprintf('https://graph.facebook.com/%s/picture/?width=%s&height=%s', $element->from->id, get_option( 'thumbnail_size_w' ), get_option( 'thumbnail_size_h' )); + + $message = $element->message; + if ( isset($element->message_tags) && !empty($element->message_tags) && is_array($element->message_tags) ) { + foreach ( $element->message_tags as $tag ) { + $message = str_replace( $tag->name, sprintf('%s' , $tag->id, $tag->name), $message); + } + } + + $c = array ( + 'comment_author' => $name, + 'comment_author_url' => $author_url, + 'comment_author_email' => $element->from->id . '@' . static::SILONAME, + 'comment_post_ID' => $post_id, + 'comment_type' => $type, + 'comment_date' => date("Y-m-d H:i:s", $ctime), + 'comment_date_gmt' => date("Y-m-d H:i:s", $ctime), + 'comment_agent' => get_class($this), + 'comment_approved' => $auto, + 'comment_content' => $message, + ); + + $this->insert_comment($post_id, $c, $element, $avatar); + } + } + } + + /** + * + */ + function query ( $starturl ) { + + $nexurl = false; + $finished = false; + $res = array(); + + if (empty($starturl)) + return false; + + while (!$finished) { + + if (empty($nexturl) or !filter_var($nexturl, FILTER_VALIDATE_URL) ) { + $url = $starturl; + } + else { + $url = $nexturl; + } + + $data = $this->service->request( $url, array( 'method' => $this->request_method, 'timeout' => 10 ) ); + + if ( Keyring_Util::is_error( $data ) ) + return ($data); + + if (!empty($data->data)) + foreach ($data->data as $element ) + $res[] = $element; + + // jump to the next url or finish + if (isset($data->paging->next) && filter_var($data->paging->next, FILTER_VALIDATE_URL) ) + $nexturl = $data->paging->next; + else + $finished = true; + } + + return $res; + } + + +}} + + +add_action( 'init', function() { + Keyring_Facebook_Reactions(); // Load the class code from above + keyring_register_reactions( + Keyring_Facebook_Reactions::SLUG, + 'Keyring_Facebook_Reactions', + plugin_basename( __FILE__ ), + __( 'Import comments, likes and re-shares from Facebook as comments for your syndicated posts.', 'keyring' ) + ); +} ); diff --git a/importers/keyring-reactions-flickr.php b/importers/keyring-reactions-flickr.php index 90c9a6c..abafbdd 100644 --- a/importers/keyring-reactions-flickr.php +++ b/importers/keyring-reactions-flickr.php @@ -68,7 +68,7 @@ class Keyring_Flickr_Reactions extends Keyring_Reactions_Base { __( 'Missing syndication URL.', 'keyring') ); - $photo_id = end((explode('/', rtrim($syndication_url, '/')))); + $photo_id = trim(end((explode('/', rtrim($syndication_url, '/'))))); if (empty($photo_id)) return new Keyring_Error( 'keyring-flickr-reactions-photo-id-not-found', diff --git a/keyring-reactions-importer.php b/keyring-reactions-importer.php index da5b919..b812c3b 100644 --- a/keyring-reactions-importer.php +++ b/keyring-reactions-importer.php @@ -118,8 +118,9 @@ abstract class Keyring_Reactions_Base { } // Make sure we have a scheduled job to handle auto-imports if enabled + // but delay it for good reasons if ( $this->get_option( 'auto_import' ) && !wp_get_schedule( $this->schedule ) ) - wp_schedule_event( time(), static::SCHEDULE, $this->schedule ); + wp_schedule_event( time() + static::SCHEDULETIME, static::SCHEDULE, $this->schedule ); // jump to the first worker $this->handle_request(); @@ -186,7 +187,6 @@ abstract class Keyring_Reactions_Base { if (!is_object($id_or_email) || !isset($id_or_email->comment_type)) return $avatar; - // check if comment has an avatar $c_avatar = get_comment_meta($id_or_email->comment_ID, 'avatar', true); @@ -198,7 +198,7 @@ abstract class Keyring_Reactions_Base { else $safe_alt = esc_attr($alt); - return sprintf( '%s', $safe_alt, $c_avatar, $size ); + return sprintf( '%s', $safe_alt, $c_avatar, $size, $size ); } /** @@ -266,8 +266,10 @@ abstract class Keyring_Reactions_Base { protected function set_option( $name, $val = null ) { if ( is_array( $name ) ) $this->options = array_merge( (array) $this->options, $name ); - else if ( is_null( $name ) && is_null( $val ) ) // $name = null to reset all options + else if ( is_null( $name ) && is_null( $val ) ) { // $name = null to reset all options $this->options = array(); + wp_unschedule_event( time(), $this->schedule ); + } else if ( is_null( $val ) && isset( $this->options[ $name ] ) ) unset( $this->options[ $name ] ); else @@ -814,7 +816,7 @@ abstract class Keyring_Reactions_Base { function done() { $this->header(); echo '

' . __( 'All done!', 'keyring' ) . '

'; - echo '

' . sprintf( __( 'Check out all your new comments.', 'keyring' ), admin_url( 'comments.php' ) ) . '

'; + echo '

' . sprintf( __( 'Check out all your new comments.', 'keyring' ), admin_url( 'edit-comments.php' ) ) . '

'; $this->footer(); $this->cleanup(); do_action( 'import_done', $this->optname ); @@ -888,6 +890,8 @@ abstract class Keyring_Reactions_Base { */ function insert_comment ( &$post_id, &$comment, &$raw, &$avatar = '' ) { + $comment_id = false; + //test if we already have this imported $args = array( 'author_email' => $comment['comment_author_email'], diff --git a/readme.txt b/readme.txt index 809f28e..51b5397 100644 --- a/readme.txt +++ b/readme.txt @@ -1,6 +1,6 @@ === Keyring Reactions Importer === Contributors: cadeyrn -Tags: flickr, 500px, backfeed, indieweb, comments, likes, favorites +Tags: facebook, flickr, 500px, backfeed, indieweb, comments, likes, favorites Requires at least: 3.0 Tested up to: 4.1 Stable tag: 0.2 @@ -17,8 +17,9 @@ Using the `syndication_urls` post meta field populated by either the [Syndicatio Currently supported networks: -* [500px](http://500px.com/) - comments, favs, likes -* [Flickr](http://flickr.com/) - comments, favs +* [500px](https://500px.com/) - comments, favs, likes +* [Flickr](https://flickr.com/) - comments, favs +* [Facebook](https://facebook.com/) - comments, likes 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/). @@ -30,6 +31,7 @@ The plugin uses the brilliant [Keyring](https://wordpress.org/plugins/keyring/) *2015-03-13* * adding Flickr +* adding Facebook = 0.1 = *2015-03-12*