Skip to content
Snippets Groups Projects
Unverified Commit 573bfb70 authored by Bartek Jaskulski's avatar Bartek Jaskulski
Browse files

feat: add strong typing to the package

parent 398bb2eb
No related branches found
No related tags found
3 merge requests!28release: 3.0.0,!23Feature/strong typing pp,!19Add strong typing for 3.0 version
Pipeline #5964 passed
Showing
with 265 additions and 345 deletions
......@@ -6,13 +6,17 @@
* @var string $value
*
* @var string $template_name Real field template.
*
*/
?>
<?php echo $renderer->render('input', [
<?php
echo $renderer->render(
'input',
[
'field' => $field,
'renderer' => $renderer,
'name_prefix' => $name_prefix,
'value' => $value,
]); ?>
]
);
......@@ -7,6 +7,13 @@
* @var string $value
*
* @var string $template_name Real field template.
*
*/
echo $renderer->render('input', ['field' => $field, 'renderer' => $renderer, 'name_prefix' => $name_prefix, 'value' => $value]);
echo $renderer->render(
'input',
[
'field' => $field,
'renderer' => $renderer,
'name_prefix' => $name_prefix,
'value' => $value,
]
);
......@@ -6,12 +6,15 @@
* @var string $value
*
* @var string $template_name Real field template.
*
*/
?>
<?php echo $renderer->render('input', [
<?php
echo $renderer->render(
'input',
[
'field' => $field,
'renderer' => $renderer,
'name_prefix' => $name_prefix,
'value' => $value,
]); ?>
]
);
......@@ -13,15 +13,23 @@ $media_container_id = 'media_' . sanitize_key( $field->get_id() );
id="<?php echo \esc_attr( $field->get_id() ); ?>"/>
<div class="custom-img-container">
<?php if ( $value ) : ?>
<img src="<?php echo $value ?>" alt="" width="100"/>
<img src="<?php echo $value; ?>" alt="" width="100"/>
<?php endif; ?>
</div>
<p class="hide-if-no-js">
<a class="upload-custom-img <?php if ( $value ): ?>hidden<?php endif ?>" href="<?php echo $value ?>">
<?php _e( 'Set image', 'wp-forms' ) ?>
<a class="upload-custom-img
<?php
if ( $value ) :
?>
hidden<?php endif ?>" href="<?php echo $value; ?>">
<?php _e( 'Set image', 'wp-forms' ); ?>
</a>
<a class="delete-custom-img <?php if ( ! $value ): ?>hidden<?php endif ?>" href="#">
<?php _e( 'Remove image', 'wp-forms' ) ?>
<a class="delete-custom-img
<?php
if ( ! $value ) :
?>
hidden<?php endif ?>" href="#">
<?php _e( 'Remove image', 'wp-forms' ); ?>
</a>
</p>
</div>
......
......@@ -7,6 +7,13 @@
* @var string $value
*
* @var string $template_name Real field template.
*
*/
echo $renderer->render('input', ['field' => $field, 'renderer' => $renderer, 'name_prefix' => $name_prefix, 'value' => $value]);
echo $renderer->render(
'input',
[
'field' => $field,
'renderer' => $renderer,
'name_prefix' => $name_prefix,
'value' => $value,
]
);
......@@ -6,12 +6,15 @@
* @var string $value
*
* @var string $template_name Real field template.
*
*/
?>
<?php echo $renderer->render('input', [
<?php
echo $renderer->render(
'input',
[
'field' => $field,
'renderer' => $renderer,
'name_prefix' => $name_prefix,
'value' => $value,
]); ?>
]
);
......@@ -6,7 +6,6 @@
* @var string $value
*
* @var string $template_name Real field template.
*
*/
?>
......@@ -14,17 +13,29 @@
<td style="padding-left:0;">
<p class="submit">
<input
<?php if ( $field->has_classes() ): ?>class="<?php echo esc_attr( $field->get_classes() ); ?>"<?php endif; ?>
<?php
if ( $field->has_classes() ) :
?>
class="<?php echo esc_attr( $field->get_classes() ); ?>"<?php endif; ?>
<?php foreach ( $field->get_attributes( [] ) as $key => $value ) : ?>
<?php echo $key ?>="<?php echo esc_attr( $value ); ?>"
<?php echo $key; ?>="<?php echo esc_attr( $value ); ?>"
<?php endforeach; ?>
type="<?php echo esc_attr( $field->get_type() ); ?>"
name="<?php echo esc_attr( $name_prefix ); ?>[<?php echo esc_attr( $field->get_name() ); ?>]"
id="<?php echo esc_attr( $field->get_id() ); ?>"
value="<?php echo esc_html( $field->get_label() ); ?>"
<?php if ( $field->is_required() ): ?>required="required"<?php endif; ?>
<?php if ( $field->is_disabled() ): ?>disabled="disabled"<?php endif; ?>
<?php if ( $field->is_readonly() ): ?>readonly="readonly"<?php endif; ?>
<?php
if ( $field->is_required() ) :
?>
required="required"<?php endif; ?>
<?php
if ( $field->is_disabled() ) :
?>
disabled="disabled"<?php endif; ?>
<?php
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
/>
</p>
</td>
......
......@@ -6,7 +6,6 @@
* @var string $value
*
* @var string $template_name Real field template.
*
*/
?>
<?php
......@@ -22,7 +21,10 @@ if( empty( $value ) || is_string( $value ) ) {
<input type="hidden" name="<?php echo $name_prefix . '[' . $field->get_name() . ']'; ?>" value="no"/>
<?php endif; ?>
<?php if ($field->get_type() === 'checkbox' && $field->has_sublabel()): ?><label><?php endif; ?>
<?php
if ( $field->get_type() === 'checkbox' && $field->has_sublabel() ) :
?>
<label><?php endif; ?>
<div class="clone-wrapper">
<input
type="<?php echo \esc_attr( $field->get_type() ); ?>"
......@@ -37,13 +39,24 @@ if( empty( $value ) || is_string( $value ) ) {
placeholder="<?php echo \esc_html( $field->get_placeholder() ); ?>"
<?php endif; ?>
<?php foreach ($field->get_attributes() as $key => $atr_val):
echo $key.'="'.\esc_attr($atr_val).'"'; ?>
<?php
foreach ( $field->get_attributes() as $key => $atr_val ) :
echo $key . '="' . \esc_attr( $atr_val ) . '"';
?>
<?php endforeach; ?>
<?php if ($field->is_required()): ?>required="required"<?php endif; ?>
<?php if ($field->is_disabled()): ?>disabled="disabled"<?php endif; ?>
<?php if ($field->is_readonly()): ?>readonly="readonly"<?php endif; ?>
<?php
if ( $field->is_required() ) :
?>
required="required"<?php endif; ?>
<?php
if ( $field->is_disabled() ) :
?>
disabled="disabled"<?php endif; ?>
<?php
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
<?php if ( \in_array( $field->get_type(), [ 'number', 'text', 'hidden' ] ) ) : ?>
value="<?php echo \esc_html( $text_value ); ?>"
<?php else : ?>
......
......@@ -6,12 +6,15 @@
* @var string $value
*
* @var string $template_name Real field template.
*
*/
?>
<?php echo $renderer->render('input', [
<?php
echo $renderer->render(
'input',
[
'field' => $field,
'renderer' => $renderer,
'name_prefix' => $name_prefix,
'value' => $value,
]); ?>
]
);
......@@ -11,7 +11,10 @@
<input type="hidden" name="<?php echo $name_prefix . '[' . $field->get_name() . ']'; ?>" value="no"/>
<?php endif; ?>
<?php if ($field->get_type() === 'checkbox' && $field->has_sublabel()): ?><label><?php endif; ?>
<?php
if ( $field->get_type() === 'checkbox' && $field->has_sublabel() ) :
?>
<label><?php endif; ?>
<input
type="<?php echo \esc_attr( $field->get_type() ); ?>"
......@@ -26,13 +29,24 @@
placeholder="<?php echo \esc_html( $field->get_placeholder() ); ?>"
<?php endif; ?>
<?php foreach ($field->get_attributes() as $key => $atr_val):
echo $key.'="'.\esc_attr($atr_val).'"'; ?>
<?php
foreach ( $field->get_attributes() as $key => $atr_val ) :
echo $key . '="' . \esc_attr( $atr_val ) . '"';
?>
<?php endforeach; ?>
<?php if ($field->is_required()): ?>required="required"<?php endif; ?>
<?php if ($field->is_disabled()): ?>disabled="disabled"<?php endif; ?>
<?php if ($field->is_readonly()): ?>readonly="readonly"<?php endif; ?>
<?php
if ( $field->is_required() ) :
?>
required="required"<?php endif; ?>
<?php
if ( $field->is_disabled() ) :
?>
disabled="disabled"<?php endif; ?>
<?php
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
<?php if ( \in_array( $field->get_type(), [ 'number', 'text', 'hidden' ] ) ) : ?>
value="<?php echo \esc_html( $value ); ?>"
<?php else : ?>
......
<?php
/**
* @var \WPDesk\Forms\Field $field
* @var string $name_prefix
* @var string $value
*/
\wp_nonce_field( $field->get_meta_value( 'action' ), $name_prefix . '[' . $field->get_name() . ']' );
......@@ -9,7 +9,11 @@
<?php if ( $field->has_description() ) : ?>
<tr>
<td style="padding-left:0;" colspan="2">
<p <?php if ( $field->has_classes() ): ?>class="<?php echo $field->get_classes(); ?>"<?php endif; ?>><?php echo wp_kses_post( $field->get_description() ); ?></p>
<p
<?php
if ( $field->has_classes() ) :
?>
class="<?php echo $field->get_classes(); ?>"<?php endif; ?>><?php echo wp_kses_post( $field->get_description() ); ?></p>
</td>
</tr>
<?php endif; ?>
<?php
/**
* @var \WPDesk\Forms\Field $field
* @var string $name_prefix
* @var string[] $value
*/
?>
<select class="wc-product-search" multiple="multiple" style="width: 50%;"
......@@ -16,8 +16,11 @@
foreach ( (array) $value as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true,
false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
echo '<option value="' . esc_attr( $product_id ) . '"' . selected(
true,
true,
false
) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
......
......@@ -7,22 +7,43 @@
?>
<select
id="<?php echo esc_attr( $field->get_id() ); ?>"
<?php if ($field->has_classes()): ?>class="<?php echo esc_attr( $field->get_classes() ); ?>"<?php endif; ?>
<?php
if ( $field->has_classes() ) :
?>
class="<?php echo esc_attr( $field->get_classes() ); ?>"<?php endif; ?>
name="<?php echo esc_attr( $name_prefix ); ?>[<?php echo esc_attr( $field->get_name() ); ?>]<?php echo $field->is_multiple() ? '[]' : ''; ?>"
<?php foreach ( $field->get_attributes() as $key => $attr_val ) : ?>
<?php echo esc_attr( $key ); ?>="<?php echo esc_attr( $attr_val ); ?>"
<?php endforeach; ?>
<?php if ($field->is_required()): ?>required="required"<?php endif; ?>
<?php if ($field->is_disabled()): ?>disabled="disabled"<?php endif; ?>
<?php if ($field->is_readonly()): ?>readonly="readonly"<?php endif; ?>
<?php if ($field->is_multiple()): ?>multiple="multiple"<?php endif; ?>
<?php
if ( $field->is_required() ) :
?>
required="required"<?php endif; ?>
<?php
if ( $field->is_disabled() ) :
?>
disabled="disabled"<?php endif; ?>
<?php
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
<?php
if ( $field->is_multiple() ) :
?>
multiple="multiple"<?php endif; ?>
>
<?php if ( $field->has_placeholder() ): ?><option value=""><?php echo esc_html( $field->get_placeholder() ); ?></option><?php endif; ?>
<?php
if ( $field->has_placeholder() ) :
?>
<option value=""><?php echo esc_html( $field->get_placeholder() ); ?></option><?php endif; ?>
<?php foreach ( $field->get_possible_values() as $possible_value => $label ) : ?>
<option
<?php if ( $possible_value === $value || (is_array($value) && in_array($possible_value, $value)) || (is_numeric($possible_value) && is_numeric($value) && (int) $possible_value === (int) $value )): ?>selected="selected"<?php endif; ?>
<?php
if ( $possible_value === $value || ( is_array( $value ) && in_array( $possible_value, $value ) ) || ( is_numeric( $possible_value ) && is_numeric( $value ) && (int) $possible_value === (int) $value ) ) :
?>
selected="selected"<?php endif; ?>
value="<?php echo esc_attr( $possible_value ); ?>"
><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
......
......@@ -8,16 +8,34 @@
<textarea
id="<?php echo esc_attr( $field->get_id() ); ?>"
<?php if ( $field->has_classes() ): ?>class="<?php echo esc_attr( $field->get_classes() ); ?>"<?php endif; ?>
<?php
if ( $field->has_classes() ) :
?>
class="<?php echo esc_attr( $field->get_classes() ); ?>"<?php endif; ?>
name="<?php echo esc_attr( $name_prefix ); ?>[<?php echo esc_attr( $field->get_name() ); ?>]"
<?php foreach ( $field->get_attributes() as $key => $attr_val ) : ?>
<?php echo esc_attr( $key ); ?>="<?php echo esc_attr( $attr_val ); ?>"
<?php endforeach; ?>
<?php if ( $field->is_required() ): ?>required="required"<?php endif; ?>
<?php if ( $field->is_disabled() ): ?>disabled="disabled"<?php endif; ?>
<?php if ( $field->is_readonly() ): ?>readonly="readonly"<?php endif; ?>
<?php if ( $field->is_multiple() ): ?>multiple="multiple"<?php endif; ?>
<?php
if ( $field->is_required() ) :
?>
required="required"<?php endif; ?>
<?php
if ( $field->is_disabled() ) :
?>
disabled="disabled"<?php endif; ?>
<?php
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
<?php
if ( $field->is_multiple() ) :
?>
multiple="multiple"<?php endif; ?>
<?php if ( $field->has_placeholder() ): ?>placeholder="<?php echo esc_html( $field->get_placeholder() ); ?>"<?php endif; ?>
<?php
if ( $field->has_placeholder() ) :
?>
placeholder="<?php echo esc_html( $field->get_placeholder() ); ?>"<?php endif; ?>
><?php echo esc_html( $value ); ?></textarea>
......@@ -13,14 +13,14 @@
<?php
$id = uniqid( 'wyswig_' );
$editor_settings = array(
'textarea_name' => esc_attr( $name_prefix ) . '[' . esc_attr( $field->get_name() ) . ']'
);
$editor_settings = [
'textarea_name' => esc_attr( $name_prefix ) . '[' . esc_attr( $field->get_name() ) . ']',
];
wp_editor( wp_kses_post( $value ), $id, $editor_settings );
?>
<script type="text/javascript">
(function () {
ShopMagic.wyswig.init('<?php echo $id; ?>');
ShopMagic.wyswig.init('<?php echo esc_attr( $id ); ?>');
}());
</script>
<?php
namespace Tests;
use WPDesk\Forms\Form\AbstractForm;
class TestForm extends \PHPUnit\Framework\TestCase
{
const FORM1_ID = 'test_form';
const FORM1_FORM_DATA = [ 'test' => true ];
const FORM1_UPDATED_FORM_DATA = [ 'test666' => true ];
private $form;
protected function setUp(){
// Create a new instance from the Abstract Class
$this->form = $this->getMockBuilder( AbstractForm::class )
->enableOriginalConstructor()
->setMethods(['get_form_id'])
->getMockForAbstractClass();
$this->form->method( 'get_form_id' )->willReturn( self::FORM1_ID );
$this->form->method( 'create_form_data' )->willReturn( self::FORM1_FORM_DATA );
}
protected function getForm(){
return clone $this->form;
}
/**
* Test getting form id.
*/
public function testFormId()
{
$form = $this->getForm();
$this->assertEquals(self::FORM1_ID, $form->get_form_id());
}
/**
* Test getting form data.
*/
public function testFormData()
{
$form = $this->getForm();
$this->assertSame( self::FORM1_FORM_DATA, $form->get_form_data());
}
/**
* Test updated form data.
*/
public function testUpdatedFormData()
{
$form = $this->getForm();
$form->update_form_data( self::FORM1_UPDATED_FORM_DATA );
$this->assertSame( array_merge( self::FORM1_FORM_DATA, self::FORM1_UPDATED_FORM_DATA ), $form->get_form_data());
}
}
\ No newline at end of file
<?php
namespace Tests;
use WPDesk\Forms\Form\AbstractForm;
use WPDesk\Forms\Form\FormsCollection;
class TestFormCollection extends \PHPUnit\Framework\TestCase {
const FORM1_ID = 'test_form';
const FORM2_ID = 'test_form2';
const FORM1_FORM_DATA = [ 'test' => true ];
const FORM2_FORM_DATA = [ 'test2' => 'potato' ];
const FORM1_PREFIXED_FORM_DATA = [ 'test_form_test' => true ];
const FORM1_UPDATED_FORM_DATA = [ 'test666' => true ];
private $formConditionalTrue;
private $formConditionalFalse;
protected function setUp() {
$this->formConditionalTrue = $this->getMockBuilder( AbstractForm::class )
->enableOriginalConstructor()
->setMethods( [ 'get_form_id', 'is_active' ] )
->getMockForAbstractClass();
$this->formConditionalTrue->method( 'get_form_id' )->willReturn( self::FORM1_ID );
$this->formConditionalTrue->method( 'is_active' )->willReturn( true );
$this->formConditionalTrue->method( 'create_form_data' )->willReturn( self::FORM1_FORM_DATA );
$this->formConditionalFalse = $this->getMockBuilder( AbstractForm::class )
->enableOriginalConstructor()
->setMethods( [ 'get_form_id', 'is_active' ] )
->getMockForAbstractClass();
$this->formConditionalFalse->method( 'get_form_id' )->willReturn( self::FORM2_ID );
$this->formConditionalFalse->method( 'is_active' )->willReturn( false );
$this->formConditionalFalse->method( 'create_form_data' )->willReturn( self::FORM2_FORM_DATA );
}
protected function getFormConditionalTrue() {
return clone $this->formConditionalTrue;
}
protected function getFormConditionalFalse() {
return clone $this->formConditionalFalse;
}
/**
* Test adding and checking single form.
*/
public function testIfFormExists() {
$collection = new FormsCollection();
$collection->add_form( $this->getFormConditionalTrue() );
$this->assertTrue( $collection->is_form_exists( self::FORM1_ID ) );
}
/**
* Test adding and checking multiple forms.
*/
public function testIfFormsExists() {
$collection = new FormsCollection();
$collection->add_forms( [
$this->getFormConditionalTrue(),
$this->getFormConditionalFalse(),
] );
$this->assertTrue( $collection->is_form_exists( self::FORM1_ID ) );
$this->assertTrue( $collection->is_form_exists( self::FORM2_ID ) );
}
/**
* Test getting single form. AbstractForm object is expected
*/
public function testGettingExistingForm() {
$collection = new FormsCollection();
$collection->add_form( $this->getFormConditionalTrue() );
$this->assertInstanceOf(
AbstractForm::class,
$collection->get_form( self::FORM1_ID )
);
}
/**
* Test getting not existing single form.
*/
public function testGettingNotExistingForm() {
$collection = new FormsCollection();
$collection->add_form( $this->getFormConditionalTrue() );
$this->expectException( \OutOfRangeException::class );
$collection->get_form( '123456' );
}
/**
* Test returned data.
*/
public function testReturnedFormsData() {
$collection = new FormsCollection();
$collection->add_forms( [
$this->getFormConditionalTrue(),
$this->getFormConditionalFalse(),
] );
$this->assertSame( self::FORM1_FORM_DATA, $collection->get_forms_data() );
}
/**
* Test returned prefixed data.
*/
public function testReturnedPrefixedFormsData() {
$collection = new FormsCollection();
$collection->add_forms( [
$this->getFormConditionalTrue(),
$this->getFormConditionalFalse(),
] );
$this->assertSame( self::FORM1_PREFIXED_FORM_DATA, $collection->get_forms_data( true ) );
}
/**
* Test returned updated data.
*/
public function testReturnedUpdatedFormsData() {
$collection = new FormsCollection();
$collection->add_forms( [
$this->getFormConditionalTrue(),
$this->getFormConditionalFalse(),
] );
$form = $collection->get_form( self::FORM1_ID );
$form->update_form_data( self::FORM1_UPDATED_FORM_DATA );
$this->assertSame( array_merge( self::FORM1_FORM_DATA, self::FORM1_UPDATED_FORM_DATA ), $collection->get_forms_data() );
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment