Hi Prebhdev Singh !
Your code is pretty clever, I like it ! Clean and short...
I did develop my own plugin (forking yours) to do what yours is doing, but... I had it erased by an hard drive crash after lots of hours working on it.
I had some nice ideas for it and would like to suggest them to you, as I would really like to see them in your plugin (since I have no more time - and motivation - now to start my own version from scratch again).
Those were my main ideas :
- do not hook your function on publish_post, but rather add a checkbox on the edit screen so you still can check for remote medias when you are editing the post, even after having published it.
- Your plugin should download remote medias but should also check first that the media do not already exists: I suggest your plugin to save a meta data (the remote media url) for each media imported, and to compare it to the url of the remote media you try to import.
+ : check if the url of the media is not local : no need to import a media that is already on our server.
- load the post content as DOMDocument element should be an easier way to parse the code
- Check if a featured image exists before setting one here + like in 1), have it as an option on the edit screen (do not force it)
- When finding a remote image in the post content, check if it is inside a link tag (a href) linking to an image : that would mean the first image is a thumbnail and that we have the link to its HD version, so we should download the HD image instead. (this is what I needed when I forked your plugin, and that is where the others ideas are coming from)
- A metabox displaying the list of the remote medias we could import is even a better idea than the checkbox I was talking in 1) : with a list, the user could choose which medias he wants to import, and ignore the others.
- Eventually allow to download other medias like videos or audios if a video or audio tag is found. (and that's why the previous point is important)
- add hooks & filters so we can extend your plugin
Hope you will like the ideas ! Give me your feedback on it !