query); // Safe search override $safe = '0'; // No mature results if(preg_match('/(safe:)(on|off)/i', $query, $matches)) { if($matches[2] == 'on') $safe = '0'; if($matches[2] == 'off') $safe = '1'; $query = str_replace($matches[0], '', $query); } unset($matches); // Is there no query left? Bail! if(empty($query)) return false; $url = 'https://api.openverse.org/v1/images/?'.http_build_query(array( 'q' => $query, // Search query 'format' => 'json', // Response format 'mature' => $safe, // Safe search (1 = ON, 0 = OFF) 'page_size' => 50 // How many results to get )); unset($query, $safe); return $url; } public function get_request_headers() { $token_file = ABSPATH.'cache/token.data'; $token = unserialize(file_get_contents($token_file)); return array( 'Accept' => 'application/json, */*;q=0.8', 'Content-type' => 'application/x-www-form-urlencoded', 'Authorization' => 'Bearer '.$token['openverse']['access_token'], 'Accept-Language' => null, 'Accept-Encoding' => null, 'Sec-Fetch-Dest' => null, 'Sec-Fetch-Mode' => null, 'Sec-Fetch-Site' => null ); } public function parse_results($response) { $engine_temp = $engine_result = array(); $json_response = json_decode($response, true); // No response if(empty($json_response)) return $engine_temp; // Figure out results and base rank $number_of_results = $rank = $json_response['result_count']; // No results if($number_of_results == 0) return $engine_temp; // Use API result foreach($json_response['results'] as $result) { // Find data and process data $image_full = sanitize($result['url']); $image_thumb = (!empty($result['thumbnail'])) ? sanitize($result['thumbnail']) : $image_full; $url = sanitize($result['foreign_landing_url']); $alt = (!is_null($result['title'])) ? sanitize($result['title']) : null; $dimensions_w = (!is_null($result['width'])) ? sanitize($result['width']) : null; $dimensions_h = (!is_null($result['height'])) ? sanitize($result['height']) : null; $filesize = (!is_null($result['filesize'])) ? sanitize($result['filesize']) : null; $creator = (!empty($result['creator'])) ? " by ".sanitize($result['creator']) : null; // Skip broken results if(empty($image_thumb)) continue; if(empty($image_full)) continue; if(empty($url)) continue; // Process data if(!is_null($creator)) $alt = $alt.$creator; if(!is_null($filesize)) $filesize = intval(preg_replace('/[^0-9]+/', '', $filesize)); // Skip duplicate IMAGE urls/results if(!empty($engine_temp)) { if(in_array($image_full, array_column($engine_temp, 'image_full'))) continue; } $engine_temp[] = array ( // Required 'image_full' => $image_full, // string 'image_thumb' => $image_thumb, // string 'url' => $url, // string 'engine_rank' => $rank, // int // Optional 'alt' => $alt, // string | null 'width' => $dimensions_w, // int | null 'height' => $dimensions_h, // int | null 'filesize' => $filesize, // int | null ); $rank -= 1; } // Base info $number_of_results = count($engine_temp); if($number_of_results > 0) { $engine_result['source'] = 'Openverse'; $engine_result['amount'] = $number_of_results; $engine_result['search'] = $engine_temp; } unset($response, $json_response, $number_of_results, $rank); return $engine_result; } } ?>