| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | if ( ! defined( 'ABSPATH' ) ) { |
| 14: | exit; |
| 15: | } |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | function epl_shortcode_listing_category_callback( $atts ) { |
| 36: | $property_types = epl_get_active_post_types(); |
| 37: | if ( ! empty( $property_types ) ) { |
| 38: | $property_types = array_keys( $property_types ); |
| 39: | } |
| 40: | |
| 41: | $attributes = shortcode_atts( |
| 42: | array( |
| 43: | 'post_type' => $property_types, |
| 44: | 'status' => array( 'current', 'sold', 'leased' ), |
| 45: | 'commercial_listing_type' => '', |
| 46: | 'category_key' => '', |
| 47: | 'category_value' => '', |
| 48: | 'category_compare' => 'IN', |
| 49: | 'limit' => '10', |
| 50: | 'offset' => '', |
| 51: | 'author' => '', |
| 52: | 'agent' => '', |
| 53: | 'template' => false, |
| 54: | 'location' => '', |
| 55: | 'tools_top' => 'off', |
| 56: | 'tools_bottom' => 'off', |
| 57: | 'sortby' => '', |
| 58: | 'sort_order' => 'DESC', |
| 59: | 'pagination' => 'on', |
| 60: | 'instance_id' => '1', |
| 61: | 'class' => '', |
| 62: | ), |
| 63: | $atts |
| 64: | ); |
| 65: | |
| 66: | $post_type = $attributes['post_type']; |
| 67: | $status = $attributes['status']; |
| 68: | $commercial_listing_type = $attributes['commercial_listing_type']; |
| 69: | $category_key = $attributes['category_key']; |
| 70: | $category_value = $attributes['category_value']; |
| 71: | $category_compare = $attributes['category_compare']; |
| 72: | $limit = $attributes['limit']; |
| 73: | $offset = $attributes['offset']; |
| 74: | $author = $attributes['author']; |
| 75: | $agent = $attributes['agent']; |
| 76: | $template = $attributes['template']; |
| 77: | $location = $attributes['location']; |
| 78: | $tools_top = $attributes['tools_top']; |
| 79: | $tools_bottom = $attributes['tools_bottom']; |
| 80: | $sortby = $attributes['sortby']; |
| 81: | $sort_order = $attributes['sort_order']; |
| 82: | $pagination = $attributes['pagination']; |
| 83: | $instance_id = $attributes['instance_id']; |
| 84: | $class = $attributes['class']; |
| 85: | |
| 86: | if ( empty( $post_type ) ) { |
| 87: | return; |
| 88: | } |
| 89: | |
| 90: | if ( is_string( $post_type ) && 'rental' === $post_type ) { |
| 91: | $meta_key_price = 'property_rent'; |
| 92: | } else { |
| 93: | $meta_key_price = 'property_price'; |
| 94: | } |
| 95: | |
| 96: | $sort_options = array( |
| 97: | 'price' => $meta_key_price, |
| 98: | 'date' => 'post_date', |
| 99: | ); |
| 100: | if ( ! is_array( $post_type ) ) { |
| 101: | $post_type = array_map( 'trim', explode( ',', $post_type ) ); |
| 102: | } |
| 103: | ob_start(); |
| 104: | $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; |
| 105: | $args = array( |
| 106: | 'post_type' => $post_type, |
| 107: | 'posts_per_page' => $limit, |
| 108: | 'paged' => $paged, |
| 109: | ); |
| 110: | |
| 111: | |
| 112: | if ( ! empty( $offset ) ) { |
| 113: | $args['offset'] = $offset; |
| 114: | $pagination = 'off'; |
| 115: | } |
| 116: | |
| 117: | if ( ! empty( $location ) ) { |
| 118: | if ( ! is_array( $location ) ) { |
| 119: | $location = explode( ',', $location ); |
| 120: | $location = array_map( 'trim', $location ); |
| 121: | |
| 122: | $args['tax_query'][] = array( |
| 123: | 'taxonomy' => 'location', |
| 124: | 'field' => 'slug', |
| 125: | 'terms' => $location, |
| 126: | ); |
| 127: | } |
| 128: | } |
| 129: | |
| 130: | if ( ! empty( $status ) ) { |
| 131: | if ( ! is_array( $status ) ) { |
| 132: | $status = explode( ',', $status ); |
| 133: | $status = array_map( 'trim', $status ); |
| 134: | |
| 135: | $args['meta_query'][] = array( |
| 136: | 'key' => 'property_status', |
| 137: | 'value' => $status, |
| 138: | 'compare' => 'IN', |
| 139: | ); |
| 140: | } |
| 141: | } |
| 142: | |
| 143: | |
| 144: | if ( ! empty( $author ) ) { |
| 145: | $author = (array) $author; |
| 146: | if ( is_array( $author ) ) { |
| 147: | $author_ids = array_map( 'epl_get_author_id_from_name', $author ); |
| 148: | $author = implode( ',', $author_ids ); |
| 149: | } |
| 150: | $args['author'] = trim( $author ); |
| 151: | } |
| 152: | |
| 153: | |
| 154: | if ( ! empty( $agent ) ) { |
| 155: | |
| 156: | $agent = array_map( 'trim', explode( ',', $agent ) ); |
| 157: | $agent = array_filter( $agent ); |
| 158: | $agent_meta_query = array( |
| 159: | 'relation' => 'OR', |
| 160: | ); |
| 161: | |
| 162: | foreach ( $agent as $single_agent ) { |
| 163: | $agent_meta_query[] = array( |
| 164: | 'key' => 'property_agent', |
| 165: | 'value' => array( $single_agent, sanitize_user( $single_agent ) ), |
| 166: | 'compare' => 'IN', |
| 167: | ); |
| 168: | $agent_meta_query[] = array( |
| 169: | 'key' => 'property_second_agent', |
| 170: | 'value' => array( $single_agent, sanitize_user( $single_agent ) ), |
| 171: | 'compare' => 'IN', |
| 172: | ); |
| 173: | |
| 174: | if ( in_array( 'commercial', $post_type, true ) || |
| 175: | in_array( 'commercial_land', $post_type, true ) || |
| 176: | in_array( 'business', $post_type, true ) |
| 177: | ) { |
| 178: | |
| 179: | $agent_meta_query[] = array( |
| 180: | 'key' => 'property_third_agent', |
| 181: | 'value' => array( $single_agent, sanitize_user( $single_agent ) ), |
| 182: | 'compare' => 'IN', |
| 183: | ); |
| 184: | $agent_meta_query[] = array( |
| 185: | 'key' => 'property_fourth_agent', |
| 186: | 'value' => array( $single_agent, sanitize_user( $single_agent ) ), |
| 187: | 'compare' => 'IN', |
| 188: | ); |
| 189: | |
| 190: | } |
| 191: | } |
| 192: | |
| 193: | $args['meta_query'][] = $agent_meta_query; |
| 194: | } |
| 195: | |
| 196: | if ( ! empty( $commercial_listing_type ) ) { |
| 197: | if ( ! is_array( $commercial_listing_type ) ) { |
| 198: | $commercial_listing_type = explode( ',', $commercial_listing_type ); |
| 199: | $commercial_listing_type = array_map( 'trim', $commercial_listing_type ); |
| 200: | |
| 201: | $args['meta_query'][] = array( |
| 202: | 'key' => 'property_com_listing_type', |
| 203: | 'value' => $commercial_listing_type, |
| 204: | 'compare' => 'IN', |
| 205: | ); |
| 206: | } |
| 207: | } |
| 208: | |
| 209: | if ( ! empty( $category_key ) && ! empty( $category_value ) ) { |
| 210: | |
| 211: | if ( ! is_array( $category_value ) && in_array( $category_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ), true ) ) { |
| 212: | $category_value = explode( ',', $category_value ); |
| 213: | $category_value = array_map( 'trim', $category_value ); |
| 214: | } |
| 215: | |
| 216: | $this_meta_query = array( |
| 217: | 'key' => $category_key, |
| 218: | 'value' => $category_value, |
| 219: | 'compare' => $category_compare, |
| 220: | ); |
| 221: | |
| 222: | if ( in_array( $category_compare, array( 'BETWEEN', 'NOT BETWEEN' ), true ) ) { |
| 223: | |
| 224: | if ( is_numeric( $category_value[0] ) ) { |
| 225: | $this_meta_query['type'] = 'numeric'; |
| 226: | } |
| 227: | } |
| 228: | $args['meta_query'][] = $this_meta_query; |
| 229: | } |
| 230: | |
| 231: | if ( ! empty( $sortby ) ) { |
| 232: | |
| 233: | if ( 'price' === $sortby ) { |
| 234: | $args['orderby'] = 'meta_value_num'; |
| 235: | $args['meta_key'] = $meta_key_price; |
| 236: | } elseif ( 'rand' === $sortby ) { |
| 237: | $args['orderby'] = 'rand'; |
| 238: | } elseif ( 'status' === $sortby ) { |
| 239: | $args['orderby'] = 'meta_value'; |
| 240: | $args['meta_key'] = 'property_status'; |
| 241: | } else { |
| 242: | $args['orderby'] = 'post_date'; |
| 243: | } |
| 244: | $args['order'] = $sort_order; |
| 245: | } |
| 246: | |
| 247: | $args['instance_id'] = $attributes['instance_id']; |
| 248: | |
| 249: | $args = epl_add_orderby_args( $args, 'shortcode', 'listing_category' ); |
| 250: | |
| 251: | |
| 252: | $args = apply_filters( 'epl_shortcode_listing_category_args', $args, $attributes ); |
| 253: | |
| 254: | $query_open = new WP_Query( $args ); |
| 255: | if ( $query_open->have_posts() ) { ?> |
| 256: | <div class="loop epl-shortcode"> |
| 257: | <div class="loop-content epl-shortcode-listing-category <?php echo ' ' . esc_attr( epl_template_class( $template, 'archive' ) ) . ' ' . esc_attr( $attributes['class'] ); ?>"> |
| 258: | <?php |
| 259: | if ( 'on' === $tools_top ) { |
| 260: | do_action( 'epl_property_loop_start', $attributes ); |
| 261: | } |
| 262: | while ( $query_open->have_posts() ) { |
| 263: | $query_open->the_post(); |
| 264: | |
| 265: | $template = str_replace( '_', '-', $template ); |
| 266: | epl_property_blog( $template ); |
| 267: | } |
| 268: | if ( 'on' === $tools_bottom ) { |
| 269: | do_action( 'epl_property_loop_end' ); |
| 270: | } |
| 271: | ?> |
| 272: | </div> |
| 273: | <div class="loop-footer"> |
| 274: | <?php |
| 275: | if ( 'on' === $pagination ) { |
| 276: | do_action( 'epl_pagination', array( 'query' => $query_open ) ); |
| 277: | } |
| 278: | ?> |
| 279: | </div> |
| 280: | </div> |
| 281: | <?php |
| 282: | } else { |
| 283: | do_action( 'epl_shortcode_results_message', 'category' ); |
| 284: | } |
| 285: | wp_reset_postdata(); |
| 286: | return ob_get_clean(); |
| 287: | } |
| 288: | add_shortcode( 'listing_category', 'epl_shortcode_listing_category_callback' ); |
| 289: | |