This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
jch-optimize.php
179 lines (142 loc) · 4.36 KB
/
jch-optimize.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
<?php
/**
* Plugin Name: JCH Optimize
* Plugin URI: http://www.jch-optimize.net/
* Description: This plugin aggregates and minifies CSS and Javascript files for optimized page download
* Version: 1.2.1
* Author: Samuel Marshall
* License: GNU/GPLv3
* Text Domain: jch-optimize
*/
/**
* JCH Optimize - Plugin to aggregate and minify external resources for
* optmized downloads
*
* @author Samuel Marshall <[email protected]>
* @copyright Copyright (c) 2014 Samuel Marshall
* @license GNU/GPLv3, See LICENSE file
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* If LICENSE file missing, see <http://www.gnu.org/licenses/>.
*/
$backend = filter_input(INPUT_GET, 'jchbackend', FILTER_SANITIZE_STRING);
define('_WP_EXEC', '1');
define('JCH_PLUGIN_URL', plugin_dir_url(__FILE__));
define('JCH_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('JCH_CACHE_DIR', WP_CONTENT_DIR . '/cache/jch-optimize/');
if (!defined('JCH_VERSION'))
{
define('JCH_VERSION', '1.2.1');
}
require_once(JCH_PLUGIN_DIR . 'jchoptimize/loader.php');
if (!file_exists(dirname(__FILE__) . '/dir.php'))
{
jch_optimize_activate();
}
if (is_admin())
{
require_once(JCH_PLUGIN_DIR . 'options.php');
}
else
{
if (defined('WP_USE_THEMES')
&& WP_USE_THEMES
&& $backend != 1
&& version_compare(PHP_VERSION, '5.3.0', '>='))
{
add_action('init', 'jch_buffer_start', 0);
add_action('template_redirect', 'jch_buffer_start', 0);
add_action('shutdown', 'jch_buffer_end', -1);
}
}
function jch_load_plugin_textdomain()
{
load_plugin_textdomain('jch-optimize', FALSE, basename(dirname(__FILE__)) . '/languages/');
}
add_action('plugins_loaded', 'jch_load_plugin_textdomain');
function jchoptimize($sHtml)
{
$options = get_option('jch_options');
try
{
$sOptimizedHtml = JchOptimize::optimize($options, $sHtml);
}
catch (Exception $e)
{
JchOptimizeLogger::log($e->getMessage(), JchPlatformSettings::getInstance($options));
$sOptimizedHtml = $sHtml;
}
return $sOptimizedHtml;
}
function jch_buffer_start()
{
ob_start();
}
function jch_buffer_end()
{
$bFlag = FALSE;
while(ob_get_level())
{
ob_end_flush();
$sHtml = ob_get_contents();
if (JchOptimizeHelper::validateHtml($sHtml))
{
$bFlag = TRUE;
break;
}
}
if(ob_get_level())
{
ob_clean();
}
ob_start();
if ($bFlag)
{
echo jchoptimize($sHtml);
}
else
{
echo $sHtml;
}
}
add_filter('plugin_action_links', 'jch_plugin_action_links', 10, 2);
function jch_plugin_action_links($links, $file)
{
static $this_plugin;
if (!$this_plugin)
{
$this_plugin = plugin_basename(__FILE__);
}
if ($file == $this_plugin)
{
$settings_link = '<a href="' . admin_url('options-general.php?page=jchoptimize-settings') . '">' . __('Settings') . '</a>';
array_unshift($links, $settings_link);
}
return $links;
}
function jch_optimize_activate()
{
$wp_filesystem = JchPlatformCache::getWpFileSystem();
$file = dirname(__FILE__) . '/dir.php';
$abspath = ABSPATH;
$code = <<<PHPCODE
<?php
\$DIR = '$abspath';
PHPCODE;
$wp_filesystem->put_contents($file, $code);
}
register_activation_hook(__FILE__, 'jch_optimize_activate');
function jch_optimize_uninstall()
{
delete_option('jch_options');
}
register_uninstall_hook(__FILE__, 'jch_optimize_uninstall');