// Check if a job listing is a duplicate function duplicate_job_listing_manager_is_duplicate( $post_id ) { $post = get_post( $post_id ); $title = $post->post_title; // Extract title components using the specified format $title_parts = duplicate_job_listing_manager_extract_title_parts( $title ); if ( empty( $title_parts ) ) { return false; } $job_title = $title_parts['job_title']; $region = $title_parts['region']; $company_name = $title_parts['company_name']; // Check for existing duplicate listings $duplicate_listing = duplicate_job_listing_manager_find_duplicate_listing( $job_title, $region, $company_name, $post_id ); if ( $duplicate_listing ) { return $duplicate_listing; } return false; } // Extract title components using the specified format function duplicate_job_listing_manager_extract_title_parts( $title ) { $title_parts = array(); // Extract job title, region, and company name from the title using the specified format preg_match( '/\[(.*?)\]/', $title, $matches ); $title_parts['job_title'] = isset( $matches[1] ) ? $matches[1] : ''; preg_match( '/Jobs in (.*?) at/', $title, $matches ); $title_parts['region'] = isset( $matches[1] ) ? $matches[1] : ''; preg_match( '/at (.*?)$/', $title, $matches ); $title_parts['company_name'] = isset( $matches[1] ) ? $matches[1] : ''; return $title_parts; } // Find duplicate job listing based on title components function duplicate_job_listing_manager_find_duplicate_listing( $job_title, $region, $company_name, $post_id ) { $query_args = array( 'post_type' => 'job_listing', 'post_status' => 'publish', 'posts_per_page' => 1, 'post__not_in' => array( $post_id ), 'meta_query' => array( 'relation' => 'AND', array( 'key' => '_job_title', 'value' => $job_title, ), array( 'key' => '_job_region', 'value' => $region, ), array( 'key' => '_company_name', 'value' => $company_name, ), ), ); $query = new WP_Query( $query_args ); if ( $query->have_posts() ) { $duplicate_listing = $query->posts[0]; return $duplicate_listing->ID; } return false; } // Mark duplicate job listings function duplicate_job_listing_manager_mark_duplicates( $post_id, $original_listing_id ) { global $wpdb; $table_name = $wpdb->prefix . 'duplicate_job_listings'; // Check if the duplicate entry already exists $existing_entry = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_name WHERE original_listing_id = %d AND duplicate_listing_id = %d", $original_listing_id, $post_id ) ); if ( ! $existing_entry ) { // Insert the duplicate entry into the database $wpdb->insert( $table_name, array( 'original_listing_id' => $original_listing_id, 'duplicate_listing_id' => $post_id, ), array( '%d', '%d', ) ); } }