-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #811 from Automattic/feature/789-cpt-admin-settings
Add admin screen for managing which post types have AMP support
- Loading branch information
Showing
16 changed files
with
866 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SYNC_README_MD=0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+PHN2ZyB3aWR0aD0iNjJweCIgaGVpZ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kzMTY2NyA0MC43MDE5NjY3LDI2LjI5MzE2NjcgQzQxLjM1MTkzMzMsMjYuMjkzMTY2NyA0MS44Nzc5LDI2LjgxOTEzMzMgNDEuODc3OSwyNy40NjkxIEM0MS44Nzc5LDI3LjczMjYgNDEuNzc0NTY2NywyNy45NjQwNjY3IDQxLjYyNzgzMzMsMjguMTYwNCBMNDEuNjI4ODY2NywyOC4xNjE0MzMzIFogTTMxLDAgQzEzLjg3ODcsMCAwLDEzLjg3OTczMzMgMCwzMSBDMCw0OC4xMjEzIDEzLjg3ODcsNjIgMzEsNjIgQzQ4LjEyMDI2NjcsNjIgNjIsNDguMTIxMyA2MiwzMSBDNjIsMTMuODc5NzMzMyA0OC4xMjAyNjY3LDAgMzEsMCBMMzEsMCBaIiBpZD0iRmlsbC0xIj48L3BhdGg+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+PC9kZWZzPiAgICA8ZyBpZD0iYW1w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MzEsNjIgQzQ4LjEyMDI2NjcsNjIgNjIsNDguMTIxMyA2MiwzMSBDNjIsMTMuODc5NzMzMyA0OC4xMjAyNjY3LDAgMzEsMCBMMzEsMCBaIiBpZD0iRmlsbC0xIj48L3BhdGg+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 | ||
} | ||
} |
Oops, something went wrong.