Skip to content

Instantly share code, notes, and snippets.

@nacin
Created August 14, 2011 21:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nacin/1145324 to your computer and use it in GitHub Desktop.
Save nacin/1145324 to your computer and use it in GitHub Desktop.
P2 Resolved Posts
<?php
/* Plugin Name: P2 Resolved Posts
* Description: Allows you to mark P2 posts for resolution.
* Author: Andrew Nacin
* Author URI: http://andrewnacin.com/
*/
/**
* @package P2_Resolved_Posts
*/
class P2_Resolved_Posts {
static $instance;
/**
* Constructor. Saves instance and sets up initial hook.
*/
function __construct() {
self::$instance = $this;
add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
register_activation_hook( __FILE__, array( $this, 'activate' ) );
}
/**
* Sets up initial hooks, if the P2 theme is in play.
*/
function after_setup_theme() {
if ( ! class_exists( 'P2' ) )
return;
add_action( 'wp_head', array( $this, 'wp_head' ) );
add_action( 'wp_footer', array( $this, 'wp_footer' ) );
add_action( 'p2_action_links', array( $this, 'p2_action_links' ) );
add_filter( 'post_class', array( $this, 'post_class' ), 10, 3 );
add_action( 'admin_post_p2_resolve', array( $this, 'admin_post_p2_resolve' ) );
add_action( 'widgets_init', array( $this, 'widgets_init' ) );
add_filter( 'request', array( $this, 'request' ) );
$this->register_taxonomy();
}
function register_taxonomy() {
register_taxonomy( 'p2_resolved', 'post', array(
'public' => true,
'query_var' => 'resolved',
'rewrite' => false,
'show_ui' => false,
) );
}
function activate() {
$this->register_taxonomy();
if ( ! term_exists( 'unresolved', 'p2_resolved' ) )
wp_insert_term( 'unresolved', 'p2_resolved' );
}
function request( $qvs ) {
if ( ! isset( $qvs['resolved'] ) )
return $qvs;
if ( ! in_array( $qvs['resolved'], array( 'resolved', 'unresolved' ) ) ) {
unset( $qvs['resolved'] );
return $qvs;
}
add_action( 'parse_query', array( $this, 'parse_query' ) );
add_filter( 'template_include', array( $this, 'force_home_template' ) );
if ( 'resolved' == $qvs['resolved'] )
return $qvs;
unset( $qvs['resolved'] );
if ( ! isset( $qvs['tax_query'] ) )
$qvs['tax_query'] = array();
$qvs['tax_query'][] = array(
'taxonomy' => 'p2_resolved',
'terms' => array( 'resolved' ),
'field' => 'slug',
'operator' => 'NOT IN',
);
return $qvs;
}
function parse_query( $query ) {
$query->is_home = true; // Force home detection.
}
function force_home_template() {
return get_home_template();
}
function widgets_init() {
register_widget( 'P2_Resolved_Posts_Widget' );
}
/**
* Adds 'Private Post' near the action links.
*/
function p2_action_links() {
if ( ! current_user_can( 'manage_options' ) )
return;
$go = add_query_arg( array(
'action' => 'p2_resolve',
'post' => get_the_ID(),
'_wp_http_referer' => urlencode( stripslashes( $_SERVER['REQUEST_URI'] ) )
), admin_url( 'admin-post.php' ) );
$go = wp_nonce_url( $go, 'p2_resolve-' . get_the_ID() );
if ( has_term( 'resolved', 'p2_resolved', get_the_ID() ) ) {
$go = add_query_arg( 'unresolve', '1', $go );
echo ' | <strong class="p2-resolve"><span class="label">' . __( 'Resolved', 'p2-resolved-posts' ) . '</span> <a class="unresolve" href="' . esc_attr( $go ) . '">' . __( 'Undo', 'p2-resolved-posts' ) . '</a></strong>';
} else {
echo ' | <strong class="p2-resolve"><span class="label"></span> <a class="resolve" href="' . esc_attr( $go ) . '">' . __( 'Mark as Resolved', 'p2-resolved-posts' ) . '</a></strong>';
}
}
function post_class( $classes, $class, $post_id ) {
if ( ! is_single() && has_term( 'resolved', 'p2_resolved', $post_id ) )
$classes[] = 'status-resolved';
return $classes;
}
function admin_post_p2_resolve() {
$post_id = absint( $_GET['post'] );
if ( ! current_user_can( 'manage_options' ) )
wp_die( __( "You can't do that.", 'p2-resolved-posts' ) );
if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'p2_resolve-' . $post_id ) )
wp_die( __( 'Did you intend to do this?', 'p2-resolved-posts' ) );
if ( ! empty( $_GET['unresolve'] ) )
wp_set_object_terms( $post_id, array(), 'p2_resolved' );
else
wp_set_object_terms( $post_id, array( 'resolved' ), 'p2_resolved' );
wp_safe_redirect( wp_get_referer() );
die();
}
/**
* Drop in some CSS.
*/
function wp_head() {
echo "<style> span.actions { padding-right: 3px; }
li.status-resolved { opacity: .5; }
strong.p2-resolve { color: #000; } </style>\n";
}
/**
* Drop in some JS.
*/
function wp_footer() {
?>
<script>
(function($){
$(document).ready( function() {
$('.p2-resolve a').click( function(e) {
e.preventDefault();
$.ajax({
url : $(this).attr('href'),
context : $(this),
success : function() {
if ( $(this).hasClass('resolve') ) {
$(this).removeClass('resolve').addClass('unresolve');
$(this).attr('href', $(this).attr('href') + '&unresolve=1');
$(this).closest('li').addClass('status-resolved');
$(this).prev().text('<?php echo esc_js( __( 'Resolved', 'p2-resolved-posts' ) ); ?>');
$(this).text('<?php echo esc_js( __( 'Undo', 'p2-resolved-posts' ) ); ?>');
} else {
$(this).removeClass('unresolve').addClass('resolve');
$(this).attr('href', $(this).attr('href').replace('&unresolve=1', '') ); $(this).closest('li').removeClass('status-resolved');
$(this).prev().text('');
$(this).text('<?php echo esc_js( __( 'Mark as Resolved', 'p2-resolved-posts' ) ); ?>');
}
}
});
});
});
})(jQuery);
</script>
<?php
}
}
new P2_Resolved_Posts;
/**
* Search widget class
*
* @since 2.8.0
*/
class P2_Resolved_Posts_Widget extends WP_Widget {
function __construct() {
$widget_ops = array(
'classname' => 'p2-resolved-posts',
'description' => __( 'Allows querying of resolved and unresolved posts', 'p2-resolved-posts' )
);
parent::__construct( 'p2_resolved_posts', __( 'P2 Resolved Posts' ), $widget_ops);
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
?>
<ul>
<?php if ( is_home() && isset( $_GET['resolved'] ) && $_GET['resolved'] == 'unresolved' ) : ?>
<li><strong>&bull; <?php _e( 'Show <strong>unresolved</strong> threads', 'p2-resolved-posts' ); ?></strong></li>
<?php else : ?>
<li><a href="<?php echo esc_url( add_query_arg( 'resolved', 'unresolved', home_url() ) ); ?>"><?php _e( 'Show <strong>unresolved</strong> threads', 'p2-resolved-posts' ); ?></a></li>
<?php endif; ?>
<?php if ( is_tax( 'p2_resolved', 'resolved' ) ) : ?>
<li><strong>&bull; <?php _e( 'Show resolved threads', 'p2-resolved-posts' ); ?></strong></li>
<?php else : ?>
<li><a href="<?php echo esc_url( add_query_arg( 'resolved', 'resolved', home_url() ) ); ?>"><?php _e( 'Show resolved threads', 'p2-resolved-posts' ); ?></a></li>
<?php endif; ?>
<?php if ( is_home() && ! isset( $_GET['resolved'] ) ) : ?>
<li><strong>&bull; <?php _e( 'Show all threads', 'p2-resolved-posts' ); ?></strong></li>
<?php else : ?>
<li><a href="<?php echo esc_url( home_url() ); ?>"><?php _e( 'Show all threads', 'p2-resolved-posts' ); ?></a></li>
<?php endif; ?>
</ul>
<?php
echo $after_widget;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
$title = $instance['title'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment