Skip to content

Commit

Permalink
Merge branch '0.7' of https://github.com/Automattic/amp-wp into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Apr 30, 2018
2 parents 1e2cd22 + 8426594 commit cd03ee4
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 181 deletions.
72 changes: 49 additions & 23 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ class AMP_Theme_Support {
public static $init_start_time;

/**
* Output buffering level when starting.
* Whether output buffering has started.
*
* @since 0.7
* @var int
* @var bool
*/
protected static $initial_ob_level = 0;
protected static $is_output_buffering = false;

/**
* Initialize.
Expand Down Expand Up @@ -264,7 +264,13 @@ public static function add_hooks() {
* Start output buffering at very low priority for sake of plugins and themes that use template_redirect
* instead of template_include.
*/
add_action( 'template_redirect', array( __CLASS__, 'start_output_buffering' ), 0 );
$priority = defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX; // phpcs:ignore PHPCompatibility.PHP.NewConstants.php_int_minFound
add_action( 'template_redirect', array( __CLASS__, 'start_output_buffering' ), $priority );

// Add validation hooks *after* output buffering has started for the response.
if ( AMP_Validation_Utils::should_validate_response() ) {
AMP_Validation_Utils::add_validation_hooks();
}

// Commenting hooks.
add_filter( 'wp_list_comments_args', array( __CLASS__, 'set_comments_walker' ), PHP_INT_MAX );
Expand All @@ -275,10 +281,6 @@ public static function add_hooks() {
remove_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' );
add_filter( 'wp_kses_allowed_html', array( __CLASS__, 'whitelist_layout_in_wp_kses_allowed_html' ), 10 );

if ( AMP_Validation_Utils::should_validate_response() ) {
AMP_Validation_Utils::add_validation_hooks();
}

// @todo Add character conversion.
}

Expand Down Expand Up @@ -940,28 +942,37 @@ public static function start_output_buffering() {
newrelic_disable_autorum();
}

ob_start();
self::$initial_ob_level = ob_get_level();
ob_start( array( __CLASS__, 'finish_output_buffering' ) );
self::$is_output_buffering = true;
}

// Note that the following must be at 0 because wp_ob_end_flush_all() runs at shutdown:1.
add_action( 'shutdown', array( __CLASS__, 'finish_output_buffering' ), 0 );
/**
* Determine whether output buffering has started.
*
* @since 0.7
* @see AMP_Theme_Support::start_output_buffering()
* @see AMP_Theme_Support::finish_output_buffering()
*
* @return bool Whether output buffering has started.
*/
public static function is_output_buffering() {
return self::$is_output_buffering;
}

/**
* Finish output buffering.
*
* @since 0.7
* @see AMP_Theme_Support::start_output_buffering()
*
* @param string $response Buffered Response.
* @return string Processed Response.
*/
public static function finish_output_buffering() {
public static function finish_output_buffering( $response ) {
AMP_Response_Headers::send_server_timing( 'amp_output_buffer', -self::$init_start_time, 'AMP Output Buffer' );

// Flush output buffer stack until we get to the output buffer we started.
while ( ob_get_level() > self::$initial_ob_level ) {
ob_end_flush();
}

echo self::prepare_response( ob_get_clean() ); // WPCS: xss ok.
self::$is_output_buffering = false;
return self::prepare_response( $response );
}

/**
Expand Down Expand Up @@ -1098,10 +1109,25 @@ public static function prepare_response( $response, $args = array() ) {
}
}

// Print all scripts, some of which may have already been printed and inject into head.
ob_start();
wp_print_scripts( array_keys( $amp_scripts ) );
$script_tags = ob_get_clean();
/*
* Inject additional AMP component scripts which have been discovered by the sanitizers into the head.
* This is adapted from wp_scripts()->do_items(), but it runs only the bare minimum required to output
* the missing scripts, without allowing other filters to apply which may cause an invalid AMP response.
*/
$script_tags = '';
foreach ( array_diff( array_keys( $amp_scripts ), wp_scripts()->done ) as $handle ) {
if ( ! wp_script_is( $handle, 'registered' ) ) {
continue;
}
$script_dep = wp_scripts()->registered[ $handle ];
$script_tags .= amp_filter_script_loader_tag(
sprintf(
"<script type='text/javascript' src='%s'></script>\n", // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
esc_url( $script_dep->src )
),
$handle
);
}
if ( ! empty( $script_tags ) ) {
$response = preg_replace(
'#(?=</head>)#',
Expand Down
20 changes: 19 additions & 1 deletion includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,25 @@ public static function register_settings() {
)
);

add_action( 'update_option_' . self::OPTION_NAME, 'flush_rewrite_rules' );
add_action( 'update_option_' . self::OPTION_NAME, array( __CLASS__, 'maybe_flush_rewrite_rules' ), 10, 2 );
}

/**
* Flush rewrite rules if the supported_post_types have changed.
*
* @since 0.6.2
*
* @param array $old_options Old options.
* @param array $new_options New options.
*/
public static function maybe_flush_rewrite_rules( $old_options, $new_options ) {
$old_post_types = isset( $old_options['supported_post_types'] ) ? $old_options['supported_post_types'] : array();
$new_post_types = isset( $new_options['supported_post_types'] ) ? $new_options['supported_post_types'] : array();
sort( $old_post_types );
sort( $new_post_types );
if ( $old_post_types !== $new_post_types ) {
flush_rewrite_rules( false );
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@
*/
class AMP_Analytics_Options_Submenu_Page {

/**
* Render entry.
*
* @param string $id Entry ID.
* @param string $type Entry type.
* @param string $config Entry config as serialized JSON.
*/
private function render_entry( $id = '', $type = '', $config = '' ) {
$is_existing_entry = ! empty( $id );

if ( $is_existing_entry ) {
$entry_slug = sprintf( '%s%s', ( $type ? $type . '-' : '' ), substr( $id, -6 ) );
$entry_slug = sprintf( '%s%s', ( $type ? $type . '-' : '' ), substr( $id, - 6 ) );
/* translators: %s is the entry slug */
$analytics_title = sprintf( __( 'Analytics: %s', 'amp' ), $entry_slug );
} else {
$analytics_title = __( 'Add new entry:', 'amp' );
}

if ( ! $is_existing_entry ) {
$id = '__new__';
$id = '__new__';
}

$id_base = sprintf( '%s[analytics][%s]', AMP_Options_Manager::OPTION_NAME, $id );
Expand Down Expand Up @@ -55,9 +60,9 @@ private function render_entry( $id = '', $type = '', $config = '' ) {
<p>
<?php
wp_nonce_field( 'analytics-options', 'analytics-options' );
submit_button( __( 'Save', 'amp' ), 'primary', 'save', false );
submit_button( esc_html__( 'Save', 'amp' ), 'primary', 'save', false );
if ( $is_existing_entry ) {
submit_button( __( 'Delete', 'amp' ), 'delete button-primary', $id_base . '[delete]', false );
submit_button( esc_html__( 'Delete', 'amp' ), 'delete button-primary', esc_attr( $id_base . '[delete]' ), false );
}
?>
</p>
Expand All @@ -78,6 +83,9 @@ public function render_title() {
<?php
}

/**
* Render.
*/
public function render() {
$analytics_entries = AMP_Options_Manager::get_option( 'analytics', array() );

Expand Down
8 changes: 7 additions & 1 deletion includes/templates/class-amp-post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ public function __construct( $post ) {
} else {
$this->post = get_post( $post );
}
$this->ID = $this->post->ID;

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

$content_max_width = self::CONTENT_MAX_WIDTH;
if ( isset( $GLOBALS['content_width'] ) && $GLOBALS['content_width'] > 0 ) {
Expand Down
Loading

0 comments on commit cd03ee4

Please sign in to comment.