Add upstream plugins

Signed-off-by: Adrian Nöthlich <git@promasu.tech>
This commit is contained in:
2019-10-25 22:42:20 +02:00
parent 5d3c2ec184
commit 290736650a
1186 changed files with 302577 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
<?php
/**
* Abstract class for notifications.
*
* @author Time.ly Network Inc.
* @since 2.0
*
* @package AI1EC
* @subpackage AI1EC.Notification
*/
abstract class Ai1ec_Notification extends Ai1ec_Base {
/**
* @var string The message to send.
*/
protected $_message;
/**
* @var array A list of recipients.
*/
protected $_recipients = array();
/**
* This function performs the actual sending of the message.
*
* Must be implemented in child classes.
*
* @return bool Success.
*/
abstract public function send();
}

View File

@@ -0,0 +1,241 @@
<?php
/**
* Admin notifications. Dispatchment is delayed.
*
* @author Time.ly Network Inc.
* @since 2.0
*
* @package AI1EC
* @subpackage AI1EC.Notification
*/
class Ai1ec_Notification_Admin extends Ai1ec_Notification {
/**
* @var string Option key for messages storage.
*/
const OPTION_KEY = 'ai1ec_admin';
/**
* @var string Name of messages for all admins.
*/
const RCPT_ALL = 'all';
/**
* @var string Name of network-admin only messages.
*/
const RCPT_NETWORK = 'network_admin_notices';
/**
* @var string Name of admin only messages.
*/
const RCPT_ADMIN = 'admin_notices';
/**
* @var array Map of messages to be rendered.
*/
protected $_message_list = array();
/**
* Add message to store.
*
* @param string $message Actual message.
* @param string $class Message box class.
* @param int $importance Optional importance parameter for the message.
* Levels of importance are as following:
* - 0 - messages limited to Ai1EC pages;
* - 1 - messages limited to [0] and Plugins/Updates pages;
* - 2 - messages limited to [1] and Dashboard.
* @param array $recipients List of message recipients.
* @param bool $persistent If set to true, messages needs to be dismissed by user.
*
* @return bool Success.
*/
public function store(
$message,
$class = 'updated',
$importance = 0,
array $recipients = array( self::RCPT_ADMIN ),
$persistent = false
) {
$this->retrieve();
$entity = compact( 'message', 'class', 'importance', 'persistent' );
$msg_key = sha1( json_encode( $entity ) );
$entity['msg_key'] = $msg_key;
if ( isset( $this->_message_list['_messages'][$msg_key] ) ) {
return true;
}
$this->_message_list['_messages'][$msg_key] = $entity;
foreach ( $recipients as $rcpt ) {
if ( ! isset( $this->_message_list[$rcpt] ) ) {
continue;
}
$this->_message_list[$rcpt][$msg_key] = $msg_key;
}
return $this->write();
}
/**
* Replace database representation with in-memory list version.
*
* @return bool Success.
*/
public function write() {
return $this->_registry->get( 'model.option' )
->set( self::OPTION_KEY, $this->_message_list );
}
/**
* Update in-memory list from data store.
*
* @return Ai1ec_Notification_Admin Instance of self for chaining.
*/
public function retrieve() {
static $default = array(
'_messages' => array(),
self::RCPT_ALL => array(),
self::RCPT_NETWORK => array(),
self::RCPT_ADMIN => array(),
);
$this->_message_list = $this->_registry->get( 'model.option' )
->get( self::OPTION_KEY, null );
if ( null === $this->_message_list ) {
$this->_message_list = $default;
} else {
$this->_message_list = array_merge(
$default,
$this->_message_list
);
}
return $this;
}
/**
* Display messages.
*
* @wp_hook network_admin_notices
* @wp_hook admin_notices
*
* @return bool Update status.
*/
public function send() {
$this->retrieve();
$destinations = array( self::RCPT_ALL, current_filter() );
$modified = false;
foreach ( $destinations as $dst ) {
if ( ! empty( $this->_message_list[$dst] ) ) {
foreach ( $this->_message_list[$dst] as $key ) {
if (
isset( $this->_message_list['_messages'][$key] )
) {
$this->_render_message(
$this->_message_list['_messages'][$key]
);
if (
! isset( $this->_message_list['_messages'][$key]['persistent'] ) ||
false === $this->_message_list['_messages'][$key]['persistent']
) {
unset( $this->_message_list['_messages'][$key] );
unset( $this->_message_list[$dst][$key] );
}
}
}
$modified = true;
}
}
if ( ! $modified ) {
return false;
}
return $this->write();
}
/**
* Delete a notice from ajax call.
*
*/
public function dismiss_notice() {
$key = $_POST['key'];
foreach ( $this->_message_list as $dest ) {
if ( isset( $this->_message_list[$dest][$key] ) ) {
unset( $this->_message_list[$dest][$key] );
}
}
$this->write();
}
protected function _render_message( array $entity ) {
$importance = 0;
if ( isset( $entity['importance'] ) ) {
$importance = ( (int)$entity['importance'] ) % 3;
}
if ( $this->are_notices_available( $importance ) ) {
static $theme = null;
if ( null === $theme ) {
$theme = $this->_registry->get( 'theme.loader' );
}
$entity['text_label'] = apply_filters(
'ai1ec_notification_label',
Ai1ec_I18n::__( 'All-in-One Event Calendar' )
);
$entity['text_dismiss_button'] = Ai1ec_I18n::__( 'Got it dismiss this' );
$file = $theme->get_file(
'notification/admin.twig',
$entity,
true
);
$file->render();
}
}
/**
* Check whereas our notices should be displayed on this page.
*
* Limits notices to Ai1EC pages and WordPress "Plugins", "Updates" pages.
* Important notices are also displayable in WordPress "Dashboard".
* Levels of importance (see $importance) are as following:
* - 0 - messages limited to Ai1EC pages;
* - 1 - messages limited to [0] and Plugins/Updates pages;
* - 2 - messages limited to [1] and Dashboard.
*
* @param int $importance The level of importance. See above for details.
*
* @return bool Availability
*/
public function are_notices_available( $importance ) {
// In CRON `get_current_screen()` is not present
// and we wish to have notice on all "our" pages
$acl = $this->_registry->get( 'acl.aco' );
if ( $acl->is_all_events_page() || $acl->are_we_editing_our_post() ) {
return true;
}
if ( $importance < 1 ) {
return false;
}
$screen = null;
if ( is_callable( 'get_current_screen' ) ) {
$screen = get_current_screen();
}
$allow_on = array(
'plugins',
'update-core',
);
if ( $importance > 1 ) {
$allow_on[] = 'dashboard';
}
if (
is_object( $screen ) &&
isset( $screen->id ) &&
in_array( $screen->id, $allow_on )
) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,157 @@
<?php
/**
* Concrete implementation for email notifications.
*
* @author Time.ly Network Inc.
* @since 2.0
* @instantiator new
* @package AI1EC
* @subpackage AI1EC.Notification
*/
class Ai1ec_Email_Notification extends Ai1ec_Notification {
/**
* @var string
*/
private $_subject;
/**
* @var array
*/
private $_translations = array();
private $_replyTo;
/**
* @param array: $translations
*/
public function set_translations( array $translations ) {
$this->_translations = $translations;
}
public function __construct(
Ai1ec_Registry_Object $registry,
$message,
array $recipients,
$subject,
array $replyTo = null
) {
parent::__construct( $registry );
$this->_message = $message;
$this->_recipients = $recipients;
$this->_subject = $subject;
$this->_replyTo = $replyTo;
}
public function send( $headers = null ) {
$this->_parse_text();
$is_plain_text = true;
if ( null !== $headers ) {
foreach ( $headers as $key => $value ) {
if ( 0 === strcasecmp( "content-type", $key ) &&
0 === strcasecmp( "text/html", $value ) ) {
$is_plain_text = false;
break;
}
}
}
$is_mandril_active = apply_filters ( 'ai1ec_is_mandril_active', null );
if ( $is_plain_text ) {
$nl2br_handler = array( $this, 'mandrill_nl2br' );
add_filter( 'mandrill_nl2br', $nl2br_handler );
if ( $is_mandril_active ) {
$double_line_break_handler = array( $this, 'convert_single_to_double_line_break' );
add_filter( 'wp_mail', $double_line_break_handler );
}
}
$phpmailer_init_handler = array( $this, 'phpmailer_init_mandril' );
add_action( 'phpmailer_init', $phpmailer_init_handler );
$new_mail_send_failed = array( $this, 'new_mail_send_failed' );
add_filter( 'wp_mail_failed', $new_mail_send_failed );
$new_mail_from_name = array( $this, 'new_mail_from_name' );
add_filter( 'wp_mail_from_name', $new_mail_from_name );
$result = wp_mail( $this->_recipients, $this->_subject, $this->_message, $headers );
remove_filter( 'wp_mail_failed', $new_mail_send_failed );
remove_filter( 'wp_mail_from_name', $new_mail_from_name );
remove_action( 'phpmailer_init', $phpmailer_init_handler );
if ( $is_plain_text ) {
remove_filter( 'mandrill_nl2br', $nl2br_handler );
if ( $is_mandril_active ) {
remove_filter( 'wp_mail', $double_line_break_handler );
}
}
return $result;
}
public function new_mail_from_name() {
return get_bloginfo( 'name' );
}
public function new_mail_from_email() {
if ( empty( $this->_replyTo ) ) {
return $this->_registry->get( 'model.settings' )
->get( 'fes_notification_email', get_bloginfo( 'admin_email' ) );
} else {
return $this->_replyTo[0];
}
}
/**
* Handle the wp_mail_failed hook to log the error
*/
public function new_mail_send_failed( $error = null) {
if ( null != $error && is_wp_error( $error ) ) {
error_log( 'wp_mail failed, code: %d, message: %s', $error->get_error_code(), $error->get_error_message() );
} else {
error_log( 'wp_mail failed, unknow error' );
}
}
public function phpmailer_init_mandril( $phpmailer ) {
if ( method_exists( $phpmailer, 'ClearReplyTos' ) ) {
$phpmailer->ClearReplyTos();
}
$phpmailer->addReplyTo( $this->new_mail_from_email(), $this->new_mail_from_name() );
}
/**
* Handle the wp_mail_filter when sending Plain texts emails and Mandril
* is used to send notifications.
*/
public function convert_single_to_double_line_break( $atts = null ) {
//As MC-AutoHtml is set to true, mandril will convert the text/plain to generate the text/html
//this conversion will Ignores the line break (\n) from the text message. The only way to keep
//the lines breaks is have double line breaks as instructed by mandril twiter account here:
//https://twitter.com/mandrillapp/status/397377474541010944
if ( isset( $atts['message'] ) && false === empty( $atts['message'] ) ) {
$temp = str_replace( "\n\r", "\n", $atts['message'] );
$temp = str_replace( "\r\n", "\n", $temp );
$atts['message'] = str_replace( "\n", "\n\n", $temp );
}
return $atts;
}
/**
* Handle the mandrill_nl2br hook.
* When sending text/plain nl2br should be avoided
*/
public function mandrill_nl2br( $nl2br = false, $message = null ) {
return true;
}
private function _parse_text() {
if ( null !== $this->_translations ) {
$this->_message = strtr( $this->_message, $this->_translations );
$this->_subject = strtr( $this->_subject, $this->_translations );
}
}
}