Skip to content

Instantly share code, notes, and snippets.

@Mxrck
Created November 9, 2017 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mxrck/220420d5f531b5f9b39c5a51930b82cd to your computer and use it in GitHub Desktop.
Save Mxrck/220420d5f531b5f9b39c5a51930b82cd to your computer and use it in GitHub Desktop.
Helper de notificaciones para wordpress
<?php
/**
* notificationHelper.php
*
* @author Mxrck
* @version 1.0
*
* Una sencilla clase creada para facilitar el envío de notificaciones
* en el backend de Wordpress, ya sea para temas o plugins, con la
* posibilidad de enviar emails.
*
* http://marcomaldonado.com
*/
namespace mxrck\tools;
class NotificationHelper {
private static $instance = null;
private $email = false;
private $sendEmails = true;
private $notifications = array();
public static $MSG_ERROR = 'notice-error';
public static $MSG_WARNING = 'notice-warning';
public static $MSG_SUCCESS = 'notice-success';
public static $MSG_INFO = 'notice-info';
private function __construct($config)
{
$this->init($config);
}
private function init($config = array())
{
if (is_array($config)) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
}
$this->hooks();
return $this;
}
public static function getInstance($config = array())
{
if (self::$instance == null) {
self::$instance = new NotificationHelper($config);
}
return self::$instance;
}
private function hooks()
{
add_action( 'admin_notices', array($this, 'showNotifications') );
return $this;
}
public function setEmail($email)
{
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->email = $email;
}
return $this;
}
public function setSendEmails($send)
{
$this->sendEmails = is_bool($send) ? $send : false;
return $this;
}
public function showNotifications()
{
foreach($this->notifications as $notification) {
printf(
'<div class="notice %1$s"><p>%2$s</p></div>',
$notification->type,
$notification->message
);
}
$this->forceSendEmails();
return $this;
}
public function forceSendEmails()
{
foreach($this->notifications as $notification) {
$this->sendEmail($notification);
}
return $this;
}
private function sendEmail($notification)
{
if (
$this->sendEmails !== false &&
$this->email !== false &&
$notification &&
isset($notification->email) &&
$notification->email === true &&
isset($notification->message) &&
isset($notification->type) &&
is_array($notification->emailOptions)
) {
$body = empty($notification->emailOptions['body']) ? $notification->message : $notification->emailOptions['body'];
wp_mail(
$this->email,
$notification->emailOptions['subject'],
$body,
$notification->emailOptions['headers'],
$notification->emailOptions['attachments']
);
}
return $this;
}
private function prepareEmailOptions($emailOptions = array(), $type = '')
{
$email = array(
'headers' => array(),
'attachments' => array(),
'subject' => __('Ésta es una notificación de tu sistema en Wordpress', 'notifications'),
'body' => ''
);
$email['headers'] = isset($emailOptions['headers']) ? $emailOptions['headers'] : $email['headers'];
$email['attachments'] = isset($emailOptions['attachments']) ? $emailOptions['attachments'] : $email['attachments'];
$email['subject'] = isset($emailOptions['subject']) ? $emailOptions['subject'] : $email['subject'];
$email['body'] = isset($emailOptions['body']) ? $emailOptions['body'] : $email['body'];
$email['subject'] = $this->prependSubject($type).$email['subject'];
return $email;
}
private function prependSubject($type)
{
$preppend = '';
switch ($type) {
case NotificationHelper::$MSG_INFO:
$preppend = __('[INFORMACIÓN]', 'notifications').' ';
break;
case NotificationHelper::$MSG_ERROR:
$preppend = __('[ERROR]', 'notifications').' ';
break;
case NotificationHelper::$MSG_WARNING:
$preppend = __('[ATENCIÓN]', 'notifications').' ';
break;
case NotificationHelper::$MSG_SUCCESS:
$preppend = __('[ÉXITO]', 'notifications').' ';
break;
}
return $preppend;
}
public function addNotification($message, $type = 'notice-info', $email = false, $emailOptions = array())
{
if (
$type != NotificationHelper::$MSG_ERROR &&
$type != NotificationHelper::$MSG_WARNING &&
$type != NotificationHelper::$MSG_SUCCESS &&
$type != NotificationHelper::$MSG_INFO
) {
$type = 'notice-info';
}
$notification = new \stdClass();
$notification->message = $message;
$notification->type = $type;
$notification->email = is_bool($email) ? $email : false;
$notification->emailOptions = $this->prepareEmailOptions($emailOptions, $type);
array_push(
$this->notifications,
$notification
);
return $this;
}
}
@Mxrck
Copy link
Author

Mxrck commented Nov 9, 2017

<?php  
/*
...
*/

// Cargando librerías
require_once 'autoload.php'; // Carga el archivo notificationHelper.php

use \mxrck\tools\NotificationHelper;

add_action('init', 'initPlugin');

function initPlugin() {  
$notificator = NotificationHelper::getInstance();
$notificator->setEmail('testing@email.com');
$notificator
    ->addNotification('Mensaje de información', NotificationHelper::$MSG_INFO)
    ->addNotification('Mensaje de error', NotificationHelper::$MSG_ERROR)
    ->addNotification('Mensaje de éxtio', NotificationHelper::$MSG_SUCCESS)
    ->addNotification('Mensaje de precaución', NotificationHelper::$MSG_WARNING)
    ->addNotification('Mensaje de error con envío de email', NotificationHelper::$MSG_ERROR, true); // Envía un email
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment