-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcode-logic.php
604 lines (445 loc) · 18.6 KB
/
shortcode-logic.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<?php
/*
Plugin Name: Shortcode Logic
Description: Extends plugin logic with some shortcode logic
Author: Sebastian Gärtner
Author URI: https://github.com/ibes
Version: 0.3
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/* thanks to plugin logic for lots of inspirations */
// Security check
if ( ! class_exists('WP') ) {
header('Status: 403 Forbidden');
header('HTTP/1.1 403 Forbidden');
exit();
}
define( 'SCLO_DBTABLE', $GLOBALS['wpdb']->base_prefix . 'shortcode_logic' );
// on register
register_activation_hook( __FILE__, 'shortcode_logic_on_activation' );
register_deactivation_hook( __FILE__, 'shortcode_logic_on_deactiviation' );
register_uninstall_hook( __FILE__, 'shortcode_logic_on_uninstall' );
function shortcode_logic_on_activation () {
shortcode_logic_create_databse_table();
// to do: regenerate shortcode rules file after reactivation
// show message if neigher plugin organizer nor plugin logic are active
}
// admin page
add_action('admin_menu', 'shortcode_logic_setup_menu');
function shortcode_logic_setup_menu(){
add_plugins_page( 'Shortcode Logic', 'Shortcode Logic', 'activate_plugins', 'shortcode_logic', 'shortcode_logic_page' );
}
// register setting
add_action( 'admin_init', 'shortcode_logic_register_setting' );
function shortcode_logic_register_setting() {
// plugins to watch
register_setting( 'shortcode_logic_options', 'shortcode_logic_watched_shortcodes' );
}
function shortcode_logic_page() {
echo '<div class="wrap">';
echo '<h2>Shortcode Logic</h2>';
// if plugin logic and plugin organizer are not activated, show message
if ( ! ( is_plugin_active('plugin-logic/plugin-logic.php') || is_plugin_active('plugin-organizer/plugin-organizer.php') ) ) {
echo '<div class="error"><p>' . __('Weder Plugin Logic noch Plugin Organizer wurden erkannt. <br>Evtl. hat Shortcode Logic somit keinen weiteren Effekt. Bitte stell sicher, dass eines der beiden Plugins aktiviert ist.') . '</p></div>';
}
global $shortcode_tags;
$output = '';
$watched_shortcodes = get_option('shortcode_logic_watched_shortcodes');
foreach ($shortcode_tags as $shortcode_tag => $callback) {
$path = '';
if ( gettype($callback) == 'string' AND strpos($callback, "::") == false) {
$reflector = new ReflectionFunction($callback);
} elseif ( gettype($callback) == 'string' ) {
$callback = explode('::' ,$callback);
$class = $callback[0];
$reflector = new ReflectionClass($class);
} elseif ( gettype($callback) == 'array' ) {
$class = $callback[0];
if ( gettype($callback[0]) != 'string') {
$class = get_class($callback[0]);
}
$reflector = new ReflectionClass($class);
}
$path = $reflector->getFileName();
$plugin_data = shortcode_logic_plugin_name_from_path( $path );
if ( $plugin_data != false ) {
$checked = isset( $watched_shortcodes[$shortcode_tag] ) ? 'checked' : '';
$output .= '<li><label><input type="checkbox" name="shortcode_logic_watched_shortcodes[' . $shortcode_tag . ']" value="' . $shortcode_tag . '" ' . $checked . '><strong>[' . $shortcode_tag . ']</strong>' . ' ' . $plugin_data['name'] . '</label></li>';
}
}
if ( '' != $output ) {
echo '<form method="post" action="options.php">';
settings_fields( 'shortcode_logic_options' );
do_settings_sections( 'shortcode_logic_options' );
echo '<ul>' . $output . '</ul>';
submit_button();
echo '</form>';
} else {
echo __('No selectable shortcodes found');
}
echo '<hr>';
echo '<form method="post" enctype="multipart/form-data">';
submit_button('Regeldatei regenerieren', 'secondary', 'shortcode_logic_regenerate_all');
echo '</form>';
if (isset($_POST['shortcode_logic_regenerate_all'])) {
$regenerate = shortcode_logic_regenerate_detected_shortcodes();
$write_rules = shortcode_logic_write_rule_file();
if ($regenerate) {
echo '<div class="notice notice-success"><p>' . __('Shortcode logic Regel regeneriert') . '</p></div>';
}
}
}
function shortcode_logic_plugin_name_from_path( $path ) {
// get plugin directory name
$replace = '/.*\/plugins\/([a-zA-Z0-9-]*)\/.*/i';
$plugin_directory_name = preg_replace($replace, '$1', $path);
// preg_replace returns NULL on error or original string if no match
// break if no plugin directory name found
if ( !isset( $plugin_directory_name ) || ( $plugin_directory_name == $path ) ) {
return false;
}
// get all registered plugins
$plugins = get_plugins();
// try to find plugin directory in registered plugins
foreach ( $plugins as $p_path => $p_info ) {
$p_dir = explode('/', $p_path);
if ( $p_dir[0] == $plugin_directory_name ) {
$plugin_info = $p_info;
$plugin_path = $p_path;
}
}
// return array of name and path
$plugin_data = array( 'name' => $plugin_info['Name'], 'path' => $plugin_path );
return $plugin_data;
}
function shortcode_logic_shortcode_to_plugin_path( $shortcode ) {
// get all registered shortcodes
global $shortcode_tags;
if ( !isset($shortcode) || !isset($shortcode_tags[$shortcode]) ) {
return false;
}
// get transient
if ( false !== ( $plugin_path = get_transient( 'shortcode-logic-path-for-' . $shortcode ) ) ) {
return $plugin_path;
}
// get callback function of selected shortcode
// callback could be function, string of Class::method, or array of ([0] => Class, [1] => method)
$callback = $shortcode_tags[$shortcode];
if ( gettype($callback) == 'string' AND strpos($callback, "::") == false) {
$reflector = new ReflectionFunction($callback);
} elseif ( gettype($callback) == 'string' ) {
$callback = explode('::' ,$callback);
$class = $callback[0];
$reflector = new ReflectionClass($class);
} elseif ( gettype($callback) == 'array' ) {
$class = $callback[0];
if ( gettype($callback[0]) != 'string') {
$class = get_class($callback[0]);
}
$reflector = new ReflectionClass($class);
}
// get path of callback function (or plugin class)
$path = '';
$path = $reflector->getFileName();
// get plugin name from path
$plugin_data = shortcode_logic_plugin_name_from_path( $path );
// set transient
set_transient( 'shortcode-logic-path-for-' . $shortcode , $plugin_data['path']);
return $plugin_data != false ? $plugin_data['path'] : false;
}
// check for shortcodes on save posts
add_action( 'save_post', 'shortcode_logic_shortcode_detector', 10, 3 );
function shortcode_logic_shortcode_detector( $post_id, $post, $update ) {
if ( 'post' != $post->post_type || defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision( $post_id ) ) {
return;
}
$changed = shortcode_logic_dectect_shortcode($post->ID, $post->post_content);
// if found shortcodes
if ( $changed ) {
shortcode_logic_write_rule_file();
}
//set_transient("shortcode_logic_admin_message", array($return, $found_shortcodes, $db_query, 'hey') , 940);
}
/*
function shortcode_logic_admin_notices() {
$user_id = get_current_user_id();
if ( $tagnames = get_transient( "shortcode_logic_admin_message" ) ) { ?>
<div class="error">
<p>
<pre>
<?php echo print_r($tagnames, true); ?>
</pre>
</p>
</div><?php
delete_transient("shortcode_logic_admin_message");
}
}
*/
// add_action( 'admin_notices', 'shortcode_logic_admin_notices' );
function shortcode_logic_create_databse_table() {
global $wpdb;
$charset_collate = '';
if ( ! empty( $wpdb->charset ) )
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if ( ! empty( $wpdb->collate ) )
$charset_collate .= " COLLATE $wpdb->collate";
$blog_id_col = ( is_multisite() ) ? "blog_id bigint(20) NOT NULL, " : '';
$wpdb->query(
"CREATE TABLE IF NOT EXISTS ". SCLO_DBTABLE ." (
$blog_id_col
post_id int NOT NULL PRIMARY KEY,
rule longtext NOT NULL,
permalink longtext NOT NULL,
checksum longtext NOT NULL
) $charset_collate;"
);
}
/**
*
* return: (bool) true if something changed
*/
function shortcode_logic_write_rule_to_db($post_id, $shortcodes) {
// Write updates to database
global $wpdb;
$db_row = $wpdb->get_results( "SELECT post_id, checksum, permalink FROM " . SCLO_DBTABLE . " WHERE post_id = '$post_id'" );
// delete rule from database
if ( empty($shortcodes) && $db_row != NULL ) {
$response = $wpdb->delete( SCLO_DBTABLE, array( 'post_id' => $post_id ) );
return true;
}
if ( empty($shortcodes) ) {
return false;
}
// use checksum to make it faster
$new_checksum = crc32(implode($shortcodes));
$new_permalink = get_permalink($post_id);
// shortcodes or permalink have changed
// update existing entry
if ( $db_row != NULL && ( $db_row[0]->checksum == $new_checksum && $db_row[0]->permalink == $new_permalink ) ) {
// nothing changed
return false;
} elseif ( $db_row != NULL ) {
// update existing entry
$rule = shortcode_logic_generate_rule($post_id, $shortcodes);
$wpdb->update( SCLO_DBTABLE, array(
'post_id' => $post_id ,
'rule' => $rule,
'permalink' => $new_permalink,
'checksum' => $new_checksum
), array('post_id' => $post_id)
);
return true;
} else {
// create new entry
$rule = shortcode_logic_generate_rule($post_id, $shortcodes);
global $wpdb;
$wpdb->insert( SCLO_DBTABLE, array(
'post_id' => $post_id,
'rule' => $rule,
'permalink' => $new_permalink,
'checksum' => $new_checksum
)
);
return true;
}
return true;
}
function shortcode_logic_generate_rule($post_id, $shortcodes) {
// this creates something like this
// $rules['http://example.com/some/url/fraqments/'] = array( 'one-plugin/one-plugin.php','another-plugin/another-plugin.php','yet-another-ultimative-wp-plugin/yet-another-ultimative-wp-plugin.php', );
$plugins = array();
foreach ($shortcodes as $shortcode) {
$plugins[] = shortcode_logic_shortcode_to_plugin_path($shortcode);
}
array_unique($plugins);
$plugin_paths = implode ( "', '" , $plugins );
$plugin_paths = "'" . $plugin_paths . "'";
$permalink = get_permalink($post_id);
$permalink = str_replace( home_url(), '', $permalink );
$rule = "\t\t\$rules['" . $permalink . "'] = array( " . $plugin_paths . " );";
return $rule;
}
function shortcode_logic_write_rule_file() {
$rule_file = WPMU_PLUGIN_DIR . '/' . 'shortcode-logic-rules.php';
$rules = shortcode_logic_generate_rules_file_content();
if ( $rules != '') {
// Check directory permissions and write the WPMU_PLUGIN_DIR directory if not exists,
if ( !file_exists( WPMU_PLUGIN_DIR ) ) {
if ( is_writable( WP_CONTENT_DIR ) ) {
mkdir(WPMU_PLUGIN_DIR, 0755);
} else {
$error_in1 = substr(WP_CONTENT_DIR, strlen( ABSPATH ));
$error_in2 = substr(WPMU_PLUGIN_DIR, strlen( ABSPATH ));
$write_error = '';
$write_error .= '<div class="update-nag"><p> Your <code>'. $error_in1 .'</code> directory isn’t <a href="http://codex.wordpress.org/Changing_File_Permissions">writable</a>.<br>';
$write_error .= 'These are the rules you should write in an PHP-File, create the '. $error_in2 .' directory and place it in the <code>'. $error_in2 .'</code> directory. ';
$write_error .= 'Click in the field and press <kbd>CTRL + a</kbd> to select all. </p>';
$write_error .= '<textarea readonly="readonly" name="rules_txt" rows="7" style="width:100%; padding:11px 15px;">' . $rules . '</textarea></div>';
$write_error .= '<br><br>';
$err_arr[] = $write_error;
$err_arr[] = $rules;
return $err_arr;
}
}
// Check directory permissions and write the plugin-logic-rules.php file
if ( file_exists( WPMU_PLUGIN_DIR ) ) {
if ( is_writable( WPMU_PLUGIN_DIR ) ) {
file_put_contents( $rule_file, $rules );
} else {
$error_in = substr(WPMU_PLUGIN_DIR, strlen( ABSPATH ));
$write_error = '';
$write_error .= '<div class="update-nag"><p> Your <code>'. $error_in .'</code> directory isn’t <a href="http://codex.wordpress.org/Changing_File_Permissions">writable</a>.<br>';
$write_error .= 'These are the rules you should write in an PHP-File and place it in the <code>'. $error_in .'</code> directory. ';
$write_error .= 'Click in the field and press <kbd>CTRL + a</kbd> to select all. </p>';
$write_error .= '<textarea readonly="readonly" name="rules_txt" rows="7" style="width:100%; padding:11px 15px;">' . $rules . '</textarea></div>';
$write_error .= '<br><br>';
$err_arr[] = $write_error;
$err_arr[] = $rules;
return $err_arr;
}
}
} elseif ( file_exists( $rule_file ) ) {
if ( !unlink( $rule_file ) ) {
wp_die( wp_sprintf(
'Error cannot delete the old rule file: <code>' . substr($rule_file, strlen( ABSPATH )) .'</code>'.
' The directory isn’t writable.'
)
);
}
}
return false;
}
/***
* Creates the plugin activation/deactivation rules for singlesite installations
* @since 1.0.4
*/
function shortcode_logic_generate_rules_file_content() {
$rule_file_content = '';
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM " . SCLO_DBTABLE );
$rules = "\t\t\$rules = array();\n";
foreach ($results as $result) {
$rules .= $result->rule . "\n" ;
}
if ( !empty($rules) ) {
// Structur from the beginning and the end of the rule file
$first_part = "<?php \n";
$first_part .= "/***\n";
$first_part .= " * Contains the rules for the activation and deactivation of the plugins \n";
$first_part .= " *\n";
$first_part .= " * @package Shortcode Logic\n";
$first_part .= " * @author ib4s\n";
$first_part .= " * \n";
$first_part .= " * @change 1.0.4\n";
$first_part .= " * @since 1.0.0\n";
$first_part .= " */\n\n";
$first_part .= "if ( !is_admin() ) {\n\n";
$first_part .= "\tfunction shortcode_logic_rules( \$plugins ) { \n";
$first_part .= "\t\t// from https://example.com/hello/world?how_are=you&great \n";
$first_part .= "\t\t// to /hello/world \n";
$first_part .= "\t\t\$url = strtok( \$_SERVER['REQUEST_URI'], '?' ); \n";
$first_part .= "\n\n";
$last_part = "\n\n";
$last_part .= "\t\tif ( isset( \$rules[\$url]) ) { \n";
$last_part .= "\t\t\t// add plugin to activated plugin list \n";
$last_part .= "\t\t\t\$plugins = array_unique( array_merge( \$plugins, \$rules[\$url] ) ); \n";
$last_part .= "\t\t} \n";
$last_part .= "\n\n";
$last_part .= "\t\treturn \$plugins; \n";
$last_part .= "\t}";
$last_part .= "\n\n";
$last_part .= "\tadd_filter( 'option_active_plugins', 'shortcode_logic_rules', 20 );\n";
$last_part .= "}\n";
$rule_file_content = $first_part . $rules . $last_part;
}
return $rule_file_content;
}
function shortcode_logic_regenerate_detected_shortcodes() {
global $wpdb;
// get all posts
$posts = $wpdb->get_results(
"SELECT ID, post_content FROM " . $GLOBALS['wpdb']->base_prefix . "posts WHERE post_type = 'post' AND post_status = 'publish'"
);
// delete all existing entries
$wpdb->query(
"TRUNCATE TABLE " . SCLO_DBTABLE
);
$output = array();
foreach ( $posts as $post )
{
shortcode_logic_dectect_shortcode($post->ID, $post->post_content);
}
return true;
}
/**
* Shortcode_logic_dectect_shortcode
* Detect shortcodes in content and write rule to database, if there is one
*
* input: post_id and post_content
* return: (bool) true if there where changes
*/
function shortcode_logic_dectect_shortcode($post_id, $post_content) {
// get plugins that should be watched
// to do - if not set, don't do stuff
$watched_shortcodes = get_option('shortcode_logic_watched_shortcodes');
if ( empty($watched_shortcodes) ) {
return;
}
// get active shortcodes
global $shortcode_tags;
if ( empty($shortcode_tags) || !is_array($shortcode_tags) ) {
return false;
} else {
$found_shortcodes = array();
// Find all registered tag names in $content.
preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $post_content, $matches );
// get active shortcodes that are watched and used in the content
$found_shortcodes = array_intersect( array_keys( $shortcode_tags ), $matches[1], array_keys( $watched_shortcodes ) );
// sort plugins
if (is_array( $found_shortcodes) ) {
sort($found_shortcodes);
} else {
$found_shortcodes = array();
}
// write or update database entry for this post
// if no plugins registered - delete entry for this post
shortcode_logic_write_rule_to_db($post_id, $found_shortcodes);
$shortcodes_count = count($found_shortcodes);
return $shortcodes_count;
}
}
/***
* Actions if user deactivate Shortcode Logic:
* Delete the rule file in the WPMU_PLUGIN_DIR and if the directory is empty also delete them
*
*/
function shortcode_logic_on_deactiviation() {
$rule_file = WPMU_PLUGIN_DIR . '/' . 'shortcode-logic-rules.php';
if ( file_exists($rule_file) ) {
if ( !unlink($rule_file) ) {
trigger_error("Cannot delete the old rule file: <code>" .
substr($rule_file, strlen( ABSPATH )) ."</code>", E_USER_ERROR);
}
}
if ( file_exists( WPMU_PLUGIN_DIR ) ) {
if ( count(scandir(WPMU_PLUGIN_DIR) <= 2) ) rmdir( WPMU_PLUGIN_DIR );
}
}
/***
* Actions if user uninstall Shortcode Logic:
* Delete database entries
* Delete setting
* Delete transient
* Delete rules file
*
*/
function shortcode_logic_on_uninstall() {
shortcode_logic_on_deactiviation();
delete_option( 'shortcode_logic_watched_plugins' );
// Drop a custom db table
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS " . SCLO_DBTABLE );
// delete transients
$wpdb->query("DELETE FROM `{$wpdb->prefix}options` WHERE `option_name` LIKE ('_transient_shortcode-logic%')");
}