Ejemplo n.º 1
0
/**
 * Get the IDs for posts in a collections
 *
 * @param string $name
 * @param array $args
 * @return array
 */
function collections_get_post_ids($name, $args = array())
{
    do_action('collections_get_posts', $name);
    $collection = Post_Collection::get_by_name($name);
    if (!$collection) {
        return array();
    }
    if (Collections()->is_customizer_preview()) {
        // Reset collections on initial page load
        if (!empty($_POST['customize_messenger_channel']) && 'preview-0' === $_POST['customize_messenger_channel'] && $collection->get_published_item_ids() != $collection->get_customizer_item_ids()) {
            $collection->set_customizer_item_ids($collection->get_published_item_ids());
        }
        return $collection->get_customizer_item_ids();
    } else {
        return $collection->get_published_item_ids();
    }
}
    /**
     * Tell the parent about which Collections were used on this page
     */
    public function action_wp_footer()
    {
        // Set up the data
        $rendered_collections = array();
        foreach ($this->rendered_collections as $name) {
            $collection = Post_Collection::get_by_name($name);
            $data = array('slug' => sanitize_title($name), 'items' => array());
            if ($collection) {
                foreach ($collection->get_customizer_items() as $post) {
                    $data['items'][] = Collections()->get_post_for_json($post);
                }
            }
            $rendered_collections[$name] = $data;
        }
        $settings = array('renderedCollections' => $rendered_collections);
        ?>
		<script type="text/javascript">
			var _wpCollectionsCustomizerPreviewSettings = <?php 
        echo json_encode($settings);
        ?>
;
		</script>
		<?php 
    }
 /**
  * Update the posts associated with widget
  *
  * @param array $new_instance
  * @param array $old_instance
  * @return array $instance
  */
 public function update($new_instance, $old_instance)
 {
     $collection = Post_Collection::get_by_name($this->get_collection_name());
     if (!$collection) {
         $collection = Post_Collection::create($this->get_collection_name());
         if (is_wp_error($collection)) {
             return $old_instance;
         }
     }
     $instance = array();
     $instance['title'] = !empty($new_instance['title']) ? sanitize_text_field($new_instance['title']) : '';
     if (!empty($new_instance['collection_items'])) {
         $instance_items = array_map('absint', explode(',', $new_instance['collection_items']));
     } else {
         $instance_items = array();
     }
     // Triggers cache bust in Customizer and also used for save
     $instance['collection_items_stash'] = $instance_items;
     // Doing a customizer preview should only modify Customizer collection items.
     if ($this->is_customizer_widget_update()) {
         $collection->set_customizer_item_ids($instance_items);
     } else {
         $collection->set_published_item_ids($instance_items);
     }
     return $instance;
 }