Skip to content

Instantly share code, notes, and snippets.

@EdwardBock
Last active October 17, 2021 13:17
Show Gist options
  • Save EdwardBock/157a5165ecbb3f8027385e1ecb8fdd85 to your computer and use it in GitHub Desktop.
Save EdwardBock/157a5165ecbb3f8027385e1ecb8fdd85 to your computer and use it in GitHub Desktop.
Theme interface segregation - proof of concept
<?php
// ...
require_once dirname(__FILE__)."/Theme.php
<?php
namespace PublicFunctionOrg\WordPress\Theme\Sample;
interface ReadingTimeFeature{
public function render($post_id);
}
<?php
namespace PublicFunctionOrg\WordPress\Theme\Sample;
class ReadingTimeFeatureImpl implements ReadingTimeFeature {
public function render($post_id){
if(function_exists('reading_time_render')){
reading_time_render($post_id);
}
}
}
<?php
get_header();
// ... all the html page rendering
while ( have_posts() ) :
the_post();
// ... post html rendering
\PublicFunctionOrg\WordPress\Theme\Sample\Theme::readingTime()->render(get_the_ID());
// ... even more post html rendering
endwhile;
// ... even more html page rendering
get_sidebar();
get_footer();
<?php
namespace PublicFunctionOrg\WordPress\Theme\Sample;
class Theme {
private function __construct(){
// https://medium.com/write-better-wordpress-code/psr-4-autoloading-in-plugins-and-themes-720f459f4df4
require_once dirname(__FILE__)."/vendor/autoload.php";
}
// ------------------------------
// singleton
// ------------------------------
private static $instance;
public static function instance(): self{
if(static::$instance == null){
static::$instance = new static();
}
return static::$instance;
}
// ------------------------------
// feature access
// ------------------------------
public function readingTime(): ReadingTimeFeature {
if($this->readingTimeFeature == null){
$this->readingTimeFeature = new ReadingTimeFeatureImpl();
}
return $this->readingTimeFeature;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment