Skip to content

Commit

Permalink
Merge branch 'release/0.9.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
dueddel committed Jan 9, 2018
2 parents 7802a2d + 3f1002d commit 30e5050
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Change Log

## [0.9.2](https://github.com/Vierbeuter/wp-plugin-core/tree/0.9.2) (2018-01-09)
### Added
* added new `WpmlWpOptions` service supporting [WPML](https://wpml.org/) for making `wp_options` translatable (requires the [String Translation module](https://wpml.org/documentation/getting-started-guide/string-translation/))

## [0.9.1](https://github.com/Vierbeuter/wp-plugin-core/tree/0.9.1) (2017-11-30)
### Added
* added `WpEditor` field to use `wp_editor(…)`
Expand Down
1 change: 1 addition & 0 deletions src/Vierbeuter/WordPress/Feature/CustomField/WpEditor.php
Expand Up @@ -50,6 +50,7 @@ protected function getDefaultSettings(): array
{
return [
'media_buttons' => false,
'quicktags' => false,
'tinymce' => [
'height' => 200,
'inline_styles' => false,
Expand Down
3 changes: 3 additions & 0 deletions src/Vierbeuter/WordPress/PluginRegistrar.php
Expand Up @@ -5,6 +5,7 @@
use Vierbeuter\WordPress\Di\Container;
use Vierbeuter\WordPress\Service\CoreTranslator;
use Vierbeuter\WordPress\Service\PluginTranslator;
use Vierbeuter\WordPress\Service\WpmlWpOptions;
use Vierbeuter\WordPress\Service\WpOptions;

/**
Expand Down Expand Up @@ -135,6 +136,8 @@ protected function addComponents(string $pluginClassName)

// add wp_options service
$this->container->addComponent(WpOptions::class, PluginData::class);
// add wp_options service supporting WPML
$this->container->addComponent(WpmlWpOptions::class, PluginData::class);
}

/**
Expand Down
46 changes: 42 additions & 4 deletions src/Vierbeuter/WordPress/Service/WpOptions.php
Expand Up @@ -48,6 +48,44 @@ protected function getDbMetaKey(string $fieldSlug, WpOptionsPage $wpOptionsPage
return $pluginPart . $pagePart . $fieldPart;
}

/**
* Returns the <code>wp_options</code> value for given option name.
*
* This method is just an encapsulation of WP's <code>get_option(…)</code> function. It's the counterpart of the
* <code>updateOption(…)</code> method.
*
* @param string $name the option's name
* @param mixed|null $default default return value in case of no wp_option found for given option name
*
* @return mixed
*
* @see https://developer.wordpress.org/reference/functions/get_option/
* @see \Vierbeuter\WordPress\Service\WpOptions::updateOption()
*/
protected function getOption(string $name, $default = null)
{
return get_option($name, $default);
}

/**
* Saves given value to <code>wp_options</code> with given option name.
*
* This method is just an encapsulation of WP's <code>update_option(…)</code> function. It's the counterpart of the
* <code>getOption(…)</code> method.
*
* @param string $name the option's name
* @param mixed $value the option's value
*
* @return bool
*
* @see https://developer.wordpress.org/reference/functions/update_option/
* @see \Vierbeuter\WordPress\Service\WpOptions::getOption()
*/
protected function updateOption(string $name, $value): bool
{
return update_option($name, $value);
}

/**
* Returns the <code>wp_options</code> value for given key.
*
Expand All @@ -63,7 +101,7 @@ protected function getDbMetaKey(string $fieldSlug, WpOptionsPage $wpOptionsPage
*/
public function get(string $key, $default = null)
{
return get_option($this->getDbMetaKey($key), $default);
return $this->getOption($this->getDbMetaKey($key), $default);
}

/**
Expand All @@ -80,7 +118,7 @@ public function get(string $key, $default = null)
*/
public function set(string $key, $value): bool
{
return update_option($this->getDbMetaKey($key), $value);
return $this->updateOption($this->getDbMetaKey($key), $value);
}

/**
Expand All @@ -98,7 +136,7 @@ public function set(string $key, $value): bool
*/
public function getByPage(WpOptionsPage $wpOptionsPage, string $fieldSlug, $default = null)
{
return get_option($this->getDbMetaKey($fieldSlug, $wpOptionsPage), $default);
return $this->getOption($this->getDbMetaKey($fieldSlug, $wpOptionsPage), $default);
}

/**
Expand All @@ -118,7 +156,7 @@ public function getByPage(WpOptionsPage $wpOptionsPage, string $fieldSlug, $defa
*/
public function setByPage(WpOptionsPage $wpOptionsPage, string $fieldSlug, $value): bool
{
return update_option($this->getDbMetaKey($fieldSlug, $wpOptionsPage), $value);
return $this->updateOption($this->getDbMetaKey($fieldSlug, $wpOptionsPage), $value);
}

/**
Expand Down
60 changes: 60 additions & 0 deletions src/Vierbeuter/WordPress/Service/WpmlWpOptions.php
@@ -0,0 +1,60 @@
<?php

namespace Vierbeuter\WordPress\Service;

/**
* The WpmlWpOptions service provides methods for easily accessing <code>wp_options</code> whilst supporting WPML.
*
* @package Vierbeuter\WordPress\Service
*
* @see https://wpml.org/forums/topic/translating-plugin-strings-stored-in-wp_options/#post-674903
* @see https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/
*/
class WpmlWpOptions extends WpOptions
{

/**
* Returns the <code>wp_options</code> value for given option name. Result may have been translated using WPML.
*
* @param string $name the option's name
* @param mixed|null $default default return value in case of no wp_option found for given option name
*
* @return mixed
*
* @see \Vierbeuter\WordPress\Service\WpmlWpOptions::updateOption()
*/
protected function getOption(string $name, $default = null)
{
// get original value
$value = parent::getOption($name, $default);

// get translated value
/** @see https://wpml.org/wpml-hook/wpml_translate_single_string/ */
$value = apply_filters('wpml_translate_single_string', $value, $this->pluginData->getPluginSlug(), $name);

return $value;
}

/**
* Saves given value to <code>wp_options</code> with given option name and registers a new string to be translated
* using WPML.
*
* @param string $name the option's name
* @param mixed $value the option's value
*
* @return bool
*
* @see \Vierbeuter\WordPress\Service\WpmlWpOptions::getOption()
*/
protected function updateOption(string $name, $value): bool
{
// save original value
$uccess = parent::updateOption($name, $value);

// add value to translatables
/** @see https://wpml.org/wpml-hook/wpml_register_single_string/ */
do_action('wpml_register_single_string', $this->pluginData->getPluginSlug(), $name, $value);

return $uccess;
}
}

0 comments on commit 30e5050

Please sign in to comment.