/** * Init when you only know the WP post * * @param $wp_post - a WP_Post instance * @return a new PmpSyncer */ public static function fromPost(WP_Post $wp_post) { $sdk = new SDKWrapper(); $guid = get_post_meta($wp_post->ID, 'pmp_guid', true); if ($guid) { $doc = $sdk->fetchDoc($guid); return new self($doc, $wp_post); // doc might be null } else { return new self(null, $wp_post); } }
/** * 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; }
/** * Input field for default distributor setting * * @since 0.0.1 */ function pmp_dist_default_distributor_input() { $sdk = new SDKWrapper(); $pmp_things = $sdk->query2json('queryDocs', array('profile' => 'user', 'limit' => 9999)); $settings = get_option('pmp_dist_settings'); $guids = $settings['pmp_dist_default_distributor']; ?> <select id="pmp_dist_default_distributor" name="pmp_dist_settings[pmp_dist_default_distributor][]" class="chosen" multiple> <?php foreach ($pmp_things['items'] as $user) { ?> <option <?php if (in_array($user['attributes']['guid'], $guids)) { ?> selected<?php } ?> value="<?php echo $user['attributes']['guid']; ?> "><?php echo $user['attributes']['title']; ?> </option> <?php } ?> </select> <script type="text/javascript"> (function() { var $ = jQuery; $(document).ready(function() { $('select.chosen').chosen({disable_search_threshold: 10}); }); })(); </script> <?php }
/** * 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); } } }
function test_getImageEnclosure() { $doc = $this->make_enclosure_image(array()); $this->assertNull(SDKWrapper::getImageEnclosure($doc)); // favors large/primary/standard crops $doc = $this->make_enclosure_image(array('foobar', 'something', 'large', 'blah')); $this->assertEquals('large', SDKWrapper::getImageEnclosure($doc)->meta->crop); $doc = $this->make_enclosure_image(array('foobar', 'primary', 'blah')); $this->assertEquals('primary', SDKWrapper::getImageEnclosure($doc)->meta->crop); $doc = $this->make_enclosure_image(array('standard', 'foo', 'blah')); $this->assertEquals('standard', SDKWrapper::getImageEnclosure($doc)->meta->crop); // falls back to first $doc = $this->make_enclosure_image(array('foo', 'and', 'bar')); $this->assertEquals('foo', SDKWrapper::getImageEnclosure($doc)->meta->crop); }
/** * Make sure distributor-set collections are preserved when pushing to the PMP * * @since 0.0.1 */ function pmp_dist_preserve_distributor_collection($doc, $previous_collection, $post) { $current_guids = array_map(function ($item) { return SDKWrapper::guid4href($item->href); }, $doc->links->collection); $previous_guids = array_map(function ($item) { return SDKWrapper::guid4href($item->href); }, $previous_collection); $difference = array_diff($previous_guids, $current_guids); if (!empty($difference)) { $sdk = new SDKWrapper(); foreach ($difference as $idx => $guid_to_check) { $collection = $sdk->fetchDoc($guid_to_check); foreach ($doc->links->distributor as $distributor) { if ($distributor->href == $collection->links->owner[0]->href) { $doc->links->collection[] = $previous_collection[$idx]; } } } } return $doc; }
/** * Handle new-ing a pmp doc (without saving it) * * @return boolean success */ protected function new_doc() { $sdk = new SDKWrapper(); $this->doc = $sdk->newDoc('base', array('attributes' => array('title' => $this->post->post_title, 'published' => date('c', strtotime($this->post->post_date)), 'itags' => array_merge(self::$ITAGS, array("post-id-{$this->post->ID}"))))); return true; }
/** * Render the plugin's series and properties pages * * @since 0.2 */ function pmp_collections_page() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.')); } if ($_GET['page'] == 'pmp-properties-menu') { $name = 'Properties'; $profile = 'property'; } else { if ($_GET['page'] == 'pmp-series-menu') { $name = 'Series'; $profile = 'series'; } } if (isset($_POST['pmp-unset-default-' . $profile])) { delete_option('pmp_default_' . $profile); } $sdk = new SDKWrapper(); $pmp_collection = $sdk->query2json('queryDocs', array('profile' => $profile, 'writeable' => 'true', 'limit' => 9999)); $context = array('PMP' => pmp_json_obj(array('default_collection' => get_option('pmp_default_' . $profile, false), 'pmp_collection' => $pmp_collection, 'profile' => $profile, 'name' => $name)), 'name' => $name, 'profile' => $profile); pmp_render_template('collections.php', $context); }
/** * Get the current user's PMP GUID * * @since 0.2 */ function pmp_get_my_guid() { $pmp_my_guid_transient_key = 'pmp_my_guid'; $pmp_my_guid_transient = get_transient($pmp_my_guid_transient_key); if (!empty($pmp_my_guid_transient)) { return $pmp_my_guid_transient; } $sdk = new SDKWrapper(); $me = $sdk->fetchUser('me'); $pmp_my_guid_transient = $me->attributes->guid; set_transient($pmp_my_guid_transient_key, $pmp_my_guid_transient, 0); return $pmp_my_guid_transient; }
/** * Builds a data structure that describes a select menu for the post based on the $type * * @param $type (string) The document option to create a select menu for * (i.e., 'group', 'property' or 'series'). * @since 0.3 */ function _pmp_select_for_post($post, $type) { $ret = array('default_guid' => get_option('pmp_default_' . $type, false), 'type' => $type); $sdk = new SDKWrapper(); $pmp_things = $sdk->query2json('queryDocs', array('profile' => $type, 'writeable' => 'true', 'limit' => 9999)); $override = pmp_get_collection_override_value($post, $type); $options = array(); // Pad the options with an empty value $options[] = array('selected' => selected($override, false, false), 'guid' => '', 'title' => '--- No ' . $type . ' ---'); if (!empty($pmp_things['items'])) { foreach ($pmp_things['items'] as $thing) { if (!empty($override)) { $selected = selected($override, $thing['attributes']['guid'], false); } $option = array('selected' => isset($selected) ? $selected : '', 'guid' => $thing['attributes']['guid'], 'title' => $thing['attributes']['title']); $options[] = $option; } } $ret['options'] = $options; return $ret; }
/** * Static field for currently connected user * * @since 0.3 */ function pmp_user_title_input() { $options = get_option('pmp_settings'); if (empty($options['pmp_api_url']) || empty($options['pmp_client_id']) || empty($options['pmp_client_secret'])) { echo '<p><em>Not connected</em></p>'; } else { try { $sdk = new SDKWrapper(); $me = $sdk->fetchUser('me'); $title = $me->attributes->title; $link = pmp_get_support_link($me->attributes->guid); echo "<p><a target='_blank' href='{$link}'>{$title}</a></p>"; } catch (\Pmp\Sdk\Exception\AuthException $e) { echo '<p style="color:#a94442"><b>Unable to connect - invalid Client-Id/Secret</b></p>'; } catch (\Pmp\Sdk\Exception\HostException $e) { echo '<p style="color:#a94442"><b>Unable to connect - ' . $options['pmp_api_url'] . ' is unreachable</b></p>'; } } }