[FRAMEWORK] Xây dựng page notifications Phần 2 (ok)

1. Tạo icon notifications trên header :)

<?php if(!is_user_logged_in()) wp_redirect( SITE_URL."login" );?>
<!DOCTYPE html>
<html <?php language_attributes(); ?> >
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="profile" href="http://gmpg.org/xfn/11"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" href="https://unpkg.com/swiper@7/swiper-bundle.min.css"/>
	<script src="<?php echo THEME_URL ?>assets/libs/js/jquery.min.js?ver=3.5.1"></script>
    <script
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB8-ypkZ-83OMzbMUGrJWa2v-XBIqQWHdo&libraries=places">
    </script>
    <?php wp_head(); ?>
</head>
<body <?php body_class() ?> >
<div id="page-load" style="opacity: 0; visibility: hidden;"><span class="loader"> </span></div>
<div id="wrapper">
	<header>
        <?php if ( is_front_page() ){
				//get_template_part( 'template-parts/header/header', 'home' );
			} elseif ( is_page( 'community') ){
				get_template_part( 'template-parts/header/header', 'tab' );
			} elseif ( is_page('account') ){
				if((isset($_GET["action"]) && $_GET['action'] != "view-event") or !isset($_GET["action"])){
					get_template_part( 'template-parts/header/header', 'account' );
				}
			}elseif(is_page(["directory","market","news","events"])){
				if(!isset($_GET["appt"])){
					get_template_part( 'template-parts/header/header', 'tab' );
				}
			}elseif(is_singular("post")){
				return;
			} else {
				get_template_part( 'template-parts/header/header', 'back' );
			}
		?>
	</header>
	<main id="main">

C:\xampp\htdocs\wordpress8\wp-content\themes\addframwork\template-parts\header\header-tab.php

<?php 
  $total_notify = (int) get_user_meta( get_current_user_id(), 'total_notify', true );
?>
<nav class="tab-header navbar p-0">
  <div class="container-fluid position-relative text-white d-flex align-items-center justify-content-between">
    <div class="title-logo flex-fill"> <?php wp_title(''); ?> </div>
    <div class="d-flex align-items-center justify-content-end">
      <a href="<?php echo SITE_URL ?>search/?appt=N" class="btn d-flex align-items-center">
        <svg width="24" height="24">
          <use href="#icon-search"></use>
        </svg>
      </a>
      <a href="<?php echo SITE_URL ?>notifications/?appt=N" class="btn-notify position-relative btn d-flex align-items-center p-0">
        <svg width="46" height="47"><use href="#icon-bell"></use></svg>
        <?php if ($total_notify): ?>
        <span class="push_notify position-absolute rounded-circle bg-primary text-white font-weight-600 d-flex align-items-center justify-content-center">
          <?php echo $total_notify; ?>
        </span>
        <?php endif; ?>
      </a>
    </div>
  </div>
</nav>

2. Sau khi ấn icon notifications chuyển đến trang notifications

Chú ý: Update thông báo về 0 khi đó trở về trang community sẽ không còn nhìn thấy thôn báo nữa.

C:\xampp\htdocs\wordpress8\wp-content\themes\addframwork\page-notifications.php

<?php get_header();?>
<?php
global $bj_controller;
$current_user_id       = get_current_user_id();
$groups_join_arr       = all_groups_have_joined($current_user_id);
$groups_join_str       = join(",", $groups_join_arr);
$groups_admin_arr      = all_groups_have_admin($current_user_id);
$groups_admin_str      = join(",", $groups_admin_arr);
$list_groups_mute_arr  = json_decode(get_user_meta($current_user_id, 'list_groups_mute', true), true);
$notification_user_arr = $bj_controller->Model("notification")->get_list_notification_by_user_id($current_user_id, $groups_join_str, $groups_admin_str);
// update_user_meta($current_user_id, 'total_notify', 0);
?>
<div class="page-notifications container mt-4">
  <div class="box-notifi-list">
    <?php if ($notification_user_arr): ?>
      <?php
        foreach ($notification_user_arr as $value) {
          $notifi_id       = $value["id"];
          $type            = $value["type"];
          $id_user_inviter = $value["from_"];
          $user            = get_userdata($id_user_inviter);
          $name            = $user->display_name;
          $avatar          = get_url_avatar($id_user_inviter);
          $link_account    = SITE_URL . "account/?id={$id_user_inviter}&appt=N";
          $status          = $value["status"];
          //$time = $value["time_created"];
          $type_link = get_permalink($value["content"]) . '?appt=N';
          if ($type == 'group_new_post') {
            if (@in_array($value["to_"], $list_groups_mute_arr)) {
              continue;
            }
          }
          switch ($type) {
          case 'invite_group':
            $type_link  = SITE_URL . "community/?action=view-group&id={$value["content"]}&appt=N";
            $group_name = get_term($value["content"])->name;
            $message    = sprintf(__("<strong>%s</strong> invited you to follow <strong>%s</strong>", "umm"), $name, $group_name);
            break;
          case 'has_like':
            $message = sprintf(__("<strong>%s</strong> liked your post", "umm"), $name);
            break;
          case 'has_comment':
            $message = sprintf(__("<strong>%s</strong> commented on your post", "umm"), $name);
            break;
          case 'has_reply':
            $message = sprintf(__("<strong>%s</strong> replied to your comment", "umm"), $name);
            break;
          case 'has_follow':
            $message   = sprintf(__("<strong>%s</strong> followed you", "umm"), $name);
            $type_link = $link_account;
            break;
          case 'group_new_post':
            $group_name = get_term($value["to_"])->name;
            $type_link  = SITE_URL . "community/?action=view-group&id={$value["to_"]}&appt=N&to#post_{$value["content"]}";
            $message    = sprintf(__("<strong>%s</strong> posted a new post in the group <strong>%s</strong>", "umm"), $name, $group_name);
            break;
          case 'new_member':
            $group_name = get_term($value["to_"])->name;
            $type_link  = SITE_URL . "community/?action=view-group&id={$value["to_"]}&appt=N";
            $message    = sprintf(__("<strong>%s</strong> just joined the group <strong>%s</strong>", "umm"), $name, $group_name);
            break;
          }
      ?>
        <div data-id="<?php echo $notifi_id; ?>" class="box-notifi__item d-flex align-items-center justify-content-between mb-3">
          <div class="box-notifi__item__avatar">
            <a href="<?php echo $link_account ?>" class="d-block" style="width:50px;">
              <img src="<?php echo $avatar ?>" class="rounded-circle">
            </a>
          </div>
          <div class="box-notifi__item__content flex-fill ml-2">
            <a href="<?php echo $type_link; ?>" class="d-block ontent__top">
              <?php echo $message; ?>
            </a>
            <?php if ($type == 'invite_group'): ?>
            <div class="content__bottom d-flex align-items-center justify-content-around mt-1">
                <button type="button" data-groupId="<?php echo $value["content"]; ?>" data-id="<?php echo $notifi_id; ?>" class="notifi-btn notifi-btn-accept btn bg-primary text-white font-weight-600">
                  <svg width="20" height="20" class="mr-1">
                    <use href="#icon-accept"></use>
                  </svg>
                  <?php _e('Accept', 'umm');?>
                </button>
                <button type="button" data-id="<?php echo $notifi_id; ?>" class="notifi-btn notifi-btn-decline btn font-weight-600"> <?php _e('Decline', 'umm');?></button>
            </div>
            <?php endif;?>
          </div>
        </div>
      <?php } ?>
    <?php endif;?>
  </div>
</div>
<?php get_footer();?>

C:\xampp\htdocs\wordpress8\wp-content\themes\addframwork\framework\models\notification.php

<?php
class BJ_notification_Model extends Pv_Controller {
  private $table_notification_name = 'notification';
  public function create_notification($input) {
    global $wpdb;
    $table = $wpdb->prefix . $this->table_notification_name;
    $data  = array(
      "from_"   => $input["from"],
      "to_"     => $input["to"],
      "type"    => $input["type"],
      "content" => $input["content"],
    );
    $format = array('%d', '%d', '%s', '%s');
    $wpdb->insert($table, $data, $format);
    return $wpdb->insert_id;
  }
  public function del_notification_exact($input) {
    global $wpdb;
    $table  = $wpdb->prefix . $this->table_notification_name;
    $result = $wpdb->delete($table, array(
      "from_"   => $input["from"],
      "to_"     => $input["to"],
      "type"    => $input["type"],
      "content" => $input["content"],
    ), array('%d', '%s', '%s', '%s'));
    return $result;
  }
  public function get_list_notification_by_user_id($user_id, $groups_join, $groups_admin=0, $limit = 10, $offset = 0) {
    global $wpdb;
    $table = $wpdb->prefix . $this->table_notification_name;
    return $wpdb->get_results($wpdb->prepare(
      "SELECT * FROM  {$table}
      WHERE `to_` = %d
      OR (`type` = 'group_new_post' AND `to_` IN ({$groups_join}) )
      OR (`type` = 'new_member' AND `to_` IN ({$groups_admin}) )
      ORDER BY id DESC LIMIT %d,%d", $user_id, $offset, $limit), ARRAY_A);
  }
}

Last updated