<?php
/**
 * Alpha Post Like Addon
 *
 * @since 4.0.0
 */

if ( ! class_exists( 'Alpha_Post_Like' ) ) {

	class Alpha_Post_Like {


		/**
		 * Constructor
		 *
		 * @since 4.0.0
		 */
		public function __construct() {

			add_filter( 'alpha_customize_fields', array( $this, 'extend_customizer_fields' ) );
			add_filter( 'alpha_theme_option_default_values', array( $this, 'extend_theme_options_default_values' ) );
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 35 );
			add_action( 'publish_post', array( $this, 'setup_likes' ) );
			add_action( 'wp_ajax_alpha_like_post', array( $this, 'like_post' ) );
			add_action( 'wp_ajax_nopriv_alpha_like_post', array( $this, 'like_post' ) );
		}


		/**
		 * Extends customizer fields
		 *
		 * @since 4.0.0
		 */
		public function extend_customizer_fields( $fields ) {

			$fields['post_show_info']['choices'] = array(
				'image'         => esc_html__( 'Media', 'alpha-core' ),
				'author'        => esc_html__( 'Meta Author', 'alpha-core' ),
				'date'          => esc_html__( 'Meta Date', 'alpha-core' ),
				'comment'       => esc_html__( 'Meta Comments Count', 'alpha-core' ),
				'like'          => esc_html__( 'Meta Like', 'alpha-core' ),
				'category'      => esc_html__( 'Category', 'alpha-core' ),
				'tag'           => esc_html__( 'Tags', 'alpha-core' ),
				'author_info'   => esc_html__( 'Author Information', 'alpha-core' ),
				'share'         => esc_html__( 'Share', 'alpha-core' ),
				'navigation'    => esc_html__( 'Prev and Next', 'alpha-core' ),
				'related'       => esc_html__( 'Related Posts', 'alpha-core' ),
				'comments_list' => esc_html__( 'Comments', 'alpha-core' ),
			);

			return $fields;
		}


		/**
		 * Extend default theme options
		 *
		 * @since 4.0.0
		 */
		public function extend_theme_options_default_values( $options ) {

			$options['post_show_info'] = array(
				'image',
				'author',
				'date',
				'category',
				'comment',
				'like',
				'tag',
				'author_info',
				'share',
				'navigation',
				'comments_list',
			);

			return $options;
		}


		/**
		 * Enqueue Script
		 *
		 * @since 4.0.0
		 */
		public function enqueue_scripts() {
			wp_enqueue_script( 'alpha-post-like', ALPHA_CORE_INC_URI . '/addons/post-like/post-like' . ALPHA_JS_SUFFIX, array( 'alpha-framework-async' ), ALPHA_CORE_VERSION, true );
		}


		/**
		 * Set up liks in post meta
		 *
		 * @since 4.0.0
		 */
		public function setup_likes( $post_id ) {
			if ( ! is_numeric( $post_id ) ) {
				return;
			}

			add_post_meta( $post_id, 'udesign_post_likes', 0, true );
		}


		/**
		 * Get cookie data for like
		 *
		 * @since 4.0.0
		 * @param $id : Post ID
		 */
		public function get_cookies( $id ) {
			return isset( $_COOKIE[ 'udesign_post_likes_' . $id ] ) ? json_decode( wp_unslash( $_COOKIE[ 'udesign_post_likes_' . $id ] ), true ) : array();
		}


		/**
		 * Like or dislike post
		 *
		 * @since 4.0.0
		 */
		public function like_post() {

			$post_id = $_POST['post_id'];

			if ( ! isset( $post_id ) || ! is_numeric( $post_id ) ) {
				return;
			}

			$likes   = get_post_meta( $post_id, 'udesign_post_likes', true );
			$cookies = $this->get_cookies( $post_id );

			if ( $cookies['action'] && 'dislike' == $cookies['action'] ) {
				$likes  = intval( $likes ) - 1;
				$action = 'like';
				if ( $likes < 0 ) {
					$likes = 0;
				}
			} else { // like
				$likes  = intval( $likes ) + 1;
				$action = 'dislike';
			}
			update_post_meta( $post_id, 'udesign_post_likes', $likes );

			$data = array(
				'likes'  => $likes,
				'action' => $action,
			);
			setcookie( 'udesign_post_likes_' . $post_id, json_encode( $data ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
			echo json_encode( $data );
			die;
		}
	}
}


new Alpha_Post_Like();
