first commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
class WPML_LS_Footer_Slot extends WPML_LS_Slot {
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function get_allowed_properties() {
|
||||
$allowed_properties = array(
|
||||
'template' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => $this->get_core_template( 'list-horizontal' ),
|
||||
),
|
||||
'slot_group' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => 'statics',
|
||||
),
|
||||
'slot_slug' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => 'footer',
|
||||
),
|
||||
);
|
||||
|
||||
return array_merge( parent::get_allowed_properties(), $allowed_properties );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
class WPML_LS_Menu_Slot extends WPML_LS_Slot {
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function is_enabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function get_allowed_properties() {
|
||||
$allowed_properties = array(
|
||||
'position_in_menu' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => 'after',
|
||||
),
|
||||
'is_hierarchical' => array(
|
||||
'type' => 'int',
|
||||
'force_missing_to' => 1,
|
||||
),
|
||||
'show' => array(
|
||||
'type' => 'int',
|
||||
'force_missing_to' => 1,
|
||||
),
|
||||
'template' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => $this->get_core_template( 'menu-item' ),
|
||||
),
|
||||
'slot_group' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => 'menus',
|
||||
),
|
||||
'slot_slug' => array(
|
||||
'type' => 'int',
|
||||
'force_missing_to' => 0,
|
||||
),
|
||||
);
|
||||
|
||||
return array_merge( parent::get_allowed_properties(), $allowed_properties );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
class WPML_LS_Post_Translations_Slot extends WPML_LS_Slot {
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function get_allowed_properties() {
|
||||
$allowed_properties = array(
|
||||
'display_before_content' => array( 'type' => 'int', 'force_missing_to' => 0 ),
|
||||
'display_after_content' => array( 'type' => 'int', 'force_missing_to' => 0 ),
|
||||
'availability_text' => array( 'type' => 'string', 'stripslashes' => true ),
|
||||
'template' => array( 'type' => 'string', 'force_missing_to' => $this->get_core_template( 'post-translations' ) ),
|
||||
'slot_group' => array( 'type' => 'string', 'force_missing_to' => 'statics' ),
|
||||
'slot_slug' => array( 'type' => 'string', 'force_missing_to' => 'post_translations' ),
|
||||
);
|
||||
|
||||
return array_merge( parent::get_allowed_properties(), $allowed_properties );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
class WPML_LS_Shortcode_Actions_Slot extends WPML_LS_Slot {
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function get_allowed_properties() {
|
||||
$allowed_properties = array(
|
||||
'show' => array(
|
||||
'type' => 'int',
|
||||
'force_missing_to' => 0,
|
||||
),
|
||||
'template' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => $this->get_core_template( 'list-horizontal' ),
|
||||
),
|
||||
'slot_group' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => 'statics',
|
||||
),
|
||||
'slot_slug' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => 'shortcode_actions',
|
||||
),
|
||||
);
|
||||
|
||||
return array_merge( parent::get_allowed_properties(), $allowed_properties );
|
||||
}
|
||||
|
||||
public function is_enabled() {
|
||||
/**
|
||||
* This filter allows to programmatically enable/disable the custom language switcher.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* @param bool The original status.
|
||||
*/
|
||||
return apply_filters( 'wpml_custom_language_switcher_is_enabled', parent::is_enabled() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
class WPML_LS_Sidebar_Slot extends WPML_LS_Slot {
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function is_enabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function get_allowed_properties() {
|
||||
$allowed_properties = array(
|
||||
'widget_title' => array(
|
||||
'type' => 'string',
|
||||
'stripslashes' => true,
|
||||
),
|
||||
'show' => array(
|
||||
'type' => 'int',
|
||||
'force_missing_to' => 1,
|
||||
),
|
||||
'template' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => $this->get_core_template( 'dropdown' ),
|
||||
),
|
||||
'slot_group' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => 'sidebars',
|
||||
),
|
||||
);
|
||||
|
||||
return array_merge( parent::get_allowed_properties(), $allowed_properties );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
class WPML_LS_Slot_Factory {
|
||||
|
||||
/**
|
||||
* @param array|WPML_LS_Slot $args
|
||||
*
|
||||
* @return WPML_LS_Slot
|
||||
*/
|
||||
public function get_slot( $args ) {
|
||||
|
||||
if ( is_array( $args ) ) {
|
||||
|
||||
$args['slot_group'] = isset( $args['slot_group'] ) ? $args['slot_group'] : null;
|
||||
$args['slot_slug'] = isset( $args['slot_slug'] ) ? $args['slot_slug'] : null;
|
||||
$slot = new WPML_LS_Slot( $args );
|
||||
|
||||
switch ( $args['slot_group'] ) {
|
||||
case 'menus':
|
||||
$slot = new WPML_LS_Menu_Slot( $args );
|
||||
break;
|
||||
|
||||
case 'sidebars':
|
||||
$slot = new WPML_LS_Sidebar_Slot( $args );
|
||||
break;
|
||||
|
||||
case 'statics':
|
||||
switch ( $args['slot_slug'] ) {
|
||||
case 'footer':
|
||||
$slot = new WPML_LS_Footer_Slot( $args );
|
||||
break;
|
||||
|
||||
case 'post_translations':
|
||||
$slot = new WPML_LS_Post_Translations_Slot( $args );
|
||||
break;
|
||||
|
||||
case 'shortcode_actions':
|
||||
$slot = new WPML_LS_Shortcode_Actions_Slot( $args );
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
$slot = $args;
|
||||
}
|
||||
|
||||
return $slot;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slot_group
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_default_slot_arguments( $slot_group ) {
|
||||
$args = array(
|
||||
'slot_group' => $slot_group,
|
||||
'display_link_for_current_lang' => 1,
|
||||
'display_names_in_native_lang' => 1,
|
||||
'display_names_in_current_lang' => 1,
|
||||
);
|
||||
|
||||
if ( $slot_group === 'menus' ) {
|
||||
$args['template'] = $this->get_core_templates( 'menu-item' );
|
||||
$args['is_hierarchical'] = 1;
|
||||
} elseif ( $slot_group === 'sidebars' ) {
|
||||
$args['template'] = $this->get_core_templates( 'dropdown' );
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slot_group
|
||||
*
|
||||
* @return WPML_LS_Slot
|
||||
*/
|
||||
public function get_default_slot( $slot_group ) {
|
||||
$slot_args = $this->get_default_slot_arguments( $slot_group );
|
||||
return $this->get_slot( $slot_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function get_core_templates( $slug ) {
|
||||
$parameters = WPML_Language_Switcher::parameters();
|
||||
$templates = isset( $parameters['core_templates'] ) ? $parameters['core_templates'] : array();
|
||||
return isset( $templates[ $slug ] ) ? $templates[ $slug ] : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_LS_Slot
|
||||
*/
|
||||
class WPML_LS_Slot {
|
||||
|
||||
/* @var array $properties */
|
||||
private $properties = array();
|
||||
|
||||
/* @var array $protected_properties */
|
||||
private $protected_properties = array(
|
||||
'slot_group',
|
||||
'slot_slug',
|
||||
);
|
||||
|
||||
/**
|
||||
* WPML_Language_Switcher_Slot constructor.
|
||||
*
|
||||
* @param array $args
|
||||
*/
|
||||
public function __construct( array $args = array() ) {
|
||||
$this->set_properties( $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $property
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get( $property ) {
|
||||
return isset( $this->properties[ $property ] ) ? $this->properties[ $property ] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $property
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function set( $property, $value ) {
|
||||
if ( ! in_array( $property, $this->protected_properties ) ) {
|
||||
$allowed_properties = $this->get_allowed_properties();
|
||||
if ( array_key_exists( $property, $allowed_properties ) ) {
|
||||
$meta_data = $allowed_properties[ $property ];
|
||||
$this->properties[ $property ] = $this->sanitize( $value, $meta_data );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string|null
|
||||
*/
|
||||
public function group() {
|
||||
return $this->get( 'slot_group' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|string|null
|
||||
*/
|
||||
public function slug() {
|
||||
return $this->get( 'slot_slug' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function is_menu() {
|
||||
return $this->group() === 'menus';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function is_sidebar() {
|
||||
return $this->group() === 'sidebars';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function is_footer() {
|
||||
return $this->group() === 'statics' && $this->slug() === 'footer';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function is_post_translations() {
|
||||
return $this->group() === 'statics' && $this->slug() === 'post_translations';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function is_shortcode_actions() {
|
||||
return $this->group() === 'statics' && $this->slug() === 'shortcode_actions';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function is_enabled() {
|
||||
return $this->get( 'show' ) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function template() {
|
||||
return $this->get( 'template' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function template_string() {
|
||||
return $this->get( 'template_string' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $args
|
||||
*/
|
||||
private function set_properties( array $args ) {
|
||||
foreach ( $this->get_allowed_properties() as $allowed_property => $meta_data ) {
|
||||
$value = null;
|
||||
if ( isset( $args[ $allowed_property ] ) ) {
|
||||
$value = $args[ $allowed_property ];
|
||||
} elseif ( isset( $meta_data['set_missing_to'] ) ) {
|
||||
$value = $meta_data['set_missing_to'];
|
||||
}
|
||||
$this->properties[ $allowed_property ] = $this->sanitize( $value, $meta_data );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function get_allowed_properties() {
|
||||
return array(
|
||||
'slot_group' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => '',
|
||||
),
|
||||
'slot_slug' => array(
|
||||
'type' => 'string',
|
||||
'force_missing_to' => '',
|
||||
),
|
||||
'show' => array(
|
||||
'type' => 'int',
|
||||
'force_missing_to' => 0,
|
||||
),
|
||||
'template' => array( 'type' => 'string' ),
|
||||
'display_flags' => array(
|
||||
'type' => 'int',
|
||||
'force_missing_to' => 0,
|
||||
),
|
||||
'display_link_for_current_lang' => array(
|
||||
'type' => 'int',
|
||||
'force_missing_to' => 0,
|
||||
),
|
||||
'display_names_in_native_lang' => array(
|
||||
'type' => 'int',
|
||||
'force_missing_to' => 0,
|
||||
),
|
||||
'display_names_in_current_lang' => array(
|
||||
'type' => 'int',
|
||||
'force_missing_to' => 0,
|
||||
),
|
||||
'include_flag_width' => array(
|
||||
'type' => 'int',
|
||||
'set_missing_to' => \WPML_LS_Settings::DEFAULT_FLAG_WIDTH,
|
||||
),
|
||||
'include_flag_height' => array(
|
||||
'type' => 'int',
|
||||
'set_missing_to' => \WPML_LS_Settings::DEFAULT_FLAG_HEIGHT,
|
||||
),
|
||||
// Colors
|
||||
'background_normal' => array( 'type' => 'string' ),
|
||||
'border_normal' => array( 'type' => 'string' ),
|
||||
'font_current_normal' => array( 'type' => 'string' ),
|
||||
'font_current_hover' => array( 'type' => 'string' ),
|
||||
'background_current_normal' => array( 'type' => 'string' ),
|
||||
'background_current_hover' => array( 'type' => 'string' ),
|
||||
'font_other_normal' => array( 'type' => 'string' ),
|
||||
'font_other_hover' => array( 'type' => 'string' ),
|
||||
'background_other_normal' => array( 'type' => 'string' ),
|
||||
'background_other_hover' => array( 'type' => 'string' ),
|
||||
'template_string' => array(
|
||||
'type' => 'string',
|
||||
'twig_string' => 1,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param array $meta_data
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function sanitize( $value, array $meta_data ) {
|
||||
if ( ! is_null( $value ) ) {
|
||||
switch ( $meta_data['type'] ) {
|
||||
case 'string':
|
||||
$value = (string) $value;
|
||||
if ( array_key_exists( 'stripslashes', $meta_data ) && $meta_data['stripslashes'] ) {
|
||||
$value = stripslashes( $value );
|
||||
}
|
||||
if ( array_key_exists( 'twig_string', $meta_data ) ) {
|
||||
$value = preg_replace( '/<br\W*?\/>/', '', $value );
|
||||
} else {
|
||||
$value = sanitize_text_field( $value );
|
||||
}
|
||||
break;
|
||||
case 'bool':
|
||||
$value = (bool) $value;
|
||||
break;
|
||||
case 'int':
|
||||
$value = (int) $value;
|
||||
break;
|
||||
}
|
||||
} elseif ( array_key_exists( 'force_missing_to', $meta_data ) ) {
|
||||
$value = $meta_data['force_missing_to'];
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The use of a plain object does not work in Twig
|
||||
* e.g: slot_settings[ option.name ~ "_normal" ] (see in panel-colors.twig)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_model() {
|
||||
$model = array();
|
||||
|
||||
foreach ( $this->properties as $property => $value ) {
|
||||
$model[ $property ] = $value;
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
protected function get_core_template( $slug ) {
|
||||
$parameters = WPML_Language_Switcher::parameters();
|
||||
$core_templates = isset( $parameters['core_templates'] ) ? $parameters['core_templates'] : array();
|
||||
return isset( $core_templates[ $slug ] ) ? $core_templates[ $slug ] : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user