-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-cloudflare.php
109 lines (100 loc) · 3.47 KB
/
wp-cloudflare.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
<?php
/*
Plugin Name: WP Cloudflare
Plugin URI: http://nabtron.com/cloudflare-rocketscript
Donate link: payment@nabtron.com
Description: enables you to enable or disable rocketscript on selected javascript files
Author: nabtron
Author URI: http://nabtron.com/
Version: 0.1.4
Min WP Version: 4.4
Max WP Version: 5.8.1
*/
// prevent direct access
defined('ABSPATH') or die('Plugin file cannot be accessed directly.');
if (!class_exists('nabcfrs_main')) {
class nabcfrs_main
{
// PHP 5 Constructor
function __construct()
{
add_action('admin_menu', 'nabcfrs_menu');
add_filter('script_loader_tag', 'nabcfrs_truefalse', 10, 2);
}
}
function nabcfrs_settings()
{
// Update routines
if (!empty($_POST['action_nabcfrs']) && 'insert' == $_POST['action_nabcfrs']) {
update_option("nabcfrs_true", $_POST['nabcfrs_true']);
update_option("nabcfrs_false", $_POST['nabcfrs_false']);
}
?>
<!-- Start Options Admin area -->
<div class="wrap">
<h2>WP Cloudflare - Cloudflare Rocketscript Options</h2>
<div style="margin-top:20px;">
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>&updated=true">
<div style="">
<table class="form-table">
<tr>
<th scope="row"><strong>Turn rocketscript on for:</strong></th>
<td><input name="nabcfrs_true" size="40" value="<?= get_option("nabcfrs_true"); ?>" type="text" />
<br />Comma seperated JS filenames. Make sure rocketscript is set to manual at cloudflare.com</td>
</tr>
<tr>
<th scope="row"><strong>Turn rocketscript off for:</strong></th>
<td><input name="nabcfrs_false" size="40" value="<?= get_option("nabcfrs_false"); ?>" type="text" />
<br />Comma seperated JS filenames. Make sure rocketscript is set to automatic at cloudflare.com</td>
</tr>
</table>
<br>
<p class="nabcfrs_submit">
<input name="action_nabcfrs" size="10" value="insert" type="hidden" />
<input name="nabcfrs_submit" type="submit" class="button-primary" id="nabcfrs_submit" value="Save changes">
</p>
</form>
<br /><br />
<hr />
<center>
<h4>Developed by <a href="http://nabtron.com/" target="_blank">Nabtron</a>.</h4>
</center>
</div>
</div>
<?php
} // End function nabcfrs_settings()
// Admin menu Option
function nabcfrs_menu()
{
add_options_page('Cloudflare Rocketscript', 'Cloudflare Rocketscript', 'manage_options', __FILE__, 'nabcfrs_settings');
}
function nabcfrs_getoption($content)
{
// if(!is_admin()) {
$nabcfrs_true = get_option('nabcfrs_true');
$nabcfrs_false = get_option('nabcfrs_false');
// }
}
function nabcfrs_truefalse($tag)
{
global $wpdb;
// get list of files for both true and false
//array map helps us trim them so that there isn't the error of skipping
//array filter removed the empty ones
$nabcfrs_true = array_filter(array_map('trim', explode(',', get_option('nabcfrs_true'))));
$nabcfrs_false = array_filter(array_map('trim', explode(',', get_option('nabcfrs_false'))));
foreach ($nabcfrs_true as $true_script) {
if (true == strpos($tag, $true_script))
return str_replace(' src', ' data-cfasync="true" src', $tag);
}
foreach ($nabcfrs_false as $false_script) {
if (true == strpos($tag, $false_script))
return str_replace(' src', ' data-cfasync="false" src', $tag);
}
return $tag;
}
}
//instantiate the class
if (class_exists('nabcfrs_main')) {
$nabwrap_main = new nabcfrs_main();
}