first commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
class WPML_Save_Themes_Plugins_Localization_Options {
|
||||
|
||||
/** @var SitePress */
|
||||
private $sitepress;
|
||||
|
||||
/**
|
||||
* WPML_Save_Themes_Plugins_Localization_Options constructor.
|
||||
*
|
||||
* @param SitePress $sitepress
|
||||
*/
|
||||
public function __construct( SitePress $sitepress ) {
|
||||
$this->sitepress = $sitepress;
|
||||
}
|
||||
|
||||
/** @param array $settings */
|
||||
public function save_settings( $settings ) {
|
||||
|
||||
foreach ( $this->get_settings() as $key => $setting ) {
|
||||
if ( array_key_exists( $key, $settings ) ) {
|
||||
$value = filter_var( $settings[ $key ], $setting['filter'] );
|
||||
if ( 'setting' === $setting['type'] ) {
|
||||
if ( $setting['st_setting'] ) {
|
||||
$st_settings = $this->sitepress->get_setting( 'st' );
|
||||
$st_settings[ $setting['settings_var'] ] = $value;
|
||||
$this->sitepress->set_setting( 'st', $st_settings );
|
||||
} else {
|
||||
$this->sitepress->set_setting( $setting['settings_var'], $value );
|
||||
}
|
||||
} elseif ( 'option' === $setting['type'] ) {
|
||||
update_option( $setting['settings_var'], $value );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->sitepress->save_settings();
|
||||
|
||||
do_action( 'theme_plugin_localization_settings_saved', $settings );
|
||||
}
|
||||
|
||||
/** @return array */
|
||||
private function get_settings() {
|
||||
$settings = array();
|
||||
$settings['theme_localization_load_textdomain'] = array(
|
||||
'settings_var' => 'theme_localization_load_textdomain',
|
||||
'filter' => FILTER_SANITIZE_NUMBER_INT,
|
||||
'type' => 'setting',
|
||||
'st_setting' => false,
|
||||
);
|
||||
$settings['gettext_theme_domain_name'] = array(
|
||||
'settings_var' => 'gettext_theme_domain_name',
|
||||
'filter' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
|
||||
'type' => 'setting',
|
||||
'st_setting' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* @param array: array of settings rendered in theme/plugin localization screen
|
||||
*/
|
||||
return apply_filters( 'wpml_localization_options_settings', $settings );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
class WPML_Theme_Plugin_Localization_UI_Hooks {
|
||||
|
||||
/** @var WPML_Theme_Plugin_Localization_UI */
|
||||
private $localization_ui;
|
||||
|
||||
/** @var WPML_Theme_Plugin_Localization_Options_UI */
|
||||
private $options_ui;
|
||||
|
||||
/**
|
||||
* WPML_Theme_Plugin_Localization_UI_Hooks constructor.
|
||||
*
|
||||
* @param WPML_Theme_Plugin_Localization_UI $localization_ui
|
||||
* @param WPML_Theme_Plugin_Localization_Options_UI $options_ui
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_Theme_Plugin_Localization_UI $localization_ui,
|
||||
WPML_Theme_Plugin_Localization_Options_UI $options_ui ) {
|
||||
|
||||
$this->localization_ui = $localization_ui;
|
||||
$this->options_ui = $options_ui;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
|
||||
add_action( 'wpml_custom_localization_type', array( $this, 'render_options_ui' ), 1 );
|
||||
}
|
||||
|
||||
public function enqueue_styles() {
|
||||
wp_enqueue_style( 'wpml-theme-plugin-localization', ICL_PLUGIN_URL . '/res/css/theme-plugin-localization.css', array( 'wpml-tooltip' ), ICL_SITEPRESS_VERSION );
|
||||
wp_enqueue_script( 'wpml-theme-plugin-localization', ICL_PLUGIN_URL . '/res/js/theme-plugin-localization.js', array( 'jquery' ), ICL_SITEPRESS_VERSION );
|
||||
wp_enqueue_script( OTGS_Assets_Handles::POPOVER_TOOLTIP );
|
||||
wp_enqueue_style( OTGS_Assets_Handles::POPOVER_TOOLTIP );
|
||||
}
|
||||
|
||||
public function render_options_ui() {
|
||||
echo $this->localization_ui->render( $this->options_ui );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use WPML\FP\Obj;
|
||||
|
||||
class WPML_Theme_Plugin_Localization_Options_Ajax implements IWPML_AJAX_Action, IWPML_DIC_Action {
|
||||
|
||||
const NONCE_LOCALIZATION_OPTIONS = 'wpml-localization-options-nonce';
|
||||
|
||||
/** @var WPML_Save_Themes_Plugins_Localization_Options */
|
||||
private $save_localization_options;
|
||||
|
||||
/**
|
||||
* WPML_Themes_Plugins_Localization_Options_Ajax constructor.
|
||||
*
|
||||
* @param WPML_Save_Themes_Plugins_Localization_Options $save_localization_options
|
||||
*/
|
||||
public function __construct( WPML_Save_Themes_Plugins_Localization_Options $save_localization_options ) {
|
||||
$this->save_localization_options = $save_localization_options;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action( 'wp_ajax_wpml_update_localization_options', array( $this, 'update_localization_options' ) );
|
||||
}
|
||||
|
||||
public function update_localization_options() {
|
||||
if ( ! $this->is_valid_request() ) {
|
||||
wp_send_json_error();
|
||||
} else {
|
||||
$this->save_localization_options->save_settings( $_POST );
|
||||
wp_send_json_success();
|
||||
}
|
||||
}
|
||||
|
||||
/** @return bool */
|
||||
private function is_valid_request() {
|
||||
return wp_verify_nonce( Obj::propOr( '', 'nonce', $_POST ), self::NONCE_LOCALIZATION_OPTIONS );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use WPML\FP\Relation;
|
||||
use function WPML\Container\make;
|
||||
|
||||
class WPML_Themes_Plugin_Localization_UI_Hooks_Factory implements IWPML_Backend_Action_Loader, IWPML_Deferred_Action_Loader {
|
||||
|
||||
/** @return WPML_Theme_Plugin_Localization_UI_Hooks */
|
||||
public function create() {
|
||||
return Relation::propEq( 'id', WPML_PLUGIN_FOLDER . '/menu/theme-localization', get_current_screen() )
|
||||
? make( WPML_Theme_Plugin_Localization_UI_Hooks::class )
|
||||
: null;
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function get_load_action() {
|
||||
return 'current_screen';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
class WPML_Theme_Plugin_Localization_Options_UI implements IWPML_Theme_Plugin_Localization_UI_Strategy {
|
||||
|
||||
/** @var SitePress */
|
||||
private $sitepress;
|
||||
|
||||
public function __construct( SitePress $sitepress ) {
|
||||
$this->sitepress = $sitepress;
|
||||
}
|
||||
|
||||
/** @return array */
|
||||
public function get_model() {
|
||||
$model = array(
|
||||
'nonce_field' => WPML_Theme_Plugin_Localization_Options_Ajax::NONCE_LOCALIZATION_OPTIONS,
|
||||
'nonce_value' => wp_create_nonce( WPML_Theme_Plugin_Localization_Options_Ajax::NONCE_LOCALIZATION_OPTIONS ),
|
||||
'section_label' => __( 'Localization options', 'sitepress' ),
|
||||
'top_options' => array(
|
||||
array(
|
||||
'template' => 'automatic-load-check.twig',
|
||||
'model' => array(
|
||||
'theme_localization_load_textdomain' => array(
|
||||
'value' => 1,
|
||||
'label' => __( "Automatically load the theme's .mo file using 'load_textdomain'", 'sitepress' ),
|
||||
'checked' => checked( $this->sitepress->get_setting( 'theme_localization_load_textdomain' ), true, false ),
|
||||
),
|
||||
'gettext_theme_domain_name' => array(
|
||||
'value' => $this->sitepress->get_setting( 'gettext_theme_domain_name', '' ),
|
||||
'label' => __( 'Enter textdomain:', 'sitepress' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
'button_label' => __( 'Save', 'sitepress' ),
|
||||
'scanning_progress_msg' => __( "Scanning now, please don't close this page.", 'sitepress' ),
|
||||
'scanning_results_title' => __( 'Scanning Results', 'sitepress' ),
|
||||
);
|
||||
|
||||
return apply_filters( 'wpml_localization_options_ui_model', $model );
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function get_template() {
|
||||
return 'options.twig';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
interface IWPML_Theme_Plugin_Localization_UI_Strategy {
|
||||
|
||||
public function get_model();
|
||||
public function get_template();
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
class WPML_Theme_Plugin_Localization_UI {
|
||||
|
||||
const TEMPLATE_PATH = '/templates/theme-plugin-localization/';
|
||||
|
||||
/**
|
||||
* @return IWPML_Template_Service
|
||||
*/
|
||||
private function get_template_service() {
|
||||
$paths = array();
|
||||
$paths[] = WPML_PLUGIN_PATH . self::TEMPLATE_PATH;
|
||||
|
||||
if ( defined( 'WPML_ST_PATH' ) ) {
|
||||
$paths[] = WPML_ST_PATH . self::TEMPLATE_PATH;
|
||||
}
|
||||
|
||||
$template_loader = new WPML_Twig_Template_Loader( $paths );
|
||||
return $template_loader->get_template();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param IWPML_Theme_Plugin_Localization_UI_Strategy $localization_strategy
|
||||
*/
|
||||
public function render( IWPML_Theme_Plugin_Localization_UI_Strategy $localization_strategy ) {
|
||||
return $this->get_template_service()->show( $localization_strategy->get_model(), $localization_strategy->get_template() );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user