-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscreenstepslive.php
More file actions
1145 lines (951 loc) · 39.6 KB
/
Copy pathscreenstepslive.php
File metadata and controls
1145 lines (951 loc) · 39.6 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
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
/*
Plugin Name: ScreenSteps Live
Plugin URI: http://screensteps.com/blog/2008/07/screensteps-live-wordpress-plugin/
Description: This plugin will incorporate lessons from your ScreenSteps Live account into your WordPress Pages.
Version: 1.1.3
Author: Blue Mango Learning Systems
Author URI: http://www.screensteps.com
*/
// Todo: Look at moving to class approach. Register callbacks within class.
// Also look at registering one callback that is triggered as page is loading. If the page
// is one we are interested in then install all other callbacks.
//$result = error_reporting(E_ERROR|E_WARNING|E_PARSE|E_NOTICE);
// Global var for SSLiveWordPress object. It is shared among multiple wp callbacks.
$screenstepslivewp = NULL;
$screenstepslivePages = NULL;
$screenstepsliveA = NULL;
// This plugin processes content of all posts
add_action('wp_head', 'screenstepslive_addHeader', 100);
add_filter('the_content', 'screenstepslive_parseContent', 100);
add_filter('the_title', 'screenstepslive_parseTitle', 100);
add_filter('wp_list_pages', 'screenstepslive_listPages', 100);
add_filter('wp_list_pages_excludes', 'screenstepslive_listPagesExcludes', 100);
add_filter('delete_post', 'screenstepslive_checkIfDeletedPostIsReferenced', 100);
add_filter('name_save_pre', 'screenstepslive_changePagePermalink', 100);
add_action('admin_menu', 'screenstepslive_addPages');
add_filter('request', 'screenstepslive_query_vars', 100);
add_filter('wp_title', 'screenstepslive_wp_title', 10, 2); // Get in on this early
function screenstepslive_initializeObject()
{
global $screenstepslivewp;
if (!$screenstepslivewp) {
// PROVIDE EXAMPLE SETTINGS AS DEFAULT
if (get_option('screenstepslive_domain') == '' && get_option('screenstepslive_reader_name') == '') {
update_option('screenstepslive_domain', 'example.screenstepslive.com');
update_option('screenstepslive_reader_name', 'example');
update_option('screenstepslive_reader_password', 'example');
update_option('screenstepslive_protocol', 'http');
update_option('screenstepslive_pages', '');
}
require_once(dirname(__FILE__) . '/sslivewordpress_class.php');
// Create ScreenSteps Live object using your domain and API key
$screenstepslivewp = new SSLiveWordPress(get_option('screenstepslive_domain'),
get_option('screenstepslive_protocol'));
$screenstepslivewp->SetUserCredentials(get_option('screenstepslive_reader_name'), get_option('screenstepslive_reader_password'));
$screenstepslivewp->user_can_read_private = current_user_can('read_private_posts') == 1;
}
// Any caller will just get a reference to this object.
return $screenstepslivewp;
}
function screenstepslive_addHeader() {
// Was a comment submitted?
if ($_POST['screenstepslive_comment_submit'] == 1) {
////////////
$sslivewp = screenstepslive_initializeObject();
$postID = $_POST['sslivecommment']['page_id'];
// Find settings for this page
$pages = get_option('screenstepslive_pages');
foreach ($pages as $key => $page_entry) {
if ($page_entry['id'] == $postID) {
$page = $page_entry;
break;
}
}
// Get out if we have nothing to offer.
if (!isset($page)) wp_die('unable to find page for comment submission');
global $screenstepslivePages;
if (empty($screenstepslivePages)) {
$screenstepslivePages['space'] = empty($page['space_permalink']) ? $page['space_id'] : $page['space_permalink'];
$screenstepslivePages['manual'] = $sslivewp->CleanseID($_GET['manual_id']);
$screenstepslivePages['bucket'] = $sslivewp->CleanseID($_GET['bucket_id']);
$screenstepslivePages['lesson'] = $sslivewp->CleanseID($_GET['lesson_id']);
}
/*$space_id = $page['space_id'];
$manual_id = $sslivewp->CleanseID($_GET['manual_id']);
$bucket_id = $sslivewp->CleanseID($_GET['bucket_id']);
*/
if ($page['resource_type'] == 'bucket' &&
( (is_string($page['resource_id']) && !empty($page['resource_id'])) || (is_int($page['resource_id']) && $page['resource_id'] > 0) )
)
$screenstepslivePages['bucket'] = $page['resource_id'];
else if ($page['resource_type'] == 'manual' &&
( (is_string($page['resource_id']) && !empty($page['resource_id'])) || (is_int($page['resource_id']) && $page['resource_id'] > 0) )
)
$screenstepslivePages['manual'] = $page['resource_id'];
/* $lesson_id = $sslivewp->CleanseID($_GET['lesson_id']);*/
////////////
if ($page['resource_type'] == 'manual') {
$resource_type = 'manual';
$resource_id = $screenstepslivePages['manual'];
} else {
$resource_type = 'bucket';
$resource_id = $screenstepslivePages['bucket'];
}
$name = $_POST['sslivecommment']['author'];
$email = $_POST['sslivecommment']['email'];
$comment = $_POST['sslivecommment']['comment'];
if (get_magic_quotes_gpc()) {
$name = stripslashes($name);
$email = stripslashes($email);
$comment = stripslashes($comment);
}
$errors = $sslivewp->SubmitLessonComment($screenstepslivePages['space'], $resource_type, $resource_id, $screenstepslivePages['lesson'],
$name, $email, $comment, $_POST['sslivecommment']['subscribe']);
if ( count($errors) > 0 ) {
foreach ($errors as $key=>$value)
{
$error_str .= '<p>' . $value . '</p>';
}
wp_die($error_str);
}
}
// CSS
$plugin_folder = basename(dirname(__FILE__));
echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/' . $plugin_folder . '/css/screenstepslive.css" />' . "\n";
}
// Support for mod-rewrite
function screenstepslive_query_vars($request)
{
global $screenstepslivePages;
//print_r($request);
/*
Array
(
[page] => /4209
[pagename] => screensteps-live-page/space/4/manual/screensteps/lesson
)
*/
$pathParts = explode('/', $request['pagename']);
//print_r($pathParts);
$pages = get_option('screenstepslive_pages');
//print_r($pages);
// Update permalink for page
if (is_array($pages))
{
foreach ($pages as $key => $page_entry) {
$newPageName = '';
foreach ($pathParts as $i => $name) {
if ($name == $page_entry['permalink']) {
if (isset($pathParts[$i+1]))
{
$pageIsSet = isset($request['page']) && !empty($request['page']);
$screenstepslivePages['space'] = $pathParts[$i+1];
if ($pathParts[$i+2] == 'manual') {
// Look for numeric id
if (!isset($pathParts[$i+4]) && $pageIsSet)
$screenstepslivePages['manual'] = substr($request['page'], 1, strlen($request['page'])); // e.g. /4709
else
$screenstepslivePages['manual'] = $pathParts[$i+3];
} else {
// Look for numeric id
if (!isset($pathParts[$i+4]) && $pageIsSet)
$screenstepslivePages['bucket'] = substr($request['page'], 1, strlen($request['page'])); // e.g. /4709
else
$screenstepslivePages['bucket'] = $pathParts[$i+3];
}
if (isset($pathParts[$i+4])) {
if ($pageIsSet)
$screenstepslivePages['lesson'] = substr($request['page'], 1, strlen($request['page'])); // e.g. /4709
else
$screenstepslivePages['lesson'] = $pathParts[$i+4];
}
$screenstepslivePages['lesson'] = explode('-', $screenstepslivePages['lesson']);
$screenstepslivePages['lesson'] = $screenstepslivePages['lesson'][0];
$request['pagename'] = $newPageName . $name;
}
break 2;
}
else
{
// May include a parent page
$newPageName .= $name . '/';
}
}
}
}
//print_r($request);
return $request;
}
function screenstepslive_changePagePermalink($data)
{
$pageID = (int) $_POST['post_ID'];
// Find settings for this page
// Can't find way to determine if revision is being inserted other than this.
// ID doesn't seem to be available anywhere. Silly.
if ($pageID > 0 && $data != ($pageID . '-revision') && $data != ($pageID . '-autosave'))
{
$pages = get_option('screenstepslive_pages');
// Update permalink for page
foreach ($pages as $key => $page_entry) {
if ($page_entry['id'] == $pageID) {
if (empty($data)) {
// WordPress hasn't assigned permalink yet. Grab one.
$data = get_sample_permalink($pageID);
$data = $data[1];
}
$pages[$key]['permalink'] = $data;
update_option('screenstepslive_pages', $pages);
break;
}
}
}
return $data;
}
function screenstepslive_listPagesExcludes($the_output) {
// Set flag that WP is listing pages
global $screenstepsliveA;
$screenstepsliveA['listing pages'] = true;
return ($the_output);
}
function screenstepslive_listPages($the_output) {
// unset flag that turns off title parsing
global $screenstepsliveA;
$screenstepsliveA['listing pages'] = false;
// We remove the link to the current SS Live page from the list. It's $title will be rewritten
// by screenstepslive_parseTitle and since WordPress has one filter for ALL titles we don't have
// a lot of options.
global $screenstepslivePages;
$postID = get_the_ID();
$post = &get_post($postID);
// Find settings for this page
$pages = get_option('screenstepslive_pages');
foreach ($pages as $key => $page_entry) {
if ($page_entry['id'] == $post->ID) {
$page = $page_entry;
break;
}
}
// Get out if we have nothing to offer.
if (!isset($page)) return ($the_output);
// Include necessary SS Live files
$sslivewp = screenstepslive_initializeObject();
if (empty($screenstepslivePages)) {
$screenstepslivePages['space'] = empty($page['space_permalink']) ? $page['space_id'] : $page['space_permalink'];
$screenstepslivePages['manual'] = $sslivewp->CleanseID($_GET['manual_id']);
$screenstepslivePages['bucket'] = $sslivewp->CleanseID($_GET['bucket_id']);
$screenstepslivePages['lesson'] = $sslivewp->CleanseID($_GET['lesson_id']);
}
/*$space_id = $page['space_id'];
$manual_id = $sslivewp->CleanseID($_GET['manual_id']);
$bucket_id = $sslivewp->CleanseID($_GET['bucket_id']);*/
if ($page['resource_type'] == 'bucket' && $page['resource_id'] > 0)
$screenstepslivePages['bucket'] = $page['resource_id'];
else if ($page['resource_type'] == 'manual' && $page['resource_id'] > 0)
$screenstepslivePages['manual'] = $page['resource_id'];
/*$lesson_id = $sslivewp->CleanseID($_GET['lesson_id']);*/
// What has this page been renamed too?
if (empty($screenstepslivePages['space']))
{
// nothing to do
} else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['lesson'])) {
if (!empty( $screenstepslivePages['manual'] )) {
if ($page['resource_id'] == 0)
{
// Page is a 'space' page.
$the_title = '<a href="' . $sslivewp->GetLinkToManual($post->ID, $screenstepslivePages['space'], $screenstepslivePages['manual']) . '">' .
$sslivewp->GetManualTitle($screenstepslivePages['space'], $screenstepslivePages['manual']) . '</a>: ' .
$sslivewp->GetManualLessonTitle($screenstepslivePages['space'], $screenstepslivePages['manual'], $screenstepslivePages['lesson']);
} else
{
// Page is a 'manual' page.
$the_title = $sslivewp->GetManualLessonTitle($screenstepslivePages['space'], $screenstepslivePages['manual'], $screenstepslivePages['lesson']);
}
} else if (!empty( $screenstepslivePages['bucket'])) {
if ($page['resource_id'] == 0)
{
// Page is a 'space' page.
$the_title = '<a href="' . $sslivewp->GetLinkToBucket($post->ID, $screenstepslivePages['space'], $screenstepslivePages['bucket']) . '">' .
$sslivewp->GetBucketTitle($screenstepslivePages['space'], $screenstepslivePages['bucket']) . '</a>: ' .
$sslivewp->GetBucketLessonTitle($screenstepslivePages['space'], $screenstepslivePages['bucket'], $screenstepslivePages['lesson']);
} else
{
// Page is a 'manual' page.
$the_title = $sslivewp->GetBucketLessonTitle($screenstepslivePages['space'], $screenstepslivePages['bucket'], $screenstepslivePages['lesson']);
}
}
} else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['manual'])) {
if ($page['resource_id'] == 0)
{
// Page is a 'space' page.
$the_title = $sslivewp->GetManualTitle($screenstepslivePages['space'], $screenstepslivePages['manual']);
} else
{
// Page is a 'manual' page.
// Nothing to do.
}
} else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['bucket'])) {
if ($page['resource_id'] == 0)
{
// Page is a 'space' page.
$the_title = $sslivewp->GetBucketTitle($screenstepslivePages['space'], $screenstepslivePages['bucket']);
} else
{
// Page is a 'bucket' page.
// Nothing to do.
}
} else {
// Spaces. Not used.
}
if (empty($the_title) || $the_title == $post->post_title) {
return ($the_output);
} else {
// Now rename the page in the list to the original post title
if ( preg_match( '/>' . preg_quote($the_title, "/") . '\</', $the_output, $matches, PREG_OFFSET_CAPTURE) ) {
$the_output = substr_replace( $the_output, '>' . $post->post_title . '<', $matches[0][1], strlen($matches[0][0]) );
}
return ($the_output);
}
}
function screenstepslive_wp_title($the_title, $sep)
{
global $screenstepslivePages;
$postID = get_the_ID();
$post = &get_post($postID);
// Find settings for this page
$pages = get_option('screenstepslive_pages');
foreach ($pages as $key => $page_entry) {
if ($page_entry['id'] == $post->ID) {
$page = $page_entry;
break;
}
}
// Get out if we have nothing to offer.
if (!isset($page)) return ($the_title);
// Include necessary SS Live files
$sslivewp = screenstepslive_initializeObject();
if (empty($screenstepslivePages)) {
$screenstepslivePages['space'] = empty($page['space_permalink']) ? $page['space_id'] : $page['space_permalink'];
$screenstepslivePages['manual'] = $sslivewp->CleanseID($_GET['manual_id']);
$screenstepslivePages['bucket'] = $sslivewp->CleanseID($_GET['bucket_id']);
$screenstepslivePages['lesson'] = $sslivewp->CleanseID($_GET['lesson_id']);
}
if ($page['resource_type'] == 'bucket' && $page['resource_id'] > 0)
$screenstepslivePages['bucket'] = $page['resource_id'];
else if ($page['resource_type'] == 'manual' && $page['resource_id'] > 0)
$screenstepslivePages['manual'] = $page['resource_id'];
if (empty($screenstepslivePages['space']))
{
// nothing to do
} else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['lesson'])) {
// Displaying a lesson
if (!empty($screenstepslivePages['manual'])) {
if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
{
// Default page is a space.
// space: manual: lesson
$the_title = $sslivewp->GetManualLessonTitle($screenstepslivePages['space'], $screenstepslivePages['manual'], $screenstepslivePages['lesson']);
} else
{
// Default page is a manual.
// spacemanual: lesson
$the_title = $sslivewp->GetManualLessonTitle($screenstepslivePages['space'], $screenstepslivePages['manual'], $screenstepslivePages['lesson']);
}
} else if (!empty($screenstepslivePages['bucket'])) {
if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
{
// Default page is a space.
// space: manual: lesson
$the_title = $sslivewp->GetBucketLessonTitle($screenstepslivePages['space'], $screenstepslivePages['bucket'], $screenstepslivePages['lesson']);
} else
{
// bucket: lesson
// Default page is a bucket.
$the_title = $sslivewp->GetBucketLessonTitle($screenstepslivePages['space'], $screenstepslivePages['bucket'], $screenstepslivePages['lesson']);
}
}
} else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['manual'])) {
// Displaying a manual
if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
{
// Default page is a space. Get manual title.
// space: manual
$the_title = $sslivewp->GetManualTitle($screenstepslivePages['space'], $screenstepslivePages['manual']);
} else
{
// Default page is a manual. Nothing to do.
}
} else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['bucket'])) {
// Displaying a bucket
if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
{
// Default page is a space. Get bucket title.
// bucket
$the_title = $sslivewp->GetBucketTitle($screenstepslivePages['space'], $screenstepslivePages['bucket']);
} else
{
// Default page is a bucket. Nothing to do.
}
} else {
// Spaces. Not used.
}
if (!empty($sep)) $the_title .= ' ' . $sep;
return ($the_title);
}
function screenstepslive_parseTitle($the_title) {
if (!is_page( $the_title)) return ($the_title); // cursed wp_list_pages calls this as well.
global $screenstepsliveA;
// Get out if WP is listing pages
if ($screenstepsliveA['listing pages'] === true) return ($the_title);
global $screenstepslivePages;
$postID = get_the_ID();
$post = &get_post($postID);
// Find settings for this page
$pages = get_option('screenstepslive_pages');
foreach ($pages as $key => $page_entry) {
if ($page_entry['id'] == $post->ID) {
$page = $page_entry;
break;
}
}
// Get out if we have nothing to offer.
if (!isset($page)) return ($the_title);
// Include necessary SS Live files
$sslivewp = screenstepslive_initializeObject();
if (empty($screenstepslivePages)) {
$screenstepslivePages['space'] = empty($page['space_permalink']) ? $page['space_id'] : $page['space_permalink'];
$screenstepslivePages['manual'] = $sslivewp->CleanseID($_GET['manual_id']);
$screenstepslivePages['bucket'] = $sslivewp->CleanseID($_GET['bucket_id']);
$screenstepslivePages['lesson'] = $sslivewp->CleanseID($_GET['lesson_id']);
}
if ($page['resource_type'] == 'bucket' && $page['resource_id'] > 0)
$screenstepslivePages['bucket'] = $page['resource_id'];
else if ($page['resource_type'] == 'manual' && $page['resource_id'] > 0)
$screenstepslivePages['manual'] = $page['resource_id'];
if (empty($screenstepslivePages['space']))
{
// nothing to do
} else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['lesson'])) {
// Displaying a lesson
if (!empty($screenstepslivePages['manual'])) {
if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
{
// Default page is a space.
// space: manual: lesson
$the_title = '<a href="' . $sslivewp->GetLinkToSpace($post->ID, $screenstepslivePages['space']) . '">' .
$the_title . '</a>: ' .
'<a href="' . $sslivewp->GetLinkToManual($post->ID, $screenstepslivePages['space'], $screenstepslivePages['manual']) . '">' .
$sslivewp->GetManualTitle($screenstepslivePages['space'], $screenstepslivePages['manual']) . '</a>: ' .
$sslivewp->GetManualLessonTitle($screenstepslivePages['space'], $screenstepslivePages['manual'], $screenstepslivePages['lesson']);
} else
{
// Default page is a manual.
// spacemanual: lesson
$the_title = '<a href="' . $sslivewp->GetLinkToManual($post->ID, $screenstepslivePages['space'], $screenstepslivePages['manual']) . '">' .
$the_title . '</a>: ' .
$sslivewp->GetManualLessonTitle($screenstepslivePages['space'], $screenstepslivePages['manual'], $screenstepslivePages['lesson']);
}
} else if (!empty($screenstepslivePages['bucket'])) {
if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
{
// Default page is a space.
// space: manual: lesson
$the_title = '<a href="' . $sslivewp->GetLinkToBucket($post->ID, $screenstepslivePages['space'], $screenstepslivePages['bucket']) . '">' .
$sslivewp->GetBucketTitle($screenstepslivePages['space'], $screenstepslivePages['bucket']) . '</a>: ' .
$sslivewp->GetBucketLessonTitle($screenstepslivePages['space'], $screenstepslivePages['bucket'], $screenstepslivePages['lesson']);
} else
{
// bucket: lesson
// Default page is a bucket.
$the_title = $sslivewp->GetBucketLessonTitle($screenstepslivePages['space'], $screenstepslivePages['bucket'], $screenstepslivePages['lesson']);
}
}
} else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['manual'])) {
// Displaying a manual
if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
{
// Default page is a space. Get manual title.
// space: manual
$the_title = '<a href="' . $sslivewp->GetLinkToSpace($post->ID, $screenstepslivePages['space']) . '">' .
$the_title . '</a>: ' .
$sslivewp->GetManualTitle($screenstepslivePages['space'], $screenstepslivePages['manual']);
} else
{
// Default page is a manual. Nothing to do.
}
} else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['bucket'])) {
// Displaying a bucket
if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
{
// Default page is a space. Get bucket title.
// bucket
$the_title = $sslivewp->GetBucketTitle($screenstepslivePages['space'], $screenstepslivePages['bucket']);
} else
{
// Default page is a bucket. Nothing to do.
}
} else {
// Spaces. Not used.
}
return ($the_title);
}
// Called by WordPress to process content
function screenstepslive_parseContent($the_content)
{
global $screenstepslivePages;
$postID = get_the_ID();
$post = &get_post($postID);
if (stristr($the_content, '{{SCREENSTEPSLIVE_CONTENT}}') !== FALSE) {
$text = NULL;
$the_content = str_ireplace('<p>', '', $the_content);
$the_content = str_ireplace('</p>', '', $the_content);
$the_content = '<div class="screenstepslive">' . "\n" . $the_content . '</div>';
// Find settings for this page
$pages = get_option('screenstepslive_pages');
foreach ($pages as $key => $page_entry) {
if ($page_entry['id'] == $post->ID) {
$page = $page_entry;
break;
}
}
// Get out if we have nothing to offer.
if (!isset($page)) return false;
//print_r($page);
// Include necessary SS Live files
$sslivewp = screenstepslive_initializeObject();
if (empty($screenstepslivePages)) {
$space_id = empty($page['space_permalink']) ? $page['space_id'] : $page['space_permalink'];
$manual_id = $sslivewp->CleanseID($_GET['manual_id']);
$bucket_id = $sslivewp->CleanseID($_GET['bucket_id']);
$lesson_id = $sslivewp->CleanseID($_GET['lesson_id']);
} else {
$space_id = $screenstepslivePages['space'];
$manual_id = $screenstepslivePages['manual'];
$bucket_id = $screenstepslivePages['bucket'];
$lesson_id = $screenstepslivePages['lesson'];
}
if ($page['resource_type'] == 'bucket' &&
( (is_string($page['resource_id']) && !empty($page['resource_id'])) || (is_int($page['resource_id']) && $page['resource_id'] > 0) )
)
$bucket_id = $page['resource_id'];
else if ($page['resource_type'] == 'manual' &&
( (is_string($page['resource_id']) && !empty($page['resource_id'])) || (is_int($page['resource_id']) && $page['resource_id'] > 0) )
)
$manual_id = $page['resource_id'];
////////////////////////
////////////////////////
if (empty($space_id))
{
// Retrieve list of all spaces
$text = $sslivewp->GetSpacesList($post->ID);
} else if (!empty($space_id) && !empty($lesson_id)) {
if ($page['resource_type'] == 'manual') {
$resource_type = 'manual';
$resource_id = $manual_id;
} else {
$resource_type = 'bucket';
$resource_id = $bucket_id;
}
// What content to display?
if (!empty($manual_id)) {
$max_len = 30;
$next_title = $sslivewp->GetNextLessonTitle($space_id, 'manual', $manual_id, $lesson_id);
$prev_title = $sslivewp->GetPrevLessonTitle($space_id, 'manual', $manual_id, $lesson_id);
if (strlen(utf8_decode($next_title)) > $max_len) $next_title = screenstepslive_utf8_substr($next_title, 0, $max_len-1) . '...';
if (strlen(utf8_decode($prev_title)) > $max_len) $prev_title = screenstepslive_utf8_substr($prev_title, 0, $max_len-1) . '...';
$next_link = $sslivewp->GetLinkToNextLesson($post->ID, $space_id, 'manual', $manual_id, $lesson_id, $next_title . ' >'); // 'Next Lesson');
$prev_link = $sslivewp->GetLinkToPrevLesson($post->ID, $space_id, 'manual', $manual_id, $lesson_id, '< ' . $prev_title); //'Previous Lesson');
$text = '<div class="screenstepslive_navigation">' . "\n";
if ($prev_link != '')
$text .= '<div class="alignleft">' . $prev_link . '</div>' . "\n";
if (!empty($next_link))
$text .= '<div class="alignright">' . $next_link . '</div>' . "\n";
$text .= '<div class="screenstepslive_nav_bottom"></div>';
$text .= '</div>';
$text .= $sslivewp->GetLessonHTML($space_id, 'manual', $manual_id, $lesson_id);
$text .= '<div class="screenstepslive_navigation">' . "\n";
if (!empty($prev_link))
$text .= '<div class="alignleft">' . $prev_link . '</div>' . "\n";
if (!empty($next_link))
$text .= '<div class="alignright">' . $next_link . '</div>' . "\n";
$text .= '<div class="screenstepslive_nav_bottom"></div>';
$text .= '</div>';
} else if (!empty($bucket_id)) {
$text = $sslivewp->GetLessonHTML($space_id, 'bucket', $bucket_id, $lesson_id);
}
if ($page['allow_comments']) {
$text .= $sslivewp->GetLessonComments($post->ID, $space_id, $resource_type, $resource_id, $lesson_id);
}
} else if (!empty($space_id) && !empty($manual_id)) {
$text = $sslivewp->GetManualList($post->ID, $space_id, $manual_id);
} else if (!empty($space_id) && !empty($bucket_id)) {
$text = $sslivewp->GetBucketList($post->ID, $space_id, $bucket_id);
} else {
$text = $sslivewp->GetSpaceList($post->ID, $space_id);
}
}
if (!is_null($text)) {
// case insensitive search/replace in PHP 5. preg_replace breaks if
// $1 or \\1 is in the string. This is the happy compromise.
if ( preg_match( '/{{SCREENSTEPSLIVE_CONTENT}}/i', $the_content, $matches, PREG_OFFSET_CAPTURE) ) {
$the_content = substr_replace( $the_content, $text, $matches[0][1], strlen($matches[0][0]) );
}
//$the_content = preg_replace('/{{SCREENSTEPSLIVE_CONTENT}}/i', $text, $the_content);
}
return $the_content;
}
function screenstepslive_utf8_substr($str,$start)
{
preg_match_all("/./u", $str, $ar);
if(func_num_args() >= 3) {
$end = func_get_arg(2);
return join("",array_slice($ar[0],$start,$end));
} else {
return join("",array_slice($ar[0],$start));
}
}
function screenstepslive_checkIfDeletedPostIsReferenced($postID) {
$pages = get_option('screenstepslive_pages');
if (is_array($pages))
{
foreach ($pages as $i => $value) {
if ($pages[$i]['id'] == $postID) {
unset($pages[$i]);
}
}
update_option('screenstepslive_pages', $pages); // array_values reindexes
//update_option('screenstepslive_pages', array_values($pages)); // array_values reindexes
}
}
// Use to replace page title
//$the_content = preg_replace('/{{SCREENSTEPSLIVE_LESSON_TITLE}}/i', $sslivewp->GetLessonTitle($manual_id, $lesson_id), $the_content);
// Add admin page
function screenstepslive_addPages()
{
add_options_page('ScreenSteps Live Options', 'ScreenSteps Live', 8, __FILE__, 'screenstepslive_optionPage');
}
// Shows Admin page
function screenstepslive_optionPage()
{
$sslivewp = screenstepslive_initializeObject();
$form_submitted = false; // So we don't create pages twice.
// API form was submitted
if ($_POST['api_submitted'] == 1) {
if (get_option('screenstepslive_domain') != $_POST['domain']) {
// Reset caches as account probably changed.
$pages = get_option('screenstepslive_pages');
if (is_array($pages) ) {
foreach ($pages as $key => $page) {
$pages[$key]['space_id'] = '';
$pages[$key]['resource_type'] = '';
$pages[$key]['resource_id'] = '';
}
update_option('screenstepslive_pages', $pages);
}
}
update_option('screenstepslive_domain', $_POST['domain']);
update_option('screenstepslive_reader_name', $_POST['reader_name']);
update_option('screenstepslive_reader_password', $_POST['reader_password']);
update_option('screenstepslive_protocol', $_POST['protocol']);
$sslivewp->domain = $_POST['domain'];
$sslivewp->SetUserCredentials($_POST['reader_name'], $_POST['reader_password']);
$sslivewp->protocol = $_POST['protocol'];
$form_submitted = true;
}
// Get list of spaces from SS Live
$spaces = $sslivewp->GetSpaces();
// Manuals form was submited
if ($_POST['pages_submitted'] == 1 && is_array($_POST['pages'])) {
// Loop through posted pages, making sure they still exist. User could have deleted one.
$pages = get_option('screenstepslive_pages');
//print_r($_POST['pages']);
//print_r($pages);
foreach ($_POST['pages'] as $page_id => $new_page) {
if (isset($pages[$page_id])) {
if ($pages[$page_id]['space_id'] != $new_page['space_id']) {
$pages[$page_id]['resource_id'] = 0;
} else {
$pages[$page_id]['resource_id'] = $new_page['resource_id'];
}
$pages[$page_id]['space_id'] = $new_page['space_id'];
$pages[$page_id]['resource_type'] = 'manual';
$pages[$page_id]['allow_comments'] = ($new_page['allow_comments'] == 'on') ? 1 : 0;
// Find permalink
foreach($spaces['space'] as $space) {
if ($pages[$page_id]['space_id'] == $space['id']) {
$pages[$page_id]['space_permalink'] = $space['permalink'];
break;
}
}
}
}
// print_r($pages);
update_option('screenstepslive_pages', $pages);
$form_submitted = true;
}
// Create template pages
if (!$form_submitted && isset($_GET['ssliveaction'])) {
switch ($_GET['ssliveaction']) {
case 'create_page':
$postID = screenstepslive_createTemplatePage();
if (intval($postID) > 0) {
$post = &get_post($postID);
$pages = get_option('screenstepslive_pages');
$pages[$postID]['id'] = $postID;
$pages[$postID]['space_id'] = $spaces['space'][0]['id'];
$pages[$postID]['space_permalink'] = $spaces['space'][0]['permalink'];
$pages[$postID]['resource_type'] = 'manual';
$pages[$postID]['resource_id'] = 0;
$pages[$postID]['permalink'] = $post->post_name; // this is empty
update_option('screenstepslive_pages', $pages);
}
break;
}
}
// FOR TESTING
//$pages = get_option('screenstepslive_pages');
//print_r($pages);
// UI
echo <<<END
<div class="wrap">
<h2>ScreenSteps Live</h2>
<br />
<fieldset class="options">
<legend>ScreenSteps Live API Information</legend>
END;
// Print API info
$http_option = get_option('screenstepslive_protocol') == 'http' ? ' selected="selected"' : '';
$https_option = get_option('screenstepslive_protocol') == 'https' ? ' selected="selected"' : '';
print ('<form method="post" action="' . $_SERVER["REQUEST_URI"] . '">' . "\n");
print ('<input type="hidden" name="api_submitted" value="1">' . "\n");
print ('<table class="optiontable form-table">');
print ('<tr><th scope="row" style="width:200px;">ScreenSteps Live Domain:</th><td>' .
'<input type="text" name="domain" id="domain" style="width:20em;" value="'. get_option('screenstepslive_domain') . '"></td></tr>');
/*print ('<tr><th scope="row">ScreenSteps Live API Key:</th><td>' .
'<input type="text" name="api_key" id="api_key" value="'. get_option('screenstepslive_api_key') . '"></td></tr>');
*/
print ('<tr><th scope="row">ScreenSteps Live Reader Account username:</th><td>' .
'<input type="text" name="reader_name" id="reader_name" value="'. get_option('screenstepslive_reader_name') . '"></td></tr>');
print ('<tr><th scope="row">ScreenSteps Live Reader Account password:</th><td>' .
'<input type="password" name="reader_password" id="reader_password" value="'. get_option('screenstepslive_reader_password') . '"></td></tr>');
print ('<tr><th scope="row">Protocol:</th><td>' .
'<select name="protocol"><option value="http"'. $http_option . '">HTTP</option>' .
'<option value="https"'. $https_option . '">HTTPS</option></select>' .
'</td></tr>');
print ('</table>');
echo <<<END
<div class="submit">
<input type="submit" id="submit_api_settings" value="Save ScreenSteps Live API Settings" />
</div>
</form>
</fieldset>
<br />
END;
// Print WordPress Pages
echo <<<END
<fieldset class="options">
<legend>WordPress Page Settings</legend>
END;
//print_r($spaces);
if ($spaces) {
if (count($spaces['space']) == 0) {
print "<div>No spaces were returned from the ScreenSteps Live server.</div>";
} else {
// Print FORM and header
print ('<form method="post" action="' . $_SERVER["REQUEST_URI"] . '">' . "\n");
print ('<input type="hidden" name="pages_submitted" value="1">' . "\n");
if (is_array($spaces['space']))
{
echo <<<END
<script>
<!--
var spaces = new Array();
END;
foreach ($spaces['space'] as $space)
{
$space_info = $sslivewp->GetSpace($space['id']);
if (is_array($space_info['assets']))
{
foreach ($space_info['assets']['asset'] as $asset)
{
if (strtolower($asset['type']) == 'manual')
{
$a_spaceID = str_replace("'", "\'", $space['id']);
$a_assetTitle = str_replace("'", "\'", $asset['title']);
$a_assetID = str_replace("'", "\'", $asset['id']);
echo <<<END
spaces.push(new Array('$a_spaceID', '$a_assetTitle', '$a_assetID'));
END;
}
}
}
}
echo <<<END
jQuery(document).ready(function($) {
jQuery('select.spaces').change(function() {
space_id = $(this).val();
page_id = $(this).attr("id").split("_")[1];
SetAssetFromSpace(space_id, page_id);
});
});
function SetAssetFromSpace(space_id, page_id) {
asset_list = jQuery("#assets_" + page_id);
options = '<option value="0">None</option>';
jQuery.each(spaces, function(i, space) {
if (space[0] == space_id) {
options += '<option value="' + space[2] + '">' + space[1] + '</option>';
}
});
jQuery(asset_list).html(options);
}
-->
</script>
END;
}
print ('<table class="optiontable form-table">');
print ('<tr>' . "\n");
print ('<th scope="column" style="width:10px;">Page ID</th>' . "\n");
print ('<th scope="column">Space</th>' . "\n");
print ('<th scope="column">Manual</th>' . "\n");
print ('<th scope="column">Allow Comments</th>' . "\n");
print ('</tr>' . "\n");
$pages = get_option('screenstepslive_pages');
$manuals = array();
$buckets = array();
if (is_array($pages) ) {
foreach ($pages as $key => $page) {
$i = $page['id'];
print ('<tr>' . "\n");
// Page id column
print ('<td width="30px">');
print ('<input type="hidden" name="pages[' . $i . '][id]" id="page_' . $i . '"' . 'value="'. $page['id'] . '"/>' . $page['id']);
print ('</td>' . "\n");
// Spaces select menu column
if (count($spaces['space']) > 0) {
print ('<td><select name="pages[' . $i . '][space_id]' . '" class="spaces" id="space_' . $page['id'] . '">');
foreach ($spaces['space'] as $key => $space) {
// Determine initial state for visible checkbox and permission settings.
if ($space['id'] == $page['space_id']) {
print '<option value="' . $space['id'] . '" selected="selected">' . $space['title'] . '</option>';