Last active
September 26, 2018 07:50
-
-
Save wpmudev-sls/130f0519b5dff3a3200705fa9bb02051 to your computer and use it in GitHub Desktop.
Stops Indexer from auto publishing posts and creates a new menu page to manually publish them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Post Indexer - Unapproved Posts | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Stops Indexer from auto publishing posts and creates a new menu page to manually publish them. | |
* Version: 1.0.0 | |
* Author: Konstantinos Xenos @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WP_List_Table' ) ) { | |
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); | |
} | |
if ( ! class_exists( 'Post_Indexer_Unapproved_Posts' ) ) { | |
class Post_Indexer_Unapproved_Posts { | |
/** | |
* Constructor | |
*/ | |
public function __construct() { | |
$this->init(); | |
} | |
/** | |
* Initialization | |
*/ | |
public function init() { | |
add_action( 'postindexer_index_post', array( $this, 'make_index_draft_post' ) ); | |
add_action( 'network_admin_menu', array( $this, 'populate_admin_menu' ) ); | |
add_action( 'wp_ajax_indexer-publish-post', array( $this, 'indexer_publish_post' ) ); | |
add_action( 'wp_ajax_indexer-unpublish-post', array( $this, 'indexer_unpublish_post' ) ); | |
} | |
/** | |
* Change new post status to "pending" | |
*/ | |
public function make_index_draft_post( $post ) { | |
global $wpdb; | |
if ( is_numeric( $post['ID'] ) ) { | |
$table = $wpdb->base_prefix . 'network_posts'; | |
$data = array( | |
'BLOG_ID' => $post['BLOG_ID'], | |
'post_status' => 'pending', | |
); | |
$where = array( 'ID' => $post['ID'] ); | |
$wpdb->update( $table, $data, $where ); | |
} | |
} | |
/** | |
* Add ajax to publish the post | |
*/ | |
public function indexer_publish_post() { | |
global $wpdb; | |
$blog_id = intval( $_POST['blog_id'] ); | |
$post_id = intval( $_POST['post_id'] ); | |
$nonce = sanitize_text_field( $_POST['nonce'] ); | |
if ( ! wp_verify_nonce( $nonce, 'indexpost' . $blog_id . $post_id ) ) { | |
wp_send_json_error(); | |
} | |
$table = $wpdb->base_prefix . 'network_posts'; | |
$data = array( | |
'BLOG_ID' => $blog_id, | |
'post_status' => 'publish', | |
); | |
$where = array( 'ID' => $post_id ); | |
$wpdb->update( $table, $data, $where ); | |
wp_send_json_success(); | |
} | |
/** | |
* Add ajax to unpublish the post | |
*/ | |
public function indexer_unpublish_post() { | |
global $wpdb; | |
$blog_id = intval( $_POST['blog_id'] ); | |
$post_id = intval( $_POST['post_id'] ); | |
$nonce = sanitize_text_field( $_POST['nonce'] ); | |
if ( ! wp_verify_nonce( $nonce, 'indexpost' . $blog_id . $post_id ) ) { | |
wp_send_json_error(); | |
} | |
$table = $wpdb->base_prefix . 'network_posts'; | |
$data = array( | |
'BLOG_ID' => $blog_id, | |
'post_status' => 'pending', | |
); | |
$where = array( 'ID' => $post_id ); | |
$wpdb->update( $table, $data, $where ); | |
wp_send_json_success(); | |
} | |
/** | |
* Create "Unaproved Posts" menu | |
*/ | |
public function populate_admin_menu() { | |
add_menu_page( | |
esc_html__( 'Unapproved Posts', 'postindexer' ), | |
esc_html__( 'Unapproved Posts', 'postindexer' ), | |
'manage_options', | |
'indexer-unapproved-posts', | |
array( $this, 'create_page' ), | |
'dashicons-admin-page' | |
); | |
} | |
/** | |
* Create the Admin Page | |
*/ | |
public function create_page() { | |
?> | |
<div class="wrap"> | |
<h1><?php esc_html_e( 'Unapproved Posts', 'postindexer' ); ?></h1> | |
<hr class="wp-header-end"> | |
<?php | |
$indexer_network_posts = new PIUP_List_Table(); | |
$indexer_network_posts->prepare_items(); | |
$indexer_network_posts->display(); | |
?> | |
<script> | |
( function ( $ ) { | |
$( document ).ready(function() { | |
$( '.indexer_publish' ).on( 'submit', function( e ) { | |
var blog_id = $( this ).find( '#blog_id' ).val(), | |
post_id = $( this ).find( '#post_id' ).val(), | |
nonce = $( this ).find( '#nonce' ).val(); | |
e.preventDefault(); | |
data = { | |
'action': 'indexer-publish-post', | |
'blog_id': blog_id, | |
'post_id': post_id, | |
'nonce': nonce | |
}; | |
$.post( ajaxurl, data, function( response ) { | |
if ( response.success ) { | |
window.location.href = window.location.href; | |
} else { | |
console.log( 'invalid nonce' ); | |
} | |
} ); | |
} ); | |
} ); | |
$( '.indexer_unpublish' ).on( 'submit', function( e ) { | |
var blog_id = $( this ).find( '#blog_id' ).val(), | |
post_id = $( this ).find( '#post_id' ).val(), | |
nonce = $( this ).find( '#nonce' ).val(); | |
e.preventDefault(); | |
data = { | |
'action': 'indexer-unpublish-post', | |
'blog_id': blog_id, | |
'post_id': post_id, | |
'nonce': nonce | |
}; | |
$.post( ajaxurl, data, function( response ) { | |
if ( response.success ) { | |
window.location.href = window.location.href; | |
} else { | |
console.log( 'invalid nonce' ); | |
} | |
} ); | |
} ); | |
} ( jQuery ) ); | |
</script> | |
<?php | |
} | |
} | |
new Post_Indexer_Unapproved_Posts(); | |
} | |
if ( ! class_exists( 'PIUP_List_Table' ) ) { | |
class PIUP_List_Table extends WP_List_Table { | |
public function get_data() { | |
global $wpdb; | |
$data = array(); | |
$recent = $wpdb->get_results( "SELECT ID, BLOG_ID, post_status FROM {$wpdb->base_prefix}network_posts ORDER BY ID DESC" ); | |
if ( ! empty( $recent ) ) { | |
foreach ( $recent as $r ) { | |
$data[] = array( | |
'post_id' => $r->ID, | |
'blog_id' => $r->BLOG_ID, | |
'post_status' => $r->post_status, | |
); | |
} | |
} | |
return $data; | |
} | |
public function get_columns() { | |
$columns = array( | |
'id' => 'Post ID', | |
'title' => esc_html__( 'Title', 'postindexer' ), | |
'blog' => esc_html__( 'Blog', 'postindexer' ), | |
'action' => esc_html__( 'Action', 'postindexer' ), | |
); | |
return $columns; | |
} | |
public function prepare_items() { | |
$columns = $this->get_columns(); | |
$hidden = array(); | |
$sortable = array(); | |
$this->_column_headers = array( $columns, $hidden, $sortable ); | |
$this->items = $this->get_data(); | |
} | |
function column_default( $item, $column_name ) { | |
switch ( $column_name ) { | |
case 'id': | |
return $item['post_id']; | |
case 'title': | |
switch_to_blog( $item['blog_id'] ); | |
$output = '<a target="_blank" href="' . get_permalink( $item['post_id'] ) . '">' . get_the_title( $item['post_id'] ) . '</a>'; | |
restore_current_blog(); | |
return $output; | |
case 'blog': | |
switch_to_blog( $item['blog_id'] ); | |
$output = '<a target="_blank" href="' . get_option( 'home' ) . '">' . get_option( 'blogname' ) . '</a>'; | |
restore_current_blog(); | |
return $output; | |
case 'action': | |
if ( 'pending' === $item['post_status'] ) { | |
$output = '<form class="indexer_publish" method="POST"> | |
<input type="hidden" name="blog_id" id="blog_id" value="' . $item['blog_id'] . '"> | |
<input type="hidden" name="post_id" id="post_id" value="' . $item['post_id'] . '"> | |
<input type="hidden" name="nonce" id="nonce" value="' . wp_create_nonce( 'indexpost' . $item['blog_id'] . $item['post_id'] ) . '"> | |
<button type="submit" class="button button-primary">' . esc_html__( 'Publish', 'postindexer' ) . '</button> | |
</form>'; | |
} elseif ( 'publish' === $item['post_status'] ) { | |
$output = '<form class="indexer_unpublish" method="POST"> | |
<input type="hidden" name="blog_id" id="blog_id" value="' . $item['blog_id'] . '"> | |
<input type="hidden" name="post_id" id="post_id" value="' . $item['post_id'] . '"> | |
<input type="hidden" name="nonce" id="nonce" value="' . wp_create_nonce( 'indexpost' . $item['blog_id'] . $item['post_id'] ) . '"> | |
<button type="submit" class="button">' . esc_html__( 'Unpublish ', 'postindexer' ) . '</button> | |
</form>'; | |
} | |
return $output; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment