-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikify.php
376 lines (277 loc) · 10.4 KB
/
wikify.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/*
Plugin Name: Wikify
Plugin URI: https://github.com/johappel/wikify/blob/master/README.md
Plugin SRC: https://github.com/johappel/wikify/archive/master.zip
Description: An easy-to-use plugin that adds the functionality of a wiki known from MediaWiki. It handles [[word]] and [[word|link label]] .
Version: 0.4
Author: Joachim Happel
Author URI: https://github.com/johappel/
Features:
* generate an auto-link to word in double brackets to a Post, with this link
* handles [[word]] and [[word|label]]
* can alternative link to external wikis
**ATTENTION**
Does not work with standard permalink setting: ?p=ID
*/
class wp_wikify {
public $forbiddenSlugs = array("login");
public $urlPrefix;
public $is_external = false;
public $external_url = '';
function __construct() {
ini_set("display_errors", 'on');
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
ini_set('error_reporting', E_ERROR);
//error_reporting(-1);
$this->urlPrefix = get_home_url() . '/';
add_filter('the_content', array($this, 'wikifyContent'));
add_action('init',array($this,'init') );
add_action('init',array($this,'wikify_button_init') );
add_action('wp_print_styles',array($this,'wikify_add_style') );
add_action( 'admin_menu', array($this,'wf_add_admin_menu') );
add_action( 'admin_init', array($this,'wf_option_forms') );
}
function init(){
$options = get_option('wf_settings');
$this->is_external = isset( $options['wf_checkbox_external_usage'] ) ;
$this->external_url = isset($options['wf_text_external_url']) ? $options['wf_text_external_url'] : '';
$this->title_prefix = isset($options['wf_text_title_prefix']) ? $options['wf_text_title_prefix'].' ' : '';
}
function wf_add_admin_menu( ) {
add_options_page( 'wikify', 'Wikify', 'manage_options', 'wikify', array($this, 'wf_options_page') );
}
function wf_option_forms( ) {
function wf_checkbox_external_usage_draw( ) {
$options = get_option( 'wf_settings' );
?>
<input type='checkbox' name='wf_settings[wf_checkbox_external_usage]' <?php checked( $options['wf_checkbox_external_usage'], 1 ); ?> value='1'>
<?php
}
function wf_text_title_prefix_draw( ) {
$options = get_option( 'wf_settings' );
if(!isset($options['wf_text_title_prefix'])){
$options['wf_text_title_prefix'] = 'wiki:';
}
?>
<input style="width:300px" type='text' name='wf_settings[wf_text_title_prefix]' value='<?php echo $options['wf_text_title_prefix']; ?>'>
<?php
}
function wf_text_undefinded_style_draw( ) {
$options = get_option( 'wf_settings' );
if(!isset($options['wf_text_undefinded_style']) || empty($options['wf_text_undefinded_style']) ){
$options['wf_text_undefinded_style'] = 'border-bottom:1px dashed black';
}
?>
<input style="width:300px" type='text' name='wf_settings[wf_text_undefinded_style]' value='<?php echo $options['wf_text_undefinded_style']; ?>'>
<?php
}
function wf_text_external_url_draw( ) {
$options = get_option( 'wf_settings' );
if( ( !isset( $options['wf_text_external_url'] ) || empty( $options['wf_text_external_url'] ) ) && isset($options['wf_checkbox_external_usage'])){
$options['wf_text_external_url'] = 'http://wikipedia.org/wiki/';
update_option('wf_settings',$options);
}
?>
<input style="width:300px" type='text' name='wf_settings[wf_text_external_url]' value='<?php echo $options['wf_text_external_url']; ?>'>
<?php
}
function wf_settings_section_callback( ) {
_e( 'You can set a prefix to the title of each wikiword like <i>wiki:</i> or <i>relilex:</i> . The title will be display on mouse over the wiki word link', 'wikify' );
echo '<br>';
_e( 'Define a link style for the undefined WikiWords','wikify');
}
function wf_external_section_callback( ) {
echo __( 'If you only want to point the WikiWords to another page, where the terms are defined, activate the following option.', 'wikify' );
}
register_setting( 'pluginPage', 'wf_settings' );
add_settings_section(
'wf_pluginPage_section',
__('Settings'),
'wf_settings_section_callback',
'pluginPage'
);
add_settings_field(
'wf_text_title_prefix',
__( 'Link title prefix:', 'wikify' ),
'wf_text_title_prefix_draw',
'pluginPage',
'wf_pluginPage_section'
);
add_settings_field(
'wf_text_undefinded_style',
__( 'Link style:', 'wikify' ),
'wf_text_undefinded_style_draw',
'pluginPage',
'wf_pluginPage_section'
);
register_setting( 'externalPage', 'wf_settings' );
add_settings_section(
'wf_pluginPage_section',
__( 'Link to extern wiki', 'wikify' ),
'wf_external_section_callback',
'externalPage'
);
add_settings_field(
'wf_checkbox_external_usage',
__( 'Use extern wiki', 'wikify' ),
'wf_checkbox_external_usage_draw',
'externalPage',
'wf_pluginPage_section'
);
add_settings_field(
'wf_text_external_url',
__( 'External url:', 'wikify' ),
'wf_text_external_url_draw',
'externalPage',
'wf_pluginPage_section'
);
}
function wf_options_page( ) {
?>
<form action='options.php' method='post'>
<h2><?php _e('Settings'); ?> > Wikify </h2>
<?php
_e('Wikify is an easy-to-use plugin that adds the functionality of a wiki known from MediaWiki.<br>
It handles shortcodes like [[word]] and [[word|link label]] in your post content.<hr>','wikify');
echo '<br>';
settings_fields( 'pluginPage' );
do_settings_sections( 'pluginPage' );
echo '<br>';
echo '<hr>';
echo '<br>';
settings_fields( 'externalPage' );
do_settings_sections( 'externalPage' );
submit_button();
?>
</form>
<hr>
wikify is designed by Joachim Happel
<?php
}
function wikify_add_style(){
$options = get_option( 'wf_settings' );
if ( empty($options['wf_text_undefinded_style']) ){
$options['wf_text_undefinded_style'] = 'border-bottom: 1px dashed black';
}
$style = '<style>.entry-content a.wikiword-undefined, .entry-summary a.wikiword-undefined {' . $options['wf_text_undefinded_style']. '}</style>';
echo $style;
}
//Add a wikfy Button to the TinyMCE Editor
function wikify_button_init() {
//Abort early if the user will never see TinyMCE
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') && get_user_option('rich_editing') == 'true')
return;
//Add a callback to regiser our tinymce plugin
add_filter("mce_external_plugins",array($this, "wikify_register_tinymce_plugin") );
// Add a callback to add our button to the TinyMCE toolbar
add_filter('mce_buttons', array($this,'wikify_add_tinymce_button') );
// Add a callback to add our stylesheet to mark the brackets
add_filter( 'tiny_mce_before_init', array($this,'wikify_add_tinymce_settings') );
}
//a callback to add a button to the toolbar
function wikify_add_tinymce_button($buttons) {
//Add the button ID to the $button array
$buttons[] = "wikify_button";
$buttons[] = "wikify_split_button";
return $buttons;
}
//a callback to register the wikify plugin
function wikify_register_tinymce_plugin($plugin_array) {
$plugin_array['wikify_button'] = plugins_url('js/wikify.js', __FILE__) ;
return $plugin_array;
}
//a callback to add a editor stylesheet
function wikify_add_tinymce_settings( $initSettings )
{
$initSettings['content_css'] .= ','.plugins_url('css/editor-style.css', __FILE__);
$initSettings['wikify_lang_prompt'] = __('Which term should be linked?', 'wikify');
return $initSettings;
}
/**
* find [[ ]] enclosed words
* either [[word]] or [[word|label]]
* and send matches to wikifyBracketedElement
*/
function wikifyContent($theContent) {
return preg_replace_callback("/\[\[([^\]]+)\]\]/", array($this, 'wikifyBracketedElement'), $theContent);
}
// split word|label and sends it to wikiLink
function wikifyBracketedElement($matches) {
$splitted = explode('|',$matches[1]);
$wikiword = $label = trim($splitted[0]);
if (isset($splitted[1])){
$label = trim($splitted[1]);
}
return $this->wikiLink($wikiword, $label);
}
// returns a link from a wiki word (wikiword|label)
// if post not exists, the wikiword will be marked as undefined and
// linked to a new post to define the wikiword
function wikiLink($wikiword, $label) {
$class = $href = '';
$slug = sanitize_title(strip_tags($wikiword));
if(in_array($slug, $this->forbiddenSlugs)) return $label; ;
if($this->is_external){
return '<a title="'.$this->title_prefix.$label.'" href="' . $this->external_url .$slug. '">' . $label . '</a>';
}
if(!self::postExists($slug)){
return '<a title="'.__('Do explain', 'wikify').' '.$wikiword.'" href="' . $this->urlPrefix . "wp-admin/post-new.php?post_title=".urlencode($wikiword) . '" class="wikiword-undefined">'.
'' . $label . '</a>';
}
return '<a title="'.$label.'" href="' . $this->urlPrefix .$slug. '">' . $label . '</a>';
}
static function postExists($slug){
global $wpdb;
$rs = $wpdb->get_var(
$wpdb->prepare(
"SELECT count(ID) FROM $wpdb->posts
WHERE post_name = %s
", $slug
)
);
return ( $rs>0) ? true : false;
}
function ajax_check_external_usage(){
if($this->is_external){
echo json_encode(array('external'=>true) ) ;
}
die();
}
function ajax_wikify_post_exists(){
if($this->is_external){
echo json_encode(array('result' => array(), 'external'=>true, 'prefix'=>$this->title_prefix) );
die();
}
global $wpdb;
$contents = explode(',',$_REQUEST['contents']);
$wikiwords = array(); $data=$result= array();
foreach($contents as $c){
$slug = esc_sql( sanitize_title(strip_tags($c)) );
$wikiwords[] = $slug;
$data[$slug]['label'] = $c;
}
$in ="'". implode("','", $wikiwords) ."'";
$rs = $wpdb->get_results(
"SELECT post_name, post_title FROM $wpdb->posts
WHERE post_name in ($in )
"
);
if($rs ){
foreach($rs as $ds){
$data[$ds->post_name]['title'] = $ds->post_title;
$data[$ds->post_name]['slug'] = $ds->post_name;
}
foreach($data as $d){
$result[]=$d;
}
}
echo json_encode(array('result' => $result, 'prefix'=>$this->title_prefix) );
die();
}
}
$wikify_instance = new wp_wikify();
add_action( 'wp_ajax_wikify_post_exists', array($wikify_instance, 'ajax_wikify_post_exists') );
add_action( 'wp_ajax_check_external_usage', array($wikify_instance, 'ajax_check_external_usage') );
?>