forked from ConnectThink/WP-SCSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-scss.php
241 lines (198 loc) · 6.84 KB
/
wp-scss.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
<?php
/**
* Plugin Name: WP-SCSS
* Plugin URI: https://github.com/ConnectThink/WP-SCSS
* Description: Compiles scss files live on wordpress.
* Version: 1.1.3
* Author: Connect Think
* Author URI: http://connectthink.com
* License: GPLv3
*/
/**
* Plugin Workflow
* 1. Create plugin global variables
* 2. Require dependancies
* a. scssphp - does scss compiling using php (vendor)
* b. wp-scss class - manages compiling
* c. options class - builds settings page
* 3. Registering Settings Page and Options
* 4. Assign plugin settings
* 5. Instantiate wp_scss object and run compiler
* 6. Handle Errors
* 7. Enqueue Styles
*/
/*
* 1. PLUGIN GLOBAL VARIABLES
*/
// Plugin Paths
if (!defined('WPSCSS_THEME_DIR'))
define('WPSCSS_THEME_DIR', get_stylesheet_directory());
if (!defined('WPSCSS_PLUGIN_NAME'))
define('WPSCSS_PLUGIN_NAME', trim(dirname(plugin_basename(__FILE__)), '/'));
if (!defined('WPSCSS_PLUGIN_DIR'))
define('WPSCSS_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . WPSCSS_PLUGIN_NAME);
if (!defined('WPSCSS_PLUGIN_URL'))
define('WPSCSS_PLUGIN_URL', WP_PLUGIN_URL . '/' . WPSCSS_PLUGIN_NAME);
// Plugin Version
if (!defined('WPSCSS_VERSION_KEY'))
define('WPSCSS_VERSION_KEY', 'wpscss_version');
if (!defined('WPSCSS_VERSION_NUM'))
define('WPSCSS_VERSION_NUM', '1.1.3');
// Add version to options table
add_option(WPSCSS_VERSION_KEY, WPSCSS_VERSION_NUM);
/*
* 2. REQUIRE DEPENDANCIES
*
* scssphp - scss compiler
* class-wp-scss
* options.php - settings for plugin page
*/
include_once WPSCSS_PLUGIN_DIR . '/scssphp/scss.inc.php'; // Sass Compiler (vendor)
include_once WPSCSS_PLUGIN_DIR . '/class/class-wp-scss.php'; // Compiling Manager
include_once WPSCSS_PLUGIN_DIR . '/options.php'; // Options page class
/**
* 3. REGISTER SETTINGS
*
* Instantiate Options Page
* Create link on plugin page to settings page
*/
if( is_admin() ) {
$wpscss_settings = new Wp_Scss_Settings();
}
add_filter('plugin_action_links', 'wpscss_plugin_action_links', 10, 2);
function wpscss_plugin_action_links($links, $file) {
static $this_plugin;
if( !$this_plugin ) {
$this_plugin = plugin_basename(__FILE__);
}
if ($file == $this_plugin) {
$settings_link = '<a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php?page=wpscss_options">Settings</a>';
array_unshift($links, $settings_link);
}
return $links;
}
/**
* 4. PLUGIN SETTINGS
*
* Pull settings from options table
* Scrub empty fields or directories that don't exists
* Assign settings via settings array to pass to object
*/
$wpscss_options = get_option( 'wpscss_options' );
$scss_dir_setting = $wpscss_options['scss_dir'];
$css_dir_setting = $wpscss_options['css_dir'];
// Checks if directories are empty
if( $scss_dir_setting == false || $css_dir_setting == false ) {
function wpscss_settings_error() {
echo '<div class="error">
<p><strong>Wp-Scss</strong> requires both directories be specified. <a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php?page=wpscss_options">Please update your settings.</a></p>
</div>';
}
add_action('admin_notices', 'wpscss_settings_error');
return 0; //exits
// Checks if directory exists
} elseif ( !file_exists(WPSCSS_THEME_DIR . $scss_dir_setting) || !file_exists(WPSCSS_THEME_DIR . $css_dir_setting) ) {
function wpscss_settings_error() {
echo '<div class="error">
<p><strong>Wp-Scss:</strong> One or more specified directories does not exist. Please create the directories or <a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php?page=wpscss_options">update your settings.</a></p>
</div>';
}
add_action('admin_notices', 'wpscss_settings_error');
return 0; //exits
}
// Plugin Settings
$wpscss_settings = array(
'scss_dir' => WPSCSS_THEME_DIR . $scss_dir_setting,
'css_dir' => WPSCSS_THEME_DIR . $css_dir_setting,
'compiling' => $wpscss_options['compiling_options'],
'errors' => $wpscss_options['errors'],
'enqueue' => $wpscss_options['enqueue']
);
/**
* 5. INSTANTIATE & EXECUTE COMPILER
*
* Passes settings to the object
* If needs_compiling passes, runs compile method
*/
$wpscss_compiler = new Wp_Scss(
$wpscss_settings['scss_dir'],
$wpscss_settings['css_dir'],
$wpscss_settings['compiling']
);
if ( $wpscss_compiler->needs_compiling() ) {
$wpscss_compiler->compile();
}
/**
* 6. HANDLE COMPILING ERRORS
*
* First block handles print errors to front end.
* This adds a small style block the header to help errors get noticed
*
* Second block handles print errors to log file.
* After the file gets over 1MB it does a purge and deletes the first
* half of entries in the file.
*/
$log_file = $wpscss_compiler->scss_dir.'error_log.log';
if ( !is_admin() && $wpscss_settings['errors'] === 'show' && count($wpscss_compiler->compile_errors) > 0) {
echo '<div class="scss_errors"><pre>';
echo '<h6 style="margin: 15px 0;">Sass Compiling Error</h6>';
foreach( $wpscss_compiler->compile_errors as $error) {
echo '<p class="sass_error">';
echo '<strong>'. $error['file'] .'</strong> <br/><em>"'. $error['message'] .'"</em>';
echo '<p class="sass_error">';
}
echo '</pre></div>';
function wpscss_error_styles() {
echo
'<style>
.scss_errors {
position: fixed;
top: 0px;
z-index: 99999;
width: 100%;
}
.scss_errors pre {
background: #f5f5f5;
border-left: 5px solid #DD3D36;
box-shadow: 0 2px 3px rgba(51,51,51, .4);
color: #666;
font-family: monospace;
font-size: 14px;
margin: 20px 0;
overflow: auto;
padding: 20px;
white-space: pre;
white-space: pre-wrap;
word-wrap: break-word;
}
</style>';
}
add_action('wp_print_styles', 'wpscss_error_styles');
} else { // Hide errors and print them to a log file.
foreach ($wpscss_compiler->compile_errors as $error) {
$error_string = date('m/d/y g:i:s', time()) .': ';
$error_string .= $error['file'] .' - '. $error['message'] . PHP_EOL;
file_put_contents($log_file, $error_string, FILE_APPEND);
$error_string = "";
}
}
// Clean out log file if it get's too large
if ( file_exists($log_file) ) {
if ( filesize($log_file) > 1000000) {
$log_contents = file_get_contents($log_file);
$log_arr = explode("\n", $log_contents);
$new_contents_arr = array_slice($log_arr, count($log_arr)/2);
$new_contents = implode(PHP_EOL, $new_contents_arr) . 'LOG FILE CLEANED ' . date('n/j/y g:i:s', time());
file_put_contents($log_file, $new_contents);
}
}
/**
* 7. ENQUEUE STYLES
*/
if ( $wpscss_settings['enqueue'] == '1' ) {
function wpscss_enqueue_styles() {
global $wpscss_compiler, $wpscss_options;
$wpscss_compiler->enqueue_files($wpscss_options['css_dir']);
}
add_action('wp_enqueue_scripts', 'wpscss_enqueue_styles', 50);
}