Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active March 19, 2018 20:33
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/e660643f9b8f0e7b5d8a272824525eb2 to your computer and use it in GitHub Desktop.
Save westonruter/e660643f9b8f0e7b5d8a272824525eb2 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: AMP Invalid
* Description: Do stuff that is invalid in AMP.
*/
namespace AMP_Invalid;
add_action( 'bad_amp_footer', function() {
?>
<div class="1">
<script type="text/javascript">console.info('evil!');</script>
</div>
<?php
} );
// @todo This needs to be checked.
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_style( 'plugin-css', plugin_dir_url( __FILE__ ) . 'plugin.css' );
wp_enqueue_script( 'plugin-js', plugin_dir_url( __FILE__ ) . 'plugin.js', array( ) );
wp_enqueue_style( 'mu-plugin-css', plugins_url( '', WPMU_PLUGIN_DIR . '/' ) . 'mooo/mu-plugin.css' );
wp_enqueue_script( 'mu-plugin-js', plugins_url( '', WPMU_PLUGIN_DIR . '/' ) . 'mooo/mu-plugin.js' );
wp_enqueue_style( 'theme-css', get_template_directory_uri() . '/theme.css' );
wp_enqueue_script( 'theme-js', get_template_directory_uri() . '/theme.js' );
wp_enqueue_script( 'jquery' );
} );
add_shortcode( 'bad_amp', function() {
return '<blockquote>Check out this <script>document.write("bad script");</script></blockquote>';
} );
add_action( 'wp_head', function() {
?>
<link rel="stylesheet" href="http://example.org/bad.css">
<style>
body { color: blue !important; }
</style>
<?php
} );
function footer_stuff() {
?>
<!-- before amp-keyframes -->
<style amp-keyframes>
@keyframes anim1 {}
@media (min-width: 600px) {
@keyframes anim1 {}
}
</style>
<?php
do_action( 'bad_amp_footer' );
}
add_action( 'wp_footer', __NAMESPACE__ . '\footer_stuff' );
add_action( 'widgets_init', function() {
register_widget( __NAMESPACE__ . '\AMP_Invalid_Widget' );
} );
/**
* Adds AMP_Invalid_Widget widget.
*/
class AMP_Invalid_Widget extends \WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'amp_invalid', // Base ID
esc_html__( 'AMP Invalid', 'amp-invalid' ), // Name
array( 'description' => esc_html__( 'A Foo Widget', 'amp-invalid' ), ) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
echo esc_html__( 'Hello, World!', 'amp-invalid' );
echo "<script>document.write('hello')</script>";
echo "<button onmouseenter=evil>clock me</button>";
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'amp-invalid' );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'amp-invalid' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // class AMP_Invalid_Widget
body {
color: green !important;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment