Author URI: http://petermolnar.eu/ License: GPLv3 */ /* This plugin could never had been done without [Keyring Social Importers](https://wordpress.org/plugins/keyring-social-importers/) and [Keyring](https://wordpress.org/plugins/keyring/) from [Beau Lebens](http://dentedreality.com.au/). Thank you! */ /* Copyright 2010-2014 Peter Molnar ( hello@petermolnar.eu ) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ /* * TODO // IDEAS * * instead of one gigantic importer, we could fire up a schedule per host * to import all the reactions from all the networks for that particular post * the question is, how good is the schedule in wp-cron, could it handle * thousands of schedules? * */ // Load Importer API if ( !function_exists( 'register_importer ' ) ) require_once ABSPATH . 'wp-admin/includes/import.php'; abstract class Keyring_Reactions_Base { // Make sure you set all of these in your importer class const SLUG = ''; // should start with letter & should only contain chars valid in javascript function name const LABEL = ''; // the line that will show up in Import page const KEYRING_NAME = ''; // Keyring service name; SLUG is not used to avoid conflict with Keyring Social Importer const KEYRING_SERVICE = ''; // Full class name of the Keyring_Service this importer requires const REQUESTS_PER_LOAD = 3; // How many posts should queried before moving over to the next branch / reload the page const REQUESTS_PER_AUTO = 16; const KEYRING_VERSION = '1.4'; // Minimum version of Keyring required const SILONAME = ''; // identifier for the silo in the syndication_url field entry const OPTNAME_POSTPOS = 'post_todo'; // options key for next post id in posts array const OPTNAME_POSTS = 'posts'; // option key for posts array const SCHEDULE = 'daily'; // this may break many things, careful if you wish to change it const SCHEDULETIME = 36400; // in tandem with the previous const RESCHEDULE = 60; // You shouldn't need to edit (or override) these ones var $step = 'greet'; var $service = false; var $token = false; var $finished = false; var $options = array(); var $posts = array(); var $errors = array(); var $request_method = 'GET'; var $optname = ''; var $methods = array(); // method name for functions => comment type to store with var $schedule = ''; public function __construct() { // Can't do anything if Keyring is not available. // Prompt user to install Keyring (if they can), and bail if ( !defined( 'KEYRING__VERSION' ) || version_compare( KEYRING__VERSION, static::KEYRING_VERSION, '<' ) ) { if ( current_user_can( 'install_plugins' ) ) { add_thickbox(); wp_enqueue_script( 'plugin-install' ); add_filter( 'admin_notices', array( &$this, 'require_keyring' ) ); } return false; } // Set some internal vars $this->optname = 'keyring-' . static::SLUG; $this->schedule = $this->optname . '_import_auto'; // Populate options for this importer $this->options = get_option( $this->optname ); // Add a Keyring handler to push us to the next step of the importer once connected add_action( 'keyring_connection_verified', array( &$this, 'verified_connection' ), 10, 2 ); // additional comment types add_action('admin_comment_types_dropdown', array(&$this, 'comment_types_dropdown')); // ... add_filter('get_avatar_comment_types', array( &$this, 'add_comment_types')); // additional cron schedules add_filter( 'cron_schedules', array(&$this, 'cron' )); // additional avatar filter add_filter( 'get_avatar' , array(&$this, 'get_avatar'), 1, 5 ); // If a request is made for a new connection, pass it off to Keyring if ( ( isset( $_REQUEST['import'] ) && static::SLUG == $_REQUEST['import'] ) && ( ( isset( $_POST[ static::SLUG . '_token' ] ) && 'new' == $_POST[ static::SLUG . '_token' ] ) || isset( $_POST['create_new'] ) ) ) { $this->reset(); Keyring_Util::connect_to( static::KEYRING_NAME, $this->optname ); exit; } // If we have a token set already, then load some details for it if ( $this->get_option( 'token' ) && $token = Keyring::get_token_store()->get_token( array( 'service' => static::KEYRING_NAME, 'id' => $this->get_option( 'token' ) ) ) ) { $this->service = call_user_func( array( static::KEYRING_SERVICE, 'init' ) ); $this->service->set_token( $token ); } // jump to the first worker $this->handle_request(); } /** * Singleton mode on */ static public function &init() { static $instance = false; if ( !$instance ) { $class = get_called_class(); $instance = new $class; } return $instance; } /** * Extend the "filter by comment type" of in the comments section * of the admin interface with all of our methods * * @param array $types the different comment types * * @return array the filtered comment types */ public function comment_types_dropdown($types) { foreach ($this->methods as $method => $type ) { if (!isset($types[ $type ])) $types[ $type ] = ucfirst( $type ); } return $types; } /** * */ public function add_comment_types ( $types ) { foreach ($this->methods as $method => $type ) { if (!in_array( $type, $types )) array_push( $types, $type ); } return $types; } /** * add our own, ridiculously intense schedule for chanining all the requests * wee need for the imports * * @param array $schedules the current schedules in WP CRON * * @return array the filtered WP CRON schedules */ public function cron ( $schedules ) { /* if (!isset($schedules[ $this->optname ])) { $schedules[ $this->optname ] = array( 'interval' => static::RESCHEDULE, 'display' => sprintf(__( '%s auto import' ), static::SLUG ) ); } */ return $schedules; } /** * of there is a comment meta 'avatar' field, use that as avatar for the commenter * * @param string $avatar the current avatar image string * @param mixed $id_or_email this could be anything that triggered the avatar all * @param string $size size for the image to display * @param string $default optional fallback * @param string $alt alt text for the avatar image */ public static function get_avatar($avatar, $id_or_email, $size, $default = '', $alt = '') { 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); if (!$c_avatar) return $avatar; if (false === $alt) $safe_alt = ''; else $safe_alt = esc_attr($alt); return sprintf( '', $safe_alt, $c_avatar, $size, $size ); } /** * 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. */ protected function handle_request_options() { $auto_import = (isset( $_POST['auto_import']) && !empty($_POST['auto_import'])) ? true : false; $auto_approve = (isset( $_POST['auto_approve']) && !empty($_POST['auto_approve'])) ? true : false; /* if ( $this->get_option('auto_import') && !wp_get_schedule( $this->schedule ) ) { wp_schedule_event( time() + static::SCHEDULETIME, static::SCHEDULE, $this->schedule ); } elseif ( $this->get_option('auto_import') && wp_get_schedule( $this->schedule != static::SCHEDULE ) ) { wp_clear_scheduled_hook ( $this->schedule ); wp_schedule_event( time() + static::SCHEDULETIME, static::SCHEDULE, $this->schedule ); } elseif ( !$this->get_option('auto_import') ) { Keyring_Util::Debug('Ez most fut?'); wp_clear_scheduled_hook ( $this->schedule ); } */ wp_clear_scheduled_hook ( $this->schedule ); if ($auto_import) { wp_schedule_event( time() + static::SCHEDULETIME, static::SCHEDULE, $this->schedule ); } else { $this->cleanup(); } // 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' => $auto_import, 'auto_approve' => $auto_approve, 'limit_posts' => sanitize_text_field($_POST['limit_posts']), ) ); $this->step = 'done'; } } /** * This step will do all the required calls for a specific method for a * specific post, parse them and insert them into the DB as comments. * * @param string $method the method to call and work with (eg. favs, comments) * @param post $post WP Post object * * @return None */ abstract protected function make_all_requests( $method, $post ); /** * Warn the user that they need Keyring installed and activated. */ protected function require_keyring() { global $keyring_required; // So that we only send the message once if ( 'update.php' == basename( $_SERVER['REQUEST_URI'] ) || $keyring_required ) return; $keyring_required = true; echo '
'; printf( __( 'The Keyring Recations Importers plugin package requires the %1$s plugin to handle authentication. Please install it by clicking the button below, or activate it if you have already installed it, then you will be able to use the importers.', 'keyring' ), 'Keyring' ); echo '
'; echo ''; echo '' . __( 'Importing Reactions...' ) . '
'; $syndication_url = false; // Need a token to do anything with this if ( !$this->service->get_token() ) return; require_once ABSPATH . 'wp-admin/includes/import.php'; require_once ABSPATH . 'wp-admin/includes/post.php'; require_once ABSPATH . 'wp-admin/includes/comment.php'; $post = get_post($post_id); if (!$post) return false; $syndication_urls = get_post_meta ( $post->ID, 'syndication_urls', true ); if (strstr( $syndication_urls, static::SILONAME )) { $syndication_urls = explode("\n", $syndication_urls ); foreach ( $syndication_urls as $url ) { if (strstr( $url, static::SILONAME )) { $syndication_url = $url; } } } if (!$syndication_url) return false; $todo = array ( 'post_id' => $post->ID, 'syndication_url' => $syndication_url, ); $msg = sprintf(__('Starting auto import for #%s', 'keyring'), $post->ID ); Keyring_Util::debug($msg); foreach ( $this->methods as $method => $type ) { $msg = sprintf(__('Processing %s for post #%s', 'keyring'), $method, $post->ID); Keyring_Util::debug($msg); $result = $this->make_all_requests( $method, $todo ); if ( Keyring_Util::is_error( $result ) ) print $result; } $this->importer_goto( 'done', 1 ); $this->footer(); do_action( 'import_end' ); return true; } /** * Handle a cron request to pick up importing reactions from where we left off. * Since the posts array & the post pointer stays untouched until the import * job if finished, we'll just continuing the import for the next post to process. * * We cannot do the whole import in one batch - there could be a massive amount * of posts to check reactions for - so we reschedule the job to start * immediately after eachother. * We're also not using the batch mode (X posts per page load) but instead * one-by-one so one iteration of the WP CRON event will not take that long * and may not cause issues later on. */ public function do_auto_import( ) { defined( 'WP_IMPORTING' ) or define( 'WP_IMPORTING', true ); do_action( 'import_start' ); set_time_limit( 0 ); Keyring_Util::debug( static::SLUG . sprintf(' auto import: init')); // In case auto-import has been disabled, clear all jobs and bail if ( !$this->get_option( 'auto_import' ) ) { Keyring_Util::debug( static::SLUG . sprintf(' auto import: clearing hook')); wp_clear_scheduled_hook( 'keyring_' . static::SLUG . '_import_auto' ); return; } // Need a token to do anything with this if ( !$this->service->get_token() ) return; require_once ABSPATH . 'wp-admin/includes/import.php'; require_once ABSPATH . 'wp-admin/includes/post.php'; require_once ABSPATH . 'wp-admin/includes/comment.php'; $next = 0; $position = 0; $num = 0; $this->get_posts(); while ( !$this->finished && $num < static::REQUESTS_PER_AUTO ) { $position = $this->get_option( static::OPTNAME_POSTPOS, 0); if ( !is_array($this->posts) || !isset($this->posts[$position]) ) return new Keyring_Error( 'keyring-reactions-post-not-set', __( 'The post to work with does not exist in the posts array. Something is definitely wrong.', 'keyring' ) ); $todo = $this->posts[$position]; Keyring_Util::debug( static::SLUG . sprintf(' auto import: doing %s/%s', $position, count($this->posts)-1 )); //$msg = sprintf(__('Starting auto import for #%s', 'keyring'), $todo['post_id']); //Keyring_Util::debug($msg); foreach ( $this->methods as $method => $type ) { $msg = sprintf(__('Processing %s for post #%s', 'keyring'), $method, $todo['post_id']); Keyring_Util::debug($msg); $result = $this->make_all_requests( $method, $todo ); if ( Keyring_Util::is_error( $result ) ) print $result; } $next = $position+1; $num++; // we're done, clean up if ( $next >= count($this->posts) ) { $this->finished = true; break; } else { $this->set_option( static::OPTNAME_POSTPOS, $next ); } } Keyring_Util::debug( sprintf ('%s auto import: current batch finised (%s to %s out of %s )', static::SLUG, (int) ($position - static::REQUESTS_PER_AUTO), $position, count($this->posts)-1 )); if ( $this->finished || $next >= count($this->posts) ) { Keyring_Util::debug( sprintf ('%s auto import: FINISHED', static::SLUG)); $this->cleanup(); do_action( 'keyring_import_done', $this->optname ); } else { Keyring_Util::debug( sprintf ('%s auto import: Rescheduling event', static::SLUG)); wp_schedule_single_event( time() + static::RESCHEDULE, $this->schedule ); } do_action( 'import_end' ); } /** * Hooked into ::dispatch(), this just handles triggering the import and then dealing with * any value returned from it. */ function do_import() { set_time_limit( 0 ); $res = $this->import(); if ( true !== $res ) { echo ''; if ( Keyring_Util::is_error( $res ) ) { $http = $res->get_error_message(); // The entire HTTP object is passed back if it's an error if ( 400 == wp_remote_retrieve_response_code( $http ) ) { printf( __( "Received an error from %s. Please wait for a while then try again.", 'keyring' ), static::LABEL ); } else if ( in_array( wp_remote_retrieve_response_code( $http ), array( 502, 503 ) ) ) { printf( __( "%s is currently experiencing problems. Please wait for a while then try again.", 'keyring' ), static::LABEL ); } else { // Raw dump, sorry echo '
' . sprintf( __( "We got an unknown error back from %s. This is what they said.", 'keyring' ), static::LABEL ) . '
'; $body = wp_remote_retrieve_body( $http ); echo ''; print_r( $body ); echo ''; } } else { _e( 'Something went wrong. Please try importing again in a few minutes (your details have been saved and the import will continue from where it left off).', 'keyring' ); } echo '
' . __( 'Importing Reactions...' ) . '
'; $this->get_posts(); while ( !$this->finished && $num < static::REQUESTS_PER_LOAD ) { echo "";
$position = $this->get_option( static::OPTNAME_POSTPOS, 0);
if ( !is_array($this->posts) || !isset($this->posts[$position]) ) {
$this->cleanup();
return new Keyring_Error(
'keyring-reactions-post-not-set',
__( 'The post to work with does not exist in the posts array. Something is definitely wrong. I\'m resetting myself now, please try importing again.', 'keyring' )
);
}
$todo = $this->posts[$position];
foreach ( $this->methods as $method => $type ) {
$msg = sprintf(__('Processing %s for post #%s
', 'keyring'), $method, $todo['post_id']);
Keyring_Util::debug($msg);
echo $msg;
$result = $this->make_all_requests( $method, $todo );
if ( Keyring_Util::is_error( $result ) )
print_r ($result);
}
echo "