예제 #1
0
function hocwp_query_post_in_same_category($args = array())
{
    $post_id = absint(isset($args['post_id']) ? $args['post_id'] : get_the_ID());
    $query = new WP_Query();
    if (!hocwp_id_number_valid($post_id)) {
        return $query;
    }
    $cache = (bool) hocwp_get_value_by_key($args, 'cache', true);
    $transient_name = 'hocwp_query_post_' . $post_id . '_in_same_category_%s';
    $transient_name = hocwp_build_transient_name($transient_name, $args);
    $query = get_transient($transient_name);
    if (!$cache || false === $query) {
        $taxonomies = get_post_taxonomies($post_id);
        foreach ($taxonomies as $key => $tax_name) {
            $taxonomy = get_taxonomy($tax_name);
            if (!$taxonomy->hierarchical) {
                unset($taxonomies[$key]);
            }
        }
        if (hocwp_array_has_value($taxonomies)) {
            $defaults = array();
            foreach ($taxonomies as $taxonomy) {
                $term_ids = wp_get_post_terms($post_id, $taxonomy, array('fields' => 'ids'));
                if (hocwp_array_has_value($term_ids)) {
                    $tax_item = array('taxonomy' => $taxonomy, 'field' => 'id', 'terms' => $term_ids);
                    $defaults = hocwp_query_sanitize_tax_query($tax_item, $defaults);
                }
            }
            $args = wp_parse_args($args, $defaults);
            $args['tax_query']['relation'] = 'OR';
            $query = hocwp_query($args);
            if ($cache && $query->have_posts()) {
                set_transient($transient_name, $query, DAY_IN_SECONDS);
            }
        }
    }
    return $query;
}
    public function widget($args, $instance)
    {
        $this->instance = $instance;
        global $post;
        $title = hocwp_widget_title($args, $instance, false);
        $post_type = $this->get_post_type_from_instance($instance);
        $post_types = array();
        foreach ($post_type as $fvdata) {
            $ptvalue = isset($fvdata['value']) ? $fvdata['value'] : '';
            if (!empty($ptvalue)) {
                $post_types[] = $ptvalue;
            }
        }
        $number = isset($instance['number']) ? $instance['number'] : $this->args['number'];
        $by = isset($instance['by']) ? $instance['by'] : $this->args['by'];
        $category = isset($instance['category']) ? $instance['category'] : json_encode($this->args['category']);
        $category = hocwp_json_string_to_array($category);
        $excerpt_length = isset($instance['excerpt_length']) ? $instance['excerpt_length'] : $this->args['excerpt_length'];
        $content_class = 'widget-content';
        $slider = hocwp_get_value_by_key($instance, 'slider', hocwp_get_value_by_key($this->args, 'slider'));
        $title_length = hocwp_get_value_by_key($instance, 'title_length', hocwp_get_value_by_key($this->args, 'title_length'));
        $hide_thumbnail = hocwp_get_value_by_key($instance, 'hide_thumbnail', hocwp_get_value_by_key($this->args, 'hide_thumbnail'));
        $widget_title_link_category = hocwp_get_value_by_key($instance, 'widget_title_link_category', hocwp_get_value_by_key($this->args, 'widget_title_link_category'));
        $category_as_widget_title = hocwp_get_value_by_key($instance, 'category_as_widget_title', hocwp_get_value_by_key($this->args, 'category_as_widget_title'));
        if ((bool) $slider) {
            hocwp_add_string_with_space_before($content_class, 'post-slider');
        }
        if ('category' == $by && (bool) $widget_title_link_category) {
            $cvalue = current($category);
            $term_id = isset($cvalue['value']) ? $cvalue['value'] : '';
            $term_id = absint($term_id);
            if ($term_id > 0) {
                $taxonomy = isset($cvalue['taxonomy']) ? $cvalue['taxonomy'] : '';
                $link = new HOCWP_HTML('a');
                $link->set_class('term-link');
                $link->set_href(get_term_link($term_id, $taxonomy));
                if ((bool) $category_as_widget_title) {
                    $term = get_term($term_id, $taxonomy);
                    $title = $term->name;
                } else {
                    $title = hocwp_get_value_by_key($instance, 'title');
                }
                $link->set_text($title);
                $title = hocwp_get_value_by_key($args, 'before_title') . $link->build() . hocwp_get_value_by_key($args, 'after_title');
            }
        }
        $query_args = array('posts_per_page' => $number, 'post_type' => $post_types);
        $w_query = new WP_Query();
        $get_by = false;
        switch ($by) {
            case 'recent':
                $get_by = true;
                break;
            case 'modified':
            case 'recent_modified':
                $get_by = true;
                $query_args['orderby'] = 'modified';
                $query_args['order'] = 'DESC';
                break;
            case 'rand':
            case 'random':
                $get_by = true;
                $query_args['orderby'] = 'rand';
                break;
            case 'most_comment':
            case 'comment_count':
            case 'comment':
                $get_by = true;
                $query_args['orderby'] = 'comment_count';
                break;
            case 'category':
                foreach ($category as $cvalue) {
                    $term_id = isset($cvalue['value']) ? $cvalue['value'] : '';
                    $term_id = absint($term_id);
                    if ($term_id > 0) {
                        $taxonomy = isset($cvalue['taxonomy']) ? $cvalue['taxonomy'] : '';
                        if (!empty($taxonomy)) {
                            $tax_item = array('taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => $term_id);
                            $query_args = hocwp_query_sanitize_tax_query($tax_item, $query_args);
                            $get_by = true;
                        }
                    }
                }
                break;
            case 'related':
                $get_by = true;
                break;
            case 'like':
                $get_by = true;
                $query_args['meta_key'] = 'likes';
                $query_args['orderby'] = 'meta_value_num';
                break;
            case 'view':
                $get_by = true;
                $query_args['meta_key'] = 'views';
                $query_args['orderby'] = 'meta_value_num';
                break;
            case 'featured':
                $get_by = true;
                hocwp_query_sanitize_featured_args($query_args);
                break;
            case 'favorite':
                break;
            case 'rate':
                break;
        }
        $get_by = apply_filters('hocwp_widget_post_get_by_check_status', $get_by, $args, $instance, $this);
        if ($get_by) {
            $query_args = apply_filters('hocwp_widget_post_query_args', $query_args, $args, $instance, $this);
            if ('related' == $by) {
                $w_query = hocwp_query_related_post($query_args);
            } else {
                $w_query = hocwp_query($query_args);
            }
        }
        $widget_html = hocwp_get_value_by_key($args, 'before_widget') . $title;
        $widget_html .= '<div class="' . $content_class . '">';
        $widget_post_pre_html = apply_filters('hocwp_widget_post_pre_html', '', $args, $instance, $this);
        if (!empty($widget_post_pre_html)) {
            $widget_html .= $widget_post_pre_html;
        } else {
            if ($w_query->have_posts()) {
                $widget_html .= apply_filters('hocwp_widget_post_before_loop', '', $args, $instance, $this);
                if ((bool) $slider) {
                    $four_posts = array_slice($w_query->posts, 0, 4);
                    $next_posts = array_slice($w_query->posts, 4, absint($w_query->post_count - 4));
                    $widget_content = apply_filters('hocwp_widget_post_pre_slider_html', '', $args, $instance, $this);
                    if (empty($widget_content)) {
                        $carousel_id = $this->id;
                        $carousel_id = hocwp_sanitize_id($carousel_id);
                        $count = 0;
                        ob_start();
                        ?>
						<div id="<?php 
                        echo $carousel_id;
                        ?>
" class="carousel slide" data-ride="carousel">
							<div class="carousel-inner" role="listbox">
								<?php 
                        foreach ($four_posts as $post) {
                            setup_postdata($post);
                            $class = 'item';
                            if (0 == $count) {
                                hocwp_add_string_with_space_before($class, 'active');
                            }
                            ?>
									<div class="<?php 
                            echo $class;
                            ?>
">
										<?php 
                            hocwp_article_before();
                            do_action('hocwp_widget_post_slider_before_post', $args, $instance, $this);
                            if (!(bool) $hide_thumbnail) {
                                if ('coupon' == $post->post_type) {
                                    $store = hocwp_get_coupon_store($post->ID);
                                    echo '<div class="text-center store-logo">';
                                    hocwp_term_the_thumbnail(array('term' => $store, 'width' => 180, 'height' => 110));
                                    echo '</div>';
                                } else {
                                    hocwp_post_thumbnail(array('width' => 300, 'height' => 200));
                                }
                            }
                            do_action('hocwp_widget_post_slider_before_post_title', $args, $instance, $this);
                            hocwp_post_title_link(array('title' => hocwp_substr(get_the_title(), $title_length)));
                            do_action('hocwp_widget_post_slider_after_post_title', $args, $instance, $this);
                            hocwp_entry_summary($excerpt_length);
                            do_action('hocwp_widget_post_slider_after_post', $args, $instance, $this);
                            hocwp_article_after();
                            ?>
									</div>
									<?php 
                            $count++;
                        }
                        wp_reset_postdata();
                        ?>
							</div>
							<ol class="carousel-indicators list-inline list-unstyled">
								<?php 
                        $count = count($four_posts);
                        for ($i = 0; $i < $count; $i++) {
                            $indicator_class = 'indicator-item';
                            if (0 == $i) {
                                hocwp_add_string_with_space_before($indicator_class, 'active');
                            }
                            ?>
									<li data-target="#<?php 
                            echo $carousel_id;
                            ?>
" data-slide-to="<?php 
                            echo $i;
                            ?>
"
									    class="<?php 
                            echo $indicator_class;
                            ?>
">
										<span><?php 
                            echo $i + 1;
                            ?>
</span>
									</li>
									<?php 
                        }
                        ?>
							</ol>
							<a class="left carousel-control" href="#<?php 
                        echo $carousel_id;
                        ?>
" role="button"
							   data-slide="prev">
								<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
								<span class="sr-only"><?php 
                        _e('Previous', 'hocwp-theme');
                        ?>
</span>
							</a>
							<a class="right carousel-control" href="#<?php 
                        echo $carousel_id;
                        ?>
" role="button"
							   data-slide="next">
								<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
								<span class="sr-only"><?php 
                        _e('Next', 'hocwp-theme');
                        ?>
</span>
							</a>
						</div>
						<?php 
                        $widget_content = ob_get_clean();
                        if (hocwp_array_has_value($next_posts)) {
                            $w_query->posts = $next_posts;
                            $w_query->post_count = count($next_posts);
                            ?>
							<div class="more-posts">
								<?php 
                            $widget_content .= $this->list_post_html($args, $instance, $w_query);
                            ?>
							</div>
							<?php 
                        }
                    }
                    $widget_content = apply_filters('hocwp_widget_post_slider_html', $widget_content, $args, $instance, $this);
                    $widget_html .= $widget_content;
                } else {
                    $widget_html .= $this->list_post_html($args, $instance, $w_query);
                }
                $widget_html .= apply_filters('hocwp_widget_post_after_loop', '', $args, $instance, $this);
            } else {
                $widget_html .= '<p class="nothing-found">' . __('Nothing found!', 'hocwp-theme') . '</p>';
            }
        }
        $widget_html = apply_filters('hocwp_widget_post_html', $widget_html, $args, $instance, $this);
        $widget_html .= '</div>';
        $widget_html .= hocwp_get_value_by_key($args, 'after_widget');
        echo $widget_html;
    }
예제 #3
0
function hocwp_classifieds_pre_get_posts(WP_Query $query)
{
    if ($query->is_main_query()) {
        if (is_search()) {
            $type = hocwp_get_value_by_key($_REQUEST, 'type');
            $province = hocwp_get_value_by_key($_REQUEST, 'province');
            $district = hocwp_get_value_by_key($_REQUEST, 'district');
            $ward = hocwp_get_value_by_key($_REQUEST, 'ward');
            $street = hocwp_get_value_by_key($_REQUEST, 'street');
            $price = hocwp_get_value_by_key($_REQUEST, 'price');
            $acreage = hocwp_get_value_by_key($_REQUEST, 'acreage');
            $object = hocwp_get_value_by_key($_REQUEST, 'object');
            $salary = hocwp_get_value_by_key($_REQUEST, 'salary');
            $tax_query = array('relation' => 'AND');
            if (hocwp_id_number_valid($type)) {
                $tax_item = array('taxonomy' => 'classifieds_type', 'field' => 'id', 'terms' => $type);
                hocwp_query_sanitize_tax_query($tax_item, $tax_query);
            }
            if (hocwp_id_number_valid($province)) {
                $tax_item = array('taxonomy' => 'category', 'field' => 'id', 'terms' => $province);
                hocwp_query_sanitize_tax_query($tax_item, $tax_query);
            }
            if (hocwp_id_number_valid($district)) {
                $tax_item = array('taxonomy' => 'category', 'field' => 'id', 'terms' => $district);
                hocwp_query_sanitize_tax_query($tax_item, $tax_query);
            }
            if (hocwp_id_number_valid($ward)) {
                $tax_item = array('taxonomy' => 'category', 'field' => 'id', 'terms' => $ward);
                hocwp_query_sanitize_tax_query($tax_item, $tax_query);
            }
            if (hocwp_id_number_valid($street)) {
                $tax_item = array('taxonomy' => 'category', 'field' => 'id', 'terms' => $street);
                hocwp_query_sanitize_tax_query($tax_item, $tax_query);
            }
            unset($query->query['price']);
            unset($query->query_vars['price']);
            if (hocwp_id_number_valid($price)) {
                $tax_item = array('taxonomy' => 'price', 'field' => 'id', 'terms' => $price);
                hocwp_query_sanitize_tax_query($tax_item, $tax_query);
            }
            unset($query->query['acreage']);
            unset($query->query_vars['acreage']);
            if (hocwp_id_number_valid($acreage)) {
                $tax_item = array('taxonomy' => 'acreage', 'field' => 'id', 'terms' => $acreage);
                hocwp_query_sanitize_tax_query($tax_item, $tax_query);
            }
            if (hocwp_id_number_valid($object)) {
                $tax_item = array('taxonomy' => 'classifieds_object', 'field' => 'id', 'terms' => $object);
                hocwp_query_sanitize_tax_query($tax_item, $tax_query);
            }
            unset($query->query['salary']);
            unset($query->query_vars['salary']);
            if (hocwp_id_number_valid($salary)) {
                $tax_item = array('taxonomy' => 'salary', 'field' => 'id', 'terms' => $salary);
                hocwp_query_sanitize_tax_query($tax_item, $tax_query);
            }
            $tax_query = hocwp_get_value_by_key($tax_query, 'tax_query', $tax_query);
            $tax_query['relation'] = 'AND';
            $query->set('tax_query', $tax_query);
            $query->set('post_type', 'post');
        }
    }
    return $query;
}