Last active
October 17, 2021 13:17
-
-
Save EdwardBock/157a5165ecbb3f8027385e1ecb8fdd85 to your computer and use it in GitHub Desktop.
Theme interface segregation - proof of concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ... | |
require_once dirname(__FILE__)."/Theme.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PublicFunctionOrg\WordPress\Theme\Sample; | |
interface ReadingTimeFeature{ | |
public function render($post_id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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