if (!defined('ABSPATH')) { exit; // Exit if accessed directly } //Headphone Type API add_action("rest_api_init", function () { register_rest_route("headphonesprice/v1", "/type/", [ "methods" => "GET", "callback" => "get_headphone_type", "permission_callback" => "__return_true", ]); }); function get_headphone_type($data) { global $wpdb; $results = $wpdb->get_results( "SELECT * FROM `zzhp_types` ORDER BY `id` ASC" ); return $results; } //Brands API add_action("rest_api_init", function () { register_rest_route("headphonesprice/v1", "/brands/", [ "methods" => "GET", "callback" => "get_headphone_brands", "permission_callback" => "__return_true", ]); }); function get_headphone_brands($data) { global $wpdb; $results = $wpdb->get_results( "SELECT * FROM `zzhp_brands` ORDER BY `id` ASC" ); return $results; } //Color API add_action("rest_api_init", function () { register_rest_route("headphonesprice/v1", "/colors/", [ "methods" => "GET", "callback" => "get_headphone_colors", "permission_callback" => "__return_true", ]); }); function get_headphone_colors($data) { global $wpdb; $results = $wpdb->get_results( "SELECT * FROM `zzhp_colors` ORDER BY `id` ASC" ); return $results; } //Bluetooth API add_action("rest_api_init", function () { register_rest_route("headphonesprice/v1", "/bluetooth/version/", [ "methods" => "GET", "callback" => "get_headphone_bluetooth_version", "permission_callback" => "__return_true", ]); }); function get_headphone_bluetooth_version($data) { global $wpdb; $results = $wpdb->get_results( "SELECT * FROM `zzhp_bluetooth_version` ORDER BY `id` ASC" ); return $results; } //Headphones initial search API function search_headphones() { global $wpdb; $query = " SELECT d.hp_id, d.hp_name, d.hp_model, d.hp_type_name, d.hp_wireless, d.hp_wired, b.hp_brand, m.hp_image_url FROM zzhp_general_details d INNER JOIN zzhp_brands b ON d.hp_brand_id = b.id INNER JOIN zzhp_images m ON d.hp_id = m.hp_id"; $results = $wpdb->get_results($query); return $results; } add_action("rest_api_init", function () { register_rest_route("headphonesprice/v1", "/search/", [ "methods" => "GET", "callback" => "search_headphones", "permission_callback" => "__return_true", ]); }); //Headphones search for detailed attributes function get_headphone_details($data) { global $wpdb; // Ensure headphone_id is provided and is an integer $headphone_id = isset($data["headphone_id"]) ? intval($data["headphone_id"]) : 0; if ($headphone_id === 0) { return new WP_Error("no_headphone_id", "No Headphone ID provided", [ "status" => 404, ]); } // Prepare the SQL query $query = " SELECT gd.*, IF(gd.hp_weight != '', CONCAT(gd.hp_weight, ' g'), NULL) AS hp_weight_g, IF(gd.hp_driver_unit != '', CONCAT(gd.hp_driver_unit, ' mm'), NULL) AS hp_driver_unit_mm, brd.*, img.*, cab.*, IF(cab.hp_cable_length != '', CONCAT(cab.hp_cable_length, ' m'), NULL) AS hp_cable_length_m, conn.*, mic.*, feat.*, pwr.*, prot.*, snd.*, IF(snd.hp_frequency_low != '' AND snd.hp_frequency_high != '', CONCAT(snd.hp_frequency_low, ' Hz - ', snd.hp_frequency_high, ' Hz'), NULL) AS hp_frequency_range, IF(snd.hp_impedance != '', CONCAT(snd.hp_impedance, ' Ohms'), NULL) AS hp_impedance_ohms, IF(snd.hp_sensitivity != '', CONCAT(snd.hp_sensitivity, ' dB/mW'), NULL) AS hp_sensitivity_db, IF(snd.hp_audio_latency != '', CONCAT(snd.hp_audio_latency, ' ms'), NULL) AS hp_audio_latency_ms, ux.* FROM zzhp_general_details gd LEFT JOIN zzhp_brands brd ON gd.hp_brand_id = brd.id LEFT JOIN zzhp_images img ON gd.hp_id = img.hp_id LEFT JOIN zzhp_cables cab ON gd.hp_id = cab.hp_id LEFT JOIN zzhp_connectivity conn ON gd.hp_id = conn.hp_id LEFT JOIN zzhp_microphone mic ON gd.hp_id = mic.hp_id LEFT JOIN zzhp_other_features feat ON gd.hp_id = feat.hp_id LEFT JOIN zzhp_power pwr ON gd.hp_id = pwr.hp_id LEFT JOIN zzhp_protection prot ON gd.hp_id = prot.hp_id LEFT JOIN zzhp_sound snd ON gd.hp_id = snd.hp_id LEFT JOIN zzhp_ux_control ux ON gd.hp_id = ux.hp_id WHERE gd.hp_id = %d; "; // Prepare and execute the query $results = $wpdb->get_results($wpdb->prepare($query, $headphone_id)); // Check for results if (empty($results)) { return new WP_Error("no_results", "No results found", [ "status" => 404, ]); } return $results; } // Register the REST API endpoint add_action("rest_api_init", function () { register_rest_route("headphonesprice/v1", "/comparison/details/", [ "methods" => "GET", "callback" => "get_headphone_details", "permission_callback" => "__return_true", ]); }); //Headphoner comparison/feature search function get_all_headphones() { global $wpdb; // Prepare the SQL query to retrieve all headphones $query = " SELECT gd.*, IF(gd.hp_weight != '', CONCAT(gd.hp_weight, ' g'), NULL) AS hp_weight_g, IF(gd.hp_driver_unit != '', CONCAT(gd.hp_driver_unit, ' mm'), NULL) AS hp_driver_unit_mm, brd.*, img.*, cab.*, IF(cab.hp_cable_length != '', CONCAT(cab.hp_cable_length, ' m'), NULL) AS hp_cable_length_m, conn.*, mic.*, feat.*, pwr.*, prot.*, snd.*, IF(snd.hp_frequency_low != '' AND snd.hp_frequency_high != '', CONCAT(snd.hp_frequency_low, ' Hz - ', snd.hp_frequency_high, ' Hz'), NULL) AS hp_frequency_range, IF(snd.hp_impedance != '', CONCAT(snd.hp_impedance, ' Ohms'), NULL) AS hp_impedance_ohms, IF(snd.hp_sensitivity != '', CONCAT(snd.hp_sensitivity, ' dB/mW'), NULL) AS hp_sensitivity_db, IF(snd.hp_audio_latency != '', CONCAT(snd.hp_audio_latency, ' ms'), NULL) AS hp_audio_latency_ms, ux.* FROM zzhp_general_details gd LEFT JOIN zzhp_brands brd ON gd.hp_brand_id = brd.id LEFT JOIN zzhp_images img ON gd.hp_id = img.hp_id LEFT JOIN zzhp_cables cab ON gd.hp_id = cab.hp_id LEFT JOIN zzhp_connectivity conn ON gd.hp_id = conn.hp_id LEFT JOIN zzhp_microphone mic ON gd.hp_id = mic.hp_id LEFT JOIN zzhp_other_features feat ON gd.hp_id = feat.hp_id LEFT JOIN zzhp_power pwr ON gd.hp_id = pwr.hp_id LEFT JOIN zzhp_protection prot ON gd.hp_id = prot.hp_id LEFT JOIN zzhp_sound snd ON gd.hp_id = snd.hp_id LEFT JOIN zzhp_ux_control ux ON gd.hp_id = ux.hp_id "; // Execute the query $results = $wpdb->get_results($query); // Check for results if (empty($results)) { return new WP_Error("no_results", "No results found", [ "status" => 404, ]); } return $results; } // Register the REST API endpoint for alll add_action("rest_api_init", function () { register_rest_route("headphonesprice/v1", "/headphones/all/", [ "methods" => "GET", "callback" => "get_all_headphones", "permission_callback" => "__return_true", ]); }); ////REST API for Finder function find_headphones(WP_REST_Request $request) { global $wpdb; // Get filter values from the request object $typeIDs = sanitize_text_field($request->get_param("hp_type")); $brandFilter = array_map( "sanitize_text_field", (array) $request->get_param("hp_brand") ); $weightRange = array_map( "sanitize_text_field", (array) $request->get_param("hp_weight") ); $bluetoothVersionId = intval($request->get_param("hp_bluetooth_version")); // Convert to integer $ipRatingFilter = sanitize_text_field($request->get_param("hp_ip_rating")); $frequencyRange = array_map( "sanitize_text_field", (array) $request->get_param("hp_frequency_range") ); $sensitivityRange = array_map( "sanitize_text_field", (array) $request->get_param("hp_sensitivity") ); $microphonesNumberRange = array_map( "sanitize_text_field", (array) $request->get_param("hp_number_of_microphones") ); $batteryLifeRange = array_map( "sanitize_text_field", (array) $request->get_param("hp_battery_life") ); $chargingCaseBatteryLifeRange = array_map( "sanitize_text_field", (array) $request->get_param("hp_charging_case_battery_life") ); $chargeTimeRange = array_map( "sanitize_text_field", (array) $request->get_param("hp_charge_time") ); $cableLengthRange = array_map( "sanitize_text_field", (array) $request->get_param("hp_cable_length") ); $ambientSoundMode = $request->get_param("ambient_sound_mode") === "1" ? 1 : 0; $auxInput = $request->get_param("hp_aux_input") === "1" ? 1 : 0; $jackConnector = $request->get_param("hp_jack_connector") === "1" ? 1 : 0; $inOnEardetection = $request->get_param("hp_in_on_ear_detection") === "1" ? 1 : 0; $muteFunction = $request->get_param("hp_mute_function") === "1" ? 1 : 0; $multipointCount = $request->get_param("hp_multipoint_count") === "1" ? 1 : 0; $voiceCommands = $request->get_param("hp_voice_commands") === "1" ? 1 : 0; // Retrieve the corresponding type names from zzhp_types $typeNames = []; if (!empty($typeIDs)) { $typeIDList = implode(",", array_map("intval", explode(",", $typeIDs))); $typeNameResults = $wpdb->get_results( "SELECT hp_type_name FROM zzhp_types WHERE id IN ($typeIDList)", ARRAY_A ); $typeNames = wp_list_pluck($typeNameResults, "hp_type_name"); } // Start building the dynamic query $query = " SELECT gd.*, img.* FROM zzhp_general_details gd LEFT JOIN zzhp_brands brd ON gd.hp_brand_id = brd.id LEFT JOIN zzhp_images img ON gd.hp_id = img.hp_id LEFT JOIN zzhp_cables cab ON gd.hp_id = cab.hp_id LEFT JOIN zzhp_connectivity conn ON gd.hp_id = conn.hp_id LEFT JOIN zzhp_microphone mic ON gd.hp_id = mic.hp_id LEFT JOIN zzhp_other_features feat ON gd.hp_id = feat.hp_id LEFT JOIN zzhp_power pwr ON gd.hp_id = pwr.hp_id LEFT JOIN zzhp_protection prot ON gd.hp_id = prot.hp_id LEFT JOIN zzhp_sound snd ON gd.hp_id = snd.hp_id LEFT JOIN zzhp_ux_control ux ON gd.hp_id = ux.hp_id WHERE 1=1 "; // Add conditions for each filter // Type name $params = []; if (!empty($typeNames)) { $placeholders = implode(", ", array_fill(0, count($typeNames), "%s")); $query .= " AND gd.hp_type_name IN ($placeholders)"; $params = array_merge($params, $typeNames); } // Handle multiple brands if (!empty($brandFilter) && is_array($brandFilter)) { // Check if the filter contains 'none', which signifies including all brands if (!in_array("none", $brandFilter)) { // Only add the brand filter to the query if 'none' is not selected $placeholders = implode( ", ", array_fill(0, count($brandFilter), "%s") ); $query .= " AND brd.hp_brand IN ($placeholders)"; $params = array_merge($params, $brandFilter); } } // Handle Bluetooth version if provided and is a valid integer if ($bluetoothVersionId > 0) { // Check if ID is a positive integer $bluetoothVersionQuery = $wpdb->prepare("SELECT hp_bluetooth_version FROM zzhp_connectivity WHERE id = %d", $bluetoothVersionId); $bluetoothVersion = $wpdb->get_var($bluetoothVersionQuery); if (!empty($bluetoothVersion)) { $query .= " AND conn.hp_bluetooth_version = %s"; $params[] = $bluetoothVersion; } } // Handle boolean fields and map true/false to 1/0 $booleanFilters = [ "hp_foldable", "hp_wireless", "hp_wired", "hp_wingtips", "hp_stereo_speaker", "hp_active_noise_cancellation", "hp_passive_noise_reduction", "hp_rechargeable_battery", "hp_wireless_charging", "hp_fast_charging", "hp_has_aptx", "hp_has_aptx_hd", "hp_in_on_ear_detection", "hp_mute_function", "hp_supports_nfc", "hp_supports_wi_fi", "hp_detachable_cable", "hp_tangle_free_cable", "hp_usb_c", "hp_jack_connector", "hp_multipoint_count", "hp_voice_commands", "hp_ambient_sound_mode", ]; foreach ($booleanFilters as $filter) { if (isset($_GET[$filter])) { $value = $_GET[$filter] ? 1 : 0; // Convert boolean to tinyint $query .= " AND gd.$filter = %d"; $params[] = $value; } } // Handle range filters for battery life, etc. function handleRangeFilter($query, &$params, $rangeFilter, $dbField) { if ( !empty($rangeFilter) && is_array($rangeFilter) && count($rangeFilter) == 2 ) { list($min, $max) = $rangeFilter; $min = floor(floatval($min)); $max = floor(floatval($max)); $query .= " AND $dbField BETWEEN %d AND %d"; $params[] = $min; $params[] = $max; } return $query; } $query = handleRangeFilter( $query, $params, $batteryLifeRange, "pwr.hp_battery_life" ); $query = handleRangeFilter( $query, $params, $cableLengthRange, "cab.hp_cable_length" ); $query = handleRangeFilter( $query, $params, $chargeTimeRange, "pwr.hp_charge_time" ); $query = handleRangeFilter( $query, $params, $chargingCaseBatteryLifeRange, "pwr.hp_charging_case_battery_life" ); $query = handleRangeFilter( $query, $params, $sensitivityRange, "snd.hp_sensitivity" ); $query = handleRangeFilter( $query, $params, $microphonesNumberRange, "mic.hp_number_of_microphones" ); $query = handleRangeFilter($query, $params, $weightRange, "gd.hp_weight"); //handle frequency filter function handleFrequencyRangeFilter($query, &$params, $frequencyRange) { if ( !empty($frequencyRange) && is_array($frequencyRange) && count($frequencyRange) == 2 ) { list($minFreq, $maxFreq) = $frequencyRange; $minFreq = floor(floatval($minFreq)); $maxFreq = floor(floatval($maxFreq)); // Ensure the lower bound of the headphone frequency range is within the specified min $query .= " AND snd.hp_frequency_low >= %d"; $params[] = $minFreq; // Ensure the upper bound of the headphone frequency range is within the specified max $query .= " AND snd.hp_frequency_high <= %d"; $params[] = $maxFreq; } return $query; } // Use this function specifically for the frequency range $query = handleFrequencyRangeFilter($query, $params, $frequencyRange); if ($ambientSoundMode) { $query .= " AND ux.hp_ambient_sound_mode = %d"; $params[] = $ambientSoundMode; } if ($inOnEardetection) { $query .= " AND ux.hp_in_on_ear_detection = %d"; $params[] = $inOnEardetection; } if ($muteFunction) { $query .= " AND ux.hp_mute_function = %d"; $params[] = $muteFunction; } if ($multipointCount) { $query .= " AND conn.hp_multipoint_count = %d"; $params[] = $multipointCount; } if ($voiceCommands) { $query .= " AND ux.hp_voice_commands = %d"; $params[] = $voiceCommands; } // Prepare and execute the query if (!empty($params)) { $prepared_query = $wpdb->prepare($query, $params); $results = $wpdb->get_results($prepared_query); } else { // When there are no parameters, but the query has been dynamically built, still use prepare for safety $prepared_query = $wpdb->prepare($query, []); $results = $wpdb->get_results($prepared_query); } // Check for results if (empty($results)) { return new WP_Error("no_results", "No results found", [ "status" => 404, ]); } return $results; } // Register the REST API endpoint for the headphone finder add_action("rest_api_init", function () { register_rest_route("headphonesprice/v1", "/finder/data/", [ "methods" => WP_REST_Server::CREATABLE, // POST method "callback" => "find_headphones", "permission_callback" => "__return_true", ]); }); // Popular headphone brand logos function headphones_brand_logos() { global $wpdb; $query = "SELECT * FROM zzhp_brand WHERE hp_brand_logo_url IS NOT NULL AND hp_brand_logo_url <> ''"; $results = $wpdb->get_results($query); return $results; } // Register the REST API endpoint add_action("rest_api_init", function () { register_rest_route("headphonesprice/v1", "/brand_logos/", [ "methods" => "GET", "callback" => "headphones_brand_logos", "permission_callback" => "__return_true", ]); }); function child_enqueue_styles_and_scripts() { // Enqueue child theme main stylesheet wp_enqueue_style( "astra-child-theme-css", get_stylesheet_directory_uri() . "/style.css", ["astra-theme-css"], CHILD_THEME_ASTRA_CHILD_VERSION, "all" ); // Enqueue universal styles for all pages wp_enqueue_style( "universal-style", get_stylesheet_directory_uri() . "/include/styles/universal.css", ["astra-child-theme-css"], "1.0.0", "all", "screen", "preload" ); // Enqueue page-specific styles global $post; // Make sure you have access to the global $post variable. if (isset($post->ID)) { switch ($post->ID) { case 1421: wp_enqueue_style( "home-page-style", get_stylesheet_directory_uri() . "/include/styles/home.css", ["astra-child-theme-css"], "1.0.0", "all", "screen", "preload" ); break; case 3135: wp_enqueue_style( "another-home-page-style", get_stylesheet_directory_uri() . "/include/styles/home.css", ["astra-child-theme-css"], "1.0.0", "all", "screen", "preload" ); break; case 1473: wp_enqueue_style( "news-page-style", get_stylesheet_directory_uri() . "/include/styles/news.css", ["astra-child-theme-css"], "1.0.0", "all", "screen", "preload" ); break; case 1447: wp_enqueue_style( "prices-page-style", get_stylesheet_directory_uri() . "/include/styles/prices.css", ["astra-child-theme-css"], "1.0.0", "all", "screen", "preload" ); break; case 6835: wp_enqueue_style( "finder-page-style", get_stylesheet_directory_uri() . "/include/styles/finder.css", ["astra-child-theme-css"], "1.0.0", "all", "screen", "preload" ); break; } } } add_action("wp_enqueue_scripts", "child_enqueue_styles_and_scripts", 15); function enqueue_custom_scripts() { global $post; // Conditionally enqueue page-specific scripts if (isset($post->ID)) { switch ($post->ID) { case 1421: wp_enqueue_script( "home-page-script", get_theme_file_uri("/include/js/home.js"), [], "1.0.0", true ); break; case 1473: wp_enqueue_script( "news-page-script", get_theme_file_uri("/include/js/news.js"), [], "1.0.0", true ); break; case 1447: wp_enqueue_script( "prices-page-script", get_theme_file_uri("/include/js/prices.js"), [], "1.0.0", true ); break; case 6835: wp_enqueue_script( "prices-page-script", get_theme_file_uri("/include/js/finder.js"), [], "1.0.0", true ); break; } } } add_action("wp_enqueue_scripts", "enqueue_custom_scripts"); if (!wp_next_scheduled('sync_custom_products_event') && function_exists('sync_custom_products_to_wp')) { wp_schedule_event(time(), 'daily', 'sync_custom_products_event'); } add_action('sync_custom_products_event', 'sync_custom_products_to_wp');