'.__( 'Invalid Captcha: Please try again.', 'advanced-classifieds-and-directory-pro' ).''; exit(); } } $this->save_listing(); } } /** * Parse request to find correct WordPress query. * * @since 1.0.0 * @access public * * @param WP_Query $wp WordPress Query object. */ public function parse_request( $wp ) { if( array_key_exists( 'acadp_action', $wp->query_vars ) && array_key_exists( 'acadp_listing', $wp->query_vars ) && (int) $wp->query_vars['acadp_listing'] > 0 ) { $id = (int) $wp->query_vars['acadp_listing']; if( 'renew' == $wp->query_vars['acadp_action'] ) { if( ! acadp_current_user_can('edit_acadp_listing', $id) ) { return; } $this->renew_listing( $id ); } if( 'delete' == $wp->query_vars['acadp_action'] ) { if( ! acadp_current_user_can('delete_acadp_listing', $id) ) { return; } $this->delete_listing( $id ); } if( 'remove-favourites' == $wp->query_vars['acadp_action'] ) { $this->remove_favourites( $id ); } } } /** * Process the shortcode [acadp_user_listings]. * * @since 1.0.0 * @access public */ public function run_shortcode_user_listings() { $shortcode = 'acadp_user_listings'; if( $user_slug = get_query_var( 'acadp_user' ) ) { $general_settings = get_option( 'acadp_general_settings' ); $listings_settings = get_option( 'acadp_listings_settings' ); $featured_listing_settings = get_option( 'acadp_featured_listing_settings' ); // Enqueue style dependencies wp_enqueue_style( ACADP_PLUGIN_NAME ); // Enqueue script dependencies if( wp_script_is( ACADP_PLUGIN_NAME.'-bootstrap', 'registered' ) ) { wp_enqueue_script( ACADP_PLUGIN_NAME.'-bootstrap' ); } wp_enqueue_script( ACADP_PLUGIN_NAME ); // ... $can_show_header = empty( $listings_settings['display_in_header'] ) ? false : true; $can_show_listings_count = $can_show_header && in_array( 'listings_count', $listings_settings['display_in_header'] ) ? true : false; $can_show_views_selector = $can_show_header && in_array( 'views_selector', $listings_settings['display_in_header'] ) ? true : false; $can_show_orderby_dropdown = $can_show_header && in_array( 'orderby_dropdown', $listings_settings['display_in_header'] ) ? true : false; $can_show_date = isset( $listings_settings['display_in_listing'] ) && in_array( 'date', $listings_settings['display_in_listing'] ) ? true : false; $can_show_user = isset( $listings_settings['display_in_listing'] ) && in_array( 'user', $listings_settings['display_in_listing'] ) ? true : false; $can_show_category = isset( $listings_settings['display_in_listing'] ) && in_array( 'category', $listings_settings['display_in_listing'] ) ? true : false; $can_show_views = isset( $listings_settings['display_in_listing'] ) && in_array( 'views', $listings_settings['display_in_listing'] ) ? true : false; $can_show_images = empty( $general_settings['has_images'] ) ? false : true; $sort_by_featured = empty( $featured_listing_settings['enabled'] ) ? false : true; $current_order = isset( $_GET['order'] ) ? $_GET['order'] : ( $listings_settings['orderby'].'-'.$listings_settings['order'] ); $can_show_pagination = 1; $has_price = empty( $general_settings['has_price'] ) ? false : true; $can_show_price = false; if( $has_price ) { $can_show_price = isset( $listings_settings['display_in_listing'] ) && in_array( 'price', $listings_settings['display_in_listing'] ) ? true : false; } $has_location = empty( $general_settings['has_location'] ) ? false : true; $can_show_location = false; if( $has_location ) { $can_show_location = isset( $listings_settings['display_in_listing'] ) && in_array( 'location', $listings_settings['display_in_listing'] ) ? true : false; } $span = 12; if( $can_show_images ) $span = $span - 2; if( $can_show_price ) $span = $span - 3; $span_middle = 'col-md-'.$span; // Define the query $paged = ( get_query_var('paged') ) ? absint( get_query_var('paged') ) : 1; $args = array( 'post_type' => 'acadp_listings', 'posts_per_page' => ! empty( $listings_settings['listings_per_page'] ) ? $listings_settings['listings_per_page'] : -1, 'paged' => $paged, 'author_name' => $user_slug, ); if( $has_location && $general_settings['base_location'] > 0 ) { $args['tax_query'] = array( array( 'taxonomy' => 'acadp_locations', 'field' => 'term_id', 'terms' => $general_settings['base_location'], 'include_children' => true, ), ); } switch( $current_order ) { case 'title-asc' : if( $sort_by_featured ) { $args['meta_key'] = 'featured'; $args['orderby'] = array( 'meta_value_num' => 'DESC', 'title' => 'ASC', ); } else { $args['orderby'] = 'title'; $args['order'] = 'ASC'; }; break; case 'title-desc' : if( $sort_by_featured ) { $args['meta_key'] = 'featured'; $args['orderby'] = array( 'meta_value_num' => 'DESC', 'title' => 'DESC', ); } else { $args['orderby'] = 'title'; $args['order'] = 'DESC'; }; break; case 'date-asc' : if( $sort_by_featured ) { $args['meta_key'] = 'featured'; $args['orderby'] = array( 'meta_value_num' => 'DESC', 'date' => 'ASC', ); } else { $args['orderby'] = 'date'; $args['order'] = 'ASC'; }; break; case 'date-desc' : if( $sort_by_featured ) { $args['meta_key'] = 'featured'; $args['orderby'] = array( 'meta_value_num' => 'DESC', 'date' => 'DESC', ); } else { $args['orderby'] = 'date'; $args['order'] = 'DESC'; }; break; case 'price-asc' : if( $sort_by_featured ) { $args['meta_query'] = array( 'relation' => 'AND', 'featured' => array( 'key' => 'featured', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), 'price' => array( 'key' => 'price', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), ); $args['orderby'] = array( 'featured' => 'DESC', 'price' => 'ASC', ); } else { $args['meta_key'] = 'price'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'ASC'; }; break; case 'price-desc' : if( $sort_by_featured ) { $args['meta_query'] = array( 'relation' => 'AND', 'featured' => array( 'key' => 'featured', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), 'price' => array( 'key' => 'price', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), ); $args['orderby'] = array( 'featured' => 'DESC', 'price' => 'DESC', ); } else { $args['meta_key'] = 'price'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'DESC'; }; break; case 'views-asc' : if( $sort_by_featured ) { $args['meta_query'] = array( 'relation' => 'AND', 'featured' => array( 'key' => 'featured', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), 'views' => array( 'key' => 'views', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), ); $args['orderby'] = array( 'featured' => 'DESC', 'views' => 'ASC', ); } else { $args['meta_key'] = 'views'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'ASC'; }; break; case 'views-desc' : if( $sort_by_featured ) { $args['meta_query'] = array( 'relation' => 'AND', 'featured' => array( 'key' => 'featured', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), 'views' => array( 'key' => 'views', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), ); $args['orderby'] = array( 'featured' => 'DESC', 'views' => 'DESC', ); } else { $args['meta_key'] = 'views'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'DESC'; }; break; } $acadp_query = new WP_Query( $args ); // Start the Loop global $post; // Process output if( $acadp_query->have_posts() ) { $view = acadp_get_listings_current_view_name(); ob_start(); include( acadp_get_template( "listings/acadp-public-listings-$view-display.php" ) ); wp_reset_postdata(); // Use reset postdata to restore orginal query return ob_get_clean(); } else { return ''.__( 'No Results Found.', 'advanced-classifieds-and-directory-pro' ).''; } } } /** * Process the shortcode [acadp_user_dashboard]. * * @since 1.0.0 * @access public */ public function run_shortcode_user_dashboard() { if( ! is_user_logged_in() ) { return acadp_login_form(); } $shortcode = 'acadp_user_dashboard'; // Enqueue style dependencies wp_enqueue_style( ACADP_PLUGIN_NAME ); // Enqueue script dependencies if( wp_script_is( ACADP_PLUGIN_NAME.'-bootstrap', 'registered' ) ) { wp_enqueue_script( ACADP_PLUGIN_NAME.'-bootstrap' ); } wp_enqueue_script( ACADP_PLUGIN_NAME ); // ... ob_start(); include( acadp_get_template( "user/acadp-public-user-dashboard-display.php" ) ); return ob_get_clean(); } /** * Process the shortcode [acadp_listing_form]. * * @since 1.0.0 * @access public */ public function run_shortcode_listing_form() { if( ! is_user_logged_in() ) { return acadp_login_form(); } $post_id = 'edit' == get_query_var( 'acadp_action' ) ? get_query_var( 'acadp_listing', 0 ) : 0; $has_permission = true; if( $post_id > 0 ) { if( ! acadp_current_user_can('edit_acadp_listing', $post_id) ) $has_permission = false; } else if( ! acadp_current_user_can('edit_acadp_listings') ) { $has_permission = false; } if( ! $has_permission ) { return __( 'You do not have sufficient permissions to access this page.', 'advanced-classifieds-and-directory-pro' ); } $shortcode = 'acadp_listing_form'; $general_settings = get_option( 'acadp_general_settings' ); $recaptcha_settings = get_option( 'acadp_recaptcha_settings' ); // Enqueue style dependencies wp_enqueue_style( ACADP_PLUGIN_NAME ); // Enqueue script dependencies wp_enqueue_script( 'jquery-form', array('jquery'), false, true ); wp_enqueue_script( "jquery-ui-sortable" ); if( wp_script_is( ACADP_PLUGIN_NAME.'-bootstrap', 'registered' ) ) { wp_enqueue_script( ACADP_PLUGIN_NAME.'-bootstrap' ); } wp_enqueue_script( ACADP_PLUGIN_NAME.'-validator' ); wp_enqueue_script( ACADP_PLUGIN_NAME ); wp_enqueue_script( ACADP_PLUGIN_NAME.'-google-map' ); if( isset( $recaptcha_settings['forms'] ) && in_array( 'listing', $recaptcha_settings['forms'] ) ) { wp_enqueue_script( ACADP_PLUGIN_NAME . "-recaptcha" ); } // ... $has_draft = 1; $category = 0; $default_location = ''; $can_add_price = empty( $general_settings['has_price'] ) ? false : true; $can_add_images = empty( $general_settings['has_images'] ) ? false : true; $can_add_video = empty( $general_settings['has_video'] ) ? false : true; $can_add_location = empty( $general_settings['has_location'] ) ? false : true; if( $can_add_location ) { $location = ( $general_settings['default_location'] > 0 ) ? $general_settings['default_location'] : $general_settings['base_location']; if( $location > 0 ) { $term = get_term_by( 'id', $location, 'acadp_locations' ); $default_location = $term->name; } } if( $post_id > 0 ) { $post = get_post( $post_id ); setup_postdata( $post ); $post_meta = get_post_meta( $post_id); if( $post->post_status !== 'draft' ) { $has_draft = 0; } $category = wp_get_object_terms( $post_id, 'acadp_categories', array( 'fields' => 'ids' ) ); $category = $category[0]; if( $can_add_location ) { $location = wp_get_object_terms( $post_id, 'acadp_locations', array( 'fields' => 'ids' ) ); $location = ! empty( $location ) ? $location[0] : -1; } } ob_start(); include( acadp_get_template( "user/acadp-public-edit-listing-display.php" ) ); wp_reset_postdata(); // Restore global post data stomped by the_post() return ob_get_clean(); } /** * Display custom fields. * * @since 1.0.0 * @access public * * @param int $post_id Post ID. * @param int $taxonomy_id Custom taxonomy ID of the current post. */ public function ajax_callback_custom_fields( $post_id = 0, $taxonomy_id = 0 ) { $ajax = false; if( isset( $_POST['taxonomy_id'] ) ) { $ajax = true; $post_id = (int) $_POST['post_id']; $taxonomy_id = (int) $_POST['taxonomy_id']; } // Get post meta for the given post_id $post_meta = get_post_meta( $post_id ); // Get custom fields for the given taxonomy_id $args = array( 'post_type' => 'acadp_fields', 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'acadp_categories', 'field' => 'term_id', 'terms' => $taxonomy_id, 'include_children' => false, ), ), 'meta_key' => 'order', 'order_by' => 'meta_value_num', 'order' => 'ASC', ); $acadp_query = new WP_Query( $args ); // Start the Loop global $post; // Process output ob_start(); include( acadp_get_template( "user/acadp-public-custom-fields-display.php" ) ); wp_reset_postdata(); // Restore global post data stomped by the_post() $output = ob_get_clean(); print $output; if( $ajax ) { wp_die(); } } /** * Upload image. * * @since 1.0.0 * @access public */ public function ajax_callback_image_upload() { // var $post_id = 0; $data = array( 'error' => 0, 'error_message' => '', 'id' => 0, 'url' => '', 'delete_label' => __( 'Delete Permanently', 'advanced-classifieds-and-directory-pro' ) ); // require the needed files require_once( ABSPATH . "wp-admin" . '/includes/image.php' ); require_once( ABSPATH . "wp-admin" . '/includes/file.php' ); require_once( ABSPATH . "wp-admin" . '/includes/media.php' ); // then loop over the files that were sent and store them using media_handle_upload(); if( $_FILES ) { foreach( $_FILES as $file => $array ) { $mime = $_FILES[ $file ]['type']; if( ! in_array( $mime, array( 'image/jpeg', 'image/jpg', 'image/png' ) ) ) { $data['error'] = 1; $data['error_message'] = __( 'Invalid file format', 'advanced-classifieds-and-directory-pro' ); } if( $_FILES[$file]['error'] !== UPLOAD_ERR_OK ) { $data['error'] = 1; $data['error_message'] = $_FILES[ $file ]['error']; } if( $data['error'] == 0 ) { $data['id'] = media_handle_upload( $file, $post_id ); $image = wp_get_attachment_image_src( $data['id'] ); $data['url'] = $image[0]; } } } // and if you want to set that image as Post then use: echo wp_json_encode( $data ); wp_die(); } /** * Delete an attachment. * * @since 1.0.0 * @access public */ public function ajax_callback_delete_attachment() { if( isset( $_POST['attachment_id'] ) ) { wp_delete_attachment( $_POST['attachment_id'], true ); } wp_die(); } /** * Save Listing. * * @since 1.0.0 * @access private */ private function save_listing() { $is_initial_submission = 1; $is_listing_edited = 0; $general_settings = get_option( 'acadp_general_settings' ); $new_listing_status = apply_filters( 'acadp_new_listing_status', $general_settings['new_listing_status'] ); $post_status = $new_listing_status; if( isset( $_POST['post_id'] ) ) { $post_id = (int) $_POST['post_id']; $post_status = get_post_status( $post_id ); if( $post_status === 'draft' ) { $post_status = $new_listing_status; } else { $post_status = $general_settings['edit_listing_status']; $is_initial_submission = 0; $is_listing_edited = 1; } } if( isset( $_POST['action'] ) && __( 'Save Draft', 'advanced-classifieds-and-directory-pro' ) == $_POST['action'] ) { $post_status = 'draft'; $is_initial_submission = 0; } // Add the content of the form to $post as an array $post_array = array( 'post_title' => $_POST['title'], 'post_content' => $_POST['description'], 'post_status' => $post_status, 'post_type' => 'acadp_listings' ); if( isset( $_POST['post_id'] ) ) { //update the existing post $post_array['ID'] = $post_id; wp_update_post( $post_array ); } else { //save the new post $post_array['post_author'] = get_current_user_id(); $post_id = wp_insert_post( $post_array ); } if( $post_id ) { // insert category taxonomy wp_set_object_terms( $post_id, (int) $_POST['acadp_category'], 'acadp_categories' ); // insert custom fields if( isset( $_POST['acadp_fields'] ) ) { foreach( $_POST['acadp_fields'] as $key => $value ) { $type = get_post_meta( $key, 'type', true ); switch( $type ) { case 'text' : $value = sanitize_text_field( $value ); break; case 'textarea' : $value = esc_textarea( $value ); break; case 'select' : case 'radio' : $value = sanitize_text_field( $value ); break; case 'checkbox' : $value = array_map( 'esc_attr', $value ); $value = implode( "\n", $value ); break; default : $value = sanitize_text_field( $value ); } update_post_meta( $post_id, $key, $value ); } } // insert images if( ! empty( $general_settings['has_images'] ) && isset( $_POST['images'] ) ) { // OK to save meta data $images = array_filter( $_POST['images'] ); if( count( $images ) ) { update_post_meta( $post_id, 'images', $images ); } else { delete_post_meta( $post_id, 'images' ); } } else { // Nothing received, all fields are empty, delete option delete_post_meta( $post_id, 'images' ); } // insert video if( ! empty( $general_settings['has_video'] ) && isset( $_POST['video'] ) ) { $video = esc_url_raw( $_POST['video'] ); update_post_meta( $post_id, 'video', $video ); } // insert contact details if( ! empty( $general_settings['has_location'] ) ) { $address = esc_textarea( $_POST['address'] ); update_post_meta( $post_id, 'address', $address ); wp_set_object_terms( $post_id, (int) $_POST['acadp_location'], 'acadp_locations' ); $zipcode = sanitize_text_field( $_POST['zipcode'] ); update_post_meta( $post_id, 'zipcode', $zipcode ); $phone = sanitize_text_field( $_POST['phone'] ); update_post_meta( $post_id, 'phone', $phone ); $email = sanitize_email( $_POST['email'] ); update_post_meta( $post_id, 'email', $email ); $website = esc_url_raw( $_POST['website'] ); update_post_meta( $post_id, 'website', $website ); $latitude = isset( $_POST['latitude'] ) ? sanitize_text_field( $_POST['latitude'] ) : ''; update_post_meta( $post_id, 'latitude', $latitude ); $longitude = isset( $_POST['longitude'] ) ? sanitize_text_field( $_POST['longitude'] ) : ''; update_post_meta( $post_id, 'longitude', $longitude ); $hide_map = isset( $_POST['hide_map'] ) ? (int) $_POST['hide_map'] : 0; update_post_meta( $post_id, 'hide_map', $hide_map ); } if( ! empty( $general_settings['has_price'] ) ) { $price = acadp_sanitize_amount( $_POST['price'] ); update_post_meta( $post_id, 'price', $price ); } $old_listing_status = get_post_meta( $post_id, 'listing_status', true ); $listing_status = ( 'publish' == $post_status ) ? 'post_status' : ( '' != $old_listing_status ? $old_listing_status : 'post_status' ); update_post_meta( $post_id, 'listing_status', $listing_status ); // redirect after save $redirect_url = network_home_url(); if( 'draft' == $post_status ) { $redirect_url = acadp_get_listing_edit_page_link( $post_id ); } else { $featured_listing_settings = get_option( 'acadp_featured_listing_settings' ); $has_checkout_page = 0; if( ! empty( $featured_listing_settings['enabled'] ) && $featured_listing_settings['price'] > 0 ) { $has_checkout_page = 1; } $has_checkout_page = apply_filters( 'has_checkout_page', $has_checkout_page, $post_id ); $redirect_url = ( $is_initial_submission && $has_checkout_page ) ? acadp_get_checkout_page_link( $post_id ) : acadp_get_manage_listings_page_link(); } // send emails if( $is_initial_submission ) { update_post_meta( $post_id, 'featured', 0 ); update_post_meta( $post_id, 'views', 0 ); acadp_email_admin_listing_submitted( $post_id ); acadp_email_listing_owner_listing_submitted( $post_id ); if( 'publish' == $post_status ) { $expiry_date = acadp_listing_expiry_date( $post_id ); update_post_meta( $post_id, 'expiry_date', $expiry_date ); acadp_email_listing_owner_listing_approved( $post_id ); } } else if( $is_listing_edited ) { acadp_email_admin_listing_edited( $post_id ); } // redirect wp_redirect( $redirect_url ); exit(); } } /** * Renew Listing. * * @since 1.0.0 * @access private * * @param int $post_id Post ID. */ private function renew_listing( $post_id ) { // Disable featured update_post_meta( $post_id, 'featured', 0 ); // Hook for developers do_action( 'acadp_before_renewal', $post_id ); // ... $has_paid_submission = apply_filters( 'has_checkout_page', 0, $post_id, 'submission' ); if( $has_paid_submission ) { $redirect_url = acadp_get_checkout_page_link( $post_id ); } else { // Update post $post_id $post_array = array( 'ID' => $post_id, 'post_status' => 'publish', ); // Update the post into the database wp_update_post( $post_array ); // Update the post_meta into the database $old_listing_status = get_post_meta( $post_id, 'listing_status', true ); if( 'expired' == $old_listing_status ) { $expiry_date = acadp_listing_expiry_date( $post_id ); } else { $old_expiry_date = get_post_meta( $post_id, 'expiry_date', true ); $expiry_date = acadp_listing_expiry_date( $post_id, $old_expiry_date ); } update_post_meta( $post_id, 'expiry_date', $expiry_date ); update_post_meta( $post_id, 'listing_status', 'post_status' ); // redirect $featured_listing_settings = get_option( 'acadp_featured_listing_settings' ); $has_checkout_page = 0; if( ! empty( $featured_listing_settings['enabled'] ) && $featured_listing_settings['price'] > 0 ) { $has_checkout_page = 1; } $has_checkout_page = apply_filters( 'has_checkout_page', $has_checkout_page, $post_id ); $redirect_url = ( $has_checkout_page ) ? acadp_get_checkout_page_link( $post_id ) : acadp_get_manage_listings_page_link(); } wp_redirect( $redirect_url ); exit(); } /** * Delete Listing. * * @since 1.0.0 * @access private * * @param int $post_id Post ID. */ private function delete_listing( $post_id ) { $images = get_post_meta( $post_id, 'images', true ); if( ! empty( $images ) ) { foreach( $images as $image ) { wp_delete_attachment( $image, true ); } } wp_delete_post( $post_id, true ); // redirect $redirect_url = acadp_get_manage_listings_page_link(); wp_redirect( $redirect_url ); exit(); } /** * Process the shortcode [acadp_manage_listings]. * * @since 1.0.0 * @access public */ public function run_shortcode_manage_listings() { if( ! is_user_logged_in() ) { return acadp_login_form(); } if( ! acadp_current_user_can('edit_acadp_listings') ) { return __( 'You do not have sufficient permissions to access this page.', 'advanced-classifieds-and-directory-pro' ); } $shortcode = 'acadp_manage_listings'; $general_settings = get_option( 'acadp_general_settings' ); $listings_settings = get_option( 'acadp_listings_settings' ); $page_settings = get_option( 'acadp_page_settings' ); $featured_listing_settings = get_option( 'acadp_featured_listing_settings' ); $can_show_images = empty( $general_settings['has_images'] ) ? false : true; $can_renew = empty( $general_settings['has_listing_renewal'] ) ? false : true; $can_promote = false; if( ! empty( $featured_listing_settings['enabled'] ) && $featured_listing_settings['price'] > 0 ) { $can_promote = true; } // Enqueue style dependencies wp_enqueue_style( ACADP_PLUGIN_NAME ); // Enqueue script dependencies if( wp_script_is( ACADP_PLUGIN_NAME.'-bootstrap', 'registered' ) ) { wp_enqueue_script( ACADP_PLUGIN_NAME.'-bootstrap' ); } wp_enqueue_script( ACADP_PLUGIN_NAME.'-validator' ); wp_enqueue_script( ACADP_PLUGIN_NAME ); // Define the query $paged = ( get_query_var('paged') ) ? absint( get_query_var('paged') ) : 1; $args = array( 'post_type' => 'acadp_listings', 'post_status' => 'any', 'posts_per_page' => ! empty( $listings_settings['listings_per_page'] ) ? $listings_settings['listings_per_page'] : -1, 'paged' => $paged, 'author' => get_current_user_id(), 's' => isset( $_REQUEST['u'] ) ? sanitize_text_field( $_REQUEST['u'] ) : '', ); $acadp_query = new WP_Query( $args ); // Start the Loop global $post; // Process output ob_start(); include( acadp_get_template( "user/acadp-public-manage-listings-display.php" ) ); wp_reset_postdata(); // Use reset postdata to restore orginal query return ob_get_clean(); } /** * Process the shortcode [acadp_favourite_listings]. * * @since 1.0.0 * @access public */ public function run_shortcode_favourite_listings() { if( ! is_user_logged_in() ) { return acadp_login_form(); } $shortcode = 'acadp_favourite_listings'; $general_settings = get_option( 'acadp_general_settings' ); $listings_settings = get_option( 'acadp_listings_settings' ); $featured_listing_settings = get_option( 'acadp_featured_listing_settings' ); // Enqueue style dependencies wp_enqueue_style( ACADP_PLUGIN_NAME ); // Enqueue script dependencies if( wp_script_is( ACADP_PLUGIN_NAME.'-bootstrap', 'registered' ) ) { wp_enqueue_script( ACADP_PLUGIN_NAME.'-bootstrap' ); } wp_enqueue_script( ACADP_PLUGIN_NAME ); // ... $can_show_header = empty( $listings_settings['display_in_header'] ) ? false : true; $can_show_listings_count = $can_show_header && in_array( 'listings_count', $listings_settings['display_in_header'] ) ? true : false; $can_show_views_selector = $can_show_header && in_array( 'views_selector', $listings_settings['display_in_header'] ) ? true : false; $can_show_orderby_dropdown = $can_show_header && in_array( 'orderby_dropdown', $listings_settings['display_in_header'] ) ? true : false; $can_show_date = isset( $listings_settings['display_in_listing'] ) && in_array( 'date', $listings_settings['display_in_listing'] ) ? true : false; $can_show_user = isset( $listings_settings['display_in_listing'] ) && in_array( 'user', $listings_settings['display_in_listing'] ) ? true : false; $can_show_category = isset( $listings_settings['display_in_listing'] ) && in_array( 'category', $listings_settings['display_in_listing'] ) ? true : false; $can_show_images = empty( $general_settings['has_images'] ) ? false : true; $sort_by_featured = empty( $featured_listing_settings['enabled'] ) ? false : true; $current_order = isset( $_GET['order'] ) ? $_GET['order'] : ( $listings_settings['orderby'].'-'.$listings_settings['order'] ); $can_show_views = false; $can_show_pagination = 1; $has_price = empty( $general_settings['has_price'] ) ? false : true; $can_show_price = false; if( $has_price ) { $can_show_price = isset( $listings_settings['display_in_listing'] ) && in_array( 'price', $listings_settings['display_in_listing'] ) ? true : false; } $has_location = empty( $general_settings['has_location'] ) ? false : true; $can_show_location = false; if( $has_location ) { $can_show_location = isset( $listings_settings['display_in_listing'] ) && in_array( 'location', $listings_settings['display_in_listing'] ) ? true : false; } $span = 12; if( $can_show_images ) $span = $span - 2; if( $can_show_price ) $span = $span - 3; $span_middle = 'col-md-'.$span; // Define the query $paged = ( get_query_var('paged') ) ? absint( get_query_var('paged') ) : 1; $favourite_posts = get_user_meta( get_current_user_id(), 'acadp_favourites', true ); $args = array( 'post_type' => 'acadp_listings', 'posts_per_page' => ! empty( $listings_settings['listings_per_page'] ) ? $listings_settings['listings_per_page'] : -1, 'paged' => $paged, 'post__in' => ! empty( $favourite_posts ) ? $favourite_posts : array(0) ); if( $has_location && $general_settings['base_location'] > 0 ) { $args['tax_query'] = array( array( 'taxonomy' => 'acadp_locations', 'field' => 'term_id', 'terms' => $general_settings['base_location'], 'include_children' => true, ), ); } switch( $current_order ) { case 'title-asc' : if( $sort_by_featured ) { $args['meta_key'] = 'featured'; $args['orderby'] = array( 'meta_value_num' => 'DESC', 'title' => 'ASC', ); } else { $args['orderby'] = 'title'; $args['order'] = 'ASC'; }; break; case 'title-desc' : if( $sort_by_featured ) { $args['meta_key'] = 'featured'; $args['orderby'] = array( 'meta_value_num' => 'DESC', 'title' => 'DESC', ); } else { $args['orderby'] = 'title'; $args['order'] = 'DESC'; }; break; case 'date-asc' : if( $sort_by_featured ) { $args['meta_key'] = 'featured'; $args['orderby'] = array( 'meta_value_num' => 'DESC', 'date' => 'ASC', ); } else { $args['orderby'] = 'date'; $args['order'] = 'ASC'; }; break; case 'date-desc' : if( $sort_by_featured ) { $args['meta_key'] = 'featured'; $args['orderby'] = array( 'meta_value_num' => 'DESC', 'date' => 'DESC', ); } else { $args['orderby'] = 'date'; $args['order'] = 'DESC'; }; break; case 'price-asc' : if( $sort_by_featured ) { $args['meta_query'] = array( 'relation' => 'AND', 'featured' => array( 'key' => 'featured', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), 'price' => array( 'key' => 'price', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), ); $args['orderby'] = array( 'featured' => 'DESC', 'price' => 'ASC', ); } else { $args['meta_key'] = 'price'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'ASC'; }; break; case 'price-desc' : if( $sort_by_featured ) { $args['meta_query'] = array( 'relation' => 'AND', 'featured' => array( 'key' => 'featured', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), 'price' => array( 'key' => 'price', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), ); $args['orderby'] = array( 'featured' => 'DESC', 'price' => 'DESC', ); } else { $args['meta_key'] = 'price'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'DESC'; }; break; case 'views-asc' : if( $sort_by_featured ) { $args['meta_query'] = array( 'relation' => 'AND', 'featured' => array( 'key' => 'featured', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), 'views' => array( 'key' => 'views', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), ); $args['orderby'] = array( 'featured' => 'DESC', 'views' => 'ASC', ); } else { $args['meta_key'] = 'views'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'ASC'; }; break; case 'views-desc' : if( $sort_by_featured ) { $args['meta_query'] = array( 'relation' => 'AND', 'featured' => array( 'key' => 'featured', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), 'views' => array( 'key' => 'views', 'type' => 'NUMERIC', 'compare' => 'EXISTS', ), ); $args['orderby'] = array( 'featured' => 'DESC', 'views' => 'DESC', ); } else { $args['meta_key'] = 'views'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'DESC'; }; break; } $acadp_query = new WP_Query( $args ); // Start the Loop global $post; // Process output if( $acadp_query->have_posts() ) { $view = acadp_get_listings_current_view_name(); ob_start(); include( acadp_get_template( "listings/acadp-public-listings-$view-display.php" ) ); wp_reset_postdata(); // Use reset postdata to restore orginal query return ob_get_clean(); } else { return ''.__( 'No Results Found.', 'advanced-classifieds-and-directory-pro' ).''; } } /** * Remove favourites. * * @since 1.0.0 * @access public * * @param int $post_id Post ID. */ public function remove_favourites( $post_id ) { $favourites = (array) get_user_meta( get_current_user_id(), 'acadp_favourites', true ); if( in_array( $post_id, $favourites ) ) { if( ( $key = array_search( $post_id, $favourites ) ) !== false ) { unset( $favourites[ $key ] ); } } $favourites = array_filter( $favourites ); $favourites = array_values( $favourites ); delete_user_meta( get_current_user_id(), 'acadp_favourites' ); update_user_meta( get_current_user_id(), 'acadp_favourites', $favourites ); // redirect $redirect_url = acadp_get_favourites_page_link(); wp_redirect( $redirect_url ); exit(); } }