Skip to content

Commit

Permalink
Merge branch 'release/0.21.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
garex committed Aug 27, 2017
2 parents b8d6fcc + 98f466c commit 99e99e1
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 87 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ matrix:

sudo: false

dist: precise

addons:
hosts:
- wpti.dev
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

## Changelog ##

### 0.21.1 ###

Add extension points for Email addon.

* No new functionality was added!


### 0.21 ###

Social sharing of test results and feedback metabox
Expand Down
9 changes: 5 additions & 4 deletions README.md

Large diffs are not rendered by default.

118 changes: 118 additions & 0 deletions src/Doer/AbstractEditor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

abstract class WpTesting_Doer_AbstractEditor extends WpTesting_Doer_AbstractDoer
{
/**
* Post meta options, groupped by name, that is filter tag also.
*
* Can be rendered by name.
*
* @var array
*/
private $metaOptions = array();

/**
* @var string
*/
private $firstMetaOptionsKey = null;

/**
* @return boolean
*/
protected function hasMetaOptionsInRequest()
{
if (empty($this->metaOptions)) {
return false;
}

return !is_null($this->getRequestValue($this->firstMetaOptionsKey));
}

public function updateMetaOptions(WpTesting_Model_AbstractParent $post)
{
foreach ($this->metaOptions as $metaOptions) {
foreach ($metaOptions as $key => $metaOption) {
if (isset($metaOption['type']) && $metaOption['type'] == 'header') {
continue;
}
$value = $this->getRequestValue($key);
$this->wp->updatePostMeta($post->getId(), $key, $value);
}
}
}

/**
* @param string $groupName
* @param array $options
*
* @return self
*/
protected function addMetaOptions($groupName, array $options)
{
$this->metaOptions[$groupName] = $this->wp->applyFilters($groupName, $options);

if (empty($this->firstMetaOptionsKey)) {
$this->firstMetaOptionsKey = key($options);
}

return $this;
}

/**
* @param string $groupName
*/
protected function renderMetaOptions($groupName)
{
$options = $this->metaOptions[$groupName];

foreach ($options as $key => $option) {
$option += array(
'default' => '',
'attributes' => '',
);
if (!isset($option['value'])) {
$option['value'] = $this->wp->getCurrentPostMeta($key);
}
if (isset($option['defaultOnAdd']) && $this->isAddAction()) {
$option['default'] = $option['defaultOnAdd'];
}
if ($option['value'] == '') {
$option['value'] = $option['default'];
}
if (empty($option['type'])) {
$option['type'] = 'checkbox';
}
if (empty($option['placeholder'])) {
$option['placeholder'] = '';
}
if (empty($option['break'])) {
$option['break'] = false;
}
$options[$key] = $option;
}

$root = dirname(dirname(__FILE__)) . '/Template/';
$this->output($root . 'AbstractEditor/metabox-options.php', array(
'options' => $options,
));
}

protected function jsonResponse(array $data)
{
header('Content-type: application/json');

$j = new WpTesting_Component_Json();
echo $j->encode($data);

exit;
}

private function isAddAction()
{
$screen = $this->wp->getCurrentScreen();
if (!$screen instanceof WP_Screen) {
return null;
}
return 'add' == $screen->action;
}
}
96 changes: 20 additions & 76 deletions src/Doer/TestEditor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class WpTesting_Doer_TestEditor extends WpTesting_Doer_AbstractDoer
class WpTesting_Doer_TestEditor extends WpTesting_Doer_AbstractEditor
{

/**
Expand Down Expand Up @@ -86,15 +86,22 @@ public function customizeUi($screen)
->addMetaBox('wpt_edit_scores', __('Edit Scores', 'wp-testing'), array($this, 'renderEditScores'), 'wpt_test')
->addMetaBox('wpt_quick_fill_scores', __('Quick Fill Scores', 'wp-testing'), array($this, 'renderQuickFillScores'), 'wpt_test')
->addMetaBox('wpt_edit_formulas', __('Edit Formulas', 'wp-testing'), array($this, 'renderEditFormulas'), 'wpt_test')
->addAction('wp_testing_test_store_all_before', array($this, 'updateMetaOptions'))
->addAction('save_post', array($this, 'saveTest'), WpTesting_WordPress_IPriority::PRIORITY_DEFAULT, 2)
;
$this
->addSubmitMiscOptions()
->addTestPageOptions()
->addResultPageOptions()
;
// Respect metabox sort order
if ($this->isWordPressAlready('3.4')) {
$this->wp->addFilter('wp_terms_checklist_args', array($this, 'filterTermsChecklistArgs'), WpTesting_WordPress_IPriority::PRIORITY_DEFAULT, 2);
} else {
$this->wp->addFilter('wp_get_object_terms', array($this, 'filterForceSortObjectTerms'), WpTesting_WordPress_IPriority::PRIORITY_DEFAULT, 4);
}
$this->wp->doAction('wp_testing_editor_customize_ui_after');

return $this;
}

Expand Down Expand Up @@ -222,7 +229,7 @@ public function renderContentEditorButtons($editorId)
$this->output('Test/Editor/content-editor-buttons');
}

private function getSubmitMiscOptions()
private function addSubmitMiscOptions()
{
$options = array(
'wpt_publish_on_home' => array(
Expand All @@ -236,15 +243,15 @@ private function getSubmitMiscOptions()
unset($options['wpt_publish_on_home']);
}

return $this->wp->applyFilters('wpt_test_editor_submit_misc_options', $options);
return $this->addMetaOptions('wpt_test_editor_submit_misc_options', $options);
}

public function renderSubmitMiscOptions()
{
$this->renderMetaboxOptions($this->getSubmitMiscOptions());
$this->renderMetaOptions('wpt_test_editor_submit_misc_options');
}

private function getTestPageOptions()
private function addTestPageOptions()
{
$options = array(
'wpt_test_page_show_progress_meter' => array(
Expand All @@ -270,15 +277,15 @@ private function getTestPageOptions()
),
);

return $this->wp->applyFilters('wpt_test_editor_test_page_options', $options);
return $this->addMetaOptions('wpt_test_editor_test_page_options', $options);
}

public function renderTestPageOptions()
{
$this->renderMetaboxOptions($this->getTestPageOptions());
$this->renderMetaOptions('wpt_test_editor_test_page_options');
}

private function getResultPageOptions()
private function addResultPageOptions()
{
$options = array(
'wpt_result_page_show_scales_diagram' => array(
Expand All @@ -299,12 +306,12 @@ private function getResultPageOptions()
),
);

return $this->wp->applyFilters('wpt_test_editor_result_page_options', $options);
return $this->addMetaOptions('wpt_test_editor_result_page_options', $options);
}

public function renderResultPageOptions()
{
$this->renderMetaboxOptions($this->getResultPageOptions());
$this->renderMetaOptions('wpt_test_editor_result_page_options');
}

/**
Expand Down Expand Up @@ -346,30 +353,13 @@ public function renderEditFormulas($item)
public function saveTest($id, $item)
{
$test = $this->createTest($item);
if (!$test->getId()) {
return;
}

$metaOptions = array_keys(
$this->getSubmitMiscOptions()
+ $this->getTestPageOptions()
+ $this->getResultPageOptions()
);
$isUpdatable = ($test->getId() && $this->hasMetaOptionsInRequest());

// Update metadata only when we have appropriate keys
$isFullEdit = (!is_null($this->getRequestValue($metaOptions[0])));
if (!$isFullEdit) {
// Update test only when we have it and at least metaoptions
if (!$isUpdatable) {
return;
}

foreach ($metaOptions as $metaOptionKey) {
if (isset($metaOptionKey['type']) && $metaOptionKey['type'] == 'header') {
continue;
}
$metaOptionValue = $this->getRequestValue($metaOptionKey);
$this->wp->updatePostMeta($test->getId(), $metaOptionKey, $metaOptionValue);
}

try {
$test->storeAll();
if ($this->isAjax()) {
Expand Down Expand Up @@ -401,13 +391,6 @@ public function saveTest($id, $item)
}
}

private function jsonResponse(array $data)
{
header('Content-type: application/json');
echo json_encode($data);
exit;
}

/**
* Emulate behavior of redirect_post(), that append message=1 to edit url.
*
Expand Down Expand Up @@ -470,43 +453,4 @@ private function isTestTaxonomy()
{
return preg_match('/^wpt_/', $this->getRequestValue('taxonomy'));
}

private function renderMetaboxOptions($options)
{
foreach ($options as $key => $option) {
$option += array(
'default' => '',
);
$option['value'] = $this->wp->getCurrentPostMeta($key);
if (isset($option['defaultOnAdd']) && $this->isAddAction()) {
$option['default'] = $option['defaultOnAdd'];
}
if ($option['value'] == '') {
$option['value'] = $option['default'];
}
if (empty($option['type'])) {
$option['type'] = 'checkbox';
}
if (empty($option['placeholder'])) {
$option['placeholder'] = '';
}
if (empty($option['break'])) {
$option['break'] = false;
}
$options[$key] = $option;
}

$this->output('Test/Editor/metabox-options', array(
'options' => $options,
));
}

private function isAddAction()
{
$screen = $this->wp->getCurrentScreen();
if (!$screen instanceof WP_Screen) {
return null;
}
return 'add' == $screen->action;
}
}
3 changes: 3 additions & 0 deletions src/Model/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* @method string getType() Gets the current value for type
* @method WpTesting_Model_Test setName(string $name) Sets the value for name (url unique part)
* @method string getName() Gets the current value for name (url unique part)
* @method integer getAuthorId() Gets the current value for author's id
* @method WpTesting_Model_Test setAuthorId(integer $authorId) Sets the current value for author's id
* @method WpTesting_Model_Scale[] buildScalesWithRangeOnce() Build scales and setup their ranges from test's questions
* @method WpTesting_Model_Formula[] buildFormulasOnce() @see WpTesting_Model_Test::buildFormulas
*/
Expand All @@ -46,6 +48,7 @@ class WpTesting_Model_Test extends WpTesting_Model_AbstractParent
'excerpt' => 'post_excerpt',
'type' => 'post_type',
'name' => 'post_name',
'author_id' => 'post_author',
);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?php /* @var array $options */ ?>
<?php $optionsKeys = array_keys($options) ?>
<?php $lastKey = end($optionsKeys) ?>
<?php foreach ($options as $key => $option): ?>
Expand All @@ -14,9 +15,12 @@
echo ($option['value']) ? 'checked="checked"' : '' ?> /> <?php
echo $option['title']
?></label>
<?php elseif ('text' == $option['type']): ?>
<?php elseif (in_array($option['type'], array('text', 'email'))): ?>
<label><?php echo $option['title'] ?></label>
<input type="text" name="<?php echo $key ?>" value="<?php echo $option['value'] ?>" placeholder="<?php echo $option['placeholder'] ?>" />
<input <?php echo $option['attributes'] ?> type="<?php echo $option['type'] ?>" name="<?php echo $key ?>" value="<?php echo $option['value'] ?>" placeholder="<?php echo $option['placeholder'] ?>" />
<?php elseif ('textarea' == $option['type']): ?>
<label><?php echo $option['title'] ?></label>
<textarea <?php echo $option['attributes'] ?> name="<?php echo $key ?>" placeholder="<?php echo $option['placeholder'] ?>"><?php echo $option['value'] ?></textarea>
<?php elseif ('header' == $option['type']): ?>
<strong><?php echo $option['title'] ?></strong>
<?php elseif ('radio' == $option['type']): ?>
Expand Down
Loading

0 comments on commit 99e99e1

Please sign in to comment.