-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreloadr-for-wordpress.php
More file actions
149 lines (130 loc) · 4.36 KB
/
reloadr-for-wordpress.php
File metadata and controls
149 lines (130 loc) · 4.36 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
<?php
/*
Plugin Name: Reloadr for WordPress
Version: 0.2.1
Description: A plugin based on "Reloadr" which watches web project files for change, and refreshes their page automatically. This is good for client-side assets(e.g. *.css) and server-side assets(e.g. *.php). Awesome scripts "Reloadr" were made by Daniel Bergey(https://github.com/dbergey).
Author: Tecking
Author URI: http://www.tecking.org/
License: GPLv2
*/
/* Copyright 2013 Tecking (email : tecking@tecking.org)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* RFW class
*/
class RFW {
public $option;
public function __construct( $option ) {
$this->option = $option;
}
public function get_path() {
$stylesheet_dir = get_stylesheet_directory();
if ( DIRECTORY_SEPARATOR === '\\' ) $stylesheet_dir = str_replace( '\\', '/', $stylesheet_dir );
$key = array(
'client' => 'rfw_client_assets',
'server' => 'rfw_server_assets'
);
$prefix = array(
'client' => get_stylesheet_directory_uri(),
'server' => $stylesheet_dir
);
$default = array(
'client' => '"' . $prefix{$this->option} . '/style.css"',
'server' => '"' . $prefix{$this->option} . '/*.php"'
);
$args = get_option( $key{$this->option} );
$path = null;
if ( !empty( $args ) ) {
$args = explode( ',', get_option( $key{$this->option} ) );
if ( is_array( $args ) ) {
foreach ( $args as $value ) {
$value = trim( $value );
$path .= ', "' . $prefix{$this->option} . esc_attr( $value ) . '"';
}
}
else {
$args = trim( $args );
$path .= ', "' . $prefix{$this->option} . esc_attr( $args ) . '"';
}
}
return $default{$this->option} . $path;
}
}
/*
* Place code in the head section
*/
add_action( 'wp_head', 'reloadr_for_wordpress' );
function reloadr_for_wordpress() {
$client = new RFW( 'client' );
$server = new RFW( 'server' );
$str = '<script type="text/javascript" src="' . plugin_dir_url( __FILE__ ) . 'Reloadr/reloadr.js"></script>';
$str .= '
<script>
Reloadr.go({
client: [
' . $client->get_path() . '
],
server: [
' . $server->get_path() . '
],
path: "' . plugin_dir_url( __FILE__ ) . 'Reloadr/reloadr.php"
});
</script>
';
echo $str;
}
/*
* Remove keys if RFW is uninstalled
*/
if ( function_exists( 'register_uninstall_hook' ) ) register_uninstall_hook( __FILE__, 'rfw_uninstall_hook' );
function rfw_uninstall_hook() {
delete_option( 'rfw_client_assets' );
delete_option( 'rfw_server_assets' );
}
/*
* Admin menu
*/
add_action( 'admin_menu', 'rfw_admin_menu' );
function rfw_admin_menu() {
add_options_page(
'Reloadr',
'Reloadr',
'manage_options',
'rfw_settings',
'rfw_settings'
);
}
function rfw_settings() { ?>
<div class="wrap">
<h2>Reloadr for WordPress</h2>
<form action="options.php" method="post">
<?php wp_nonce_field( 'update-options' ); ?>
<p>Separate values with commas.</p>
<table class="form-table">
<tr>
<th>Client-side Assets</th>
<td><input type="text" name="rfw_client_assets" value="<?php echo esc_attr( get_option( 'rfw_client_assets' ) ); ?>"><br>
e.g. /your_css_directory/style.css (default: /style.css)</td>
</tr>
<tr>
<th>Server-side Assets</th>
<td><input type="text" name="rfw_server_assets" value="<?php echo esc_attr( get_option( 'rfw_server_assets' ) ); ?>"><br>
Wildcard is available.<br />e.g. /your_includes_directory/*.php (default: /*.php)</td>
</tr>
</table>
<input type="hidden" name="action" value="update">
<input type="hidden" name="page_options" value="rfw_client_assets,rfw_server_assets">
<p class="submit"><input type="submit" class="button-primary" value="Save Changes"></p>
</form>
</div>
<?php }