my code stock.com

Pascal Bajorat

PrimusNote Notification Plugin
by Pascal Bajorat

Snippet options

Download: Download snippet as primusnote-notification-plugin.php.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

<?php
/*
Plugin Name: PrimusNote Email Notification
Plugin URI: https://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: 1.0.0
Author: pascalbajorat
Author URI: https://www.pascal-bajorat.com/
License: GPL2

Copyright (c) 2017 by Pascal-Bajorat.com.
*/

if( !class_exists('pn_email_notification') )
{
	class pn_email_notification
	{
		public function init()
		{
			add_action(
				'comment_post',
				array(
					__CLASS__,
					'email_notification'
				),
				99,
				3
			);
		}

		public static function email_notification( $comment_ID, $comment_approved, $commentdata )
		{
			if( ! $comment_approved ) {
				return;
			}

			$comment_content = $commentdata->comment_content;

			$post_id = $commentdata->comment_post_ID;
			$post = get_post($post_id);

			$comment_author_user_id = $commentdata->user_ID;
			$comment_author = get_userdata($comment_author_user_id);
			$comment_author_nicename = $comment_author->user_nicename;

			$get_users_to_notify = self::get_users_to_notify(
				$post_id,
				array(
					// Falls mehr Daten benötigt werden, hier übergeben
				)
			);

			if( is_array($get_users_to_notify) ) {
				foreach($get_users_to_notify as $user) {
					$user_data = get_userdata($user);

					if( !$user_data )
						continue;

					$wp_mail = self::mail(
						$user_data->user_email,
						__('New Comment', 'primusnote').': '.$post->post_title,
						$comment_author_nicename.":\n\r".$comment_content."\n\r\n\r". get_comment_link($comment_ID)
					);

					if( $wp_mail ) {
						// email sent
					} else {
						// email failed
					}
				}
			}
		}

		/**
		 * Lädt die Liste aller User die benachrichtigt werden müssen
		 *
		 * @param int $post_id
		 * @param array $data
		 *
		 * @return array|bool
		 */
		public static function get_users_to_notify( $post_id, $data=array() )
		{
			// $data ist aktuell nicht in verwendung
			// $data kann für die übergabe weiterer Infos verwendet werden um die Funktion zu erweitern

			$get_comments = get_comments(array(
				'post_id' => $post_id
			));

			$users_to_notify = array();

			// User ID von Nutzer laden, welche bereits ein Kommentar hinterlassen haben
			if( $get_comments ) {
				foreach( $get_comments as $comment ) {
					array_push($users_to_notify, $comment->user_id);
				}
			}

			// User ID laden von Nutzern die der Aufgabe zugewiesen wurden
			$get_task_related_users = self::get_task_related_users( $post_id, $data );

			if( is_array($get_task_related_users) ) {
				array_merge($users_to_notify, $get_task_related_users);
			}

			if( count($users_to_notify) >= 1 ) {
				return array_unique($users_to_notify);
			} else {
				return false;
			}
		}

		/**
		 * Hier Daten laden, von PN Usern die zugewiesen wurden
		 *
		 * @param int $post_id
		 * @param array $data
		 *
		 * @return bool
		 */
		public static function get_task_related_users( $post_id, $data=array() )
		{

			// Hier User-IDs von Usern laden, die noch relevant für die Benachrichtigung sind,
			// z.B. User die für eine Aufgabe als Zuständig eingetragen sind, aber bisher noch kein Kommentar geschrieben haben
			// als return Wert wird ein Array mit den Users-IDs erwartet oder false

			return false;
		}
		
		public static function mail( $to, $subject, $message, $headers = '', $attachments = array() )
		{
			return wp_mail( $to, $subject, $message, $headers, $attachments );
		}
	}
}


add_action(
	'plugins_loaded',
	array(
		'pn_email_notification',
		'init'
	)
);

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.