prefix . "answers";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
id_comment int NOT NULL,
id_post int NOT NULL,
email text NOT NULL,
username text NOT NULL,
to_author text NOT NULL,
comment_date text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
register_activation_hook( __FILE__, 'abfc_install_table' );
add_action( 'comment_post', 'abfc_message_post', 10, 2 );
function abfc_message_post( $comment_ID, $comment_approved ) {
global $wpdb;
$datum = date("j.n.Y H:i");
$table_name = $wpdb->prefix . "answers";
$table_name_comments = $wpdb->prefix . "comments";
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
$current_user_email = $current_user->user_email;
$current_user_name = $current_user->user_login;
} else {
$current_user_email = $wpdb->get_var( "SELECT comment_author_email FROM $table_name_comments WHERE comment_ID = '$comment_ID'");
$current_user_name = $wpdb->get_var("SELECT comment_author FROM $table_name_comments WHERE comment_ID = '$comment_ID'");
}
$tmp = $wpdb->get_var( "SELECT comment_author FROM $table_name_comments WHERE comment_ID = '$comment_ID'");
$tmp_parent = $wpdb->get_var( "SELECT comment_parent FROM $table_name_comments WHERE comment_ID = '$comment_ID'");
$tmp_post_id = $wpdb->get_var( "SELECT comment_post_ID FROM $table_name_comments WHERE comment_ID = '$comment_ID'");
$sel = $wpdb->get_results("SELECT * FROM $table_name_comments");
if ($tmp_parent!=0) {
$ofi_author = $wpdb->get_var("SELECT comment_author_email FROM $table_name_comments WHERE comment_ID = '$tmp_parent'"); // Vyhledá autora, kterému se ukáže poté, že mu někdo odpověděl
if ($current_user_email != $ofi_author) {
foreach ($sel as $key ) {
if ($tmp_parent == $key->comment_ID) {
$wpdb->insert($table_name, array('id_comment' => $comment_ID, 'id_post' => $tmp_post_id, 'email' => $current_user_email, 'username' => $current_user_name, 'to_author' => $ofi_author ,'comment_date' => $datum ));
$pocet = $wpdb->get_var("SELECT count(email) FROM $table_name WHERE to_author='$ofi_author'");
//pokud jsou na jeden email v databázi více než 3 výsledky, staré se smažou
if ($pocet > 3) {
$limit = $pocet - 3;
for ($i=0; $i < $limit ; $i++) {
$smazat= $wpdb->get_var("SELECT min(id) FROM $table_name WHERE to_author='$ofi_author'");
$wpdb->delete($table_name, array('id' => $smazat ));
}
}
}
}
}
}
}
function abfc_button() {
$response = "";
$number = 0;
$table_name = $wpdb->prefix . "answers";
global $wpdb;
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
$current_user_email = $current_user->user_email;
$table_name = $wpdb->prefix . "answers";
$table_name_posts = $wpdb->prefix . "posts";
$search = $wpdb->get_results( "SELECT * FROM $table_name WHERE to_author = '$current_user_email' ORDER BY id desc");
foreach ($search as $key => $value){
$post = $wpdb->get_var( "SELECT guid FROM $table_name_posts WHERE ID = '$value->id_post'");
$response .= "";
$response .= "
";
$response .= "
" . $value->comment_date . "
";
$response .= "
";
$number++;
}
} else {
$road = get_site_url() . "/wp-login.php";
$response .= " Please login here
";
}
$ab_div .= "";
if ($number > 0) {
$ab_div .= "
" .$number. "
";
}
$ab_div .= '
';
$ab_div .= "
";
$ab_div .='
';
$ab_div .='
';
$ab_div .='
';
$ab_div .="";
$ab_div .= $response;
$ab_div .='
';
echo $ab_div;
}
function abfc_delete() {
global $wpdb;
if (isset($_POST["respond_id"])) {
$respond_id = $_POST["respond_id"];
if (is_numeric($respond_id)) {
$table_name = $wpdb->prefix . "answers";
$wpdb->delete($table_name, array('id' => $respond_id ));
}
}
}
function abfc_style() {
wp_register_style('abfc_style', plugins_url('style.css',__FILE__ ));
wp_register_script('abfc_script', plugins_url('scripts.js',__FILE__ ));
wp_enqueue_style('abfc_style');
wp_enqueue_script('abfc_script');
}
add_action('wp_footer','abfc_style');
add_action('wp_footer', 'abfc_delete');
add_action('wp_footer', 'abfc_button');