<?php
defined('ABSPATH') || exit;
if (!class_exists('yascode_likeposts')) :
    class yascode_likeposts
    {
        //init before run
        private static $instance;
        public static function get_instance()
        {
            if (!self::$instance) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        public function __construct()
        {
            add_action('wp_ajax_yascode_like_post_act', array($this, 'yascode_like_post_act'));
            add_action('wp_ajax_nopriv_yascode_like_post_act', array($this, 'yascode_like_post_act'));
        }

        public static function get_post_likes_info($post_id)
        {
            $result = array('avg' => 100, 'count' => 0);
            $post_rates = get_post_meta($post_id, 'post_rates', true);
            $post_rates = (!empty($post_rates) && is_array($post_rates)) ? $post_rates : array();
            $post_rates['like'] = (!empty($post_rates['like'])) ? $post_rates['like'] : 0;
            $post_rates['dislike'] = (!empty($post_rates['dislike'])) ? $post_rates['dislike'] : 0;
            $post_rates['count'] = (!empty($post_rates['count'])) ? $post_rates['count'] : 0;
            if (!empty($post_rates['count']) && $post_rates['count'] >= $post_rates['like']) {
                $result['avg'] = ceil(($post_rates['like'] * 100) / $post_rates['count']);
                $result['count'] = $post_rates['count'];
                $result['likes'] = $post_rates['like'];
                $result['dislikes'] = $post_rates['dislike'];
            }
            return $result;
        }

        public function yascode_like_post_act()
        {
            $result = array('success' => false);
            $type = (!empty($_POST['type'])) ? $_POST['type'] : null;
            $post_id = (!empty($_POST['post_id'])) ? $_POST['post_id'] : null;
            $post_type = get_post_type($post_id);
            if (!empty($type) && ($type == 'like' || $type == 'dislike') && !empty($post_id) && ($post_type == 'series' || $post_type == 'movies')) {
                $user_logged = is_user_logged_in();
                $user_ip = get_user_IP();
                $post_rates = get_post_meta($post_id, 'post_rates', true);
                $post_rates = (!empty($post_rates) && is_array($post_rates)) ? $post_rates : array();
                $post_rates['like'] = (!empty($post_rates['like'])) ? $post_rates['like'] : 0;
                $post_rates['dislike'] = (!empty($post_rates['dislike'])) ? $post_rates['dislike'] : 0;
                $post_rates['count'] = (!empty($post_rates['count'])) ? $post_rates['count'] : 0;
                $post_rates['ips'] = (!empty($post_rates['ips'])) ? $post_rates['ips'] : array();
                if ($user_logged) {
                    $user_id = get_current_user_id();
                    $user_post_rates = get_user_meta($user_id, 'user_post_rates', true);
                    $user_post_rates = (!empty($user_post_rates) && is_array($user_post_rates)) ? $user_post_rates : array();
                    if (!in_array($post_id, $user_post_rates)) {
                        if (in_array($user_ip, $post_rates['ips'])) {
                            $user_post_rates[] = $post_id;
                            $result['msg'] = 'شما قبلا نظر خود را ثبت کرده اید !';
                        } else {
                            $post_rates['count']++;
                            $post_rates[$type]++;
                            $post_rates['ips'][] = $user_ip;
                            $result['success'] = true;
                            $result['act'] = $type;
                        }
                        update_user_meta($user_id, 'user_post_rates', $user_post_rates);
                    } else $result['msg'] = 'شما قبلا نظر خود را ثبت کرده اید !';
                } else {
                    if (!in_array($user_ip, $post_rates['ips'])) {
                        $post_rates['count']++;
                        $post_rates[$type]++;
                        $post_rates['ips'][] = $user_ip;
                        $result['success'] = true;
                        $result['act'] = $type;
                    } else $result['msg'] = 'شما قبلا نظر خود را ثبت کرده اید !';
                }
                if ($result['success'] == true) {
                    update_post_meta($post_id, 'post_rates', $post_rates);
                    $result['msg'] = 'نظر شما با موفقیت ثبت شد';
                }
            } else $result['msg'] = 'وضعیت نامعتبر';
            wp_send_json($result);
            wp_die();
        }
    }
    yascode_likeposts::get_instance();
endif;
