PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/screenstepslive.php

https://github.com/bluemango/screensteps-live-wordpress-plugin
PHP | 1145 lines | 825 code | 162 blank | 158 comment | 247 complexity | 0139a1f05a705f8076134f3d382d59e5 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: ScreenSteps Live
  4. Plugin URI: http://screensteps.com/blog/2008/07/screensteps-live-wordpress-plugin/
  5. Description: This plugin will incorporate lessons from your ScreenSteps Live account into your WordPress Pages.
  6. Version: 1.1.3
  7. Author: Blue Mango Learning Systems
  8. Author URI: http://www.screensteps.com
  9. */
  10. // Todo: Look at moving to class approach. Register callbacks within class.
  11. // Also look at registering one callback that is triggered as page is loading. If the page
  12. // is one we are interested in then install all other callbacks.
  13. //$result = error_reporting(E_ERROR|E_WARNING|E_PARSE|E_NOTICE);
  14. // Global var for SSLiveWordPress object. It is shared among multiple wp callbacks.
  15. $screenstepslivewp = NULL;
  16. $screenstepslivePages = NULL;
  17. $screenstepsliveA = NULL;
  18. // This plugin processes content of all posts
  19. add_action('wp_head', 'screenstepslive_addHeader', 100);
  20. add_filter('the_content', 'screenstepslive_parseContent', 100);
  21. add_filter('the_title', 'screenstepslive_parseTitle', 100);
  22. add_filter('wp_list_pages', 'screenstepslive_listPages', 100);
  23. add_filter('wp_list_pages_excludes', 'screenstepslive_listPagesExcludes', 100);
  24. add_filter('delete_post', 'screenstepslive_checkIfDeletedPostIsReferenced', 100);
  25. add_filter('name_save_pre', 'screenstepslive_changePagePermalink', 100);
  26. add_action('admin_menu', 'screenstepslive_addPages');
  27. add_filter('request', 'screenstepslive_query_vars', 100);
  28. add_filter('wp_title', 'screenstepslive_wp_title', 10, 2); // Get in on this early
  29. function screenstepslive_initializeObject()
  30. {
  31. global $screenstepslivewp;
  32. if (!$screenstepslivewp) {
  33. // PROVIDE EXAMPLE SETTINGS AS DEFAULT
  34. if (get_option('screenstepslive_domain') == '' && get_option('screenstepslive_reader_name') == '') {
  35. update_option('screenstepslive_domain', 'example.screenstepslive.com');
  36. update_option('screenstepslive_reader_name', 'example');
  37. update_option('screenstepslive_reader_password', 'example');
  38. update_option('screenstepslive_protocol', 'http');
  39. update_option('screenstepslive_pages', '');
  40. }
  41. require_once(dirname(__FILE__) . '/sslivewordpress_class.php');
  42. // Create ScreenSteps Live object using your domain and API key
  43. $screenstepslivewp = new SSLiveWordPress(get_option('screenstepslive_domain'),
  44. get_option('screenstepslive_protocol'));
  45. $screenstepslivewp->SetUserCredentials(get_option('screenstepslive_reader_name'), get_option('screenstepslive_reader_password'));
  46. $screenstepslivewp->user_can_read_private = current_user_can('read_private_posts') == 1;
  47. }
  48. // Any caller will just get a reference to this object.
  49. return $screenstepslivewp;
  50. }
  51. function screenstepslive_addHeader() {
  52. // Was a comment submitted?
  53. if ($_POST['screenstepslive_comment_submit'] == 1) {
  54. ////////////
  55. $sslivewp = screenstepslive_initializeObject();
  56. $postID = $_POST['sslivecommment']['page_id'];
  57. // Find settings for this page
  58. $pages = get_option('screenstepslive_pages');
  59. foreach ($pages as $key => $page_entry) {
  60. if ($page_entry['id'] == $postID) {
  61. $page = $page_entry;
  62. break;
  63. }
  64. }
  65. // Get out if we have nothing to offer.
  66. if (!isset($page)) wp_die('unable to find page for comment submission');
  67. global $screenstepslivePages;
  68. if (empty($screenstepslivePages)) {
  69. $screenstepslivePages['space'] = empty($page['space_permalink']) ? $page['space_id'] : $page['space_permalink'];
  70. $screenstepslivePages['manual'] = $sslivewp->CleanseID($_GET['manual_id']);
  71. $screenstepslivePages['bucket'] = $sslivewp->CleanseID($_GET['bucket_id']);
  72. $screenstepslivePages['lesson'] = $sslivewp->CleanseID($_GET['lesson_id']);
  73. }
  74. /*$space_id = $page['space_id'];
  75. $manual_id = $sslivewp->CleanseID($_GET['manual_id']);
  76. $bucket_id = $sslivewp->CleanseID($_GET['bucket_id']);
  77. */
  78. if ($page['resource_type'] == 'bucket' &&
  79. ( (is_string($page['resource_id']) && !empty($page['resource_id'])) || (is_int($page['resource_id']) && $page['resource_id'] > 0) )
  80. )
  81. $screenstepslivePages['bucket'] = $page['resource_id'];
  82. else if ($page['resource_type'] == 'manual' &&
  83. ( (is_string($page['resource_id']) && !empty($page['resource_id'])) || (is_int($page['resource_id']) && $page['resource_id'] > 0) )
  84. )
  85. $screenstepslivePages['manual'] = $page['resource_id'];
  86. /* $lesson_id = $sslivewp->CleanseID($_GET['lesson_id']);*/
  87. ////////////
  88. if ($page['resource_type'] == 'manual') {
  89. $resource_type = 'manual';
  90. $resource_id = $screenstepslivePages['manual'];
  91. } else {
  92. $resource_type = 'bucket';
  93. $resource_id = $screenstepslivePages['bucket'];
  94. }
  95. $name = $_POST['sslivecommment']['author'];
  96. $email = $_POST['sslivecommment']['email'];
  97. $comment = $_POST['sslivecommment']['comment'];
  98. if (get_magic_quotes_gpc()) {
  99. $name = stripslashes($name);
  100. $email = stripslashes($email);
  101. $comment = stripslashes($comment);
  102. }
  103. $errors = $sslivewp->SubmitLessonComment($screenstepslivePages['space'], $resource_type, $resource_id, $screenstepslivePages['lesson'],
  104. $name, $email, $comment, $_POST['sslivecommment']['subscribe']);
  105. if ( count($errors) > 0 ) {
  106. foreach ($errors as $key=>$value)
  107. {
  108. $error_str .= '<p>' . $value . '</p>';
  109. }
  110. wp_die($error_str);
  111. }
  112. }
  113. // CSS
  114. $plugin_folder = basename(dirname(__FILE__));
  115. echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/' . $plugin_folder . '/css/screenstepslive.css" />' . "\n";
  116. }
  117. // Support for mod-rewrite
  118. function screenstepslive_query_vars($request)
  119. {
  120. global $screenstepslivePages;
  121. //print_r($request);
  122. /*
  123. Array
  124. (
  125. [page] => /4209
  126. [pagename] => screensteps-live-page/space/4/manual/screensteps/lesson
  127. )
  128. */
  129. $pathParts = explode('/', $request['pagename']);
  130. //print_r($pathParts);
  131. $pages = get_option('screenstepslive_pages');
  132. //print_r($pages);
  133. // Update permalink for page
  134. if (is_array($pages))
  135. {
  136. foreach ($pages as $key => $page_entry) {
  137. $newPageName = '';
  138. foreach ($pathParts as $i => $name) {
  139. if ($name == $page_entry['permalink']) {
  140. if (isset($pathParts[$i+1]))
  141. {
  142. $pageIsSet = isset($request['page']) && !empty($request['page']);
  143. $screenstepslivePages['space'] = $pathParts[$i+1];
  144. if ($pathParts[$i+2] == 'manual') {
  145. // Look for numeric id
  146. if (!isset($pathParts[$i+4]) && $pageIsSet)
  147. $screenstepslivePages['manual'] = substr($request['page'], 1, strlen($request['page'])); // e.g. /4709
  148. else
  149. $screenstepslivePages['manual'] = $pathParts[$i+3];
  150. } else {
  151. // Look for numeric id
  152. if (!isset($pathParts[$i+4]) && $pageIsSet)
  153. $screenstepslivePages['bucket'] = substr($request['page'], 1, strlen($request['page'])); // e.g. /4709
  154. else
  155. $screenstepslivePages['bucket'] = $pathParts[$i+3];
  156. }
  157. if (isset($pathParts[$i+4])) {
  158. if ($pageIsSet)
  159. $screenstepslivePages['lesson'] = substr($request['page'], 1, strlen($request['page'])); // e.g. /4709
  160. else
  161. $screenstepslivePages['lesson'] = $pathParts[$i+4];
  162. }
  163. $screenstepslivePages['lesson'] = explode('-', $screenstepslivePages['lesson']);
  164. $screenstepslivePages['lesson'] = $screenstepslivePages['lesson'][0];
  165. $request['pagename'] = $newPageName . $name;
  166. }
  167. break 2;
  168. }
  169. else
  170. {
  171. // May include a parent page
  172. $newPageName .= $name . '/';
  173. }
  174. }
  175. }
  176. }
  177. //print_r($request);
  178. return $request;
  179. }
  180. function screenstepslive_changePagePermalink($data)
  181. {
  182. $pageID = (int) $_POST['post_ID'];
  183. // Find settings for this page
  184. // Can't find way to determine if revision is being inserted other than this.
  185. // ID doesn't seem to be available anywhere. Silly.
  186. if ($pageID > 0 && $data != ($pageID . '-revision') && $data != ($pageID . '-autosave'))
  187. {
  188. $pages = get_option('screenstepslive_pages');
  189. // Update permalink for page
  190. foreach ($pages as $key => $page_entry) {
  191. if ($page_entry['id'] == $pageID) {
  192. if (empty($data)) {
  193. // WordPress hasn't assigned permalink yet. Grab one.
  194. $data = get_sample_permalink($pageID);
  195. $data = $data[1];
  196. }
  197. $pages[$key]['permalink'] = $data;
  198. update_option('screenstepslive_pages', $pages);
  199. break;
  200. }
  201. }
  202. }
  203. return $data;
  204. }
  205. function screenstepslive_listPagesExcludes($the_output) {
  206. // Set flag that WP is listing pages
  207. global $screenstepsliveA;
  208. $screenstepsliveA['listing pages'] = true;
  209. return ($the_output);
  210. }
  211. function screenstepslive_listPages($the_output) {
  212. // unset flag that turns off title parsing
  213. global $screenstepsliveA;
  214. $screenstepsliveA['listing pages'] = false;
  215. // We remove the link to the current SS Live page from the list. It's $title will be rewritten
  216. // by screenstepslive_parseTitle and since WordPress has one filter for ALL titles we don't have
  217. // a lot of options.
  218. global $screenstepslivePages;
  219. $postID = get_the_ID();
  220. $post = &get_post($postID);
  221. // Find settings for this page
  222. $pages = get_option('screenstepslive_pages');
  223. foreach ($pages as $key => $page_entry) {
  224. if ($page_entry['id'] == $post->ID) {
  225. $page = $page_entry;
  226. break;
  227. }
  228. }
  229. // Get out if we have nothing to offer.
  230. if (!isset($page)) return ($the_output);
  231. // Include necessary SS Live files
  232. $sslivewp = screenstepslive_initializeObject();
  233. if (empty($screenstepslivePages)) {
  234. $screenstepslivePages['space'] = empty($page['space_permalink']) ? $page['space_id'] : $page['space_permalink'];
  235. $screenstepslivePages['manual'] = $sslivewp->CleanseID($_GET['manual_id']);
  236. $screenstepslivePages['bucket'] = $sslivewp->CleanseID($_GET['bucket_id']);
  237. $screenstepslivePages['lesson'] = $sslivewp->CleanseID($_GET['lesson_id']);
  238. }
  239. /*$space_id = $page['space_id'];
  240. $manual_id = $sslivewp->CleanseID($_GET['manual_id']);
  241. $bucket_id = $sslivewp->CleanseID($_GET['bucket_id']);*/
  242. if ($page['resource_type'] == 'bucket' && $page['resource_id'] > 0)
  243. $screenstepslivePages['bucket'] = $page['resource_id'];
  244. else if ($page['resource_type'] == 'manual' && $page['resource_id'] > 0)
  245. $screenstepslivePages['manual'] = $page['resource_id'];
  246. /*$lesson_id = $sslivewp->CleanseID($_GET['lesson_id']);*/
  247. // What has this page been renamed too?
  248. if (empty($screenstepslivePages['space']))
  249. {
  250. // nothing to do
  251. } else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['lesson'])) {
  252. if (!empty( $screenstepslivePages['manual'] )) {
  253. if ($page['resource_id'] == 0)
  254. {
  255. // Page is a 'space' page.
  256. $the_title = '<a href="' . $sslivewp->GetLinkToManual($post->ID, $screenstepslivePages['space'], $screenstepslivePages['manual']) . '">' .
  257. $sslivewp->GetManualTitle($screenstepslivePages['space'], $screenstepslivePages['manual']) . '</a>: ' .
  258. $sslivewp->GetManualLessonTitle($screenstepslivePages['space'], $screenstepslivePages['manual'], $screenstepslivePages['lesson']);
  259. } else
  260. {
  261. // Page is a 'manual' page.
  262. $the_title = $sslivewp->GetManualLessonTitle($screenstepslivePages['space'], $screenstepslivePages['manual'], $screenstepslivePages['lesson']);
  263. }
  264. } else if (!empty( $screenstepslivePages['bucket'])) {
  265. if ($page['resource_id'] == 0)
  266. {
  267. // Page is a 'space' page.
  268. $the_title = '<a href="' . $sslivewp->GetLinkToBucket($post->ID, $screenstepslivePages['space'], $screenstepslivePages['bucket']) . '">' .
  269. $sslivewp->GetBucketTitle($screenstepslivePages['space'], $screenstepslivePages['bucket']) . '</a>: ' .
  270. $sslivewp->GetBucketLessonTitle($screenstepslivePages['space'], $screenstepslivePages['bucket'], $screenstepslivePages['lesson']);
  271. } else
  272. {
  273. // Page is a 'manual' page.
  274. $the_title = $sslivewp->GetBucketLessonTitle($screenstepslivePages['space'], $screenstepslivePages['bucket'], $screenstepslivePages['lesson']);
  275. }
  276. }
  277. } else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['manual'])) {
  278. if ($page['resource_id'] == 0)
  279. {
  280. // Page is a 'space' page.
  281. $the_title = $sslivewp->GetManualTitle($screenstepslivePages['space'], $screenstepslivePages['manual']);
  282. } else
  283. {
  284. // Page is a 'manual' page.
  285. // Nothing to do.
  286. }
  287. } else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['bucket'])) {
  288. if ($page['resource_id'] == 0)
  289. {
  290. // Page is a 'space' page.
  291. $the_title = $sslivewp->GetBucketTitle($screenstepslivePages['space'], $screenstepslivePages['bucket']);
  292. } else
  293. {
  294. // Page is a 'bucket' page.
  295. // Nothing to do.
  296. }
  297. } else {
  298. // Spaces. Not used.
  299. }
  300. if (empty($the_title) || $the_title == $post->post_title) {
  301. return ($the_output);
  302. } else {
  303. // Now rename the page in the list to the original post title
  304. if ( preg_match( '/>' . preg_quote($the_title, "/") . '\</', $the_output, $matches, PREG_OFFSET_CAPTURE) ) {
  305. $the_output = substr_replace( $the_output, '>' . $post->post_title . '<', $matches[0][1], strlen($matches[0][0]) );
  306. }
  307. return ($the_output);
  308. }
  309. }
  310. function screenstepslive_wp_title($the_title, $sep)
  311. {
  312. global $screenstepslivePages;
  313. $postID = get_the_ID();
  314. $post = &get_post($postID);
  315. // Find settings for this page
  316. $pages = get_option('screenstepslive_pages');
  317. foreach ($pages as $key => $page_entry) {
  318. if ($page_entry['id'] == $post->ID) {
  319. $page = $page_entry;
  320. break;
  321. }
  322. }
  323. // Get out if we have nothing to offer.
  324. if (!isset($page)) return ($the_title);
  325. // Include necessary SS Live files
  326. $sslivewp = screenstepslive_initializeObject();
  327. if (empty($screenstepslivePages)) {
  328. $screenstepslivePages['space'] = empty($page['space_permalink']) ? $page['space_id'] : $page['space_permalink'];
  329. $screenstepslivePages['manual'] = $sslivewp->CleanseID($_GET['manual_id']);
  330. $screenstepslivePages['bucket'] = $sslivewp->CleanseID($_GET['bucket_id']);
  331. $screenstepslivePages['lesson'] = $sslivewp->CleanseID($_GET['lesson_id']);
  332. }
  333. if ($page['resource_type'] == 'bucket' && $page['resource_id'] > 0)
  334. $screenstepslivePages['bucket'] = $page['resource_id'];
  335. else if ($page['resource_type'] == 'manual' && $page['resource_id'] > 0)
  336. $screenstepslivePages['manual'] = $page['resource_id'];
  337. if (empty($screenstepslivePages['space']))
  338. {
  339. // nothing to do
  340. } else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['lesson'])) {
  341. // Displaying a lesson
  342. if (!empty($screenstepslivePages['manual'])) {
  343. if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
  344. {
  345. // Default page is a space.
  346. // space: manual: lesson
  347. $the_title = $sslivewp->GetManualLessonTitle($screenstepslivePages['space'], $screenstepslivePages['manual'], $screenstepslivePages['lesson']);
  348. } else
  349. {
  350. // Default page is a manual.
  351. // spacemanual: lesson
  352. $the_title = $sslivewp->GetManualLessonTitle($screenstepslivePages['space'], $screenstepslivePages['manual'], $screenstepslivePages['lesson']);
  353. }
  354. } else if (!empty($screenstepslivePages['bucket'])) {
  355. if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
  356. {
  357. // Default page is a space.
  358. // space: manual: lesson
  359. $the_title = $sslivewp->GetBucketLessonTitle($screenstepslivePages['space'], $screenstepslivePages['bucket'], $screenstepslivePages['lesson']);
  360. } else
  361. {
  362. // bucket: lesson
  363. // Default page is a bucket.
  364. $the_title = $sslivewp->GetBucketLessonTitle($screenstepslivePages['space'], $screenstepslivePages['bucket'], $screenstepslivePages['lesson']);
  365. }
  366. }
  367. } else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['manual'])) {
  368. // Displaying a manual
  369. if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
  370. {
  371. // Default page is a space. Get manual title.
  372. // space: manual
  373. $the_title = $sslivewp->GetManualTitle($screenstepslivePages['space'], $screenstepslivePages['manual']);
  374. } else
  375. {
  376. // Default page is a manual. Nothing to do.
  377. }
  378. } else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['bucket'])) {
  379. // Displaying a bucket
  380. if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
  381. {
  382. // Default page is a space. Get bucket title.
  383. // bucket
  384. $the_title = $sslivewp->GetBucketTitle($screenstepslivePages['space'], $screenstepslivePages['bucket']);
  385. } else
  386. {
  387. // Default page is a bucket. Nothing to do.
  388. }
  389. } else {
  390. // Spaces. Not used.
  391. }
  392. if (!empty($sep)) $the_title .= ' ' . $sep;
  393. return ($the_title);
  394. }
  395. function screenstepslive_parseTitle($the_title) {
  396. if (!is_page( $the_title)) return ($the_title); // cursed wp_list_pages calls this as well.
  397. global $screenstepsliveA;
  398. // Get out if WP is listing pages
  399. if ($screenstepsliveA['listing pages'] === true) return ($the_title);
  400. global $screenstepslivePages;
  401. $postID = get_the_ID();
  402. $post = &get_post($postID);
  403. // Find settings for this page
  404. $pages = get_option('screenstepslive_pages');
  405. foreach ($pages as $key => $page_entry) {
  406. if ($page_entry['id'] == $post->ID) {
  407. $page = $page_entry;
  408. break;
  409. }
  410. }
  411. // Get out if we have nothing to offer.
  412. if (!isset($page)) return ($the_title);
  413. // Include necessary SS Live files
  414. $sslivewp = screenstepslive_initializeObject();
  415. if (empty($screenstepslivePages)) {
  416. $screenstepslivePages['space'] = empty($page['space_permalink']) ? $page['space_id'] : $page['space_permalink'];
  417. $screenstepslivePages['manual'] = $sslivewp->CleanseID($_GET['manual_id']);
  418. $screenstepslivePages['bucket'] = $sslivewp->CleanseID($_GET['bucket_id']);
  419. $screenstepslivePages['lesson'] = $sslivewp->CleanseID($_GET['lesson_id']);
  420. }
  421. if ($page['resource_type'] == 'bucket' && $page['resource_id'] > 0)
  422. $screenstepslivePages['bucket'] = $page['resource_id'];
  423. else if ($page['resource_type'] == 'manual' && $page['resource_id'] > 0)
  424. $screenstepslivePages['manual'] = $page['resource_id'];
  425. if (empty($screenstepslivePages['space']))
  426. {
  427. // nothing to do
  428. } else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['lesson'])) {
  429. // Displaying a lesson
  430. if (!empty($screenstepslivePages['manual'])) {
  431. if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
  432. {
  433. // Default page is a space.
  434. // space: manual: lesson
  435. $the_title = '<a href="' . $sslivewp->GetLinkToSpace($post->ID, $screenstepslivePages['space']) . '">' .
  436. $the_title . '</a>: ' .
  437. '<a href="' . $sslivewp->GetLinkToManual($post->ID, $screenstepslivePages['space'], $screenstepslivePages['manual']) . '">' .
  438. $sslivewp->GetManualTitle($screenstepslivePages['space'], $screenstepslivePages['manual']) . '</a>: ' .
  439. $sslivewp->GetManualLessonTitle($screenstepslivePages['space'], $screenstepslivePages['manual'], $screenstepslivePages['lesson']);
  440. } else
  441. {
  442. // Default page is a manual.
  443. // spacemanual: lesson
  444. $the_title = '<a href="' . $sslivewp->GetLinkToManual($post->ID, $screenstepslivePages['space'], $screenstepslivePages['manual']) . '">' .
  445. $the_title . '</a>: ' .
  446. $sslivewp->GetManualLessonTitle($screenstepslivePages['space'], $screenstepslivePages['manual'], $screenstepslivePages['lesson']);
  447. }
  448. } else if (!empty($screenstepslivePages['bucket'])) {
  449. if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
  450. {
  451. // Default page is a space.
  452. // space: manual: lesson
  453. $the_title = '<a href="' . $sslivewp->GetLinkToBucket($post->ID, $screenstepslivePages['space'], $screenstepslivePages['bucket']) . '">' .
  454. $sslivewp->GetBucketTitle($screenstepslivePages['space'], $screenstepslivePages['bucket']) . '</a>: ' .
  455. $sslivewp->GetBucketLessonTitle($screenstepslivePages['space'], $screenstepslivePages['bucket'], $screenstepslivePages['lesson']);
  456. } else
  457. {
  458. // bucket: lesson
  459. // Default page is a bucket.
  460. $the_title = $sslivewp->GetBucketLessonTitle($screenstepslivePages['space'], $screenstepslivePages['bucket'], $screenstepslivePages['lesson']);
  461. }
  462. }
  463. } else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['manual'])) {
  464. // Displaying a manual
  465. if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
  466. {
  467. // Default page is a space. Get manual title.
  468. // space: manual
  469. $the_title = '<a href="' . $sslivewp->GetLinkToSpace($post->ID, $screenstepslivePages['space']) . '">' .
  470. $the_title . '</a>: ' .
  471. $sslivewp->GetManualTitle($screenstepslivePages['space'], $screenstepslivePages['manual']);
  472. } else
  473. {
  474. // Default page is a manual. Nothing to do.
  475. }
  476. } else if (!empty($screenstepslivePages['space']) && !empty($screenstepslivePages['bucket'])) {
  477. // Displaying a bucket
  478. if (is_numeric($page['resource_id']) && $page['resource_id'] == 0)
  479. {
  480. // Default page is a space. Get bucket title.
  481. // bucket
  482. $the_title = $sslivewp->GetBucketTitle($screenstepslivePages['space'], $screenstepslivePages['bucket']);
  483. } else
  484. {
  485. // Default page is a bucket. Nothing to do.
  486. }
  487. } else {
  488. // Spaces. Not used.
  489. }
  490. return ($the_title);
  491. }
  492. // Called by WordPress to process content
  493. function screenstepslive_parseContent($the_content)
  494. {
  495. global $screenstepslivePages;
  496. $postID = get_the_ID();
  497. $post = &get_post($postID);
  498. if (stristr($the_content, '{{SCREENSTEPSLIVE_CONTENT}}') !== FALSE) {
  499. $text = NULL;
  500. $the_content = str_ireplace('<p>', '', $the_content);
  501. $the_content = str_ireplace('</p>', '', $the_content);
  502. $the_content = '<div class="screenstepslive">' . "\n" . $the_content . '</div>';
  503. // Find settings for this page
  504. $pages = get_option('screenstepslive_pages');
  505. foreach ($pages as $key => $page_entry) {
  506. if ($page_entry['id'] == $post->ID) {
  507. $page = $page_entry;
  508. break;
  509. }
  510. }
  511. // Get out if we have nothing to offer.
  512. if (!isset($page)) return false;
  513. //print_r($page);
  514. // Include necessary SS Live files
  515. $sslivewp = screenstepslive_initializeObject();
  516. if (empty($screenstepslivePages)) {
  517. $space_id = empty($page['space_permalink']) ? $page['space_id'] : $page['space_permalink'];
  518. $manual_id = $sslivewp->CleanseID($_GET['manual_id']);
  519. $bucket_id = $sslivewp->CleanseID($_GET['bucket_id']);
  520. $lesson_id = $sslivewp->CleanseID($_GET['lesson_id']);
  521. } else {
  522. $space_id = $screenstepslivePages['space'];
  523. $manual_id = $screenstepslivePages['manual'];
  524. $bucket_id = $screenstepslivePages['bucket'];
  525. $lesson_id = $screenstepslivePages['lesson'];
  526. }
  527. if ($page['resource_type'] == 'bucket' &&
  528. ( (is_string($page['resource_id']) && !empty($page['resource_id'])) || (is_int($page['resource_id']) && $page['resource_id'] > 0) )
  529. )
  530. $bucket_id = $page['resource_id'];
  531. else if ($page['resource_type'] == 'manual' &&
  532. ( (is_string($page['resource_id']) && !empty($page['resource_id'])) || (is_int($page['resource_id']) && $page['resource_id'] > 0) )
  533. )
  534. $manual_id = $page['resource_id'];
  535. ////////////////////////
  536. ////////////////////////
  537. if (empty($space_id))
  538. {
  539. // Retrieve list of all spaces
  540. $text = $sslivewp->GetSpacesList($post->ID);
  541. } else if (!empty($space_id) && !empty($lesson_id)) {
  542. if ($page['resource_type'] == 'manual') {
  543. $resource_type = 'manual';
  544. $resource_id = $manual_id;
  545. } else {
  546. $resource_type = 'bucket';
  547. $resource_id = $bucket_id;
  548. }
  549. // What content to display?
  550. if (!empty($manual_id)) {
  551. $max_len = 30;
  552. $next_title = $sslivewp->GetNextLessonTitle($space_id, 'manual', $manual_id, $lesson_id);
  553. $prev_title = $sslivewp->GetPrevLessonTitle($space_id, 'manual', $manual_id, $lesson_id);
  554. if (strlen(utf8_decode($next_title)) > $max_len) $next_title = screenstepslive_utf8_substr($next_title, 0, $max_len-1) . '...';
  555. if (strlen(utf8_decode($prev_title)) > $max_len) $prev_title = screenstepslive_utf8_substr($prev_title, 0, $max_len-1) . '...';
  556. $next_link = $sslivewp->GetLinkToNextLesson($post->ID, $space_id, 'manual', $manual_id, $lesson_id, $next_title . ' >'); // 'Next Lesson');
  557. $prev_link = $sslivewp->GetLinkToPrevLesson($post->ID, $space_id, 'manual', $manual_id, $lesson_id, '< ' . $prev_title); //'Previous Lesson');
  558. $text = '<div class="screenstepslive_navigation">' . "\n";
  559. if ($prev_link != '')
  560. $text .= '<div class="alignleft">' . $prev_link . '</div>' . "\n";
  561. if (!empty($next_link))
  562. $text .= '<div class="alignright">' . $next_link . '</div>' . "\n";
  563. $text .= '<div class="screenstepslive_nav_bottom"></div>';
  564. $text .= '</div>';
  565. $text .= $sslivewp->GetLessonHTML($space_id, 'manual', $manual_id, $lesson_id);
  566. $text .= '<div class="screenstepslive_navigation">' . "\n";
  567. if (!empty($prev_link))
  568. $text .= '<div class="alignleft">' . $prev_link . '</div>' . "\n";
  569. if (!empty($next_link))
  570. $text .= '<div class="alignright">' . $next_link . '</div>' . "\n";
  571. $text .= '<div class="screenstepslive_nav_bottom"></div>';
  572. $text .= '</div>';
  573. } else if (!empty($bucket_id)) {
  574. $text = $sslivewp->GetLessonHTML($space_id, 'bucket', $bucket_id, $lesson_id);
  575. }
  576. if ($page['allow_comments']) {
  577. $text .= $sslivewp->GetLessonComments($post->ID, $space_id, $resource_type, $resource_id, $lesson_id);
  578. }
  579. } else if (!empty($space_id) && !empty($manual_id)) {
  580. $text = $sslivewp->GetManualList($post->ID, $space_id, $manual_id);
  581. } else if (!empty($space_id) && !empty($bucket_id)) {
  582. $text = $sslivewp->GetBucketList($post->ID, $space_id, $bucket_id);
  583. } else {
  584. $text = $sslivewp->GetSpaceList($post->ID, $space_id);
  585. }
  586. }
  587. if (!is_null($text)) {
  588. // case insensitive search/replace in PHP 5. preg_replace breaks if
  589. // $1 or \\1 is in the string. This is the happy compromise.
  590. if ( preg_match( '/{{SCREENSTEPSLIVE_CONTENT}}/i', $the_content, $matches, PREG_OFFSET_CAPTURE) ) {
  591. $the_content = substr_replace( $the_content, $text, $matches[0][1], strlen($matches[0][0]) );
  592. }
  593. //$the_content = preg_replace('/{{SCREENSTEPSLIVE_CONTENT}}/i', $text, $the_content);
  594. }
  595. return $the_content;
  596. }
  597. function screenstepslive_utf8_substr($str,$start)
  598. {
  599. preg_match_all("/./u", $str, $ar);
  600. if(func_num_args() >= 3) {
  601. $end = func_get_arg(2);
  602. return join("",array_slice($ar[0],$start,$end));
  603. } else {
  604. return join("",array_slice($ar[0],$start));
  605. }
  606. }
  607. function screenstepslive_checkIfDeletedPostIsReferenced($postID) {
  608. $pages = get_option('screenstepslive_pages');
  609. if (is_array($pages))
  610. {
  611. foreach ($pages as $i => $value) {
  612. if ($pages[$i]['id'] == $postID) {
  613. unset($pages[$i]);
  614. }
  615. }
  616. update_option('screenstepslive_pages', $pages); // array_values reindexes
  617. //update_option('screenstepslive_pages', array_values($pages)); // array_values reindexes
  618. }
  619. }
  620. // Use to replace page title
  621. //$the_content = preg_replace('/{{SCREENSTEPSLIVE_LESSON_TITLE}}/i', $sslivewp->GetLessonTitle($manual_id, $lesson_id), $the_content);
  622. // Add admin page
  623. function screenstepslive_addPages()
  624. {
  625. add_options_page('ScreenSteps Live Options', 'ScreenSteps Live', 8, __FILE__, 'screenstepslive_optionPage');
  626. }
  627. // Shows Admin page
  628. function screenstepslive_optionPage()
  629. {
  630. $sslivewp = screenstepslive_initializeObject();
  631. $form_submitted = false; // So we don't create pages twice.
  632. // API form was submitted
  633. if ($_POST['api_submitted'] == 1) {
  634. if (get_option('screenstepslive_domain') != $_POST['domain']) {
  635. // Reset caches as account probably changed.
  636. $pages = get_option('screenstepslive_pages');
  637. if (is_array($pages) ) {
  638. foreach ($pages as $key => $page) {
  639. $pages[$key]['space_id'] = '';
  640. $pages[$key]['resource_type'] = '';
  641. $pages[$key]['resource_id'] = '';
  642. }
  643. update_option('screenstepslive_pages', $pages);
  644. }
  645. }
  646. update_option('screenstepslive_domain', $_POST['domain']);
  647. update_option('screenstepslive_reader_name', $_POST['reader_name']);
  648. update_option('screenstepslive_reader_password', $_POST['reader_password']);
  649. update_option('screenstepslive_protocol', $_POST['protocol']);
  650. $sslivewp->domain = $_POST['domain'];
  651. $sslivewp->SetUserCredentials($_POST['reader_name'], $_POST['reader_password']);
  652. $sslivewp->protocol = $_POST['protocol'];
  653. $form_submitted = true;
  654. }
  655. // Get list of spaces from SS Live
  656. $spaces = $sslivewp->GetSpaces();
  657. // Manuals form was submited
  658. if ($_POST['pages_submitted'] == 1 && is_array($_POST['pages'])) {
  659. // Loop through posted pages, making sure they still exist. User could have deleted one.
  660. $pages = get_option('screenstepslive_pages');
  661. //print_r($_POST['pages']);
  662. //print_r($pages);
  663. foreach ($_POST['pages'] as $page_id => $new_page) {
  664. if (isset($pages[$page_id])) {
  665. if ($pages[$page_id]['space_id'] != $new_page['space_id']) {
  666. $pages[$page_id]['resource_id'] = 0;
  667. } else {
  668. $pages[$page_id]['resource_id'] = $new_page['resource_id'];
  669. }
  670. $pages[$page_id]['space_id'] = $new_page['space_id'];
  671. $pages[$page_id]['resource_type'] = 'manual';
  672. $pages[$page_id]['allow_comments'] = ($new_page['allow_comments'] == 'on') ? 1 : 0;
  673. // Find permalink
  674. foreach($spaces['space'] as $space) {
  675. if ($pages[$page_id]['space_id'] == $space['id']) {
  676. $pages[$page_id]['space_permalink'] = $space['permalink'];
  677. break;
  678. }
  679. }
  680. }
  681. }
  682. // print_r($pages);
  683. update_option('screenstepslive_pages', $pages);
  684. $form_submitted = true;
  685. }
  686. // Create template pages
  687. if (!$form_submitted && isset($_GET['ssliveaction'])) {
  688. switch ($_GET['ssliveaction']) {
  689. case 'create_page':
  690. $postID = screenstepslive_createTemplatePage();
  691. if (intval($postID) > 0) {
  692. $post = &get_post($postID);
  693. $pages = get_option('screenstepslive_pages');
  694. $pages[$postID]['id'] = $postID;
  695. $pages[$postID]['space_id'] = $spaces['space'][0]['id'];
  696. $pages[$postID]['space_permalink'] = $spaces['space'][0]['permalink'];
  697. $pages[$postID]['resource_type'] = 'manual';
  698. $pages[$postID]['resource_id'] = 0;
  699. $pages[$postID]['permalink'] = $post->post_name; // this is empty
  700. update_option('screenstepslive_pages', $pages);
  701. }
  702. break;
  703. }
  704. }
  705. // FOR TESTING
  706. //$pages = get_option('screenstepslive_pages');
  707. //print_r($pages);
  708. // UI
  709. echo <<<END
  710. <div class="wrap">
  711. <h2>ScreenSteps Live</h2>
  712. <br />
  713. <fieldset class="options">
  714. <legend>ScreenSteps Live API Information</legend>
  715. END;
  716. // Print API info
  717. $http_option = get_option('screenstepslive_protocol') == 'http' ? ' selected="selected"' : '';
  718. $https_option = get_option('screenstepslive_protocol') == 'https' ? ' selected="selected"' : '';
  719. print ('<form method="post" action="' . $_SERVER["REQUEST_URI"] . '">' . "\n");
  720. print ('<input type="hidden" name="api_submitted" value="1">' . "\n");
  721. print ('<table class="optiontable form-table">');
  722. print ('<tr><th scope="row" style="width:200px;">ScreenSteps Live Domain:</th><td>' .
  723. '<input type="text" name="domain" id="domain" style="width:20em;" value="'. get_option('screenstepslive_domain') . '"></td></tr>');
  724. /*print ('<tr><th scope="row">ScreenSteps Live API Key:</th><td>' .
  725. '<input type="text" name="api_key" id="api_key" value="'. get_option('screenstepslive_api_key') . '"></td></tr>');
  726. */
  727. print ('<tr><th scope="row">ScreenSteps Live Reader Account username:</th><td>' .
  728. '<input type="text" name="reader_name" id="reader_name" value="'. get_option('screenstepslive_reader_name') . '"></td></tr>');
  729. print ('<tr><th scope="row">ScreenSteps Live Reader Account password:</th><td>' .
  730. '<input type="password" name="reader_password" id="reader_password" value="'. get_option('screenstepslive_reader_password') . '"></td></tr>');
  731. print ('<tr><th scope="row">Protocol:</th><td>' .
  732. '<select name="protocol"><option value="http"'. $http_option . '">HTTP</option>' .
  733. '<option value="https"'. $https_option . '">HTTPS</option></select>' .
  734. '</td></tr>');
  735. print ('</table>');
  736. echo <<<END
  737. <div class="submit">
  738. <input type="submit" id="submit_api_settings" value="Save ScreenSteps Live API Settings" />
  739. </div>
  740. </form>
  741. </fieldset>
  742. <br />
  743. END;
  744. // Print WordPress Pages
  745. echo <<<END
  746. <fieldset class="options">
  747. <legend>WordPress Page Settings</legend>
  748. END;
  749. //print_r($spaces);
  750. if ($spaces) {
  751. if (count($spaces['space']) == 0) {
  752. print "<div>No spaces were returned from the ScreenSteps Live server.</div>";
  753. } else {
  754. // Print FORM and header
  755. print ('<form method="post" action="' . $_SERVER["REQUEST_URI"] . '">' . "\n");
  756. print ('<input type="hidden" name="pages_submitted" value="1">' . "\n");
  757. if (is_array($spaces['space']))
  758. {
  759. echo <<<END
  760. <script>
  761. <!--
  762. var spaces = new Array();
  763. END;
  764. foreach ($spaces['space'] as $space)
  765. {
  766. $space_info = $sslivewp->GetSpace($space['id']);
  767. if (is_array($space_info['assets']))
  768. {
  769. foreach ($space_info['assets']['asset'] as $asset)
  770. {
  771. if (strtolower($asset['type']) == 'manual')
  772. {
  773. $a_spaceID = str_replace("'", "\'", $space['id']);
  774. $a_assetTitle = str_replace("'", "\'", $asset['title']);
  775. $a_assetID = str_replace("'", "\'", $asset['id']);
  776. echo <<<END
  777. spaces.push(new Array('$a_spaceID', '$a_assetTitle', '$a_assetID'));
  778. END;
  779. }
  780. }
  781. }
  782. }
  783. echo <<<END
  784. jQuery(document).ready(function($) {
  785. jQuery('select.spaces').change(function() {
  786. space_id = $(this).val();
  787. page_id = $(this).attr("id").split("_")[1];
  788. SetAssetFromSpace(space_id, page_id);
  789. });
  790. });
  791. function SetAssetFromSpace(space_id, page_id) {
  792. asset_list = jQuery("#assets_" + page_id);
  793. options = '<option value="0">None</option>';
  794. jQuery.each(spaces, function(i, space) {
  795. if (space[0] == space_id) {
  796. options += '<option value="' + space[2] + '">' + space[1] + '</option>';
  797. }
  798. });
  799. jQuery(asset_list).html(options);
  800. }
  801. -->
  802. </script>
  803. END;
  804. }
  805. print ('<table class="optiontable form-table">');
  806. print ('<tr>' . "\n");
  807. print ('<th scope="column" style="width:10px;">Page ID</th>' . "\n");
  808. print ('<th scope="column">Space</th>' . "\n");
  809. print ('<th scope="column">Manual</th>' . "\n");
  810. print ('<th scope="column">Allow Comments</th>' . "\n");
  811. print ('</tr>' . "\n");
  812. $pages = get_option('screenstepslive_pages');
  813. $manuals = array();
  814. $buckets = array();
  815. if (is_array($pages) ) {
  816. foreach ($pages as $key => $page) {
  817. $i = $page['id'];
  818. print ('<tr>' . "\n");
  819. // Page id column
  820. print ('<td width="30px">');
  821. print ('<input type="hidden" name="pages[' . $i . '][id]" id="page_' . $i . '"' . 'value="'. $page['id'] . '"/>' . $page['id']);
  822. print ('</td>' . "\n");
  823. // Spaces select menu column
  824. if (count($spaces['space']) > 0) {
  825. print ('<td><select name="pages[' . $i . '][space_id]' . '" class="spaces" id="space_' . $page['id'] . '">');
  826. foreach ($spaces['space'] as $key => $space) {
  827. // Determine initial state for visible checkbox and permission settings.
  828. if ($space['id'] == $page['space_id']) {
  829. print '<option value="' . $space['id'] . '" selected="selected">' . $space['title'] . '</option>';
  830. } else {
  831. print '<option value="' . $space['id'] . '">' . $space['title'] . '</option>';
  832. }
  833. }
  834. print('</select></td>' . "\n");
  835. } else {
  836. print ('<td>None</td>' . "\n");
  837. }
  838. // Manual select menu column
  839. if ( ( is_int( $page['space_id'] ) && $page['space_id'] > 0) ||
  840. ( !is_int( $page['space_id'] ) && !empty( $page['space_id'] ) ) ) {
  841. if (!isset($manuals[ $page['space_id'] ])) {
  842. $space = $sslivewp->GetSpace($page['space_id']);
  843. if (is_array($space['assets']['asset'])) {
  844. foreach ($space['assets']['asset'] as $asset) {
  845. if (strtolower($asset['type']) == 'manual') {
  846. $manuals[ $page['space_id'] ][] = array('id'=>$asset['id'], 'title'=>$asset['title']);
  847. } elseif (strtolower($asset['type']) == 'bucket') {
  848. $buckets[ $page['space_id'] ][] = array('id'=>$asset['id'], 'title'=>$asset['title']);
  849. }
  850. }
  851. }
  852. }
  853. if (count($manuals[ $page['space_id'] ]) > 0) {
  854. print ('<td><select name="pages[' . $i . '][resource_id]' . '" class="assets" id="assets_' . $page['id'] . '">');
  855. print ('<option value="0">None</option>');
  856. foreach ($manuals[ $page['space_id'] ] as $manual) {
  857. // Determine initial state for visible checkbox and permission settings.
  858. if ($manual['id'] == $page['resource_id']) {
  859. print '<option value="' . $manual['id'] . '" selected="selected">' . $manual['title'] . '</option>';
  860. } else {
  861. print '<option value="' . $manual['id'] . '">' . $manual['title'] . '</option>';
  862. }
  863. }
  864. print('</select></td>' . "\n");
  865. } else {
  866. print ('<input type="hidden" name="pages[' . $i . '][resource_id]" value="0" />');
  867. print ('<td>No manuals in space</td>' . "\n");
  868. }
  869. } else {
  870. print ('<input type="hidden" name="pages[' . $i . '][resource_id]" value="0" />');
  871. print ('<td>None</td>' . "\n");
  872. }
  873. $checked = ($page['allow_comments'] == 1) ? 'checked' : '';
  874. print ('<td><input type="checkbox" name="pages[' . $i . '][allow_comments]" ' . $checked . '></td>' . "\n");
  875. print ('</tr>' . "\n");
  876. }
  877. }
  878. print ('<tr><td colspan="4">');
  879. print ('<p><a href="' . $_SERVER["REQUEST_URI"] . '&ssliveaction=create_page">Create ScreenSteps Live Page</a></p>');
  880. print ('</td></tr>');
  881. print ("</table>\n");
  882. echo <<<END
  883. <div class="submit">
  884. <input type="submit" id="submit_page_settings" value="Save WordPress Page Settings"/>
  885. </div>
  886. </form>
  887. END;
  888. }
  889. } else {
  890. print ("<div>Error:" . $sslivewp->last_error . "</div>\n");
  891. }
  892. echo <<<END
  893. </fieldset>
  894. <br />
  895. END;
  896. }
  897. function screenstepslive_createTemplatePage($type='') {
  898. if (!current_user_can( 'edit_others_pages' )) {
  899. return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
  900. }
  901. $user = wp_get_current_user();
  902. $post['post_author'] = $user->id;
  903. $post['post_type'] = 'page';
  904. $post['post_status'] = 'draft';
  905. $post['comment_status'] = 'closed';
  906. $post['ping_status'] = 'closed';
  907. switch($type) {
  908. case 'spaces':
  909. $post['post_title'] = 'Spaces';
  910. $post['post_content'] = '{{SCREENSTEPSLIVE_CONTENT}}';
  911. break;
  912. case 'space':
  913. $post['post_title'] = '{{SCREENSTEPSLIVE_SPACE_TITLE}}';
  914. $post['post_content'] = '{{SCREENSTEPSLIVE_CONTENT}}' . "\n" .
  915. '<a href="{{SCREENSTEPSLIVE_LINK_TO_SPACES_INDEX}}">Return to spaces</a>';
  916. break;
  917. case 'manual':
  918. $post['post_title'] = '{{SCREENSTEPSLIVE_MANUAL_TITLE}}';
  919. $post['post_content'] = '{{SCREENSTEPSLIVE_CONTENT}}' . "\n" .
  920. '<a href="{{SCREENSTEPSLIVE_LINK_TO_SPACE}}">Return to space</a>';
  921. break;
  922. case 'bucket':
  923. $post['post_title'] = '{{SCREENSTEPSLIVE_BUCKET_TITLE}}';
  924. $post['post_content'] = '{{SCREENSTEPSLIVE_CONTENT}}' . "\n" .
  925. '<a href="{{SCREENSTEPSLIVE_LINK_TO_SPACE}}">Return to space</a>';
  926. break;
  927. case 'lesson':
  928. $post['post_title'] = '{{SCREENSTEPSLIVE_LESSON_TITLE}}';
  929. $post['post_content'] = '{{SCREENSTEPSLIVE_CONTENT}}' . "\n" .
  930. '{{SCREENSTEPSLIVE_LINK_TO_PREV_LESSON text="Previous Lesson: {{SCREENSTEPSLIVE_PREV_LESSON_TITLE}}"}}' . "\n" .
  931. '{{SCREENSTEPSLIVE_LINK_TO_NEXT_LESSON text="Next Lesson: {{SCREENSTEPSLIVE_NEXT_LESSON_TITLE}}"}}' . "\n" .
  932. '<a href="{{SCREENSTEPSLIVE_LINK_TO_MANUAL}}">Return to Manual</a>';
  933. break;
  934. case 'bucket lesson':
  935. $post['post_title'] = '{{SCREENSTEPSLIVE_LESSON_TITLE}}';
  936. $post['post_content'] = '{{SCREENSTEPSLIVE_CONTENT}}' . "\n" .
  937. '<a href="{{SCREENSTEPSLIVE_LINK_TO_BUCKET}}">Return to Lesson Bucket</a>';
  938. break;
  939. default:
  940. $post['post_title'] = 'ScreenSteps Live Page';
  941. $post['post_content'] = '{{SCREENSTEPSLIVE_CONTENT}}';
  942. break;
  943. }
  944. $postID = wp_insert_post($post);
  945. if (is_wp_error($postID))
  946. return $post_ID;
  947. if (empty($postID))
  948. return 0;
  949. return $postID;
  950. }
  951. ?>