Esempio n. 1
0
/**
 * For each PMP post in the WP database, fetch the corresponding Doc from PMP and check if
 * the WP post differs from the PMP Doc. If it does differ, update the post in the WP database.
 *
 * @since 0.1
 */
function pmp_get_updates()
{
    pmp_debug('========== pmp_get_updates ==========');
    $posts = pmp_get_pmp_posts();
    foreach ($posts as $post) {
        $syncer = PmpPost::fromPost($post);
        $syncer->pull();
    }
}
 /**
  * lookup pmp document from a post
  */
 function test_from_post()
 {
     $syncer = PmpPost::fromPost($this->wp_post);
     $this->assertNotNull($syncer->doc);
     $this->assertNotNull($syncer->post);
     $this->assertEquals($this->pmp_story->attributes->guid, $syncer->doc->attributes->guid);
     // post isn't in the pmp yet
     update_post_meta($this->wp_post->ID, 'pmp_guid', null);
     $syncer = PmpPost::fromPost($this->wp_post);
     $this->assertNull($syncer->doc);
     // lost access (403 forbidden) to this pmp document
     update_post_meta($this->wp_post->ID, 'pmp_guid', 'some-guid-i-cannot-see');
     $syncer = PmpPost::fromPost($this->wp_post);
     $this->assertNull($syncer->doc);
 }
 /**
  * delete a post, when you lose access upstream
  */
 function test_pull_delete()
 {
     $syncer = new PmpPost($this->pmp_story, $this->wp_post);
     $this->assertTrue($syncer->pull(true));
     $this->assertCount(2, $syncer->attachment_syncers);
     // make sure these posts all get cleaned up
     $id = $this->wp_post->ID;
     $attach_id1 = $syncer->attachment_syncers[0]->post->ID;
     $attach_id2 = $syncer->attachment_syncers[1]->post->ID;
     // make it look like the top-level doc disappeared
     update_post_meta($id, 'pmp_guid', 'foobar');
     $syncer = PmpPost::fromPost($this->wp_post);
     $this->assertTrue($syncer->pull(true));
     $this->assertNull($syncer->post);
     $this->assertCount(2, $syncer->attachment_syncers);
     $this->assertNull(get_post($id));
     $this->assertNull(get_post($attach_id1));
     $this->assertNull(get_post($attach_id2));
 }
/**
 * When the PMP notification hub sends an update, handle it
 *
 * @since 0.3
 */
function pmp_do_notification_callback()
{
    global $wpdb;
    pmp_debug('========== pmp_do_notification_callback ==========');
    $body = file_get_contents('php://input');
    $hash = hash_hmac('sha1', $body, PMP_NOTIFICATIONS_SECRET);
    // get a COMPLETE mapping of known-PMP-guids to top-level WP-posts
    $pmp_post_data = $wpdb->get_results("select post_id, meta_value, post_parent " . "from {$wpdb->posts} join {$wpdb->postmeta} on (ID = post_id) " . "where meta_key = 'pmp_guid'", ARRAY_A);
    // map to the TOP LEVEL post (attachments map to their parent)
    $pmp_guids = array();
    foreach ($pmp_post_data as $row) {
        if ($row['post_parent'] > 0) {
            $pmp_guids[$row['meta_value']] = $row['post_parent'];
        } else {
            $pmp_guids[$row['meta_value']] = $row['post_id'];
        }
    }
    // check hub signature
    if ($_SERVER['HTTP_X_HUB_SIGNATURE'] !== "sha1={$hash}") {
        var_log('INVALID PMP notifications HTTP_X_HUB_SIGNATURE');
        var_log("  Expected: sha1={$hash}");
        var_log("  Got:      " . $_SERVER['HTTP_X_HUB_SIGNATURE']);
        return;
    }
    // parse xml pubsubhubbub body
    $xml = simplexml_load_string($body);
    foreach ($xml->channel->item as $item) {
        $item_json = json_decode(json_encode($item));
        $item_guid = $item_json->guid;
        // look for Posts tied to that guid
        if (isset($pmp_guids[$item_guid])) {
            $post = get_post($pmp_guids[$item_guid]);
            if ($post) {
                $syncer = PmpPost::fromPost($post);
                $syncer->pull();
            }
        }
    }
}
Esempio n. 5
0
/**
 * Handle pushing post content to PMP. Works with posts and attachments (images).
 *
 * @since 0.2
 */
function pmp_handle_push($post_id)
{
    $post = get_post($post_id);
    $syncer = PmpPost::fromPost($post);
    if ($syncer->push()) {
        return $syncer->doc->attributes->guid;
    } else {
        return null;
    }
}