first commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
class WPML_TP_Lock_Factory {
|
||||
|
||||
public function create() {
|
||||
return new WPML_TP_Lock( new WPML_WP_API() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
class WPML_TP_Lock_Notice_Factory implements IWPML_Backend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
$tp_lock_factory = new WPML_TP_Lock_Factory();
|
||||
$notices = wpml_get_admin_notices();
|
||||
return new WPML_TP_Lock_Notice( $tp_lock_factory->create(), $notices );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
class WPML_TP_Lock_Notice implements IWPML_Action {
|
||||
|
||||
const NOTICE_GROUP = 'tp-lock';
|
||||
const NOTICE_LOCKED = 'locked';
|
||||
|
||||
/** @var WPML_TP_Lock $tp_lock */
|
||||
private $tp_lock;
|
||||
|
||||
/** @var WPML_Notices $notices */
|
||||
private $notices;
|
||||
|
||||
public function __construct( WPML_TP_Lock $tp_lock, WPML_Notices $notices ) {
|
||||
$this->tp_lock = $tp_lock;
|
||||
$this->notices = $notices;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action( 'admin_init', array( $this, 'handle_notice' ) );
|
||||
}
|
||||
|
||||
public function handle_notice() {
|
||||
$locker_reason = $this->tp_lock->get_locker_reason();
|
||||
|
||||
if ( (bool) $locker_reason ) {
|
||||
$text = '<p>' . __( 'Some communications with the translation proxy are locked.', 'wpml-translation-management' ) . '</p>';
|
||||
$text .= '<p>' . $locker_reason . '</p>';
|
||||
$notice = $this->notices->create_notice( self::NOTICE_LOCKED, $text, self::NOTICE_GROUP );
|
||||
$notice->set_css_class_types( 'notice-warning' );
|
||||
$this->notices->add_notice( $notice );
|
||||
} else {
|
||||
$this->notices->remove_notice( self::NOTICE_GROUP, self::NOTICE_LOCKED );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
class WPML_TP_Lock {
|
||||
|
||||
private $lockable_endpoints = array(
|
||||
'/jobs/{job_id}/xliff.json',
|
||||
);
|
||||
|
||||
/** @var WPML_WP_API $wp_api */
|
||||
private $wp_api;
|
||||
|
||||
public function __construct( WPML_WP_API $wp_api ) {
|
||||
$this->wp_api = $wp_api;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_locked( $url ) {
|
||||
return $this->get_locker_reason() && $this->is_lockable( $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|false
|
||||
*/
|
||||
public function get_locker_reason() {
|
||||
if ( 'test' === $this->wp_api->constant( 'WPML_ENVIRONMENT' ) ) {
|
||||
return __( 'The constant WPML_ENVIRONMENT is set to "Test".', 'wpml-translation-management' );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_lockable( $url ) {
|
||||
$endpoint = preg_replace( '#^' . OTG_TRANSLATION_PROXY_URL . '#', '', $url, 1 );
|
||||
return in_array( $endpoint, $this->lockable_endpoints, true );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user