__( 'Security check failed.', 'adirectory' ) ] ); wp_die(); } $termid = absint( $_POST['termid'] ) ?? 0; $submissionfields = array_filter( AdCsvHelper::generate_submission_fields( $termid ), function ( $field ) { return ! in_array( $field['type'], [ 'badges', 'businesshour' ] ); } ); $csvheader = [ [ __( 'Title', 'adirectory' ), __( 'Description', 'adirectory' ), __( 'Excerpt', 'adirectory' ), __( 'Categories', 'adirectory' ), __( 'Locations', 'adirectory' ), __( 'Tags', 'adirectory' ), __( 'Images', 'adirectory' ), __( 'Status', 'adirectory' ), __( 'Published Date', 'adirectory' ), ], ]; foreach ( $submissionfields as $field ) { $csvheader[0][] = $field['label']; } // Get the post type for this directory type $post_type = adqs_get_post_type_by_directory( $termid ); if ( empty( $post_type ) ) { wp_die( esc_html__( 'Invalid directory type', 'adirectory' ) ); } $args = [ 'post_type' => $post_type, 'posts_per_page' => -1, ]; $query = new \WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $featured_image = get_the_post_thumbnail_url( get_the_ID(), 'medium' ); $image_urls = []; if ( $featured_image ) { $image_urls[] = $featured_image; } $images = get_post_meta( get_the_ID(), '_images', true ); if ( $images ) { foreach ( $images as $image_id ) { $image_urls[] = wp_get_attachment_url( $image_id ); } } // Get dynamic taxonomies for this post $post_type = get_post_type(); $category_taxonomy = adqs_get_taxonomy_for_post_type( $post_type, 'categories' ); $location_taxonomy = adqs_get_taxonomy_for_post_type( $post_type, 'locations' ); $tag_taxonomy = adqs_get_taxonomy_for_post_type( $post_type, 'tags' ); $category_terms = []; $location_terms = []; $tags_terms = []; if ( $category_taxonomy ) { $category_terms = wp_get_post_terms( get_the_ID(), $category_taxonomy, [ 'fields' => 'names' ] ); } if ( $location_taxonomy ) { $location_terms = wp_get_post_terms( get_the_ID(), $location_taxonomy, [ 'fields' => 'names' ] ); } if ( $tag_taxonomy ) { $tags_terms = wp_get_post_terms( get_the_ID(), $tag_taxonomy, [ 'fields' => 'names' ] ); } $listarr = [ get_the_title(), get_the_content(), get_the_excerpt(), implode( ',', $category_terms ), implode( ',', $location_terms ), implode( ',', $tags_terms ), implode( ',', $image_urls ), get_post_status(), get_the_date( 'Y-m-d H:i:s' ), ]; foreach ( $submissionfields as $field ) { $listarr[] = get_post_meta( get_the_ID(), $field['meta'], true ) ?? ''; } $csvheader[] = $listarr; } } wp_reset_postdata(); header( 'Content-Type: text/csv' ); header( 'Content-Disposition: attachment; filename="exported_data_' . uniqid() . '.csv"' ); header( 'Pragma: no-cache' ); header( 'Expires: 0' ); $output = fopen( 'php://output', 'w' ); foreach ( $csvheader as $row ) { fputcsv( $output, $row ); } fclose( $output ); exit(); } public function get_csv_data( $file, $deilmeter = ',' ) { $csv = []; if ( ! $file ) { return $csv; } if ( ! is_readable( $file ) ) { chmod( $file, 0744 ); } if ( is_readable( $file ) && ( $handle = fopen( $file, 'r' ) ) !== false ) { $headers = array_map( function ( $header ) { return strtolower( str_replace( ' ', '', $header ) ); }, fgetcsv( $handle, 0, $deilmeter ) ); while ( ( $row = fgetcsv( $handle, 0, $deilmeter ) ) !== false ) { $data = []; foreach ( $headers as $key => $header ) { $data[ $header ] = $row[ $key ]; } $csv[] = $data; } fclose( $handle ); } return $csv; } public function get_listing_default_statuses() { return [ 'publish', 'future', 'draft', 'pending', 'private' ]; } public function adqs_upload_import_list() { if ( ! check_ajax_referer( 'adqs-importer-nonce', 'security', 'false' ) ) { wp_send_json_error( [ 'messsage' => 'Nonce verification failed' ] ); } $csvmapfrom = wp_unslash( $_POST['mapping'] ); $csvpath = sanitize_text_field( $_POST['csv_file'] ); $delimiter = isset( $_POST['delimiter'] ) ? wp_unslash( $_POST['delimiter'] ) : ','; $position = isset( $_POST['position'] ) ? intval( $_POST['position'] ) : 0; $directory_id = isset( $_POST['dir_id'] ) ? intval( $_POST['dir_id'] ) : 0; $post_type = adqs_get_post_type_by_directory( $directory_id ); $cat_taxonomy_slug = 'adqs_category'; $loc_taxonomy_slug = 'adqs_location'; $tag_taxonomy_slug = 'adqs_tags'; $csvdata = $this->get_csv_data( $csvpath, $delimiter ); $total = count( $csvdata ); $imported = 0; $failed = 0; $attempted = 0; $offset = $total > 100 ? 20 : ( $total < 40 ? 2 : 5 ); $listings = array_slice( $csvdata, $position, $offset ); $default_expiry_duration = adqs_get_setting_option( 'listing_expiry_date', 365 ); $expiry_date = date( 'Y-m-d H:i:s', strtotime( '+' . $default_expiry_duration . ' days' ) ); foreach ( $listings as $row ) { $args = [ 'post_type' => $post_type, 'post_title' => '', 'post_content' => '', 'post_status' => 'publish', 'meta_input' => [ 'adqs_directory_type' => (int) $directory_id, '_expiry_date' => $expiry_date, '_from_export_import' => '1', ], ]; $image_urls = []; $categories = []; $locations = []; $tags = []; foreach ( $row as $column => $value ) { if ( empty( $csvmapfrom[ $column ] ) ) { continue; } if ( 'published_date' === $csvmapfrom[ $column ] ) { continue; } if ( 'listing_status' === $csvmapfrom[ $column ] ) { $list_status = trim( strtolower( $value ) ); if ( in_array( $list_status, $this->get_listing_default_statuses() ) ) { $args['post_status'] = $list_status; } continue; } if ( 'listing_title' === $csvmapfrom[ $column ] && ! empty( $value ) ) { $args['post_title'] = $value; continue; } if ( 'listing_content' === $csvmapfrom[ $column ] && ! empty( $value ) ) { $args['post_content'] = $value; continue; } if ( 'listing_images' === $csvmapfrom[ $column ] && ! empty( $value ) ) { $image_urls = explode( ',', $value ); continue; } if ( 'listing_cats' === $csvmapfrom[ $column ] && ! empty( $value ) ) { $categories = array_map( 'trim', explode( ',', $value ) ); continue; } if ( 'listing_locs' === $csvmapfrom[ $column ] && ! empty( $value ) ) { $locations = array_map( 'trim', explode( ',', $value ) ); continue; } if ( 'listing_tags' === $csvmapfrom[ $column ] && ! empty( $value ) ) { $tags = array_map( 'trim', explode( ',', $value ) ); continue; } if ( $value ) { $meta_key = $csvmapfrom[ $column ]; // If the field is a textarea checkbox type if ( str_ends_with( $meta_key, '_checkboxtextarea' ) ) { // Base key (remove _checkbox) $base_key = str_replace( '_checkboxtextarea', '', $meta_key ); // Extract the unique ID from textarea key $textarea_id = str_replace( '_textarea_', '', $base_key ); // Final meta key for checkbox = true $final_meta_key = '_textarea_list_' . $textarea_id; // Save both metas $args['meta_input'][ $final_meta_key ] = true; // Checkbox = true $args['meta_input'][ $base_key ] = $value; // Save textarea content } else { // Regular field $args['meta_input'][ $meta_key ] = $value; } } } if ( ! empty( $args['post_title'] ) ) { remove_filter( 'transition_post_status', 'adqs_post_status_change_mail' ); $post_id = wp_insert_post( $args ); if ( is_wp_error( $post_id ) ) { ++$failed; continue; } ++$imported; ++$attempted; if ( $post_id ) { if ( ! empty( $image_urls ) ) { $attachment_ids = []; foreach ( $image_urls as $index => $image_url ) { $image_id = $this->upload_image_from_url( trim( $image_url ), $post_id ); if ( $image_id ) { if ( $index === 0 ) { set_post_thumbnail( $post_id, $image_id ); } else { $attachment_ids[] = $image_id; } } } if ( ! empty( $attachment_ids ) ) { update_post_meta( $post_id, '_images', $attachment_ids ); } } $this->insert_terms_to_taxonomy( $cat_taxonomy_slug, $categories, $post_id, $directory_id ); $this->insert_terms_to_taxonomy( $loc_taxonomy_slug, $locations, $post_id, $directory_id ); $this->insert_terms_to_taxonomy( $tag_taxonomy_slug, $tags, $post_id, $directory_id ); } } } $isComplete = ( $position + count( $listings ) ) >= $total; $percentage = round( ( ( $position + count( $listings ) ) / $total ) * 100, 0 ); wp_send_json_success( [ 'position' => $position + count( $listings ), 'current_listings' => $listings, 'total_post' => $total, 'complete' => $isComplete, 'percent_complete' => $percentage, 'imported' => $imported, 'failed' => $failed, 'attempted' => $attempted, ] ); } function insert_terms_to_taxonomy( $taxonomy_slug, $terms, $post_id, $directory_id ) { if ( ! empty( $terms ) ) { $term_ids = []; foreach ( $terms as $term_name ) { if ( empty( trim( $term_name ) ) ) { continue; } $term = get_term_by( 'name', $term_name, $taxonomy_slug ); if ( ! $term ) { $term = wp_insert_term( $term_name, $taxonomy_slug ); if ( is_wp_error( $term ) ) { continue; // Skip this term and continue with others } $termid = $term['term_id']; } else { $termid = is_array( $term ) ? $term['term_id'] : $term->term_id; } $term_ids[] = $termid; } if ( ! empty( $term_ids ) ) { wp_set_post_terms( $post_id, $term_ids, $taxonomy_slug ); } } } private function upload_image_from_url( $image_url, $post_id ) { $temp_file = download_url( $image_url ); if ( is_wp_error( $temp_file ) ) { return false; } $file = [ 'name' => basename( $image_url ), 'type' => mime_content_type( $temp_file ), 'tmp_name' => $temp_file, 'error' => 0, 'size' => filesize( $temp_file ), ]; $overrides = [ 'test_form' => false, ]; $attachment_id = media_handle_sideload( $file, $post_id ); if ( is_wp_error( $attachment_id ) ) { @unlink( $temp_file ); return false; } return $attachment_id; } public function get_csv_header_sample( $file ) { $headers = []; $sample_value = []; $sample_array = []; if ( ( $handle = fopen( $file, 'r' ) ) !== false ) { $headers = fgetcsv( $handle, 0, ',' ); $sample_value = fgetcsv( $handle, 1000, ',' ); fclose( $handle ); } foreach ( $headers as $key => $value ) { $sample_array[ $value ] = $sample_value[ $key ] ?? ''; } return [ 'headers' => $headers, 'sample' => $sample_array, ]; } public function adqs_get_dir_mapped_to() { if ( ! check_ajax_referer( 'adqs-importer-nonce', 'security', false ) ) { wp_send_json_error( [ 'messsage' => 'Nonce verification failed' ] ); } $termid = absint( $_POST['termid'] ); $file = sanitize_text_field( $_POST['csvpath'] ); $delimiter = $_POST['delimiter'] ?? ','; $newArray = AdCsvHelper::generate_submission_fields( $termid ); $csvhedaer = $this->get_csv_header_sample( $file ); ob_start(); ?> $header ) { $col_header = strtolower( str_replace( ' ', '', $header ) ); ?>
%s