-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-logic.php
More file actions
244 lines (212 loc) · 8.8 KB
/
script-logic.php
File metadata and controls
244 lines (212 loc) · 8.8 KB
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
<?php
/*
Plugin Name: Script Logic
Plugin URI: http://scriptbaker.com/script-logic
Description: Control scripts & stylesheets with WP's conditional tags is_home etc
Version: 0.3
Author: Tahir Yasin
Author URI: http://scriptbaker.com/
*/
if (!class_exists('sbScriptLogic')) :
// DEFINE PLUGIN ID
define('SLPLUGINOPTIONS_ID', 'sb_scriptlogic');
// DEFINE PLUGIN NICK
define('SLPLUGINOPTIONS_NAME', 'Script Logic');
class sbScriptLogic
{
public function __construct()
{
if (is_admin())
{
add_action('admin_enqueue_scripts', array($this, 'load_custom_scripts'));
add_action('switch_theme', array($this, 'switch_theme'));
add_action('admin_init', array($this, 'register'));
add_action('admin_menu', array($this, 'menu'));
}
add_action('wp_print_scripts', array($this, 'inspect_scripts'));
add_action('wp_enqueue_scripts', array($this, 'inspect_styles'));
}
/**
* Return absolute file path
* @param string $file
* @return string
*/
public static function file_path($file)
{
return plugin_dir_path(__FILE__) . $file;
}
/** hooking the plugin options/settings
*/
public static function register()
{
register_setting(SLPLUGINOPTIONS_ID . '_options', 'sb_scriptlogic', array('sbScriptLogic', 'load_scripts'));
}
/**
* Manually Load Scripts
* @param array $input
* @return array $input
*/
function load_scripts($input)
{
if (!empty($_POST['loadscripts']))
{
@file_get_contents(get_home_url());
}
return $input;
}
/**
* Usage: hooking (registering) the plugin menu
*/
public static function menu()
{
// Create menu tab
add_options_page(SLPLUGINOPTIONS_NAME . ' Plugin Options', SLPLUGINOPTIONS_NAME, 'manage_options', SLPLUGINOPTIONS_ID . '_options', array('sbScriptLogic', 'options_page'));
}
/**
* Show options/settings form page
*/
public static function options_page()
{
if (!current_user_can('manage_options'))
{
wp_die(__('You do not have sufficient permissions to access this page.'));
}
if (!empty($_POST['loadscripts']))
{
@file_get_contents(get_home_url());
}
$plugin_id = SLPLUGINOPTIONS_ID;
// display options page
include(self::file_path('options.php'));
}
function inspect_styles()
{
if (!is_admin())
{
global $wp_styles;
$option_name = 'sb_registered_styles';
if ($sl_settings = get_option($option_name))
{
$sl_settings = unserialize($sl_settings);
if (is_array($sl_settings->queue) && is_array($wp_styles->queue))
$merged_queue = $sl_settings->queue + $wp_styles->queue;
else
$merged_queue = $wp_styles->queue;
if (is_array($sl_settings->registered) && is_array($wp_styles->registered))
$merged_registered = $sl_settings->registered + $wp_styles->registered;
else
$merged_registered = $wp_styles->registered;
if(is_array($merged_queue))
$wp_styles->queue = array_unique($merged_queue);
else
$wp_styles->queue = array();
$wp_styles->registered = $merged_registered;
// The option already exists, so we just update it.
update_option($option_name, serialize($wp_styles));
} else
{
// The option hasn't been added yet. We'll add it with $autoload set to 'no'.
add_option($option_name, serialize($wp_styles), null, 'no');
}
if (!empty($wp_styles->queue))
{
foreach ($wp_styles->queue as $handle)
{
$sb_scriptlogic_val = get_option('sb_scriptlogic');
if (!empty($sb_scriptlogic_val['style'][$handle]))
{
$scriptLogic = stripslashes(trim($sb_scriptlogic_val['style'][$handle]));
$scriptLogic = apply_filters("script_logic_eval_override", $scriptLogic);
if ($scriptLogic === false)
{
wp_dequeue_style($handle);
continue;
}
if ($scriptLogic === true)
continue;
if (stristr($scriptLogic, "return") === false)
$scriptLogic = "return (" . $scriptLogic . ");";
if (!eval($scriptLogic))
{
wp_dequeue_style($handle);
}
}
}
}
}
}
function inspect_scripts()
{
if (!is_admin())
{
global $wp_scripts;
$option_name = 'sb_registered_scripts';
if ($sl_settings = get_option($option_name))
{
$sl_settings = unserialize($sl_settings);
if (is_array($sl_settings->queue) && is_array($wp_scripts->queue))
$merged_queue = $sl_settings->queue + $wp_scripts->queue;
else
$merged_queue = $wp_scripts->queue;
if (is_array($sl_settings->registered) && is_array($wp_scripts->registered))
$merged_registered = $sl_settings->registered + $wp_scripts->registered;
else
$merged_registered = $wp_scripts->registered;
if(is_array($merged_queue))
$wp_scripts->queue = array_unique($merged_queue);
else
$wp_scripts->queue = array();
$wp_scripts->registered = $merged_registered;
// The option already exists, so we just update it.
update_option($option_name, serialize($wp_scripts));
} else
{
// The option hasn't been added yet. We'll add it with $autoload set to 'no'.
add_option($option_name, serialize($wp_scripts), null, 'no');
}
if (!empty($wp_scripts->queue))
{
foreach ($wp_scripts->queue as $handle)
{
$sb_scriptlogic_val = get_option('sb_scriptlogic');
if (!empty($sb_scriptlogic_val['script'][$handle]))
{
$scriptLogic = stripslashes(trim($sb_scriptlogic_val['script'][$handle]));
$scriptLogic = apply_filters("script_logic_eval_override", $scriptLogic);
if ($scriptLogic === false)
{
wp_dequeue_script($handle);
continue;
}
if ($scriptLogic === true)
continue;
if (stristr($scriptLogic, "return") === false)
$scriptLogic = "return (" . $scriptLogic . ");";
if (!eval($scriptLogic))
{
wp_dequeue_script($handle);
}
}
}
}
}
}
function load_custom_scripts()
{
wp_enqueue_script('jquery-ui-core'); // enqueue jQuery UI Core
wp_enqueue_script('jquery-ui-tabs'); // enqueue jQuery UI Tabs
wp_enqueue_style('script-logic-admin-ui-css', plugins_url('css/jquery-ui.css', __FILE__), false, '0.1', false);
}
/*
* Reload scripts on theme switch
*/
function switch_theme($theme)
{
delete_option('sb_registered_scripts');
delete_option('sb_registered_styles');
return;
}
}
$sl = new sbScriptLogic();
endif;
?>