forked from crowdfavorite/wp-capsule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
1651 lines (1524 loc) · 54.4 KB
/
functions.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
include('ui/functions.php');
$cap_client = new Capsule_Client;
$cap_client->add_actions();
// Class here is mostly for namespacing of methods and properties
class Capsule_Client {
var $post_type_prefix = '_cc_';
var $server_term_id_key = '_cap_server_term_id';
var $server_term_tax_key = '_cap_server_term_tax';
var $server_term_slug_key = '_cap_server_term_slug';
var $server_api_key = '_cap_server_api_key';
var $server_url_key = '_cap_server_url';
function __construct() {
// Used for debugging, add this constant to your local-config or wp-config.php to override
// Shows hidden taxonomies and post types.
@define('CAP_CLIENT_DEBUG', false);
}
function add_actions() {
// Come in after post types and taxonomies registered
add_action('wp_loaded', array($this, 'request_handler'));
// Priority 11 to come after taxonomy registration
add_action('init', array($this, 'register_post_types'), 11);
add_action('admin_menu', array($this, 'add_menu_pages'));
add_action('wp_insert_post', array($this, 'insert_post'), 10, 2);
add_action('admin_notices', array($this, 'capsule_admin_notice'));
add_action('wp_footer', array($this, 'wp_cron'));
}
public function wp_cron() {
if (!wp_next_scheduled('capsule_queue')) {
wp_schedule_event(time(), 'hourly', 'capsule_queue');
}
add_action('capsule_queue', 'capsule_queue_run');
}
// Handles all client actions including those coming in via ajax
public function request_handler() {
if (isset($_REQUEST['capsule_client_action'])) {
switch ($_REQUEST['capsule_client_action']) {
case 'add_server':
if (wp_verify_nonce($_POST['_server_nonce'], '_cap_client_server_management')) {
$server_data = array(
'server_name' => isset($_POST['server_name']) ? $_POST['server_name'] : '',
'api_key' => isset($_POST['server_api_key']) ? $_POST['server_api_key'] : '',
'server_url' => isset($_POST['server_url']) ? $_POST['server_url'] : '',
);
$post = $this->add_server($server_data);
}
break;
case 'add_server_ajax':
$errors = array();
$server_data = array(
'server_name' => isset($_POST['server_name']) ? $_POST['server_name'] : '',
'api_key' => isset($_POST['server_api_key']) ? $_POST['server_api_key'] : '',
'server_url' => isset($_POST['server_url']) ? $_POST['server_url'] : '',
);
if (wp_verify_nonce($_POST['_server_nonce'], '_cap_client_server_management')) {
$test_errors = $this->test_credentials($server_data['api_key'], $server_data['server_url']);
$duplicate_errors = $this->duplicate_server_check($server_data['server_name'], $server_data['server_url']);
$validation_errors = array_merge($test_errors, $duplicate_errors);
if (empty($validation_errors)) {
$post = $this->add_server($server_data);
echo json_encode(array(
'result' => 'success',
'html' => $this->server_row_markup($post, ''),
));
die();
}
else {
$errors = $validation_errors;
}
}
else {
$errors[] = array(
'message' => __('Could not save server data', 'capsule'),
'type' => 'general',
);
}
$results = array(
'result' => 'error',
'errors' => $errors,
'data' => array(
'name' => $server_data['server_name'],
'api_key' => $server_data['api_key'],
'url' => $server_data['server_url'],
)
);
// Something didn't go quite right
echo json_encode($results);
die();
break;
// Delete server handled slightly differently, with a link and GET params
case 'delete_server':
if (wp_verify_nonce($_REQUEST['_wpnonce'], '_cap_client_delete_server')) {
$result = $this->delete_server($_GET['server_id']);
if (isset($_GET['doing_ajax'])) {
if ( $result !== false) {
echo json_encode(array('result' => 'success'));
}
else {
echo json_encode(array('result' => 'error'));
}
die();
}
}
break;
case 'update_server_ajax':
$errors = array();
$data['server_name'] = isset($_POST['server_name']) ? $_POST['server_name'] : '';
$data['api_key'] = isset($_POST['api_key']) ? $_POST['api_key'] : '';
$data['server_url'] = isset($_POST['server_url']) ? $_POST['server_url'] : '';
if (wp_verify_nonce($_POST['_server_nonce'], '_cap_client_server_management')) {
if ($server_id = $_POST['server_id']) {
$test_errors = $this->test_credentials($data['api_key'], $data['server_url']);
$duplicate_errors = $this->duplicate_server_check($data['server_name'], $data['server_url'], $server_id);
$validation_errors = array_merge($test_errors, $duplicate_errors);
if (empty($validation_errors)) {
if ($server = $this->update_server($server_id, $data)) {
echo json_encode(array(
'data' => array(
'name' => $server->post_title,
'api_key' => $server->api_key,
'url' => $server->url,
),
'result' => 'success'
));
die();
}
else {
$errors[] = array(
'message' => __('Could not save server.', 'capsule'),
'type' => 'genereal',
);
}
}
else {
$errors = $validation_errors;
}
}
else {
$errors[] = array(
'message' => __('No server ID found.', 'capsule'),
'type' => 'genereal',
);
}
}
echo json_encode(array(
'result' => 'error',
'data' => array(
'name' => $data['server_name'],
'api_key' => $data['api_key'],
'url' => $data['server_url'],
),
'errors' => $errors,
));
die();
break;
// Non ajax way, have to update all servers
case 'update_servers':
if (wp_verify_nonce($_POST['_server_nonce'], '_cap_client_server_management')) {
$servers = isset($_POST['servers']) ? $_POST['servers'] : array();
if (!empty($servers)) {
foreach ($servers as $server_id => $server_data) {
$this->update_server($server_id, $server_data);
}
}
}
break;
case 'save_mapping':
if (wp_verify_nonce($_POST['_save_mapping_nonce'], '_cap_client_save_mapping')) {
if (!empty($_POST['cap_client_mapping'])) {
$this->save_mapping($_POST['cap_client_mapping']);
}
}
break;
case 'another_project_mapping_ajax':
if (isset($_POST['post_id']) && isset($_POST['taxonomy'])) {
$taxonomy = $_POST['taxonomy'];
$post_id = $_POST['post_id'];
$post = get_post($post_id);
$terms = $this->get_taxonomy_terms($taxonomy);
echo $this->term_select_markup($post, $taxonomy, $terms, 0);
}
die();
break;
default:
break;
}
}
}
/**
* Return all terms in a group of taxonomies
*
* @param string|array $taxonomies Taxonomy name or list of Taxonomy names
* @return array Array of term objects
**/
function get_taxonomy_terms($taxonomies) {
return get_terms($taxonomies, array(
'hide_empty' => false,
'orderby' => 'slug',
'order' => 'ASC',
));
}
/**
* Get a list of servers
* Returns an array of servers with the key as the post type slug:
* array(
* 'server-1' => array(
* 'api-key' => '12345abc',
* 'url' => 'http://capsule-server-1.com';
* ),
* 'server-2' => array(
* 'api-key' => 'A4d*DYnohiO',
* 'url' => 'http://capsule-server-2.com';
* )
* )
*
* Potential feature - Filter servers based on owner for multi-user support
**/
function get_servers() {
$query = new WP_Query(array(
'post_type' => 'server',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'name'
));
$servers = array();
if ($query->have_posts()) {
foreach ($query->posts as $post) {
$servers[] = $this->process_server($post);
}
}
return $servers;
}
function process_server($server) {
if (!is_object($server)) {
$server = get_post($server);
}
$api_key = get_post_meta($server->ID, $this->server_api_key, true);
$url = get_post_meta($server->ID, $this->server_url_key, true);
$server->api_key = $api_key;
$server->url = $url;
return $server;
}
/**
* Get the post type name thats generated from a post name
* Note this does not return the post type of the post, but
* the post type which was generated from/for the post
*
*/
public function post_type_slug($post_name) {
// 20 Character limit on post type names
return substr(sha1($this->post_type_prefix.$post_name), 0, 20);
}
/**
* Registers required post types. These include the server post type
* and one post type for each server which stores the term mappings
**/
public function register_post_types() {
// Register the server type
$default_args = array(
'public' => CAP_CLIENT_DEBUG,
'publicly_queryable' => true,
'show_ui' => false,
'show_in_menu' => false,
'query_var' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
);
$args = $default_args;
$args['label'] = __('Servers', 'capsule');
register_post_type('server', $args);
$servers = $this->get_servers();
$args = array_merge($default_args, array(
'taxonomies' => $this->taxonomies_to_map(),
));
// Generate post types for each of the servers, this is where the server
// terms are stored. Must have unique, reproducable names.
foreach ($servers as $server_post) {
$args['label'] = $server_post->post_title;
register_post_type($this->post_type_slug($server_post->post_name), $args);
}
}
/**
* Get a list of taxonomies to pull from the capsule server and map local
* terms to.
*
* Filterable with 'capsule_client_taxonomies_to_map'
*
* @return array Array of taxonomy slugs
**/
public function taxonomies_to_map() {
$taxonomies = array(
'projects',
);
return apply_filters('capsule_client_taxonomies_to_map', $taxonomies);
}
// Add menu pages
public function add_menu_pages() {
global $menu;
$menu['3'] = array( '', 'read', 'separator-capsule', '', 'wp-menu-separator' );
add_menu_page(__('Capsule', 'capsule_client'), __('Capsule', 'capsule'), 'read', 'capsule', array($this, 'capsule_help'), '', '3.1' );
// needed to make separator show up
ksort($menu);
add_submenu_page('capsule', __('Servers', 'capsule_client'), __('Servers', 'capsule'), 'manage_options', 'capsule-servers', array($this, 'server_management_page'));
add_submenu_page('capsule', __('Projects', 'capsule_client'), __('Projects', 'capsule'), 'manage_options', 'capsule-projects', array($this, 'term_mapping_page'));
}
// Menu page
public function capsule_help() {
?>
<style type="text/css">
.capsule-welcome {
background: #222;
color: #fff;
width: 100%;
}
.capsule-welcome h1 {
font-weight: normal;
line-height: 100%;
margin: 0;
padding: 15px 0 10px 15px;
}
.capsule-welcome p {
font-weight: normal;
line-height: 100%;
margin: 0;
padding: 0 0 15px 15px;
}
.capsule-admin h3 {
margin-top: 25px;
}
.capsule-admin hr {
border: 0;
border-top: 1px solid #222;
clear: both;
margin: 0 100px 10px;
}
.capsule-screenshot {
border: 1px solid #ccc;
padding: 2px;
width: 90%;
}
.capsule-doc-col-left {
clear: left;
float: left;
margin-right: 30px;
margin-bottom: 30px;
max-width: 500px;
width: 45%;
}
.capsule-doc-col-right {
clear: right;
float: left;
margin-bottom: 30px;
max-width: 500px;
width: 45%;
}
</style>
<div class="wrap capsule-admin">
<div class="capsule-welcome">
<h1><?php _e('Capsule', 'capsule'); ?></h1>
<p><?php _e('The developer\'s code journal', 'capsule'); ?></p>
</div>
<img src="<?php echo get_template_directory_uri(); ?>/docs/hero.jpg" style="width: 100%;" alt="" />
<div class="capsule-doc-col-left">
<h3><?php _e('Overview', 'capsule'); ?></h3>
<p><?php _e('Many developers keep a scratch document open next to their project code or IDE when they are coding. This document ends up containing miscellaneous artifacts: failed code attempts, data formats, math calculations, etc. Most of the time, this document gets thrown away.', 'capsule'); ?></p>
<p><?php _e('Capsule is a replacement for that scratch document. It archives and organizes your development artifacts for future reference.', 'capsule'); ?></p>
<p><?php _e('We have intentionally designed Capsule so that you you can stay on the front-end of the app for everything except administrative tasks (adding Capsule Servers, mapping projects, etc.).', 'capsule'); ?></p>
<p><?php printf(__('Can\'t wait to get started? <a href="%s">Post away!</a>', 'capsule'), esc_url(home_url())); ?></p>
<h3><?php _e('Projects & Tags', 'capsule'); ?></h3>
<p><img src="<?php echo get_template_directory_uri(); ?>/docs/tags.jpg" alt="<?php _e('Projects and Tags', 'capsule'); ?>" class="capsule-screenshot" /></p>
<p><?php _e('Capsule stores metadata about your posts to make them easy to filter and find later. You can specify projects and tags for each post, just by entering them into the content of your post. Capsule uses the following syntax to parse projects and tags:', 'capsule'); ?></p>
<ul>
<li><?php _e('Projects: @example, @example-project, @example.com', 'capsule'); ?></li>
<li><?php _e('Tags: #example, #example-tag, #example.com', 'capsule'); ?></li>
</ul>
<p><?php _e('Simply include these in the content of your post and Capsule will find them and store them as standard WordPress taxonomy terms for your post.', 'capsule'); ?></p>
<h3><?php _e('Search', 'capsule'); ?></h3>
<p><img src="<?php echo get_template_directory_uri(); ?>/docs/search.jpg" alt="<?php _e('Search', 'capsule'); ?>" class="capsule-screenshot" /></p>
<p><?php _e('We\'re saving this information to make it useful in the future, so we\'ve got to be able to find it again. Capsule supports both keyword search and filtering by projects, tags, code languages and date range, whew! When using keyword search you can auto-complete projects, tags, and code languages by using their syntax prefix.', 'capsule'); ?></p>
<p><img src="<?php echo get_template_directory_uri(); ?>/docs/filter.jpg" alt="<?php _e('Filters', 'capsule'); ?>" class="capsule-screenshot" /></p>
<p><?php _e('When filtering, multiple projects/tags/etc. can be selected and are all populated with auto-complete.', 'capsule'); ?></p>
<h3><?php _e('Icon and Fluid Apps', 'capsule'); ?></h3>
<p><?php _e('Capsule works great with apps like Fluid that give you an application for a website. Need an icon for your app? Find it in the <code>wp-content/themes/capule/ui/assets/icon/</code> dir.', 'capsule'); ?></p>
</div>
<div class="capsule-doc-col-right">
<h3><?php _e('Editing', 'capsule'); ?></h3>
<!-- 1050 x 450 -->
<p><img src="<?php echo get_template_directory_uri(); ?>/docs/editing.jpg" alt="<?php _e('Editing', 'capsule'); ?>" class="capsule-screenshot" /></p>
<p><?php _e('Bring up the editor for a post by clicking the Edit icon or double-clicking on the post content.', 'capsule'); ?></p>
<p><?php _e('Capsule supports <a href="http://michelf.ca/projects/php-markdown/extra/">Markdown Extra</a> syntax with one minor nuance. Since we are using hashtag notation to create tags for our posts, to create a title using Markdown syntax Capsule requires a space between the "#" and the title text. Example:', 'capsule'); ?></p>
<ul>
<li><?php _e('Title: # I am a Title!', 'capsule'); ?></li>
<li><?php _e('Tag: #i-am-a-tag', 'capsule'); ?></li>
</ul>
<p><?php _e('When you are editing a post, Capsule auto-saves for you every 10 seconds. There is an "edited" indicator in the upper left corner of the editor next to the Last Saved time. Of course you can also save explicitly at any time using the keyboard shortcut. Capsule also saves when you close the editor.', 'capsule'); ?></p>
<p><?php _e('If you want to keep a post easily accessible, you can star it and it will remain at the top of your posts list (until it is un-starred). You can star as many posts as you like.', 'capsule'); ?></p>
<h3><?php _e('Code Syntax Highlighting', 'capsule'); ?></h3>
<p><img src="<?php echo get_template_directory_uri(); ?>/docs/highlighting.jpg" alt="<?php _e('Syntax Highlighting', 'capsule'); ?>" class="capsule-screenshot" /></p>
<p><?php _e('Capsule supports GitHub-style fenced code blocks, and syntax highlighting for code blocks.', 'capsule'); ?></p>
<pre>```php
// Say hello!
echo 'Hello World';
```</pre>
<p><?php _e('Additionally, when you use fenced code blocks Capsule saves the code language as metadata for your post.', 'capsule'); ?></p>
<h3><?php _e('Keyboard Shortcuts', 'capsule'); ?></h3>
<table class="widefat">
<thead>
<tr>
<th> </th>
<th><?php _e('Mac', 'capsule'); ?></th>
<th><?php _e('Windows', 'capsule'); ?></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php _e('Save', 'capsule'); ?></td>
<td><?php _e('Command-S', 'capsule'); ?></td>
<td><?php _e('Control-S', 'capsule'); ?></td>
</tr>
<tr>
<td><?php _e('Re-center active editor', 'capsule'); ?></td>
<td><?php _e('Command-Shift-0', 'capsule'); ?></td>
<td><?php _e('Control-Shift-0', 'capsule'); ?></td>
</tr>
<tr>
<td><?php _e('Close active editor', 'capsule'); ?></td>
<td><?php _e('Esc', 'capsule'); ?></td>
<td><?php _e('Esc', 'capsule'); ?></td>
</tr>
<tr>
<td><?php _e('Indent current line', 'capsule'); ?></td>
<td><?php _e('Command-]', 'capsule'); ?></td>
<td><?php _e('Control-]', 'capsule'); ?></td>
</tr>
<tr>
<td><?php _e('Outdent current line', 'capsule'); ?></td>
<td><?php _e('Command-[', 'capsule'); ?></td>
<td><?php _e('Control-[', 'capsule'); ?></td>
</tr>
<tr>
<td><?php _e('Navigate Home', 'capsule'); ?></td>
<td><?php _e('Shift-H', 'capsule'); ?></td>
<td><?php _e('Shift-H', 'capsule'); ?></td>
</tr>
<tr>
<td><?php _e('Create New Post', 'capsule'); ?></td>
<td><?php _e('Shift-N', 'capsule'); ?></td>
<td><?php _e('Shift-N', 'capsule'); ?></td>
</tr>
<tr>
<td><?php _e('Set Focus to Search', 'capsule'); ?></td>
<td><?php _e('Shift-F', 'capsule'); ?></td>
<td><?php _e('Shift-F', 'capsule'); ?></td>
</tr>
</tbody>
</table>
</div>
<br style="clear: both;">
<hr>
<div class="capsule-doc-col-left">
<h3><?php _e('Working With a Team', 'capsule'); ?></h3>
<p><?php _e('While Capsule is a tool for an individual developer, it is also a tool for team collaboration. You can connect to one or more Capsule Servers and replicate selected posts to those servers.', 'capsule'); ?></p>
<ol>
<li><?php printf(__('<a href="%s">Add a Capsule Server</a> (you must have an account on the Capsule Server)', 'capsule'), esc_url(admin_url('admin.php?page=capsule-servers'))); ?></li>
<li><?php printf(__('<a href="%s">Connect to the Server\'s Projects</a>', 'capsule'), esc_url(admin_url('admin.php?page=capsule-projects'))); ?></li>
</ol>
<p><?php _e('Once you map a local project to a project on a Capsule Server, any posts for that project will be automatically replicated to the Capsule Server (if you want, you can send the same local project to multiple Capsule Servers). While you maintain your single development journal, you can connect to multiple Capsule Servers to coordinate with multiple development teams.', 'capsule'); ?></p>
<p><?php _e('The Capsule Server allows you to view posts by project, tag, developer, date range, and keyword search.', 'capsule'); ?></p>
</div>
<div class="capsule-doc-col-right">
<h3><?php _e('Capsule Server', 'capsule'); ?></h3>
<p><?php _e('Anyone can set up a <a href="http://crowdfavorite.com/capsule/">Capsule Server</a>. It is free, Open Source and built on WordPress; just like Capsule.', 'capsule'); ?></p>
<p><?php _e('Add users to your Capsule Server and they will be able to connect their Capsule journals to your Server.', 'capsule'); ?></p>
</div>
<br style="clear: both;">
<hr>
<div class="capsule-doc-col-left">
<h3><?php _e('Credits', 'capsule'); ?></h3>
<p><?php _e('Capsule was conceived and executed by the brilliant and devastatingly good-looking men and women at <a href="http://crowdfavorite.com">Crowd Favorite</a>.', 'capsule'); ?></p>
<p><?php _e('Capsule is released under the GPL v2 license.', 'capsule'); ?></p>
</div>
<div class="capsule-doc-col-right">
<h3> </h3>
<p><?php _e('In the finest tradition of Open Source, Capsule was built on the shoulders of the following giants:', 'capsule'); ?></p>
<?php capsule_credits(); ?>
</div>
</div>
<?php
}
public function capsule_admin_notice(){
if (isset($_GET['page']) && strpos($_GET['page'], 'capsule') !== false) {
return;
}
?>
<style type="text/css">
.capsule-welcome {
background: #222;
color: #fff;
margin: 30px 10px 10px 0;
padding: 15px;
}
.capsule-welcome h1 {
font-weight: normal;
line-height: 100%;
margin: 0 0 10px 0;
}
.capsule-welcome p {
font-weight: normal;
line-height: 100%;
margin: 0;
}
.capsule-welcome a,
.capsule-welcome a:visited {
color: #f8f8f8;
}
</style>
<section class="capsule-welcome">
<h1><?php _e('Welcome to Capsule', 'capsule'); ?></h1>
<p><?php printf(__('Please read the overview, FAQs and more about <a href="%s">how Capsule works</a>.', 'capsule'), esc_url(admin_url('admin.php?page=capsule'))); ?></p>
</section>
<?php
}
// Markup for server management
public function server_management_page() {
$servers = $this->get_servers();
?>
<style type="text/css">
#cap-servers {
padding-top: 10px;
}
#cap-servers td {
padding: 5px;
}
.cap-api-key {
font-family: monospace;
white-space: nowrap;
}
.cap-delete,
.cap-delete:visited {
color: #BC0B0B;
}
}
.cap-delete:hover,
.cap-delete:active {
color: #f00;
}
.cap-editable {
display: none;
margin-bottom: 2px;
}
.cap-not-editable {
margin-top: 3px;
margin-left: 4px;
}
div.cap-edit-server-actions {
margin: 0 0 3px 0;
}
.capsule-error {
background: #FFEBE8;
border: 1px solid #c00;
-moz-border-radius: 3px; /* FF1+ */
-webkit-border-radius: 3px; /* Saf3+, Chrome */
border-radius: 3px; /* Standard. IE9+ */
color: #900;
margin-top: 5px;
padding: 4px 6px;
}
.capsule-error p {
line-height: 150%;
margin: 0;
padding: 0;
}
.capsule-spinner {
background: url(<?php echo admin_url('images/loading.gif'); ?>) no-repeat center center;
background-size: 16px;
margin: 5px 0 0 5px;
height: 16px;
width: 16px;
display: none;
}
.capsule-float-left {
float: left;
}
input.cap-input-error,
input.cap-input-error:focus {
border: 1px solid #c00;
}
</style>
<div class="wrap capsule-admin">
<div id="icon-options-general" class="icon32"></div>
<h2><?php _e('Capsule: Servers', 'capsule'); ?></h2>
<p class="description"><?php printf(__('Connect to one or more Capsule Servers to replicate selected content to those servers. <a href="%s">Learn More</a>', 'capsule'), esc_url(admin_url('admin.php?page=capsule'))); ?></p>
<div id="cap-servers">
<form method="post" id="js-cap-servers">
<table class="wp-list-table widefat fixed posts">
<thead>
<tr>
<th scope="col" class="manage-column column-label" width="15%">
<?php _e('Server Name', 'capsule'); ?>
</th>
<th scope="col" class="manage-column column-api-key" width="35%">
<?php _e('Server URL', 'capsule'); ?>
</th>
<th scope="col" class="manage-column column-api-key" width="40%">
<?php _e('Server API Key', 'capsule'); ?>
</th>
<th scope="col" class="manage-column column-actions" width="10%">
</th>
</tr>
</thead>
<tbody>
<?php
$class = '';
foreach ($servers as $server_post) {
$class = ($class == '') ? ' alternate' : '';
echo $this->server_row_markup($server_post, $class);
}
$class = ($class == '') ? ' alternate' : '';
?>
<tr id="js-server-item-new" class="<?php echo esc_attr($class); ?>">
<td>
<div>
<input type="text" class="widefat js-cap-server-name" name="server_name" value="" placeholder="<?php _e('Server Name', 'capsule'); ?>" />
</div>
</td>
<td>
<div>
<input type="text" class="widefat js-cap-server-url" name="server_url" value="" placeholder="<?php _e('Server URL', 'capsule'); ?>" />
</div>
</td>
<td>
<div>
<input type="text" class="widefat js-cap-server-api-key" name="server_api_key" value="" placeholder="<?php _e('API Key', 'capsule'); ?>" />
</div>
</td>
<td>
<div>
<input type="submit" class="js-cap-add capsule-float-left button" value="<?php _e('Add Server', 'capsule'); ?>" /><span class="capsule-float-left capsule-spinner"></span>
<input type="hidden" value="add_server" name="capsule_client_action" />
<?php wp_nonce_field('_cap_client_server_management', '_server_nonce', true, true); ?>
</div>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
<script type="text/javascript">
(function($) {
$(function() {
function capsule_process_server_errors($tr, result, server_id, new_server) {
new_server == undefined ? new_server : false;
var error_html = '';
var err_msg = '';
var $new_error_div;
capsule_remove_input_errors($tr);
if (!new_server) {
//$tr.fadeIn();
$tr.fadeIn();
}
for (var key in result.errors) {
$new_error_div = $('<div class="capsule-error js-cap-error-'+server_id+'" style="display:none;"></div>');
error_html = result.errors[key].message;
if (result.errors[key].type == 'url') {
$('.js-cap-server-url', $tr).addClass('cap-input-error');
}
if (result.errors[key].type == 'credentials') {
$('.js-cap-server-api-key', $tr).addClass('cap-input-error');
}
if (result.errors[key].type == 'name') {
$('.js-cap-server-name', $tr).addClass('cap-input-error');
}
err_msg = '<b><?php _e('Error: ', 'capsule'); ?></b>' + result.data.name+' - ' + error_html;
$new_error_div.html('<p>' + err_msg + '</p>')
.appendTo('#cap-servers').fadeIn();
}
}
function capsule_reset_server_form($form) {
$('input[name="server_name"]', $form).val("");
$('input[name="server_url"]', $form).val("");
$('input[name="server_api_key"]', $form).val("");
}
function capsule_remove_input_errors($tr) {
$('.js-cap-server-api-key', $tr).removeClass('cap-input-error');
$('.js-cap-server-url', $tr).removeClass('cap-input-error');
$('.js-cap-server-name', $tr).removeClass('cap-input-error');
}
$('#wpbody-content').on('click', '.js-cap-edit-server', function(e) {
var server_id = $(this).data('server_id');
e.preventDefault();
$('tr#js-server-item-'+server_id+' .js-cap-not-editable').hide();
$('tr#js-server-item-'+server_id+' .js-cap-editable').show();
});
$('form#js-cap-servers').on('submit', function(e) {
var $form = $(this);
var $spinner = $('.js-cap-add').siblings('.capsule-spinner');
var $tr = $('tr#js-server-item-new');
e.preventDefault();
$form.find('input[name="capsule_client_action"]').val('add_server_ajax');
$('.js-cap-error-new').hide();
capsule_remove_input_errors($tr);
$spinner.show();
$.post(
ajaxurl,
$form.serialize(),
function(result) {
$spinner.hide();
if (result.result == 'success') {
$(result.html).insertBefore("tr#js-server-item-new").hide().fadeIn();
capsule_reset_server_form($form);
}
else {
capsule_process_server_errors($tr, result, 'new', true);
}
},
'json'
);
});
$('#wpbody-content').on('click', '.js-cap-save-server', function(e) {
var server_id = $(this).data('server_id');
var $form = $('form#js-cap-servers');
var $tr = $('tr#js-server-item-'+server_id);
var $spinner = $(this).siblings('.capsule-spinner');
e.preventDefault();
$spinner.show();
$.post(
ajaxurl,
{
capsule_client_action : 'update_server_ajax',
server_name : $('#js-server-name-'+server_id).val(),
server_id : server_id,
api_key : $('#js-server-api_key-'+server_id).val(),
server_url : $('#js-server-url-'+server_id).val(),
_server_nonce : $('#_server_nonce').val(),
_wp_http_referer : $form.find('input[name="_wp_http_referer"]').val()
},
function(result) {
$spinner.hide();
if (result.result == 'success') {
$('.js-cap-server-api-key', $tr).removeClass('cap-input-error');
$('.js-cap-server-url', $tr).removeClass('cap-input-error');
$('.js-static-server-api-'+server_id).html(result.data.api_key);
$('.js-static-server-name-'+server_id).html(result.data.name);
$('.js-static-server-url-'+server_id).html(result.data.url);
$('.js-cap-error-'+server_id).remove();
$tr.animate({opacity:0}, function() {
$('.js-cap-not-editable', $tr).show();
$('.js-cap-editable', $tr).hide();
$tr.animate({opacity:1});
});
}
else {
$('.js-cap-error-'+server_id).fadeOut(function() {
$(this).remove()
});
$tr.fadeOut(function() {
capsule_process_server_errors($tr, result, server_id, false);
});
}
},
'json'
);
});
$('#wpbody-content').on('click', '.js-server-delete', function(e) {
var url = $(this).attr('href');
var $form = $('#js-cap-servers');
var server_id = $(this).data('server_id');
e.preventDefault();
$.get(
url,
{ doing_ajax : true },
function(data) {
if (data.result == 'success') {
$form.find('#js-server-item-'+server_id).fadeOut();
}
else {
//nothing it hasn't been deleted
}
},
'json'
);
});
});
})(jQuery);
</script>
<?php
}
/**
* Markup for a 'row' representing a server.
*
* @param object $server_post Normal WP Post object, has api_key and url properties if set @see get_servers())
* @param string $class Additional class to put on the wrapper for the row
*
* @return string HTML for the row
**/
public function server_row_markup($server_post, $class = '') {
$delete_url = add_query_arg('capsule_client_action', 'delete_server', admin_url());
$delete_url = add_query_arg('server_id', $server_post->ID, $delete_url);
// wp_nonce_url does esc_html
$delete_url = wp_nonce_url($delete_url, '_cap_client_delete_server');
$name_base = 'servers['.$server_post->ID.']';
$html = '
<tr id="'.esc_attr('js-server-item-'.$server_post->ID).'" class="'.esc_attr('server-item'.$class).'">
<td>
<div class="js-cap-not-editable cap-not-editable '.esc_attr('js-static-server-name-'.$server_post->ID).'">'
.esc_html($server_post->post_title).
'</div>
<div class="js-cap-editable">
<input type="text" class="widefat js-cap-editable cap-editable js-cap-server-name" id="'.esc_attr('js-server-name-'.$server_post->ID).'" name="'.$name_base.'[server_name]" value="'.esc_attr($server_post->post_title).'" />
</div>
</td>
<td>
<div class="js-cap-not-editable cap-not-editable '.esc_attr('js-static-server-url-'.$server_post->ID).'">'
.esc_html($server_post->url).
'</div>
<div class="js-cap-editable">
<input type="text" class="widefat js-cap-editable cap-editable js-cap-server-url" id="'.esc_attr('js-server-url-'.$server_post->ID).'" name="'.$name_base.'[server_url]" value="'.esc_attr($server_post->url).'" />
</div>
</td>
<td>
<div class="cap-api-key js-cap-not-editable cap-not-editable '.esc_attr('js-static-server-api-'.$server_post->ID).'">'
.esc_html($server_post->api_key).
'</div>
<div class="js-cap-editable cap-editable">
<input type="text" class="widefat js-cap-editable js-cap-server-api-key" id="'.esc_attr('js-server-api_key-'.$server_post->ID).'" name="'.$name_base.'[api_key]" value="'.esc_attr($server_post->api_key).'" />
</div>
</td>
<td>
<div class="js-cap-not-editable cap-not-editable cap-edit-server-actions">
<a href="#" class="js-cap-edit-server" data-server_id="'.esc_attr($server_post->ID).'">'.__('Edit', 'capsule').'</a> |
<a href="'.$delete_url.'" data-server_id="'.esc_attr($server_post->ID).'" class="delete js-server-delete cap-delete">'.__('Delete', 'capsule').'</a>
</div>
<div class="js-cap-editable cap-editable">
<a href="#" class="capsule-float-left js-cap-save-server button-primary" data-server_id="'.esc_attr($server_post->ID).'">'.__('Save', 'capsule').'</a><span class="capsule-float-left capsule-spinner"></span>
</div>
</td>
</tr>';
return $html;
}
/**
* Create a new server based on data
* @param array $data Array of data, see defaults for potential values
*
* @return bool True if server was added, false otherwise
**/
public function add_server($data) {
$defaults = array(
'server_name' => null,
'api_key' => null,
'server_url' => null,
);
$post_data = $postarr = wp_parse_args($data, $defaults);
$post_arr = array(
'post_title' => $post_data['server_name'],
'post_type' => 'server',
'post_status' => 'publish',
);
$post_id = wp_insert_post($post_arr);
if ($post_id) {
update_post_meta($post_id, $this->server_api_key, $post_data['api_key']);
update_post_meta($post_id, $this->server_url_key, $post_data['server_url']);
$post = get_post($post_id);
$post->api_key = $post_data['api_key'];
$post->url = $post_data['server_url'];
return $post;
}
return false;
}
/**
* Delete a server post.
*
* @param in $server_id ID of the post to delete
* @return mixed False on failure (from wp_delete_post)
**/
public function delete_server($server_id) {
return wp_delete_post($server_id, true);
}
/**
* Update a server with relevant data. Does not create it if it doesnt exist
*
*
* @param int $server_id Post ID of the server to update
* @param array $data Array of data. Possible values include:
* 'server_name' => post_title (label for server)
* 'api_key' => Server's api key, stored in post meta
* 'server_url' => Server's url, stored in post meta
* @return bool True if the update was successful, false if the post doesnt exist or something went wrong
*/
public function update_server($server_id, $data) {
$post = get_post($server_id);
if (!$post) {
return false;
}
$post_arr = array(
'ID' => $server_id,
);
if (isset($data['server_name'])) {
$post_arr['post_title'] = $data['server_name'];
}
$result = wp_update_post($post_arr);
if ($result && !is_wp_error($result)) {
if (isset($data['api_key'])) {
update_post_meta($server_id, $this->server_api_key, $data['api_key']);
}
if (isset($data['server_url'])) {
update_post_meta($server_id, $this->server_url_key, $data['server_url']);
}
return $this->process_server($server_id);
}
return false;
}
public function duplicate_server_check($server_name, $server_url, $server_id = 0) {
$errors = array();
$all_servers = $this->get_servers();
if (is_array($all_servers)) {
foreach ($all_servers as $server) {
// Dont check against self
if ($server->ID == $server_id) {
continue;
}
if (strtolower(trim($server->url, ' \t\n\r\0\x0B/')) == strtolower(trim($server_url, ' \t\n\r\0\x0B/'))) {