/** * 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 }
/** * Print distributor meta box for doc/post owner * * @since 0.0.1 */ function pmp_distributor_options_meta_box_for_owner() { global $post; $sdk = new SDKWrapper(); $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']; $action = empty($existing_distributors) ? 'Set' : 'Update'; wp_nonce_field('pmp_dist_meta_box', 'pmp_dist_meta_box_nonce'); ?> <div id="pmp-dist-options" class="async-menu-container"> <p>Set distributor(s) for this post</p> <div id="pmp-distributor-for-post" class="async-menu-option pmp-dist-option-for-post" data-pmp-dist-option-type="distributor"> <span class="spinner"></span> </div> </div> <div id="pmp-publish-actions"> <?php submit_button($action . ' dist. options', 'primary', 'pmp_save_distributors_for_post', false, $attrs); ?> </div><?php }
/** * 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); }
/** * 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; }