Skip to content

Instantly share code, notes, and snippets.

@marushu
Last active December 19, 2015 07:09
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 marushu/3cf7d16428a8b59a7e68 to your computer and use it in GitHub Desktop.
Save marushu/3cf7d16428a8b59a7e68 to your computer and use it in GitHub Desktop.
Override custom post type widgets plugin class
<?php
/**
* Class WP_Widget_Recent_Posts_Be_will_Test
* override custom-post-type-widget at widget-custom-post-type-recent-posts.php
* https://wordpress.org/plugins/custom-post-type-widgets/
*/
class WP_Widget_Recent_Posts_Be_will extends WP_Widget {
public function __construct() {
$widget_ops = array( 'classname' => 'widget_recent_entries', 'description' => 'カスタム投稿タイプWidgetの最近の投稿' );
parent::__construct( 'custom-post-type-recent-posts', __( 'Recent Posts (Custom Post Type)', 'custom-post-type-widgets' ), $widget_ops );
$this->alt_option_name = 'widget_custom_post_type_recent_posts';
add_action( 'save_post', array( &$this, 'flush_widget_cache' ) );
add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) );
add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) );
}
public function widget( $args, $instance ) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'widget_custom_post_type_recent_posts', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent Posts', 'custom-post-type-widgets' ) : $instance['title'], $instance, $this->id_base );
$posttype = $instance['posttype'];
if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) ) {
$number = 5;
}
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
$post_types = get_post_types( array( 'public' => true ), 'objects' );
if ( array_key_exists( $posttype, (array) $post_types ) ) {
$r = new WP_Query( array(
'post_type' => $posttype,
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
) );
if ( $r->have_posts() ) : ?>
<?php echo $args['before_widget']; ?>
<?php if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
} ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
<a href="<?php the_permalink() ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
wp_reset_postdata();
endif;
}
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'widget_custom_post_type_recent_posts', $cache, 'widget' );
}
else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['posttype'] = strip_tags( $new_instance['posttype'] );
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset( $alloptions['widget_custom_post_type_recent_posts'] ) ) {
delete_option( 'widget_custom_post_type_recent_posts' );
}
return $instance;
}
public function flush_widget_cache() {
wp_cache_delete( 'widget_custom_post_type_recent_posts', 'widget' );
}
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$posttype = isset( $instance['posttype'] ) ? $instance['posttype']: 'post';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'custom-post-type-widgets' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'posttype' ); ?>"><?php _e( 'Post Type:', 'custom-post-type-widgets' ); ?></label>
<select name="<?php echo $this->get_field_name( 'posttype' ); ?>" id="<?php echo $this->get_field_id( 'posttype' ); ?>">
<?php
$post_types = get_post_types( array( 'public' => true ), 'objects' );
foreach ( $post_types as $post_type => $value ) {
if ( 'attachment' == $post_type ) {
continue;
}
?>
<option value="<?php echo esc_attr( $post_type ); ?>"<?php selected( $post_type, $posttype ); ?>><?php _e( $value->label, 'custom-post-type-widgets' ); ?></option>
<?php } ?>
</select>
</p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:', 'custom-post-type-widgets' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?', 'custom-post-type-widgets' ); ?></label></p>
<?php
}
}
class Custom_Post_Type_Widgets_Be_Will {
public function __construct() {
add_action( 'widgets_init', array( $this, 'init' ), 10 );
}
public function init() {
if ( ! is_blog_installed() ) {
return;
}
register_widget( 'WP_Widget_Recent_Posts_Be_will' );
}
public function uninstall() {}
}
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if( is_plugin_active( 'custom-post-type-widgets/custom-post-type-widgets.php' ) ) {
//include_once ( WP_PLUGIN_DIR . '/custom-post-type-widgets/inc/widget-custom-post-type-recent-posts.php' );
//if( class_exists( 'WP_Custom_Post_Type_Widgets_Recent_Posts' ) )
$custom_post_type_widgets = new Custom_Post_Type_Widgets_Be_Will();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment