Skip to content
Snippets Groups Projects
  • Mohammad Jangda's avatar
    28aa0e16
    Syndicate thumbnails for XMLRPC Push · 28aa0e16
    Mohammad Jangda authored
    * Remove old thumbnail cruft
    * Replace with new hotness to properly syndicate thumbnails attached to posts (requires Syndication plugin be active on both sites as it uses a custom endpoint)
    * Remove internal success and error tracking for clients (this was causing problems with the ability to do subsequent XMLRPC calls: edit/new post then send thumbnail)
    * Client actions should now return an ID, bool, or WP_Error
    
    fixes #33
    28aa0e16
    History
    Syndicate thumbnails for XMLRPC Push
    Mohammad Jangda authored
    * Remove old thumbnail cruft
    * Replace with new hotness to properly syndicate thumbnails attached to posts (requires Syndication plugin be active on both sites as it uses a custom endpoint)
    * Remove internal success and error tracking for clients (this was causing problems with the ability to do subsequent XMLRPC calls: edit/new post then send thumbnail)
    * Client actions should now return an ID, bool, or WP_Error
    
    fixes #33
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
class-syndication-wp-rest-client.php 7.77 KiB
<?php

include_once(dirname(__FILE__) . '/interface-syndication-client.php');
include_once( dirname( __FILE__ ) . '/push-syndicate-encryption.php' );

class Syndication_WP_REST_Client implements Syndication_Client {

	private $access_token;
	private $blog_ID;

	private $port;
	private $useragent;
	private $timeout;

	function __construct( $site_ID, $port = 80, $timeout = 45 ) {

		$this->access_token = push_syndicate_decrypt( get_post_meta( $site_ID, 'syn_site_token', true) );
		$this->blog_ID	  = get_post_meta( $site_ID, 'syn_site_id', true);
		$this->timeout	  = $timeout;
		$this->useragent	= 'push-syndication-plugin';
		$this->port		 = $port;

	}

	public static function get_client_data() {
		return array( 'id' => 'WP_REST', 'modes' => array( 'push' ), 'name' => 'WordPress.com REST' );
	}
	
	public function new_post( $post_ID ) {

		$post = (array)get_post( $post_ID );

		// This filter can be used to exclude or alter posts during a content push
		$post = apply_filters( 'syn_rest_push_filter_new_post', $post, $post_ID );
		if ( false === $post )
			return true;
		
		$response = wp_remote_post( 'https://public-api.wordpress.com/rest/v1/sites/' . $this->blog_ID . '/posts/new/', array(
			'timeout'	   => $this->timeout,
			'user-agent'	=> $this->useragent,
			'sslverify'	 => false,
			'headers'	   => array (
				'authorization' => 'Bearer ' . $this->access_token,
				'Content-Type'  => 'application/x-www-form-urlencoded'
			),
			'body' => array (
				'title'		 => $post['post_title'],
				'content'	   => $post['post_content'],
				'excerpt'	   => $post['post_excerpt'],
				'status'		=> $post['post_status'],
				'password'	  => $post['post_password'],
				'date'		  => $post['post_date_gmt'],
				'categories'	=> $this->_prepare_terms( wp_get_object_terms( $post_ID, 'category', array('fields' => 'names') ) ),
				'tags'		  => $this->_prepare_terms( wp_get_object_terms( $post_ID, 'post_tag', array('fields' => 'names') ) )
			),
		) );

		if ( is_wp_error( $response ) ) {
			return $response;
		}

		$response = json_decode( wp_remote_retrieve_body( $response ) );

		if( empty( $response->error ) ) {
			return $response->ID;
		} else {
			return new WP_Error( 'rest-push-new-fail', $response->message );
		}

	}