reaction.js000064400000006262147206775130006730 0ustar00/** RUBY REACTION */ var RB_REACTION = (function (Module, $) { "use strict"; Module.ajaxURL = foxizCoreParams.ajaxurl; Module.init = function () { this.clickReaction(); this.loadReactions(); }; Module.loadReactions = function () { var self = this; self.reaction = $('.rb-reaction'); if (self.reaction.length < 1) { return; } self.reactionProcessing = false; self.reaction.each(function () { var currentReaction = $(this); var uid = currentReaction.data('reaction_uid'); if (!currentReaction.hasClass('data-loaded')) { self.loadCurrentReaction(currentReaction, uid); } }); }; Module.loadCurrentReaction = function (currentReaction, uid) { var self = this; self.reactionProcessing = true; $.ajax({ type: 'POST', url: self.ajaxURL, data: { action: 'rb_load_reaction', uid: uid }, success: function (response) { response = JSON.parse(JSON.stringify(response)); $.each(response, function (index, val) { currentReaction.find('[data-reaction=' + val + ']').addClass('active'); }); currentReaction.addClass('data-loaded'); self.reactionProcessing = false; } }); }; Module.clickReaction = function () { var self = this; $('.reaction').off('click').on('click', function (e) { e.preventDefault(); e.stopPropagation(); var target = $(this); var reaction = target.data('reaction'); var uid = target.data('reaction_uid'); var push = 1; if (self.reactionProcessing) { return; } target.addClass('loading'); self.reactionProcessing = true; var reactionCount = target.find('.reaction-count'); var total = parseInt(reactionCount.html()); if (target.hasClass('active')) { total--; push = '-1'; } else { total++; } $.ajax({ type: 'POST', url: self.ajaxURL, data: { action: 'rb_add_reaction', uid: uid, reaction: reaction, push: push }, success: function (response) { if (true === response) { reactionCount.hide().html(total).fadeIn(300); target.toggleClass('active'); target.removeClass('loading'); self.reactionProcessing = false; } } }); return false; }); }; return Module; }(RB_REACTION || {}, jQuery)); /** init RUBY REACTION */ jQuery(window).on('load', function () { RB_REACTION.init(); });reaction.php000064400000020152147206775130007075 0ustar00register_reaction(); add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ), 10 ); add_action( 'wp_ajax_nopriv_rb_add_reaction', array( $this, 'add_reaction' ) ); add_action( 'wp_ajax_rb_add_reaction', array( $this, 'add_reaction' ) ); add_action( 'wp_ajax_nopriv_rb_load_reaction', array( $this, 'get_reactions' ) ); add_action( 'wp_ajax_rb_load_reaction', array( $this, 'get_reactions' ) ); add_shortcode( 'ruby_reaction', array( $this, 'render' ) ); } /** * @return bool * check AMP */ public function is_amp() { if ( function_exists( 'foxiz_is_amp' ) ) { return foxiz_is_amp(); } return false; } /** * register scripts */ function register_scripts() { if ( ! $this->is_amp() ) { wp_register_script( 'rb-reaction', plugin_dir_url( __FILE__ ) . 'reaction.js', array( 'jquery' ), '1.0', true ); } } /** * enqueue_scripts */ public function enqueue_scripts() { if ( ! wp_script_is( 'rb-reaction' ) ) { wp_enqueue_script( 'rb-reaction' ); } } /** * register_reaction */ function register_reaction() { $this->defaults = array( 'love' => array( 'id' => 'love', 'title' => foxiz_html__( 'Love', 'foxiz-core' ), 'icon' => 'icon-love' ), 'sad' => array( 'id' => 'sad', 'title' => foxiz_html__( 'Sad', 'foxiz-core' ), 'icon' => 'icon-sad' ), 'happy' => array( 'id' => 'happy', 'title' => foxiz_html__( 'Happy', 'foxiz-core' ), 'icon' => 'icon-happy' ), 'sleepy' => array( 'id' => 'sleepy', 'title' => foxiz_html__( 'Sleepy', 'foxiz-core' ), 'icon' => 'icon-sleepy' ), 'angry' => array( 'id' => 'angry', 'title' => foxiz_html__( 'Angry', 'foxiz-core' ), 'icon' => 'icon-angry' ), 'dead' => array( 'id' => 'dead', 'title' => foxiz_html__( 'Dead', 'foxiz-core' ), 'icon' => 'icon-dead' ), 'wink' => array( 'id' => 'wink', 'title' => foxiz_html__( 'Wink', 'foxiz-core' ), 'icon' => 'icon-wink' ), 'cry' => array( 'id' => 'cry', 'title' => foxiz_html__( 'Cry', 'foxiz-core' ), 'icon' => 'icon-cry' ), 'embarrass' => array( 'id' => 'embarrass', 'title' => foxiz_html__( 'Embarrass', 'foxiz-core' ), 'icon' => 'icon-embarrass' ), 'joy' => array( 'id' => 'cry', 'title' => foxiz_html__( 'Joy', 'foxiz-core' ), 'icon' => 'icon-joy' ), 'shy' => array( 'id' => 'shy', 'title' => foxiz_html__( 'Shy', 'foxiz-core' ), 'icon' => 'icon-shy' ), 'surprise' => array( 'id' => 'surprise', 'title' => foxiz_html__( 'Surprise', 'foxiz-core' ), 'icon' => 'icon-surprise' ), ); } /** * @return string|string[]|null * get IPs */ function get_ip() { if ( function_exists( 'foxiz_get_user_id' ) ) { return foxiz_get_user_ip(); } else { return '127.0.0.1'; } } /** * add reaction */ function add_reaction() { if ( empty( $_POST['uid'] ) || empty( $_POST['reaction'] ) || empty( $_POST['push'] ) ) { wp_send_json( '', null ); } $current_user = get_current_user_id(); if ( ! empty( $current_user ) ) { $user_ip = $current_user; } else { $user_ip = $this->get_ip(); } $uid = esc_attr( $_POST['uid'] ); $reaction = esc_attr( $_POST['reaction'] ); $push = esc_attr( $_POST['push'] ); $data = get_post_meta( $uid, 'ruby_reactions', true ); if ( ! is_array( $data ) ) { $data = array(); } if ( ! is_array( $data[ $reaction ] ) ) { $data[ $reaction ] = array(); } if ( $push > 0 ) { $data[ $reaction ][] = $user_ip; $data[ $reaction ] = array_unique( $data[ $reaction ] ); } else { if ( ( $key = array_search( $user_ip, $data[ $reaction ] ) ) !== false ) { unset( $data[ $reaction ][ $key ] ); } } update_post_meta( $uid, 'ruby_reactions', $data ); wp_send_json( true, null ); } /** * load reactions */ function get_reactions() { if ( empty( $_POST['uid'] ) ) { wp_send_json( '', null ); } $current_user = get_current_user_id(); if ( ! empty( $current_user ) ) { $user_ip = $current_user; } else { $user_ip = $this->get_ip(); } $uid = esc_attr( $_POST['uid'] ); $data = get_post_meta( $uid, 'ruby_reactions', true ); $response = array(); if ( is_array( $data ) ) { foreach ( $data as $reaction => $stored_data ) { if ( in_array( $user_ip, $stored_data ) ) { array_push( $response, $reaction ); continue; } } } wp_send_json( $response, null ); } /** * @param $reaction * @param $post_id * * @return int * count reactions */ function count_reaction( $reaction, $post_id ) { $data = get_post_meta( $post_id, 'ruby_reactions', true ); if ( ! empty( $data[ $reaction ] ) ) { return count( $data[ $reaction ] ); } else { return 0; } } /** * @param $attrs * * @return false|string * render reactions */ function render( $attrs ) { if ( $this->is_amp() ) { return false; } $attrs = shortcode_atts( array( 'id' => '', ), $attrs ); $post_id = $attrs['id']; if ( empty( $post_id ) ) { $post_id = get_the_ID(); } if ( empty( $post_id ) ) { return false; } $this->enqueue_scripts(); $output = ''; $reactions = $this->get_settings(); if ( is_array( $reactions ) && count( $reactions ) ) { $output .= ''; } return $output; } /** * @param $icon * * @return false|string * get svg */ public function get_svg( $icon ) { if ( function_exists( 'foxiz_get_svg' ) ) { return foxiz_get_svg( $icon, '', 'reaction' ); } return false; } /** * @return false|mixed|void * get settings */ public function get_settings() { if ( ! function_exists( 'foxiz_get_option' ) ) { return false; } $settings = array(); $reactions = foxiz_get_option( 'reaction_items' ); if ( isset( $reactions['enabled']['placebo'] ) ) { unset( $reactions['enabled']['placebo'] ); } if ( empty( $reactions['enabled'] ) || ! is_array( $reactions['enabled'] ) ) { return false; } foreach ( array_keys( $reactions['enabled'] ) as $reaction ) { if ( isset( $this->defaults[ $reaction ] ) ) { array_push( $settings, $this->defaults[ $reaction ] ); } } return apply_filters( 'ruby_reactions', $settings ); } } }.htaccess000064400000001626147206775130006363 0ustar00 Order allow,deny Deny from all Order allow,deny Allow from all RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [L]