Skip to content

Commit

Permalink
Merge pull request #828 from newclarity/develop
Browse files Browse the repository at this point in the history
Added autoloader to reduce complexity; fix phpcs issues
  • Loading branch information
westonruter committed Dec 11, 2017
2 parents cee3876 + 575ac66 commit b94b10b
Show file tree
Hide file tree
Showing 48 changed files with 2,629 additions and 905 deletions.
3 changes: 2 additions & 1 deletion .dev-lib
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
SYNC_README_MD=0
SYNC_README_MD=0
PATH_EXCLUDES_PATTERN=includes/lib/*
20 changes: 4 additions & 16 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@
define( 'AMP__DIR__', dirname( __FILE__ ) );
define( 'AMP__VERSION', '0.6.0-alpha' );

require_once AMP__DIR__ . '/includes/class-amp-autoloader.php';
AMP_Autoloader::register();

require_once AMP__DIR__ . '/back-compat/back-compat.php';
require_once AMP__DIR__ . '/includes/amp-helper-functions.php';
require_once AMP__DIR__ . '/includes/class-amp-post-type-support.php';
require_once AMP__DIR__ . '/includes/admin/functions.php';
require_once AMP__DIR__ . '/includes/admin/class-amp-customizer.php';
require_once AMP__DIR__ . '/includes/admin/class-amp-post-meta-box.php';
require_once AMP__DIR__ . '/includes/settings/class-amp-customizer-settings.php';
require_once AMP__DIR__ . '/includes/settings/class-amp-customizer-design-settings.php';
require_once AMP__DIR__ . '/includes/actions/class-amp-frontend-actions.php';
require_once AMP__DIR__ . '/includes/actions/class-amp-paired-post-actions.php';

register_activation_hook( __FILE__, 'amp_activate' );
function amp_activate() {
Expand Down Expand Up @@ -150,7 +146,7 @@ function amp_maybe_add_actions() {
}

function amp_load_classes() {
require_once( AMP__DIR__ . '/includes/class-amp-post-template.php' ); // this loads everything else
_deprecated_function( __FUNCTION__, '0.6.0' );
}

function amp_add_frontend_actions() {
Expand Down Expand Up @@ -196,8 +192,6 @@ function amp_render_post( $post ) {
}
$post_id = $post->ID;

amp_load_classes();

/**
* Fires before rendering a post in AMP.
*
Expand Down Expand Up @@ -246,9 +240,3 @@ function amp_redirect_old_slug_to_new_url( $link ) {

return $link;
}

// Unconditionally load code required when running unit tests.
if ( function_exists( 'tests_add_filter' ) ) {
amp_load_classes();
require_once dirname( __FILE__ ) . '/tests/stubs.php';
}
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "automattic/amp-wp",
"description": "WordPress plugin for adding AMP support.",
"homepage": "https://github.com/Automattic/amp-wp",
"type": "wordpress-plugin",
"license": "GPL-2.0"
}
2 changes: 1 addition & 1 deletion dev-lib
23 changes: 18 additions & 5 deletions includes/actions/class-amp-frontend-actions.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
<?php
// Callbacks for adding AMP-related things to the main theme

require_once( AMP__DIR__ . '/includes/actions/class-amp-actions.php' );
/**
* Class AMP_Frontend_Actions
*
* @package AMP
*/

/**
* Class AMP_Frontend_Actions
*
* Callbacks for adding AMP-related things to the main theme
*/
class AMP_Frontend_Actions {


/**
* Register hooks.
*/
public static function register_hooks() {
add_action( 'wp_head', 'AMP_Frontend_Actions::add_canonical' );
}


/**
* Add canonical link.
*/
public static function add_canonical() {
if ( false === apply_filters( 'add_canonical_link', true ) ) {
return;
Expand Down
96 changes: 77 additions & 19 deletions includes/actions/class-amp-paired-post-actions.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
<?php
/**
* Class AMP_Paired_Post_Actions
*
* @package AMP
*/

require_once( AMP__DIR__ . '/includes/actions/class-amp-actions.php' );

/**
* Class AMP_Paired_Post_Actions
*/
class AMP_Paired_Post_Actions extends AMP_Actions {


/**
* Register hooks.
*/
public static function register_hooks() {
add_action( 'amp_post_template_head', 'AMP_Paired_Post_Actions::add_generator_metadata' );
add_action( 'amp_post_template_head', 'AMP_Paired_Post_Actions::add_title' );
Expand All @@ -16,42 +25,75 @@ public static function register_hooks() {
add_action( 'amp_post_template_data', 'AMP_Paired_Post_Actions::add_analytics_scripts' );
add_action( 'amp_post_template_footer', 'AMP_Paired_Post_Actions::add_analytics_data' );
}


/**
* Add title.
*
* @param AMP_Post_Template $amp_template template.
*/
public static function add_title( $amp_template ) {
?>
<title><?php echo esc_html( $amp_template->get( 'document_title' ) ); ?></title>
<?php
}

/**
* Add canonical link.
*
* @param AMP_Post_Template $amp_template Template.
*/
public static function add_canonical_link( $amp_template ) {
?>
<link rel="canonical" href="<?php echo esc_url( $amp_template->get( 'canonical_url' ) ); ?>" />
<?php
}

/**
* Print scripts.
*
* @param AMP_Post_Template $amp_template Template.
*/
public static function add_scripts( $amp_template ) {
$scripts = $amp_template->get( 'amp_component_scripts', array() );
foreach ( $scripts as $element => $script ) :
$custom_type = ($element == 'amp-mustache') ? 'template' : 'element'; ?>
$custom_type = ( 'amp-mustache' === $element ) ? 'template' : 'element';
?>
<script custom-<?php echo esc_attr( $custom_type ); ?>="<?php echo esc_attr( $element ); ?>" src="<?php echo esc_url( $script ); ?>" async></script>
<?php endforeach; ?>
<script src="<?php echo esc_url( $amp_template->get( 'amp_runtime_script' ) ); ?>" async></script>
<?php
}

/**
* Print fonts.
*
* @param AMP_Post_Template $amp_template Template.
*/
public static function add_fonts( $amp_template ) {
$font_urls = $amp_template->get( 'font_urls', array() );
foreach ( $font_urls as $slug => $url ) : ?>
?>
<?php foreach ( $font_urls as $slug => $url ) : ?>
<link rel="stylesheet" href="<?php echo esc_url( $url ); ?>">
<?php endforeach;
<?php endforeach; ?>
<?php
}

/**
* Print boilerplate CSS.
*
* @param AMP_Post_Template $amp_template Template.
*/
public static function add_boilerplate_css( $amp_template ) {
?>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<?php
}

/**
* Print Schema.org metadata.
*
* @param AMP_Post_Template $amp_template Template.
*/
public static function add_schemaorg_metadata( $amp_template ) {
$metadata = $amp_template->get( 'metadata' );
if ( empty( $metadata ) ) {
Expand All @@ -62,54 +104,70 @@ public static function add_schemaorg_metadata( $amp_template ) {
<?php
}

/**
* Print styles.
*
* @param AMP_Post_Template $amp_template Template.
*/
public static function add_styles( $amp_template ) {
$styles = $amp_template->get( 'post_amp_styles' );
if ( ! empty( $styles ) ) {
echo '/* Inline styles */' . PHP_EOL;
echo '/* Inline styles */' . PHP_EOL; // WPCS: XSS OK.
foreach ( $styles as $selector => $declarations ) {
$declarations = implode( ';', $declarations ) . ';';
printf( '%1$s{%2$s}', $selector, $declarations );
printf( '%1$s{%2$s}', $selector, $declarations ); // WPCS: XSS OK.
}
}
}


/**
* Add analytics scripts.
*
* @param array $data Data.
* @return array Data.
*/
public static function add_analytics_scripts( $data ) {
if ( ! empty( $data['amp_analytics'] ) ) {
$data['amp_component_scripts']['amp-analytics'] = 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js';
}
return $data;
}

public static function add_analytics_data( $amp_template ) {

/**
* Print analytics data.
*
* @param AMP_Post_Template $amp_template Template.
*/
public static function add_analytics_data( $amp_template ) {
$analytics_entries = $amp_template->get( 'amp_analytics' );
if ( empty( $analytics_entries ) ) {
return;
}

foreach ( $analytics_entries as $id => $analytics_entry ) {
if ( ! isset( $analytics_entry['type'], $analytics_entry['attributes'], $analytics_entry['config_data'] ) ) {
/* translators: %1$s is analytics entry ID, %2$s is actual entry keys. */
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'Analytics entry for %1$s is missing one of the following keys: `type`, `attributes`, or `config_data` (array keys: %2$s)', 'amp' ), esc_html( $id ), esc_html( implode( ', ', array_keys( $analytics_entry ) ) ) ), '0.3.2' );
continue;
}

$script_element = AMP_HTML_Utils::build_tag( 'script', array(
'type' => 'application/json',
), wp_json_encode( $analytics_entry['config_data'] ) );

$amp_analytics_attr = array_merge( array(
'id' => $id,
'type' => $analytics_entry['type'],
), $analytics_entry['attributes'] );
echo AMP_HTML_Utils::build_tag( 'amp-analytics', $amp_analytics_attr, $script_element );

echo AMP_HTML_Utils::build_tag( 'amp-analytics', $amp_analytics_attr, $script_element ); // WPCS: XSS OK.
}
}

/**
* Add AMP generator metadata.
* Print AMP generator metadata.
*
* @param object $amp_template AMP_Post_Template object.
* @param AMP_Post_Template $amp_template AMP_Post_Template object.
* @since 0.6
*/
public static function add_generator_metadata( $amp_template ) {
Expand Down
10 changes: 6 additions & 4 deletions includes/admin/functions.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php
// Callbacks for adding AMP-related things to the admin.

require_once AMP__DIR__ . '/includes/options/class-amp-options-menu.php';
require_once AMP__DIR__ . '/includes/options/views/class-amp-options-manager.php';

define( 'AMP_CUSTOMIZER_QUERY_VAR', 'customize_amp' );

/**
Expand Down Expand Up @@ -31,6 +28,11 @@ function amp_maybe_init_customizer() {
add_action( 'admin_menu', 'amp_add_customizer_link' );
}

/**
* Get permalink for the first AMP-eligible post.
*
* @return string|null
*/
function amp_admin_get_preview_permalink() {
/**
* Filter the post type to retrieve the latest for use in the AMP template customizer.
Expand All @@ -40,7 +42,7 @@ function amp_admin_get_preview_permalink() {
$post_type = (string) apply_filters( 'amp_customizer_post_type', 'post' );

if ( ! post_type_supports( $post_type, 'amp' ) ) {
return;
return null;
}

$post_ids = get_posts( array(
Expand Down
18 changes: 16 additions & 2 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<?php
/**
* AMP Helper Functions
*
* @package AMP
*/

/**
* Get AMP permalink.
* Retrieves the full AMP-specific permalink for the given post ID.
*
* @since 0.1
*
* @param int $post_id Post ID.
*
* @return string AMP permalink.
*/
function amp_get_permalink( $post_id ) {
Expand All @@ -27,7 +33,7 @@ function amp_get_permalink( $post_id ) {
}

$parsed_url = wp_parse_url( get_permalink( $post_id ) );
$structure = get_option( 'permalink_structure' );
$structure = get_option( 'permalink_structure' );
if ( empty( $structure ) || ! empty( $parsed_url['query'] ) || is_post_type_hierarchical( get_post_type( $post_id ) ) ) {
$amp_url = add_query_arg( AMP_QUERY_VAR, '', get_permalink( $post_id ) );
} else {
Expand Down Expand Up @@ -64,6 +70,8 @@ function post_supports_amp( $post ) {
* Are we currently on an AMP URL?
*
* Note: will always return `false` if called before the `parse_query` hook.
*
* @return bool Whether it is the AMP endpoint.
*/
function is_amp_endpoint() {
if ( 0 === did_action( 'parse_query' ) ) {
Expand All @@ -73,6 +81,12 @@ function is_amp_endpoint() {
return false !== get_query_var( AMP_QUERY_VAR, false );
}

/**
* Get AMP asset URL.
*
* @param string $file Relative path to file in assets directory.
* @return string URL.
*/
function amp_get_asset_url( $file ) {
return plugins_url( sprintf( 'assets/%s', $file ), AMP__FILE__ );
}
Loading

0 comments on commit b94b10b

Please sign in to comment.