diff --git a/README.md b/README.md index 3ec6915..e574174 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,10 @@ Wordpress post feed API service for downloading a list of new blog posts. +The library downloads the feed of posts from Wordpress and provides a simple API for reading them. + +Posts from the feed are automatically cached for a set period of time, so they don't have to be downloaded again in each request. + 📦 Installation --------------- @@ -18,6 +22,86 @@ $ composer require baraja-core/wordpress-post-feed You can use the package manually by creating an instance of the internal classes, or register a DIC extension to link the services directly to the Nette Framework. +Configuration +------------- + +If you are installing the package into the Nette Framework, you can simply register the extension: + +```yaml +extensions: + wordpressPostFeed: Baraja\WordPressPostFeed\WordpressPostFeedExtension +``` + +However, the extension is not required, as you can create an instance manually: + +```php +$feed = new \Baraja\WordPressPostFeed\Feed; +``` + +By default, no dependencies are required. Feed will take care of all dependencies by itself. + +How to use +---------- + +Basic usage is simple: + +```php +$feed = new \Baraja\WordPressPostFeed\Feed; + +$posts = $feed->load('https://blog.mycleverminds.cz/feed/'); + +foreach ($posts as $post) { + echo $post->getTitle(); + echo $post->getDescription(); + echo $post->getLink(); + echo $post->getDate()->format('Y-m-d'); + echo $post->getCreator(); + echo json_encode($post->getCategories()); + echo $post->getMainImageUrl(); +} +``` + +The feed will be downloaded automatically, then cached and further requests will be handled instantly straight from the cache. + +If you only need to retrieve some posts from the feed, you can use the `limit` and `offset` arguments: + +```php +$posts = $feed->load( + url: 'https://blog.mycleverminds.cz/feed/', + limit: 3, + offset: 1, +); +``` + +Posts are automatically cached for the set period (default 2 hours). If the period expires, the cache is automatically invalidated and the next request will have to retrieve the data and rebuild the cache. This retrieval may slow down this request. If you want to handle all requests instantly, the cache for the feed needs to be automatically refreshed by cron. + +Cron will call the following method to refresh the cache: + +```php +$feed = new \Baraja\WordPressPostFeed\Feed; +$feed->updateCache('https://blog.mycleverminds.cz/feed/'); +``` + +Image manipulation +------------------ + +When downloading posts from the feed, images are also automatically downloaded and copied to disk. The image storage is provided by the `\Baraja\WordPressPostFeed\ImageStorage` service, which is automatically registered by the library. The default directory for storing images is `wordpress-post-feed` relative to your `index.php`. + +The service provides the following methods: + +| Method | Description | +|--------|-------------| +| `save(string $url): void` | Downloads the image from the URL and saves it to disk | +| `getInternalPath(string $url): string` | Returns the absolute path for the internal data store. | +| `getAbsoluteInternalUrl(string $url): string` | Returns the absolute URL to retrieve the image | +| `getRelativeInternalUrl(string $url): string` | Returns the relative URL to retrieve the image | + +Working with images is also available directly from the `Post` entity via methods: + +- `getAbsoluteInternalUrl()` +- `getRelativeInternalUrl()` +- `getMainImageUrl()` + 📄 License -----------