Last active
January 27, 2021 21:52
-
-
Save rgadon107/60284a7562d730d61dd3d703a11200a8 to your computer and use it in GitHub Desktop.
Solution to Code Challenge 3 from "Refactoring Tweaks Workbook"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Code Challenge 3, from “Refactoring Tweaks Workbook” by Tonya Mork ( 2016, LeanPub (https://leanpub.com/littlegreenbookofrefactoringtweaks-workbook) ). */ | |
/*************************************************** | |
* | |
* Original code to be refactored. | |
* | |
**************************************************/ | |
class Form { | |
//.. | |
/** | |
* Render a repeatable group row | |
* @since 1.0.2 | |
* @param Field $field_group Field group field object | |
* @param string $remove_disabled Attribute string to disable the remove button | |
*/ | |
public function render_group_row( $field_group, $remove_disabled ) { | |
$field_group->perform_param_callback( 'before_group_row' ); | |
$closed_class = $field_group->options( 'closed' ) ? ' closed' : ''; | |
echo ' | |
<div class="postbox plugin-row plugin-repeatable-grouping', $closed_class, '" data-iterator="', $field_group->index, '">'; | |
if ( $field_group->args( 'repeatable' ) ) { | |
echo '<button type="button" ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="dashicons-before dashicons-no-alt plugin-remove-group-row"></button>'; | |
} | |
echo ' | |
<div class="pluginhandle" title="' , esc_attr__( 'Click to toggle', 'plugin' ), '"><br></div> | |
<h3 class="plugin-group-title pluginhandle-title"><span>', $field_group->replace_hash( $field_group->options( 'group_title' ) ), '</span></h3> | |
<div class="inside plugin-td plugin-nested plugin-field-list">'; | |
// Loop and render repeatable group fields | |
foreach ( array_values( $field_group->args( 'fields' ) ) as $field_args ) { | |
if ( 'hidden' == $field_args['type'] ) { | |
// Save rendering for after the metabox | |
$this->add_hidden_field( $field_args, $field_group ); | |
} else { | |
$field_args['show_names'] = $field_group->args( 'show_names' ); | |
$field_args['context'] = $field_group->args( 'context' ); | |
$field = $this->get_field( $field_args, $field_group )->render_field(); | |
} | |
} | |
if ( $field_group->args( 'repeatable' ) ) { | |
echo ' | |
<div class="plugin-row plugin-remove-field-row"> | |
<div class="plugin-remove-row"> | |
<button type="button" ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="button plugin-remove-group-row alignright">', $field_group->options( 'remove_button' ), '</button> | |
</div> | |
</div> | |
'; | |
} | |
echo ' | |
</div> | |
</div> | |
'; | |
$field_group->perform_param_callback( 'after_group_row' ); | |
} | |
} | |
/*************************************************** | |
* | |
* Refactored Code | |
* | |
* | |
* Relative file path: {plugin-name}/src/admin/class-form.php | |
* | |
**************************************************/ | |
namespace pluginName\admin\Form | |
class Form { | |
//.. | |
/** | |
* Render a repeatable group row | |
* @since 1.0.2 | |
* @param Field $field_group Field group field object | |
* @param string $remove_disabled Attribute string to disable the remove button | |
*/ | |
public function render_group_row( Field $field_group, $remove_disabled ) { | |
$field_group->perform_param_callback( 'before_group_row' ); | |
include __DIR__ . '/views/view-repeatable-grouping.php'; | |
$field_group->perform_param_callback( 'after_group_row' ); | |
} | |
} | |
/********************************************************************** | |
* | |
* Helper function ( within same namespace as Form::render_group_row() ). | |
* | |
***********************************************************************/ | |
/** | |
* Loop and render repeatable group fields. | |
* | |
* @since 1.0.0 | |
* @param Field $field_group | |
* @return void | |
*/ | |
function render_repeatable_group_fields( Field $field_group ) { | |
foreach ( array_values( $field_group->args( 'fields' ) ) as $field_args ) { | |
if ( 'hidden' == $field_args['type'] ) { | |
// Save rendering for after the metabox | |
$this->add_hidden_field( $field_args, $field_group ); | |
} else { | |
$field_args['show_names'] = $field_group->args( 'show_names' ); | |
$field_args['context'] = $field_group->args( 'context' ); | |
$field = $this->get_field( $field_args, $field_group )->render_field(); | |
} | |
} | |
} | |
//======================================================================== | |
// | |
// Relative file path: {plugin-name}/src/admin/views/view-repeatable-grouping.php | |
// | |
//========================================================================= | |
/* | |
* HTML for the repeatable field group in the plugin admin. | |
*/ | |
namespace pluginName\admin\Form; | |
$closed_class = $field_group->options( 'closed' ) ? ' closed' : ''; | |
?> | |
<div class="postbox plugin-row plugin-repeatable-grouping<?php $closed_class; ?>" data-iterator="<?php $field_group->index ?>"> | |
<?php | |
if ( $field_group->args( 'repeatable' ) ) { | |
echo ?><button type="button" <?php $remove_disabled; ?> data-selector="<?php $field_group->id(); ?> . '_repeat" class="dashicons-before dashicons-no-alt plugin-remove-group-row"></button> | |
<?php | |
} ?> | |
<div class="pluginhandle" title="<?php esc_attr__( 'Click to toggle', 'plugin' ); ?>"><br></div> | |
<h3 class="plugin-group-title pluginhandle-title"> | |
<span><?php $field_group->replace_hash( $field_group->options( 'group_title' ) ); ?></span> | |
</h3> | |
<div class="inside plugin-td plugin-nested plugin-field-list"> | |
<?php render_repeatable_group_fields( Field $field_group ); | |
if ( $field_group->args( 'repeatable' ) ) { | |
echo ?><div class="plugin-row plugin-remove-field-row"> | |
<div class="plugin-remove-row"> | |
<button type="button" <?php $remove_disabled; ?> data-selector="<?php $field_group->id(); ?>_repeat" | |
class="button plugin-remove-group-row alignright"><?php $field_group->options( 'remove_button' ); ?> | |
</button> | |
</div> | |
</div> | |
<?php | |
} ?> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment