Support » Plugins » Hacks » Inserts into procedures

  • Hello, I’ve been playing arround with procedures and tried putting the comment insert into a procedure. I’ve got some trouble and was wondering if you guys could take a look.

    function wp_insert_comment($commentdata) {
    	$comment_ID = comment_ID;
    	$comment_post_ID = comment_post_ID;
    	$comment_author = comment_author;
    	$comment_author_email = comment_author_email;
    	$comment_author_url = comment_author_url;
    	$comment_author_IP = comment_author_IP;
    	$comment_date = comment_date;
    	$comment_date_gmt = comment_date_gmt;
    	$comment_content = comment_content;
    	$comment_karma = comment_karma;
    	$comment_approved = comment_approved;
    	$comment_agent = comment_agent;
    	$comment_type = comment_type;
    	$comment_parent = comment_parent;
    	$user_id = user_id;
    	global $wpdb;
    	extract(wp_unslash($commentdata), EXTR_SKIP);
    
    	if ( ! isset($comment_author_IP) )
    		$comment_author_IP = '';
    	if ( ! isset($comment_date) )
    		$comment_date = current_time('mysql');
    	if ( ! isset($comment_date_gmt) )
    		$comment_date_gmt = get_gmt_from_date($comment_date);
    	if ( ! isset($comment_parent) )
    		$comment_parent = 0;
    	if ( ! isset($comment_approved) )
    		$comment_approved = 1;
    	if ( ! isset($comment_karma) )
    		$comment_karma = 0;
    	if ( ! isset($user_id) )
    		$user_id = 0;
    	if ( ! isset($comment_type) )
    		$comment_type = '';
    
    	//$data = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id');
    	$wpdb->insert($wpdb->comments, '$comment_ID','$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_karma', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id');
    	//$wpdb->insert($wpdb->comments, 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id');
    
    	$id = (int) $wpdb->insert_id;
    
    	if ( $comment_approved == 1 )
    		wp_update_comment_count($comment_post_ID);
    
    	$comment = get_comment($id);
    
    	/**
    	 * Fires immediately after a comment is inserted into the database.
    	 *
    	 * @since 2.8.0
    	 *
    	 * @param int $id      The comment ID.
    	 * @param obj $comment Comment object.
    	 */
    	do_action( 'wp_insert_comment', $id, $commen );
    
    	wp_cache_set( 'last_changed', microtime(), 'comment' );
    
    	return $id;
    }

    So here I send all the variables to another function, because it didn’t work with the array solution that was previously used. So i send the variables to

    function _insert_replace_helper( $table, $comment_ID, $comment_post_ID, $comment_author, $comment_author_email, $comment_author_url, $comment_author_IP, $comment_date, $comment_date_gmt, $comment_content, $comment_karma, $comment_approved, $comment_agent, $comment_type, $comment_parent, $user_id, $format = null, $type = 'INSERT' ) {
    
    $sql = "CALL CommentsProcedure('$comment_ID', '$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_karma', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id')";
    
    		return $this->query( $this->prepare( $sql, $comment_ID, $comment_post_ID, $comment_author, $comment_author_email, $comment_author_url, $comment_author_IP, $comment_date, $comment_date_gmt, $comment_content, $comment_karma, $comment_approved, $comment_agent, $comment_type, $comment_parent, $user_id ) );
    	}

    And heres the procedure i made

    DELIMITER $$
    
    CREATE PROCEDURE <code>CommentsProcedure</code>(IN comment_ID bigint(20), IN comment_post_ID bigint(20), IN comment_author tinytext, IN comment_author_email VARCHAR(100), IN comment_author_url VARCHAR(200), IN comment_author_IP VARCHAR(100), IN comment_date datetime, IN comment_date_gmt datetime, IN comment_content text,
     IN comment_karma int(11), IN comment_approved VARCHAR(20), IN comment_agent VARCHAR(255), IN comment_type VARCHAR(20), IN comment_parent bigint(20), IN user_ID bigint(20))
    BEGIN
    	INSERT INTO wp_comments(comment_ID, comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_karma, comment_approved, comment_agent, comment_type, comment_parent, user_id)
    	VALUES(comment_ID, comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_karma, comment_approved, comment_agent, comment_type, comment_parent, user_id);
    
    END
    $$
    
    DELIMITER ;

    I have no clue what i do wrong, would be helpful if someone could give me some guidance.(Sorry for the bad english, not native speaker, feel free to ask if something is unclear.)
    Thanks in advance

Viewing 1 replies (of 1 total)
  • Without checking into your database manipulation techniques, I note the following.

    You seem to have modified wp_insert_comment().

    This code

    $comment_ID = comment_ID;
    $comment_post_ID = comment_post_ID;
    $comment_author = comment_author;
    $comment_author_email = comment_author_email;
    $comment_author_url = comment_author_url;
    $comment_author_IP = comment_author_IP;
    $comment_date = comment_date;
    $comment_date_gmt = comment_date_gmt;
    $comment_content = comment_content;
    $comment_karma = comment_karma;
    $comment_approved = comment_approved;
    $comment_agent = comment_agent;
    $comment_type = comment_type;
    $comment_parent = comment_parent;
    $user_id = user_id;

    Doesn’t do anything at all. Unless you are referencing constants, which I think you are not. You seem to want to extract the fields of $commentdata and make them local variables, but that is exactly what

    extract(wp_unslash($commentdata), EXTR_SKIP);

    would apparently do.

    It appears you are not having an actual need for the solution you seek and you are just playing around, but I’m not interested at this point in learning the same things, so I will leave it to someone else to provide feedback, if at all.

Viewing 1 replies (of 1 total)
  • The topic ‘Inserts into procedures’ is closed to new replies.