render_header(); $this->render_screens(); $this->render_footer(); exit; } public function render_screens() { $settings = get_option( 'adqs_admin_settings', [] ); $images_url = ADQS_DIRECTORY_ASSETS_URL . '/admin/img/onboarding/'; $setting_instance = new Setting(); $currency_list = $setting_instance->currencyList; $current_currency = isset( $settings['select_currency'] ) ? $settings['select_currency'] : 'USD__$__before'; ?>

display_name ) . '!'; ?>

onboarding
> aDirectory Setup admin_url( 'admin-ajax.php' ), 'adminUrl' => admin_url(), 'nonce' => wp_create_nonce( 'adirectory_setup_nonce' ), 'strings' => [ 'saving' => esc_html__( 'Saving...', 'adirectory' ), 'saved' => esc_html__( 'Settings saved successfully!', 'adirectory' ), 'error' => esc_html__( 'An error occurred. Please try again.', 'adirectory' ), 'importing' => esc_html__( 'Importing...', 'adirectory' ), 'imported' => esc_html__( 'Import completed successfully!', 'adirectory' ), 'skipConfirm' => esc_html__( 'Are you sure you want to skip the setup?', 'adirectory' ), ], ] ); } } public function ajax_save_settings() { check_ajax_referer( 'adirectory_setup_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Permission denied', 'adirectory' ) ] ); } $settings = []; $fields = [ 'listing_per_page', 'ajax_filters', 'select_currency', 'demo_contact', 'map_latitude', 'map_longitude' ]; foreach ( $fields as $field ) { if ( isset( $_POST[ $field ] ) ) { if ( in_array( $field, [ 'ajax_filters', 'demo_contact' ] ) ) { $settings[ $field ] = sanitize_text_field( $_POST[ $field ] ) === '1' ? '1' : '0'; } else { $settings[ $field ] = sanitize_text_field( $_POST[ $field ] ); } } } if ( isset( $_POST['directory-type'] ) ) { $settings['directory_type'] = absint( $_POST['directory-type'] ) === 0 ? 'single' : 'multi'; } $existing_settings = get_option( 'adqs_admin_settings', [] ); $merged_settings = array_merge( $existing_settings, $settings ); update_option( 'adqs_admin_settings', $merged_settings ); if ( isset( $_POST['share_data'] ) && $_POST['share_data'] === '1' ) { if ( ! class_exists( 'Appsero\Client' ) ) { require_once __DIR__ . '/appsero/src/Client.php'; } $client = new \Appsero\Client( 'f5f86071-9f3d-43e2-b259-602b34155ffd', 'aDirectory - Directory Listing WordPress Plugin', ADQS_DIRECTORY_FILE ); $insights = $client->insights(); $insights->hide_notice()->init(); $insights->optin(); } wp_send_json_success( [ 'message' => esc_html__( 'Settings saved successfully!', 'adirectory' ) ] ); } public function ajax_import_demo() { check_ajax_referer( 'adirectory_setup_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Permission denied', 'adirectory' ) ] ); } $admin_settings = get_option( 'adqs_admin_settings', [] ); $demo_import = $admin_settings['demo_contact']; if ( ! $demo_import ) { wp_send_json_success( [ 'message' => esc_html__( 'Onboarding Completed Successfully', 'adirectory' ), 'complete' => true, ] ); } $batch = isset( $_POST['batch'] ) ? absint( $_POST['batch'] ) : 0; $directory_type = $admin_settings['directory_type'] ?? 'single'; $transient_key = 'adirectory_demo_import_data_' . $directory_type; $import_data = get_transient( $transient_key ); if ( false === $import_data || $batch === 0 ) { $request = wp_remote_get( 'https://connect.adirectory.io/wp-json/connectad/v1/setup_data' ); $response = wp_remote_retrieve_body( $request ); $response = json_decode( $response, true ); if ( is_wp_error( $response ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Failed to import demo content', 'adirectory' ) ] ); } $data = isset( $response['data'] ) ? $response['data'] : $response; if ( empty( $data ) || ! is_array( $data ) ) { wp_send_json_error( [ 'message' => esc_html__( 'No demo content data available', 'adirectory' ) ] ); } if ( $directory_type === 'single' ) { $data = array_slice( $data, 0, 1 ); } $import_data = [ 'items' => $data, 'total' => count( $data ), 'directory_type' => $directory_type, ]; set_transient( $transient_key, $import_data, HOUR_IN_SECONDS ); } $total_batches = $import_data['total']; if ( $batch >= $total_batches ) { delete_transient( $transient_key ); wp_send_json_success( [ 'message' => esc_html__( 'Demo content imported successfully', 'adirectory' ), 'complete' => true, 'batch' => $batch, 'total' => $total_batches, 'percent_complete' => 100, ] ); } $item = $import_data['items'][ $batch ]; if ( empty( $item['json'] ) || empty( $item['slug'] ) ) { wp_send_json_error( [ 'message' => esc_html__( 'Invalid import data', 'adirectory' ) ] ); } $directory_id = $this->import_directory_types( $item['json'], $item['slug'] ); if ( $directory_id && ! is_wp_error( $directory_id ) && ! empty( $item['csv'] ) ) { if ( class_exists( DirectoryManager::class ) ) { $directory_manager = new DirectoryManager(); $directory_manager->register_post_types(); } flush_rewrite_rules( false ); $this->import_listings_from_csv( $item['csv'], $directory_id ); } $percent_complete = round( ( ( $batch + 1 ) / $total_batches ) * 100, 0 ); $is_complete = ( $batch + 1 ) >= $total_batches; if ( $is_complete ) { delete_transient( $transient_key ); update_option( 'adqs_onboarding_completed', true, false ); $admin_settings = get_option( 'adqs_admin_settings', [] ); $cleanup_keys = [ 'demo_contact', 'directory_type' ]; foreach ( $cleanup_keys as $cleanup_key ) { if ( isset( $admin_settings[ $cleanup_key ] ) ) { unset( $admin_settings[ $cleanup_key ] ); } } update_option( 'adqs_admin_settings', $admin_settings ); $this->delete_default_directory(); adqs_schedule_rewrite_flush(); } wp_send_json_success( [ 'message' => sprintf( esc_html__( 'Directory %1$d of %2$d imported', 'adirectory' ), $batch + 1, $total_batches ), 'batch' => $batch + 1, 'total' => $total_batches, 'percent_complete' => $percent_complete, 'complete' => $is_complete, ] ); } public function import_directory_types( $zip_url, $slug = '' ) { global $wp_filesystem; if ( empty( $wp_filesystem ) ) { require_once ABSPATH . '/wp-admin/includes/file.php'; WP_Filesystem(); } $zip_response = wp_remote_get( $zip_url, [ 'timeout' => 300, 'sslverify' => true, ] ); if ( is_wp_error( $zip_response ) || empty( wp_remote_retrieve_body( $zip_response ) ) ) { return new \WP_Error( 'failed_to_import', __( 'Failed to import directory types', 'adirectory' ) ); } $zip_body = wp_remote_retrieve_body( $zip_response ); $upload_dir = wp_upload_dir(); $temp_dir = $upload_dir['basedir'] . '/adirectory-temp-' . time(); if ( ! wp_mkdir_p( $temp_dir ) ) { return new \WP_Error( 'failed_to_import', __( 'Failed to import directory types', 'adirectory' ) ); } $zip_file = $temp_dir . '/' . $slug . '.zip'; if ( ! $wp_filesystem->put_contents( $zip_file, $zip_body, FS_CHMOD_FILE ) ) { $wp_filesystem->rmdir( $temp_dir, true ); return new \WP_Error( 'failed_to_import', __( 'Failed to import directory types', 'adirectory' ) ); } $unzip_result = unzip_file( $zip_file, $temp_dir ); if ( is_wp_error( $unzip_result ) ) { $wp_filesystem->delete( $zip_file ); $wp_filesystem->rmdir( $temp_dir, true ); return new \WP_Error( 'failed_to_import', __( 'Failed to import directory types', 'adirectory' ) ); } $json_file = $temp_dir . '/' . $slug . '.json'; if ( ! file_exists( $json_file ) ) { $files = list_files( $temp_dir ); foreach ( $files as $file ) { if ( pathinfo( $file, PATHINFO_EXTENSION ) === 'json' ) { $json_file = $file; break; } } } $directory_id = null; if ( file_exists( $json_file ) ) { $json_content = $wp_filesystem->get_contents( $json_file ); if ( $json_content ) { $json_data = json_decode( $json_content, true ); if ( is_array( $json_data ) ) { $singular_name = sanitize_text_field( $json_data['singularName'] ?? '' ); $plural_name = sanitize_text_field( $json_data['pluralName'] ?? '' ); if ( empty( $singular_name ) || empty( $plural_name ) ) { $wp_filesystem->delete( $zip_file ); $wp_filesystem->rmdir( $temp_dir, true ); return new \WP_Error( 'missing_fields', __( 'Singular name and plural name are required.', 'adirectory' ) ); } $directory_id = adqs_upsert_directory( [ 'singular_name' => $singular_name, 'plural_name' => $plural_name, 'slug' => sanitize_title( $singular_name ), ] ); if ( ! is_wp_error( $directory_id ) ) { $directory_image = sanitize_text_field( $json_data['directoryImage'] ?? '' ); $directory_icon = sanitize_text_field( $json_data['directoryIcon'] ?? '' ); $fields = isset( $json_data['fields'] ) ? map_deep( wp_unslash( $json_data['fields'] ), 'sanitize_text_field' ) : []; if ( ! empty( $directory_image ) ) { $uploaded_image = $this->upload_image_from_url( $directory_image ); if ( $uploaded_image ) { update_term_meta( $directory_id, 'adqs_directory_img', $uploaded_image ); } } if ( ! empty( $directory_icon ) ) { update_term_meta( $directory_id, 'adqs_directory_icon', $directory_icon ); } if ( ! empty( $fields ) ) { update_term_meta( $directory_id, 'adqs_metafields_types', $fields ); } $post_type_meta = get_term_meta( $directory_id, 'adqs_directory_post_type', true ); if ( empty( $post_type_meta ) ) { $post_type_key = adqs_create_post_type_key( sanitize_title( $singular_name ) ); update_term_meta( $directory_id, 'adqs_directory_post_type', $post_type_key ); } if ( class_exists( DirectoryManager::class ) ) { $directory_manager = new DirectoryManager(); $directory_manager->register_post_types(); } } } } } $wp_filesystem->delete( $zip_file ); $wp_filesystem->rmdir( $temp_dir, true ); return $directory_id; } private function upload_image_from_url( $image_url ) { if ( empty( $image_url ) ) { return ''; } $tmp = download_url( $image_url ); if ( is_wp_error( $tmp ) ) { return ''; } $file_array = [ 'name' => basename( $image_url ), 'tmp_name' => $tmp, ]; $attachment_id = media_handle_sideload( $file_array, 0 ); if ( is_wp_error( $attachment_id ) ) { @unlink( $tmp ); return ''; } return wp_get_attachment_url( $attachment_id ); } private function import_listings_from_csv( $csv_url, $directory_id ) { if ( class_exists( DirectoryManager::class ) ) { $directory_manager = new DirectoryManager(); $directory_manager->register_post_types(); } $csv_response = wp_remote_get( $csv_url, [ 'timeout' => 300, 'sslverify' => true, ] ); if ( is_wp_error( $csv_response ) || empty( wp_remote_retrieve_body( $csv_response ) ) ) { return; } $csv_body = wp_remote_retrieve_body( $csv_response ); $upload_dir = wp_upload_dir(); $temp_file = $upload_dir['basedir'] . '/adirectory-csv-' . time() . '.csv'; global $wp_filesystem; if ( empty( $wp_filesystem ) ) { require_once ABSPATH . '/wp-admin/includes/file.php'; WP_Filesystem(); } if ( ! $wp_filesystem->put_contents( $temp_file, $csv_body, FS_CHMOD_FILE ) ) { return; } $csv_data = $this->get_csv_data( $temp_file ); if ( empty( $csv_data ) ) { @unlink( $temp_file ); return; } $post_type = adqs_get_post_type_by_directory( $directory_id ); if ( empty( $post_type ) ) { $post_type = get_term_meta( $directory_id, 'adqs_directory_post_type', true ); } if ( empty( $post_type ) ) { @unlink( $temp_file ); return; } if ( ! post_type_exists( $post_type ) && class_exists( DirectoryManager::class ) ) { $directory_manager = new DirectoryManager(); $directory_manager->register_post_types(); } $mapping = $this->generate_csv_mapping( $csv_data, $directory_id ); if ( empty( $mapping ) ) { @unlink( $temp_file ); return; } $cat_taxonomy_slug = adqs_get_taxonomy_for_post_type( $post_type, 'categories' ); $loc_taxonomy_slug = adqs_get_taxonomy_for_post_type( $post_type, 'locations' ); $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 ( $csv_data 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 = []; foreach ( $row as $column => $value ) { if ( empty( $mapping[ $column ] ) ) { continue; } $mapped_field = $mapping[ $column ]; if ( empty( $value ) && ! in_array( $mapped_field, [ 'listing_title', 'listing_content' ] ) ) { continue; } if ( 'published_date' === $mapped_field ) { continue; } if ( 'listing_status' === $mapped_field ) { $list_status = trim( strtolower( $value ) ); if ( in_array( $list_status, [ 'publish', 'future', 'draft', 'pending', 'private' ] ) ) { $args['post_status'] = $list_status; } continue; } if ( 'listing_title' === $mapped_field ) { $args['post_title'] = $value; continue; } if ( 'listing_content' === $mapped_field ) { $args['post_content'] = $value; continue; } if ( 'listing_images' === $mapped_field ) { $image_urls = array_map( 'trim', explode( ',', $value ) ); continue; } if ( 'listing_cats' === $mapped_field ) { $categories = array_map( 'trim', explode( ',', $value ) ); continue; } if ( 'listing_locs' === $mapped_field ) { $locations = array_map( 'trim', explode( ',', $value ) ); continue; } if ( $value ) { $meta_key = $mapped_field; if ( substr( $meta_key, -18 ) === '_checkboxtextarea' ) { $base_key = str_replace( '_checkboxtextarea', '', $meta_key ); $textarea_id = str_replace( '_textarea_', '', $base_key ); $final_meta_key = '_textarea_list_' . $textarea_id; $args['meta_input'][ $final_meta_key ] = true; $args['meta_input'][ $base_key ] = $value; } else { $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 ) && $post_id ) { if ( ! empty( $image_urls ) ) { $attachment_ids = []; foreach ( $image_urls as $index => $image_url ) { $image_url = trim( $image_url ); if ( ! empty( $image_url ) ) { $image_id = $this->upload_listing_image_from_url( $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 ); } } if ( ! empty( $categories ) ) { $this->insert_terms_to_taxonomy( $cat_taxonomy_slug, $categories, $post_id, $directory_id ); } if ( ! empty( $locations ) ) { $this->insert_terms_to_taxonomy( $loc_taxonomy_slug, $locations, $post_id, $directory_id ); } } } } @unlink( $temp_file ); } private function generate_csv_mapping( $csv_data, $directory_id ) { if ( empty( $csv_data ) ) { return []; } $first_row = reset( $csv_data ); $headers = array_keys( $first_row ); $mapping = []; $submission_fields = []; if ( class_exists( AdCsvHelper::class ) ) { $submission_fields = AdCsvHelper::generate_submission_fields( $directory_id ); } foreach ( $headers as $header ) { $normalized_header = strtolower( str_replace( ' ', '', $header ) ); if ( in_array( $normalized_header, [ 'title', 'name', 'listingtitle' ] ) ) { $mapping[ $header ] = 'listing_title'; } elseif ( in_array( $normalized_header, [ 'description', 'content' ] ) ) { $mapping[ $header ] = 'listing_content'; } elseif ( in_array( $normalized_header, [ 'categories', 'listingcategories' ] ) ) { $mapping[ $header ] = 'listing_cats'; } elseif ( in_array( $normalized_header, [ 'locations', 'listinglocations' ] ) ) { $mapping[ $header ] = 'listing_locs'; } elseif ( in_array( $normalized_header, [ 'images', 'listingimages' ] ) ) { $mapping[ $header ] = 'listing_images'; } elseif ( in_array( $normalized_header, [ 'status', 'listingstatus' ] ) ) { $mapping[ $header ] = 'listing_status'; } elseif ( in_array( $normalized_header, [ 'published', 'createdat', 'publishedat', 'publisheddate' ] ) ) { $mapping[ $header ] = 'published_date'; } else { $matched = false; foreach ( $submission_fields as $field ) { $normalized_label = strtolower( str_replace( ' ', '', $field['label'] ) ); if ( $normalized_header === $normalized_label ) { $mapping[ $header ] = $field['meta']; $matched = true; break; } } if ( ! $matched ) { $mapping[ $header ] = '_' . strtolower( str_replace( ' ', '_', $header ) ); } } } return $mapping; } private function get_csv_data( $file, $delimiter = ',' ) { $csv = []; if ( ! $file || ! file_exists( $file ) ) { return $csv; } if ( ! is_readable( $file ) ) { chmod( $file, 0744 ); } if ( is_readable( $file ) && ( $handle = fopen( $file, 'r' ) ) !== false ) { $original_headers = fgetcsv( $handle, 0, $delimiter ); if ( $original_headers === false ) { fclose( $handle ); return $csv; } while ( ( $row = fgetcsv( $handle, 0, $delimiter ) ) !== false ) { $data = []; foreach ( $original_headers as $key => $header ) { $data[ $header ] = $row[ $key ] ?? ''; } $csv[] = $data; } fclose( $handle ); } return $csv; } private function insert_terms_to_taxonomy( $taxonomy_slug, $terms, $post_id, $directory_id ) { if ( empty( $terms ) ) { return; } $term_ids = []; foreach ( $terms as $term_name ) { $term_name = trim( $term_name ); if ( empty( $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; } $termid = $term['term_id']; } else { $termid = $term->term_id; } $term_ids[] = $termid; } if ( ! empty( $term_ids ) ) { wp_set_post_terms( $post_id, $term_ids, $taxonomy_slug ); foreach ( $term_ids as $term_id ) { adqs_update_term_directories( $term_id, [ $directory_id ], true ); } } } private function upload_listing_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 ), ]; $attachment_id = media_handle_sideload( $file, $post_id ); if ( is_wp_error( $attachment_id ) ) { @unlink( $temp_file ); return false; } return $attachment_id; } /** * Delete the default directory created on plugin activation * This is called when demo content import is completed during onboarding * * @return void */ private function delete_default_directory() { $default_directory = get_term_by( 'slug', 'listing', 'adqs_listing_types' ); if ( ! $default_directory || is_wp_error( $default_directory ) ) { return; } wp_delete_term( $default_directory->term_id, 'adqs_listing_types' ); } }