( $sizes[0], $sizes[1] ); } public function url_has_200_response( $url ) { if ( ! $url || ! ini_get( 'allow_url_fopen' ) ) { return false; } $file_headers = get_headers( $url ); if ( empty( $file_headers[0] ) ) { return false; } return false !== strstr( $file_headers[0], '200' ); } public function is_external_url( $absolute_url ) { if ( str_starts_with( $absolute_url, $this->get_content_url() ) ) { return false; } if ( str_starts_with( $absolute_url, $this->upload_dir->get_upload_url() ) ) { return false; } return true; } public function url_to_path( $absolute_url ) { if ( str_starts_with( $absolute_url, $this->get_content_url() ) ) { return str_replace( $this->get_content_url(), WP_CONTENT_DIR, $absolute_url ); } $upload_url = $this->upload_dir->get_upload_url(); $upload_dir = $this->upload_dir->get_upload_path(); if ( str_starts_with( $absolute_url, $upload_url ) ) { return str_replace( $upload_url, $upload_dir, $absolute_url ); } return false; } private function get_content_url() { if ( ! $this->content_url ) { $this->content_url = content_url(); } return $this->content_url; } /** * Get full size image url from resized one. * * @param string $src Image URL. * * @return string * @since 3.0 * */ public function get_url_without_dimensions( $src ) { $extensions = array( 'gif', 'jpg', 'jpeg', 'png', 'webp', ); if ( ! preg_match( '/(-\d+x\d+)\.(' . implode( '|', $extensions ) . ')(?:\?.+)?$/i', $src, $src_parts ) ) { return $src; } // Remove WP's resize string to get the original image. $original_src = str_replace( $src_parts[1], '', $src ); // Extracts the file path to the image minus the base url. $file_path = substr( $original_src, strlen( $this->upload_dir->get_upload_url() ) ); // Continue only if the file exists. if ( file_exists( $this->upload_dir->get_upload_path() . $file_path ) ) { return $original_src; } // Revert to source if file does not exist. return $src; } /** * Convert original image URL to scaled image URL. * * @param string $src Original image URL. * * @return string|null */ public function get_scaled_image_url( $original_src ) { if ( strpos( $original_src, '-scaled' ) !== false ) { return $original_src; } $path_info = pathinfo( $original_src ); $scaled_filename = $path_info['filename'] . '-scaled.' . $path_info['extension']; $scaled_src = str_replace( $path_info['basename'], $scaled_filename, $original_src ); $scaled_path = $this->url_to_path( $scaled_src ); if ( $scaled_path && file_exists( $scaled_path ) ) { return $scaled_src; } return null; } public function normalize_url( $url ) { $url = str_replace( array( 'http://', 'https://', 'www.' ), '', $url ); return untrailingslashit( $url ); } public function is_relative( $url ) { return ! empty( preg_match( '/^\./', $url ) ) || empty( wp_parse_url( $url, PHP_URL_HOST ) ); } }