-
Notifications
You must be signed in to change notification settings - Fork 1
/
revision-post.php
executable file
·151 lines (136 loc) · 3.95 KB
/
revision-post.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
<?php
/*
Plugin Name: Revision Post
Plugin URI: http://www.mobvox.com.br
Description: Permite liberar um post para revisão pública.
Author: Daniel Pakuschewski
Version: 0.1
Author URI: http://www.danielpk.com.br
*/
class Revision_Post {
/**
* Hold DB Name
* @var int
*/
private $dbName = 'revision';
/**
* Hold Wpdb instance
*/
private $Wpdb;
public function __construct() {
global $wpdb;
$this->Wpdb = $wpdb;
$this->dbName = $wpdb->prefix . $this->dbName;
if (!is_admin()) {
add_action('init', array(&$this, 'show_revision'));
} else {
add_action('admin_menu', array(&$this, 'meta_box'));
add_action('save_post', array(&$this, 'save_post'), 1, 2);
}
}
/**
* Add revision_link box inside Post form.
*/
public function meta_box() {
add_meta_box('revisionpost', 'Revisão Pública', array(&$this, 'revision_link'), 'post', 'normal', 'high');
}
/**
* Hook Save Post
* @param int $postID
*/
public function save_post($postID, $Post) {
if(isset($_POST['revision_status']) && $_POST['revision_status'] == true && $Post->post_type != 'revision'){
$this->Wpdb->query("INSERT INTO {$this->dbName} SET post_id = '{$postID}', created = NOW()");
}elseif($_POST['action'] != 'inline-save' && $_POST['revision_status'] == false){
$this->Wpdb->query("DELETE FROM {$this->dbName} WHERE post_id = '{$postID}'");
}
}
/**
* Box inside post form.
* @param array $post
*/
public function revision_link($post) {
if (!in_array($post->post_status, array('publish'))) {
?>
<p>
<label for="revision_status" class="selectit">
<input type="checkbox" name="revision_status" id="revision_status" value="1" <?php if ($this->isRevision($post->ID)){ echo ' checked="checked"'; } ?> /> Habilitar Revisão Pública</label>
</p>
<?php
if($this->isRevision($post->ID)){
$url = htmlentities(add_query_arg(array('p' => $post->ID, 'preview' => 'true'), get_option('home') . '/'));
echo "<p><a href='$url'>$url</a><br /><br />\r\n";
}
} else {
echo '<p>Esse post já está publicado.</p>';
}
}
public function show_revision() {
if (!is_admin() && isset($_GET['p']) && isset($_GET['preview'])) {
$postID = (int) $_GET['p'];
if(!$this->isRevision($postID) && !current_user_can('edit_posts')){
wp_die('Você não ter permissão para acessar esse post.');
}
add_filter('posts_results', array(&$this, 'fake_publish'));
}
}
/**
* Change post state to publish.
* @param array $posts
* @return array
*/
public function fake_publish($posts) {
$posts[0]->post_status = 'publish';
return $posts;
}
/**
* Check if $postID is checked for revision.
* @param int $postID
* @return boolen
*/
private function isRevision($postID){
$count = $this->Wpdb->get_var($this->Wpdb->prepare("SELECT COUNT(*) FROM {$this->dbName} WHERE post_id = {$postID}"));
if($count){
return true;
}
return false;
}
/**
* Create table into database for hold posts to revision.
* @return void
*/
public function install(){
if($this->Wpdb->get_var("SHOW TABLES LIKE '" . $this->dbName . "'") != $this->dbName) {
$sql = "CREATE TABLE " . $this->dbName . " (
id int(11) NOT NULL AUTO_INCREMENT,
post_id int(11) NOT NULL,
created datetime NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
/**
* Drop table that hold posts to revision.
*/
public function uninstall(){
if($this->Wpdb->get_var("SHOW TABLES LIKE '" . $this->dbName . "'") == $this->dbName) {
$sql = "DROP TABLE " . $this->dbName . ";";
$this->Wpdb->query($sql);
}
}
}
$Revision_Post = new Revision_Post();
/* Plugin Install */
register_activation_hook(__FILE__, 'install' );
function install(){
$Revision_Post = new Revision_Post();
$Revision_Post->install();
}
/* Plugin Uninstall */
register_deactivation_hook( __FILE__, 'uninstall' );
function uninstall(){
$Revision_Post = new Revision_Post();
$Revision_Post->uninstall();
}