Skip to content

Commit

Permalink
Merge pull request #811 from Automattic/feature/789-cpt-admin-settings
Browse files Browse the repository at this point in the history
Add admin screen for managing which post types have AMP support
  • Loading branch information
westonruter authored Dec 6, 2017
2 parents 6df01e9 + 8c14063 commit c73f479
Show file tree
Hide file tree
Showing 16 changed files with 866 additions and 149 deletions.
1 change: 1 addition & 0 deletions .dev-lib
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SYNC_README_MD=0
50 changes: 35 additions & 15 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@
* Plugin URI: https://github.com/automattic/amp-wp
* Author: Automattic
* Author URI: https://automattic.com
* Version: 0.5.1
* Version: 0.6.0-alpha
* Text Domain: amp
* Domain Path: /languages/
* License: GPLv2 or later
*
* @package AMP
*/

define( 'AMP__FILE__', __FILE__ );
define( 'AMP__DIR__', dirname( __FILE__ ) );
define( 'AMP__VERSION', '0.5.1' );

require_once( AMP__DIR__ . '/back-compat/back-compat.php' );
require_once( AMP__DIR__ . '/includes/amp-helper-functions.php' );
require_once( AMP__DIR__ . '/includes/admin/functions.php' );
require_once( AMP__DIR__ . '/includes/admin/class-amp-customizer.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' );
define( 'AMP__VERSION', '0.6.0-alpha' );

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/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 All @@ -47,20 +49,19 @@ function amp_deactivate() {
flush_rewrite_rules();
}

AMP_Post_Type_Support::init();

add_action( 'init', 'amp_init' );
function amp_init() {
if ( false === apply_filters( 'amp_is_enabled', true ) ) {
return;
}

define( 'AMP_QUERY_VAR', apply_filters( 'amp_query_var', 'amp' ) );

do_action( 'amp_init' );

load_plugin_textdomain( 'amp', false, plugin_basename( AMP__DIR__ ) . '/languages' );

add_rewrite_endpoint( AMP_QUERY_VAR, EP_PERMALINK );
add_post_type_support( 'post', AMP_QUERY_VAR );

add_filter( 'request', 'amp_force_query_var_value' );
add_action( 'wp', 'amp_maybe_add_actions' );
Expand All @@ -73,6 +74,25 @@ function amp_init() {
}
}

/**
* Define AMP query var.
*
* This function must be invoked through the 'after_setup_theme' action to allow
* the AMP setting to declare the post types support earlier than plugins/theme.
*
* @since 0.6
*/
function define_query_var() {
/**
* Filter the AMP query variable.
*
* @since 0.3.2
* @param string $query_var The AMP query variable.
*/
define( 'AMP_QUERY_VAR', apply_filters( 'amp_query_var', 'amp' ) );
}
add_action( 'after_setup_theme', 'define_query_var', 3 );

// Make sure the `amp` query var has an explicit value.
// Avoids issues when filtering the deprecated `query_string` hook.
function amp_force_query_var_value( $query_vars ) {
Expand Down
19 changes: 14 additions & 5 deletions includes/admin/functions.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?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' );
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' );

/**
* Sets up the AMP template editor for the Customizer.
*/
Expand Down Expand Up @@ -80,15 +82,22 @@ function amp_add_customizer_link() {
}

/**
* Registers a top-level menu for AMP configuration options
* Registers AMP settings.
*/
function amp_add_options_menu() {
if ( ! is_admin() ) {
return;
}

$show_options_menu = apply_filters( 'amp_options_menu_is_enabled', true );
if ( true !== $show_options_menu ) {
/**
* Filter whether to enable the AMP settings.
*
* @since 0.5
* @param bool $enable Whether to enable the AMP settings. Default true.
*/
$short_circuit = apply_filters( 'amp_options_menu_is_enabled', true );

if ( true !== $short_circuit ) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions includes/class-amp-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private function sanitize( $content ) {
}
}

// @todo Split this class out into a separate file.
class AMP_Content_Sanitizer {
public static function sanitize( $content, array $sanitizer_classes, $global_args = array() ) {
$scripts = array();
Expand Down
66 changes: 66 additions & 0 deletions includes/class-amp-post-type-support.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* AMP Post type support.
*
* @package AMP
* @since 0.6
*/

/**
* Class AMP_Post_Type_Support.
*/
class AMP_Post_Type_Support {

/**
* Add hooks.
*/
public static function init() {
add_action( 'after_setup_theme', array( __CLASS__, 'add_post_type_support' ), 5 );
}

/**
* Get post types that plugin supports out of the box (which cannot be disabled).
*
* @return string[] Post types.
*/
public static function get_builtin_supported_post_types() {
return array_filter( array( 'post' ), 'post_type_exists' );
}

/**
* Get post types that are eligible for AMP support.
*
* @since 0.6
* @return string[] Post types eligible for AMP.
*/
public static function get_eligible_post_types() {
return array_merge(
self::get_builtin_supported_post_types(),
array_values( get_post_types(
array(
'public' => true,
'_builtin' => false,
),
'names'
) )
);
}

/**
* Declare support for post types.
*
* This function should only be invoked through the 'after_setup_theme' action to
* allow plugins/theme to overwrite the post types support.
*
* @since 0.6
*/
public static function add_post_type_support() {
$post_types = array_merge(
self::get_builtin_supported_post_types(),
AMP_Options_Manager::get_option( 'supported_post_types', array() )
);
foreach ( $post_types as $post_type ) {
add_post_type_support( $post_type, AMP_QUERY_VAR );
}
}
}
5 changes: 4 additions & 1 deletion includes/options/class-amp-analytics-options-submenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ private function add_submenu() {
public function amp_options_styles() {
?>
<style>
.analytics-data-container #delete {
.analytics-data-container .button.delete,
.analytics-data-container .button.delete:hover,
.analytics-data-container .button.delete:active,
.analytics-data-container .button.delete:focus {
background: red;
border-color: red;
text-shadow: 0 0 0;
Expand Down
129 changes: 106 additions & 23 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,137 @@
<?php
/**
* AMP Options.
*
* @package AMP
*/

require_once( AMP__DIR__ . '/includes/options/class-amp-analytics-options-submenu.php' );
require_once( AMP__DIR__ . '/includes/options/views/class-amp-options-menu-page.php' );
require_once( AMP__DIR__ . '/includes/options/views/class-amp-options-manager.php' );
// Includes.
require_once AMP__DIR__ . '/includes/options/class-amp-analytics-options-submenu.php';
require_once AMP__DIR__ . '/includes/options/views/class-amp-options-manager.php';

/**
* AMP_Options_Menu class.
*/
class AMP_Options_Menu {
const ICON_BASE64_SVG = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PHN2ZyB3aWR0aD0iNjJweCIgaGVpZ2h0PSI2MnB4IiB2aWV3Qm94PSIwIDAgNjIgNjIiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+ICAgICAgICA8dGl0bGU+QU1QLUJyYW5kLUJsYWNrLUljb248L3RpdGxlPiAgICA8ZGVzYz5DcmVhdGVkIHdpdGggU2tldGNoLjwvZGVzYz4gICAgPGRlZnM+PC9kZWZzPiAgICA8ZyBpZD0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sNjIgMzEsNjIgQzQ4LjEyMDI2NjcsNjIgNjIsNDguMTIxMyA2MiwzMSBDNjIsMTMuODc5NzMzMyA0OC4xMjAyNjY3LDAgMzEsMCBMMzEsMCBaIiBpZD0iRmlsbC0xIj48L3BhdGg+ICAgICAgICA8L2c+ICAgIDwvZz48L3N2Zz4=';

private $menu_page;
private $menu_slug;

public function __construct() {
$this->menu_page = new AMP_Options_Menu_Page();
$this->menu_slug = 'amp-plugin-options';
}
/**
* The AMP svg menu icon.
*
* @var string
*/
const ICON_BASE64_SVG = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PHN2ZyB3aWR0aD0iNjJweCIgaGVpZ2h0PSI2MnB4IiB2aWV3Qm94PSIwIDAgNjIgNjIiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+ICAgICAgICA8dGl0bGU+QU1QLUJyYW5kLUJsYWNrLUljb248L3RpdGxlPiAgICA8ZGVzYz5DcmVhdGVkIHdpdGggU2tldGNoLjwvZGVzYz4gICAgPGRlZnM+PC9kZWZzPiAgICA8ZyBpZD0iYW1wLWxvZ28taW50ZXJuYWwtc2l0ZSIgc3Ryb2tlPSJub25lIiBzdHJva2Utd2lkdGg9IjEiIGZpbGw9Im5vbmUiIGZpbGwtcnVsZT0iZXZlbm9kZCI+ICAgICAgICA8ZyBpZD0iQU1QLUJyYW5kLUJsYWNrLUljb24iIGZpbGw9IiMwMDAwMDAiPiAgICAgICAgICAgIDxwYXRoIGQ9Ik00MS42Mjg4NjY3LDI4LjE2MTQzMzMgTDI4LjYyNDM2NjcsNDkuODAzNTY2NyBMMjYuMjY4MzY2Nyw0OS44MDM1NjY3IEwyOC41OTc1LDM1LjcwMTY2NjcgTDIxLjM4MzgsMzUuNzEwOTY2NyBDMjEuMzgzOCwzNS43MTA5NjY3IDIxLjMxNTYsMzUuNzEzMDMzMyAyMS4yODM1NjY3LDM1LjcxMzAzMzMgQzIwLjYzMzYsMzUuNzEzMDMzMyAyMC4xMDc2MzMzLDM1LjE4NzA2NjcgMjAuMTA3NjMzMywzNC41MzcxIEMyMC4xMDc2MzMzLDM0LjI1ODEgMjAuMzY3LDMzLjc4NTg2NjcgMjAuMzY3LDMzLjc4NTg2NjcgTDMzLjMyOTEzMzMsMTIuMTY5NTY2NyBMMzUuNzI0NCwxMi4xNzk5IEwzMy4zMzYzNjY3LDI2LjMwMzUgTDQwLjU4NzI2NjcsMjYuMjk0MiBDNDAuNTg3MjY2NywyNi4yOTQyIDQwLjY2NDc2NjcsMjYuMjkzMTY2NyA0MC43MDE5NjY3LDI2LjI5MzE2NjcgQzQxLjM1MTkzMzMsMjYuMjkzMTY2NyA0MS44Nzc5LDI2LjgxOTEzMzMgNDEuODc3OSwyNy40NjkxIEM0MS44Nzc5LDI3LjczMjYgNDEuNzc0NTY2NywyNy45NjQwNjY3IDQxLjYyNzgzMzMsMjguMTYwNCBMNDEuNjI4ODY2NywyOC4xNjE0MzMzIFogTTMxLDAgQzEzLjg3ODcsMCAwLDEzLjg3OTczMzMgMCwzMSBDMCw0OC4xMjEzIDEzLjg3ODcsNjIgMzEsNjIgQzQ4LjEyMDI2NjcsNjIgNjIsNDguMTIxMyA2MiwzMSBDNjIsMTMuODc5NzMzMyA0OC4xMjAyNjY3LDAgMzEsMCBMMzEsMCBaIiBpZD0iRmlsbC0xIj48L3BhdGg+ICAgICAgICA8L2c+ICAgIDwvZz48L3N2Zz4=';

/**
* Initialize.
*/
public function init() {
add_action( 'admin_post_amp_analytics_options', 'AMP_Options_Manager::handle_analytics_submit' );

add_action( 'admin_menu', array( $this, 'add_menu_items' ) );
}

/**
* Add menu.
*/
public function add_menu_items() {

add_menu_page(
__( 'AMP Options', 'amp' ),
__( 'AMP', 'amp' ),
'manage_options',
$this->menu_slug,
array( $this->menu_page, 'render' ),
AMP_Options_Manager::OPTION_NAME,
array( $this, 'render_screen' ),
self::ICON_BASE64_SVG
);

add_submenu_page(
AMP_Options_Manager::OPTION_NAME,
__( 'AMP Settings', 'amp' ),
__( 'General', 'amp' ),
'manage_options',
AMP_Options_Manager::OPTION_NAME
);

add_settings_section(
'post_types',
false,
'__return_false',
AMP_Options_Manager::OPTION_NAME
);
add_settings_field(
'supported_post_types',
__( 'Post Type Support', 'amp' ),
array( $this, 'render_post_types_support' ),
AMP_Options_Manager::OPTION_NAME,
'post_types'
);

$submenus = array(
new AMP_Analytics_Options_Submenu( $this->menu_slug ),
new AMP_Analytics_Options_Submenu( AMP_Options_Manager::OPTION_NAME ),
);

// Create submenu items and calls on the Submenu Page object to render the actual contents of the page.
foreach ( $submenus as $submenu ) {
$submenu->init( $this->menu_slug );
$submenu->init();
}
}

$this->remove_toplevel_menu_item();
/**
* Post types support section renderer.
*
* @since 0.6
*/
public function render_post_types_support() {
$builtin_support = AMP_Post_Type_Support::get_builtin_supported_post_types();
$element_name = AMP_Options_Manager::OPTION_NAME . '[supported_post_types][]';
?>
<fieldset>
<?php foreach ( array_map( 'get_post_type_object', AMP_Post_Type_Support::get_eligible_post_types() ) as $post_type ) : ?>
<?php
$element_id = AMP_Options_Manager::OPTION_NAME . "-supported_post_types-{$post_type->name}";
$is_builtin = in_array( $post_type->name, $builtin_support, true );
?>
<?php if ( $is_builtin ) : ?>
<input type="hidden" name="<?php echo esc_attr( $element_name ); ?>" value="<?php echo esc_attr( $post_type->name ); ?>">
<?php endif; ?>
<input
type="checkbox"
id="<?php echo esc_attr( $element_id ); ?>"
name="<?php echo esc_attr( $element_name ); ?>"
value="<?php echo esc_attr( $post_type->name ); ?>"
<?php checked( true, post_type_supports( $post_type->name, AMP_QUERY_VAR ) ); ?>
<?php disabled( $is_builtin ); ?>
>
<label for="<?php echo esc_attr( $element_id ); ?>">
<?php echo esc_html( $post_type->label ); ?>
</label>
<br>
<?php endforeach; ?>
<p class="description"><?php esc_html_e( 'Enable/disable AMP post type(s) support', 'amp' ); ?></p>
</fieldset>
<?php
}

// Helper function to avoid having the top-level menu as
// the first menu item
function remove_toplevel_menu_item() {
global $submenu;
if ( isset( $submenu['amp-plugin-options'][0] ) ) {
unset( $submenu['amp-plugin-options'][0] );
/**
* Display Settings.
*
* @since 0.6
*/
public function render_screen() {
if ( ! empty( $_GET['settings-updated'] ) ) { // WPCS: CSRF ok.
AMP_Options_Manager::check_supported_post_type_update_errors();
}
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<?php settings_errors(); ?>
<form action="options.php" method="post">
<?php
settings_fields( AMP_Options_Manager::OPTION_NAME );
do_settings_sections( AMP_Options_Manager::OPTION_NAME );
submit_button();
?>
</form>
</div>
<?php
}
}
Loading

0 comments on commit c73f479

Please sign in to comment.