/** * * @global object $wpdb WordPress Database object. * * @param $atts * @return string */ function geodir_sc_popular_in_neighbourhood($atts) { ob_start(); $defaults = array('post_type' => 'gd_place', 'category' => '0', 'list_sort' => 'latest', 'post_number' => 5, 'layout' => 'gridview_onehalf', 'character_count' => 20, 'add_location_filter' => 1); $params = shortcode_atts($defaults, $atts); /** * Being validating $params */ // Check we have a valid post_type if (!gdsc_is_post_type_valid($params['post_type'])) { $params['post_type'] = 'gd_place'; } // Manage the entered categories if (0 != $params['category'] || '' != $params['category']) { $params['category'] = gdsc_manage_category_choice($params['post_type'], $params['category']); } // Validate our sorting choice $params['list_sort'] = gdsc_validate_sort_choice($params['list_sort']); // Post_number needs to be a positive integer $params['post_number'] = absint($params['post_number']); if (0 == $params['post_number']) { $params['post_number'] = 1; } // Validate our layout choice // Outside of the norm, I added some more simple terms to match the existing // So now I just run the switch to set it properly. $params['layout'] = gdsc_validate_layout_choice($params['layout']); // Validate character_count $params['character_count'] = absint($params['character_count']); if (20 > $params['character_count']) { $params['character_count'] = 20; } /** * End validation */ global $wpdb, $post, $geodir_post_type; if ($geodir_post_type == '') { $geodir_post_type = 'gd_place'; } $all_postypes = geodir_get_posttypes(); $location_id = ''; $not_in_array = array(); if (geodir_is_page('detail') || geodir_is_page('preview') || geodir_is_page('add-listing')) { if (isset($post->post_type) && $post->post_type == $params['post_type'] && isset($post->post_location_id)) { $not_in_array[] = $post->ID; $location_id = $post->post_location_id; } } elseif (in_array($geodir_post_type, $all_postypes) && $geodir_post_type == $params['post_type']) { if (isset($_SESSION['gd_city']) && $_SESSION['gd_city'] != '') { $location_id = $wpdb->get_var($wpdb->prepare("SELECT location_id FROM " . POST_LOCATION_TABLE . " WHERE city_slug = %s", array($_SESSION['gd_city']))); } else { $default_location = geodir_get_default_location(); $location_id = $default_location->location_id; } } $gd_neighbourhoods = geodir_get_neighbourhoods($location_id); if ($gd_neighbourhoods) { ?> <div class="geodir_locations geodir_location_listing"> <?php $hood_slug_arr = array(); if (!empty($gd_neighbourhoods)) { foreach ($gd_neighbourhoods as $hoodslug) { $hood_slug_arr[] = $hoodslug->hood_slug; } } $query_args = array('posts_per_page' => $params['post_number'], 'is_geodir_loop' => true, 'post__not_in' => $not_in_array, 'gd_neighbourhood' => $hood_slug_arr, 'gd_location' => $params['add_location_filter'] ? true : false, 'post_type' => $params['post_type'], 'order_by' => $params['list_sort'], 'excerpt_length' => $params['character_count']); if ($params['category'] != 0 || $params['category'] != '') { $category_taxonomy = geodir_get_taxonomies($params['post_type']); $tax_query = array('taxonomy' => $category_taxonomy[0], 'field' => 'id', 'terms' => $params['category']); $query_args['tax_query'] = array($tax_query); } global $gridview_columns; query_posts($query_args); if (strstr($params['layout'], 'gridview')) { $listing_view_exp = explode('_', $params['layout']); $gridview_columns = $params['layout']; $layout = $listing_view_exp[0]; } $template = apply_filters("geodir_template_part-listing-listview", geodir_plugin_path() . '/geodirectory-templates/listing-listview.php'); include $template; wp_reset_query(); ?> </div> <?php } $output = ob_get_contents(); ob_end_clean(); return $output; }
/** * The geodirectory listings shortcode. * * This implements the functionality of the shortcode for displaying geodirectory listings. * * @since 1.4.2 * * @param array $atts { * Attributes of the shortcode. * * @type string $title The title to be displayed above listings. * @type string $post_type Post type of listing. Default gd_place. * @type string $category Category ids to filter listings. Ex: 1,3. Default empty. * @type string $list_sort Sorting for listings. Should be from az, latest, featured, high_review, high_rating.Default 'latest'. * @type string $event_type Event type filter. Should today, upcoming, past, all. Default empty. For post type gd_event only. * @type int $post_number No. of post to display. Default 10. * @type string $layout Layout to display listing. Should be gridview_onehalf, gridview_onethird gridview_onefourth, gridview_onefifth, list. Default 'gridview_onehalf'. * @type string $listing_width Listing width. Optional * @type int $character_count The excerpt length of content * @type int|bool $add_location_filter Filter listings using current location. Default 1. * @type int|bool $show_featured_only Display only featured listings. Default empty. * @type int|bool $show_special_only Display only special offers listings. Default empty. * @type int|bool $with_pics_only Display listings which has image available. Default empty. * @type int|bool $with_videos_only Display listings which has video available. Default empty. * @type int|bool $with_pagination Display pagination for listings. Default 1. * @type int|bool $top_pagination Display pagination on top of listings. Default 0. Required $with_pagination true. * @type int|bool $bottom_pagination Display pagination on bottom of listings. Default 1. Required $with_pagination true. * } * @param string $content The enclosed content. Optional. * @return string HTML content to display geodirectory listings. */ function geodir_sc_gd_listings($atts, $content = '') { $defaults = array('title' => '', 'post_type' => 'gd_place', 'category' => 0, 'list_sort' => 'latest', 'event_type' => '', 'post_number' => 10, 'layout' => 'gridview_onehalf', 'listing_width' => '', 'character_count' => 20, 'add_location_filter' => 1, 'show_featured_only' => '', 'show_special_only' => '', 'with_pics_only' => '', 'with_videos_only' => '', 'with_pagination' => '1', 'top_pagination' => '0', 'bottom_pagination' => '1'); $params = shortcode_atts($defaults, $atts); $params['title'] = wp_strip_all_tags($params['title']); $params['post_type'] = gdsc_is_post_type_valid($params['post_type']) ? $params['post_type'] : 'gd_place'; // Validate the selected category/ies - Grab the current list based on post_type $category_taxonomy = geodir_get_taxonomies($params['post_type']); $categories = get_terms($category_taxonomy, array('orderby' => 'count', 'order' => 'DESC', 'fields' => 'ids')); // Make sure we have an array if (!is_array($params['category'])) { $params['category'] = explode(',', $params['category']); } // Array_intersect returns only the items in $params['category'] that are also in our category list // Otherwise it becomes empty and later on that will mean "All" $params['category'] = array_intersect($params['category'], $categories); // Post_number needs to be a positive integer $params['post_number'] = absint($params['post_number']); $params['post_number'] = $params['post_number'] > 0 ? $params['post_number'] : 10; // Validate character_count //todo: is this necessary? $params['character_count'] = $params['character_count']; // Validate our layout choice // Outside of the norm, I added some more simple terms to match the existing // So now I just run the switch to set it properly. $params['layout'] = gdsc_validate_layout_choice($params['layout']); // Validate our sorting choice $params['list_sort'] = gdsc_validate_sort_choice($params['list_sort']); // Validate Listing width, used in the template widget-listing-listview.php // The context is in width=$listing_width% - So we need a positive number between 0 & 100 $params['listing_width'] = gdsc_validate_listing_width($params['listing_width']); // Validate the checkboxes used on the widget $params['add_location_filter'] = gdsc_to_bool_val($params['add_location_filter']); $params['show_featured_only'] = gdsc_to_bool_val($params['show_featured_only']); $params['show_special_only'] = gdsc_to_bool_val($params['show_special_only']); $params['with_pics_only'] = gdsc_to_bool_val($params['with_pics_only']); $params['with_videos_only'] = gdsc_to_bool_val($params['with_videos_only']); $params['with_pagination'] = gdsc_to_bool_val($params['with_pagination']); $params['top_pagination'] = gdsc_to_bool_val($params['top_pagination']); $params['bottom_pagination'] = gdsc_to_bool_val($params['bottom_pagination']); /** * End of validation */ if (isset($atts['geodir_ajax'])) { $params['geodir_ajax'] = $atts['geodir_ajax']; unset($atts['geodir_ajax']); } if (isset($atts['pageno'])) { $params['pageno'] = $atts['pageno']; unset($atts['pageno']); } $params['shortcode_atts'] = $atts; $output = geodir_sc_gd_listings_output($params); return $output; }
/** * Generates popular post category HTML. * * @since 1.0.0 * @since 1.5.1 Added option to set default post type. * @package GeoDirectory * @global object $wpdb WordPress Database object. * @global string $plugin_prefix Geodirectory plugin table prefix. * @global string $geodir_post_category_str The geodirectory post category. * @param array|string $args Display arguments including before_title, after_title, before_widget, and after_widget. * @param array|string $instance The settings for the particular instance of the widget. */ function geodir_popular_post_category_output($args = '', $instance = '') { // prints the widget global $wpdb, $plugin_prefix, $geodir_post_category_str; extract($args, EXTR_SKIP); echo $before_widget; /** This filter is documented in geodirectory_widgets.php */ $title = empty($instance['title']) ? __('Popular Categories', 'geodirectory') : apply_filters('widget_title', __($instance['title'], 'geodirectory')); $gd_post_type = geodir_get_current_posttype(); $category_limit = isset($instance['category_limit']) && $instance['category_limit'] > 0 ? (int) $instance['category_limit'] : 15; $default_post_type = !empty($gd_post_type) ? $gd_post_type : (isset($instance['default_post_type']) && gdsc_is_post_type_valid($instance['default_post_type']) ? $instance['default_post_type'] : ''); $taxonomy = array(); if (!empty($gd_post_type)) { $taxonomy[] = $gd_post_type . "category"; } else { $taxonomy = geodir_get_taxonomies($gd_post_type); } $terms = get_terms($taxonomy); $a_terms = array(); $b_terms = array(); foreach ($terms as $term) { if ($term->count > 0) { $a_terms[$term->taxonomy][] = $term; } } if (!empty($a_terms)) { foreach ($a_terms as $b_key => $b_val) { $b_terms[$b_key] = geodir_sort_terms($b_val, 'count'); } $default_taxonomy = $default_post_type != '' && isset($b_terms[$default_post_type . 'category']) ? $default_post_type . 'category' : ''; $tax_change_output = ''; if (count($b_terms) > 1) { $tax_change_output .= "<select data-limit='{$category_limit}' class='geodir-cat-list-tax' onchange='geodir_get_post_term(this);'>"; foreach ($b_terms as $key => $val) { $ptype = get_post_type_object(str_replace("category", "", $key)); $tax_change_output .= "<option value='{$key}' " . selected($key, $default_taxonomy, false) . ">" . __($ptype->labels->singular_name, 'geodirectory') . " " . __('Categories', 'geodirectory') . "</option>"; } $tax_change_output .= "</select>"; } if (!empty($b_terms)) { $terms = $default_taxonomy != '' && isset($b_terms[$default_taxonomy]) ? $b_terms[$default_taxonomy] : reset($b_terms); // get the first array global $cat_count; //make global so we can change via function $cat_count = 0; ?> <div class="geodir-category-list-in clearfix"> <div class="geodir-cat-list clearfix"> <?php echo $before_title . __($title) . $after_title; echo $tax_change_output; echo '<ul class="geodir-popular-cat-list">'; geodir_helper_cat_list_output($terms, $category_limit); echo '</ul>'; ?> </div> <?php $hide = ''; if ($cat_count < $category_limit) { $hide = 'style="display:none;"'; } echo "<div class='geodir-cat-list-more' {$hide} >"; echo '<a href="javascript:void(0)" class="geodir-morecat geodir-showcat">' . __('More Categories', 'geodirectory') . '</a>'; echo '<a href="javascript:void(0)" class="geodir-morecat geodir-hidecat geodir-hide">' . __('Less Categories', 'geodirectory') . '</a>'; echo "</div>"; /* add scripts */ add_action('wp_footer', 'geodir_popular_category_add_scripts', 100); ?> </div> <?php } } echo $after_widget; }
/** * Get the category id from category name/slug. * * @since 1.0.0 * * @param string $post_type Post type. * @param string $category Post category. * @return int Term id. */ function gdsc_manage_category_choice($post_type, $category) { if (0 == $category || '' == $category) { return ''; } if (!gdsc_is_post_type_valid($post_type)) { return ''; } $taxonomies = geodir_get_taxonomies($post_type); $categories = get_terms(array('taxonomy' => $taxonomies[0])); $cat_id = 0; foreach ($categories as $cat) { if (is_numeric($category)) { if (absint($category) == $cat->term_id) { $cat_id = $cat->term_id; break; } } else { if ($category == $cat->slug) { $cat_id = $cat->term_id; break; } if ($category == $cat->name) { $cat_id = $cat->term_id; break; } } } return $cat_id; }