public
Created

P2 Resolved Posts

  • Download Gist
p2-resolved-posts.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
<?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;
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.