Skip to content

Instantly share code, notes, and snippets.

@kachi
Created February 28, 2013 05:15
Show Gist options
  • Save kachi/5054369 to your computer and use it in GitHub Desktop.
Save kachi/5054369 to your computer and use it in GitHub Desktop.
<?php
if (!class_exists('ApproveFromPreview')){
class ApproveFromPreview {
function __construct() {
add_filter('the_content',array($this,'add_approve_button'));
add_filter('the_content',array($this,'show_approved'));
add_action('wp',array($this,'approve_post'));
}
public function add_approve_button($content){
global $post;
if (is_preview() &&
current_user_can( 'edit_post', $post->ID ) &&
current_user_can( 'publish_posts', $post->ID ) &&
in_array($post->post_status,array('draft', 'pending'))
){
return $this->get_button().$content;
}
return $content;
}
public function show_approved($content){
$msg = '';
if ( isset( $_GET['msg'] ) ){
if( $_GET['msg'] == 'approved' )
$con = '<div class="success" style="background: #c9e8f5;padding: 3px;">' . __( '公開されました' ) . '</div>';
else
$con = '<div class="error" style="background: #f8c7c7;padding: 3px;">' . __( '記事は下書きのままです' ) . '</div>';
}
return $con . $content;
}
public function get_button(){
global $post;
return '<a href="'.wp_nonce_url( "?action=AFP&pid=" . $post->ID, 'AFP_NONCE').'" class="button">'.__('この記事を公開する').'</a><br/>';
}
public function approve_post(){
if (!isset($_REQUEST['_wpnonce']) ||
!isset($_REQUEST['pid']) ||
!isset( $_REQUEST['action']) ||
$_REQUEST['action'] != "AFP" )
{
return;
}
$nonce = $_REQUEST['_wpnonce'];
if ( !wp_verify_nonce( $nonce, 'AFP_NONCE' ) ) {
return;
}
$pid = intval($_REQUEST['pid']);
if (current_user_can( 'edit_post',$pid ) && current_user_can( 'publish_posts', $pid )){
$p = $this->change_post_status($pid,'publish');
if ($p > 0){
$redirect = add_query_arg( array('msg' => 'approved'), get_permalink($p) );
wp_redirect( $redirect );
exit;
}else{
$redirect = add_query_arg( array('msg' => 'not_approved'), get_permalink() );
wp_redirect( $redirect );
exit;
}
return;
}
return;
}
public function change_post_status($post_id,$status){
$current_post = get_post( $post_id, 'ARRAY_A' );
$current_post['post_status'] = $status;
return wp_update_post($current_post);
}
}
}
new ApproveFromPreview();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment