-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesdlv-play.php
145 lines (120 loc) · 4 KB
/
esdlv-play.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
<?php
/*
Plugin Name: ESDLV Play
Plugin URI: ---
Description: ESDLV gamification plugin
Version: 0.1
Author: Javier Malonda
Author URI: elsentidodelavida.net
License: GPL2
*/
defined('ABSPATH') or die("Bye bye");
// Install, on plugin activation, new table with user scores
global $pu_db_version;
$pu_db_version = '1.0';
/**
* esdlv_play_table_install - Creates the esdlv_table on activation
*
* @return void
*/
function esdlv_play_table_install() {
global $wpdb;
global $esdlv_play_db_version;
$table_name = $wpdb->prefix . 'esdlv_play';
$charset_collate = $wpdb->get_charset_collate();
// Two spaces between the words PRIMARY KEY and the definition of your primary key.
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
user_id smallint NOT NULL,
user_score mediumint NOT NULL default 0,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
// Execute DB delta changes. Actually create the table.
dbDelta( $sql );
add_option( 'esdlv_play_db_version', $esdlv_play_db_version );
}
register_activation_hook( __FILE__, 'esdlv_play_table_install' );
// Adds a shortcode calling the function below
add_shortcode( 'esdlv_scores', 'esdlv_play_show_scores' );
/**
* esdlv_play_show_scores - Prints a table showing the user scores
*
* @return void
*/
function esdlv_play_show_scores() {
// If user is registered
if ( is_user_logged_in() ) {
global $wpdb;
// Get every user with his score from the play table
$table_name = $wpdb->prefix . 'esdlv_play';
$charset_collate = $wpdb->get_charset_collate();
$results = $wpdb->get_results(
$wpdb->prepare( "SELECT * FROM $table_name ORDER BY user_score DESC" )
);
// Shortcodes need a returned value, not echoed.
// Start an output buffer that will capture all echoes
ob_start();
echo "<table class='esdlv-scores'>";
echo "<tr>";
echo "<th>Usuario</th><th>Puntuación</th>";
foreach ( $results as $result ) {
$user_id = $result->user_id;
$username = get_user_by('id', $user_id)->display_name;
// Print table
echo "<tr>";
echo "<td>" . $username . "</td><td>" . $result->user_score . "</td>";
echo "</tr>";
}
echo "</table>";
// End output buffer
$output = ob_get_clean();
}
// Not registered users
else {
ob_start();
echo "<p>Esta página sólo está disponible para usuarios registrados.";
echo "<br>";
echo "<p>Puedes registrarte <a href='https://elsentidodelavida.net/wp-login.php?action=register'>aquí</a>.";
$output = ob_get_clean();
}
return $output;
}
/**
* esdlv_play_add_score_on_comment - Adds a score to a user upon commenting
*
* @param mixed $comment_ID
* @return void
*/
function esdlv_play_add_score_on_comment( $comment_ID ) {
// Find out the user who commented
$user_id = get_comment( $comment_ID )->user_id;
// update user score in score table
$score_comment = 10;
global $wpdb;
// Get user's current score
$table_name = $wpdb->prefix . 'esdlv_play';
$result = $wpdb->get_results(
$wpdb->prepare( "SELECT user_score FROM $table_name WHERE user_id = $user_id" )
);
$current_score = $result[0]->user_score;
$new_score = $current_score + $score_comment;
// Update score
$wpdb->update( $table_name, array(
'user_score' => $new_score,
'time' => current_time('mysql')
),
array( 'user_id' => $user_id ) );
}
add_action( 'comment_post', 'esdlv_play_add_score_on_comment' );
/**
* esdlv_play Load custom CSS
*
* @return void
*/
function esdlv_play_css() {
wp_register_style('esdlv_play_css', plugins_url('style.css',__FILE__ ));
wp_enqueue_style('esdlv_play_css');
}
add_action( 'wp_enqueue_scripts','esdlv_play_css');