/**
  * Load PMP-related metadata from the wordpress database
  */
 protected function load_pmp_post_meta()
 {
     $this->post_meta = array();
     // look for any meta fields starting with "pmp_"
     if ($this->post) {
         $all_meta = get_post_meta($this->post->ID);
         foreach ($all_meta as $key => $value) {
             if (preg_match('/^pmp_/', $key)) {
                 $this->post_meta[$key] = is_array($value) && count($value) == 1 ? $value[0] : $value;
             }
         }
     }
     // BACKWARDS COMPATIBILITY: set pmp_last_pushed from pmp_owner
     if (!empty($this->post_meta['pmp_owner'])) {
         if ($this->post_meta['pmp_owner'] == pmp_get_my_guid()) {
             if (isset($this->post_meta['pmp_modified'])) {
                 $this->post_meta['pmp_last_pushed'] = $this->post_meta['pmp_modified'];
             } else {
                 $this->post_meta['pmp_last_pushed'] = date('c', time());
             }
             update_post_meta($this->post->ID, 'pmp_last_pushed', $this->post_meta['pmp_last_pushed']);
         }
         unset($this->post_meta['pmp_owner']);
         delete_post_meta($this->post->ID, 'pmp_owner');
     }
 }
Example #2
0
/**
 * Returns data used to build select menu of users that can be set as a distributor for a post
 *
 * @since 0.0.1
 */
function _pmp_dist_option_select_distributor_for_post($post)
{
    $ret = array('default_guid' => get_option('pmp_default_distributor', false), 'type' => 'distributor');
    $sdk = new SDKWrapper();
    $pmp_things = $sdk->query2json('queryDocs', array('profile' => 'user', 'limit' => 9999));
    $pmp_things['items'] = array_filter($pmp_things['items'], function ($item) {
        if ($item['attributes']['guid'] !== pmp_get_my_guid()) {
            return $item;
        }
    });
    $pmp_guid = get_post_meta($post->ID, 'pmp_guid', true);
    $pmp_doc = $sdk->query2json('fetchDoc', $pmp_guid);
    $existing_distributors = $pmp_doc['items'][0]['links']['distributor'];
    $existing_options = array();
    if (!empty($existing_distributors)) {
        foreach ($existing_distributors as $distributor) {
            $distributor->guid = $sdk->guid4href($distributor->href);
            array_push($existing_options, $distributor);
        }
    }
    $existing_guids = array_map(function ($item) {
        return $item->guid;
    }, $existing_options);
    $settings = get_option('pmp_dist_settings');
    $default_guids = $settings['pmp_dist_default_distributor'];
    if (!empty($pmp_things['items'])) {
        foreach ($pmp_things['items'] as $thing) {
            $thing_guid = $thing['attributes']['guid'];
            if (in_array($thing_guid, $existing_guids) || empty($existing_guids) && in_array($thing_guid, $default_guids)) {
                $selected = true;
            } else {
                $selected = false;
            }
            if (empty($existing_guids) && in_array($thing_guid, $default_guids)) {
                $title = $thing['attributes']['title'] . ' (default)';
            } else {
                $title = $thing['attributes']['title'];
            }
            $option = array('selected' => $selected, 'guid' => $thing['attributes']['guid'], 'title' => $title);
            $options[] = $option;
        }
    }
    $ret['options'] = $options;
    return $ret;
}
Example #3
0
/**
 * See if a user is considered a distributor for a given post
 *
 * @since 0.0.1
 */
function pmp_user_is_distributor_of_post($post_id, $user_guid = null)
{
    if (empty($user_guid)) {
        $user_guid = pmp_get_my_guid();
    }
    $sdk = new SDKWrapper();
    $user = $sdk->fetchDoc($user_guid);
    $pmp_guid = get_post_meta($post_id, 'pmp_guid', true);
    if (empty($pmp_guid)) {
        return false;
    }
    $doc = $sdk->fetchDoc($pmp_guid);
    if (!empty($doc->links->distributor)) {
        foreach ($doc->links->distributor as $distrib) {
            if (SDKWrapper::guid4href($distrib->href) == $user_guid) {
                return true;
            }
        }
    }
    return false;
}
 function test_pmp_get_my_guid()
 {
     $me = $this->sdk_wrapper->fetchUser('me');
     $this->assertEquals(pmp_get_my_guid(), $me->attributes->guid);
 }
Example #5
0
 function test_pmp_save_users()
 {
     if ($this->skip) {
         $this->markTestSkipped('This test requires site options `pmp_api_url`, `pmp_client_id` and `pmp_client_secret`');
         return;
     }
     $_POST['data'] = addslashes(json_encode(array('group_guid' => $GLOBALS['pmp_stash']['test_group_guid'], 'user_guids' => array(pmp_get_my_guid()))));
     $_POST['security'] = wp_create_nonce('pmp_ajax_nonce');
     try {
         $this->_handleAjax("pmp_save_users");
     } catch (WPAjaxDieContinueException $e) {
         $result = json_decode($this->_last_response, true);
         $this->assertTrue($result['success']);
         // Clean up/delete test group
         if ($result['success']) {
             $group_doc = $this->sdk_wrapper->fetchDoc($GLOBALS['pmp_stash']['test_group_guid']);
             $group_doc->delete();
         }
     }
 }
 /**
  * did a doc originate from this wordpress site?
  */
 function test_is_local()
 {
     $syncer = new PmpPost($this->pmp_story, $this->wp_post);
     $this->assertFalse($syncer->is_local());
     // should unset pmp_owner
     update_post_meta($this->wp_post->ID, 'pmp_owner', 'somebody-else');
     $this->assertNotEmpty(get_post_meta($this->wp_post->ID, 'pmp_owner', true));
     $syncer = new PmpPost($this->pmp_story, $this->wp_post);
     $this->assertEmpty(get_post_meta($this->wp_post->ID, 'pmp_owner', true));
     $this->assertArrayNotHasKey('pmp_owner', $syncer->post_meta);
     $this->assertArrayNotHasKey('pmp_last_pushed', $syncer->post_meta);
     $this->assertFalse($syncer->is_local());
     // unset the pmp_owner, but set the pmp_last_pushed
     update_post_meta($this->wp_post->ID, 'pmp_owner', pmp_get_my_guid());
     $this->assertNotEmpty(get_post_meta($this->wp_post->ID, 'pmp_owner', true));
     $syncer = new PmpPost($this->pmp_story, $this->wp_post);
     $this->assertEmpty(get_post_meta($this->wp_post->ID, 'pmp_owner', true));
     $this->assertArrayNotHasKey('pmp_owner', $syncer->post_meta);
     $this->assertEquals($this->pmp_story->attributes->modified, $syncer->post_meta['pmp_last_pushed']);
     $this->assertTrue($syncer->is_local());
 }
Example #7
0
/**
 * Update the transient that stores the current user's PMP GUID
 *
 * @since 0.2
 */
function pmp_update_my_guid_transient()
{
    pmp_get_my_guid();
}