Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/0d12d09119cee62ed2596c9313116c79 to your computer and use it in GitHub Desktop.
Save westonruter/0d12d09119cee62ed2596c9313116c79 to your computer and use it in GitHub Desktop.
<?php
/**
* AMP Update Supported Post Types WP-CLI Command
*
* @package Google\AMP_Update_Supported_Post_Types_CLI_Command
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2021 Google Inc.
*
* @wordpress-plugin
* Plugin Name: AMP Update Supported Post Types WP-CLI Command
* Plugin URI: https://gist.github.com/westonruter/0d12d09119cee62ed2596c9313116c79
* Description: Update the the post types that support AMP via the WP-CLI command <code>wp amp update-supported-post-types</code>.
* Version: 0.1
* Author: Weston Ruter, Google
* Author URI: https://weston.ruter.net/
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Gist Plugin URI: https://gist.github.com/westonruter/0d12d09119cee62ed2596c9313116c79
* Requires at least: 4.9
* Requires PHP: 5.6
*/
if ( ! defined( 'WP_CLI' ) ) {
return;
}
WP_CLI::add_command(
'amp update-supported-post-types',
/**
* Update the post types that are enabled for AMP.
*
* ## OPTIONS
*
* <post_type>...
* : The post type(s) to support.
*
* ## EXAMPLES
*
* # Set supported post type to just 'post'.
* $ wp amp update-supported-post-types post
*
* # Set supported post type to just 'post' and 'page'.
* $ wp amp update-supported-post-types post page
*
* @param string[] $new_post_types
*/
static function ( $new_post_types ) {
$new_post_types = array_unique( $new_post_types );
sort( $new_post_types );
$old_post_types = AMP_Options_Manager::get_option( 'supported_post_types' );
sort( $old_post_types );
$unrecognized_post_types = array_diff( $new_post_types, get_post_types() );
if ( ! empty( $unrecognized_post_types ) ) {
WP_CLI::error( 'Unrecognized post types: ' . join( ', ', $unrecognized_post_types ) );
}
if ( $new_post_types === $old_post_types ) {
WP_CLI::log( 'Supported post types already updated to: ' . join( ', ', $new_post_types ) );
return;
}
if ( AMP_Options_Manager::update_option( 'supported_post_types', $new_post_types ) ) {
WP_CLI::success( 'Updated supported post types to: ' . join( ', ', $new_post_types ) );
} else {
WP_CLI::error( 'Failed to update supported post types to: ' . join( ', ', $new_post_types ) );
}
}
);
@westonruter
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment