Skip to content

Commit

Permalink
feat(Widget): instroduce ControlAwareWidgetInterface/Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
whizark committed Jan 8, 2016
1 parent e115437 commit 30547cb
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/Component/Widget/ControlAwareWidgetInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Control Aware Widget Interface
*
* @author Whizark <devaloka@whizark.com>
* @see http://whizark.com
* @copyright Copyright (C) 2015 Whizark.
* @license MIT
*/

namespace Devaloka\Component\Widget;

/**
* Interface ControlAwareWidgetInterface
*
* @package Devaloka\Component\Widget
*
* @codeCoverageIgnore
*/
interface ControlAwareWidgetInterface extends WidgetInterface
{
/**
* Gets the Widget control options.
*
* @return mixed[] The control options.
*/
public function getControlOptions();

/**
* Renders a Widget settings form.
*
* @param mixed[] $instance The instance-specific Widget settings.
*
* @return string The rendered HTML.
*/
public function renderForm(array $instance);

/**
* Displays a Widget settings form.
*
* @param mixed[] $instance The instance-specific Widget settings.
*/
public function displayForm(array $instance);

/**
* Updates a Widget settings.
*
* @param mixed[] $newInstance The new settings.
* @param mixed[] $oldInstance The old settings.
*
* @return mixed[]|bool The settings to save or false to cancel saving.
*/
public function update(array $newInstance, array $oldInstance);
}
86 changes: 86 additions & 0 deletions src/Component/Widget/ControlAwareWidgetTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Control Aware Widget Trait
*
* @author Whizark <devaloka@whizark.com>
* @see http://whizark.com
* @copyright Copyright (C) 2015 Whizark.
* @license MIT
*/

namespace Devaloka\Component\Widget;

/**
* Class ControlAwareWidgetTrait
*
* @package Devaloka\Component\Widget
*
* @codeCoverageIgnore
*/
trait ControlAwareWidgetTrait
{
use WidgetTrait;

/**
* Gets the Widget control options.
*
* @return mixed[] The control options.
*/
public function getControlOptions()
{
/** @var \WP_Widget $this */

return $this->control_options;
}

/**
* Renders a Widget settings form.
*
* @param mixed[] $instance The instance-specific Widget settings.
*
* @return string The rendered HTML.
*/
public function renderForm(array $instance)
{
ob_start();

parent::form($instance);

return ob_get_clean();
}

/**
* Displays a Widget settings form.
*
* @param mixed[] $instance The instance-specific Widget settings.
*/
public function displayForm(array $instance)
{
echo $this->renderForm($instance);
}

/**
* Displays a Widget settings form.
*
* @see ControlAwareWidgetTrait::displayForm() :alias:
*
* @param mixed[] $instance The instance-specific Widget settings.
*/
public function form(array $instance)
{
$this->displayForm($instance);
}

/**
* Updates a Widget settings.
*
* @param mixed[] $newInstance The new settings.
* @param mixed[] $oldInstance The old settings.
*
* @return mixed[]|bool The settings to save or false to cancel saving.
*/
public function update($newInstance, $oldInstance)
{
return parent::update($newInstance, $oldInstance);
}
}

0 comments on commit 30547cb

Please sign in to comment.