Completed
Push β€” master ( 878290...c42111 )
by Gary
02:11
created

react.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
Plugin Name: React
4
Description: πŸ’© Reactions.
5
Version: 0.1
6
*/
7
8
class React {
9
	/**
10
	 * React constructor.
11
	 */
12
	function __construct() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
		$this->enqueue();
14
15
		add_action( 'wp_head',     array( $this, 'print_settings' ) );
16
		add_action( 'wp_footer',   array( $this, 'print_selector' ) );
17
18
 		add_filter( 'the_content', array( $this, 'the_content'    ) );
19
	}
20
21
	/**
22
	 * Initialises the reactions.
23
	 *
24
	 * @return React Static instance of the React class.
25
	 */
26
	static function init() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
		static $instance;
28
29
		if ( ! $instance ) {
30
			$instance = new React;
31
		}
32
33
		return $instance;
34
	}
35
36
	/**
37
	 * Print the JavaScript settings.
38
	 */
39
	function print_settings() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
40
		?>
41
			<script type="text/javascript">
42
				window.wp = window.wp || {};
43
				window.wp.react = window.wp.react || {};
44
				window.wp.react.settings = {
45
					emoji_url: '<?php echo plugins_url( 'emoji.json', __FILE__ ) ?>'
46
				}
47
			</script>
48
		<?php
49
	}
50
51
	/**
52
	 * Enqueue relevant JS and CSS
53
	 */
54
	function enqueue() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
55
		wp_enqueue_style( 'react-emoji', plugins_url( 'emoji.css', __FILE__ ) );
56
57
		wp_enqueue_script( 'react-emoji', plugins_url( 'emoji.js', __FILE__ ), array(), false, true );
58
	}
59
60
	/**
61
	 * Add the reaction buttons to the post content.
62
	 * @param  string $content The content HTML
63
	 * @return string The content HTML, with the react buttons attached
64
	 */
65
	function the_content( $content ) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
66
		$post_id = get_the_ID();
67
		if ( ! $post_id ) {
68
			return $content;
69
		}
70
71
		$reactions = get_comments( array(
72
			'post_id' => $post_id,
73
			'type'    => 'reaction',
74
		) );
75
76
		$reactions_summary = array();
77
		foreach( $reactions as $reaction ) {
78
			if ( ! isset( $reactions_summary[ $reaction->comment_content ] ) ) {
79
				$reactions_summary[ $reaction->comment_content ] = 0;
80
			}
81
82
			$reactions_summary[ $reaction->comment_content ]++;
83
		}
84
85
		$content .= '<div class="emoji-reactions">';
86
87
		foreach ( $reactions_summary as $emoji => $count ) {
88
			$content .= "<div data-emoji='$emoji' data-count='$count' data-post='$post_id' class='emoji-reaction'><div class='emoji'>$emoji</div><div class='count'>$count</div>";
89
		}
90
91
		/* translators: This is the emoji used for the "Add new emoji reaction" button */
92
		$content .= '<div data-post="$post_id" class="emoji-reaction-add"><div class="emoji">' . __( 'πŸ˜ƒ+', 'reactions' ) . '</div></div>';
93
		$content .= '</div>';
94
		return $content;
95
	}
96
97
	function print_selector() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
98
		?>
99
			<div id="emoji-reaction-selector" style="display: none;">
100
				<div class="tabs">
101
					<span data-tab="0" alt="<?php echo __( 'People',   'reactions' ); ?>"><?php echo __( 'πŸ˜€', 'reactions' ); ?></span>
102
					<span data-tab="1" alt="<?php echo __( 'Nature',   'reactions' ); ?>"><?php echo __( '🌿', 'reactions' ); ?></span>
103
					<span data-tab="2" alt="<?php echo __( 'Food',     'reactions' ); ?>"><?php echo __( 'πŸ”', 'reactions' ); ?></span>
104
					<span data-tab="3" alt="<?php echo __( 'Activity', 'reactions' ); ?>"><?php echo __( '⚽️', 'reactions' ); ?></span>
105
					<span data-tab="4" alt="<?php echo __( 'Places',   'reactions' ); ?>"><?php echo __( '✈️', 'reactions' ); ?></span>
106
					<span data-tab="5" alt="<?php echo __( 'Objects',  'reactions' ); ?>"><?php echo __( 'πŸ’‘', 'reactions' ); ?></span>
107
					<span data-tab="6" alt="<?php echo __( 'Symbols',  'reactions' ); ?>"><?php echo __( '❀', 'reactions' ); ?></span>
108
					<span data-tab="7" alt="<?php echo __( 'Flags',    'reactions' ); ?>"><?php echo __( 'πŸ‡ΊπŸ‡Έ', 'reactions' ); ?></span>
109
				</div>
110
				<div class="container container-0"></div>
111
				<div class="container container-1"></div>
112
				<div class="container container-2"></div>
113
				<div class="container container-3"></div>
114
				<div class="container container-4"></div>
115
				<div class="container container-5"></div>
116
				<div class="container container-6"></div>
117
				<div class="container container-7"></div>
118
			</div>
119
		<?php
120
	}
121
}
122
123
add_action( 'init', array( 'React', 'init' ) );
124