From 3f2b47e7c16c7885fe9f4c1076f2ace8375cc50e Mon Sep 17 00:00:00 2001 From: ORYC Date: Mon, 7 Mar 2016 09:03:16 +0700 Subject: [PATCH] validate, sanitize and esc data --- dw-reactions.php | 94 +++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/dw-reactions.php b/dw-reactions.php index 8109e69..d2e399e 100644 --- a/dw-reactions.php +++ b/dw-reactions.php @@ -111,14 +111,14 @@ public function layout( $post_id = false, $button = true, $count = true ) {
- +
- - - - - - + + + + + +
@@ -146,7 +146,7 @@ public function count_like_layout( $post_id = false ) { $count = get_post_meta( $post_id, 'dw_reaction_' . $reaction ); if ( !empty( $count ) ) { - echo ''.count( $count ).''; + echo ''.esc_attr( count( $count ) ).''; } } echo ''; @@ -171,45 +171,49 @@ public function enqueue_script() { public function ajax() { check_admin_referer( '_dw_reaction_action', 'nonce' ); - if ( empty( $_POST['post'] ) ) { - wp_send_json_error( array( 'message' => __( 'Missing post', 'reactions' ) ) ); + $post_id = intval( $_POST['post'] ); + $type = sanitize_title( $_POST['type'] ); + + if ( empty( $post_id ) ) { + wp_send_json_error( array( 'message' => __( 'Missing post.', 'reactions' ) ) ); } - if ( empty( $_POST['type'] ) ) { - wp_send_json_error( array( 'message' => __( 'Missing type', 'reactions' ) ) ); + if ( empty( $type ) ) { + wp_send_json_error( array( 'message' => __( 'Missing type.', 'reactions' ) ) ); } // delete old reactions - $is_liked = $this->is_liked( get_current_user_id(), $_POST['post'] ); + $is_liked = $this->is_liked( get_current_user_id(), $post_id ); if ( $is_liked ) { - delete_post_meta( $_POST['post'], $is_liked, get_current_user_id() ); - if ( isset( $_POST['vote_type'] ) && 'unvote' == $_POST['vote_type'] ) { - $total = get_post_meta( $_POST['post'], 'dw_reaction_total_liked', true ) ? get_post_meta( $_POST['post'], 'dw_reaction_total_liked', true ) : 0; + delete_post_meta( $post_id, $is_liked, get_current_user_id() ); + $vote_type = sanitize_title( $_POST['vote_type' ] ); + if ( isset( $vote_type ) && 'unvote' == $vote_type ) { + $total = get_post_meta( $post_id, 'dw_reaction_total_liked', true ) ? get_post_meta( $post_id, 'dw_reaction_total_liked', true ) : 0; if ( $total >= 0 ) { $total = (int) $total - 1; - update_post_meta( $_POST['post'], 'dw_reaction_total_liked', $total ); + update_post_meta( $post_id, 'dw_reaction_total_liked', $total ); } ob_start(); - $this->count_like_layout( $_POST['post'] ); + $this->count_like_layout( $post_id ); $content = ob_get_clean(); wp_send_json_success( array( 'html' => $content, 'type' => 'unvoted' ) ); } } if ( !$is_liked ) { - $total = get_post_meta( $_POST['post'], 'dw_reaction_total_liked', true ) ? get_post_meta( $_POST['post'], 'dw_reaction_total_liked', true ) : 0; + $total = get_post_meta( $post_id, 'dw_reaction_total_liked', true ) ? get_post_meta( $post_id, 'dw_reaction_total_liked', true ) : 0; $total = (int) $total + 1; - update_post_meta( $_POST['post'], 'dw_reaction_total_liked', $total ); + update_post_meta( $post_id, 'dw_reaction_total_liked', $total ); } - $count = get_post_meta( $_POST['post'], 'dw_reaction_' . $_POST['type'] ); + $count = get_post_meta( $post_id, 'dw_reaction_' . $type ); // update to database - add_post_meta( $_POST['post'], 'dw_reaction_' . $_POST['type'], get_current_user_id() ); + add_post_meta( $post_id, 'dw_reaction_' . $type, get_current_user_id() ); ob_start(); - $this->count_like_layout( $_POST['post'] ); + $this->count_like_layout( $post_id ); $content = ob_get_clean(); wp_send_json_success( array( 'html' => $content, 'type' => 'voted' ) ); @@ -315,50 +319,50 @@ public function setting_layout() { ?>

- +
-

+

- + - +

-

+

-

-

+

+

-

-

-

-

+

+

+

+


-

+

-

wp-content/themes/<Your theme folder>/.', 'reactions' ); ?>

-

archive.php, single.php, post.php or page.php also.', 'reactions' ); ?>

-

<?php while (have_posts()) : the_post(); ?>.', 'reactions' ); ?>

-

<?php if (function_exists('dw_reactions')) { dw_reactions() } ?>.", 'reactions' ); ?>

+

wp-content/themes/<Your theme folder>/.', 'reactions' ); ?>

+

archive.php, single.php, post.php or page.php also.', 'reactions' ); ?>

+

<?php while (have_posts()) : the_post(); ?>.', 'reactions' ); ?>

+

<?php if (function_exists('dw_reactions')) { dw_reactions() } ?>.", 'reactions' ); ?>


-

[reactions] into the selected post/page and it will embed reactions into that post/page only.', 'reactions' ); ?>

-

[reactions id="1"], where 1 is the ID of the post/page.', 'reactions' ); ?>

-

[reactions count=false button=true].', 'reactions' ) ?>

-

[reactions count=true button=false].', 'reactions' ) ?>

+

[reactions] into the selected post/page and it will embed reactions into that post/page only.', 'reactions' ); ?>

+

[reactions id="1"], where 1 is the ID of the post/page.', 'reactions' ); ?>

+

[reactions count=false button=true].', 'reactions' ) ?>

+

[reactions count=true button=false].', 'reactions' ) ?>

- +