first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
<?php
/**
* Interface WPML_Settings_Interface
*
* @author OnTheGoSystems
*/
interface IWPML_TF_Settings {
/**
* @return array of name/value pairs
*
* Each property should have its own setter "set_{$property_name}"
*/
public function get_properties();
}

View File

@@ -0,0 +1,18 @@
<?php
/**
* Class WPML_TF_Settings_Handler
*
* @author OnTheGoSystems
*/
abstract class WPML_TF_Settings_Handler {
/**
* @param string $class_name
*
* @return string
*/
protected function get_option_name( $class_name ) {
return sanitize_title( $class_name );
}
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* Class WPML_TF_Settings_Read
*
* @author OnTheGoSystems
*/
class WPML_TF_Settings_Read extends WPML_TF_Settings_Handler {
/**
* @param string $settings_class
*
* @return IWPML_TF_Settings
*
* @throws InvalidArgumentException
*/
public function get( $settings_class ) {
if ( ! class_exists( $settings_class ) ) {
throw new InvalidArgumentException( $settings_class . ' does not exist.' );
}
if ( ! in_array( 'IWPML_TF_Settings', class_implements( $settings_class ) ) ) {
throw new InvalidArgumentException( $settings_class . ' should implement IWPML_TF_Settings.' );
}
$settings_properties = get_option( $this->get_option_name( $settings_class ) );
/** @var IWPML_TF_Settings $settings */
$settings = new $settings_class();
if ( is_array( $settings_properties ) ) {
$this->set_properties( $settings, $settings_properties );
}
return $settings;
}
/**
* @param IWPML_TF_Settings $settings
* @param array $settings_properties
*
* @throws BadMethodCallException
*/
private function set_properties( IWPML_TF_Settings $settings, array $settings_properties ) {
foreach ( $settings->get_properties() as $property_name => $property_value ) {
if ( method_exists( $settings, 'set_' . $property_name ) ) {
if ( isset( $settings_properties[ $property_name ] ) ) {
call_user_func( array( $settings, 'set_' . $property_name ), $settings_properties[ $property_name ] );
}
} else {
throw new BadMethodCallException( 'The method set_' . $property_name . ' is required in ' . get_class( $settings ) . '.' );
}
}
}
}

View File

@@ -0,0 +1,18 @@
<?php
/**
* Class WPML_TF_Settings_Write
*
* @author OnTheGoSystems
*/
class WPML_TF_Settings_Write extends WPML_TF_Settings_Handler {
/**
* @param IWPML_TF_Settings $settings
*
* @return bool
*/
public function save( IWPML_TF_Settings $settings ) {
return update_option( $this->get_option_name( get_class( $settings ) ), $settings->get_properties() );
}
}

View File

@@ -0,0 +1,180 @@
<?php
use WPML\API\Sanitize;
/**
* Class WPML_TF_Settings
*
* @author OnTheGoSystems
*/
class WPML_TF_Settings implements IWPML_TF_Settings {
const BUTTON_MODE_DISABLED = 'disabled';
const BUTTON_MODE_LEFT = 'left';
const BUTTON_MODE_RIGHT = 'right';
const BUTTON_MODE_CUSTOM = 'custom';
const ICON_STYLE_LEGACY = 'translation';
const ICON_STYLE_STAR = 'star';
const ICON_STYLE_THUMBSUP = 'thumbsup';
const ICON_STYLE_BULLHORN = 'bullhorn';
const ICON_STYLE_COMMENT = 'comment';
const ICON_STYLE_QUOTE = 'quote';
const DISPLAY_ALWAYS = 'always';
const DISPLAY_CUSTOM = 'custom';
const EXPIRATION_ON_PUBLISH_OR_UPDATE = 'publish_or_update';
const EXPIRATION_ON_PUBLISH_ONLY = 'publish_only';
const EXPIRATION_ON_UPDATE_ONLY = 'update_only';
const DELAY_DAY = 1;
const DELAY_WEEK = 7;
const DELAY_MONTH = 30;
/** @var bool $enabled */
private $enabled = false;
/** @var string $button_mode */
private $button_mode = self::BUTTON_MODE_LEFT;
/** @var string $icon_style */
private $icon_style = self::ICON_STYLE_LEGACY;
/** @var null|array $languages_to */
private $languages_to = null;
/** @var string $display_mode */
private $display_mode = self::DISPLAY_CUSTOM;
/** @var string $expiration_mode */
private $expiration_mode = self::EXPIRATION_ON_PUBLISH_OR_UPDATE;
/** @var int $expiration_delay_quantity */
private $expiration_delay_quantity = 1;
/** @var int $expiration_delay_unit */
private $expiration_delay_unit = self::DELAY_MONTH;
/**
* @param bool $enabled
*/
public function set_enabled( $enabled ) {
$this->enabled = (bool) $enabled;
}
/**
* @return bool
*/
public function is_enabled() {
return $this->enabled;
}
/**
* @param string $button_mode
*/
public function set_button_mode( $button_mode ) {
$this->button_mode = Sanitize::string( $button_mode );
}
/**
* @return string
*/
public function get_button_mode() {
return $this->button_mode;
}
/** @param string $style */
public function set_icon_style( $style ) {
$this->icon_style = Sanitize::string( $style );
}
/** @return string */
public function get_icon_style() {
return $this->icon_style;
}
/**
* @param array $languages_to
*/
public function set_languages_to( array $languages_to ) {
$this->languages_to = array_map( 'sanitize_title', $languages_to );
}
/**
* @return null|array
*/
public function get_languages_to() {
return $this->languages_to;
}
/**
* @param string $display_mode
*/
public function set_display_mode( $display_mode ) {
$this->display_mode = Sanitize::string( $display_mode );
}
/**
* @return string
*/
public function get_display_mode() {
return $this->display_mode;
}
/**
* @param string $expiration_mode
*/
public function set_expiration_mode( $expiration_mode ) {
$this->expiration_mode = Sanitize::string( $expiration_mode );
}
/**
* @return string
*/
public function get_expiration_mode() {
return $this->expiration_mode;
}
/**
* @param int $expiration_delay_quantity
*/
public function set_expiration_delay_quantity( $expiration_delay_quantity ) {
$this->expiration_delay_quantity = (int) $expiration_delay_quantity;
}
/**
* @return int
*/
public function get_expiration_delay_quantity() {
return $this->expiration_delay_quantity;
}
/**
* @param int $expiration_delay_unit
*/
public function set_expiration_delay_unit( $expiration_delay_unit ) {
$this->expiration_delay_unit = (int) $expiration_delay_unit;
}
/**
* @return int
*/
public function get_expiration_delay_unit() {
return $this->expiration_delay_unit;
}
/**
* @return int delay in days before expiration
*/
public function get_expiration_delay_in_days() {
return $this->expiration_delay_quantity * $this->expiration_delay_unit;
}
/**
* @return array
*/
public function get_properties() {
return get_object_vars( $this );
}
}