Skip to content

Instantly share code, notes, and snippets.

@nacin
Created August 14, 2011 22:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nacin/1145395 to your computer and use it in GitHub Desktop.
Save nacin/1145395 to your computer and use it in GitHub Desktop.
P2 Privately Published Posts
<?php
/* Plugin Name: P2 Privately Published Posts
* Description: Allows you to publish private posts to a P2.
* Author: Andrew Nacin
* Author URI: http://andrewnacin.com/
*/
/* WARNING about studying and copying this code:
*
* P2 is not currently an ideal platform for developing extensions. Some of this
* plugin is hacky and may make kittens and core developers cry. It is not
* guaranteed to work as-is in a future version of P2. While I'll keep it updated
* to work, please do your best to avoid these techniques. Do as I say, not as I do.
*
* -- Nacin
*/
/**
* @package P2_Privately_Published_Posts
*/
class P2_Privately_Published_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' ) );
}
/**
* Sets up initial hooks, if the P2 theme is in play.
*/
function after_setup_theme() {
if ( ! class_exists( 'P2' ) )
return;
add_action( 'p2_ajax', array( $this, 'p2_ajax' ) );
add_action( 'wp_head', array( $this, 'wp_head' ) );
add_action( 'p2_post_form', array( $this, 'p2_post_form' ) );
add_action( 'wp_footer', array( $this, 'wp_footer' ) );
add_action( 'p2_action_links', array( $this, 'p2_action_links' ) );
}
/**
* Fires on the p2_ajax hook. Calls the method appropriate for the action.
*/
function p2_ajax( $action ) {
if ( method_exists( $this, 'p2_ajax_' . $action ) )
call_user_func( array( $this, 'p2_ajax_' . $action ) );
}
/**
* Fires on the new_post action for p2_ajax. Removes any 'private' tag and adds
* a filter that will later modify the post status.
*/
function p2_ajax_new_post() {
$tags = $_POST['tags'];
$tags = is_array( $tags ) ? $tags : explode( ',', trim( $tags, " \n\t\r\0\x0B," ) );
if ( in_array( 'private', $tags ) ) {
unset( $tags[ array_search( 'private', $tags ) ] );
$_POST['tags'] = implode( ',', $tags );
add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 );
}
}
/**
* Fires on the get_latest_posts action for p2_ajax. Adds a hook to pre_get_posts that
* addresses an issue in P2_Ajax::get_lastest_posts().
*/
function p2_ajax_get_latest_posts() {
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
}
function wp_insert_post_data( $data, $postarr ) {
remove_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 );
$data['post_status'] = 'private';
return $data;
}
/**
* Works around an issue where post_status=publish is unnecessarily passed to query_posts(),
* rather than letting WP_Query decide what the user is allowed to see.
*/
function pre_get_posts( $query ) {
remove_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
unset( $query->query_vars['post_status'] );
}
/**
* Adds 'Private Post' near the action links.
*/
function p2_action_links() {
global $post;
if ( 'private' == $post->post_status )
echo ' | <strong class="p5">' . __( 'Private Post', 'p2-privately-published-posts' ) . '</strong>';
}
/**
* Drop in some CSS.
*/
function wp_head() {
echo "<style> span.actions { padding-right: 3px; }
li.status-private { background: #FEF0F0; }
#p5-private-wrap { visibility: hidden; }
#p5-private-wrap label { font-size: 1.2em }
strong.p5 { color: #900; } </style>\n";
}
/**
* Add checkbox for privately publishing a post.
*/
function p2_post_form() {
echo '<div id="p5-private-wrap"><label for="p5-private"><input type="checkbox" id="p5-private" /> ' . __( 'Privately publish this post', 'p2-privately-published-posts' ) . '</label></div>';
}
/**
* Drop in some JS.
*/
function wp_footer() {
?>
<script>
(function($){
$(document).ready( function() {
$('#p5-private-wrap').css('visibility', 'visible');
$('#p5-private').change( function() {
var tags, original, tagIt;
tags = $('#tags').val();
tags = tags.length ? tags.split(',') : [];
tags = $.map( tags, function(i) { return $.trim(i); } );
tagIt = tags.indexOf('<?php echo esc_js( __( 'Tag it', 'p2' ) ); ?>');
if ( -1 !== tagIt )
tags.splice( tagIt, 1 );
original = tags.slice();
if ( this.checked && -1 === $.inArray( 'private', tags ) ) {
tags.push('private');
} else if ( ! this.checked && -1 !== $.inArray( 'private', tags ) ) {
tags.splice( tags.indexOf('private'), 1 );
}
if ( original !== tags )
$('#tags').val( tags.join(', ') );
});
$('#tags').change( function() {
var tags = $(this).val().split(',');
tags = $.map( tags, function(i) { return $.trim(i); } );
$('#p5-private').prop('checked', -1 !== tags.indexOf('private'));
});
});
})(jQuery);
</script>
<?php
}
}
new P2_Privately_Published_Posts;
@Ramoonus
Copy link

why not release this to the WP.org plugin repository?

@nacin
Copy link
Author

nacin commented Aug 16, 2011

Probably will at some point. Though, of the three plugins, this one employs the worst hack by far.

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