Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('publish_listings', 'vba_update', 10, 1);
- add_action('draft_listings', 'vba_update', 10, 1);
- function vba_update($post_id){
- $args = array(
- 'include' => tb_get_followers($user_id)
- );
- $user_query = new WP_User_Query($args);
- if(!empty($user_query->results)) :
- $post_title = get_the_title( $post_id );
- $post_url = get_permalink( $post_id );
- $subject = 'A post has been updated';
- $message = "A post has been updated on your website:nn";
- $message.= "<a href='". $post_url. "'>" .$post_title. "</a>nn";
- /* foreach($user_query->results as $user) :
- wp_mail($user->data->user_email, $subject, $message);
- endforeach; */
- endif;
- }
- add_action('wp_ajax_update_notification_db' , 'update_notification_db');
- add_action('wp_ajax_nopriv_update_notification_db','update_notification_db');
- function update_notification_db(){
- //This script will accept following three commands as GET parameter and will act on as follows:
- //INSERT_NOTIFICATION - Insert the notification in the [notifications] table
- //FETCH_NOTIFICATIONS - Get all notifications for a user from [notifications] table
- //DELETE_NOTIFICATIONS - Deletes all notifications for a user from [notifications] table
- //SET_IS_VIEWED_FLAG - Call this command from front-end when user has viewed the notification
- $command = "";
- if (isset($_GET['command'])) {
- $command = $_GET['command'];
- }
- $user_id = "";
- if (isset($_GET['user_id'])) {
- $user_id = $_GET['user_id'];
- }
- if (strcmp($user_id,"") == 0) {
- echo "<br>Error: user_id must be specified as GET parameter.";
- exit;
- }
- if (strcmp($command,"") == 0) {
- echo "<br>Sorry, no command specified. Exiting...";
- exit;
- }
- //Open the MySQL database connection here and select the database in which [notifications] table has been created
- global $wpdb;
- $servername = "localhost";
- $username = "";
- $password = "";
- $dbname = "root";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- if (strcmp($command,"INSERT_NOTIFICATION") == 0) {
- $notification_text = "";
- if (isset($_GET['notification_text'])) {
- $notification_text = $_GET['notification_text'];
- }
- $is_viewed = "";
- if (isset($_GET['is_viewed'])) {
- $is_viewed = $_GET['is_viewed'];
- }
- if (strcmp($notification_text,"") == 0) {
- echo "<br>Error: notification_text must be specified as GET parameter.";
- exit;
- }
- if (strcmp($is_viewed,"") == 0) {
- echo "<br>Error: is_viewed must be specified as GET parameter.";
- exit;
- }
- //Insert notification into the table for given user
- $sql = "select * from notifications";
- $result = $conn->query($sql);
- $id = mysql_num_rows($result);
- $id = $id + 1;
- $creation_date_time = date("Y:m:d H:i:s");
- $view_date_time = date("Y:m:d H:i:s");
- $is_viewed = 'NO';
- $sql = "INSERT INTO `notifications`(`id`, `creation_date_time`, `view_date_time`, `user_id`, `notification_text`, `is_viewed`) VALUES ('".$id."','".$creation_date_time."','".$view_date_time."','".$user_id."','".$notification_text."','".$is_viewed."')";
- $result = $conn->query($sql);
- $conn->close();
- exit;
- }
- if (strcmp($command,"FETCH_NOTIFICATIONS") == 0) {
- //Select all notifications for a given user_id and return in JSON format
- $sql = "select * from notifications where user_id = '".$user_id."' and is_viewed = NO";
- $res = mysqli_query($con,$sql);
- $result = array();
- while($row = mysqli_fetch_array($res)) {
- array_push($result,array('id'=>$row[0],'creation_date_time'=>$row[1],'view_date_time'=>$row[2],'user_id'=>$row[3],'notification_text'=>$row[4],'is_viewed'=>$row[5]));
- }
- $conn->close();
- echo json_encode(array("result"=>$result_array));
- //Here you can format all notifications into well-organized HTML code just like Facebook in order to present in nice way
- //For example purpose , it is being returned in JSON format
- exit;
- }
- if (strcmp($command,"DELETE_NOTIFICATIONS") == 0) {
- //Delete all notifications for a given user_id
- $sql = "delete from notifications where use_id = '".$user_id."'";
- $res = mysqli_query($con,$sql);
- $conn->close();
- exit;
- }
- if (strcmp($command,"SET_IS_VIEWED_FLAG") == 0) {
- //Update here is_viewed flag to YES for notification and user_id
- exit;
- }
- }
- <script type="text/javascript" charset="utf-8">
- function show_notifications() {
- var command = '&command=FETCH_NOTIFICATIONS&user_id=1';
- $(document).ready(function() {
- $.ajax({
- url: ajaxurl,
- cache: false,
- data: {action: 'update_notification_db' + command},
- success: function(data) {
- console.log(data);
- //$("#notification_results").append(html);
- //Change the content of the DIV in User's profile page where notifications will be displayed
- }
- });
- });
- }
- function startTimer() {
- //call show_notifications
- show_notifications();
- //then start interval
- setInterval(show_notifications, 30000); //Refresh after 30 seconds
- }
- </script>
- <script>
- function create_notifications() {
- var command = '&command=INSERT_NOTIFICATION&user_id=1&creation_date_time=201610101200&view_date_time=2016101012000¬ification_text=Hello&is_viewed=NO';
- $(document).ready(function() {
- $.ajax({
- url: ajaxurl,
- cache: false,
- data: {action: 'update_notification_db' + command},
- success: function(data){
- console.log(data);
- }
- });
- });
- }
- </script>
Add Comment
Please, Sign In to add comment