<?php // phpcs:ignore

namespace SEOPress\Services;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * SearchAttachment
 */
class SearchAttachment {

	/**
	 * The searchByUrl function.
	 *
	 * @param string $url The url.
	 *
	 * @return array
	 */
	public function searchByUrl( $url ) { // phpcs:ignore -- TODO: check if method is outside this class before renaming.
		global $wpdb;
		$parsed_url = wp_parse_url( $url );
		$path       = $parsed_url['path'];

		// Extract the file name without the size attribute.
		$filename       = basename( $path );
		$filename_parts = explode( '-', $filename );
		array_pop( $filename_parts ); // Remove the size attribute part.
		$clean_filename = implode( '-', $filename_parts );

		$limit = apply_filters( 'seopress_search_attachment_result_limit', 50 );
		if ( $limit > 200 ) {
			$limit = 200;
		}
		$data = $wpdb->get_results(
			$wpdb->prepare(
				"
			SELECT *
			FROM $wpdb->posts p
            WHERE p.guid LIKE %s
            AND p.post_type = 'attachment'
			LIMIT %d",
				'%' . $clean_filename . '%',
				$limit
			),
			ARRAY_A
		);

		foreach ( $data as $key => $value ) {
			$data[ $key ] = $value['ID'];
		}

		return $data;
	}
}
