controller
2 months ago
core
1 year ago
class-ga-admin.php
2 months ago
class-ga-autoloader.php
4 years ago
class-ga-frontend.php
2 months ago
class-ga-helper.php
2 months ago
class-ga-hook.php
4 years ago
class-ga-notice.php
2 months ago
class-ga-oauth.php
2 months ago
class-ga-sharethis.php
4 years ago
class-ga-stats.php
2 months ago
class-ga-template.php
4 years ago
class-ga-template.php
83 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Google Analytics template. |
| 4 | * |
| 5 | * @package GoogleAnalytics |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Class Ga_Template |
| 10 | */ |
| 11 | class Ga_Template { |
| 12 | /** |
| 13 | * Array of template properties. |
| 14 | * |
| 15 | * @var array Props array. |
| 16 | */ |
| 17 | protected $props; |
| 18 | |
| 19 | /** |
| 20 | * Relative path in view/ folder. |
| 21 | * |
| 22 | * @var string Path string. |
| 23 | */ |
| 24 | protected $path; |
| 25 | |
| 26 | /** |
| 27 | * Ga_Template constructor. |
| 28 | * |
| 29 | * @param string $path Relative path in view/ folder. |
| 30 | * @param array $props Array of props to be passed to the template. |
| 31 | */ |
| 32 | public function __construct( $path, $props = array() ) { |
| 33 | $this->path = $path; |
| 34 | $this->props = $props; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Include rendered template inline. |
| 39 | * |
| 40 | * @param string $path Relative path in view/ folder. |
| 41 | * @param array $props Array of props to be passed to the template. |
| 42 | */ |
| 43 | public static function load( $path, $props = array() ) { |
| 44 | ( new static( $path, $props ) )->include_template(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get rendered template. |
| 49 | * |
| 50 | * @param string $path Relative path in view/ folder. |
| 51 | * @param array $props Array of props to be passed to the template. |
| 52 | * |
| 53 | * @return string Rendered template. |
| 54 | */ |
| 55 | public static function render( $path, $props = array() ) { |
| 56 | return ( new static( $path, $props ) )->render_template(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Include template. |
| 61 | */ |
| 62 | public function include_template() { |
| 63 | $template_path = GA_PLUGIN_DIR . '/view/' . $this->path . '.php'; |
| 64 | |
| 65 | if ( is_readable( $template_path ) ) { |
| 66 | load_template( $template_path, false, $this->props ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get rendered template. |
| 72 | * |
| 73 | * @return string |
| 74 | */ |
| 75 | public function render_template() { |
| 76 | ob_start(); |
| 77 | $this->include_template(); |
| 78 | $render = ob_get_contents(); |
| 79 | |
| 80 | return false === empty( $render ) ? $render : ''; |
| 81 | } |
| 82 | } |
| 83 |