Skip to content

Commit

Permalink
Merge branch 'release/0.9.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
dueddel committed Jan 9, 2018
2 parents 30e5050 + 305cd00 commit d859dd7
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 33 deletions.
14 changes: 12 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# Change Log

## [0.9.2](https://github.com/Vierbeuter/wp-plugin-core/tree/0.9.2) (2018-01-09)
## [0.9.3](https://github.com/Vierbeuter/wp-plugin-core/tree/0.9.3) (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/))
* added `WpmlWpOptionsPage` to make better use of `WpmlWpOptions` service

### Changed
* remove requirement of WPML's String Translation module (as added in 0.9.2)
* config pages for `wp_options` (if extending `WpmlWpOptionsPage`) can now take care of WPML's language switch located in WP's admin bar (config pages that don't have to be translatable can still extend `WpOptionsPage`)

## ~~[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/))~~

*Note*: Changes have been nearly completely overriden by 0.9.3 Do not rely on this version, update ASAP.

## [0.9.1](https://github.com/Vierbeuter/wp-plugin-core/tree/0.9.1) (2017-11-30)
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,7 @@ protected function render(): void
echo '<table class="form-table">';

// render all custom fields
foreach ($this->getFields() as $field) {
$fieldId = $this->getFieldId($field->getSlug());
$value = $this->wpOptions->getByPage($this, $field->getSlug());

$field->renderConfig($fieldId, $value);
}
$this->renderFields();

// update button
echo '<tr>';
Expand Down Expand Up @@ -137,4 +132,17 @@ protected function getFieldId(string $fieldSlug): string
// concat slug of page with custom-field slug
return $this->getSlug() . '-' . $fieldSlug;
}

/**
* Renders all custom fields.
*/
protected function renderFields(): void
{
foreach ($this->getFields() as $field) {
$fieldId = $this->getFieldId($field->getSlug());
$value = $this->wpOptions->getByPage($this, $field->getSlug());

$field->renderConfig($fieldId, $value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Vierbeuter\WordPress\Feature\AdminPanel\AdminPage;

use Vierbeuter\WordPress\Service\WpmlWpOptions;

/**
* WpmlWpOptionsPage is the base class for implementing and adding settings pages to the wp-admin panel that entries of
* <code>wp_options</code> can be configured with. For ease of use you just have to provide a list of custom fields
* (each one will automatically be mapped to an entry of <code>wp_options</code>).
*
* These options pages are translatable using WPML.
*
* To read and update your <code>wp_options</code> entries that are defined in this page through these custom-fields
* just use the WpmlWpOptions service.
*
* @package Vierbeuter\WordPress\Feature\AdminPanel\AdminPage
*
* @see \Vierbeuter\WordPress\Service\WpmlWpOptions
*/
abstract class WpmlWpOptionsPage extends WpOptionsPage
{

/**
* WpmlWpOptionsPage constructor.
*
* @param \Vierbeuter\WordPress\Service\WpmlWpOptions $wpOptions
*/
public function __construct(WpmlWpOptions $wpOptions)
{
// override parent constructor to enforce the usage of WpmlWpOptions instead of WpOptions
parent::__construct($wpOptions);
}

/**
* Handles post requests for current or all languages.
*
* @param array $post
*
* @throws \Exception if WPML is not active or loaded (yet)
*/
protected function handlePost(array $post): void
{
global $sitepress;

if (empty($sitepress)) {
throw new \LogicException('WPML is either not installed/active or currently not loaded yet.');
}

// if specific language selected
if ($sitepress->get_current_language() != 'all') {
parent::handlePost($post);

return;
}

/** @var WpmlWpOptions $wpOptions */
$wpOptions = $this->wpOptions;

// update wp_options for all custom fields
foreach ($this->getFields() as $field) {
foreach ($sitepress->get_active_languages() as $language) {
$langCode = $language['code'];
$fieldId = $this->getFieldId($field->getSlug()) . '_' . $langCode;

if (isset($post[$fieldId])) {
// POST values are escaped by default using addslashes(), see wp_magic_quotes() and add_magic_quotes()
/** @see wp_magic_quotes() */
/** @see add_magic_quotes() */
// --> unescape the value with stripslashes() which is the counterpart of addslashes()
$post[$fieldId] = stripslashes($post[$fieldId]);

$wpOptions->setByPage($this, $field->getSlug(), $post[$fieldId], $langCode);
}
}
}
}

/**
* Renders all custom fields for current or all languages.
*
* @throws \Exception if WPML is not active or loaded (yet)
*/
protected function renderFields(): void
{
global $sitepress;

if (empty($sitepress)) {
throw new \LogicException('WPML is either not installed/active or currently not loaded yet.');
}

// if specific language selected
if ($sitepress->get_current_language() != 'all') {
parent::renderFields();

return;
}

/** @var WpmlWpOptions $wpOptions */
$wpOptions = $this->wpOptions;

foreach ($this->getFields() as $field) {
foreach ($sitepress->get_active_languages() as $language) {
$langCode = $language['code'];
$fieldId = $this->getFieldId($field->getSlug()) . '_' . $langCode;
$value = $wpOptions->getByPage($this, $field->getSlug(), null, $langCode);

$field->renderWpmlConfig($fieldId, $langCode, $value);
}
}
}
}
46 changes: 44 additions & 2 deletions src/Vierbeuter/WordPress/Feature/CustomField/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,56 @@ public function renderConfig(string $fieldId, string $value = null): void
echo '</tr>';
}

/**
* Renders the markup of this custom-field for a ConfigPage supporting WPML.
*
* @param string $fieldId
* @param string $langCode
* @param string|null $value
*
* @see \Vierbeuter\WordPress\Feature\AdminPanel\AdminPage\WpmlWpOptionsPage
*/
public function renderWpmlConfig(string $fieldId, string $langCode, string $value = null): void
{
// wrapper begin
echo '<tr>';

// header area ("left side")
echo '<th scope="row">';
// label
$this->renderLabel($fieldId, strtoupper($langCode));
echo '</th>';

// input area ("right side")
echo '<td>';
// the actual input field
$this->renderField(null, $fieldId, $value);
// optional description/usage hint
/**
* styling with CSS class "custom-field-note"
*
* @see \Vierbeuter\WordPress\Feature\AddCustomPostTypes::admin_head()
*/
$this->renderDescription($fieldId, 'custom-field-note clear');
// other… e.g. Javascript
$this->renderAnythingAfterField(null, $fieldId, $value);
echo '</td>';

// wrapper end
echo '</tr>';
}

/**
* Renders the label's markup.
*
* @param string $fieldId
* @param string|null $labelAppendix
*/
protected function renderLabel(string $fieldId): void
protected function renderLabel(string $fieldId, string $labelAppendix = null): void
{
echo '<label for="' . $fieldId . '">' . $this->label . '</label>';
$appendToLabel = !empty($labelAppendix) ? ' (' . $labelAppendix . ')' : '';

echo '<label for="' . $fieldId . '">' . $this->label . $appendToLabel . '</label>';
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Vierbeuter/WordPress/Feature/CustomField/ReadOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ class ReadOnly extends CustomField
* Renders the label's markup.
*
* @param string $fieldId
* @param string|null $labelAppendix
*/
protected function renderLabel(string $fieldId): void
protected function renderLabel(string $fieldId, string $labelAppendix = null): void
{
echo '<label>' . $this->label . '</label>';
$appendToLabel = !empty($labelAppendix) ? ' (' . $labelAppendix . ')' : '';

echo '<label>' . $this->label . $appendToLabel . '</label>';
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Vierbeuter/WordPress/Feature/CustomField/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ abstract class Selection extends CustomField
* Renders the label's markup.
*
* @param string $fieldId
* @param string|null $labelAppendix
*/
protected function renderLabel(string $fieldId): void
protected function renderLabel(string $fieldId, string $labelAppendix = null): void
{
echo '<label>' . $this->label . '</label>';
$appendToLabel = !empty($labelAppendix) ? ' (' . $labelAppendix . ')' : '';

echo '<label>' . $this->label . $appendToLabel . '</label>';
}

/**
Expand Down

0 comments on commit d859dd7

Please sign in to comment.