first commit
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TM_Unsent_Jobs_Notifications_Hooks
|
||||
*/
|
||||
class WPML_TM_Unsent_Jobs_Notice_Hooks {
|
||||
/** @var string */
|
||||
protected $dismissed_option_key;
|
||||
|
||||
/**
|
||||
* @var WPML_TM_Unsent_Jobs_Notice
|
||||
*/
|
||||
private $wpml_tm_notice_email_notice;
|
||||
|
||||
/**
|
||||
* @var WPML_Notices
|
||||
*/
|
||||
private $wpml_admin_notices;
|
||||
|
||||
/**
|
||||
* @var WPML_WP_API
|
||||
*/
|
||||
private $wp_api;
|
||||
|
||||
/**
|
||||
* WPML_TM_Unsent_Jobs_Notice_Hooks constructor.
|
||||
*
|
||||
* @param WPML_TM_Unsent_Jobs_Notice $wpml_tm_notice_email_notice
|
||||
* @param WPML_WP_API $wp_api
|
||||
* @param string $dismissed_option_key
|
||||
*/
|
||||
public function __construct( WPML_TM_Unsent_Jobs_Notice $wpml_tm_notice_email_notice, WPML_WP_API $wp_api, $dismissed_option_key ) {
|
||||
$this->wpml_tm_notice_email_notice = $wpml_tm_notice_email_notice;
|
||||
$this->wpml_admin_notices = wpml_get_admin_notices();
|
||||
$this->wp_api = $wp_api;
|
||||
$this->dismissed_option_key = $dismissed_option_key;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action( 'wpml_tm_jobs_translator_notification', array( $this, 'email_for_job' ) );
|
||||
add_action( 'wpml_tm_basket_committed', array( $this, 'add_notice' ) );
|
||||
add_action( 'shutdown', array( $this, 'remove_notice' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $args
|
||||
*/
|
||||
public function email_for_job( $args ) {
|
||||
$job_set = array_key_exists( 'job', $args ) && $args['job'];
|
||||
$event_set = array_key_exists( 'event', $args ) && $args['event'];
|
||||
if ( $job_set && $event_set ) {
|
||||
if ( 'unsent' === $args['event'] ) {
|
||||
$this->wpml_tm_notice_email_notice->add_job( $args );
|
||||
} else {
|
||||
$this->wpml_tm_notice_email_notice->remove_job( $args );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function add_notice() {
|
||||
$this->wpml_tm_notice_email_notice->add_notice( $this->wpml_admin_notices, $this->get_dismissed_option_key() );
|
||||
}
|
||||
|
||||
public function remove_notice() {
|
||||
if ( $this->wp_api->is_jobs_tab() ) {
|
||||
$this->wpml_admin_notices->remove_notice( WPML_TM_Unsent_Jobs_Notice::NOTICE_GROUP_ID, WPML_TM_Unsent_Jobs_Notice::NOTICE_ID );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function get_dismissed_option_key() {
|
||||
return $this->dismissed_option_key;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TM_Unsent_Jobs_Notice_Template
|
||||
*/
|
||||
class WPML_TM_Unsent_Jobs_Notice_Template {
|
||||
|
||||
const TEMPLATE_FILE = 'jobs-not-notified.twig';
|
||||
|
||||
/**
|
||||
* @var WPML_Twig_Template
|
||||
*/
|
||||
private $template_service;
|
||||
|
||||
/**
|
||||
* WPML_TM_Unsent_Jobs_Notice_Template constructor.
|
||||
*
|
||||
* @param IWPML_Template_Service $template_service
|
||||
*/
|
||||
public function __construct( IWPML_Template_Service $template_service ) {
|
||||
$this->template_service = $template_service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $jobs
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_notice_body( $jobs ) {
|
||||
$model = $this->get_notice_model( $jobs );
|
||||
|
||||
return $this->template_service->show( $model, self::TEMPLATE_FILE );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $jobs
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_notice_model( $jobs ) {
|
||||
$translators_tab = 'admin.php?page=' . WPML_TM_FOLDER . '/menu/main.php&sm=translators';
|
||||
$jobs_formatted = $this->get_formatted_jobs( $jobs );
|
||||
|
||||
$model = array(
|
||||
'strings' => array(
|
||||
'title' => esc_html__( 'Translations may delay because translators did not receive notifications', 'wpml-translation-management' ),
|
||||
'body' => esc_html__( 'You have sent documents to translation. WPML can send notification emails to assigned translators, but translators for some languages have selected not to receive this notification.', 'wpml-translation-management' ),
|
||||
'jobs' => $jobs_formatted,
|
||||
'bottom' => sprintf(
|
||||
esc_html__( 'You should contact your %1$s and ask them to enable the notification emails which will allow them to see when there is new work waiting for them. To enable notifications, translators need to log-in to this site, go to their user profile page and change the related option in the WPML language settings section.', 'wpml-translation-management' ),
|
||||
'<a href="' . admin_url( $translators_tab ) . '">' . esc_html__( 'translators' ) . '</a> '
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $jobs
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_formatted_jobs( $jobs ) {
|
||||
$jobs_formatted = array();
|
||||
foreach ( $jobs as $job ) {
|
||||
$jobs_formatted[] = sprintf( __( 'Job %1$s: %2$s - %3$s', 'wpml-translation-management' ), $job['job_id'], $job['lang_from'], $job['lang_to'] );
|
||||
}
|
||||
|
||||
return $jobs_formatted;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
use WPML\Core\Twig_Loader_Filesystem;
|
||||
use WPML\Core\Twig_Environment;
|
||||
|
||||
/**
|
||||
* Class WPML_TM_Unsent_Jobs_Notice
|
||||
*/
|
||||
class WPML_TM_Unsent_Jobs_Notice {
|
||||
|
||||
const OPT_JOBS_NOT_NOTIFIED = '_wpml_jobs_not_notified';
|
||||
const NOTICE_ID = 'job-not-notified';
|
||||
const NOTICE_GROUP_ID = 'tm-jobs-notification';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $body;
|
||||
|
||||
/**
|
||||
* @var WPML_WP_API
|
||||
*/
|
||||
private $wp_api;
|
||||
|
||||
/**
|
||||
* @var WPML_TM_Unsent_Jobs_Notice_Template
|
||||
*/
|
||||
private $notice_template;
|
||||
|
||||
/**
|
||||
* WPML_TM_Unsent_Jobs_Notice constructor.
|
||||
*
|
||||
* @param WPML_WP_API $wp_api
|
||||
* @param WPML_TM_Unsent_Jobs_Notice_Template|null $notice_template
|
||||
*/
|
||||
public function __construct( WPML_WP_API $wp_api, WPML_TM_Unsent_Jobs_Notice_Template $notice_template = null ) {
|
||||
$this->wp_api = $wp_api;
|
||||
$this->notice_template = $notice_template;
|
||||
}
|
||||
|
||||
private function prepare_notice_body() {
|
||||
$this->body = $this->get_notice_template()->get_notice_body( $this->get_jobs() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|WPML_TM_Unsent_Jobs_Notice_Template
|
||||
*/
|
||||
private function get_notice_template() {
|
||||
if ( ! $this->notice_template ) {
|
||||
$template_paths = array(
|
||||
WPML_TM_PATH . '/templates/notices/',
|
||||
);
|
||||
$twig_loader = new Twig_Loader_Filesystem( $template_paths );
|
||||
$environment_args = array();
|
||||
if ( WP_DEBUG ) {
|
||||
$environment_args['debug'] = true;
|
||||
}
|
||||
$twig = new Twig_Environment( $twig_loader, $environment_args );
|
||||
$twig_service = new WPML_Twig_Template( $twig );
|
||||
|
||||
$this->notice_template = new WPML_TM_Unsent_Jobs_Notice_Template( $twig_service );
|
||||
}
|
||||
|
||||
return $this->notice_template;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WPML_Notices $wpml_admin_notices
|
||||
*/
|
||||
public function add_notice( WPML_Notices $wpml_admin_notices, $dismissed_option_key ) {
|
||||
if ( $this->get_jobs() ) {
|
||||
$this->prepare_notice_body();
|
||||
|
||||
$notice = new WPML_Notice( self::NOTICE_ID, $this->body, 'tm-jobs-notification' );
|
||||
$notice->set_css_class_types( 'info' );
|
||||
$notice->add_display_callback( array( $this->wp_api, 'is_jobs_tab' ) );
|
||||
$this->add_actions( $notice );
|
||||
$this->remove_notice_from_dismissed_list( self::NOTICE_GROUP_ID, $dismissed_option_key );
|
||||
|
||||
$wpml_admin_notices->add_notice( $notice );
|
||||
|
||||
$this->update_jobs_option( array() );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function remove_notice_from_dismissed_list( $notice_group_id, $dismissed_option_key ) {
|
||||
$dismissed_notices = get_option( $dismissed_option_key );
|
||||
|
||||
if ( is_array( $dismissed_notices ) ) {
|
||||
foreach ( (array) $dismissed_notices as $key => $notices ) {
|
||||
if ( $key === $notice_group_id ) {
|
||||
unset( $dismissed_notices[ $key ] );
|
||||
}
|
||||
}
|
||||
|
||||
update_option( $dismissed_option_key, $dismissed_notices );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WPML_Notice $notice
|
||||
*/
|
||||
private function add_actions( WPML_Notice $notice ) {
|
||||
$dismiss_action = new WPML_Notice_Action( __( 'Dismiss', 'wpml-translation-management' ), '#', true, false, false, true );
|
||||
$notice->add_action( $dismiss_action );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $args
|
||||
*/
|
||||
public function add_job( $args ) {
|
||||
$job_id = $args['job']->get_id();
|
||||
$lang_from = $args['job']->get_source_language_code( true );
|
||||
$lang_to = $args['job']->get_language_code( true );
|
||||
|
||||
$jobs = $this->get_jobs();
|
||||
|
||||
if ( ! wp_filter_object_list( $jobs, array( 'job_id' => $job_id ) ) ) {
|
||||
$jobs[] = array(
|
||||
'job_id' => $job_id,
|
||||
'lang_from' => $lang_from,
|
||||
'lang_to' => $lang_to,
|
||||
);
|
||||
|
||||
$this->update_jobs_option( $jobs );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $args
|
||||
*/
|
||||
public function remove_job( $args ) {
|
||||
$job_id = $args['job']->get_id();
|
||||
$unsent_jobs = $this->get_jobs();
|
||||
|
||||
if ( $unsent_jobs ) {
|
||||
foreach ( $unsent_jobs as $key => $unsent_job ) {
|
||||
if ( $unsent_job['job_id'] === $job_id ) {
|
||||
unset( $unsent_jobs[ $key ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->update_jobs_option( $unsent_jobs );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $jobs
|
||||
*/
|
||||
private function update_jobs_option( $jobs ) {
|
||||
update_option( self::OPT_JOBS_NOT_NOTIFIED, $jobs );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function get_jobs() {
|
||||
return get_option( self::OPT_JOBS_NOT_NOTIFIED );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user