17. <?php
$context = Timber::get_context();
$context['posts'] = Timber::get_posts([
'posts_per_page' => 5,
'tag' => 'club'
]);
Timber::render('index.twig', $context);
index.php
t
Procedural
"
Procedural code is often
not DRY or reusable.
It’s difficult to change.
Hard to read.
18. index.php
class IndexController
{
}
public function handle()
{
$context = Timber::get_context();
$context['posts'] = Timber::get_posts([
'posts_per_page' => 5,
'tag' => 'club'
]);
}
extends BaseController
return new TimberResponse('index.twig', $context);return Timber::render('index.twig', $context);
19. index.php
class IndexController extends BaseController
{
public function handle()
{
$context = Timber::get_context();
$context['posts'] = $this->getPosts();
return new TimberResponse('index.twig', $context);
}
private function getPosts()
{
return Timber::get_posts([
'posts_per_page' => 5,
'tag' => 'club'
]);
}
}
20. Benefits
• Write Object Orientated code rather than Procedural code
• Can use inheritance to extend a base class for common functionality
• Can encapsulate more complex routines in private functions
• Make use of PSR-7 compliant Response objects
23. // ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'root');
/** MySQL database password */
define('DB_PASSWORD', ‘letmein');
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
wp-config.php
34. Lumberjack Config
• Post Type Registration
• Image sizes
• Error logging
• Menu locations
• Load paths of Twig files for Timber
• Basic theme support (e.g. enable Featured Image support)
39. class Product extends Post
{
public function getPhotos() : array
{
// Do database query to get the assigned photos
}
}
Encapsulate Business Logic
$product = new Product(123);
$photos = $product->getPhotos();
40. class Product extends Post
{
}
Register Custom Post Types
// config/posttypes.php
return [
'register' => [
AppPostTypesProduct::class,
],
];
protected static function getPostTypeConfig()
{
return [
'labels' => [
'name' => __('Products'),
'singular_name' => __('Product'),
],
'public' => true,
'has_archive' => false,
];
}
public static function getPostType()
{
return 'product';
}
46. Router::get('hello/world', function () {
return HtmlResponse('<h1>Hello World!</h1>');
});
Simple Routes
use RareloopLumberjackFacadesRouter;
use ZendDiactorosResponseHtmlResponse;
50. Router benefits
• Extend WordPress site with custom URL endpoints
• Access to all REST based verbs
• Can add Groups, for collecting similar resources together
52. Baked-in Responses
$response = new TimberResponse(‘templates/home.twig’, $context);
// From https://github.com/zendframework/zend-diactoros
$response = new TextResponse('Hello world!');
$response = new HtmlResponse($htmlContent);
$response = new XmlResponse($xml);
$response = new JsonResponse($data);
$response = new EmptyResponse(); // Basic 204 response:
$response = new RedirectResponse('/user/login');
58. • PSR11 Dependency Injection
Container (using PHP-DI)
• Facades
• Exception Handling (PSR3)
• Validation (currently an external
package)
• View Models
More power at your fingertips ✨
• Service Providers
• Hatchet (CLI)
• Global helper functions (e.g.
config(‘app.environment’); )
• Debug bar & Dump server
• ‘Responsable’ objects (from Laravel) -
in, but currently undocumented
59. • Sessions (coming soon)
• Example theme (currently in development)
• Automated Testing
• Unit tests with PHPUnit
• Integration tests with Laravel Dusk
On the radar 📡
60. namespace TestsBrowser;
use ChadanukDuskWordpressTestsTestCase;
class ExampleTest extends TestCase
{
/* @test */
public function can_see_home_page_title()
{
$postFactory = new WP_UnitTest_Factory_For_Post();
$postId = $postFactory->create_object([
'post_title' => 'Home page title',
'post_type' => 'page',
'post_name' => 'home'
]);
$this->browse(function ($browser) {
$browser->visit(‘/')
->assertSee('Home page title');
});
}
}
66. • Evaluate the product mindset for you (and your team?)
• WordPress is awesome, we can make it even more awesome!
• Challenge and push your stack forward
• Write code which is easier to change
• Lumberjack is basically Laravel in WordPress
• Use only what you need to in Lumberjack
In summary…