Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @author Jegtheme
- */
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- if ( ! class_exists( 'JNews_Like_Count' ) ) {
- /**
- * Class JNews Like Count
- */
- Class JNews_Like_Count
- {
- /**
- * @var JNews_Like_Count
- */
- private static $instance;
- /**
- * @var string
- *
- */
- private $cookie = "jnews_like_count_setting";
- private $table_name = "jnews_post_like";
- /**
- * @return JNews_Like_Count
- */
- public static function getInstance()
- {
- if (null === static::$instance)
- {
- static::$instance = new static();
- }
- return static::$instance;
- }
- /**
- * JNews_Like_Count constructor.
- *
- */
- public function __construct()
- {
- $this->init_like();
- $this->setup_hook();
- }
- /**
- * Create table for post like count
- *
- */
- private function init_like()
- {
- global $wpdb;
- $table_name = $wpdb->prefix . $this->table_name;
- if( $wpdb->get_var("show tables like '$table_name'") != $table_name ) {
- $sql = "CREATE TABLE " . $table_name . " (
- `id` bigint(11) NOT NULL AUTO_INCREMENT,
- `post_id` int(11) NOT NULL,
- `date_time` datetime NOT NULL,
- `value` int(2) NOT NULL,
- `ip` varchar(40) NOT NULL,
- `user_id` int(11) NOT NULL DEFAULT '0',
- `cookie` varchar(25) NOT NULL,
- PRIMARY KEY (`id`)
- )";
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
- dbDelta($sql);
- }
- }
- /**
- * Setup hook function
- *
- */
- public function setup_hook()
- {
- add_action( 'init', array($this, 'set_cookie') );
- add_action( 'wp_ajax_jnews_like', array($this, 'ajax_do_action_type') );
- add_action( 'wp_ajax_nopriv_jnews_like', array($this, 'ajax_do_action_type') );
- }
- /**
- * Set cookie
- *
- */
- public function set_cookie()
- {
- if ( !isset($_COOKIE[$this->cookie]) ) {
- $cookietimeout = get_option("cookie_timeout", time() + 24 * 60 * 60 * 30);
- setcookie($this->cookie, uniqid(), $cookietimeout, '/');
- }
- }
- /**
- * Get total like and dislike
- * @param integer $post_id
- * @param string $type
- * @return string $result
- */
- public function get_count($post_id, $type = null)
- {
- global $wpdb;
- $table_name = $wpdb->prefix . $this->table_name;
- if ( $type === "like" ) {
- $result = $wpdb->get_var(
- $wpdb->prepare(
- "SELECT COUNT(id) FROM $table_name WHERE post_id = %d AND value >= 0",
- $post_id
- )
- );
- return $result;
- }
- if ( $type === "dislike" ) {
- $result = $wpdb->get_var(
- $wpdb->prepare(
- "SELECT COUNT(id) FROM $table_name WHERE post_id = %d AND value <= 0",
- $post_id
- )
- );
- return $result;
- }
- return false;
- }
- /**
- * Get status if have liked or disliked
- * @param integer $post_id
- * @return string $status
- */
- public function get_status($post_id)
- {
- global $wpdb;
- $table_name = $wpdb->prefix . $this->table_name;
- $ip = $this->get_ip();
- $cookie = ( isset($_COOKIE[$this->cookie]) ) ? $_COOKIE[$this->cookie] : '' ;
- $status = $wpdb->get_var(
- $wpdb->prepare(
- "SELECT value FROM $table_name WHERE post_id = %d AND ip = %s AND cookie = '%s'",
- $post_id, $ip, $cookie
- )
- );
- return $status;
- }
- /**
- * Main function
- * @param integer $post_id
- * @param string $type
- * @return json
- */
- public function do_action_type($post_id, $type)
- {
- $status = $this->get_status($post_id);
- $result = '';
- global $wpdb;
- // if like update into dislike and then
- if ( $status === "1" || $status === "-1" ) {
- $result = $this->update_value($post_id, $type);
- if ( $result ) {
- echo json_encode(array(
- 'response' => 2,
- 'message' => esc_html__( 'Thanks for your vote!', 'jnews-plugin'),
- 'like' => $this->get_count($post_id, 'like'),
- 'dislike' => $this->get_count($post_id, 'dislike')
- ));
- } else {
- echo json_encode(array(
- 'response' => 0,
- 'message' => esc_html__( 'Internal DB Error!', 'jnews-plugin'),
- ));
- }
- }
- if ( $status === NULL ) {
- $result = $this->insert_value($post_id, $type);
- if ( $result ) {
- echo json_encode(array(
- 'response' => 1,
- 'message' => esc_html__( 'Thanks for your vote!', 'jnews-plugin'),
- 'like' => $this->get_count($post_id, 'like'),
- 'dislike' => $this->get_count($post_id, 'dislike'),
- 'type' => $type
- ));
- } else {
- echo json_encode(array(
- 'response' => 0,
- 'message' => esc_html__( 'Internal DB Error!', 'jnews-plugin'),
- ));
- }
- }
- }
- /**
- * Main function for ajax
- *
- */
- public function ajax_do_action_type()
- {
- $this->do_action_type($_POST['post_id'], $_POST['type']);
- die();
- }
- /**
- * Insert data into database
- * @param integer $post_id
- * @param string $type
- * @return bool
- */
- public function insert_value($post_id, $type)
- {
- global $wpdb;
- $table_name = $wpdb->prefix . $this->table_name;
- $ip = $this->get_ip();
- $cookie = ( isset($_COOKIE[$this->cookie]) ) ? $_COOKIE[$this->cookie] : '' ;
- $value = ($type === 'dislike') ? -1 : 1 ;
- $query = $wpdb->prepare(
- "INSERT INTO $table_name SET
- post_id = %d,
- date_time = '" . date( 'Y-m-d H:i:s' ) . "',
- user_id = %d,
- cookie = %s,
- ip = %s,
- value = %d",
- $post_id, get_current_user_id(), $cookie, $ip, $value
- );
- $success = $wpdb->query($query);
- return $success;
- }
- /**
- * Update data value into database
- * @param integer $post_id
- * @param string $type
- * @return bool
- */
- public function update_value($post_id, $type)
- {
- global $wpdb;
- $table_name = $wpdb->prefix . $this->table_name;
- $ip = $this->get_ip();
- $cookie = ( isset($_COOKIE[$this->cookie]) ) ? $_COOKIE[$this->cookie] : '' ;
- $value = ($type === 'dislike') ? -1 : 1 ;
- $query = $wpdb->prepare(
- "UPDATE $table_name SET
- value = %d,
- date_time = '" . date( 'Y-m-d H:i:s' ) . "',
- user_id = %d
- WHERE post_id = %d AND cookie = %s AND ip = %s",
- $value, get_current_user_id(), $post_id, $cookie, $ip
- );
- $success = $wpdb->query($query);
- return $success;
- }
- /**
- * Get actual ip address
- * @return string
- */
- public function get_ip()
- {
- if (getenv('HTTP_CLIENT_IP')) {
- $ip = getenv('HTTP_CLIENT_IP');
- } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
- $ip = getenv('HTTP_X_FORWARDED_FOR');
- } elseif (getenv('HTTP_X_FORWARDED')) {
- $ip = getenv('HTTP_X_FORWARDED');
- } elseif (getenv('HTTP_FORWARDED_FOR')) {
- $ip = getenv('HTTP_FORWARDED_FOR');
- } elseif (getenv('HTTP_FORWARDED')) {
- $ip = getenv('HTTP_FORWARDED');
- } else {
- $ip = $_SERVER['REMOTE_ADDR'];
- }
- return $ip;
- }
- public function generate_dislike()
- {
- $output = "<a class=\"dislike <?php echo $class = (jnews_like_count()->get_status(get_the_ID()) === '-1' ) ? 'active' : '' ; ?>\" href=\"#\" data-id=\"<?php echo get_the_ID(); ?>\" data-type=\"dislike\" data-message=\"\"><i class=\"fa <?php echo $icon = (jnews_like_count()->get_status(get_the_ID()) === '-1' ) ? 'fa-thumbs-down' : 'fa-thumbs-o-down' ; ?> fa-flip-horizontal\"></i> <?php echo jnews_like_count()->get_count(get_the_ID(), 'dislike'); ?></a>";
- return apply_filters('jnews_like_render_dislike', $output);
- }
- public function generate_html()
- {
- $output = "<div class=\"jeg_meta_like\">
- <a class=\"like href=\"#\" data-id=\"<?php echo get_the_ID(); ?>\" data-type=\"like\" data-message=\"\"><i class=\"fa <?php echo $icon = (jnews_like_count()->get_status(get_the_ID()) === '1' ) ? 'fa-thumbs-up' : 'fa-thumbs-o-up' ; ?>\"></i> <?php echo jnews_like_count()->get_count(get_the_ID(), 'like'); ?></a>
- " . $this->generate_dislike() . "
- </div>";
- }
- }
- JNews_Like_Count::getInstance();
- /**
- * @return JNews_Like_Count
- */
- function jnews_like_count()
- {
- return JNews_Like_Count::getInstance();
- }
- do_action('jnews_render_like', 'jnews_render_like');
- function jnews_render_like()
- {
- JNews_Like_Count::getInstance()->generate_html();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement