Skip to content

Commit

Permalink
Re-use existing classes; update analytics to use same validation method
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Dec 5, 2017
1 parent 5b0e1cd commit 1befc79
Show file tree
Hide file tree
Showing 18 changed files with 1,109 additions and 661 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
7 changes: 4 additions & 3 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@

require_once AMP__DIR__ . '/back-compat/back-compat.php';
require_once AMP__DIR__ . '/includes/amp-helper-functions.php';
require_once AMP__DIR__ . '/includes/amp-post-types-support.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/settings/class-amp-settings.php';
require_once AMP__DIR__ . '/includes/settings/class-amp-settings-post-types.php';
require_once AMP__DIR__ . '/includes/actions/class-amp-frontend-actions.php';
require_once AMP__DIR__ . '/includes/actions/class-amp-paired-post-actions.php';

Expand All @@ -49,13 +47,16 @@ function amp_deactivate() {
flush_rewrite_rules();
}

AMP_Post_Type_Support::add_hooks();

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

do_action( 'amp_init' );
add_action( 'admin_init', 'AMP_Options_Manager::register_settings' );

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

Expand Down
4 changes: 0 additions & 4 deletions includes/admin/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ function amp_add_options_menu() {
return;
}

// Initialize settings.
AMP_Settings::get_instance()->init();
AMP_Settings_Post_Types::get_instance()->init();

$amp_options = new AMP_Options_Menu();
$amp_options->init();
}
Expand Down
2 changes: 1 addition & 1 deletion includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function amp_get_permalink( $post_id ) {
}

function post_supports_amp( $post ) {
// Because `add_rewrite_endpoint` doesn't let us target specific post_types :(.
// Because `add_rewrite_endpoint` doesn't let us target specific post_types :(
if ( ! post_type_supports( $post->post_type, AMP_QUERY_VAR ) ) {
return false;
}
Expand Down
35 changes: 0 additions & 35 deletions includes/amp-post-types-support.php

This file was deleted.

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
59 changes: 59 additions & 0 deletions includes/class-amp-post-type-support.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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 add_hooks() {
add_action( 'amp_init', array( __CLASS__, 'add_builtin_post_type_support' ) );
add_action( 'after_setup_theme', array( __CLASS__, 'add_elected_post_type_support' ), 5 );
}

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

/**
* Declare core post types support for built-in post types.
*
* @since 0.6
*/
public static function add_builtin_post_type_support() {
foreach ( self::get_builtin_supported_post_types() as $post_type ) {
add_post_type_support( $post_type, AMP_QUERY_VAR );
}
}

/**
* Declare custom post types support.
*
* 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_elected_post_type_support() {
$post_types = array_merge(
array_keys( array_filter( AMP_Options_Manager::get_option( 'supported_post_types', array() ) ) ),
self::get_builtin_supported_post_types()
);
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
172 changes: 170 additions & 2 deletions includes/options/class-amp-options-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
*/
class AMP_Options_Menu {

/**
* The AMP svg menu icon.
*
* @var string
*/
const ICON_BASE64_SVG = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PHN2ZyB3aWR0aD0iNjJweCIgaGVpZ2h0PSI2MnB4IiB2aWV3Qm94PSIwIDAgNjIgNjIiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+ICAgICAgICA8dGl0bGU+QU1QLUJyYW5kLUJsYWNrLUljb248L3RpdGxlPiAgICA8ZGVzYz5DcmVhdGVkIHdpdGggU2tldGNoLjwvZGVzYz4gICAgPGRlZnM+PC9kZWZzPiAgICA8ZyBpZD0iYW1wLWxvZ28taW50ZXJuYWwtc2l0ZSIgc3Ryb2tlPSJub25lIiBzdHJva2Utd2lkdGg9IjEiIGZpbGw9Im5vbmUiIGZpbGwtcnVsZT0iZXZlbm9kZCI+ICAgICAgICA8ZyBpZD0iQU1QLUJyYW5kLUJsYWNrLUljb24iIGZpbGw9IiMwMDAwMDAiPiAgICAgICAgICAgIDxwYXRoIGQ9Ik00MS42Mjg4NjY3LDI4Lj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() {
$submenu = new AMP_Analytics_Options_Submenu( AMP_Settings::MENU_SLUG );
$submenu->init( AMP_Settings::MENU_SLUG );

add_menu_page(
__( 'AMP Options', 'amp' ),
__( 'AMP', 'amp' ),
'manage_options',
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(
'supported_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,
'supported_post_types'
);

$submenus = array(
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();
}
}

/**
* Getter for the supported post types.
*
* @since 0.6
* @return array Supported post types list.
*/
public function get_supported_post_types() {
$core = get_post_types( array(
'name' => 'post',
), 'objects' );
$cpt = get_post_types( array(
'public' => true,
'_builtin' => false,
), 'objects' );

return array_merge( $core, $cpt );
}

/**
* Handle errors.
*
* @since 0.6
*/
public function errors() {
$on_update = (
isset( $_GET['settings-updated'] ) // WPCS: CSRF ok.
&&
true === (bool) wp_unslash( $_GET['settings-updated'] ) // WPCS: CSRF ok.
);

// Only apply on update.
if ( ! $on_update ) {
return;
}

$builtin_support = AMP_Post_Type_Support::get_builtin_supported_post_types();
$settings = AMP_Options_Manager::get_option( 'supported_post_types', array() );
foreach ( $this->get_supported_post_types() as $post_type ) {
if ( ! isset( $post_type->name, $post_type->label ) || in_array( $post_type->name, $builtin_support, true ) ) {
continue;
}

$post_type_support = post_type_supports( $post_type->name, AMP_QUERY_VAR );
$value = ! empty( $settings[ $post_type->name ] );

$error = null;
if ( true === $value && true !== $post_type_support ) {
/* translators: %s: Post type name. */
$error = __( '"%s" could not be activated because support is removed by a plugin or theme', 'amp' );
} elseif ( empty( $value ) && true === $post_type_support ) {
/* translators: %s: Post type name. */
$error = __( '"%s" could not be deactivated because support is added by a plugin or theme', 'amp' );
}

if ( isset( $error ) ) {
add_settings_error(
$post_type->name,
$post_type->name,
sprintf(
$error,
$post_type->label
)
);
}
}
}

/**
* 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();
?>
<fieldset>
<?php foreach ( $this->get_supported_post_types() as $post_type ) : ?>
<?php
$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( $id ); ?>" value="1">
<?php endif; ?>
<input
type="checkbox"
value="1"
id="<?php echo esc_attr( $id ); ?>"
name="<?php echo esc_attr( $id ); ?>"
<?php checked( true, post_type_supports( $post_type->name, AMP_QUERY_VAR ) ); ?>
<?php disabled( $is_builtin ); ?>
>
<label for="<?php echo esc_attr( $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
}

/**
* Display Settings.
*
* @since 0.6
*/
public function render_screen() {
$this->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 1befc79

Please sign in to comment.