0

I am using the Front End PM plugin and I am trying to convert the reply messages form and the create a new message form to submit by AJAX but I am having a bit of difficulty trying to figure out how the forms are actually being submitted naturally. If someone could help me convert the following code to an AJAX ready format I'd be really appreciative! Thanks in advance.

Here is the PHP code:

 function fep_send_message( $message = null, $override = array() ) {
if ( null === $message ) {
    $message = $_POST;
}
if ( ! empty( $message['fep_parent_id'] ) ) {
    $message['post_parent'] = absint( $message['fep_parent_id'] );
    $message['post_status'] = fep_get_option( 'reply_post_status', 'publish' );
    $message['message_title'] = __( 'RE:', 'front-end-pm' ). ' ' . wp_slash( fep_get_message_field( 'mgs_title', $message['post_parent'] ) );
    $message['message_to_id'] = fep_get_participants( $message['post_parent'], true );
} else {
    $message['post_status'] = fep_get_option( 'parent_post_status','publish' );
    $message['post_parent'] = 0;
}
$message = apply_filters( 'fep_filter_message_before_send', $message );
if ( empty( $message['message_title'] ) || empty( $message['message_content'] ) ) {
    return false;
}
// Create post array
$post = array(
    'post_title'    => $message['message_title'],
    'post_content'  => $message['message_content'],
    'post_status'   => $message['post_status'],
    'post_parent'   => $message['post_parent'],
    'post_type'     => 'message',
    'post_author'   => get_current_user_id(),
    'mgs_created'   => current_time( 'mysql', true ),
);

if ( $override && is_array( $override ) ) {
    $post = wp_parse_args( $override, $post );
}
if( ! $post['post_parent'] && 'threaded' === fep_get_message_view() ){
    $post['mgs_last_reply_by'] = $post['post_author'];
    $post['mgs_last_reply_excerpt'] = fep_get_the_excerpt_from_content( 50, $post['post_content'] );
    $post['mgs_last_reply_time'] = $post['mgs_created'];
}

$post = apply_filters( 'fep_filter_message_after_override', $post, $message );

foreach( $post as $k => $v ){
    if( 0 === strpos( $k, 'post_') ){
        $post[ str_replace( 'post_', 'mgs_', $k ) ] = $v;
        unset( $post[ $k ] );
    }
}
$post = wp_unslash( $post );

$new_message = new FEP_Message;
$message_id = $new_message->insert( $post );
// Insert the message into the database
if ( ! $message_id  ) {
    return false;
}
/*
$inserted_message = FEP_Message::get_instance( $message_id );
if( ! $inserted_message ){
    return false;
}
*/
if( ! empty( $message['message_to_id'] ) ){
    $message['message_to_id'] = (array) $message['message_to_id'];
    $message['message_to_id'][] = $new_message->mgs_author;
    $new_message->insert_participants( $message['message_to_id'] );
}
do_action( 'fep_action_message_after_send', $message_id, $message, $new_message );

fep_status_change( 'new', $new_message );

return $message_id;
}

Here is my current jquery attempt at AJAX:

$('#ajax-reply-form').submit(function(e){
                                e.preventDefault();
                               var formData = $('#ajax-reply-form').serialize();

                               $.ajax({
                                    url: ajaxurl,
                                    type: 'POST',
                                    data: {action: formData + '&action=fep_ajax_reply'},
                                    success: function (data) {
                                        console.log(data);
                                    }
                                });
                            });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.