File: /var/www/html/wp-content/themes/ganryujima_new_1602/lib/comments.php
<?php
/**
* 追加の初期化
* ユーザの仮登録データベースがなければ作成する
*/
add_action('wpmem_after_init', 'custom_init');
function custom_init() {
global $wpdb;
// HTML メールを送れるようにする
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
// 仮登録ユーザテーブルが存在しなければ作成する
$interim_user_table_name = $wpdb->prefix . 'interim_user';
if($wpdb->get_var('SHOW TABLES LIKE "' . $interim_user_table_name . '"') != $interim_user_table_name) {
$sql = "CREATE TABLE $interim_user_table_name (
request_id INT NOT NULL AUTO_INCREMENT,
request_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
token VARCHAR(30) NOT NULL,
datas TEXT NOT NULL,
PRIMARY KEY (request_id)
);";
$wpdb->query($sql);
}
// パスワード変更リクエストの保存テーブルの作成
$reset_password_table_name = $wpdb->prefix . 'reset_password';
if($wpdb->get_var('SHOW TABLES LIKE "' . $reset_password_table_name . '"') != $reset_password_table_name) {
$sql = "CREATE TABLE $reset_password_table_name (
request_id INT NOT NULL AUTO_INCREMENT,
request_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
token VARCHAR(30) NOT NULL,
user_id INT NOT NULL,
PRIMARY KEY (request_id)
);";
$wpdb->query($sql);
}
}
function set_html_content_type() {
return 'text/html';
}
/**
* アバターを独自画像に変更する
*/
add_filter('get_avatar', 'get_original_avatar', 10, 5);
function get_original_avatar($avatar, $id_or_email, $size, $default, $alt) {
if ( is_numeric( $id_or_email ) )
$user_id = (int) $id_or_email;
elseif ( is_string( $id_or_email ) && ( $user = get_user_by( 'email', $id_or_email ) ) )
$user_id = $user->ID;
elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) )
$user_id = (int) $id_or_email->user_id;
$profile_image = get_user_meta($user_id, 'profile_image');
if(count($profile_image) > 0 && $profile_image[0] != '') {
$profile_image = $profile_image[0];
} else {
$rand_num = rand(1, 3);
$profile_image = home_url() . '/assets/images/avatar/avatar'.$rand_num.'.svg';
}
$html = '<img src="' . $profile_image . '">';
return $html;
}
/**
* Ajax API 用の送信先 URL をグローバル領域に宣言
*/
add_filter('wp_head', 'define_ajax_url');
function define_ajax_url() {
echo '<script>var ajaxUrl = "' . admin_url('admin-ajax.php') . '";</script>';
}
/**
* コメント完了後に同一ページへリダイレクトする
* また、画像がアップロードされている場合はリダイレクト前に画像を設定する
* 更に、Open Graph のサイト情報がアップロードされていたら保存する
*/
add_filter('comment_post_redirect', 'grjm_comment_redirect', 10, 2);
function grjm_comment_redirect( $location, $comment ) {
$comment->comment_ID;
if(isset($_FILES) && count($_FILES) > 0 && is_uploaded_file($_FILES['comment_image']['tmp_name'])) {
$path = ABSPATH . 'assets/img/comment/' . $comment->comment_ID . '_' . $_FILES['comment_image']['name'];
move_uploaded_file($_FILES['comment_image']['tmp_name'], $path);
$url = home_url() . '/assets/img/comment/' . $comment->comment_ID . '_' . $_FILES['comment_image']['name'];
add_comment_meta($comment->comment_ID, 'comment_image', $url);
}
if($_POST['og_url'] != '' || $_POST['og_title'] != '' || $_POST['og_description'] != '' || $_POST['og_thumbnail'] != '') {
$open_graph = array(
'url' => $_POST['og_url'],
'title' => $_POST['og_title'],
'description' => $_POST['og_description'],
'thumbnail' => $_POST['og_thumbnail']
);
$open_graph = json_encode($open_graph, JSON_UNESCAPED_UNICODE);
add_comment_meta($comment->comment_ID, 'open_graph', $open_graph);
}
return $location;
}
/**
* Ajax で自身のコメントを削除する処理
*/
add_action('wp_ajax_delete_comment', 'grjm_delete_comment');
function grjm_delete_comment() {
if(!is_user_logged_in()) {
return false;
}
if(!isset($_POST['comment_id'])) {
return false;
}
$user_id = get_current_user_id();
$comment = get_comment($_POST['comment_id']);
if($comment == null) {
return false;
}
if($user_id != $comment->user_id) {
return false;
}
$result = wp_delete_comment($comment->comment_ID, true);
return $result;
}
?>