/**
  * lookup post from pmp document
  */
 function test_from_doc()
 {
     $syncer = PmpPost::fromDoc($this->pmp_story);
     $this->assertNotNull($syncer->doc);
     $this->assertNotNull($syncer->post);
     $this->assertEquals($this->wp_post->ID, $syncer->post->ID);
     // post doesn't exist
     update_post_meta($this->wp_post->ID, 'pmp_guid', 'foobar');
     $syncer = PmpPost::fromDoc($this->pmp_story);
     $this->assertNull($syncer->post);
     // top-level vs attachments
     update_post_meta($this->wp_post->ID, 'pmp_guid', $this->pmp_story->attributes->guid);
     wp_update_post(array('ID' => $this->wp_post->ID, 'post_parent' => 9999));
     $syncer = PmpPost::fromDoc($this->pmp_story);
     $this->assertNull($syncer->post);
 }
Ejemplo n.º 2
0
 function setUp()
 {
     parent::setUp();
     $settings = get_option('pmp_settings');
     if (empty($settings['pmp_api_url']) || empty($settings['pmp_client_id']) || empty($settings['pmp_client_secret'])) {
         $this->skip = true;
     } else {
         $this->skip = false;
         $this->sdk_wrapper = new SDKWrapper();
         // A test query that's all but guaranteed to return at least one result.
         $this->query = array('text' => 'Obama', 'limit' => 10, 'profile' => 'story');
         $this->editor = $this->factory->user->create();
         $user = get_user_by('id', $this->editor);
         $user->set_role('editor');
         wp_set_current_user($user->ID);
         $result = $this->sdk_wrapper->queryDocs($this->query);
         $this->pmp_story = $result->items()->first();
         $syncer = PmpPost::fromDoc($this->pmp_story);
         $syncer->pull();
     }
 }
Ejemplo n.º 3
0
/**
 * For each saved search query, query the PMP and perform the appropriate action (e.g., auto draft, auto publish or do nothing)
 *
 * @since 0.3
 */
function pmp_import_for_saved_queries()
{
    $search_queries = pmp_get_saved_search_queries();
    $sdk = new SDKWrapper();
    foreach ($search_queries as $id => $query_data) {
        if ($query_data->options->query_auto_create == 'off') {
            continue;
        }
        $default_opts = array('profile' => 'story', 'limit' => 25);
        $cron_name = 'pmp_last_saved_search_cron_' . sanitize_title($query_data->options->title);
        $last_saved_search_cron = get_option($cron_name, false);
        if (!empty($last_saved_search_cron)) {
            $default_opts['startcreated'] = $last_saved_search_cron;
        } else {
            // First time pulling, honor the initial pull limit
            if (!empty($query_data->options->initial_pull_limit)) {
                $default_opts['limit'] = $query_data->options->initial_pull_limit;
            }
        }
        $query_args = array_merge($default_opts, (array) $query_data->query);
        pmp_debug("========== saved-searching: {$query_data->options->title} ==========");
        pmp_debug($query_args);
        $result = $sdk->queryDocs($query_args);
        if (empty($result)) {
            pmp_debug('  -- NO RESULTS!');
            continue;
        } else {
            pmp_debug("  -- got {$result->items()->count()} of {$result->items()->totalItems()} total");
        }
        // process results, recording the biggest "created" date
        $last_created = null;
        foreach ($result->items() as $item) {
            $syncer = PmpPost::fromDoc($item);
            if ($syncer->post) {
                $syncer->pull();
            } else {
                if ($query_data->options->query_auto_create == 'draft') {
                    $syncer->pull(false, 'draft');
                } else {
                    $syncer->pull(false, 'publish');
                }
            }
            // make sure we got a post out of the deal
            $post_id = $syncer->post->ID;
            if (!$post_id) {
                continue;
            }
            if (is_null($last_created) || $item->attributes->created > $last_created) {
                $last_created = $item->attributes->created;
            }
            // set the category(s)
            if (isset($query_data->options->post_category)) {
                // Make sure "Uncategorized" category doesn't stick around if it
                // wasn't explicitly set as a category for the saved search import.
                $assigned_categories = wp_get_post_categories($post_id);
                $uncategorized = get_category(1);
                // Check for "Uncategorized" in the already-assigned categories
                $in_assigned_cats = array_search($uncategorized->term_id, $assigned_categories);
                // Check for "Uncategorized" in the saved-search categories
                $in_saved_search_cats = array_search($uncategorized->term_id, $query_data->options->post_category);
                // If "Uncategorized" is in assigned categories and NOT in saved-search categories, ditch it.
                if ($in_assigned_cats >= 0 && $in_saved_search_cats === false) {
                    unset($assigned_categories[array_search($uncategorized->term_id, $assigned_categories)]);
                }
                // Set the newly generated list of categories for the post
                wp_set_post_categories($post_id, array_values(array_unique(array_merge($assigned_categories, $query_data->options->post_category))));
            }
        }
        // only set the last-searched-cron if we got a date
        if ($last_created) {
            update_option($cron_name, $last_created);
        }
    }
}
Ejemplo n.º 4
0
function _pmp_ajax_create_post($is_draft = false)
{
    $sdk = new SDKWrapper();
    // make sure we don't search for a blank string
    $guid = empty($_POST['pmp_guid']) ? 'nothing' : $_POST['pmp_guid'];
    $doc = $sdk->fetchDoc($guid);
    if (empty($doc)) {
        return array('success' => false, 'message' => "Cannot find PMP document {$guid}");
    } else {
        $syncer = PmpPost::fromDoc($doc);
        // pull from PMP
        if ($syncer->pull()) {
            return array('success' => true, 'data' => array('edit_url' => html_entity_decode(get_edit_post_link($syncer->post->ID)), 'post_id' => $syncer->post->ID));
        } else {
            return array('success' => false, 'message' => "Error: unable to pull PMP document {$guid}");
        }
    }
}