Skip to content

Commit

Permalink
Added classmap autoloader, removed all require_once(), deprecated amp…
Browse files Browse the repository at this point in the history
…_load_classes() and amp_load_fasterimage_classes(), created /include/templates/ directory for template-related classes, and Options Manager out of the views subdirectory.
  • Loading branch information
mikeschinkel committed Dec 8, 2017
1 parent eeb502b commit 8346524
Show file tree
Hide file tree
Showing 37 changed files with 199 additions and 145 deletions.
4 changes: 0 additions & 4 deletions .gitmodules

This file was deleted.

13 changes: 2 additions & 11 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@
define( 'AMP__DIR__', dirname( __FILE__ ) );
define( 'AMP__VERSION', '0.6.0-alpha' );

require_once AMP__DIR__ . '/autoloader.php';
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 @@ -137,7 +131,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 @@ -181,8 +175,6 @@ function amp_render_post( $post ) {
}
$post_id = $post->ID;

amp_load_classes();

do_action( 'pre_amp_render_post', $post_id );

amp_add_post_template_actions();
Expand Down Expand Up @@ -227,6 +219,5 @@ function amp_redirect_old_slug_to_new_url( $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';
}
153 changes: 153 additions & 0 deletions autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

/**
* Autoload the classes used by the AMP plugin.
*
* Class AMP_Autoloader
*/
class AMP_Autoloader {

/**
* Map of Classname to relative filepath sans extension.
*
* @example Format:
*
* array(
* 'Class_Name1' => 'subdir-of-includes/filename1',
* 'Class_Name2' => '2nd-subdir-of-includes/filename2',
* 'Class_Name3' => '!/full/path/to/filename3.php',
* );
*
* @var string[]
*/
private $_classmap;

/**
* Provide access to the singlton instance
*
* @var AMP_Autoloader
*/
private static $_instance;

/**
* Perform the autoload.
*
* Run as few lines of code as possible.
*
* If $class_name has first char as '!' strip it and assume a full path w/extension.
* Otherwise assume subdir or /includes/ w/o extension.
*
* @param string $class_name
*/
function _autoload( $class_name ) {
if ( ! isset( $this->_classmap[ $class_name ] ) ) {
return;
}
$filepath = $this->_classmap[ $class_name ];
require( '!' !== $filepath[ 0 ] ? __DIR__ . "/includes/{$filepath}.php" : substr( $filepath, 1 ) );
}

/**
* Trigger creation of AMP_Autoloader singleton instance and return it.
*
* @return AMP_Autoloader
*/
public static function instance() {
if ( ! isset( static::$_instance ) ) {
static::$_instance = new static();
}
return static::$_instance;
}

/**
* Registers to autoloader to PHP.
*
* Called at the end of this file; calling a second time has no effect.
*/
public static function register() {
static $registered = false;
if ( ! $registered ) {
spl_autoload_register( [ static::instance(), '_autoload' ] );
$registered = true;
}
}

/**
* Allows an extensions plugin to register a class and its file for autoloading
*
* @param string $class_name Full classname (include namespace if applicable)
* @param string $filepath Absolute filepath to class
*/
public static function register_autoload_class( $class_name, $filepath ) {
self::$_instance->_classmap[ $class_name ] = '!' . $filepath;
}

/**
* Creates instance and initializes classmap.
*
* AMP_Autoloader constructor.
*/
private function __construct() {
$this->_classmap = array(
'AMP_Actions' => 'actions/class-amp-actions',
'AMP_Frontend_Actions' => 'actions/class-amp-frontend-actions',
'AMP_Paired_Post_Actions' => 'actions/class-amp-paired-post-actions',
'AMP_Template_Customizer' => 'admin/class-amp-customizer',
'AMP_Post_Meta_Box' => 'admin/class-amp-post-meta-box',
'AMP_Post_Type_Support' => 'class-amp-post-type-support',
'AMP_Base_Embed_Handler' => 'embeds/class-amp-base-embed-handler',
'AMP_DailyMotion_Embed_Handler' => 'embeds/class-amp-dailymotion-embed',
'AMP_Facebook_Embed_Handler' => 'embeds/class-amp-facebook-embed',
'AMP_Gallery_Embed_Handler' => 'embeds/class-amp-gallery-embed',
'AMP_Instagram_Embed_Handler' => 'embeds/class-amp-instagram-embed',
'AMP_Pinterest_Embed_Handler' => 'embeds/class-amp-pinterest-embed',
'AMP_SoundCloud_Embed_Handler' => 'embeds/class-amp-soundcloud-embed',
'AMP_Twitter_Embed_Handler' => 'embeds/class-amp-twitter-embed',
'AMP_Vimeo_Embed_Handler' => 'embeds/class-amp-vimeo-embed',
'AMP_Vine_Embed_Handler' => 'embeds/class-amp-vine-embed',
'AMP_YouTube_Embed_Handler' => 'embeds/class-amp-youtube-embed',
'FastImage' => '/lib/fastimage/class-fastimage.php',
'WillWashburn\\Stream\\Exception\\StreamBufferTooSmallException' => 'lib/fasterimage/Stream/Exception/StreamBufferTooSmallException',
'WillWashburn\\Stream\\StreamableInterface' => 'lib/fasterimage/Stream/StreamableInterface',
'WillWashburn\\Stream\\Stream' => 'lib/fasterimage/Stream/Stream',
'FasterImage\\Exception\\InvalidImageException' => 'lib/fasterimage/Exception/InvalidImageException',
'FasterImage\\ExifParser' => 'lib/fasterimage/ExifParser',
'FasterImage\\ImageParser' => 'lib/fasterimage/ImageParser',
'FasterImage\\FasterImage' => 'lib/fasterimage/FasterImage',
'AMP_Analytics_Options_Submenu' => 'options/class-amp-analytics-options-submenu',
'AMP_Options_Menu' => 'options/class-amp-options-menu',
'AMP_Options_Manager' => 'options/class-amp-options-manager',
'AMP_Analytics_Options_Submenu_Page' => 'options/views/class-amp-analytics-options-submenu-page',
'AMP_Options_Menu_Page' => 'options/views/class-amp-options-menu-page',
'AMP_Allowed_Tags_Generated' => 'sanitizers/class-amp-allowed-tags-generated',
'AMP_Audio_Sanitizer' => 'sanitizers/class-amp-audio-sanitizer',
'AMP_Base_Sanitizer' => 'sanitizers/class-amp-base-sanitizer',
'AMP_Blacklist_Sanitizer' => 'sanitizers/class-amp-blacklist-sanitizer',
'AMP_Iframe_Sanitizer' => 'sanitizers/class-amp-iframe-sanitizer',
'AMP_Img_Sanitizer' => 'sanitizers/class-amp-img-sanitizer',
'AMP_Playbuzz_Sanitizer' => 'sanitizers/class-amp-playbuzz-sanitizer',
'AMP_Style_Sanitizer' => 'sanitizers/class-amp-style-sanitizer',
'AMP_Tag_And_Attribute_Sanitizer' => 'sanitizers/class-amp-tag-and-attribute-sanitizer',
'AMP_Video_Sanitizer' => 'sanitizers/class-amp-video-sanitizer',
'AMP_Customizer_Design_Settings' => 'settings/class-amp-customizer-design-settings',
'AMP_Customizer_Settings' => 'settings/class-amp-customizer-settings',
'AMP_Content' => 'templates/class-amp-content',
'AMP_Content_Sanitizer' => 'templates/class-amp-content-sanitizer',
'AMP_Post_Template' => 'templates/class-amp-post-template',
'AMP_DOM_Utils' => 'utils/class-amp-dom-utils',
'AMP_HTML_Utils' => 'utils/class-amp-html-utils',
'AMP_Image_Dimension_Extractor' => 'utils/class-amp-image-dimension-extractor',
'AMP_String_Utils' => 'utils/class-amp-string-utils',
'AMP_WP_Utils' => 'utils/class-amp-wp-utils',
'WPCOM_AMP_Polldaddy_Embed' => 'wpcom/class-amp-polldaddy-embed.php',
);
}
}

/**
* Call method to register this autoloader with PHP.
*/
AMP_Autoloader::register();



1 change: 0 additions & 1 deletion includes/actions/class-amp-frontend-actions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?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 {

Expand Down
2 changes: 0 additions & 2 deletions includes/actions/class-amp-paired-post-actions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

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

class AMP_Paired_Post_Actions extends AMP_Actions {

public static function register_hooks() {
Expand Down
13 changes: 9 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' );

add_action( 'admin_init', 'AMP_Options_Manager::register_settings' );
Expand Down Expand Up @@ -33,6 +30,9 @@ function amp_maybe_init_customizer() {
add_action( 'admin_menu', 'amp_add_customizer_link' );
}

/**
* @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 @@ -42,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 Expand Up @@ -106,6 +106,11 @@ function amp_add_options_menu() {
}
add_action( 'wp_loaded', 'amp_add_options_menu' );

/**
* @param array $analytics
*
* @return array
*/
function amp_add_custom_analytics( $analytics ) {
$analytics_entries = AMP_Options_Manager::get_option( 'analytics', array() );

Expand Down
1 change: 0 additions & 1 deletion includes/embeds/class-amp-base-embed-handler.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

// Used by some children
require_once( AMP__DIR__ . '/includes/utils/class-amp-html-utils.php' );

abstract class AMP_Base_Embed_Handler {
protected $DEFAULT_WIDTH = 600;
Expand Down
1 change: 0 additions & 1 deletion includes/embeds/class-amp-dailymotion-embed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );

// Much of this class is borrowed from Jetpack embeds
class AMP_DailyMotion_Embed_Handler extends AMP_Base_Embed_Handler {
Expand Down
1 change: 0 additions & 1 deletion includes/embeds/class-amp-facebook-embed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );

class AMP_Facebook_Embed_Handler extends AMP_Base_Embed_Handler {
const URL_PATTERN = '#https?://(www\.)?facebook\.com/.*#i';
Expand Down
1 change: 0 additions & 1 deletion includes/embeds/class-amp-gallery-embed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );

class AMP_Gallery_Embed_Handler extends AMP_Base_Embed_Handler {
private static $script_slug = 'amp-carousel';
Expand Down
1 change: 0 additions & 1 deletion includes/embeds/class-amp-instagram-embed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );

// Much of this class is borrowed from Jetpack embeds
class AMP_Instagram_Embed_Handler extends AMP_Base_Embed_Handler {
Expand Down
1 change: 0 additions & 1 deletion includes/embeds/class-amp-pinterest-embed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php');

class AMP_Pinterest_Embed_Handler extends AMP_Base_Embed_Handler {

Expand Down
1 change: 0 additions & 1 deletion includes/embeds/class-amp-soundcloud-embed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );

class AMP_SoundCloud_Embed_Handler extends AMP_Base_Embed_Handler {
const URL_PATTERN = '#https?://api\.soundcloud\.com/tracks/.*#i';
Expand Down
1 change: 0 additions & 1 deletion includes/embeds/class-amp-twitter-embed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );

// Much of this class is borrowed from Jetpack embeds
class AMP_Twitter_Embed_Handler extends AMP_Base_Embed_Handler {
Expand Down
1 change: 0 additions & 1 deletion includes/embeds/class-amp-vimeo-embed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );

// Much of this class is borrowed from Jetpack embeds
class AMP_Vimeo_Embed_Handler extends AMP_Base_Embed_Handler {
Expand Down
1 change: 0 additions & 1 deletion includes/embeds/class-amp-vine-embed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );

class AMP_Vine_Embed_Handler extends AMP_Base_Embed_Handler {
const URL_PATTERN = '#https?://vine\.co/v/([^/?]+)#i';
Expand Down
1 change: 0 additions & 1 deletion includes/embeds/class-amp-youtube-embed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once( AMP__DIR__ . '/includes/embeds/class-amp-base-embed-handler.php' );

// Much of this class is borrowed from Jetpack embeds
class AMP_YouTube_Embed_Handler extends AMP_Base_Embed_Handler {
Expand Down
18 changes: 1 addition & 17 deletions includes/lib/fasterimage/amp-fasterimage.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
<?php

function amp_load_fasterimage_classes() {
// We're not using composer to pull in FasterImage so we need to load the files manually
$fasterimage__DIR__ = dirname( __FILE__ );

// Stream files
require_once( $fasterimage__DIR__ . '/Stream/Exception/StreamBufferTooSmallException.php' );
require_once( $fasterimage__DIR__ . '/Stream/StreamableInterface.php' );
require_once( $fasterimage__DIR__ . '/Stream/Stream.php' );

// FasterImage files
require_once( $fasterimage__DIR__ . '/Exception/InvalidImageException.php' );
require_once( $fasterimage__DIR__ . '/ExifParser.php' );
require_once( $fasterimage__DIR__ . '/ImageParser.php' );
require_once( $fasterimage__DIR__ . '/FasterImage.php' );
_deprecated_function( __FUNCTION__, '0.6.0' );
}

function amp_get_fasterimage_client( $user_agent ) {
if ( ! class_exists( 'FasterImage\FasterImage' ) ) {
amp_load_fasterimage_classes();
}

return new FasterImage\FasterImage( $user_agent );
}
2 changes: 0 additions & 2 deletions includes/options/class-amp-analytics-options-submenu.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

require_once( AMP__DIR__ . '/includes/options/views/class-amp-analytics-options-submenu-page.php' );
require_once( AMP__DIR__ . '/includes/utils/class-amp-html-utils.php' );

class AMP_Analytics_Options_Submenu {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?php
/**
* AMP_Options_Manager class.
* class AMP_Options_Manager.
*
* @todo This file is located in the wrong directory.
* @package AMP
*/

require_once AMP__DIR__ . '/includes/utils/class-amp-html-utils.php';

/**
* Class AMP_Options_Manager
*/
class AMP_Options_Manager {

/**
Expand Down
Loading

0 comments on commit 8346524

Please sign in to comment.