Advertisement
jegtheme

class-jnews-like.php

Jul 25th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.99 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author Jegtheme
  4.  */
  5.  
  6. if ( ! defined( 'ABSPATH' ) ) {
  7.     exit;
  8. }
  9.  
  10. if ( ! class_exists( 'JNews_Like_Count' ) ) {
  11.    
  12.     /**
  13.      * Class JNews Like Count
  14.      */
  15.     Class JNews_Like_Count
  16.     {
  17.         /**
  18.          * @var JNews_Like_Count
  19.          */
  20.         private static $instance;
  21.  
  22.         /**
  23.          * @var string
  24.          *
  25.          */
  26.         private $cookie = "jnews_like_count_setting";
  27.         private $table_name = "jnews_post_like";
  28.  
  29.         /**
  30.          * @return JNews_Like_Count
  31.          */
  32.         public static function getInstance()
  33.         {
  34.             if (null === static::$instance)
  35.             {
  36.                 static::$instance = new static();
  37.             }
  38.             return static::$instance;
  39.         }
  40.  
  41.         /**
  42.          * JNews_Like_Count constructor.
  43.          *
  44.          */
  45.         public function __construct()
  46.         {
  47.             $this->init_like();
  48.             $this->setup_hook();
  49.         }
  50.  
  51.         /**
  52.          * Create table for post like count
  53.          *
  54.          */
  55.         private function init_like()
  56.         {
  57.             global $wpdb;
  58.             $table_name = $wpdb->prefix . $this->table_name;
  59.  
  60.             if( $wpdb->get_var("show tables like '$table_name'") != $table_name ) {
  61.                 $sql = "CREATE TABLE " . $table_name . " (
  62.                        `id` bigint(11) NOT NULL AUTO_INCREMENT,
  63.                        `post_id` int(11) NOT NULL,
  64.                        `date_time` datetime NOT NULL,
  65.                        `value` int(2) NOT NULL,
  66.                        `ip` varchar(40) NOT NULL,
  67.                        `user_id` int(11) NOT NULL DEFAULT '0',
  68.                        `cookie` varchar(25) NOT NULL,
  69.                        PRIMARY KEY (`id`)
  70.                    )";
  71.  
  72.                 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  73.                 dbDelta($sql);
  74.             }
  75.         }
  76.  
  77.         /**
  78.          * Setup hook function
  79.          *
  80.          */
  81.         public function setup_hook()
  82.         {
  83.             add_action( 'init', array($this, 'set_cookie') );
  84.  
  85.             add_action( 'wp_ajax_jnews_like',        array($this, 'ajax_do_action_type') );
  86.             add_action( 'wp_ajax_nopriv_jnews_like', array($this, 'ajax_do_action_type') );
  87.         }
  88.  
  89.         /**
  90.          * Set cookie
  91.          *
  92.          */
  93.         public function set_cookie()
  94.         {
  95.             if ( !isset($_COOKIE[$this->cookie]) ) {
  96.                 $cookietimeout = get_option("cookie_timeout", time() + 24 * 60 * 60 * 30);
  97.                 setcookie($this->cookie, uniqid(), $cookietimeout, '/');
  98.             }
  99.         }
  100.  
  101.         /**
  102.          * Get total like and dislike
  103.          * @param  integer $post_id
  104.          * @param  string  $type    
  105.          * @return string  $result  
  106.          */
  107.         public function get_count($post_id, $type = null)
  108.         {
  109.             global $wpdb;
  110.             $table_name = $wpdb->prefix . $this->table_name;
  111.  
  112.             if ( $type === "like" ) {
  113.                 $result = $wpdb->get_var(
  114.                             $wpdb->prepare(
  115.                                 "SELECT COUNT(id) FROM $table_name WHERE post_id = %d AND value >= 0",
  116.                                 $post_id
  117.                             )
  118.                         );
  119.  
  120.                 return $result;
  121.             }
  122.  
  123.             if ( $type === "dislike" ) {
  124.                 $result = $wpdb->get_var(
  125.                             $wpdb->prepare(
  126.                                 "SELECT COUNT(id) FROM $table_name WHERE post_id = %d AND value <= 0",
  127.                                 $post_id
  128.                             )
  129.                         );  
  130.  
  131.                 return $result;
  132.             }
  133.            
  134.             return false;
  135.         }
  136.  
  137.         /**
  138.          * Get status if have liked or disliked
  139.          * @param  integer $post_id
  140.          * @return string  $status
  141.          */
  142.         public function get_status($post_id)
  143.         {
  144.             global $wpdb;
  145.             $table_name = $wpdb->prefix . $this->table_name;
  146.             $ip = $this->get_ip();
  147.             $cookie = ( isset($_COOKIE[$this->cookie]) ) ? $_COOKIE[$this->cookie] : '' ;
  148.  
  149.             $status = $wpdb->get_var(
  150.                         $wpdb->prepare(
  151.                             "SELECT value FROM $table_name WHERE post_id = %d AND ip = %s AND cookie = '%s'",
  152.                             $post_id, $ip, $cookie
  153.                         )
  154.                     );
  155.  
  156.             return $status;
  157.         }
  158.  
  159.         /**
  160.          * Main function
  161.          * @param  integer $post_id
  162.          * @param  string  $type
  163.          * @return json
  164.          */
  165.         public function do_action_type($post_id, $type)
  166.         {
  167.             $status = $this->get_status($post_id);
  168.             $result = '';
  169.             global $wpdb;
  170.  
  171.             // if like update into dislike and then
  172.             if ( $status === "1" || $status === "-1" ) {
  173.                 $result = $this->update_value($post_id, $type);
  174.                 if ( $result ) {
  175.                     echo json_encode(array(
  176.                         'response' => 2,
  177.                         'message'  => esc_html__( 'Thanks for your vote!', 'jnews-plugin'),
  178.                         'like'     => $this->get_count($post_id, 'like'),
  179.                         'dislike'  => $this->get_count($post_id, 'dislike')
  180.                     ));
  181.                 } else {
  182.                     echo json_encode(array(
  183.                         'response' => 0,
  184.                         'message'  => esc_html__( 'Internal DB Error!', 'jnews-plugin'),
  185.                     ));
  186.                 }
  187.             }
  188.  
  189.             if ( $status === NULL ) {
  190.                 $result = $this->insert_value($post_id, $type);
  191.                 if ( $result ) {
  192.                     echo json_encode(array(
  193.                         'response' => 1,
  194.                         'message'  => esc_html__( 'Thanks for your vote!', 'jnews-plugin'),
  195.                         'like'     => $this->get_count($post_id, 'like'),
  196.                         'dislike'  => $this->get_count($post_id, 'dislike'),
  197.                         'type'     => $type
  198.                     ));
  199.                 } else {
  200.                     echo json_encode(array(
  201.                         'response' => 0,
  202.                         'message'  => esc_html__( 'Internal DB Error!', 'jnews-plugin'),
  203.                     ));
  204.                 }
  205.             }
  206.         }
  207.  
  208.         /**
  209.          * Main function for ajax
  210.          *
  211.          */
  212.         public function ajax_do_action_type()
  213.         {
  214.             $this->do_action_type($_POST['post_id'], $_POST['type']);
  215.             die();
  216.         }
  217.  
  218.         /**
  219.          * Insert data into database
  220.          * @param  integer $post_id
  221.          * @param  string  $type    
  222.          * @return bool          
  223.          */
  224.         public function insert_value($post_id, $type)
  225.         {
  226.             global $wpdb;
  227.             $table_name = $wpdb->prefix . $this->table_name;
  228.             $ip = $this->get_ip();
  229.             $cookie = ( isset($_COOKIE[$this->cookie]) ) ? $_COOKIE[$this->cookie] : '' ;
  230.             $value = ($type === 'dislike') ? -1 : 1 ;
  231.  
  232.             $query = $wpdb->prepare(
  233.                         "INSERT INTO $table_name SET
  234.                        post_id = %d,
  235.                        date_time = '" . date( 'Y-m-d H:i:s' ) . "',
  236.                        user_id = %d,
  237.                        cookie = %s,
  238.                        ip = %s,
  239.                        value = %d",
  240.                         $post_id, get_current_user_id(), $cookie, $ip, $value
  241.                     );
  242.  
  243.             $success = $wpdb->query($query);
  244.             return $success;
  245.         }
  246.  
  247.         /**
  248.          * Update data value into database
  249.          * @param  integer $post_id
  250.          * @param  string  $type    
  251.          * @return bool          
  252.          */
  253.         public function update_value($post_id, $type)
  254.         {
  255.             global $wpdb;
  256.             $table_name = $wpdb->prefix . $this->table_name;
  257.             $ip = $this->get_ip();
  258.             $cookie = ( isset($_COOKIE[$this->cookie]) ) ? $_COOKIE[$this->cookie] : '' ;
  259.             $value = ($type === 'dislike') ? -1 : 1 ;
  260.  
  261.             $query = $wpdb->prepare(
  262.                         "UPDATE $table_name SET
  263.                        value = %d,
  264.                        date_time = '" . date( 'Y-m-d H:i:s' ) . "',
  265.                        user_id = %d
  266.                        WHERE post_id = %d AND cookie = %s AND ip = %s",
  267.                         $value, get_current_user_id(), $post_id, $cookie, $ip
  268.                     );
  269.  
  270.             $success = $wpdb->query($query);
  271.             return $success;
  272.         }
  273.  
  274.         /**
  275.          * Get actual ip address
  276.          * @return string
  277.          */
  278.         public function get_ip()
  279.         {
  280.             if (getenv('HTTP_CLIENT_IP')) {
  281.                 $ip = getenv('HTTP_CLIENT_IP');
  282.             } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
  283.                 $ip = getenv('HTTP_X_FORWARDED_FOR');
  284.             } elseif (getenv('HTTP_X_FORWARDED')) {
  285.                 $ip = getenv('HTTP_X_FORWARDED');
  286.             } elseif (getenv('HTTP_FORWARDED_FOR')) {
  287.                 $ip = getenv('HTTP_FORWARDED_FOR');
  288.             } elseif (getenv('HTTP_FORWARDED')) {
  289.                 $ip = getenv('HTTP_FORWARDED');
  290.             } else {
  291.                 $ip = $_SERVER['REMOTE_ADDR'];
  292.             }
  293.            
  294.             return $ip;
  295.         }
  296.  
  297.         public function generate_dislike()
  298.         {
  299.             $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>";
  300.            
  301.             return apply_filters('jnews_like_render_dislike', $output);
  302.         }
  303.        
  304.         public function generate_html()
  305.         {
  306.             $output = "<div class=\"jeg_meta_like\">
  307.                <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>
  308.                " . $this->generate_dislike() . "
  309.            </div>";
  310.         }
  311.  
  312.     }
  313.  
  314.     JNews_Like_Count::getInstance();
  315.  
  316.     /**
  317.      * @return JNews_Like_Count
  318.      */
  319.     function jnews_like_count()
  320.     {
  321.         return JNews_Like_Count::getInstance();
  322.     }
  323.  
  324.  
  325.     do_action('jnews_render_like', 'jnews_render_like');
  326.  
  327.     function jnews_render_like()
  328.     {
  329.         JNews_Like_Count::getInstance()->generate_html();
  330.     }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement