Skip to content

Commit

Permalink
Merge pull request #1810 from ampproject/fix/phpcs-violations
Browse files Browse the repository at this point in the history
Update coding standards and fix all violations
  • Loading branch information
westonruter authored Jan 15, 2019
2 parents c92e129 + b4c0f22 commit 1bac4d5
Show file tree
Hide file tree
Showing 116 changed files with 4,270 additions and 2,661 deletions.
23 changes: 20 additions & 3 deletions back-compat/back-compat.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
<?php
/**
* Functions for managing legacy templates
*
* @package AMP
*/

// If you want to use the template that shipped with v0.3 and earlier, you can use this to force that.
// Note that this may not stick around forever, so use caution and `function_exists`.
/**
* Adds hooks to use legacy templates.
*
* If you want to use the template that shipped with v0.3 and earlier, you can use this to force that.
* Note that this may not stick around forever, so use caution and `function_exists`.
*/
function amp_backcompat_use_v03_templates() {
add_filter( 'amp_customizer_is_enabled', '__return_false' );
add_filter( 'amp_post_template_dir', '_amp_backcompat_use_v03_templates_callback', 0 ); // early in case there are other overrides
add_filter( 'amp_post_template_dir', '_amp_backcompat_use_v03_templates_callback', 0 ); // Early in case there are other overrides.
}

/**
* Callback for getting the legacy templates directory.
*
* @access private
*
* @param string $templates Template directory.
* @return string Legacy template directory.
*/
function _amp_backcompat_use_v03_templates_callback( $templates ) {
return AMP__DIR__ . '/back-compat/templates-v0-3';
}
10 changes: 9 additions & 1 deletion back-compat/templates-v0-3/header-bar.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php $site_icon_url = $this->get( 'site_icon_url' ); ?>
<?php
/**
* Legacy template for the AMP title bar.
*
* @package AMP
*/

$site_icon_url = $this->get( 'site_icon_url' );
?>

<nav class="amp-wp-title-bar">
<div>
Expand Down
20 changes: 16 additions & 4 deletions back-compat/templates-v0-3/meta-author.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
<?php $post_author = $this->get( 'post_author' ); ?>
<?php
/**
* Legacy template for the AMP author byline.
*
* @package AMP
*/

$post_author = $this->get( 'post_author' );
$avatar_url = get_avatar_url(
$post_author->user_email,
array(
'size' => 24,
)
);
?>
<li class="amp-wp-byline">
<?php if ( function_exists( 'get_avatar_url' ) ) : ?>
<amp-img src="<?php echo esc_url( get_avatar_url( $post_author->user_email, array(
'size' => 24,
) ) ); ?>" width="24" height="24" layout="fixed"></amp-img>
<amp-img src="<?php echo esc_url( $avatar_url ); ?>" width="24" height="24" layout="fixed"></amp-img>
<?php endif; ?>
<span class="amp-wp-author"><?php echo esc_html( $post_author->display_name ); ?></span>
</li>
14 changes: 11 additions & 3 deletions back-compat/templates-v0-3/meta-taxonomy.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
<?php $categories = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'amp' ) ); ?>
<?php
/**
* Legacy template for the AMP post taxonomy term lists.
*
* @package AMP
*/

$categories = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'amp' ) );
?>
<?php if ( $categories ) : ?>
<li class="amp-wp-tax-category">
<span class="screen-reader-text">Categories:</span>
<?php echo $categories; ?>
<?php echo $categories; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</li>
<?php endif; ?>

<?php $tags = get_the_tag_list( '', _x( ', ', 'Used between list items, there is a space after the comma.', 'amp' ) ); ?>
<?php if ( $tags && ! is_wp_error( $tags ) ) : ?>
<li class="amp-wp-tax-tag">
<span class="screen-reader-text">Tags:</span>
<?php echo $tags; ?>
<?php echo $tags; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</li>
<?php endif; ?>
8 changes: 8 additions & 0 deletions back-compat/templates-v0-3/meta-time.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<?php
/**
* Legacy template for the AMP post date.
*
* @package AMP
*/

?>
<li class="amp-wp-posted-on">
<time datetime="<?php echo esc_attr( date( 'c', $this->get( 'post_publish_timestamp' ) ) ); ?>">
<?php
Expand Down
10 changes: 9 additions & 1 deletion back-compat/templates-v0-3/single.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<?php
/**
* Legacy template for the AMP post.
*
* @package AMP
*/

?>
<!doctype html>
<html amp <?php language_attributes(); ?>>
<head>
Expand All @@ -17,7 +25,7 @@
<ul class="amp-wp-meta">
<?php $this->load_parts( apply_filters( 'amp_post_template_meta_parts', array( 'meta-author', 'meta-time', 'meta-taxonomy' ) ) ); ?>
</ul>
<?php echo $this->get( 'post_amp_content' ); // amphtml content; no kses ?>
<?php echo $this->get( 'post_amp_content' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
<?php do_action( 'amp_post_template_footer', $this ); ?>
</body>
Expand Down
10 changes: 10 additions & 0 deletions back-compat/templates-v0-3/style.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<?php
/**
* Legacy template for the AMP stylesheet.
*
* @package AMP
*/

// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped

?>
/* Merriweather fonts */
@font-face {
font-family:'Merriweather';
Expand Down
18 changes: 10 additions & 8 deletions bin/create-comments-on-test-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ function amp_create_comments_test_post() {
$post = $q->next_post();
$post_id = $post->ID;
} else {
$post_id = wp_insert_post( array(
'post_name' => 'amp-test-comments',
'post_title' => 'AMP Test Comments',
'post_type' => 'post',
'post_status' => 'publish',
'post_content' => amp_get_test_random_content( 200 ),
) );
$post_id = wp_insert_post(
array(
'post_name' => 'amp-test-comments',
'post_title' => 'AMP Test Comments',
'post_type' => 'post',
'post_status' => 'publish',
'post_content' => amp_get_test_random_content( 200 ),
)
);

if ( ! $post_id || is_wp_error( $post_id ) ) {
throw new Exception( 'The test post could not be added, please try again.' );
Expand Down Expand Up @@ -111,7 +113,7 @@ function amp_get_test_random_content( $num_words = 55, $more = '.' ) {
// Bootstrap.
if ( defined( 'WP_CLI' ) ) {
try {
$post_id = amp_create_comments_test_post();
$post_id = amp_create_comments_test_post(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
WP_CLI::success( sprintf( 'Please take a look at: %s', amp_get_permalink( $post_id ) . '#development=1' ) );
} catch ( Exception $e ) {
WP_CLI::error( $e->getMessage() );
Expand Down
56 changes: 33 additions & 23 deletions bin/create-embed-test-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,24 @@ function amp_get_test_data_entries() {
* @return string|WP_CLI::error The media item ids separated by comma on success; error otherwise.
*/
function amp_get_media_items_ids( $type, $image_count = 3 ) {
$query = new \WP_Query( array(
'post_type' => 'attachment',
'post_mime_type' => $type,
'post_status' => 'inherit',
'posts_per_page' => $image_count,
'fields' => 'ids',
) );
$query = new \WP_Query(
array(
'post_type' => 'attachment',
'post_mime_type' => $type,
'post_status' => 'inherit',
'posts_per_page' => $image_count,
'fields' => 'ids',
)
);
if ( $query->post_count < $image_count ) {
throw new Exception( sprintf(
'Please make sure at least %1$s "%2$s" attachments are accessible and run this script again. There are currently only %3$s.',
$image_count,
$type,
$query->found_posts
) );
throw new Exception(
sprintf(
'Please make sure at least %1$s "%2$s" attachments are accessible and run this script again. There are currently only %3$s.',
$image_count,
$type,
$query->found_posts
)
);
}
return implode( ',', $query->get_posts() );
}
Expand All @@ -258,11 +262,13 @@ function amp_create_embed_test_post( $data_entries ) {
if ( $page ) {
$page_id = $page->ID;
} else {
$page_id = wp_insert_post( array(
'post_name' => 'amp-test-embeds',
'post_title' => 'AMP Test Embeds',
'post_type' => 'page',
) );
$page_id = wp_insert_post(
array(
'post_name' => 'amp-test-embeds',
'post_title' => 'AMP Test Embeds',
'post_type' => 'page',
)
);

if ( ! $page_id || is_wp_error( $page_id ) ) {
throw new Exception( 'The test page could not be added, please try again.' );
Expand All @@ -281,10 +287,14 @@ function amp_create_embed_test_post( $data_entries ) {
}
}

$update = wp_update_post( wp_slash( array(
'ID' => $page_id,
'post_content' => $content,
) ) );
$update = wp_update_post(
wp_slash(
array(
'ID' => $page_id,
'post_content' => $content,
)
)
);

if ( ! $update ) {
throw new Exception( 'The test page could not be updated, please try again.' );
Expand All @@ -295,7 +305,7 @@ function amp_create_embed_test_post( $data_entries ) {
// Bootstrap.
if ( defined( 'WP_CLI' ) ) {
try {
$post_id = amp_create_embed_test_post( amp_get_test_data_entries() );
$post_id = amp_create_embed_test_post( amp_get_test_data_entries() ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
WP_CLI::success( sprintf( 'Please take a look at: %s', amp_get_permalink( $post_id ) . '#development=1' ) );
} catch ( Exception $e ) {
WP_CLI::error( $e->getMessage() );
Expand Down
38 changes: 22 additions & 16 deletions bin/create-gutenberg-test-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ function get_test_block_permutations() {
* @return int|\WP_Error $post_id The post ID where the reusable block is stored, and 0 or WP_Error in case of failure.
*/
function create_test_reusable_block() {
return wp_insert_post( array(
'post_type' => 'wp_block',
'post_title' => 'Test Reusable Block',
'post_content' => '<!-- wp:core/video --><figure class="wp-block-video"><video src="https://videos.files.wordpress.com/DK5mLrbr/video-ca6dc0ab4a_hd.mp4" controls=""></video></figure><!-- /wp:core/video -->',
'post_status' => 'publish',
) );
return wp_insert_post(
array(
'post_type' => 'wp_block',
'post_title' => 'Test Reusable Block',
'post_content' => '<!-- wp:core/video --><figure class="wp-block-video"><video src="https://videos.files.wordpress.com/DK5mLrbr/video-ca6dc0ab4a_hd.mp4" controls=""></video></figure><!-- /wp:core/video -->',
'post_status' => 'publish',
)
);
}

/**
Expand All @@ -139,21 +141,25 @@ function create_gutenberg_test_post( $content ) {
if ( $page ) {
$page_id = $page->ID;
} else {
$page_id = wp_insert_post( array(
'post_name' => $slug,
'post_title' => $title,
'post_type' => 'page',
) );
$page_id = wp_insert_post(
array(
'post_name' => $slug,
'post_title' => $title,
'post_type' => 'page',
)
);

if ( ! $page_id || is_wp_error( $page_id ) ) {
throw new \Exception( $failure_message );
}
}

$update = wp_update_post( array(
'ID' => $page_id,
'post_content' => $content,
) );
$update = wp_update_post(
array(
'ID' => $page_id,
'post_content' => $content,
)
);

if ( ! $update ) {
throw new \Exception( $failure_message );
Expand All @@ -164,7 +170,7 @@ function create_gutenberg_test_post( $content ) {
// Bootstrap.
if ( defined( 'WP_CLI' ) ) {
try {
$post_id = create_gutenberg_test_post( get_test_block_fixtures() );
$post_id = create_gutenberg_test_post( get_test_block_fixtures() ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
\WP_CLI::success( sprintf( 'The test page is at: %s', \amp_get_permalink( $post_id ) . '#development=1' ) );
} catch ( \Exception $e ) {
\WP_CLI::error( $e->getMessage() );
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"require-dev": {
"wp-coding-standards/wpcs": "1.2.1",
"dealerdirect/phpcodesniffer-composer-installer": "0.4.4",
"wimg/php-compatibility": "8.2.0"
"phpcompatibility/php-compatibility": "9.1.1"
}
}
Loading

0 comments on commit 1bac4d5

Please sign in to comment.