-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgust.php
204 lines (188 loc) · 8.64 KB
/
gust.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
<?php
/*
Plugin Name: Gust
Plugin URI: https://github.com/ideag/gust
Description: A port of the Ghost admin interface
Author: Arūnas Liuiza
Version: 0.4.1
Author URI: http://arunas.co/
*/
define ('GUST_SUBPATH', gust_get_subpath());
define ('GUST_TITLE', __('Gust','gust'));
define ('GUST_VERSION', 'v0.4.1');
define ('GUST_PLUGIN_PATH', plugin_dir_path(__FILE__));
define ('GUST_PLUGIN_URL', plugin_dir_url(__FILE__));
// === ACTIVATION
register_activation_hook(__FILE__,'gust_install');
function gust_install(){
gust_init_rewrites();
flush_rewrite_rules();
gust_permalink_check();
}
// === FILTERS/ACTIONS
// init options
require_once('gust.class.php');
add_action('init', array( 'Gust', 'init_options' ), 100 );
// init rewrites
add_action('init','gust_init_rewrites', 101);
add_action('pre_get_posts','gust_drop_in',1);
// monitor for permalink changes
add_action('admin_init','gust_permalink_check');
// filter WordPres Admin Bar
add_action( 'admin_bar_menu', 'gust_admin_bar_filter', 999 );
// filter "Edit post" link in frontend/Admin bar
add_filter('get_edit_post_link','gust_edit_post_link',10,3);
add_filter('post_row_actions', 'gust_edit_post_link_admin', 10, 2);
add_filter('page_row_actions', 'gust_edit_post_link_admin', 10, 2);
function gust_permalink_check(){
if (!gust_is_pretty_permalinks()) {
add_action( 'admin_notices', 'gust_no_permalink_notice',1000 );
}
}
function gust_no_permalink_notice() {
?>
<div class="error">
<p><?php _e('Gust: You do not use pretty permalinks. Please enable them <a href="options-permalink.php">here</a> to use Gust.', 'gust' ); ?></p>
</div>
<?php
}
function gust_is_pretty_permalinks(){
global $wp_rewrite;
if ($wp_rewrite->permalink_structure == '')
return false;
else
return true;
}
function gust_init_rewrites() {
add_rewrite_tag( '%gust_api%', '(ghost|'.GUST_NAME.'|api)');
add_rewrite_tag( '%gust_q%', '(.*)');
add_permastruct('gust_calls', '%gust_api%/%gust_q%',array('with_front'=>false));
}
function gust_drop_in($q) {
if ((get_query_var('gust_api')=='ghost'||get_query_var('gust_api')==GUST_NAME||get_query_var('gust_api')=='api' )&& $q->is_main_query()) {
define('WP_ADMIN',true);
require_once(GUST_PLUGIN_PATH.'/assets/dispatch/dispatch.php');
D::config('dispatch.views', GUST_PLUGIN_PATH.'views');
D::config('dispatch.layout', false);
D::config('dispatch.url', get_bloginfo('url'));
$posttypes = implode('|',array_keys(Gust::$options['main_posttypes']));
$taxonomies = implode('|',get_taxonomies(array('show_ui'=>true)));
if (get_query_var('gust_api')=='api' && $q->is_main_query()) {
require_once('gust-api.php');
D::on('POST', '/'.GUST_API_ROOT.'/session', array('Gust_API', 'login'));
D::on('POST', '/'.GUST_API_ROOT.'/password', array('Gust_API', 'forgotten'));
D::on('GET', '/'.GUST_API_ROOT.'/posts', array('Gust_API', 'posts'));
D::on('GET', '/'.GUST_API_ROOT.'/post(/:id@[0-9]+)', array('Gust_API', 'post'));
D::on('POST', '/'.GUST_API_ROOT.'/post(/:id@[0-9]+)', array('Gust_API', 'post_save'));
D::on('DELETE','/'.GUST_API_ROOT.'/post(/:id@[0-9]+)', array('Gust_API', 'post_delete'));
D::on('GET', '/'.GUST_API_ROOT.'/metakeys', array('Gust_API', 'get_meta_keys'));
D::on('GET', '/'.GUST_API_ROOT.'/post/:id@[0-9]+/meta', array('Gust_API', 'post_meta_list'));
D::on('POST', '/'.GUST_API_ROOT.'/post/:id@[0-9]+/meta', array('Gust_API', 'post_meta_update'));
D::on('DELETE','/'.GUST_API_ROOT.'/post/:id@[0-9]+/meta', array('Gust_API', 'post_meta_delete'));
D::on('GET', '/'.GUST_API_ROOT.'/autosave/:id@[0-9]+', array('Gust_API', 'autosave_get'));
D::on('POST', '/'.GUST_API_ROOT.'/autosave/:id@[0-9]+', array('Gust_API', 'autosave'));
D::on('POST', '/'.GUST_API_ROOT.'/upload/:id@[0-9]+', array('Gust_API', 'upload'));
D::on('DELETE','/'.GUST_API_ROOT.'/upload', array('Gust_API', 'upload_delete'));
D::on('GET', '/'.GUST_API_ROOT.'/post/:id@[0-9]+/image', array('Gust_API', 'post_image_list'));
D::on('POST', '/'.GUST_API_ROOT.'/post/:id@[0-9]+/image', array('Gust_API', 'post_image_add'));
D::on('DELETE','/'.GUST_API_ROOT.'/post/:id@[0-9]+/image', array('Gust_API', 'post_image_delete'));
D::on('POST', '/'.GUST_API_ROOT.'/post/:id@[0-9]+/featured', array('Gust_API', 'post_set_featured'));
D::on('GET', '/'.GUST_API_ROOT.'/:type@'.$taxonomies, array('Gust_API', 'tax'));
D::on('POST', '/'.GUST_API_ROOT.'/:type@'.$taxonomies, array('Gust_API', 'tax_add'));
D::on('GET', '/'.GUST_API_ROOT.'/:type@'.$taxonomies.'/:id@[0-9]+', array('Gust_API', 'tax_single'));
D::on('POST', '/'.GUST_API_ROOT.'/:type@'.$taxonomies.'/:id@[0-9]+', array('Gust_API', 'tax_update'));
D::on('DELETE','/'.GUST_API_ROOT.'/:type@'.$taxonomies.'/:id@[0-9]+', array('Gust_API', 'tax_delete'));
} else if (
(get_query_var('gust_api')==GUST_NAME || get_query_var('gust_api')=='ghost')
&&
($q->is_main_query())
) {
require_once('gust-views.php');
D::on('GET', '/ghost(/:q@.*)', array('Gust_views', 'ghost'));
D::on('GET', '/'.GUST_NAME, array('Gust_views', 'root'));
D::on('GET', '/'.GUST_NAME.'/login', array('Gust_views', 'login'));
D::on('GET', '/'.GUST_NAME.'/signout', array('Gust_views', 'signout'));
D::on('GET', '/'.GUST_NAME.'/forgotten', array('Gust_views', 'forgotten'));
D::on('GET', '/'.GUST_NAME.'/:type@'.$posttypes, array('Gust_views', 'post_type'));
D::on('GET', '/'.GUST_NAME.'/editor', array('Gust_views', 'editor_default'));
D::on('GET', '/'.GUST_NAME.'/editor/:type@'.$posttypes,array('Gust_views', 'editor_new'));
D::on('GET', '/'.GUST_NAME.'/editor/:id@[0-9]+', array('Gust_views', 'editor'));
D::on('POST', '/'.GUST_NAME.'/coffee', array('Gust', 'paypal_submit'));
D::on('*', '/'.GUST_NAME.'/coffee/confirm', array('Gust_views', 'coffee_confirm'));
}
D::dispatch();
die('');
}
}
function gust_admin_bar_filter( $wp_admin_bar ) {
$logo = $wp_admin_bar->get_node('wp-logo');
$logo->href = get_bloginfo('url').'/'.GUST_NAME.'/';
$wp_admin_bar->add_node($logo);
if (!is_admin()) {
$name = $wp_admin_bar->get_node('site-name');
$name->href = get_bloginfo('url').'/'.GUST_NAME.'/';
$wp_admin_bar->add_node($name);
$nodes = $wp_admin_bar->get_nodes();
$args = array(
'id' => 'gust',
'title' => __('Gust Dashboard','gust'),
'href' => get_bloginfo('url').'/'.GUST_NAME.'/',
'meta' => array( 'class' => 'gust' ),
'parent'=> 'site-name'
);
$wp_admin_bar->add_node( $args );
if (is_array($nodes)) foreach ($nodes as $node) {
if ($node->id == 'dashboard') {
$node->title = __('WP Dasboard','gust');
}
$wp_admin_bar->remove_node($node->id);
$wp_admin_bar->add_node($node);
}
}
}
function gust_edit_post_link($link, $post_id, $context) {
if (!is_admin()) {
$link = get_bloginfo('url').'/'.GUST_NAME.'/editor/'.$post_id;
}
return $link;
}
function gust_edit_post_link_admin($actions, $post) {
$posttypes = array_keys(Gust::$options['main_posttypes']);
if (in_array($post->post_type,$posttypes)) {
$actions['gust_edit'] = '<a href="'. get_bloginfo('url').'/'.GUST_NAME.'/editor/'.$post->ID . '" class="gust-edit">' . __('Edit in Gust','gust') . '</a>';
}
return $actions;
}
/*
function gust_uuid_post($post_id) {
$uuid = get_post_meta($post_id,'_uuid',true);
if (!$uuid) {
$uuid = gust_gen_uuid();
update_post_meta( $post_id, '_uuid', $uuid );
}
return $uuid;
}
function gust_gen_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
*/
function gust_get_subpath(){
$url = get_bloginfo('url');
$url = parse_url($url);
$url = isset($url['path'])?$url['path']:'';
return $url;
}