/** * 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(); } }
/** * Enqueue all of the necessary assets for the plugin */ public function enqueue_assets() { global $wp_customize; if (!$this->did_register_assets) { $this->action_enqueue_scripts_register(); } wp_enqueue_script('collections'); wp_enqueue_style('collections'); wp_enqueue_style('collection-control', Collections()->get_url('css/collection-control.css'), array('collections')); wp_enqueue_script('collection-control', Collections()->get_url('js/collection-control.js'), array('jquery', 'jquery-ui-sortable', 'collections')); if (is_admin() && !has_action('admin_footer', array($this, 'render_add_post_modal'))) { add_action('admin_footer', array($this, 'render_templates')); } if (!empty($wp_customize) && !has_action('customize_controls_print_footer_scripts', array($this, 'render_add_post_modal'))) { add_action('customize_controls_print_footer_scripts', array($this, 'render_templates')); } }
/** * 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 }
/** * Output the collection management view * * @param array $instance */ public function form($instance) { global $wp_customize; Collections()->enqueue_assets(); $vars = array('title_field_id' => $this->get_field_id('title'), 'title_field_name' => $this->get_field_name('title'), 'title' => !empty($instance['title']) ? $instance['title'] : '', 'collection_items_field_id' => $this->get_field_id('collection_items'), 'collection_items_field_name' => $this->get_field_name('collection_items'), 'widget_id' => $this->id, 'widget_instance_id' => md5(rand(0, 10000) . time())); $vars['collection_items'] = $vars['collection_item_ids'] = array(); if ($collection = Post_Collection::get_by_name($this->get_collection_name())) { // Show Customizer items in the form when doing an update if ($this->is_customizer_widget_update()) { $vars['collection_item_ids'] = $collection->get_customizer_item_ids(); } else { $vars['collection_item_ids'] = $collection->get_published_item_ids(); } // Reset the Customizer if needed, but only on initial page load if ($this->is_customizer() && empty($_POST['wp_customize']) && $collection->get_customizer_item_ids() !== $vars['collection_item_ids']) { $collection->set_customizer_item_ids($vars['collection_item_ids']); } foreach ($vars['collection_item_ids'] as $post_id) { $vars['collection_items'][] = Collections()->get_post_for_json($post_id); } } echo Collections()->get_view('widget-form', $vars); }