-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-plugin.php
270 lines (226 loc) · 10.3 KB
/
test-plugin.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/*
Plugin Name: My first Plugin
Plugin URI: http://localhost
*/
//Apokrypsh ths admin bar gia toys subscribers
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function get_min_id($username){
$mysqli70 = new mysqli( "localhost", "root", "", "wp" );
$min_query = "SELECT MIN(post_id) FROM article_selection WHERE username = '$username'";
$min_post_id = $mysqli70->query($min_query);
$min_id = mysqli_fetch_row($min_post_id)[0];
return $min_id;
}
function handle_priority_records($username){
$mysqli60 = new mysqli( "localhost", "root", "", "wp" );
$query = "SELECT COUNT(*) FROM article_selection WHERE username = '$username'";
$count_query = $mysqli60->query($query);
$count = mysqli_fetch_row($count_query)[0];
if ($count > 5){
$min_post_id = get_min_id($username);
$delete_query = "DELETE FROM article_id WHERE post_id = '$min_post_id'";
$mysqli60->query($delete_query);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Oi dyo aytes synarthseis trexoyn mono otan ena post dhmosieyetai gia prwth fora
add_action('transition_post_status', 'run_when_post_published', 10, 3);
//add_action('transition_post_status', 'GetLastPostId', 10, 3);
function run_when_post_published($new_status, $old_status, $post){
if ( $new_status == 'publish' && $old_status != 'publish' ) { //Elegxei prohgoymeno kai twrino status (einai gia to add_action)
$mysqli = new mysqli( "localhost", "root", "", "wp" );
$sql_get_users = "SELECT ID FROM wp_users";
$ids = $mysqli->query($sql_get_users);
$sql_delete = "DELETE FROM interest_array WHERE count_tags = 263"; //testing query
$mysqli->query($sql_delete); //testing query
//Antlhsh ID toy teleytaioy dhmosieymenoy post
$mysqli2 = new mysqli( "localhost", "root", "", "wp" );
$recent_posts = wp_get_recent_posts( array( 'numberposts' => '1' ) );
$thePostID = $recent_posts[0]['ID'];
//Antlhsh tags toy teleytaioy dhmosieymenoy post apo to ID poy vrethike parapanw
$mysqli3 = new mysqli( "localhost", "root", "", "wp" );
$tags_of_new_post = get_the_tags($thePostID);
$tags_ids = array_map(create_function('$o', 'return $o->term_id;'), $tags_of_new_post);
foreach ($tags_ids as &$value1) {
$the_tags_of_new_post = intval($value1);
}
//Kathgoriopoihsh endiaferontos vasei tags, ana xrhsth
while($row = $ids->fetch_assoc()) {
$r_id = $row["ID"];
$sql_order_top = "SELECT tags_id FROM interest_array WHERE user_id='$r_id' ORDER BY count_tags DESC LIMIT 3";
$sql_order_middle = "SELECT tags_id FROM interest_array WHERE user_id='$r_id' ORDER BY count_tags DESC LIMIT 4,6";
$ordered_tags_per_user_top = $mysqli->query($sql_order_top);
$top_priorities_arrays = mysqli_fetch_all($ordered_tags_per_user_top,MYSQLI_ASSOC);
$top_priorities = array_column($top_priorities_arrays,'tags_id');
$ordered_tags_per_user_middle = $mysqli->query($sql_order_middle);
$middle_priorities_arrays = mysqli_fetch_all($ordered_tags_per_user_middle,MYSQLI_ASSOC);
$middle_priorities = array_column($middle_priorities_arrays,'tags_id');
$result = array_intersect($top_priorities, $tags_ids);
if(empty($result)) {
$result2 = array_intersect($middle_priorities, $tags_of_new_post);
if(empty($result2)) {
return 3; //TIPOTA
}
else { //if(!empty($result2))
$mysqli20 = new mysqli( "localhost", "root", "", "wp" );
$user = get_user_by('id', $r_id);
$username = $user->user_login;
handle_priority_records($username);
$query20 = $mysqli20->query("INSERT INTO article_selection (user_id, username, post_id, priority) VALUES ('$r_id', '$username', '$thePostID', 2)");
$mysqli20->close(); //EINAI STA MIDDLE
}
}
else {//if(!empty($result1)){
// 1; //EINAI STA TOP
$mysqli30 = new mysqli( "localhost", "root", "", "wp" );
$user = get_user_by('id', $r_id);
$username = $user->user_login;
handle_priority_records($username);
$query30 = $mysqli30->query("INSERT INTO article_selection (user_id, username, post_id, priority) VALUES ('$r_id', '$username', '$thePostID', 1)");
$mysqli30->close(); //EINAI STA TOP
}
}
$mysqli->close();
}
}
add_action('rest_api_init', 'get_ontology');
function get_ontology(){
register_rest_route('ontology/picks/', '/user/(?P<username>[a-zA-Z0-9-]+)',array(
'methods' => GET,
'callback' => 'get_ontology_picks',
'args' => ['username']));
}
function get_ontology_picks($data){
$username = $data['username'];
$user = get_user_by('login', $username);
$mysqli_user = new mysqli( "localhost", "root", "", "wp" );
$query_ontology = "SELECT artist_name FROM ontology_picks WHERE user = '$username'";
$query_user = $mysqli_user->query($query_ontology);
$user_pick = mysqli_fetch_row($query_user)[0];
$tag = get_term_by('name', $user_pick, 'post_tag', $filter = 'utf8_encode');
$tag_id = $tag->term_id;
//Get the last 10 posts of the user's pick
$request1 = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request1->set_query_params(array(
'tags' => $tag_id,
'per_page' => 2));
$response1 = rest_do_request( $request1 );
$response_array1 = json_decode(json_encode($response1, true), true);
$response_array_data1 = $response_array1["data"];
echo json_encode($response_array_data1, true);
}
///////////////////////////--------------------------------------------------------------------------------///////////////////////////
add_action('rest_api_init', 'get_user_id');
function get_user_id(){
register_rest_route('userid/','user/(?P<username>[a-zA-Z0-9-]+)', array(
'methods' => GET,
'callback' => 'get_this_id',
'args' => [ 'username' ]));
}
function get_this_id($data){
$username = $data['username'];
$user = get_user_by('login', $username);
return $user;
}
add_action('rest_api_init', 'get_all_data');
/*function get_all_data_test(){
register_rest_route('suggestions/', 'user/(?P<id>\d+)', array(
'methods' => GET,
'callback' => 'get_all',
'args' => [ 'id' ] ));
}*/
function get_all_data(){
register_rest_route('suggestions/', '/user/(?P<username>[a-zA-Z0-9-]+)', array(
'methods' => GET,
'callback' => 'get_id',
'args' => [ 'username' ] ));
}
function get_id($data){
$username = $data['username'];
$user = get_user_by('login', $username);
get_all($user->ID);
}
function get_all($data){
//Get the last 10 posts of the website
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'per_page', 10 );
$response = rest_do_request( $request );
$response_array = json_decode(json_encode($response, true), true);
$response_array_data = $response_array["data"];
//Get the suggested articles for the user
//First you need to get the ids of these posts
$mysqli_suggested = new mysqli( "localhost", "root", "", "wp" );
$user_id = $data/*['id']*/;
$query_suggested_posts_id_init = "SELECT post_id FROM article_selection WHERE user_id=%u";
$query_suggested_posts_id = sprintf($query_suggested_posts_id_init,$user_id);
$suggested = $mysqli_suggested->query($query_suggested_posts_id);
$suggested_posts_ids_array = [];
while ($row = mysqli_fetch_assoc($suggested)){
array_push($suggested_posts_ids_array,$row);
}
$suggested_posts_ids = array_column($suggested_posts_ids_array,'post_id');
//echo json_encode($suggested_posts_ids);
//All the suggested posts's ids are in $suggested_posts_ids
foreach ($suggested_posts_ids as $value){
$article_id = strval($value);
$link1 = "/wp/v2/posts/%u";
$link = sprintf($link1,$article_id);
$request1 = new WP_REST_Request('GET', $link);
$request1->set_param( 'per_page', 10 );
$response1 = rest_do_request( $request1 );
$response_array1 = json_decode(json_encode($response1, true), true);
$response_array1_data = $response_array1["data"];
$count = sizeof($response_array_data);
$response_array_data[$count] = $response_array1_data;
}
echo json_encode($response_array_data, true);
}
add_action('wp_head', 'your_function_name');
function your_function_name(){
?>
<?php
//Changes - Phase 1: Travame ta tags twn arthrwn poy diavazei o kathe xrhsths kai ta apothikeyoyme sto database
$mysqli = new mysqli("localhost", "root", "", "wp");
/*function debug_to_console( $data ) {
$output = $data;
if ( is_array( $output ) )
$output = implode( ',', $output);
echo "<script>console.log( 'Debug Objects: " . $output . "' );</script>";
}*/
$user_id = intval(get_current_user_id());
//debug_to_console("The user is ".$user_id);
$url_test = get_permalink();
//debug_to_console("The url is ".$url_test);
//Tsekaroyme ton pinaka gi ayton ton xrhsth kai ayto to tag
if ( $url_test == 'http://localhost/wordpress/' OR $user_id == 0 ){
//do nothing
//debug_to_console("eimai to url ".$url_test);
}
else {
$tag_objects = get_the_tags($post->ID);
$tags_id = array_map(create_function('$o', 'return $o->term_id;'), $tag_objects);
//print_r( $tags_id );
foreach ($tags_id as &$value) {
$tag = intval($value);
$check_interest = $mysqli->query("SELECT * FROM interest_array WHERE user_id = '$user_id' AND tags_id = '$tag'");
if (is_null($check_interest->fetch_assoc())) {
$sql = "INSERT INTO interest_array (user_id, tags_id, count_tags) VALUES ('$user_id', '$tag', 1)";
$mysqli->query($sql);
}
else {
$sql_update = "UPDATE interest_array SET count_tags=count_tags+1 WHERE user_id = '$user_id' AND tags_id = '$tag'";
$mysqli->query($sql_update);
}
}
$mysqli->close();
}
?> <?php
};
?>