-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptofr-comments.php
More file actions
205 lines (146 loc) · 5.63 KB
/
cryptofr-comments.php
File metadata and controls
205 lines (146 loc) · 5.63 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* @package cryptofrcomments
*/
/*
Plugin Name: CryptoFR Comments
Plugin URI: https://cryptofr.com
Description: Plugin for installing CryptoFR Forum Comments Box
Version: 1.0.0
Author: CryptoFR
Author URI: https://cryptofr.com
License: --
Text Domain: CryptoFR Comments-plugin
*/
defined( 'ABSPATH' ) or die( 'Nope, not accessing this' );
class cryptofrcomments{
public $plugin_name;
// Contructor and definitions of actions, hooks and filters
function __construct(){
$this->plugin_name= plugin_basename(__FILE__);
define('PLUGIN_PATH', dirname(__FILE__)); // Definition of the base path of the plugin
define('NODEBB_URL', 'https://testforum.cryptofr.com'); // Definition of the CryptoFr Main Forum URL
include (PLUGIN_PATH."/includes/methods.php");
//-- Hooks
add_action('publish_post',array($this,'markPostOnPublish'),10,2 ); // When a post is published
add_action('admin_enqueue_scripts', 'publish'); // When an admin page is loaded
add_action('admin_menu','add_admin_pages'); // When the admin menu is loaded
add_action( 'get_footer', 'front'); // When the footer on the front is loaded
//-- Endpoints
// Get the blogger info from its id
add_action( 'rest_api_init', function () {
register_rest_route( 'cryptofr-comments', '/getbloggerendpoint/(?P<post_author>\d+)', array(
'methods' => 'GET',
'callback' => 'getbloggerendpoint'
) );
} );
// Set the status to publish or pending of one article
add_action( 'rest_api_init', function () {
register_rest_route( 'cryptofr-comments', '/publishendpoint', array(
'methods' => 'POST',
'callback' => 'publishendpoint'
) );
} );
// Set the status to publish to multiple articles from an array
add_action( 'rest_api_init', function () {
register_rest_route( 'cryptofr-comments', '/publishendpointarray', array(
'methods' => 'POST',
'callback' => 'publishendpointarray'
) );
} );
// Get the blogg info from the article
add_action( 'rest_api_init', function () {
register_rest_route( 'cryptofr-comments', '/getbloggerbypostid/(?P<post_id>\d+)', array(
'methods' => 'GET',
'callback' => 'getbloggerbypostid'
) );
} );
// Set the status to publish or pending of one article
add_action( 'rest_api_init', function () {
register_rest_route( 'cryptofr-comments', '/attachmentendpoint', array(
'methods' => 'POST',
'callback' => 'attachmentendpoint'
) );
} );
// -- Filters
// Overwrite wordpress comments template
add_filter('comments_template', 'cryptofrCommentsTemplate',10,1);
// Overwrite the action links on the plugin list
add_filter("plugin_action_links_$this->plugin_name", 'settings_link');
}
// -- MARK AN ARTICLE ON THE WORDPRES DATABASE, READY TO BE PUBLISH TO THE CRYPTOFR MAIN FORUM
function markPostOnPublish($ID, $post) {
global $wpdb;
$table_name = $wpdb->prefix . 'posts';
// if (!$ID || !$post) return false;
$sqlCommand = "SELECT * FROM ".$table_name." WHERE ID=%s AND cryptofrcomments='Disabled'";
$wpdb->query($wpdb->prepare($sqlCommand, $post->ID ));
if ($wpdb->last_result){
$sqlCommand = "UPDATE ".$table_name." SET cryptofrcomments='Marked' WHERE ID=%s";
$wpdb->query($wpdb->prepare($sqlCommand, $post->ID ));
}
}
// -- WHEN PLUGIN IS ACTIVATED CREATE/ALTER THE WORDPRESS DATABASE TO ADD FIELDS TO THE WP_POST TABLE AND A NEW TABLE CRYPTOFRCOMMENTS TO STORE THE CONFIG OF THE PLUGIN
function activate(){
ob_start();
global $wpdb;
// Altering post table for cryptofrcomments attribute
// $table_name = $wpdb->prefix . 'posts';
//$check_column = (array) $wpdb->get_results( "SELECT count(cryptofrcomment) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME = '{$table_name}' AND COLUMN_NAME = 'cryptofrcomments'" )[0];
// $check_column = (int) array_shift($check_column);
// if($check_column == 0) {
// $wpdb->query(
// "ALTER TABLE $table_name
// ADD COLUMN `cryptofrcomments` VARCHAR(55) NOT NULL DEFAULT 'Disabled'
// ");
// }
// Create "cryptofrcomments" table for saving config if not exists
$sqlCommand= "DROP TABLE IF EXISTS `cryptofrcomments`";
$wpdb->query($sqlCommand);
$sqlCommand ="
CREATE TABLE IF NOT EXISTS `cryptofrcomments` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`cid` int (11) NOT NULL,
`allow_guest` char(1) NOT NULL,
`default_avatar` VARCHAR(300) NOT NULL DEFAULT '',
`avatar_name` VARCHAR(100) NOT NULL DEFAULT '',
`attached` VARCHAR(55) NOT NULL DEFAULT 'Pending',
`accept_comments` BOOLEAN NOT NULL,
`moderate_comments` BOOLEAN NOT NULL,
`use_keywords` BOOLEAN NOT NULL,
`case_sensitive` BOOLEAN NOT NULL,
`words_rendering` BOOLEAN NOT NULL,
PRIMARY KEY (`ID`)
);";
$wpdb->query($sqlCommand);
// Create "cryptofrcomments_cids" table for saving optional cids
$sqlCommand= "DROP TABLE IF EXISTS `cryptofrcomments_cids`";
$wpdb->query($sqlCommand);
$sqlCommand ="
CREATE TABLE IF NOT EXISTS `cryptofrcomments_cids` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`cid` int (11) NOT NULL,
PRIMARY KEY (`ID`)
);";
$wpdb->query($sqlCommand);
$sqlCommand ="
INSERT INTO `cryptofrcomments` (cid)
VALUES (0);
";
$wpdb->query($sqlCommand);
flush_rewrite_rules();
}
// -- DEACTIVATION OF THE WORDPRESS PLUGIN
function deactivate(){
flush_rewrite_rules();
}
}
// Initialization of the plugin object
if (class_exists('cryptofrcomments')){
$cryptofrcomments= new cryptofrcomments();
//echo "yes";
}
//activation
register_activation_hook(__FILE__,array($cryptofrcomments,'activate'));
//deactivation
register_deactivation_hook(__FILE__,array($cryptofrcomments,'deactivate'));