/** * Get posts. * * @param stdClass $settings * * @return array */ protected function get_posts($settings) { // By default we add posts per page key with the value -1 (all). if (!isset($settings->query['posts_per_page'])) { $settings->query['posts_per_page'] = -1; } // Prepare arguments for WP_Query. $args = array_merge($settings->query, ['post_type' => papi_to_array($settings->post_type), 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false]); $query = new WP_Query($args); $posts = $query->get_posts(); // Keep only objects. $posts = papi_get_only_objects($posts); $results = []; // Set labels. foreach ($posts as $post) { $obj = get_post_type_object($post->post_type); if (empty($obj)) { continue; } if (!isset($results[$obj->labels->menu_name])) { $results[$obj->labels->menu_name] = []; } $results[$obj->labels->menu_name][] = $post; } return $results; }
/** * Get posts. * * @param string $post_type * * @return array */ protected function get_posts($post_type = '') { $query = $this->get_setting('query'); $layout = $this->get_setting('layout'); // By default we add posts per page key with the value -1 (all). if (!isset($query['posts_per_page'])) { $query['posts_per_page'] = -1; } if ($layout !== 'advanced') { $post_type = $this->get_post_types(); } else { if (empty($post_type)) { $post_type = $this->get_post_types(); $post_type = array_shift($post_type); } } // Prepare arguments for WP_Query. $args = array_merge($query, ['post_type' => $post_type, 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false]); $posts = (new WP_Query($args))->posts; // Keep only objects. $posts = papi_get_only_objects($posts); $results = []; foreach ($posts as $post) { $obj = get_post_type_object($post->post_type); if (empty($obj)) { continue; } if (!isset($results[$obj->labels->menu_name])) { $results[$obj->labels->menu_name] = []; } $results[$obj->labels->menu_name][] = $post; } ksort($results); return $results; }
/** * Render property html. */ public function html() { $post_id = papi_get_post_id(); $slug = $this->html_name(); $settings = $this->get_settings(); $settings_json = []; $sort_option = $this->get_sort_option($post_id); $sort_options = static::get_sort_options(); $values = papi_get_only_objects($this->get_value()); $items = $this->get_items($settings); if (papi_is_empty($settings->items)) { $values = array_map([$this, 'convert_post_to_item'], $values); } else { foreach (array_keys($sort_options) as $key) { if (strpos($key, 'Post') === 0) { unset($sort_options[$key]); } } } // Remove existing values if `only once` is active. if ($this->get_setting('only_once')) { $items = array_udiff($items, $values, function ($a, $b) { // Backwards compatibility with both `post_title` and `title`. return strcmp(strtolower(isset($a->post_title) ? $a->post_title : $a->title), strtolower(isset($b->post_title) ? $b->post_title : $b->title)); }); } // Convert all sneak case key to camel case. foreach ((array) $settings as $key => $val) { if (!is_string($key) || !in_array($key, ['only_once', 'limit'], true)) { continue; } if ($key = papi_camel_case($key)) { $settings_json[$key] = $val; } } ?> <div class="papi-property-relationship" data-settings='<?php echo esc_attr(papi_maybe_json_encode($settings_json)); ?> '> <input type="hidden" name="<?php echo esc_attr($slug); ?> []" data-papi-rule="<?php echo esc_attr($slug); ?> " /> <div class="relationship-inner"> <div class="relationship-top-left"> <label for="<?php echo esc_attr($this->html_id('search')); ?> "><?php esc_html_e('Search', 'papi'); ?> </label> <input id="<?php echo esc_attr($this->html_id('search')); ?> " type="search" /> </div> <div class="relationship-top-right"> <?php if ($settings->show_sort_by) { ?> <label for="<?php echo esc_attr($this->html_id('sort_option')); ?> "><?php esc_html_e('Sort by', 'papi'); ?> </label> <select id="<?php echo esc_attr($this->html_id('sort_option')); ?> " name="<?php echo esc_attr($this->html_id('sort_option')); ?> "> <?php foreach (array_keys($sort_options) as $key) { ?> <option value="<?php echo esc_attr($key); ?> " <?php echo $key === $sort_option ? 'selected="selected"' : ''; ?> ><?php echo esc_html($key); ?> </option> <?php } ?> </select> <?php } ?> </div> <div class="papi-clear"></div> </div> <div class="relationship-inner"> <div class="relationship-left"> <ul> <?php foreach ($items as $item) { if (!empty($item->title)) { ?> <li> <input type="hidden" data-name="<?php echo esc_attr($slug); ?> []" value="<?php echo esc_attr($item->id); ?> "/> <a href="#" title="<?php echo esc_attr($item->title); ?> "><?php echo esc_html($item->title); ?> </a> <span class="icon plus"></span> </li> <?php } } ?> </ul> </div> <div class="relationship-right"> <ul> <?php foreach ($values as $item) { ?> <li> <input type="hidden" name="<?php echo esc_attr($slug); ?> []" value="<?php echo esc_attr($item->id); ?> "/> <a href="#"><?php echo esc_attr($item->title); ?> </a> <span class="icon minus"></span> </li> <?php } ?> </ul> </div> <div class="papi-clear"></div> </div> </div> <?php }
/** * Render property html. */ public function html() { $post_id = papi_get_post_id(); $slug = $this->html_name(); $settings = $this->get_settings(); $settings_json = []; $sort_option = $this->get_sort_option($post_id); $sort_options = static::get_sort_options(); $values = papi_get_only_objects($this->get_value()); $items = $this->get_items($settings); if (papi_is_empty($settings->items)) { $values = array_map([$this, 'convert_post_to_item'], $values); } else { foreach ($sort_options as $key => $sort) { if (strpos($key, 'Post') === 0) { unset($sort_options[$key]); } } } // Convert all sneak case key to camel case. foreach ((array) $settings as $key => $val) { if (!is_string($key) || !in_array($key, ['only_once', 'limit'])) { continue; } $settings_json[papi_camel_case($key)] = $val; } ?> <div class="papi-property-relationship" data-settings='<?php echo json_encode($settings_json); ?> '> <input type="hidden" name="<?php echo $slug; ?> []" data-papi-rule="<?php echo $slug; ?> " /> <div class="relationship-inner"> <div class="relationship-top-left"> <label for="<?php echo $this->html_id('search'); ?> "><?php _e('Search', 'papi'); ?> </label> <input id="<?php echo $this->html_id('search'); ?> " type="search" /> </div> <div class="relationship-top-right"> <?php if ($settings->show_sort_by) { ?> <label for="<?php echo $this->html_id('sort_option'); ?> "><?php _e('Sort by', 'papi'); ?> </label> <select id="<?php echo $this->html_id('sort_option'); ?> " name="<?php echo $this->html_id('sort_option'); ?> "> <?php foreach ($sort_options as $key => $v) { ?> <option value="<?php echo $key; ?> " <?php echo $key === $sort_option ? 'selected="selected"' : ''; ?> ><?php echo $key; ?> </option> <?php } ?> </select> <?php } ?> </div> <div class="papi-clear"></div> </div> <div class="relationship-inner"> <div class="relationship-left"> <ul> <?php foreach ($items as $item) { if (!empty($item->title)) { ?> <li> <input type="hidden" data-name="<?php echo $slug; ?> []" value="<?php echo $item->id; ?> "/> <a href="#"><?php echo $item->title; ?> </a> <span class="icon plus"></span> </li> <?php } } ?> </ul> </div> <div class="relationship-right"> <ul> <?php foreach ($values as $item) { ?> <li> <input type="hidden" name="<?php echo $slug; ?> []" value="<?php echo $item->id; ?> "/> <a href="#"><?php echo $item->title; ?> </a> <span class="icon minus"></span> </li> <?php } ?> </ul> </div> <div class="papi-clear"></div> </div> </div> <?php }