-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.php
executable file
·444 lines (370 loc) · 20.3 KB
/
admin.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
add_action('admin_menu', 'weever_admin_add_page');
weever_admin_warnings();
/**
* Add the administration pages for Weever Apps. Uses the admin_print_scripts action to ensure
* the javascript/css is only loaded on our configuration screens.
*/
function weever_admin_add_page() {
if ( function_exists('add_menu_page') )
{
// Ensure there are no directory references
$page = ( isset( $_GET['page'] ) ? basename( $_GET['page'] ) : '' );
// Pseudo-page to enable/disable
$mypage = add_submenu_page( '', __( 'Weever App', 'weever' ), __( 'Weever App', 'weever' ), 'manage_options', 'weever-app-toggle', 'weever_admin_page' );
// If this is a weever page, add it as the admin menu item (so it is always highlighted properly between admin page tabs)
if ( substr( $page, 0, strlen('weever-') ) == 'weever-' && file_exists( dirname( __FILE__ ) . '/templates/admin/tabs/' . str_replace( 'weever-', '', $page ) . '.php' ) )
{
$mypage = add_menu_page(__('Weever App', 'weever'), __('Weever App', 'weever'), 'manage_options', $page, 'weever_admin_page', '');
add_action( "admin_print_scripts-$mypage", 'weever_page_scripts_init' );
add_action( "admin_print_styles-$mypage", 'weever_page_styles_init' );
}
else
{
// $mypage = add_submenu_page('', __('Weever Apps Configuration', 'weever'), __('App Features and Navigation', 'weever'), 'manage_options', 'weever-tabs', 'weever_conf');
if ( get_option( 'weever_api_key' ) )
$mypage = add_menu_page(__('Weever App', 'weever'), __('Weever App', 'weever'), 'manage_options', 'weever-list', 'weever_admin_page', '');
else
$mypage = add_menu_page(__('Weever App', 'weever'), __('Weever App', 'weever'), 'manage_options', 'weever-account', 'weever_admin_page', '');
add_action( "admin_print_scripts-$mypage", 'weever_page_scripts_init' );
add_action( "admin_print_styles-$mypage", 'weever_page_styles_init' );
}
}
}
function weever_remove_wp_magic_quotes() {
$_GET = stripslashes_deep( $_GET );
$_POST = stripslashes_deep( $_POST );
$_COOKIE = stripslashes_deep( $_COOKIE );
$_REQUEST = stripslashes_deep( $_REQUEST );
}
/**
* Page controller, loads the wrapper layout and the indivdual page content
*
* TODO: Wrap all tabs into one so they can flip between them sans refresh?
*/
function weever_admin_page() {
// Set the content
// Should never get here without the page get var being set
// TODO: Create a WeeverView class wrapper to pass arbitrary options to the view?
$page = basename( $_GET['page'] );
$content = dirname( __FILE__ ) . '/templates/admin/tabs/' . str_replace( 'weever-', '', $page ) . '.php';
if ( ( function_exists('current_user_can') && current_user_can('manage_options') ) ) {
// Verify this is a valid page
if ( ! file_exists( $content ) )
die( __( 'Invalid page given', 'weever' ) );
// Load the weeverapp object, which fetches the admin page content
try {
$weeverapp = new WeeverApp();
if ( ! $weeverapp->loaded ) {
add_settings_error('weever_settings', 'weever_settings', $weeverapp->error_message . " " . sprintf( __( '<a target="_new" href="%s">Contact Weever Apps support</a>', 'weever' ), 'http://weeverapps.com/support' ) );
} else {
if ( isset( $_GET['page'] ) and 'weever-theme' == $_GET['page'] ) {
$weeverapp->load_theme();
}
}
} catch (Exception $e) {
add_settings_error('weever_settings', 'weever_settings', __( 'Error loading necessary data' ) . " " . sprintf( __( '<a target="_new" href="%s">Contact Weever Apps support</a>', 'weever' ), 'http://weeverapps.com/support' ) );
}
if ( isset( $_GET['weever-app-enabled'] ) ) {
try {
if ( 0 == $_GET['weever-app-enabled'] || 1 == $_GET['weever-app-enabled'] ) {
$weeverapp->app_enabled = ( $_GET['weever-app-enabled'] ? 1 : 0 );
$weeverapp->save();
add_settings_error('weever_config', 'weever_settings', __( 'Weever Apps status updated', 'weever' ), 'updated' );
}
} catch ( Exception $e ) {
add_settings_error('weever_config', 'weever_settings', $e->getMessage() . " " . sprintf( __( '<a target="_new" href="%s">Contact Weever Apps support</a>', 'weever' ), 'http://weeverapps.com/support' ) );
}
}
// Handle form submission
if ( isset( $_POST['submit'] ) || isset( $_POST['stagingmode'] ) ) {
// Remove wordpress slashes
weever_remove_wp_magic_quotes();
switch ( $_GET['page'] ) {
case 'weever-theme':
try {
// Handle any image uploads
$overrides = array( 'test_form' => false );
$images = array( 'tablet_load_live', 'tablet_landscape_load_live', 'phone_load_live', 'icon_live', 'titlebar_logo_live' );
foreach ( $images as $image ) {
if ( array_key_exists( $image, $_FILES ) ) {
$file = wp_handle_upload( $_FILES[$image], $overrides );
if ( isset( $file['error'] ) && 'No file was uploaded.' != $file['error'] ) {
add_settings_error('weever_theme', 'weever_settings', sprintf( __( 'Error uploading file: %s', 'weever' ), $file['error'] ) . ' ' . sprintf( __( '<a target="_new" href="%s">Contact Weever Apps support</a>', 'weever' ), 'http://weeverapps.com/support' ) );
} elseif ( isset( $file['url'] ) ) {
$weeverapp->theme->$image = $file['url'];
add_settings_error('weever_api_key', 'weever_settings', __( 'Theme image updated', 'weever' ), 'updated');
}
}
}
$weeverapp->theme->css = $_POST['css'];
$weeverapp->theme->useCssOverride = ( isset( $_POST['useCssOverride'] ) && $_POST['useCssOverride'] ) ? 1 : 0;
$weeverapp->theme->titlebarSource = $_POST['titlebarSource'];
$weeverapp->theme->titlebarHtml = $_POST['titlebarHtml'];
$weeverapp->theme->template = $_POST['template'];
$weeverapp->launch->install_prompt = $_POST['install_prompt'];
$weeverapp->title = $_POST['title'];
$weeverapp->titlebar_title = $_POST['titlebar_title'];
$weeverapp->save_theme();
$weeverapp->save();
add_settings_error('weever_api_key', 'weever_settings', __( 'Weever Apps theme settings saved', 'weever' ), 'updated');
} catch (Exception $e) {
add_settings_error('weever_theme', 'weever_settings', $e->getMessage() . " " . sprintf( __( '<a target="_new" href="%s">Contact Weever Apps support</a>', 'weever' ), 'http://weeverapps.com/support' ) );
}
break;
case 'weever-account':
try {
$weeverapp->site_key = trim( $_POST['site_key'] );
if ( isset( $_POST['stagingmode'] ) ) {
// Toggle staging mode
$weeverapp->staging_mode = ! $weeverapp->staging_mode;
}
$weeverapp->save();
add_settings_error('weever_api_key', 'weever_settings', __( 'Weever Apps account settings saved', 'weever' ), 'updated');
} catch (Exception $e) {
add_settings_error('weever_api_key', 'weever_settings', $e->getMessage() . " " . sprintf( __( '<a target="_new" href="%s">Contact Weever Apps support</a>', 'weever' ), 'http://weeverapps.com/support' ) );
}
break;
case 'weever-config':
try {
// Save each of the mobile device options
foreach ( $weeverapp->get_device_option_names() as $option ) {
// Sanitize input
$weeverapp->$option = ( isset( $_POST[$option] ) && $_POST[$option] ) ? 1 : 0;
}
// Remaining options
$weeverapp->google_analytics = ( isset( $_POST['google_analytics'] ) ? $_POST['google_analytics'] : '' );
$weeverapp->ecosystem = ( isset( $_POST['ecosystem'] ) ? $_POST['ecosystem'] : '' );
$weeverapp->domain = ( isset( $_POST['domain'] ) ? $_POST['domain'] : '' );
$weeverapp->loadspinner = ( isset( $_POST['loadspinner'] ) ? $_POST['loadspinner'] : '' );
$weeverapp->granular = ( isset( $_POST['granular'] ) && $_POST['granular'] ) ? 1 : 0;
$weeverapp->local = ( isset( $_POST['local'] ) ? $_POST['local'] : '' );
$weeverapp->save();
add_settings_error('weever_config', 'weever_settings', __( 'Weever App settings saved', 'weever' ), 'updated');
} catch (Exception $e) {
add_settings_error('weever_config', 'weever_settings', $e->getMessage() . " " . sprintf( __( '<a target="_new" href="%s">Contact Weever Apps support</a>', 'weever' ), 'http://weeverapps.com/support' ) );
}
break;
}
// Most form control handled via AJAX calls rather than direct post
}
// Check if the domain is different than the current site domain
if ( $weeverapp->loaded && $weeverapp->site_key ) {
if ( ! stripos( site_url(), $weeverapp->primary_domain ) )
add_settings_error('weever_settings', 'weever_settings', sprintf( __( 'Your Weever App subscription key url %s does not match the current Wordpress site url %s - please verify your Wordpress settings, that you have the correct subscription key, or contact support.' ), $weeverapp->primary_domain, site_url() ) . " " . sprintf( __( '<a target="_new" href="%s">Contact Weever Apps support</a>', 'weever' ), 'http://weeverapps.com/support' ) );
}
} else {
die( __( 'Access denied', 'weever' ) );
}
// Include the main content to fire things off
require( dirname( __FILE__) . '/templates/admin/layout.php' );
}
function weever_admin_warnings() {
global $wp_version;
if ( ! get_option( 'weever_api_key' ) && ! isset( $_POST['submit']) && ( function_exists( 'is_multisite' ) || ! version_compare( $wp_version, '3.0', '<' ) ) ) {
function weever_warning() {
echo "
<div id='weever-warning' class='updated'><p><strong>".__('Weever Apps is almost ready.', 'weever')."</strong> ".sprintf(__('You must <a href="%1$s">enter your Weever Apps Subscription Key</a> for it to work. Don\'t have one? <a target="_blank" href="http://weeverapps.com/pricing">Get one here</a>.', 'weever'), "plugins.php?page=weever-account")."</p></div>
";
}
add_action( 'admin_notices', 'weever_warning' );
return;
}
}
function weever_admin_init() {
global $wp_version;
// all admin functions are disabled in old versions
if ( ! function_exists( 'is_multisite' ) && version_compare( $wp_version, '3.0', '<' ) ) {
function weever_version_warning() {
echo "
<div id='weever-warning' class='updated'><p><strong>".sprintf( __( 'Weever Apps %s requires WordPress 3.0 or higher.', 'weever' ), WeeverConst::VERSION ) ."</strong> ".sprintf( __( 'Please <a href="%s">upgrade WordPress</a> to a current version.', 'weever' ), 'http://codex.wordpress.org/Upgrading_WordPress' ). "</p></div>
";
}
add_action( 'admin_notices', 'weever_version_warning' );
return;
}
}
add_action( 'admin_init', 'weever_admin_init' );
/**
* Load styles needed for Weever Apps
*/
function weever_page_styles_init() {
wp_register_style( 'weever.css', WEEVER_PLUGIN_URL . 'static/css/weever.css', array( 'jquery-ui.css', 'jquery-ui-new.css', 'wp-jquery-ui-dialog', 'thickbox' ), WeeverConst::VERSION );
wp_register_style( 'jquery-ui.css', WEEVER_PLUGIN_URL . 'static/css/jquery-ui.css' );
wp_register_style( 'jquery-ui-new.css', WEEVER_PLUGIN_URL . 'static/css/jquery-ui-new.css' );
//wp_register_style( 'jquery-ui-bootstrap.css', WEEVER_PLUGIN_URL . 'static/css/jquery-ui-1.8.16.custom.css', array( 'wp-jquery-ui-dialog' ) );
wp_register_style( 'jquery-impromptu.css', WEEVER_PLUGIN_URL . 'static/css/jquery-impromptu.css' );
wp_enqueue_style( 'jquery-ui.css' );
wp_enqueue_style( 'jquery-ui-new.css' );
wp_enqueue_style( 'jquery-impromptu.css' );
wp_enqueue_style( 'weever.css' );
wp_enqueue_style( 'thickbox' );
}
/**
* Loads scripts needed for Weever Apps
*/
function weever_page_scripts_init() {
wp_register_script( 'jquery-impromptu.js', plugins_url( 'static/js/jquery-impromptu.js', __FILE__ ) );
wp_enqueue_script( 'jquery-impromptu.js' );
wp_register_script( 'jquery-validate.js', plugins_url( 'static/js/jquery.validate.min.js', __FILE__ ), array( 'jquery' ) );
wp_enqueue_script( 'jquery-validate.js' );
wp_register_script( 'weever.js', plugins_url( 'static/js/weever.js', __FILE__ ), array( 'jquery', 'jquery-ui-core', 'jquery-ui-tabs', 'jquery-ui-sortable', 'jquery-ui-dialog' ), WeeverConst::VERSION );
wp_enqueue_script( 'weever.js' );
wp_localize_script( 'weever.js', 'WPText', WeeverHelper::get_js_strings() );
// Page-specific scripts
if ( isset( $_GET['page'] ) )
{
$page = basename( $_GET['page'] );
switch ( $page ) {
case 'weever-account':
wp_register_script( 'weever.account.js', plugins_url( 'static/js/account.js', __FILE__ ), array( 'jquery' ), WeeverConst::VERSION );
wp_enqueue_script( 'weever.account.js' );
break;
case 'weever-list':
wp_register_script( 'weever.list_icons.js', plugins_url( 'static/js/list_icons.js', __FILE__ ), array( 'jquery' ), WeeverConst::VERSION );
wp_enqueue_script( 'weever.list_icons.js' );
wp_register_script( 'weever.list_select.js', plugins_url( 'static/js/list_select.js', __FILE__ ), array( 'jquery' ), WeeverConst::VERSION );
wp_enqueue_script( 'weever.list_select.js' );
wp_register_script( 'weever.list_submit.js', plugins_url( 'static/js/list_submit.js', __FILE__ ), array( 'jquery' ), WeeverConst::VERSION );
wp_enqueue_script( 'weever.list_submit.js' );
wp_register_script( 'weever.list.js', plugins_url( 'static/js/list.js', __FILE__ ), array( 'jquery' ), WeeverConst::VERSION );
wp_enqueue_script( 'weever.list.js' );
break;
case 'weever-theme':
wp_register_script( 'weever.theme.js', plugins_url( 'static/js/theme.js', __FILE__ ), array( 'jquery' ), WeeverConst::VERSION );
wp_enqueue_script( 'weever.theme.js' );
break;
case 'weever-config':
wp_register_script( 'weever.config.js', plugins_url( 'static/js/config.js', __FILE__ ), array( 'jquery' ), WeeverConst::VERSION );
wp_enqueue_script( 'weever.config.js' );
break;
case 'weever-support':
break;
default:
wp_die();
}
}
}
function weever_app_toggle() {
if ( function_exists('current_user_can') && current_user_can('manage_options') && ! isset( $_GET['weever-app-enabled'] ) )
{
$weeverapp = new WeeverApp( false );
if ( $weeverapp->site_key ) {
?>
<div class="wx-app-admin-link-enabled" <?php echo ($weeverapp->app_enabled ? '' : ' style="display:none;" '); ?>>
<?php echo __( 'Weever App Enabled for Mobile Visitors', 'weever' ); ?>
| <a href="<?php echo admin_url( 'admin.php?page=weever-list' ); ?>"><?php echo __( 'Settings', 'weever' ); ?></a>
</div>
<div class="wx-app-admin-link-disabled" <?php echo ($weeverapp->app_enabled ? ' style="display:none;" ' : ''); ?>>
<?php echo __( 'Weever App Disabled for Mobile Visitors', 'weever' ); ?>
| <a href="<?php echo admin_url( 'admin.php?page=weever-list' ); ?>"><?php echo __( 'Settings', 'weever' ); ?></a>
</div>
<?php
}
}
}
add_action( 'admin_notices', 'weever_app_toggle' );
/**
* Add custom fields for post editing, for KML and custom marker url
*/
function weever_add_custom_box() {
add_meta_box('weever_sectionid', __( 'Weever Apps - Map Data', 'myplugin_textdomain' ), 'weever_inner_custom_box', 'post', 'advanced' );
}
add_action('admin_menu', 'weever_add_custom_box');
function weever_inner_custom_box() {
echo '<input type="hidden" id="weever_nonce" name="weever_nonce" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
echo '
<p>
<label for="weever-kml">Display Address:</label>
<input type="text" id="weever-map-address" name="weever-map-address" value="">
</p>
<p>
<label for="weever-kml">KML File URL:</label>
<input type="text" id="weever-kml" name="weever-kml" value="">
</p>
<p>
<label for="weever-kml">Custom Map Marker URL:</label>
<input type="text" id="weever-map-marker" name="weever-map-marker" value="">
</p>
<p>
NOTE: Custom map markers must be PNG image sprites that are 128 pixels by 74 pixels. The image on the left is the normal state, the one on the right is the selected state; each is 64x74 pixels placed beside each other in the same transparent PNG image file.
</p>
';
}
function weever_post_admin_head() {
global $post;
$post_id = $post->ID;
$post_type = $post->post_type;
$zoom = (int) get_option('geolocation_default_zoom');
?>
<script type="text/javascript">
jQuery(function() {
jQuery(document).ready(function() {
jQuery('#weever-map-address').val('<?php echo esc_js(get_post_meta($post_id, 'weever_map_address', true)); ?>');
jQuery('#weever-kml').val('<?php echo esc_js(get_post_meta($post_id, 'weever_kml', true)); ?>');
jQuery('#weever-map-marker').val('<?php echo esc_js(get_post_meta($post_id, 'weever_map_marker', true)); ?>');
});
});
</script>
<?php
}
add_action('admin_head-post-new.php', 'weever_post_admin_head');
add_action('admin_head-post.php', 'weever_post_admin_head');
function weever_save_postdata($post_id) {
// Check authorization, permissions, autosave, etc
if ( ! isset( $_POST['weever_nonce'] ) or ! wp_verify_nonce( $_POST['weever_nonce'], plugin_basename( __FILE__ ) ) )
return $post_id;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
if( 'page' == $_POST['post_type'] ) {
if( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
update_post_meta($post_id, 'weever_map_address', $_POST['weever-map-address']);
update_post_meta($post_id, 'weever_kml', $_POST['weever-kml']);
update_post_meta($post_id, 'weever_map_marker', $_POST['weever-map-marker']);
return $post_id;
}
add_action('save_post', 'weever_save_postdata');
function weever_section_text() {
echo '<p></p>';
}
function weever_api_key_string() {
$weever_api_key = get_option( 'weever_api_key' );
echo "<input id='weever_api_key' name='weever_api_key' size='40' type='text' value='$weever_api_key' />";
}
function weever_api_key_validate($weever_api_key) {
$weever_api_key = trim($weever_api_key);
$stage_url = "";
$postdata = http_build_query(
array(
'stage' => $stage_url,
'app' => 'json',
'site_key' => $weever_api_key,
'm' => "tab_sync",
'version' => WeeverConst::VERSION,
'generator' => WeeverConst::NAME
)
);
$result = wp_remote_get( WeeverConst::LIVE_SERVER."?".$postdata );
if ( is_array( $result ) and isset( $result['body'] ) )
{
$state = json_decode($result['body']);
if ( "Site key missing or invalid." == $result['body'] )
{
$weever_api_key = '';
add_settings_error('weever_api_key', 'weever_settings', 'Invalid Weever API key');
} else {
add_settings_error('weever_api_key', 'weever_settings', "Weever Apps API key saved and updated - ".$state->results->config->primary_domain, 'updated');
}
}
else
{
add_settings_error('weever_api_key', 'weever_settings', 'Weever API key could not be verified');
}
return $weever_api_key;
}