-
Notifications
You must be signed in to change notification settings - Fork 6
/
mentionable.php
268 lines (231 loc) · 6.7 KB
/
mentionable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/**
* Plugin Name: Mentionable
* Plugin URI: http://x-team.com
* Description: Mention WordPress content with inline autocomplete inside tinyMCE.
* Version: 0.2.1
* Author: X-Team, Jonathan Bardo, Topher
* Author URI: http://x-team.com/wordpress/
* License: GPLv2+
* Text Domain: mentionable
* Domain Path: /languages
*/
/**
* Copyright (c) 2013 X-Team (http://x-team.com/)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 or, at
* your discretion, any later version, as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Mentionable {
/**
* Class name in lowercase
*
* @var string
* @access private
*/
private static $class_name;
/**
* Autocomplete component class
*
* @var Mentionable_Autocomplete
* @access public
*/
public $autocomplete;
/**
* Content component class
*
* @var Mentionable_Content
* @access public
*/
public $content;
/**
* Post metas component class
*
* @var Mentionable_Postmetas
* @access public
*/
public $postmetas;
/**
* Settings instance
*
* @var Mentionable_Settings
* @access public
*/
public $settings;
/**
* Current admin post_type
*
* @var string
* @access public
*/
public static $current_post_type;
/**
* Constructor | Add required hooks
*
* @access public
*
* @return \Mentionable
*/
public function __construct() {
// Set class name in lowercase
self::$class_name = strtolower( __CLASS__ );
// Get current post type in admin
self::$current_post_type = $this->get_current_admin_post_type();
// Set constans needed by the plugin.
add_action( 'plugins_loaded', array( $this, 'define_constants' ), 1 );
// Internationalize the text strings used.
add_action( 'plugins_loaded', array( $this, 'i18n' ), 2 );
// Setup all dependent class
add_action( 'after_setup_theme', array( $this, 'setup' ), 3 );
}
/**
* Define constants used by the plugin.
*
* @access public
* @action plugins_loaded
* @return void
*/
public function define_constants() {
// Set constant path to the plugin directory.
define( 'MENTIONABLE_DIR', trailingslashit( plugin_dir_path( __FILE__ ) ) );
// Set constant path to the includes directory.
define( 'MENTIONABLE_INCLUDES_DIR', MENTIONABLE_DIR . trailingslashit( 'includes' ) );
// Plugin url
$plugin_dirname = basename( dirname( __FILE__ ) );
define( 'MENTIONABLE_URL', trailingslashit( plugin_dir_url( '' ) ) . $plugin_dirname );
}
/**
* Loads the translation files.
*
* @access public
* @action plugins_loaded
* @return void
*/
public function i18n() {
// Load the translation of the plugin
load_plugin_textdomain( 'mentionable', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Setup all classes needed for the plugin
*
* @access public
* @action plugins_loaded
* @return void
*/
public function setup() {
// Register settings
require_once( MENTIONABLE_INCLUDES_DIR . '/' . self::$class_name . '-settings.php' );
$this->settings = new Mentionable_Settings;
// Register tmce plugin -- because pluggable.php is loaded after plugin
add_action( 'admin_init', array( $this, 'admin_init' ) );
if ( in_array( self::$current_post_type, Mentionable_Settings::$options['post_types'] ) ) {
// Filter tinymce css to add custom
add_filter( 'mce_css', array( $this, 'filter_mce_css' ) );
// Enqueue admin script
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
}
// Intanciate postmetas class
require_once( MENTIONABLE_INCLUDES_DIR . '/' . self::$class_name . '-postmetas.php' );
$this->postmetas = new Mentionable_Postmetas;
// Instanciate content class
require_once( MENTIONABLE_INCLUDES_DIR . '/' . self::$class_name . '-content.php' );
$this->content = new Mentionable_Content;
}
/**
* Add the required action and filter after init hook
*
* @action admin_init
* @access public
*
* @return void
*/
public function admin_init(){
if (
'true' === get_user_option( 'rich_editing' )
&&
in_array( self::$current_post_type, Mentionable_Settings::$options['post_types'] )
) {
add_filter( 'mce_external_plugins', array( $this, 'register_tmce_plugin' ) );
}
// Add ajax handler for autocomplete action
require_once( MENTIONABLE_INCLUDES_DIR . '/' . self::$class_name . '-autocomplete.php' );
$this->autocomplete = new Mentionable_Autocomplete();
}
/**
* Register the tinymce plugin
*
* @param array $plugin_array
* @filter mce_external_plugins
* @access public
*
* @return array
*/
public function register_tmce_plugin( $plugin_array ) {
$plugin_array['mentionable'] = MENTIONABLE_URL . '/js/' . self::$class_name . '-tmce.js';
return $plugin_array;
}
/**
* Enqueue required scripts and style in admin section
*
* @return void
*/
public function admin_enqueue_scripts() {
wp_enqueue_style( 'mentionable_css', MENTIONABLE_URL . '/css/' . self::$class_name . '-style.css', '0.1.0' );
wp_localize_script(
'jquery',
self::$class_name,
array(
'nonce' => wp_create_nonce( self::$class_name . '_nonce' ),
'action' => 'get_mentionable',
)
);
}
/**
* Add custom css in tinyMCE iframe
*
* @param string $mce_css The concatenated css
*
* @return string
*/
public function filter_mce_css( $mce_css ) {
if ( ! empty( $mce_css ) ){
$mce_css .= ',';
}
if ( apply_filters( 'add_mentionnable_tmce_style', '__return_true' ) ) {
$mce_css .= MENTIONABLE_URL . '/css/' . self::$class_name . '-tmce-style.css';
}
return $mce_css;
}
/**
* Helper function to get current admin post_type
*
* @since 0.1.0
* @access private
* @attribution mjangda <https://gist.github.com/mjangda/476964>
*
* @return string
*/
private function get_current_admin_post_type() {
global $pagenow;
$pages = array( 'edit.php', 'post.php', 'post-new.php' );
if ( in_array( $pagenow, $pages ) && ! isset( $_REQUEST['post_type'] ) ) {
$current_post_type = 'post';
} else if ( isset( $_REQUEST['post_type'] ) ) {
$current_post_type = sanitize_key( $_REQUEST['post_type'] );
}
return isset( $current_post_type ) ? $current_post_type : null;
}
}
// Register global plugin controller
$GLOBALS['mentionable'] = new Mentionable();