Skip to content

Commit

Permalink
Modified AMP admin post type support error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ThierryA committed Dec 3, 2017
1 parent cdf9058 commit f630062
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 728 deletions.
57 changes: 28 additions & 29 deletions includes/settings/class-amp-settings-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,46 +129,45 @@ public function get_name_attribute( $post_type ) {
}

/**
* Check whether the post type should be disabled or not.
*
* Since we can't flag a post type which is not enabled by setting and removed by plugin/theme,
* we can't disable the checkbox but the errors() takes care of this scenario.
* Handle errors.
*
* @since 0.6
* @param string $post_type The post type name.
* @return bool True if disabled; false otherwise.
*/
public function disabled( $post_type ) {
$settings = $this->get_settings();
public function errors() {
$on_update = (
isset( $_GET['settings-updated'] ) // WPCS: CSRF ok.
&&
! empty( (bool) wp_unslash( $_GET['settings-updated'] ) ) // WPCS: CSRF ok.
);

// Disable if post type support was not added by setting and added by plugin/theme.
if ( post_type_supports( $post_type, AMP_QUERY_VAR ) && ! isset( $settings[ $post_type ] ) ) {
return true;
// Only apply on update.
if ( ! $on_update ) {
return;
}

return false;
}
foreach ( $this->get_supported_post_types() as $post_type ) {
if ( ! isset( $post_type->name, $post_type->label ) ) {
continue;
}

/**
* Handle errors.
*
* @since 0.6
*/
public function errors() {
$settings = $this->get_settings();
$post_type_support = post_type_supports( $post_type->name, AMP_QUERY_VAR );
$value = $this->get_settings( $post_type->name );

foreach ( $settings as $post_type => $value ) {
// Throw error if post type support was added by setting and removed by plugin/theme.
if ( true === $value && ! post_type_supports( $post_type, AMP_QUERY_VAR ) ) {
$post_type_object = get_post_type_object( $post_type );
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,
$post_type,
$post_type->name,
$post_type->name,
sprintf(
/* Translators: %s: Post type name. */
__( '"%s" could not be activated because support was removed by a plugin or theme', 'amp' ),
isset( $post_type_object->label ) ? $post_type_object->label : $post_type
$error,
$post_type->label
)
);
}
Expand Down
Loading

0 comments on commit f630062

Please sign in to comment.