Similar to Zend_Service_Twitter, this service allows you to easily consume a WordPress blog's content via WordPress' XML-RPC gateway.
Zend_Service_Wordpress tries to keep a small, consistent API that returns results based on the context of the call.
For example, you can call getCategories()
on both the ZendX_Service_Wordpress
and
ZendX_Service_Wordpress_Post
.
With the first, you'll receive all of the categories for the entire blog while the
latter returns only the categories attached to that post (as
ZendX_Service_Wordpress_Category
objects, of course).
// ZendX_Service_Wordpress(XMLRPC_URL, USERNAME, PASSWORD[, CACHE = true ])
//
// Caching assumes that categories/posts/tags aren't being edited during the lifetime
// or the page rendering
$wordpress = new ZendX_Service_Wordpress('http://wordpress-site.com/xmlrpc.php', 'username', 'password');
// Get default blog
$blog = $wordpress->getBlog();
// or a specific blog by id:
$blog = $wordpress->getBlog(0)
echo $blog->getTitle(); // My WordPress Blog
echo $blog->getDescription(); // Another WordPress Blog
echo $blog->getUrl(); // http://wordpress-site.com/
echo $blog->getLink(); // <a href="http://wordpress-site.com" title="My WordPress Blog">My WordPress Blog</a>
// etc...
foreach ($blog->getTags() as $tag) {
// Uses ZendX_Service_Wordpress_Tag object
echo sprintf('<a href="%s">%s</a>', $tag->getUrl(), $tag);
// or
echo $tag->getLink();
}
foreach ($blog->getCategories() as $category) {
// Uses ZendX_Service_Wordpress_Category object
echo sprintf('<a href="%s">%s</a>', $category->getUrl(), $category);
// or
echo $category->getLink();
}
foreach ($blog->getRecentPosts(10) as $post) {
// Uses ZendX_Service_Wordpress_Post object
echo $post->getLink();
}
echo $post->getTitle();
echo $post->getExcerpt();
echo $post->getBody();
// or
echo $post // __toString method calls `getBody()`
foreach ($post->getTags() as $tag) {
echo $tag->getLink();
}
foreach ($post->getCategories() as $category) {
echo $category->getLink();
}
You can perform unit testing by installing PHPUnit (I recommend the manual install) and doing...
# cd zend-service-wordpress/tests
# phpunit
Hopefully, you should see something like:
phpunit PHPUnit 3.3.9 by Sebastian Bergmann.
.......
Time: 18 seconds
OK (7 tests, 28 assertions)
If you want to run a specific test, simply run:
# phpunit --filter testTags.
However, if you want to run all tests for Wordpress
in general (or perhaps a
subset of tests for just Blog
, Post
, Category
, Author
, or Tag
), simply run:
# phpunit --group Wordpress
# phpunit --group Blog
# phpunit --group Post
etc...
You can run PHP CodeSniffer to ensure your markup follows the Zend standard by running:
# cd zend-service-wordpress
# phpcs --report=full --standard=Zend .