public

P2 Privately Published Posts

  • Download Gist
p2-privately-published-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
<?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;

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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.