Last active
August 12, 2020 21:54
-
-
Save westonruter/fafcc03ff7a2cbb66308835fdf8ff6bb to your computer and use it in GitHub Desktop.
Proof of concept for adapting existing AMP Legacy Reader mode post templates as part of a Reader theme in AMP plugin v2.0
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 | |
/** | |
* ⚠️ WARNING! The code here is not guaranteed to work in the future. It is intended as a short-term solution to | |
* utilize legacy AMP post templates with AMP plugin v2.0. | |
*/ | |
// Prevent the normal AMP Customizer initialization. | |
remove_action( 'after_setup_theme', 'amp_init_customizer', 12 ); | |
/** | |
* Customize AMP initialization to force legacy settings to be added. | |
* | |
* @see amp_init_customizer() | |
*/ | |
function custom_legacy_amp_init_customizer() { | |
if ( | |
! class_exists( 'AMP_Template_Customizer' ) | |
|| | |
! method_exists( 'AMP_Template_Customizer', 'register_legacy_settings' ) | |
|| | |
! method_exists( 'AMP_Template_Customizer', 'register_legacy_ui' ) | |
|| | |
! class_exists( 'AMP_Customizer_Design_Settings' ) | |
|| | |
! method_exists( 'AMP_Customizer_Design_Settings', 'init' ) | |
) { | |
return; | |
} | |
// Fire up the AMP Customizer. | |
add_action( 'customize_register', 'custom_legacy_init_amp_template_customizer', 500 ); | |
// Add some basic design settings + controls to the Customizer. | |
add_action( 'amp_init', [ 'AMP_Customizer_Design_Settings', 'init' ] ); | |
// Add a link to the AMP Customizer in Reader mode. | |
add_action( 'admin_menu', 'amp_add_customizer_link' ); | |
} | |
add_action( 'after_setup_theme', 'custom_legacy_amp_init_customizer', 12 ); | |
/** | |
* Wrapped version of `AMP_Template_Customizer::init()` which forces legacy AMP initialization and undoes other initialization. | |
* | |
* @see AMP_Template_Customizer::init() | |
* @param WP_Customize_Manager $wp_customize Customize manager. | |
*/ | |
function custom_legacy_init_amp_template_customizer( WP_Customize_Manager $wp_customize ) { | |
$self = AMP_Template_Customizer::init( $wp_customize ); | |
/** This action is documented in amp/includes/admin/class-amp-template-customizer.php */ | |
do_action( 'amp_customizer_init', $self ); | |
$self->register_legacy_settings(); | |
$self->register_legacy_ui(); | |
add_action( 'customize_controls_print_footer_scripts', [ $self, 'print_legacy_controls_templates' ] ); | |
add_action( 'customize_preview_init', [ $self, 'init_legacy_preview' ] ); | |
add_action( 'customize_controls_enqueue_scripts', [ $self, 'add_legacy_customizer_scripts' ] ); | |
// Undo what AMP_Template_Customizer::init() did for Reader themes. | |
remove_action( 'customize_controls_enqueue_scripts', [ $self, 'add_customizer_scripts' ] ); | |
remove_action( 'customize_controls_print_footer_scripts', [ $self, 'render_setting_import_section_template' ] ); | |
} |
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 | |
if ( | |
! function_exists( 'amp_is_available' ) | |
|| | |
! function_exists( 'amp_get_slug' ) | |
|| | |
! class_exists( 'AMP_Post_Template' ) | |
|| | |
! defined( 'AMP__DIR__' ) | |
|| | |
! file_exists( AMP__DIR__ . '/includes/templates/reader-template-loader.php' ) | |
) { | |
wp_die( 'AMP plugin not active.', 'Temporarily unavailable', [ 'response' => 503 ] ); | |
} | |
if ( ! amp_is_available() || ! is_singular() ) { | |
if ( wp_safe_redirect( remove_query_arg( amp_get_slug() ) ) ) { | |
die( 'AMP unavailable. Redirecting to non-AMP version.' ); | |
} | |
return; | |
} | |
/* | |
// Uncomment to override the AMP legacy template directory to be different from using the templates in the AMP plugin. | |
add_filter( | |
'amp_post_template_dir', | |
static function () { | |
return __DIR__ . '/templates'; | |
} | |
); | |
*/ | |
/* | |
// Uncomment to override an individual AMP legacy template file to be different from using the templates in the AMP plugin. | |
add_filter( | |
'amp_post_template_file', | |
static function () { | |
// ... | |
} | |
); | |
*/ | |
require_once AMP__DIR__ . '/includes/templates/reader-template-loader.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
/* | |
Theme Name: Custom AMP Legacy | |
Version: 0.1 | |
Description: Legacy AMP templates ported over to be used as a Reader theme. | |
License: GNU General Public License v2 or later | |
License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
AMP Compatibility: theme-support | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment