Skip to content

Commit

Permalink
Move to lazy gathering of AMP_Post_Template properties
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Nov 18, 2019
1 parent d536c8f commit c74ba97
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 32 deletions.
88 changes: 57 additions & 31 deletions includes/templates/class-amp-post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/**
* Class AMP_Post_Template
*
* @property WP_Post $post
*
* @since 0.2
*/
class AMP_Post_Template {
Expand Down Expand Up @@ -48,14 +50,6 @@ class AMP_Post_Template {
*/
const DEFAULT_NAVBAR_COLOR = '#fff';

/**
* Template directory.
*
* @since 0.2
* @var string
*/
private $template_dir;

/**
* Post template data.
*
Expand All @@ -72,36 +66,29 @@ class AMP_Post_Template {
*/
public $ID;

/**
* Post.
*
* @since 0.2
* @var WP_Post
*/
public $post;

/**
* AMP_Post_Template constructor.
*
* @param WP_Post|int $post Post.
*/
public function __construct( $post ) {

$this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' );

if ( $post instanceof WP_Post ) {
$this->post = $post;
} else {
$this->post = get_post( $post );
}

// Make sure we have a post, or bail if not.
if ( $this->post instanceof WP_Post ) {
$this->ID = $this->post->ID;
} else {
return;
if ( is_int( $post ) ) {
$this->ID = $post;
} elseif ( $post instanceof WP_Post ) {
$this->ID = $post->ID;
}
}

/**
* Set data.
*
* This is called in the get method the first time it is called.
*
* @since 1.5
*
* @see \AMP_Post_Template::get()
*/
private function set_data() {
$content_max_width = self::CONTENT_MAX_WIDTH;
if ( isset( $GLOBALS['content_width'] ) && $GLOBALS['content_width'] > 0 ) {
$content_max_width = $GLOBALS['content_width'];
Expand Down Expand Up @@ -156,6 +143,41 @@ public function __construct( $post ) {
$this->data = apply_filters( 'amp_post_template_data', $this->data, $this->post );
}

/**
* Getter.
*
* @since 1.5
*
* @param string $name Property name.
* @return mixed
*/
public function __get( $name ) {
switch ( $name ) {
case 'post':
return get_post( $this->ID );
}
return null;
}

/**
* Get template directory for Reader mode.
*
* @since 0.2
* @return string Template dir.
*/
private function get_template_dir() {
static $template_dir = null;
if ( ! isset( $template_dir ) ) {
/**
* Filters the Reader template directory.
*
* @since 0.3.3
*/
$template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' );
}
return $template_dir;
}

/**
* Getter.
*
Expand All @@ -165,6 +187,10 @@ public function __construct( $post ) {
* @return mixed Value.
*/
public function get( $property, $default = null ) {
if ( ! isset( $this->data ) ) {
$this->set_data();
}

if ( isset( $this->data[ $property ] ) ) {
return $this->data[ $property ];
}
Expand Down Expand Up @@ -219,7 +245,7 @@ public function load_parts( $templates ) {
* @return string Template path.
*/
private function get_template_path( $template ) {
return sprintf( '%s/%s.php', $this->template_dir, $template );
return sprintf( '%s/%s.php', $this->get_template_dir(), $template );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion templates/html-end.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<?php
/**
* Fires when just before printing the </body> closing tag.
* Fires just before printing the </body> closing tag.
*
* @since 0.2
* @see wp_footer()
Expand Down

0 comments on commit c74ba97

Please sign in to comment.