true, ); foreach ( $ajax_events as $ajax_event => $status ) { if ( $status ) { add_action( 'wp_ajax_wpaf_' . $ajax_event, array( __CLASS__, $ajax_event ) ); add_action( 'wp_ajax_nopriv_wpaf_' . $ajax_event, array( __CLASS__, $ajax_event ) ); } } } /** * AJAX adding new vote */ public static function vote() { if ( ! check_ajax_referer( 'wpaf-security-nonce', 'security' ) ) { wp_die(); } $coupon_id = isset( $_POST['coupon_id'] ) ? absint( wp_unslash( $_POST['coupon_id'] ) ) : 0; $meta = isset( $_POST['meta'] ) ? sanitize_key( wp_unslash( $_POST['meta'] ) ) : ''; if ( $meta === "up" || $meta === "down" ) { $up_count = absint( get_post_meta( $coupon_id, '_up_count', true ) ); $down_count = absint( get_post_meta( $coupon_id, '_down_count', true ) ); if ( 0 === $up_count && 0 === $down_count ) { $up_count = count( array_filter( explode( ",", get_post_meta( $coupon_id, "_up", true ) ) ) ); $down_count = count( array_filter( explode( ",", get_post_meta( $coupon_id, "_down", true ) ) ) ); } if ( 'up' === $meta ) { $up_count++; update_post_meta( $coupon_id, '_up_count', $up_count ); } else { $down_count++; update_post_meta( $coupon_id, '_down_count', $down_count ); } $all_votes = $up_count + $down_count; $percent = $all_votes > 0 ? (int) ceil( $up_count / $all_votes * 100 ) : 100; /* translators: %d: integer percent of upvotes */ echo esc_html( sprintf( __( '%d%% Success', 'affcoupon' ), $percent ) ); } else { echo esc_html__( 'Failed', 'affcoupon' ); } wp_die(); } }