Updates to Embed Privacy’s thumbnail generation
Published: – Leave a comment Last update:
With the latest feature update 1.9.0 for Embed Privacy, it received a new thumbnail generation method. And I want to talk about it. Basically, it’s part of a broader approach to enhance the extensibility of the plugin while cleanup code and keep it easily maintainable.
The interface
It all starts with a new interface for embed providers with thumbnail support. As per definition, this interface makes sure that all thumbnail providers behave the same and are standardized.
In this case, it means that every thumbnail provider must implement a functionality to get the thumbnail, store it. Also it must accomplish the possibility to get a thumbnail by the ID of an embed as well as getting its path, URL and the title of the thumbnail provider. Last, but not least, there must be a functionality to check whether the current embed is from this specific thumbnail provider.
Abstract thumbnail provider
Also newly introduced is a new abstract class to implement some basic functionality. It implements the methods to get the path and URL of a stored thumbnail as well as the check for matching an embed with a thumbnail provider.
All actual thumbnail providers must extend this class to be a working thumbnail provider.
An actual thumbnail provider
Having an actual thumbnail provider, it must first register it via a filter (for more information, see the documentation) and implement the actual download and saving methods already described above.
Since there are already a couple of thumbnail providers available, feel free to use them as base for your custom implementations. You can find them on GitHub (this link points to v1.9.0; in case you read this later, make sure to always check the latest version for possible changes).
Tackled problems
There are some problems that are not always that easy when it comes to embed thumbnails. Some of them already have a solution within the thumbnail providers that ship with Embed Privacy. If you experience them as well, you can checkout the code of the particular thumbnail provider that has a solution for it.
No ID available
Usually, there is some sort of unique identifier available in either the URL or the embed code of an embed provider. This is always needed to connect and save the thumbnail to a specific embed. In case of SlideShare for example there is a problem that it may not be always available. While the thumbnail URL contains such an ID, the actual embedded URL does not. To still keep them connected, there is a method to get all metadata of a thumbnail provider for a specific post (via Thumbnail::get_metadata()
). Since every thumbnail stores two post meta entries (one with the location of the thumbnail and the second with the actual embedded URL the thumbnail belongs to), you can use this metadata to check for the URL and extract the ID from the post meta key (which always is in the format Thumbnail::METADATA_PREFIX . '_<provider-name>_<id>_url'
).
No image available
The default oEmbed code for WordPress TV for instance does not return any thumbnail whatsoever. As a solution, I download the URL to the actual content (not to the iframe source itself) that you probably visit via WordPress.tv and extract the value of the og:image
meta element, that contains a proper thumbnail.
Migration
The old Thumbnails
class acts solely as a wrapper for backwards compatibility, but will be removed with version 2.0.0. If you’ve already used functionality from it, please migrate it as noted above. Its functionality is either part of the class epiphyt\Embed_Privacy\thumbnail\Thumbnail
or specific in one of the available thumbnail providers.
Conclusion
The code is now cleaner and easier to maintain and extend. It’s the first iteration of a whole revision of Embed Privacy. There’s more to come!