Skip to content

Instantly share code, notes, and snippets.

Created April 17, 2013 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5401599 to your computer and use it in GitHub Desktop.
Save anonymous/5401599 to your computer and use it in GitHub Desktop.
<?php
/**
* Display the create new topic link on category page.
*
* @since 0.0.1
*
* @param string $text Optional. The link text. Default is 'Create New Topic'.
* @param string $before Optional. The text before link. Default is none.
* @param string $after Optional. The text after link. Default is none.
* @return string The final link.
*/
function vp_new_topic_link( $text = '', $before = '', $after = '') {
if ( ! is_user_logged_in() )
return;
global $wp_query;
$node_id = $wp_query->get_queried_object_id();
if ( is_category() ) {
$url = vp_get_page_url_by_slug( 'new', 'node=' . $node_id );
} else {
$url = vp_get_page_url_by_slug( 'new' );
}
if ( '' == $text )
$text = __( 'Create New Topic', 'v2press' );
$link = $before . '<a rel="nofollow" class="btn" href="' . $url . '">' . $text . '</a>' . $after;
echo $link;
}
/**
* The create new topic form.
*
* @since 0.0.1
*/
function vp_new_topic_form() {
ob_start();
vp_error_messages();
?>
<form id="vp-new-topic" action="" method="post">
<fieldset>
<?php if ( empty( $_GET['node'] ) ) { ?>
<p><?php wp_dropdown_categories( array( 'show_option_all' => __( 'Select Node', 'v2press' ) ) ); ?></p>
<?php } ?>
<p><input type="text" name="topic_title" placeholder="<?php _e( 'Topic title here', 'v2press' ); ?>" id="topic_title" class="form-field wider" value="" /></p>
<p><textarea name="topic_content_filtered" id="topic_content_filtered" class="form-field wider" rows="10" cols="20" ></textarea></p>
<p>
<input type="hidden" name="action" value="new_topic" />
<input type="hidden" name="new_topic_nonce" value="<?php echo wp_create_nonce( 'new-topic-nonce' ); ?>" />
<input type="submit" name="submit" class="btn" value="<?php _e( 'Create Topic', 'v2press' ); ?>" />
</p>
</fieldset>
</form>
<?php
echo ob_get_clean();
}
/**
* Do the create new topic form process.
*
* @since 0.0.1
*/
function vp_do_create_new_topic() {
if ( empty( $_POST['action'] ) || ( 'new_topic' != $_POST['action'] ) )
return;
if ( isset( $_POST['topic_title'] ) && wp_verify_nonce( $_POST['new_topic_nonce'], 'new-topic-nonce' ) ) {
if ( !empty( $_GET['node'] ) )
$node_id = (int) $_GET['node'];
else
$node_id = $_POST['cat'];
$title = $_POST['topic_title'];
$content_filtered = strip_tags( $_POST['topic_content_filtered'] );
$user_id = get_current_user_id();
// Node empty
if ( empty( $node_id ) || ( 0 == $node_id ) ) {
vp_errors()->add( 'node_empty', __( 'Please select a node', 'v2press' ) );
}
// Node id invalid
if ( !empty( $node_id ) ) {
$node = get_term_by( 'id', $node_id, 'category');
if ( !$node ) {
vp_errors()->add( 'node_invalid', 'Please do not cheating me.', 'v2press' );
}
}
// Title empty
if ( empty( $title ) ) {
vp_errors()->add( 'topic_title_empty', __( 'Please enter the topic title', 'v2press' ) );
}
// Content empty
if ( empty( $content_filtered ) ) {
vp_errors()->add( 'topic_content_empty', __( 'Please write something useful', 'v2press' ) );
}
$errors = vp_errors()->get_error_messages();
if( empty( $errors ) ) {
$data = array(
'post_author' => $user_id,
'post_category' => (array) $node_id,
'post_title' => $title,
'post_content_filtered' => $content_filtered,
'post_status' => 'publish'
);
require_once( VP_LIBS_PATH . '/markdown-extra.php');
$data['post_content'] = Markdown( $content_filtered );
$topic = wp_insert_post( $data );
if ( $topic ) {
$topic_url = get_permalink( $topic );
wp_redirect( $topic_url );
exit;
}
}
}
}
add_action( 'template_redirect', 'vp_do_create_new_topic' );
/**
* The topic edit links.
*
* @since 0.0.1
*/
function vp_edit_topic_links() {
$topic_id = get_the_ID();
if ( !is_user_logged_in() || !current_user_can( 'edit_post', $topic_id ) )
return;
$pub_time = get_the_time( 'U' );
$time_diff = abs( current_time( 'timestamp' ) - $pub_time );
// Cannot edit topic 15mins after created
if ( 900 < $time_diff )
return;
$edit_url = add_query_arg( 'edit', 'true', get_permalink() );
$link = '<span class="topic-edit">';
$link .= '<a rel="nofollow" href="' . $edit_url . '">' . __( 'edit', 'v2press' ) . '</a>';
$link .= '</span>';
echo $link;
}
/**
* If the current page the edit topic page.
*
* @since 0.0.1
*/
function vp_is_edit() {
if ( is_single() && isset( $_GET['edit'] ) && 'true' == $_GET['edit'] )
return true;
return false;
}
/**
* The edit topic form.
*
* @since 0.0.1
*/
function vp_edit_topic_form() {
$pub_time = get_the_time( 'U' );
$time_diff = abs( current_time( 'timestamp' ) - $pub_time );
// Cannot edit topic 15mins after created
if ( 900 < $time_diff ) {
echo '<p>' . __( 'You cannot edt this topic 15 minites after created.', 'v2press' ) . '</p>';
return;
}
ob_start();
$topic_id = get_query_var( 'p' );
$topic = get_post( $topic_id );
vp_error_messages();
?>
<form id="vp-edit-topic" action="" method="post">
<fieldset>
<p><input type="text" name="topic_title" id="topic_title" class="form-field wider" value="<?php echo $topic->post_title ?>" /></p>
<p><textarea name="topic_content_filtered" id="topic_content_filtered" class="form-field wider" rows="10" cols="20" ><?php echo $topic->post_content_filtered; ?></textarea></p>
<p>
<input type="hidden" name="action" value="edit_topic" />
<input type="hidden" name="edit_topic_nonce" value="<?php echo wp_create_nonce( 'edit-topic-nonce' ); ?>" />
<input type="submit" name="submit" class="btn" value="<?php _e( 'Submit Changes', 'v2press' ); ?>" />
</p>
</fieldset>
</form>
<?php
echo ob_get_clean();
}
/**
* Do the edit topic form process.
*
* @since 0.0.1
*/
function vp_do_edit_topic() {
if ( empty( $_POST['action'] ) || ( 'edit_topic' != $_POST['action'] ) )
return;
if ( isset( $_POST['topic_title'] ) && wp_verify_nonce( $_POST['edit_topic_nonce'], 'edit-topic-nonce' ) ) {
$topic_id = get_query_var( 'p' );
$title = $_POST['topic_title'];
$content_filtered = strip_tags( $_POST['topic_content_filtered'] );
// Title empty
if ( empty( $title ) ) {
vp_errors()->add( 'topic_title_empty', __( 'Please enter the topic title', 'v2press' ) );
}
// Content empty
if ( empty( $content_filtered ) ) {
vp_errors()->add( 'topic_content_empty', __( 'Please write something useful', 'v2press' ) );
}
$errors = vp_errors()->get_error_messages();
if( empty( $errors ) ) {
$data = array(
'ID' => $topic_id,
'post_title' => $title,
'post_content_filtered' => $content_filtered,
);
require_once( VP_LIBS_PATH . '/markdown-extra.php' );
$data['post_content'] = Markdown( $content_filtered );
$topic = wp_update_post( $data );
if ( $topic ) {
$topic_url = get_permalink( $topic );
wp_redirect( $topic_url );
exit;
}
}
}
}
add_action( 'template_redirect', 'vp_do_edit_topic' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment