alert("' . esc_js( $message ) . '");'; return; } $adqs_admin_settings = get_option( 'adqs_admin_settings', [] ); $guest_listing_approv = $adqs_admin_settings['enable_guest_listing_post_approv'] ?? 0; $logged_in_user_approv = $adqs_admin_settings['enable_logged_in_user_listing_approval'] ?? 0; $listing_expiry_days = $adqs_admin_settings['listing_expiry_date'] ?? 365; // Determine if approval is required based on user status $requires_approval = is_user_logged_in() ? $logged_in_user_approv : $guest_listing_approv; $preseet_fields = [ 'address', 'businesshour', 'fax', 'map', 'phone', 'pricing', 'tagline', 'video', 'view_count', 'website', 'zip', 'social_media_link', 'email' ]; $custom_fields = [ 'checkbox', 'date', 'field_images', 'number', 'radio', 'select', 'text', 'textarea', 'time', 'url' ]; $posts_args = [ 'post_title' => sanitize_text_field( $_POST['post_title'] ?? '' ), 'post_content' => isset( $_POST['post_content'] ) ? wp_kses_post( wp_unslash( $_POST['post_content'] ) ) : '', 'post_type' => $post_type, ]; $current_user_id = get_current_user_id(); if ( isset( $_GET['postid'] ) ) { $post_id = absint( $_GET['postid'] ); $post_author = get_post_field( 'post_author', $post_id ); if ( $post_author != $current_user_id ) { $default_redirect = adqs_get_permalink_by_key( 'adqs_user_dashboard' ); $custom_redirect = get_permalink( absint( get_option( 'adqs_view_page_id' ) ) ); wp_redirect( esc_url( ! empty( $custom_redirect ) ? $custom_redirect : $default_redirect ) ); exit; } } // Only set post_status for NEW listings, not when updating existing ones if ( ! isset( $_GET['postid'] ) ) { $posts_args['post_status'] = current_user_can( 'manage_options' ) ? 'publish' : ( $requires_approval ? 'pending' : 'publish' ); } if ( isset( $_POST['tax_input'] ) ) { $posts_args['tax_input'] = $_POST['tax_input'] ?? []; } if ( ! empty( $tag_taxonomy ) && taxonomy_exists( $tag_taxonomy ) ) { $tags = isset( $_POST[ $tag_taxonomy ] ) ? explode( ',', sanitize_text_field( $_POST[ $tag_taxonomy ] ) ) : []; $tag_ids = []; foreach ( $tags as $tag_name ) { $tag_name = trim( $tag_name ); if ( empty( $tag_name ) ) { continue; } $term = term_exists( $tag_name, $tag_taxonomy ); if ( ! $term ) { $term = wp_insert_term( $tag_name, $tag_taxonomy ); } if ( ! is_wp_error( $term ) ) { $tag_ids[] = intval( $term['term_id'] ); } } // Add tag IDs to tax_input if any exist if ( ! empty( $tag_ids ) ) { $posts_args['tax_input'][ $tag_taxonomy ] = $tag_ids; } } if ( isset( $_GET['postid'] ) ) { // Update existing listing $posts_args['ID'] = absint( $_GET['postid'] ); $post_id = wp_update_post( $posts_args, true ); if ( is_wp_error( $post_id ) ) { $message = __( 'Failed to update listing: ', 'adirectory' ) . $post_id->get_error_message(); echo ''; return; } // Ensure taxonomy data is properly saved when updating if ( isset( $_POST['tax_input'] ) && ! empty( $_POST['tax_input'] ) ) { foreach ( $_POST['tax_input'] as $taxonomy => $term_ids ) { if ( ! empty( $term_ids ) && is_array( $term_ids ) ) { $sanitized_term_ids = array_map( 'absint', $term_ids ); wp_set_post_terms( $post_id, $sanitized_term_ids, $taxonomy ); } } } // Ensure tags are properly saved when updating if ( ! empty( $tag_taxonomy ) && taxonomy_exists( $tag_taxonomy ) && isset( $_POST[ $tag_taxonomy ] ) ) { $tags = explode( ',', sanitize_text_field( $_POST[ $tag_taxonomy ] ) ); if ( ! empty( $tags ) ) { $tag_ids = []; foreach ( $tags as $tag_name ) { $tag_name = trim( $tag_name ); if ( empty( $tag_name ) ) { continue; } $term = term_exists( $tag_name, $tag_taxonomy ); if ( ! $term ) { $term = wp_insert_term( $tag_name, $tag_taxonomy ); } if ( ! is_wp_error( $term ) ) { $tag_ids[] = intval( $term['term_id'] ); } } if ( ! empty( $tag_ids ) ) { wp_set_post_terms( $post_id, $tag_ids, $tag_taxonomy ); } } } do_action( 'adqs_listing_updated', $post_id ); } else { if ( is_user_logged_in() ) { $user_id = get_current_user_id(); } else { $guest_email = sanitize_email( $_POST['guest_email'] ?? '' ); // Validate guest email is provided if ( empty( $guest_email ) ) { $message = __( 'Email address is required for guest listings. Please enter your email address.', 'adirectory' ); echo ''; return; } // Validate email format if ( ! is_email( $guest_email ) ) { $message = __( 'Please enter a valid email address.', 'adirectory' ); echo ''; return; } if ( email_exists( $guest_email ) ) { $message = __( 'This email is already registered. Please use another email address.', 'adirectory' ); echo ''; return; } else { $user_data = [ 'user_login' => $guest_email, 'user_email' => $guest_email, 'user_pass' => wp_generate_password(), 'role' => 'subscriber', ]; $user_id = wp_insert_user( $user_data ); if ( ! is_wp_error( $user_id ) ) { $user = get_user_by( 'id', $user_id ); $reset_key = get_password_reset_key( $user ); $reset_link = site_url( "wp-login.php?action=rp&key=$reset_key&login=" . rawurlencode( $user->user_login ) ); // Get newsletter checkbox value $newsletter_check = isset( $_POST['newsletter_check'] ) ? absint( $_POST['newsletter_check'] ) : 0; // Pass all required parameters for integrations (e.g., Mailchimp) do_action( 'adqs_after_new_registration', $user_id, '', '', $guest_email, $newsletter_check, $reset_link ); } } } $posts_args['post_author'] = $user_id; $post_id = wp_insert_post( $posts_args ); // Save guest email if user is not logged in if ( ! is_user_logged_in() && ! empty( $guest_email ) ) { update_post_meta( $post_id, '_guest_email', $guest_email ); } // Track whether this is a guest or logged-in user listing $is_guest_listing = ! is_user_logged_in() ? 'yes' : 'no'; update_post_meta( $post_id, '_is_guest_listing', $is_guest_listing ); // Ensure taxonomy data is properly saved for guest users if ( ! is_user_logged_in() && isset( $_POST['tax_input'] ) && ! empty( $_POST['tax_input'] ) ) { foreach ( $_POST['tax_input'] as $taxonomy => $term_ids ) { if ( ! empty( $term_ids ) && is_array( $term_ids ) ) { $sanitized_term_ids = array_map( 'absint', $term_ids ); wp_set_post_terms( $post_id, $sanitized_term_ids, $taxonomy ); } } } // Ensure tags are properly saved for guest users if ( ! is_user_logged_in() && ! empty( $tag_taxonomy ) && taxonomy_exists( $tag_taxonomy ) ) { $tags = isset( $_POST[ $tag_taxonomy ] ) ? explode( ',', sanitize_text_field( $_POST[ $tag_taxonomy ] ) ) : []; if ( ! empty( $tags ) ) { $tag_ids = []; foreach ( $tags as $tag_name ) { $tag_name = trim( $tag_name ); if ( empty( $tag_name ) ) { continue; } $term = term_exists( $tag_name, $tag_taxonomy ); if ( ! $term ) { $term = wp_insert_term( $tag_name, $tag_taxonomy ); } if ( ! is_wp_error( $term ) ) { $tag_ids[] = intval( $term['term_id'] ); } } if ( ! empty( $tag_ids ) ) { wp_set_post_terms( $post_id, $tag_ids, $tag_taxonomy ); } } } // Calculate and save expiry date if ( $post_id && ! is_wp_error( $post_id ) && ! empty( $listing_expiry_days ) && ! isset( $_GET['adqs_order'] ) ) { $current_time = current_time( 'mysql' ); $expiry_date = wp_date( 'Y-m-d H:i:s', strtotime( "+{$listing_expiry_days} days", strtotime( $current_time ) ) ); update_post_meta( $post_id, '_expiry_date', $expiry_date ); } if ( isset( $_GET['adqs_order'] ) ) { // Get package info for the SPECIFIC order ID that was selected by the user $selected_order_id = absint( $_GET['adqs_order'] ); $package_info = adqs_get_listing_pricing_package_info( $post_id, $selected_order_id ); $expiry_days_from_pricing = ''; // Get expiry days based on active pricing addon if ( $pricing_active === 'self' && function_exists( 'adp_get_query_pricing_meta' ) ) { // Standard pricing package $expiry_days_from_pricing = adp_get_query_pricing_meta( ( $package_info['pricing_id'] ?? 0 ), 'adqs_listing_expire_days', true ); } elseif ( $pricing_active === 'wc' && function_exists( 'adp_wc_get_meta' ) ) { // WooCommerce pricing package $expiry_days_from_pricing = adp_wc_get_meta( ( $package_info['pricing_id'] ?? 0 ), 'adqs_listing_expire_days' ); } if ( ! empty( $expiry_days_from_pricing ) ) { $current_time = current_time( 'mysql' ); $expiry_date = wp_date( 'Y-m-d H:i:s', strtotime( "+{$expiry_days_from_pricing} days", strtotime( $current_time ) ) ); update_post_meta( $post_id, '_expiry_date', $expiry_date ); } update_post_meta( $post_id, 'adqs_pricing_order_no', absint( $_GET['adqs_order'] ) ); } } if ( isset( $_POST['feat_thumbnail_id'] ) ) { if ( $_POST['feat_thumbnail_id'] !== '0' && ! empty( $_POST['feat_thumbnail_id'] ) ) { update_post_meta( $post_id, '_thumbnail_id', sanitize_text_field( $_POST['feat_thumbnail_id'] ) ); } } if ( isset( $_POST['slider_thumbnail_id'] ) ) { if ( $_POST['slider_thumbnail_id'] !== '0' && ! empty( $_POST['slider_thumbnail_id'] ) ) { $sliderids = explode( ',', sanitize_text_field( $_POST['slider_thumbnail_id'] ) ); update_post_meta( $post_id, '_images', $sliderids ); } } if ( ! function_exists( 'adqs_section_title_display' ) ) { function adqs_section_title_display( $post_id = 0, $directory_type_id = 0 ) { $meta_fields = adqs_get_listing_fields( $directory_type_id ); if ( ! empty( $meta_fields ) && is_array( $meta_fields ) ) { foreach ( $meta_fields as $section ) { $sectionId = isset( $section['id'] ) ? $section['id'] : ''; update_post_meta( $post_id, "adqs_mtsh_hide_{$sectionId}", sanitize_text_field( $_POST[ "adqs_mtsh_hide_{$sectionId}" ] ?? '' ) ); } } } adqs_section_title_display( $post_id, $directory_type ); } if ( isset( $_POST['adqs_plan_pkg'] ) ) { $user_id = get_current_user_id(); $listing_type = ( $_POST['adqs_plan_pkg'] === 'adqs_reg_list' ) ? 'regular' : 'featured'; // Enhanced validation using the new permission system if ( function_exists( 'adp_validate_user_permissions' ) ) { $permission_result = adp_validate_user_permissions( $user_id, $directory_type, $listing_type ); if ( ! $permission_result['can_add'] ) { $message = sprintf( __( 'You cannot add this listing. %s', 'adirectory' ), $permission_result['reason'] ); echo ''; return; } } // Get pricing package permissions for this listing $pricing_permissions = adqs_get_pricing_package_permissions( $user_id, $directory_type, $order_id ); if ( $pricing_active === 'self' ) { if ( $_POST['adqs_plan_pkg'] === 'adqs_reg_list' ) { $regular_listing = get_user_meta( $user_id, "adp_order_reg_listing_{$order_id}", true ); // Only decrement if not unlimited if ( $regular_listing !== 'unlimited' && $regular_listing !== '' ) { $regular_listing = max( 0, intval( $regular_listing ) - 1 ); update_user_meta( $user_id, "adp_order_reg_listing_{$order_id}", $regular_listing ); } } else { $featured_listing = get_user_meta( $user_id, "adp_order_fea_listing_{$order_id}", true ); // Only decrement if not unlimited if ( $featured_listing !== 'unlimited' && $featured_listing !== '' ) { $featured_listing = max( 0, intval( $featured_listing ) - 1 ); update_user_meta( $user_id, "adp_order_fea_listing_{$order_id}", $featured_listing ); } update_post_meta( $post_id, '_is_featured', 'yes' ); } // Save pricing package permissions if ( ! empty( $pricing_permissions ) ) { update_post_meta( $post_id, '_pricing_permissions', $pricing_permissions ); } } elseif ( $pricing_active === 'wc' ) { if ( $_POST['adqs_plan_pkg'] === 'adqs_reg_list' ) { $regular_listing = get_user_meta( $user_id, "adp_wc_order_reg_listing_{$order_id}", true ); // Only decrement if not unlimited if ( $regular_listing !== 'unlimited' && $regular_listing !== '' ) { $regular_listing = max( 0, intval( $regular_listing ) - 1 ); update_user_meta( $user_id, "adp_wc_order_reg_listing_{$order_id}", $regular_listing ); } } else { $featured_listing = get_user_meta( $user_id, "adp_wc_order_fea_listing_{$order_id}", true ); // Only decrement if not unlimited if ( $featured_listing !== 'unlimited' && $featured_listing !== '' ) { $featured_listing = max( 0, intval( $featured_listing ) - 1 ); update_user_meta( $user_id, "adp_wc_order_fea_listing_{$order_id}", $featured_listing ); } update_post_meta( $post_id, '_is_featured', 'yes' ); } // Save pricing package permissions if ( ! empty( $pricing_permissions ) ) { update_post_meta( $post_id, '_pricing_permissions', $pricing_permissions ); } } } $listing_fields = adqs_get_listing_fields( $directory_type ) ?? []; $custom_fields_data = []; if ( ! empty( $listing_fields ) ) { foreach ( $listing_fields as $section ) { $sectionFields = $section['fields'] ?? []; foreach ( $sectionFields as $field ) { $input_type = $field['input_type'] ?? ''; if ( in_array( $input_type, $custom_fields ) ) { $fieldid = $field['fieldid'] ?? ''; $custom_fields_data[ $fieldid ] = $input_type; } } } } if ( ! empty( $custom_fields_data ) && is_array( $custom_fields_data ) ) { foreach ( $custom_fields_data as $id => $input_type ) { $meta_key = sanitize_key( "_{$input_type}_{$id}" ); switch ( $input_type ) { case 'text': case 'number': case 'date': case 'time': case 'select': case 'radio': update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ $meta_key ] ?? '' ) ); break; case 'url': update_post_meta( $post_id, $meta_key, sanitize_url( $_POST[ $meta_key ] ?? 0 ) ); break; case 'textarea': update_post_meta( $post_id, $meta_key, sanitize_textarea_field( $_POST[ $meta_key ] ?? 0 ) ); $meta_key = sanitize_key( "_{$input_type}_list_{$id}" ); update_post_meta( $post_id, $meta_key, $_POST[ $meta_key ] ?? '' ); break; case 'checkbox': $getData = $_POST[ $meta_key ] ?? []; $getData = map_deep( $getData, 'sanitize_text_field' ); update_post_meta( $post_id, $meta_key, $getData ); break; case 'field_images': // $getData = $Helper->post_data( $_POST, $meta_key, array() ); // $getData = map_deep( $getData, 'absint' ); // update_post_meta( $post_id, $meta_key, $getData ); // break; } } } if ( ! empty( $preseet_fields ) && is_array( $preseet_fields ) ) { foreach ( $preseet_fields as $name_slug ) { $meta_key = sanitize_key( "_{$name_slug}" ); switch ( $name_slug ) { case 'address': case 'fax': case 'phone': case 'tagline': case 'view_count': case 'email': case 'zip': update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ $meta_key ] ?? 0 ) ); break; case 'video': case 'website': update_post_meta( $post_id, $meta_key, sanitize_url( $_POST[ $meta_key ] ?? 0 ) ); break; case 'map': $_map_lat = $_POST['_map_lat'] ?? ''; $_map_lon = $_POST['_map_lon'] ?? ''; $_hide_map = $_POST['_hide_map'] ?? '0'; update_post_meta( $post_id, '_map_lat', sanitize_text_field( $_map_lat ) ); update_post_meta( $post_id, '_map_lon', sanitize_text_field( $_map_lon ) ); update_post_meta( $post_id, '_hide_map', sanitize_text_field( $_hide_map ) ); break; case 'pricing': $_price_type = $_POST['_price_type'] ?? ''; $_price = $_POST['_price'] ?? ''; $_price_sub = $_POST['_price_sub'] ?? ''; $_price_range = $_POST['_price_range'] ?? ''; update_post_meta( $post_id, '_price_type', sanitize_text_field( $_price_type ) ); update_post_meta( $post_id, '_price', sanitize_text_field( $_price ) ); update_post_meta( $post_id, '_price_sub', sanitize_text_field( $_price_sub ) ); update_post_meta( $post_id, '_price_range', sanitize_text_field( $_price_range ) ); break; case 'businesshour': $business_hour_data = $_POST['bhc'] ?? []; update_post_meta( $post_id, 'adqs_business_data', $business_hour_data ); break; case 'social_media_link': $social_media_link_data = $_POST['social_media_link'] ?? []; update_post_meta( $post_id, 'adqs_social_media_link', $social_media_link_data ); break; } } } // Fire hook after all metabox data is saved (for both new and updated listings) do_action( 'adqs_frontend_after_save_metabox_data', $post_id ); // Get redirect page from admin settings $adqs_admin_settings = get_option( 'adqs_admin_settings', [] ); $custom_redirect_page_id = $adqs_admin_settings['custom_page_redirect'] ?? ''; // Set redirect page - use custom redirect if set, otherwise use user dashboard if ( ! empty( $custom_redirect_page_id ) && is_numeric( $custom_redirect_page_id ) ) { $loginPage = get_permalink( intval( $custom_redirect_page_id ) ); } else { $loginPage = adqs_get_permalink_by_key( 'adqs_user_dashboard' ); } // Fallback to home page if no valid redirect page found if ( empty( $loginPage ) ) { $loginPage = home_url(); } ?>