-
Notifications
You must be signed in to change notification settings - Fork 383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#849 Add paired mode custom templates #856
Conversation
… branch Remove erroneous hash from id on amp-wp-header
…in order to better follow WP hierarchy.
* @since 0.7 | ||
* @var bool | ||
*/ | ||
public $active; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel the term "active" might be too ambiguous here. This is what the callback property within the theme support addition controls, and is used when we do have a custom template included but want to control if it should be AMP or default WP templates (since the custom will try to follow WP template hierarchy and we may not always want it to).
@ThierryA and @westonruter, would either of you mind reviewing this PR for #849? 😃 |
|
||
$this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' ); | ||
if ( isset( $args['active_callback'] ) ) { | ||
$this->active = $args['active_callback'](); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this may need to be call_user_func( $args['active_callback'] )
for PHP 5.2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind! I was way wrong. This works even back in PHP 4: https://3v4l.org/TcVa0
@kaitnyl It looks like I pushed up some code after you pushed up yours. Take a look at this line of my #857 PR here: I think that instead of adding code to |
…dd/849-paired-mode-templates
Implement full template hierarchy support
…red-mode-templates
includes/class-amp-theme-support.php
Outdated
$args = array_shift( $support ); | ||
|
||
// @todo We might want to rename active_callback to available_callback.. | ||
if ( isset( $args['active_callback'] ) && is_callable( $args['active_callback'] ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we rename active_callback
to available_callback
? It would seem a little strange for an active callback to return true
if on the frontend in paired mode, since is_amp_endpoint()
would be false.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the name isn't the most accurate. I've updated it to available_callback
in b10c0fe, but display_callback
might even be a more clear. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I like available
more. It sounds more like something that would return a boolean. There is a display_callback
in core, but it is used in widgets to actually render something out. Otherwise, in core, there is active_callback
and video-active-callback
.
Core usage: https://gist.github.com/westonruter/49552860d1e57a20547bcebd090ef269
I'm torn between available and active.
includes/class-amp-theme-support.php
Outdated
if ( isset( $args['template_path'] ) ) { | ||
$amp_templates = array(); | ||
foreach ( $templates as $template ) { | ||
$amp_templates[] = $args['template_path'] . '/' . $template; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since template_path
is a relative path from the theme root to locate the AMP templates directory, is this still the best name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated these to use template_dir
which is what AMP_Post_Template
is calling it, too. Setting this will look like the following, which should be easier since it'll be done inside of a theme:
add_theme_support( 'amp', array(
'template_dir' => 'amp-templates',
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, there is the amp_post_template_dir
filter:
$this->template_dir = apply_filters( 'amp_post_template_dir', AMP__DIR__ . '/templates' );
One difference is that the template_dir
is absolute here, whereas what we're doing in the theme support is relative. I doubt there will be confusion anyway.
…red mode templates to match AMP_Post_Template naming.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@westonruter @kaitnyl below is my CR. Thanks,
includes/class-amp-theme-support.php
Outdated
@@ -14,6 +14,84 @@ class AMP_Theme_Support { | |||
|
|||
const COMPONENT_SCRIPTS_PLACEHOLDER = '<!--AMP_COMPONENT_SCRIPTS_PLACEHOLDER-->'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add PHPDoc.
amp.php
Outdated
@@ -131,9 +131,9 @@ function amp_maybe_add_actions() { | |||
// Add hooks for when a themes that support AMP. | |||
if ( current_theme_supports( 'amp' ) ) { | |||
if ( amp_is_canonical() || is_amp_endpoint() ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is_amp_endpoint()
returns true
if amp_is_canonical()
so this if statement could simply be if ( is_amp_endpoint() )
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since $is_amp_endpoint = is_amp_endpoint();
is declared further down, I would suggest to move it up to the top of this function and replace is_amp_endpoint()
with the variable.
amp.php
Outdated
} else { | ||
AMP_Frontend_Actions::register_hooks(); | ||
amp_add_frontend_actions(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why calling this here again since it is already a wp_head
callback and it already has the necessary conditional statement in the function itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ThierryA I don't understand. This function is only being called here in the amp_maybe_add_actions()
function. It gets called here at the top in the case where the theme supports amp
. Otherwise, it gets called below when the theme does not.
includes/class-amp-theme-support.php
Outdated
$args = array_shift( $support ); | ||
|
||
if ( isset( $args['available_callback'] ) && is_callable( $args['available_callback'] ) ) { | ||
return $args['available_callback'](); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using call_user_func()
would probably be more appropriate here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose it would be more readable.
includes/amp-frontend-actions.php
Outdated
return; | ||
} | ||
|
||
$amp_url = amp_get_permalink( get_queried_object_id() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really a need to store this in a variable, the following would be readable too:
printf(
'<link rel="amphtml" href="%s">',
esc_url( amp_get_permalink( get_queried_object_id() ) )
);
includes/class-amp-theme-support.php
Outdated
public static function init() { | ||
require_once AMP__DIR__ . '/includes/amp-post-template-actions.php'; | ||
if ( amp_is_canonical() ) { | ||
$is_amp_endpoint = ( false !== get_query_var( AMP_QUERY_VAR, false ) ); // Because is_amp_endpoint() now returns true if amp_is_canonical(). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There isn't really a need to store this in a variable, the condition could be added directly in the if statement below.
*/ | ||
public static function register_paired_hooks() { | ||
foreach ( self::$template_types as $template_type ) { | ||
add_filter( "{$template_type}_template_hierarchy", array( __CLASS__, 'filter_paired_template_hierarchy' ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be preferred if there was a template_hierarchy
filter in core so that the list of types wouldn't have to be hard-coded. But alas, there is no such filter: https://github.com/WordPress/wordpress-develop/blob/fb5b6170564b403944a8fbf8a4e6eb877b26a092/src/wp-includes/template.php#L30-L44
…adability improvements.
This PR will allow users to specify a custom AMP template folder from within their own theme for #849.
===
Completed
template_include
in order to follow typical template schemas.Testing example inside a theme:
Updated as of b10c0fe.
===
In question
This part of the acceptance criteria may not be needed (but could be useful for clarity)? Before we get to
amp_prepare_render()
, we're initially insideamp_maybe_add_actions()
and that will prevent callingamp_prepare_render()
depending on this line.