Skip to content

Commit

Permalink
form handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tareq1988 committed Oct 9, 2015
1 parent 8324540 commit 8f9c901
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 50 deletions.
2 changes: 1 addition & 1 deletion admin-menu-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
$class_menu = str_replace( $search_array, $replace_array, $class_menu );
?>
<p><strong><?php printf( 'class-%s.php', str_replace( '_', '-', strtolower( $_POST['class_name'] ) ) ); ?></strong></p>
<pre style="overflow-y: scroll;width:100%;"><?php echo htmlentities( $class_menu ); ?></pre>
<pre class="prettyprint" style="overflow-y: scroll;width:100%;"><?php echo htmlentities( $class_menu ); ?></pre>
<?php } ?>

<form class="form-horizontal" method="post">
Expand Down
28 changes: 28 additions & 0 deletions assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,32 @@ footer.footer {
color: #767676;
text-align: center;
border-top: 1px solid #e5e5e5;
}

.social-links {
text-align: center;
margin-bottom: 20px;
}

ul.social-buttons {
display: inline-block;
padding-left: 0;
margin-bottom: 0;
list-style: none;
}

ul.social-buttons li {
display: inline-block;
padding: 5px 8px;
line-height: 1;
}

ul.social-buttons iframe {
border: none;
overflow: hidden;
}

pre.prettyprint {
border: 1px solid #eee !important;
padding: 15px !important;
}
25 changes: 24 additions & 1 deletion footer.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
<footer class="footer">
<div class="container">
<div class="row">
<p>Made by <a href="https://twitter.com/tareq_cse">@tareq_cse</a></p>
<!--
<div class="social-links">
<ul class="social-buttons">
<li>
<iframe class="github-btn" src="https://ghbtns.com/github-btn.html?user=tareq1988&amp;repo=wp-generators&amp;type=watch&amp;count=true" width="100" height="20" title="Star on GitHub"></iframe>
</li>
<li>
<iframe class="github-btn" src="https://ghbtns.com/github-btn.html?user=tareq1988&amp;repo=wp-generators&amp;type=fork&amp;count=true" width="102" height="20" title="Fork on GitHub"></iframe>
</li>
<li class="follow-btn">
<a href="https://twitter.com/tareq_cse" class="twitter-follow-button" data-show-count="false">Follow @tareq_cse</a>
</li>
<li class="tweet-btn">
<a href="https://twitter.com/share" class="twitter-share-button" data-text="Checkout this awesome #WordPress Code Generator, its pretty cool!" data-via="tareq_cse">Tweet</a>
</li>
</ul>
</div>
-->
<p>Made by <a href="http://tareq.wedevs.com">Tareq Hasan</a></p>
</div>
</div>
</footer>

<script src="assets/js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="assets/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
<script src="assets/js/script.js" type="text/javascript" charset="utf-8"></script>
<!--
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
-->
</body>
</html>
112 changes: 104 additions & 8 deletions form.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ function build_rows( $edit = false ) {

switch ($input_type) {
case 'text':
case 'number':
$value = '';

if ( $edit ) {
$value = sprintf( '<?php echo esc_attr( $item->%s ); ?>', $_POST['name'][$key] );
}

$rows .= $indent . $tab . $tab . sprintf( '<input type="text" name="%1$s" id="%1$s" class="regular-text" placeholder="<?php echo esc_attr( \'%2$s\', \'%3$s\' ); ?>" value="%4$s"%5$s />', $_POST['name'][$key], $_POST['label'][$key], $_POST['textdomain'], $value, $required ) . "\n";
$rows .= $indent . $tab . $tab . sprintf( '<input type="%6$s" name="%1$s" id="%1$s" class="regular-text" placeholder="<?php echo esc_attr( \'%2$s\', \'%3$s\' ); ?>" value="%4$s"%5$s />', $_POST['name'][$key], $_POST['label'][$key], $_POST['textdomain'], $value, $required, $input_type ) . "\n";
break;

case 'textarea':
Expand Down Expand Up @@ -77,9 +78,9 @@ function build_rows( $edit = false ) {

if ( ! empty( $_POST['help'][ $key ] ) ) {
if ( $input_type == 'textarea' ) {
$rows .= $indent . $tab . $tab . '<p class="description">' . $_POST['help'][$key] . "</p>\n";
$rows .= $indent . $tab . $tab . '<p class="description"><?php _e(\'' . $_POST['help'][$key] . "', '{$_POST['textdomain']}' ); ?></p>\n";
} else {
$rows .= $indent . $tab . $tab . '<span class="description">' . $_POST['help'][$key] . "</span>\n";
$rows .= $indent . $tab . $tab . '<span class="description"><?php _e(\'' . $_POST['help'][$key] . "', '{$_POST['textdomain']}' ); ?></span>\n";
}
}

Expand All @@ -105,32 +106,88 @@ function build_rows( $edit = false ) {
</div>

<?php if ( isset( $_POST['submit'] ) ) {
$form_code = file_get_contents( 'templates/form.php' );
$new_rows = build_rows();
$edit_rows = build_rows( true );
$form_code = file_get_contents( 'templates/form.php' );
$form_handler = file_get_contents( 'templates/form-handler.php' );
$form_functions = file_get_contents( 'templates/form-functions.php' );

$new_rows = build_rows();
$edit_rows = build_rows( true );
$tab = ' ';

$form_fields = '';
$required_form_fields = '';
$form_fields_array = "array(\n";

foreach ($_POST['input_type'] as $key => $input_type) {
switch ($input_type) {
case 'number':
$form_fields .= $tab . $tab . sprintf( '$%1$s = isset( $_POST[\'%1$s\'] ) ? intval( $_POST[\'%1$s\'] ) : 0;', $_POST['name'][$key] ) . "\n";
break;

case 'textarea':
$form_fields .= $tab . $tab . sprintf( '$%1$s = isset( $_POST[\'%1$s\'] ) ? wp_kses_post( $_POST[\'%1$s\'] ) : \'\';', $_POST['name'][$key] ) . "\n";
break;

default:
$form_fields .= $tab . $tab . sprintf( '$%1$s = isset( $_POST[\'%1$s\'] ) ? sanitize_text_field( $_POST[\'%1$s\'] ) : \'\';', $_POST['name'][$key] ) . "\n";
break;
}

if ( $_POST['required'][ $key ] == 'yes' ) {
$required_form_fields .= $tab . $tab . sprintf( 'if ( ! $%s ) {', $_POST['name'][ $key ] ) . "\n";
$required_form_fields .= $tab . $tab . $tab . sprintf( '$errors[] = __( \'Error: %s is required\', \'%s\' );', $_POST['label'][ $key ], $_POST['textdomain'] ) . "\n";
$required_form_fields .= $tab . $tab . "}\n\n";
}

$form_fields_array .= $tab . $tab . $tab . sprintf( '\'%1$s\' => $%1$s,', $_POST['name'][ $key ] ) . "\n";
}

$form_fields_array .= $tab . $tab . ");";

// var_dump($form_fields);
// var_dump($required_form_fields);
// var_dump($form_fields_array);

$search_array = array(
'%heading%',
'%textdomain%',
'%nonce%',
'%submit_name%',
'%page_slug%',
'%singular_name%',
'%mysql_table%',
);

$replace_array = array(
$_POST['heading'],
$_POST['textdomain'],
$_POST['nonce'],
$_POST['submit_name'],
$_POST['page_slug'],
$_POST['singular_name'],
$_POST['mysql_table'],
);

$new_code = $edit_code = str_replace( $search_array, $replace_array, $form_code );
$new_code = str_replace( array( '%rows%', '%submit_new_text%' ), array( $new_rows, $_POST['submit_new_text'] ), $new_code );
$edit_code = str_replace( array( '%rows%', '%submit_new_text%' ), array( $edit_rows, $_POST['submit_edit_text'] ), $edit_code );

$form_handler = str_replace( $search_array, $replace_array, $form_handler );
$form_handler = str_replace( array( '%form_fields%', '%required_form_fields%', '%form_fields_array%' ), array( $form_fields, $required_form_fields, $form_fields_array ), $form_handler );

$form_functions = str_replace( $search_array, $replace_array, $form_functions );
?>
<p><strong>add-item.php</strong></p>
<pre style="overflow-y: scroll;width:100%;"><?php echo htmlentities( $new_code ); ?></pre>
<pre class="prettyprint"><?php echo htmlentities( $new_code ); ?></pre>

<p><strong>edit-item.php</strong></p>
<pre style="overflow-y: scroll;width:100%;"><?php echo htmlentities( $edit_code ); ?></pre>
<pre class="prettyprint"><?php echo htmlentities( $edit_code ); ?></pre>

<p><strong>class-form-handler.php</strong></p>
<pre class="prettyprint"><?php echo htmlentities( $form_handler ); ?></pre>

<p><strong>functions-<?php echo $_POST['singular_name']; ?>.php</strong></p>
<pre class="prettyprint"><?php echo htmlentities( $form_functions ); ?></pre>
<?php } ?>

<form class="form-horizontal" method="post">
Expand Down Expand Up @@ -177,6 +234,43 @@ function build_rows( $edit = false ) {
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label" for="page_slug">Page Slug</label>
<div class="col-md-4">
<div class="input-group">
<span class="input-group-addon">admin.php?page=</span>
<input id="page_slug" name="page_slug" type="text" placeholder="test-page" class="form-control input-md" value="<?php echo isset( $_POST['page_slug' ] ) ? $_POST['page_slug'] : ''; ?>">
</div>
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label" for="singular_name">Singular Name</label>
<div class="col-md-4">
<input id="singular_name" name="singular_name" type="text" placeholder="book" class="form-control input-md" value="<?php echo isset( $_POST['singular_name' ] ) ? $_POST['singular_name'] : ''; ?>">
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label" for="mysql_table">MySQL Table Name</label>
<div class="col-md-4">
<div class="input-group">
<span class="input-group-addon">wp_</span>
<input id="mysql_table" name="mysql_table" type="text" placeholder="comments" class="form-control input-md" value="<?php echo isset( $_POST['mysql_table' ] ) ? $_POST['mysql_table'] : ''; ?>">
</div>
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label" for="date_field">Date Field</label>
<div class="col-md-4">
<label class="checkbox-inline" for="date_field-1">
<input type="hidden" name="date_field" value="off">
<input type="checkbox" name="date_field" id="date_field-1" value="on"> Add date field on insert statement
</label>
</div>
</div>

<table class="table table-striped">
<thead>
<tr>
Expand All @@ -199,6 +293,7 @@ function build_rows( $edit = false ) {
<td>
<select name="input_type[<?php echo $key; ?>]" class="form-control input-md">
<option value="text" <?php echo $input_type == 'text' ? 'selected' : ''; ?>>Text</option>
<option value="number" <?php echo $input_type == 'number' ? 'selected' : ''; ?>>Number</option>
<option value="textarea" <?php echo $input_type == 'textarea' ? 'selected' : ''; ?>>Text Area</option>
<option value="select" <?php echo $input_type == 'select' ? 'selected' : ''; ?>>Select Dropdown</option>
<option value="checkbox" <?php echo $input_type == 'checkbox' ? 'selected' : ''; ?>>Checkbox</option>
Expand Down Expand Up @@ -236,6 +331,7 @@ function build_rows( $edit = false ) {
<td>
<select name="input_type[1]" class="form-control input-md">
<option value="text">Text</option>
<option value="number">Number</option>
<option value="textarea">Text Area</option>
<option value="select">Select Dropdown</option>
<option value="checkbox">Checkbox</option>
Expand Down
7 changes: 3 additions & 4 deletions list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@

<?php if ( isset( $_POST['submit'] ) ) { ?>
<p><strong><?php printf( 'class-%s-list-table.php', $_POST['singular_name'] ); ?></strong></p>
<pre style="overflow-y: scroll;width:100%;"><?php echo htmlentities( $list_table_codes ); ?></pre>
<pre class="prettyprint" style="overflow-y: scroll;width:100%;"><?php echo htmlentities( $list_table_codes ); ?></pre>

<p><strong><?php printf( '%s-functions.php', $_POST['singular_name'] ); ?></strong></p>
<pre style="overflow-y: scroll;width:100%;"><?php echo htmlentities( $list_table_functions ); ?></pre>
<pre class="prettyprint" style="overflow-y: scroll;width:100%;"><?php echo htmlentities( $list_table_functions ); ?></pre>

<p><strong><?php printf( '%s-views.php', $_POST['singular_name'] ); ?></strong></p>
<pre style="overflow-y: scroll;width:100%;"><?php echo htmlentities( $list_table_view ); ?></pre>
<pre class="prettyprint" style="overflow-y: scroll;width:100%;"><?php echo htmlentities( $list_table_view ); ?></pre>
<?php } ?>

<form class="form-horizontal" method="post">
Expand All @@ -97,7 +97,6 @@
<label class="col-md-4 control-label" for="singular_name">Singular Name</label>
<div class="col-md-4">
<input id="singular_name" name="singular_name" type="text" placeholder="book" class="form-control input-md" required>

</div>
</div>

Expand Down
48 changes: 48 additions & 0 deletions templates/form-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* Insert a new leave policy
*
* @param array $args
*/
function prefix_insert_%singular_name%( $args = array() ) {
global $wpdb;

$defaults = array(
'id' => null,
'date' => current_time( 'mysql' )
);

$args = wp_parse_args( $args, $defaults );
$table_name = $wpdb->prefix . '%mysql_table%';

// some basic validation
if ( empty( $args['name'] ) ) {
return new WP_Error( 'no-name', __( 'No name provided.', 'wp-error' ) );
}

if ( ! intval( $args['value'] ) ) {
return new WP_Error( 'no-value', __( 'No duration provided.', 'wp-error' ) );
}

// remove row id to determine if new or update
$row_id = (int) $args['id'];
unset( $args['id'] );

if ( ! $row_id ) {

// insert a new
if ( $wpdb->insert( $table_name, $args ) ) {
return $wpdb->insert_id;
}

} else {

// do update method here
if ( $wpdb->update( $table_name, $args, array( 'id' => $row_id ) ) ) {
return $row_id;
}
}

return false;
}
Loading

0 comments on commit 8f9c901

Please sign in to comment.