Guest User

Untitled

a guest
Jul 4th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. add_action('publish_listings', 'vba_update', 10, 1);
  2. add_action('draft_listings', 'vba_update', 10, 1);
  3. function vba_update($post_id){
  4.  
  5.  
  6. $args = array(
  7. 'include' => tb_get_followers($user_id)
  8. );
  9. $user_query = new WP_User_Query($args);
  10.  
  11. if(!empty($user_query->results)) :
  12.  
  13. $post_title = get_the_title( $post_id );
  14. $post_url = get_permalink( $post_id );
  15. $subject = 'A post has been updated';
  16. $message = "A post has been updated on your website:nn";
  17. $message.= "<a href='". $post_url. "'>" .$post_title. "</a>nn";
  18.  
  19.  
  20. /* foreach($user_query->results as $user) :
  21.  
  22. wp_mail($user->data->user_email, $subject, $message);
  23.  
  24. endforeach; */
  25.  
  26. endif;
  27.  
  28. }
  29.  
  30.  
  31. add_action('wp_ajax_update_notification_db' , 'update_notification_db');
  32. add_action('wp_ajax_nopriv_update_notification_db','update_notification_db');
  33. function update_notification_db(){
  34.  
  35. //This script will accept following three commands as GET parameter and will act on as follows:
  36. //INSERT_NOTIFICATION - Insert the notification in the [notifications] table
  37. //FETCH_NOTIFICATIONS - Get all notifications for a user from [notifications] table
  38. //DELETE_NOTIFICATIONS - Deletes all notifications for a user from [notifications] table
  39. //SET_IS_VIEWED_FLAG - Call this command from front-end when user has viewed the notification
  40.  
  41. $command = "";
  42.  
  43. if (isset($_GET['command'])) {
  44.  
  45. $command = $_GET['command'];
  46.  
  47. }
  48.  
  49. $user_id = "";
  50.  
  51. if (isset($_GET['user_id'])) {
  52.  
  53. $user_id = $_GET['user_id'];
  54.  
  55. }
  56.  
  57. if (strcmp($user_id,"") == 0) {
  58.  
  59. echo "<br>Error: user_id must be specified as GET parameter.";
  60.  
  61. exit;
  62. }
  63.  
  64. if (strcmp($command,"") == 0) {
  65.  
  66. echo "<br>Sorry, no command specified. Exiting...";
  67.  
  68. exit;
  69.  
  70. }
  71.  
  72. //Open the MySQL database connection here and select the database in which [notifications] table has been created
  73. global $wpdb;
  74. $servername = "localhost";
  75. $username = "";
  76. $password = "";
  77. $dbname = "root";
  78.  
  79. // Create connection
  80. $conn = new mysqli($servername, $username, $password, $dbname);
  81.  
  82. // Check connection
  83. if ($conn->connect_error) {
  84.  
  85. die("Connection failed: " . $conn->connect_error);
  86.  
  87. }
  88.  
  89. if (strcmp($command,"INSERT_NOTIFICATION") == 0) {
  90.  
  91. $notification_text = "";
  92.  
  93. if (isset($_GET['notification_text'])) {
  94.  
  95. $notification_text = $_GET['notification_text'];
  96.  
  97. }
  98.  
  99. $is_viewed = "";
  100.  
  101. if (isset($_GET['is_viewed'])) {
  102.  
  103. $is_viewed = $_GET['is_viewed'];
  104.  
  105. }
  106.  
  107. if (strcmp($notification_text,"") == 0) {
  108.  
  109. echo "<br>Error: notification_text must be specified as GET parameter.";
  110.  
  111. exit;
  112.  
  113. }
  114.  
  115. if (strcmp($is_viewed,"") == 0) {
  116.  
  117. echo "<br>Error: is_viewed must be specified as GET parameter.";
  118.  
  119. exit;
  120.  
  121. }
  122.  
  123. //Insert notification into the table for given user
  124. $sql = "select * from notifications";
  125.  
  126. $result = $conn->query($sql);
  127.  
  128. $id = mysql_num_rows($result);
  129.  
  130. $id = $id + 1;
  131.  
  132. $creation_date_time = date("Y:m:d H:i:s");
  133.  
  134. $view_date_time = date("Y:m:d H:i:s");
  135.  
  136. $is_viewed = 'NO';
  137.  
  138. $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."')";
  139.  
  140. $result = $conn->query($sql);
  141.  
  142. $conn->close();
  143.  
  144. exit;
  145.  
  146. }
  147.  
  148. if (strcmp($command,"FETCH_NOTIFICATIONS") == 0) {
  149. //Select all notifications for a given user_id and return in JSON format
  150. $sql = "select * from notifications where user_id = '".$user_id."' and is_viewed = NO";
  151.  
  152. $res = mysqli_query($con,$sql);
  153.  
  154. $result = array();
  155.  
  156. while($row = mysqli_fetch_array($res)) {
  157.  
  158. 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]));
  159.  
  160. }
  161.  
  162. $conn->close();
  163.  
  164. echo json_encode(array("result"=>$result_array));
  165.  
  166. //Here you can format all notifications into well-organized HTML code just like Facebook in order to present in nice way
  167.  
  168. //For example purpose , it is being returned in JSON format
  169.  
  170. exit;
  171.  
  172. }
  173.  
  174. if (strcmp($command,"DELETE_NOTIFICATIONS") == 0) {
  175. //Delete all notifications for a given user_id
  176. $sql = "delete from notifications where use_id = '".$user_id."'";
  177.  
  178. $res = mysqli_query($con,$sql);
  179.  
  180. $conn->close();
  181.  
  182. exit;
  183.  
  184. }
  185.  
  186. if (strcmp($command,"SET_IS_VIEWED_FLAG") == 0) {
  187.  
  188. //Update here is_viewed flag to YES for notification and user_id
  189. exit;
  190.  
  191. }
  192.  
  193. }
  194.  
  195. <script type="text/javascript" charset="utf-8">
  196. function show_notifications() {
  197.  
  198. var command = '&command=FETCH_NOTIFICATIONS&user_id=1';
  199.  
  200. $(document).ready(function() {
  201. $.ajax({
  202. url: ajaxurl,
  203. cache: false,
  204. data: {action: 'update_notification_db' + command},
  205. success: function(data) {
  206. console.log(data);
  207. //$("#notification_results").append(html);
  208. //Change the content of the DIV in User's profile page where notifications will be displayed
  209. }
  210. });
  211.  
  212. });
  213.  
  214.  
  215. }
  216.  
  217. function startTimer() {
  218. //call show_notifications
  219. show_notifications();
  220. //then start interval
  221. setInterval(show_notifications, 30000); //Refresh after 30 seconds
  222. }
  223. </script>
  224.  
  225. <script>
  226. function create_notifications() {
  227.  
  228. var command = '&command=INSERT_NOTIFICATION&user_id=1&creation_date_time=201610101200&view_date_time=2016101012000&notification_text=Hello&is_viewed=NO';
  229.  
  230. $(document).ready(function() {
  231. $.ajax({
  232. url: ajaxurl,
  233. cache: false,
  234. data: {action: 'update_notification_db' + command},
  235. success: function(data){
  236. console.log(data);
  237. }
  238. });
  239. });
  240. }
  241.  
  242.  
  243. </script>
Add Comment
Please, Sign In to add comment