Create a gist now

Instantly share code, notes, and snippets.

Embed
<?php
$shortcodes = [
'recent_posts' => __DIR__ . 'views/recent-posts.php',
'products' => __DIR__ . 'views/products.php'
];
new Shortcodes( $shortcodes );
<?php
class Posts {
/**
* @var \WP_Query
*/
protected $query;
public function __construct( array $args )
{
$this->query = new \WP_Query( $args );
}
public function getQuery() : \WP_Query
{
return $this->query;
}
public function getPosts() : array
{
return $this->query->posts;
}
}
class Shortcodes {
/**
* Shortcode collection
*
* @var array
*/
protected $shortcodes;
/**
* Shortcodes constructor.
*
* @param array $shortcodes Shortocdes to use in form of 'tag' => 'template_path'
*/
public function __construct( array $shortcodes )
{
$this->shortcodes = $shortcodes;
foreach ( $shortcodes as $shortcode => $template ){
if( is_file( $template ) && file_exists( $template ) ){
add_shortcode( $shortcode, [ $this, $shortcode ] );
}
}
}
/**
* Dynamically create callback for shortcodes
*
* @param string $name The shortcode tag
* @param array $arguments Not used right now, but could be used for shortcode atts
*
* @return string
*/
public function __call( $name, $arguments )
{
if( array_key_exists( $name, $this->shortcodes ) ){
return $this->genericHandler( $this->toCamelCase( $name ), $this->shortcodes[ $name ] );
}
}
/**
* Render a shortcode using posts collection and its corresponding post collection
*
* @param string $collection_name
* @param string $template
*
* @return string
*/
protected function genericHandler( string $collection_name, string $template )
{
$posts = System::getInstance()->getContainer()->get( $collection_name )->getPosts();
ob_start();
include $template;
return ob_get_clean();
}
/**
* Turn shortcode name into camelCased argument
*
* @param $string
*
* @return string
*/
protected function toCamelCase($string) : string
{
$string = str_replace('-', ' ', $string);
$string = str_replace('_', ' ', $string);
$string = ucwords(strtolower($string));
$string = str_replace(' ', '', $string);
return $string;
}
}
composer require php-di/php-di
composer require ocramius/proxy-manager
<?php
class PostCollections {
protected static $recent_posts;
protected static $products;
public function recent_posts() : array
{
if( null === static::$recent_posts ){
static::$recent_posts = ( new Posts( [ 'posts_per_page' => 3 ] ) )->getPosts();
}
return static::$recent_posts;
}
public function products() : array
{
if( null === static::$products ){
static::$products = ( new Posts( [ 'posts_per_page' => 15, 'post_type' => 'product' ] ) )->getPosts();
}
return static::$products;
}
}
<?php
class PostsFactory {
public static function create( $post_type, int $posts_per_page = 10, int $page = 1, array $args = [] ) : Posts
{
$args = wp_parse_args( $args, [
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'page' => $page
] );
return new Posts( $args );
}
}
<?php
add_action( 'plugins_loaded', function(){
add_action( 'parse_request', function( \WP $wp ){
if( isset( $wp->query_vars[ 'name' ] ) ){
switch( $wp->query_vars[ 'name' ] ){
case 'recent-posts' :
$route = 'RecentPosts';
break;
case 'products' :
$route = 'Products';
break;
default:
$route = null;
break;
}
if( $route ){
status_header( 200 );
$controller = new Controller();
echo $controller->$route( System::getInstance()->getContainer()->get( $route ) );
exit;
}
}
});
});
class Controller {
public function recentPosts( Posts $posts )
{
$posts = $posts->getPosts();
}
public function prodcuts( Posts $posts )
{
$posts = $posts->getPosts();
}
}
<?php
class RecentPostShortcode {
protected $posts;
protected $template_path;
const SHORTCODE_NAME = 'recent_posts_shortcode';
public function __construct( array $posts, string $template_path )
{
$this->posts = $posts;
$this->template_path = $template_path;
}
public function handler(){
ob_start();
$posts = $this->posts;
include $this->template_path;
return ob_get_clean();
}
}
add_action( 'init', function(){
$object = new RecentPostShortcode( PostsFactory::create( 'post', 3 ), '/path/to/template.php' );
add_shortcode( RecentPostShortcode::SHORTCODE_NAME, [ $object, 'handler' ] );
});
class recent_posts_shortcode{
/**
* @var \WP_Query
*/
protected $query;
public function __construct() {
add_shortcode( 'recent_posts', [ $this, 'handler' ] );
}
public function handler(){
$this->do_query();
while( $this->query->have_posts() ){
$this->query->the_post();
echo $this->query->post->post_title;
}
}
protected function do_query(){
$this->query = new \WP_Query( [
'post_type' => 'posts',
'posts_per_page' => 3
] );
}
}
add_action( 'init', function(){
$class = new recent_posts_shortcode();
});
use DI\ContainerBuilder;
final class System {
/**
* @var \DI\Container
*/
protected $container;
/**
* @var System
*/
protected static $instance;
protected function __construct() {
$this->buildContainer();
}
public function getInstance() : System
{
if( null == static::$instance ){
static::$instance = new static();
}
return static::$instance;
}
public function getContainer() : \DI\Container
{
return $this->container;
}
/**
* @return \DI\Container
*/
protected function buildContainer() {
$builder = new ContainerBuilder();
$builder->addDefinitions( [
'RecentPosts' => function ( ContainerBuilder $c ) {
return PostsFactory::create( 'post', 3 );
},
'Products' => function ( ContainerBuilder $c ) {
return PostsFactory::create( 'product', 50 );
}
] );
$this->container = $builder->build();
}
}
<?php
/*
Plugin Name: Example of the God Singleton
*/
final class Plugin {
public $emails;
public $settings;
public $database;
public $api;
public $admin;
public $front_end;
public $assets;
protected static $instance;
private function __construct(){}
public function get_instance(){
if( null === self::$instance ) {
self::$instance = new self();
self::$instance->emails = new Plugin_Emails();
self::$instance->settings = new Plugin_Setttings();
self::$instance->database = new Plugin_Database();
self::$insanct->api = new Plugin_Api();
self::$instance->front_end = new Plugin_Front_End();
self::$instance->assets = new Plugin_Assets();
}
return self::$instance;
}
}
add_action( 'init', array( 'Plugin', 'get_instance' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment