Skip to content

Commit

Permalink
Working ajax for adding new wiki page. Need to save the actual page now.
Browse files Browse the repository at this point in the history
  • Loading branch information
colegeissinger committed May 27, 2014
1 parent 861ae5e commit 7ce05d6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 11 deletions.
2 changes: 1 addition & 1 deletion assets/js/add-wiki.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions assets/js/src/add-wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
* @param event
*/
function wiki_add_submit( event ) {
var form_data = {};

event.preventDefault();

// Make sure we aren't already processing..
Expand All @@ -48,7 +50,14 @@
}

// Get the form data
var formData = $add_wiki_form.serializeArray();
$( '.wiki-form-field' ).each( function( field ) {
var $this = $( this );

form_data[ $this.attr( 'name' ) ] = $this.val();
});

// Get the wp_editor content
form_data[ 'wiki-content'] = tinyMCE.activeEditor.getContent();

// Update the messages element we are working on submitting
$processing = $( '#wiki-messages' ).removeClass().addClass( 'wiki-loading' ).text( 'Saving Wiki...' );
Expand All @@ -59,11 +68,11 @@
error: wiki_add_error,
data: {
nonce: $nonce,
data: formData
data: form_data
}
});
}

$add_wiki_form.on( 'submit', wiki_add_submit() );
$add_wiki_form.on( 'submit', wiki_add_submit );

} )( this, jQuery );
2 changes: 1 addition & 1 deletion includes/class-wiki-wiki-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function load_resources() {

// Load only on Add Wiki page
// @todo Add in checks against wiki pages and load conditionally
wp_enqueue_script( 'wiki-wiki', WIKI_WIKI_URL . 'assets/js/add-wiki.min.js', array( 'jquery' ), WIKI_WIKI_VERSION, true );
wp_enqueue_script( 'wiki-wiki', WIKI_WIKI_URL . 'assets/js/add-wiki.min.js', array( 'wp-util' ), WIKI_WIKI_VERSION, true );
}

/**
Expand Down
45 changes: 44 additions & 1 deletion includes/wiki-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,49 @@ public static function updated_messages( $messages ) {
}

public static function add_new_wiki() {
wp_send_json_success( array( 'done' => 'yup' ) );
// Check the user is logged in
if ( ! is_user_logged_in() ) {
wp_send_json_error( 'You are currently not signed in! Can\'t complete request!' );
} else {
if ( isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'wiki-wiki-add-wiki-nonce' ) ) {
$values = self::sanitize_form_data( $_POST['data'] );

} else {
wp_send_json_error( 'Invalid request!' );
}
}
}

/**
* Sanitize our wiki form data
*
* @param $data
*
* @return array|bool
*/
private static function sanitize_form_data( $data ) {

if ( ! isset( $data ) || ! is_array( $data) ) {
return false;
}

$cleaned = array();
foreach ( $data as $key => $val ) {
$key = sanitize_key( $key );

switch ( $key ) {
case 'wiki-content':
$cleaned[ $key ] = wp_kses_post( $val );
break;
case 'wiki-category':
case 'wiki-parent':
$cleaned[ $key ] = absint( $val );
break;
default:
$cleaned[ $key ] = sanitize_text_field( $val );
}
}

return $cleaned;
}
}
10 changes: 6 additions & 4 deletions templates/page-new-wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,29 @@
<div class="form-group">
<label for="wiki-title" class="col-sm-2 control-label input-lg">Title</label>
<div class="col-sm-10">
<input type="text" name="wiki-title" class="form-control" id="wiki-title" placeholder="<?php _e( 'Give it a good title... somethingy wimpy.', 'wiki_wiki' ); ?>">
<input type="text" name="wiki-title" class="form-control wiki-form-field" id="wiki-title" placeholder="<?php _e( 'Give it a good title... somethingy wimpy.', 'wiki_wiki' ); ?>">
</div>
</div>
<div class="form-group">
<label for="wiki-content" class="col-sm-2 control-label"><?php _e( 'Content', 'wiki_wiki' ); ?></label>
<div class="col-sm-10">
<?php wp_editor( '', 'wiki-content' ); ?>
<?php wp_editor( '', 'wiki-content', array(
'editor_class' => 'wiki-form-field',
) ); ?>
</div>
</div>
<div class="form-group">
<label for="wiki-category" class="col-sm-2 control-label"><?php _e( 'Category', 'wiki_wiki' ); ?></label>
<div class="col-sm-10">
<select name="" class="form-control" id="wiki-category">
<select name="wiki-category" class="form-control wiki-form-field" id="wiki-category">
<option value=""><?php _e( '-- Choose A Category --', 'wiki_wiki' ); ?></option>
</select>
</div>
</div>
<div class="form-group">
<label for="wiki-parent" class="col-sm-2 control-label"><?php _e( 'Parent Wiki', 'wiki_wiki' ); ?></label>
<div class="col-sm-10">
<select name="" class="form-control" id="wiki-parent">
<select name="wiki-parent" class="form-control wiki-form-field" id="wiki-parent">
<option value=""><?php _e( '-- Assign To A Parent --', 'wiki_wiki' ); ?></option>
</select>
<span class="help-block"><?php _e( 'Assign this new page as a child to an existing wiki page.', 'wiki_wiki' ); ?></span>
Expand Down
2 changes: 1 addition & 1 deletion wiki_wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@

add_action( 'init', array( 'Wiki_Wiki_Utils', 'init' ) );
add_action( 'init', array( 'Wiki_wiki_Post_Type', 'init' ) );
add_action( 'wp_ajax_wiki_wiki_add_wiki', array( 'Wiki_Wiki_Post_Type', 'add_new_wiki' ) );

// Front-end Actions
if ( ! is_admin() ) {
add_action( 'wp_enqueue_scripts', array( 'Wiki_Wiki_Utils', 'load_resources' ) );
add_action( 'template_redirect', array( 'Wiki_Wiki_Routes', 'template_redirect' ) );
add_action( 'wp_ajax_wiki_wiki_add_wiki', array( 'Wiki_Wiki_Post_Type', 'add_new_wiki' ) );
}

// Admin Actions
Expand Down

0 comments on commit 7ce05d6

Please sign in to comment.