-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomagicalinks.php
469 lines (356 loc) · 17.7 KB
/
automagicalinks.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php
/**
* @package Automagicalinks
* @version 0.1
*/
/*
Plugin Name: Automagicalinks
Plugin URI: https://github.com/baizmandesign/automagicalinks
Description: Automagically convert text to internal links on your website.
Author: Saul Baizman
Version: 0.1
Author URI: https://baizmandesign.com/
License: GPLv2
*/
register_activation_hook ( __FILE__, 'automagicalinks_set_default_options_array' );
function automagicalinks_set_default_options_array ( ) {
$options = array (
'autolinking' => '',
'automagicality' => '',
'link_characters' => '',
'link_escape_character' => '',
'allowed_post_types' => '',
'allowed_link_names' => '',
'excluded_elements' => '',
'aliases' => '',
) ;
update_option ( 'automagicalinks_options', $options );
}
add_action ( 'admin_menu', 'automagicalinks_admin_menu', 1 ) ;
function automagicalinks_admin_menu ( )
{
add_options_page ( 'Automagicalinks Settings', 'Automagicalinks', 'manage_options', 'automagicalinks', 'automagicalinks_settings_page', 'dashicons-admin-links' );
}
add_action( 'admin_init', 'automagicalinks_settings' );
function automagicalinks_settings ( )
{
register_setting( 'automagicalinks-plugin-settings-group', 'autolinking' );
register_setting( 'automagicalinks-plugin-settings-group', 'automagicality' );
register_setting( 'automagicalinks-plugin-settings-group', 'link_characters' );
register_setting( 'automagicalinks-plugin-settings-group', 'link_escape_character' );
register_setting( 'automagicalinks-plugin-settings-group', 'allowed_post_types' );
register_setting( 'automagicalinks-plugin-settings-group', 'allowed_link_names' );
register_setting( 'automagicalinks-plugin-settings-group', 'excluded_elements' );
register_setting( 'automagicalinks-plugin-settings-group', 'aliases' );
}
add_action ( 'admin_post_update', 'save_automagicalinks_settings' ) ;
function save_automagicalinks_settings ( ) {
// Check that user has proper security level
if ( ! current_user_can ( 'manage_options' ) ) {
wp_die ( 'Not allowed' ) ;
}
$automagicalinks_options = get_option ( 'automagicalinks_options' ) ;
$updated_options = array ( ) ;
foreach ( $automagicalinks_options as $option => $value ) {
if ( isset ( $_POST[$option] ) ) {
$updated_options[$option] = $_POST[$option] ;
}
else {
// For the checkboxes.
$updated_options[$option] = '' ;
}
}
// Update the options.
update_option ( 'automagicalinks_options', $updated_options );
// Redirect with success=1 query string.
wp_redirect (
add_query_arg (
array (
'page' => 'automagicalinks',
'message' => '1',
),
admin_url ( 'options-general.php' )
)
);
}
function automagicalinks_settings_page ()
{
$automagicalinks_options = get_option ( 'automagicalinks_options' ) ;
?>
<div class="wrap">
<h1>Automagicalinks settings</h1>
<?php if ( isset( $_GET['message'] ) && $_GET['message'] == '1' ) : ?>
<div id='message' class='updated fade'>
<p><strong>Settings Saved</strong></p>
</div>
<?php endif ; ?>
<form method="post" action="<?php echo get_admin_url() ; ?>admin-post.php">
<?php settings_fields ( 'automagicalinks-plugin-settings-group' ) ; ?>
<?php do_settings_sections ( 'automagicalinks-plugin-settings-group' ) ; ?>
<table class="form-table">
<tbody>
<tr>
<th scope="row" colspan="4">Post Types Whose Names Will Become Links</th>
</tr>
<?php
$all_post_types = get_post_types ( ) ;
$allowed_link_names = $automagicalinks_options['allowed_link_names'] ;
$columns = 4 ;
$column_counter = 0;
$post_counter = 0 ;
ksort ($all_post_types);
foreach ( $all_post_types as $name => $value ) {
if ( $column_counter%$columns == 0 ) {
printf( '<tr>' );
}
$replace['-'] = ' ' ;
$replace['_'] = ' ' ;
$replace['wp'] = 'WP' ;
$name = ucwords ( strtr ( $name, $replace ) ) ;
printf ( '<td><input type="checkbox" name="allowed_link_names[%1$s]" id="posts_%3$d" value="1"' . checked ( '1', isset ( $allowed_link_names[$value] ), false ) . '> <label for="posts_%3$d">%2$s</label></td>', $value, $name, $post_counter );
$column_counter++ ;
if ( $column_counter%$columns == 0 ) {
printf ( '</tr>' );
$column_counter = 0;
}
$post_counter++ ;
}
?>
</tbody>
</table>
<!--////////////////////////////////////////////////////////////-->
<table class="form-table">
<tbody>
<tr>
<th scope="row" colspan="4">Post Types Whose Text Will Be Scanned For The Names Of The Post Types Identified Above</th>
</tr>
<?php
$allowed_post_types = $automagicalinks_options['allowed_post_types'] ;
$columns = 4 ;
$column_counter = 0;
$post_counter = 0 ;
ksort ($all_post_types);
foreach ( $all_post_types as $name => $value ) {
if ( $column_counter%$columns==0 ) {
printf( '<tr>' );
}
$replace['-'] = ' ' ;
$replace['_'] = ' ' ;
$replace['wp'] = 'WP' ;
$name = ucwords( strtr ( $name, $replace ) ) ;
printf ( '<td><input type="checkbox" name="allowed_post_types[%1$s]" id="posts_%3$d" value="1"' . checked ( '1', isset ( $allowed_post_types[$value] ), false ) . '> <label for="posts_%3$d">%2$s</label></td>', $value, $name, $post_counter );
$column_counter++ ;
if ( $column_counter%$columns==0 ) {
printf( '</tr>' );
$column_counter = 0;
}
$post_counter++ ;
}
?>
</tbody>
</table>
<!--////////////////////////////////////////////////////////////-->
<table class="form-table">
<tr valign="top">
<th scope="row" width="30%"><label for="autolinking">Enable Autolinking:</label></th>
<td width="70%"><input type="checkbox" name="autolinking" id="autolinking" value="1" <?php checked ( '1', $automagicalinks_options['autolinking'], true ); ?>/></td>
</tr>
<tr valign="top">
<th scope="row"><small>With autolinks, any text in the body of a page wrapped in link characters (below) and that matches a page name will be linked to that page.</small></th>
<td></td>
</tr>
<tr valign="top">
<th scope="row">Link Characters:</th>
<td>
<select name="link_characters">
<?php
$link_character_pairs = array (
'[[ ]]',
'{{ }}',
'## ##',
'%% %%',
'|| ||',
) ;
foreach ( $link_character_pairs as $pairs ) {
list ( $start, $end ) = explode ( ' ', $pairs ) ;
$selected = $pairs == $automagicalinks_options['link_characters'] ? ' selected' : '' ;
printf ( '<option value="%1$s %2$s"%4$s>%1$s%3$s%2$s</option>', $start, $end, 'text', $selected ) ;
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">Aliases:<br></th>
<td><textarea name="aliases" rows="8" cols="50" placeholder="William Smith=Will Smith,Willy Smith"><?php echo esc_attr ( $automagicalinks_options['aliases'] ) ; ?></textarea></td>
</tr>
<tr valign="top">
<th scope="row"><label for="automagicality">Enable Automagicality:</label></th>
<td><input type="checkbox" name="automagicality" id="automagicality"
value="1"<?php checked ( '1', $automagicalinks_options['automagicality'], true ); ?>/></td>
</tr>
<tr valign="top">
<th scope="row"><small style="color: red">Note: enabling Automagicality ignores the link characters. (Don't worry, they'll be removed.) Use for extreme awesome.</small></th>
<td></td>
</tr>
<tr valign="top">
<th scope="row">Automagicalink Escape Characters:</th>
<td>
<select name="link_escape_character">
<?php
$escape_characters = array (
'!!',
'--',
) ;
foreach ( $escape_characters as $characters ) {
$selected = $characters == $automagicalinks_options['link_escape_character'] ? ' selected' : '' ;
printf ( '<option value="%1$s"%2$s>%1$s</option>', $characters, $selected ) ;
}
?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><small>Each word in a phrase must be escaped to prevent automagical links from manifesting.</small></th>
<td></td>
</tr>
<tr valign="top">
<th scope="row">Globally excluded phrases:<br></th>
<td><textarea name="excluded_elements" rows="8" cols="50" placeholder="Enter exclusion item per line."><?php echo esc_attr ( $automagicalinks_options['excluded_elements'] ); ?></textarea></td>
</tr>
</table>
<?php
?>
<?php submit_button(); ?>
</form>
</div>
<?php }
function automagicalinks_filter ( $content ) {
global $wpdb;
$automagicalinks_options = get_option ( 'automagicalinks_options' ) ;
$autolinking = $automagicalinks_options['autolinking'] ;
$automagicality = $automagicalinks_options['automagicality'] ;
$link_characters = $automagicalinks_options['link_characters'] ;
list ( $link_start_characters, $link_end_characters ) = explode (' ', $link_characters ) ;
$link_escape_character = $automagicalinks_options['link_escape_character'] ;
$allowed_post_types = $automagicalinks_options['allowed_post_types'] ;
$allowed_link_names = $automagicalinks_options['allowed_link_names'] ;
$excluded_elements = $automagicalinks_options['excluded_elements'] ;
$aliases = $automagicalinks_options['aliases'] ;
if ( $autolinking ) {
if ( is_singular ( ) && in_the_loop ( ) && is_main_query ( ) ) {
// Check for whether we're looking at an allowed post type.
$post_types = array_keys ( $allowed_post_types ) ;
$this_post_type = get_post_type ( ) ;
if ( ! in_array ( $this_post_type, $post_types ) ) {
return $content ;
}
$replace_pairs = array ( ) ;
$duplicates_pairs = array ( );
$link_names = array_keys ( $allowed_link_names ) ;
if ( ! $link_names ) {
return $content ;
}
// Note our permalink structure below.
// This may have to be customized for other sites!
// In the wp_options table there is a record where option_name = "permalink_structure",
// according to https://wordpress.stackexchange.com/questions/58625/where-is-permalink-info-stored-in-database
$all_pages_sql = sprintf ("SELECT ID, post_title, post_name, post_type, concat_ws('/','%s', post_type, post_name,'') AS permalink FROM %s WHERE post_type IN ('%s') AND post_title != '%s' and post_status = '%s'",
'//' . $_SERVER['HTTP_HOST'],
$wpdb->posts,
implode ( "','", $link_names ),
'Auto Draft',
'publish') ;
$all_pages = $wpdb->get_results($all_pages_sql);
if ( $all_pages ) {
// Create aliases.
if ( $aliases ) {
$aliases_substitutions = array ();
$aliases_array = explode( "\n", $aliases );
foreach ( $aliases_array as $alias ) {
// Is this a validly formatted line?
if ( strstr( $alias, '=' ) ) {
list ( $real_title, $synonyms ) = explode( '=', $alias );
// Does this alias have multiple synonyms?
if ( strstr( $synonyms, ',' ) ) {
$synonyms_array = explode( ',', $synonyms );
} else {
$synonyms_array[] = $synonyms;
}
foreach ( $synonyms_array as $synonym ) {
// The trim() call removes a newline character.
$aliases_substitutions[$real_title][] = trim ( $synonym ) ;
}
// Check that we're not replacing an alias with a page that exists.
foreach ( $all_pages as $page ) {
if ( $page->post_title == $real_title ) {
unset ( $aliases_substitutions[$real_title] ) ;
}
}
}
}
}
foreach ( $all_pages as $page ) {
// Look for double brackets or not?
$search = $automagicality ? $page->post_title : $link_start_characters . $page->post_title . $link_end_characters;
$replace = sprintf( '<a href="%1$s">%2$s</a>', $page->permalink, $page->post_title );
// Fix "real" links that get nested after we automagically link them:
// <a href=""><a href="">Word</a></a>
$dupe_search = sprintf( '<a href="%1$s">',$page->permalink).sprintf( '<a href="%1$s">',$page->permalink).$page->post_title.'</a>'.'</a>';
$dupe_replace = $replace;
// Does the current page have any alias substitutions?
if ( isset ( $aliases_substitutions[$page->post_title]) ) {
foreach ( $aliases_substitutions as $real => $akas ) {
foreach ( $akas as $aka ) {
$aka_search = $automagicality ? $aka : $link_start_characters . $aka . $link_end_characters;
$aka_replace = sprintf( '<a href="%1$s">%2$s</a>', $page->permalink, $aka ) ;
$aka_dupe_search = sprintf( '<a href="%1$s">',$page->permalink).sprintf( '<a href="%1$s">',$page->permalink).$aka.'</a>'.'</a>';
$aka_dupe_replace = $aka_replace ;
$replace_pairs[ $aka_search ] = $aka_replace ;
$duplicates_pairs[ $aka_dupe_search ] = $aka_dupe_replace ;
}
}
}
$replace_pairs[ $search ] = $replace;
$duplicates_pairs[ $dupe_search ] = $dupe_replace;
}
/*
* Excluded the exceptions.
*/
$excluded_elements_array = explode("\n",$excluded_elements) ;
if ($excluded_elements_array) {
foreach ( $excluded_elements_array as $excluded_element ) {
// Remove newline character.
$excluded_element = trim ( $excluded_element ) ;
if ( isset ( $replace_pairs[$excluded_element] ) ) {
unset ( $replace_pairs[$excluded_element] ) ;
unset ( $duplicates_pairs[$excluded_element]) ;
}
}
}
// The magic happens here.
$content = strtr ( $content, $replace_pairs );
$content = strtr ( $content, $duplicates_pairs );
// Remove escape character.
$content = str_replace ( $link_escape_character, '', $content );
}
else {
return $content;
}
}
}
// Remove start characters, end characters, and escape characters in
// case the user enables and later disables autolinking.
// Requires the plugin to still be activated, of course!
if ($link_start_characters) {
$content = str_replace( $link_start_characters,'',$content) ;
}
if ($link_end_characters) {
$content = str_replace( $link_end_characters,'',$content) ;
}
if ($link_escape_character) {
$content = str_replace( $link_escape_character,'',$content) ;
}
return $content;
}
add_filter ( 'the_content', 'automagicalinks_filter' ) ;