PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-wiki/wordpress-wiki.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 961 lines | 659 code | 132 blank | 170 comment | 165 complexity | 588e094ee5be9dae8f9966c67719ff92 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name:WordPress Wiki
  4. Plugin URI: http://wordpress.org/extend/plugins/wordpress-wiki/
  5. Description: Add Wiki functionality to your wordpress site.
  6. Version: 0.8
  7. Author: Instinct Entertainment
  8. Author URI: http://www.instinct.co.nz
  9. /* Major version for "major" releases */
  10. /**
  11. * Guess the wp-content and plugin urls/paths
  12. */
  13. if ( !defined('WP_CONTENT_URL') )
  14. define( 'WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
  15. if ( !defined('WP_CONTENT_DIR') )
  16. define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
  17. if (!defined('PLUGIN_URL'))
  18. define('PLUGIN_URL', WP_CONTENT_URL . '/plugins/');
  19. if (!defined('PLUGIN_PATH'))
  20. define('PLUGIN_PATH', WP_CONTENT_DIR . '/plugins/');
  21. if(get_option('numberOfRevisions') == NULL){
  22. add_option('numberOfRevisions', 5, '', 'yes' );
  23. }
  24. if(get_option('wiki_email_admins') == NULL){
  25. add_option('wiki_email_admins', 0, '', 'yes');
  26. }
  27. if(get_option('wiki_show_toc_onfrontpage') == NULL){
  28. add_option('wiki_show_toc_onfrontpage', 0, '', 'yes');
  29. }
  30. if(get_option('wiki_cron_last_email_date') == NULL){
  31. add_option('wiki_cron_last_email_date', date('Y-m-d G:i:s') , '', 'yes');
  32. }
  33. define('WPWIKI_FILE_PATH', dirname(__FILE__));
  34. define('WPWIKI_DIR_NAME', basename(WPWIKI_FILE_PATH));
  35. ///controller function for admin settings
  36. if(isset($_POST['submit'])){
  37. if(isset($_POST['numberOfRevisions'])){
  38. update_option('numberOfRevisions', (int)$_POST['numberOfRevisions'], '', 'yes');
  39. }
  40. //exit(print_r($_POST));
  41. if($_POST['emailnotification'] == 'on'){
  42. update_option('wiki_email_admins', 1, '' , 'yes');
  43. }elseif(!isset($_POST['emailnotification'])){
  44. update_option('wiki_email_admins', 0, '' , 'yes');
  45. }
  46. if($_POST['showonfrontpage'] == 'on'){
  47. update_option('wiki_show_toc_onfrontpage', 1, '' , 'yes');
  48. }elseif(!isset($_POST['showonfrontpage'])){
  49. update_option('wiki_show_toc_onfrontpage', 0, '' , 'yes');
  50. }
  51. if($_POST['cronnotify'] == 'on'){
  52. update_option('wiki_cron_email', 1, '' , 'yes');
  53. }elseif(!isset($_POST['cronnotify'])){
  54. update_option('wiki_cron_email', 0, '' , 'yes');
  55. }
  56. //echo print_r($_POST, true).get_option('wiki_email_admins');
  57. }
  58. //exit(get_option('wiki_email_admins'));
  59. /**
  60. * The following roles and capabilities code has been removed because it does not work. If you can help with this, please do.
  61. */
  62. global $wp_roles;
  63. if ( ! isset($wp_roles) )
  64. $wp_roles = new WP_Roles();
  65. if ( ! get_role('wiki_editor')){
  66. $role_capabilities = array('read'=>true
  67. ,'edit_posts'=>true
  68. ,'edit_others_posts'=>true
  69. ,'edit_published_posts'=>true
  70. // ,'delete_posts'=>true
  71. // ,'delete_published_posts'=>true
  72. // ,'publish_posts'=>true
  73. // ,'publish_pages'=>true
  74. // ,'delete_pages'=>true
  75. // ,'edit_pages'=>true
  76. // ,'edit_others_pages'=>true
  77. // ,'edit_published_pages'=>true
  78. // ,'delete_published_pages'=>true
  79. ,'edit_wiki'=>true);
  80. $wp_roles->add_role('wiki_editor', 'Wiki Editor',$role_capabilities);
  81. }
  82. $role = get_role('wiki_editor');
  83. $role->add_cap('edit_wiki');
  84. $role->add_cap('edit_pages');
  85. $role->add_cap('edit_post');
  86. $role->add_cap('edit_others_posts');
  87. $role->add_cap('edit_published_posts');
  88. function wiki_post_revisions($content='') {
  89. global $post, $current_user, $role;
  90. if ( !$post = get_post( $post->ID ) )
  91. return $content;
  92. $initial_post_id = $post->ID;
  93. if($post->post_type == 'revision' && ($post->post_parent > 0)) {
  94. if(!$post = get_post( $post->post_parent ))
  95. return $content;
  96. }
  97. $defaults = array( 'parent' => false, 'right' => false, 'left' => false, 'format' => 'list', 'type' => 'all' );
  98. extract( wp_parse_args( $args, $defaults ), EXTR_SKIP );
  99. $type = 'revision';
  100. switch ( $type ) {
  101. case 'autosave' :
  102. if ( !$autosave = wp_get_post_autosave( $post->ID ) )
  103. return $content;
  104. $revisions = array( $autosave );
  105. break;
  106. case 'revision' : // just revisions - remove autosave later
  107. case 'all' :
  108. default :
  109. if ( !$revisions = wp_get_post_revisions( $post->ID ) )
  110. return $content;
  111. break;
  112. }
  113. //echo("<pre>".print_r($revisions,true)."</pre>");
  114. $titlef = _c( '%1$s by %2$s|post revision 1:datetime, 2:name' );
  115. if ( $parent )
  116. array_unshift( $revisions, $post );
  117. $rows = '';
  118. $class = false;
  119. $can_edit_post = current_user_can( 'edit_post', $post->ID );
  120. //Track the first iteration as this is the current version auther who is different from the original
  121. $k=0;
  122. $i = 0;
  123. foreach ( $revisions as $revision ) {
  124. if($i == get_option('numberOfRevisions')) break;
  125. $is_selected = '';
  126. if ( !current_user_can( 'read_post', $revision->ID ) )
  127. continue;
  128. if ( 'revision' === $type && wp_is_post_autosave( $revision ) )
  129. continue;
  130. if($initial_post_id == $revision->ID ) {
  131. $is_selected = "class='selected-revision'";
  132. }
  133. $date = wiki_post_revision_title( $revision );
  134. $name = get_author_name( $revision->post_author );
  135. $title = sprintf( $titlef, $date, $name );
  136. $rows .= "\t<li $is_selected >$title</li>\n";
  137. $i++;
  138. }
  139. if($k==0)
  140. // Current author
  141. $post_author=$name;
  142. $k++;
  143. $wpwiki_members_data = get_post_meta($post->ID,'_wiki_page');
  144. if (current_user_can('edit_wiki') && (is_array($wpwiki_members_data) && ($wpwiki_members_data[0] == 1)) && current_user_can('edit_pages')) {
  145. $link = get_permalink($post->ID);
  146. $output .= "<h4>". 'Post Revisions'."</h4>";
  147. $output .= "<ul class='post-revisions'>\n";
  148. $output .= "\t<li $is_selected ><a href='".$link."'>Current revision</a> by ".$post_author." - <a href='".get_edit_post_link( $post->ID )."'>Edit this page</a></li>\n";
  149. $output .= $rows;
  150. $output .= "</ul>";
  151. }
  152. if((is_user_logged_in() == false)&&(is_array($wpwiki_members_data) && ($wpwiki_members_data[0] == 1))){
  153. $siteurl = get_option('siteurl');
  154. $currentpage = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
  155. $output .= "This page is wiki editable click <a href='".$siteurl."/wp-login.php?redirect_to=http://".$currentpage."'> here</a> to edit this page.";
  156. }
  157. return $content.$output;
  158. }
  159. function wiki_post_revision_title( $revision, $link = true ) {
  160. if ( !$revision = get_post( $revision ) )
  161. return $revision;
  162. if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
  163. return false;
  164. $datef = _c( 'j F, Y @ G:i|revision date format');
  165. $autosavef = __( '%s [Autosave]' );
  166. $currentf = __( '%s [Current Revision]' );
  167. $date = date_i18n( $datef, strtotime( $revision->post_modified_gmt . ' +0000' ) );
  168. if ( $link ) {
  169. $link = get_permalink($revision->post_parent)."?revision=".$revision->ID;
  170. $date = "<a href='$link'>$date</a>";
  171. }
  172. if ( !wp_is_post_revision( $revision ) )
  173. $date = sprintf( $currentf, $date );
  174. elseif ( wp_is_post_autosave( $revision ) )
  175. $date = sprintf( $autosavef, $date );
  176. return $date;
  177. }
  178. function wiki_substitute_in_revision_id($query) {
  179. /**
  180. * This function substitutes the revision ID for the post ID
  181. * we need to set $query->is_single and $query->is_page to false, otherwise it cannot select the revisions
  182. */
  183. if(((int)$_GET['revision'] > 0) && ($query->is_single == true)) {
  184. $query->query_vars['page_id'] = $_GET['revision'];
  185. $query->query_vars['pagename'] = null;
  186. $query->query_vars['post_type'] = 'revision';
  187. $query->is_single = false;
  188. } else if(((int)$_GET['revision'] > 0) && ($query->is_page == true)) {
  189. $query->query_vars['page_id'] = $_GET['revision'];
  190. $query->query_vars['pagename'] = null;
  191. $query->query_vars['post_type'] = 'revision';
  192. $query->is_single = false;
  193. $query->is_page = false;
  194. // echo("<pre>".print_r($query,true)."</pre>");
  195. }
  196. }
  197. function wiki_view_sql_query($query) {
  198. /**
  199. * This function makes wordpress treat a revision as a single post
  200. */
  201. global $wp_query;
  202. if((int)$_GET['revision'] > 0 ) {
  203. $wp_query->is_single= true;
  204. // echo("<pre>".print_r($wp_query,true)."</pre>");
  205. }
  206. return $query;
  207. }
  208. /**
  209. * wiki page metabox section starts
  210. */
  211. function wordpressWiki_metabox() {
  212. /**
  213. * this function creates the HTML for the wiki page metabox module
  214. */
  215. global $wpdb, $post_meta_cache;
  216. if(is_numeric($_GET['post'])) {
  217. $post_ID = (int)$_GET['post'];
  218. $wpwiki_members_data = get_post_meta($post_ID,'_wiki_page');
  219. if(is_array($wpwiki_members_data) && ($wpwiki_members_data[0] == 1)) {
  220. $checked_status = "checked='checked'";
  221. $wiki_toc_data = get_post_meta($post_ID,'_wiki_page_toc');
  222. if(is_array($wiki_toc_data) && ($wiki_toc_data[0] == 1)) {
  223. $wiki_toc_status = "checked='checked'";
  224. } else {
  225. $wiki_toc_status = "";
  226. }
  227. } else {
  228. $checked_status = "";
  229. $wiki_toc_status = "disabled";
  230. }
  231. } else {
  232. $checked_status = "";
  233. $wiki_toc_status = "disabled";
  234. }
  235. echo "<label class='selectit' for='wiki_page'>
  236. <input id='wiki_page_check' type='hidden' value='1' name='wiki_page_check' />
  237. <input id='wiki_page' type='checkbox' $checked_status value='1' name='wiki_page' onchange = 'check_toc();' />";
  238. _e("This page/post is a wiki friendly page and may be edited by authors and contributors.");
  239. echo "
  240. </label><br />
  241. <lable class = 'selectit' for = 'wiki_toc'>
  242. <input id='wiki_toc' type='checkbox' $wiki_toc_status value='1' name='wiki_toc' />";
  243. _e("Enable Table of Contents");
  244. echo "</label>";
  245. }
  246. /**
  247. * wiki page metabox section starts
  248. */
  249. function wiki_metabox_module() {
  250. /**
  251. * this function creates the HTML for the wiki page metabox module
  252. */
  253. add_meta_box( 'wordpressWiki', __( 'Wordpress Wiki', 'wordpressWiki' ),
  254. 'wordpressWiki_metabox', 'page', 'advanced' );
  255. add_meta_box( 'wordpressWiki', __( 'Wordpress Wiki', 'wordpressWiki' ),
  256. 'wordpressWiki_metabox', 'post', 'advanced' );
  257. }
  258. function wiki_metabox_module_submit($post_ID) {
  259. /**
  260. * this function saves the HTML for the wiki page metabox module
  261. */
  262. global $wpdb;
  263. if(is_numeric($post_ID) && ($_POST['wiki_page_check'] == 1)) {
  264. if(isset($_POST['wiki_page']) && ($_POST['wiki_page'] == 1)) {
  265. $wpwiki_members_value = 1;
  266. } else {
  267. $wpwiki_members_value = 0;
  268. }
  269. if(isset($_POST['wiki_toc']) && ($_POST['wiki_toc'] == 1)) {
  270. $wiki_toc_value = 1;
  271. } else {
  272. $wiki_toc_value = 0;
  273. }
  274. $wpwiki_check_members_data = $wpdb->get_var("SELECT `meta_id` FROM `".$wpdb->postmeta."` WHERE `post_id` IN('".$post_ID."') AND `meta_key` IN ('wiki_page') LIMIT 1");
  275. //changed by jeffry 23-02-09 fixes the meta content bug
  276. update_post_meta($post_ID, '_wiki_page', (int)(bool)$wpwiki_members_value);
  277. $wpwiki_check_toc_data = $wpdb->get_var("SELECT `meta_id` FROM `".$wpdb->postmeta."` WHERE `post_id` IN('".$post_ID."') AND `meta_key` IN ('wiki_page_toc') LIMIT 1");
  278. //changed by jeffry 23-02-09 fixes the meta content bug
  279. update_post_meta($post_ID, '_wiki_page_toc', (int)(bool)$wiki_toc_value);
  280. // need to change the custom fields value too, else it tries to reset what we just did.
  281. if(is_array($_POST['meta'])) {
  282. foreach($_POST['meta'] as $meta_key=>$meta_data) {
  283. if($meta_data['key'] == 'wiki_page') {
  284. $_POST['meta'][$meta_key]['value'] = $wpwiki_members_value;
  285. }
  286. if($meta_data['key'] == 'wiki_page_toc') {
  287. $_POST['meta'][$meta_key]['value'] = $wiki_toc_value;
  288. }
  289. }
  290. }
  291. }
  292. }
  293. /**
  294. * wiki page metabox module ends
  295. */
  296. function wiki_exclude_pages_filter($excludes) {
  297. global $wpdb;
  298. // get the list of excluded pages and merge them with the current list
  299. $excludes = array_merge((array)$excludes, (array)$wpdb->get_col("SELECT DISTINCT `post_id` FROM `".$wpdb->postmeta."` WHERE `meta_key` IN ( 'wiki_page' ) AND `meta_value` IN ( '1' )"));
  300. return $excludes;
  301. }
  302. function table_of_contents($content) {
  303. /**
  304. * This creates the Table of Contents
  305. */
  306. global $wpdb,$post;
  307. $wpwiki_members_data = get_post_meta($post->ID,'_wiki_page');
  308. if ($wpwiki_members_data[0] != '1') {
  309. return $content;
  310. }
  311. // Check whether table of contents is set or not
  312. $wiki_toc_data = get_post_meta($post->ID,'_wiki_page_toc');
  313. // second condition checks: are we on the front page and
  314. // is front page displaying set. - tony@irational.org
  315. if ($wiki_toc_data[0] != '1'
  316. || (is_front_page() && !get_option('wiki_show_toc_onfrontpage'))) {
  317. return $content;
  318. }
  319. preg_match_all("|<h2>(.*)</h2>|", $content, $h2s, PREG_PATTERN_ORDER);
  320. $content = preg_replace("|<h2>(.*)</h2>|", "<a name='$1'></a><h2>$1</h2>", $content);
  321. $content = preg_replace("|<h3>(.*)</h3>|", "<a name='$1'></a><h3>$1</h3>", $content);
  322. $h2s = $h2s[1];
  323. $content = str_replace("\n", "::newline::", $content);
  324. preg_match_all("|</h2>(.*)<h2>|U", $content, $h3s_contents, PREG_PATTERN_ORDER);
  325. /**
  326. * The following lines are really ugly for finding <h3> after the last </h2> please tidy it up if u know a better solution, and please let us know about it.
  327. */
  328. $last_h2_pos = explode('</h2>', $content);
  329. $last_h2_pos = array_pop($last_h2_pos);
  330. $last_h2_pos[1] = $last_h2_pos;
  331. $h3s_contents[1][] = $last_h2_pos;
  332. if (!is_array($h3s_contents[1])) {
  333. $h3s_contents[1] = array();
  334. }
  335. array_push($h3s_contents[1], $last_h2_pos);
  336. foreach ($h3s_contents[1] as $key => $h3s_content) {
  337. preg_match_all("|<h3>(.*)</h3>|U", $h3s_content, $h3s[$key], PREG_PATTERN_ORDER);
  338. }
  339. $table = "<ol class='content_list'>";
  340. foreach($h2s as $key => $h2) {
  341. $table .= "<li><a href='#$h2'>".($key+1)." ".$h2."</a></li>";
  342. if (!empty($h3s[$key][1])) {
  343. foreach($h3s[$key][1] as $key1 => $h3) {
  344. $table .= "<li class='lvl2'><a href='#$h3'>".($key+1).".".($key1+1)." ".$h3."</a></li>";
  345. }
  346. }
  347. }
  348. $table .= "</ol>";
  349. $content = str_replace("::newline::", "\n", $content);
  350. return "<div class='contents'><h3>Contents</h3><p> &#91; <a class='show' onclick='toggle_hide_show(this)'>hide</a> &#93; </p>$table</div>".$content;
  351. }
  352. function wp_wiki_head() {
  353. /**
  354. * Include CSS
  355. */
  356. echo "<link href='". PLUGIN_URL ."/".WPWIKI_DIR_NAME."/style.css' rel='stylesheet' type='text/css' />";
  357. }
  358. /**
  359. * Enqueue Scripts
  360. */
  361. function wiki_enqueue_scripts() {
  362. wp_enqueue_script("jquery");
  363. wp_enqueue_script('wordpress-wiki', PLUGIN_URL ."/".WPWIKI_DIR_NAME."/wordpress-wiki.js");
  364. }
  365. //Feed Functions
  366. /**
  367. * Add new feed to WordPress
  368. * @global <type> $wp_rewrite
  369. */
  370. function wiki_add_feed( ) {
  371. if (function_exists('load_plugin_textdomain')) {
  372. $plugin_dir = basename(dirname(__FILE__));
  373. load_plugin_textdomain('wordpress_wiki', '', $plugin_dir);
  374. }
  375. global $wp_rewrite;
  376. add_feed('wiki', 'wiki_create_feed');
  377. add_action('generate_rewrite_rules', 'wiki_rewrite_rules');
  378. $wp_rewrite->flush_rules();
  379. }
  380. /**
  381. * Modify feed rewrite rules
  382. * @param <type> $wp_rewrite
  383. */
  384. function wiki_rewrite_rules( $wp_rewrite ) {
  385. $new_rules = array(
  386. 'feed/(.+)' => 'index.php?feed='.$wp_rewrite->preg_index(1)
  387. );
  388. $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
  389. }
  390. /**
  391. * This function creates the actual feed
  392. */
  393. function wiki_create_feed() {
  394. global $wpdb;
  395. $posts = $wpdb->get_results($wpdb->prepare("select * from $wpdb->posts where ID in (
  396. select post_id from $wpdb->postmeta where
  397. meta_key = 'wiki_page' and meta_value = 1)
  398. and post_type in ('post','page') order by post_modified desc"));
  399. header('Content-type: text/xml; charset=' . get_option('blog_charset'), true);
  400. echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
  401. ?>
  402. <rss version="2.0"
  403. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  404. xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  405. xmlns:dc="http://purl.org/dc/elements/1.1/"
  406. >
  407. <channel>
  408. <title><?php print(__('Recently modifiyed wiki pages for : ')); bloginfo_rss('name'); ?></title>
  409. <link><?php bloginfo_rss('url') ?></link>
  410. <description><?php bloginfo_rss("description") ?></description>
  411. <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></pubDate>
  412. <generator>http://wordpress.org/?v=<?php bloginfo_rss('version'); ?></generator>
  413. <language><?php echo get_option('rss_language'); ?></language>
  414. <?php
  415. if (count($posts) > 0) {
  416. foreach ($posts as $post) {
  417. $content = '
  418. <p>'.__('wiki URL: ').'<a href="'. get_permalink($post->ID).'">'.$post->post_title.'</a></p>
  419. <p>'.__('Modifiyed By: ').'<a href="'. get_author_posts_url($post->post_author).'">'. get_author_name($post->post_author).'</a></p>
  420. ';
  421. ?>
  422. <item>
  423. <title><![CDATA[<?php print(htmlspecialchars($post->post_title)); ?>]]></title>
  424. <link><![CDATA[<?php print(get_permalink($post->ID)); ?>]]></link>
  425. <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', $post->post_date_gmt, false); ?></pubDate>
  426. <guid isPermaLink="false"><?php print($post->ID); ?></guid>
  427. <description><![CDATA[<?php print($content); ?>]]></description>
  428. <content:encoded><![CDATA[<?php print($content); ?>]]></content:encoded>
  429. </item>
  430. <?php $items_count++; if (($items_count == get_option('posts_per_rss')) && !is_date()) { break; } } } ?>
  431. </channel>
  432. </rss>
  433. <?php
  434. die();
  435. }
  436. function wiki_get_post_revision($id){
  437. global $wpdb;
  438. $sql = "SELECT `$wpdb->users`.`user_login` FROM $wpdb->users LEFT JOIN $wpdb->posts ON $wpdb->posts.`post_author` = $wpdb->users.`ID` WHERE $wpdb->posts.`post_parent`= $id AND $wpdb->posts.`post_type`='revision' AND `post_name` LIKE '%revision%' ORDER BY $wpdb->posts.`post_date` DESC";
  439. //echo $sql;
  440. $revision = $wpdb->get_var($sql);
  441. return $revision;
  442. // exit('<pre>'.print_r($revision,true).'</pre>');
  443. }
  444. /**
  445. * function to output the contents of our Dashboard Widget
  446. */
  447. function wiki_dashboard_widget_function() {
  448. global $wpdb;
  449. // Display whatever it is you want to show
  450. $posts = $wpdb->get_results($wpdb->prepare("select * from $wpdb->posts where ID in (
  451. select post_id from $wpdb->postmeta where
  452. meta_key = 'wiki_page' and meta_value = 1)
  453. and post_type in ('post','page') order by post_modified desc limit 5"));
  454. ?>
  455. <div class="rss-widget">
  456. <ul>
  457. <?php
  458. if (count($posts) > 0) {
  459. foreach ($posts as $post) {
  460. $name = wiki_get_post_revision($post->ID);
  461. ?>
  462. <li><a href = "<?php echo get_permalink($post->ID)?>"><?php echo $post->post_title ?></a> (<?php echo $name; ?>)</li>
  463. <?php
  464. }
  465. } else {
  466. ?>
  467. <li><?php _e("No contributions yet.") ?></li>
  468. <?php
  469. }
  470. ?>
  471. </ul>
  472. </div>
  473. <?php
  474. }
  475. /**
  476. * function to hook
  477. */
  478. function wiki_dashboard_widget() {
  479. wp_add_dashboard_widget('wiki_dashboard_widget', 'Recent contributions to Wiki', 'wiki_dashboard_widget_function');
  480. }
  481. /**
  482. * Fetch the posts edited by the current user
  483. * @global <type> $userdata
  484. * @global <type> $wpdb
  485. * @param <type> $nos
  486. */
  487. function wiki_get_user_posts($nos) {
  488. global $userdata;
  489. global $wpdb;
  490. get_currentuserinfo();
  491. $nos = ($nos <= 0) ? 10: $nos;
  492. $count = 0;
  493. $posts = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_author = %d and post_status = 'publish' and post_type in ('post','page')", $userdata->ID));
  494. ?>
  495. <ul>
  496. <?php
  497. foreach ($posts as $post) {
  498. if (get_post_meta($post->ID, "_wiki_page", "true") == "1") {
  499. printf("<li><a href = '%s'>%s</a></li>", get_permalink($post->ID) ,$post->post_title);
  500. $count++;
  501. if ($count == $nos) {
  502. break;
  503. }
  504. }
  505. }
  506. if ($count == 0) {
  507. ?>
  508. <li> <?php _e("You have made no contributions"); ?> </li>
  509. <?php
  510. }
  511. ?>
  512. </ul>
  513. <?php
  514. }
  515. /**
  516. * Widget Initialization
  517. * @return <type>
  518. */
  519. function wiki_widget_myc_init() {
  520. if(!function_exists('register_sidebar_widget')) { return; }
  521. /**
  522. * Widget Calls this function
  523. * @param <type> $args
  524. */
  525. function wiki_widget_myc($args) {
  526. global $userdata;
  527. get_currentuserinfo();
  528. if ($userdata->ID == 0 || trim($userdata->ID == "")) {
  529. return;
  530. } else {
  531. extract($args);
  532. $widget_options = get_option('widget_myc');
  533. $widget_title = $widget_options['title'];
  534. $widget_nos = $widget_options['nos'];
  535. echo $before_widget . $before_title . $widget_title . $after_title;
  536. wiki_get_user_posts($widget_nos);
  537. echo $after_widget;
  538. }
  539. }
  540. /**
  541. * Widget Control function
  542. */
  543. function wiki_widget_myc_control() {
  544. $options = $newoptions = get_option('widget_myc');
  545. if ( $_POST["myc-submit"] ) {
  546. $newoptions['title'] = strip_tags(stripslashes($_POST["myc-title"]));
  547. $newoptions['nos'] = strip_tags(stripslashes($_POST["myc-nos"]));
  548. }
  549. if ( $options != $newoptions ) {
  550. $options = $newoptions;
  551. update_option('widget_myc', $options);
  552. }
  553. $title = attribute_escape($options['title']);
  554. $nos = absint(attribute_escape($options['nos']));
  555. $nos = ($nos <= 0) ? 10: $nos;
  556. ?>
  557. <p>
  558. <label for="myc-title"><?php _e('Title:'); ?><br />
  559. <input style="width: 250px;" id="myc-title" name="myc-title" type="text" value="<?php echo $title; ?>" />
  560. </label>
  561. </p>
  562. <p>
  563. <label for="myc-nos"><?php _e('Number of posts to show:'); ?>
  564. <input name="myc-nos" id="myc-nos" type="text" size = "3" maxlength="3" value="<?php echo $nos; ?>" />
  565. </label>
  566. </p>
  567. <input type="hidden" id="myc-submit" name="myc-submit" value="1" />
  568. <?php
  569. }
  570. $widget_ops = array('classname' => 'widget_my_contributions', 'description' => __( "My Contributions widget for WordPress Wiki Plugin") );
  571. wp_register_sidebar_widget('my_contributions', __('My Contributions'), 'wiki_widget_myc', $widget_ops);
  572. wp_register_widget_control('my_contributions', __('My Contributions'),'wiki_widget_myc_control', 300, 100);
  573. }
  574. /**
  575. * Build links from shortcodes
  576. * @param <type> $content
  577. * @return <type> modifiyed content
  578. */
  579. function wiki_build_links($content) {
  580. global $post;
  581. if (get_post_meta($post->ID, "_wiki_page", "true") == "1") {
  582. // If it is a wiki post or page, then parse the content and build links
  583. $pattern = '/(\[\[([^\]]*)\]\])/i';
  584. return preg_replace_callback($pattern, "wiki_callback_func", $content);
  585. } else {
  586. //If it is not a wiki post or page then return the content.
  587. return $content;
  588. }
  589. }
  590. /**
  591. * Call back function for regex
  592. * @param <type> $m
  593. * @return <type> link
  594. */
  595. function wiki_callback_func($m) {
  596. global $post;
  597. $splited = explode("|", $m[2]);
  598. if (count($splited) == 2) {
  599. $link_text = trim($splited[1]);
  600. $link_slug = trim($splited[0]);
  601. } else {
  602. $link_slug = $link_text = $m[2];
  603. }
  604. $link = get_permalink_by_title($link_slug);
  605. if (!$link) {
  606. // If there is no post with that title
  607. if ($post->post_type == "page") {
  608. $link = get_bloginfo("wpurl") . "/wp-admin/page-new.php" ;
  609. } else {
  610. $link = get_bloginfo("wpurl") . "/wp-admin/post-new.php" ;
  611. }
  612. }
  613. return "<a href = '" . $link . "' >" . $link_text . "</a>";
  614. }
  615. /**
  616. * Get Permalink by post title
  617. * @global <type> $wpdb
  618. * @param <type> $page_title
  619. * @return <type> permalink
  620. */
  621. function get_permalink_by_title($page_title) {
  622. global $wpdb;
  623. $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type in ('post','page')", $page_title ));
  624. if ( $post )
  625. return get_permalink($post);
  626. return NULL;
  627. }
  628. /**
  629. * wiki_page_edit_notification
  630. * @global <type> $wpdb
  631. * @param <type> $pageID
  632. * @return NULL
  633. */
  634. function wiki_page_edit_notification($pageID) {
  635. global $wpdb;
  636. if(get_option('wiki_email_admins') == 1){
  637. $emails = getAllAdmins();
  638. $sql = "SELECT post_title, guid FROM ".$wpdb->prefix."posts WHERE ID=".$pageID;
  639. $subject = "Wiki Change";
  640. $results = $wpdb->get_results($sql);
  641. $pageTitle = $results[0]->post_title;
  642. $pagelink = $results[0]->guid;
  643. $message = "A Wiki Page has been modified on '".get_option('home')."' \n\r The page title is ".$pageTitle.". \n\r To visit this page <a href='".$pagelink."'> click here</a>.";
  644. //exit(print_r($emails, true));
  645. foreach($emails as $email){
  646. wp_mail($email, $subject, $message);
  647. }
  648. }
  649. }
  650. /**
  651. * getAllAdmins
  652. * @global <type> $wpdb
  653. * @param <type> NULL
  654. * @return email addresses for all administrators
  655. */
  656. function getAllAdmins(){
  657. global $wpdb;
  658. $sql = "SELECT ID from $wpdb->users";
  659. $IDS = $wpdb->get_col($sql);
  660. foreach($IDS as $id){
  661. $user_info = get_userdata($id);
  662. if($user_info->user_level == 10){
  663. $emails[] = $user_info->user_email;
  664. }
  665. }
  666. return $emails;
  667. }
  668. /**
  669. * Template tag which can be added in the 404 page
  670. */
  671. function wiki_404() {
  672. $not_found = str_replace("/", "", $_SERVER['REQUEST_URI']);
  673. echo "<p>" . __("Sorry, the page with title ") . $not_found . __(" is not created yet. Click") . '<a href="' . get_bloginfo('wpurl') . '/wp-admin/post-new.php">' . __("here") . '</a>' . __(" to create a new page with that title.") . "</p";
  674. }
  675. /**
  676. * If page edit capabilities are checked for a wiki page, grant them if current user has edit_wiki cap.
  677. * @global <type> $wp_query
  678. * @param <array> $wp_blogcaps : current user's blog-wide capabilities
  679. * @param <array> $reqd_caps : primitive capabilities being tested / requested
  680. * @param <array> $args = array:
  681. * $args[0] = original capability requirement passed to current_user_can (possibly a meta cap)
  682. * $args[1] = user being tested
  683. * $args[2] = object id (could be a postID, linkID, catID or something else)
  684. * @return <array> capabilities as array key
  685. */
  686. function wiki_page_cap($wp_blogcaps, $reqd_caps, $args) {
  687. static $busy;
  688. if ( ! empty($busy) ) // don't process recursively
  689. return $wp_blogcaps;
  690. $busy = true;
  691. // Note: add edit_private_pages if you want the edit_wiki cap to satisfy that check also.
  692. if ( ! array_diff( $reqd_caps, array( 'edit_pages', 'edit_others_pages', 'edit_published_pages' ) ) ) {
  693. // determine page ID
  694. if ( ! empty($args[2]) )
  695. $page_id = $args[2];
  696. elseif ( ! empty($_GET['post']) )
  697. $page_id = $_GET['post'];
  698. elseif ( ! empty($_POST['ID']) )
  699. $page_id = $_POST['ID'];
  700. elseif ( ! empty($_POST['post_ID']) )
  701. $page_id = $_POST['post_ID'];
  702. elseif ( ! is_admin() ) {
  703. global $wp_query;
  704. if ( ! empty($wp_query->post->ID) ) {
  705. $page_id = $wp_query->post->ID;
  706. }
  707. }
  708. if ( ! empty($page_id) ) {
  709. global $current_user, $scoper;
  710. if ( ! empty($scoper) && function_exists('is_administrator_rs') && ! is_administrator_rs() && $scoper->cap_defs->is_member('edit_wiki') ) {
  711. // call Role Scoper has_cap filter directly because recursive calling of has_cap filter confuses WP
  712. $user_caps = $scoper->cap_interceptor->flt_user_has_cap( $current_user->allcaps, array('edit_wiki'), array('edit_wiki', $current_user->ID, $page_id ) );
  713. } else
  714. $user_caps = $current_user->allcaps;
  715. if ( ! empty( $user_caps['edit_wiki'] ) ) {
  716. // Static-buffer the metadata to avoid performance toll from multiple cap checks.
  717. static $wpsc_members_data;
  718. if ( ! isset($wpsc_members_data) )
  719. $wpsc_members_data = array();
  720. if ( ! isset($wpsc_members_data[$page_id]) )
  721. $wpsc_members_data[$page_id] = get_post_meta($page_id,'_wiki_page');
  722. // If the page in question is a wiki page, give current user credit for all page edit caps.
  723. if ( is_array($wpsc_members_data[$page_id]) && ($wpsc_members_data[$page_id][0] == 1) ) {
  724. $wp_blogcaps = array_merge( $wp_blogcaps, array_fill_keys($reqd_caps, true) );
  725. }
  726. }
  727. }
  728. }
  729. $busy = false;
  730. return $wp_blogcaps;
  731. }
  732. add_filter('user_has_cap', 'wiki_page_cap', 100, 3); // this filter must be applied after Role Scoper's because we're changing the cap requirement
  733. add_action('edit_form_advanced','wiki_metabox_module');
  734. add_action('edit_page_form', 'wiki_metabox_module');
  735. add_action('edit_post', 'wiki_metabox_module_submit');
  736. add_action('publish_post', 'wiki_metabox_module_submit');
  737. add_action('save_post', 'wiki_metabox_module_submit');
  738. add_action('edit_page_form', 'wiki_metabox_module_submit');
  739. add_action('pre_get_posts', 'wiki_substitute_in_revision_id');
  740. add_filter('posts_request', 'wiki_view_sql_query');
  741. add_filter('the_content', 'table_of_contents', 9);
  742. add_filter('the_content', 'wiki_post_revisions', 11);
  743. //add css
  744. add_action('wp_head', 'wp_wiki_head');
  745. add_action('init', 'wiki_enqueue_scripts', 9);
  746. // Feeds
  747. add_action('init', 'wiki_add_feed', 11);
  748. // Hoook into the 'wp_dashboard_setup' action to register our other functions
  749. add_action('wp_dashboard_setup', 'wiki_dashboard_widget' );
  750. add_action('plugins_loaded', 'wiki_widget_myc_init');
  751. add_filter("the_content", "wiki_build_links", 999);
  752. add_action('admin_menu', 'wiki_admin_menu');
  753. //hook to check whether a page has been edited
  754. add_action('publish_page', 'wiki_page_edit_notification');
  755. function more_reccurences() {
  756. return array(
  757. 'weekly' => array('interval' => 604800, 'display' => 'Once Weekly'),
  758. 'fortnightly' => array('interval' => 1209600, 'display' => 'Once Fortnightly'),
  759. );
  760. }
  761. add_filter('cron_schedules', 'more_reccurences');
  762. if (!wp_next_scheduled('cron_email_hook')) {
  763. wp_schedule_event( time(), 'weekly', 'cron_email_hook' );
  764. }
  765. add_action( 'cron_email_hook', 'cron_email' );
  766. function cron_email() {
  767. if (get_option('wiki_cron_email') == 1) {
  768. $last_email = get_option('wiki_cron_last_email_date');
  769. $emails = getAllAdmins();
  770. $sql = "SELECT post_title, guid FROM ".$wpdb->prefix."posts WHERE post_modifiyed > ".$last_email;
  771. $subject = "Wiki Change";
  772. $results = $wpdb->get_results($sql);
  773. $message = " The following Wiki Pages has been modified on '".get_option('home')."' \n\r ";
  774. if ($results) {
  775. foreach ($results as $result) {
  776. $pageTitle = $result->post_title;
  777. $pagelink = $result->guid;
  778. $message .= "Page title is ".$pageTitle.". \n\r To visit this page <a href='".$pagelink."'> click here</a>.\n\r\n\r";
  779. //exit(print_r($emails, true));
  780. foreach($emails as $email){
  781. wp_mail($email, $subject, $message);
  782. }
  783. }
  784. }
  785. update_option('wiki_cron_last_email_date', date('Y-m-d G:i:s'));
  786. }
  787. }
  788. function wiki_admin_menu(){
  789. add_submenu_page('options-general.php', 'Wiki settings','Wiki settings', 10 , __FILE__, 'wiki_plugin_options');
  790. }
  791. function wiki_plugin_options(){
  792. if(get_option('wiki_email_admins') == 0){
  793. $checked = '';
  794. }else if(get_option('wiki_email_admins') == 1){
  795. $checked = 'checked=checked';
  796. }
  797. if(get_option('wiki_show_toc_onfrontpage') == 0){
  798. $checked_toc = '';
  799. }else if(get_option('wiki_show_toc_onfrontpage') == 1){
  800. $checked_toc = 'checked=checked';
  801. }
  802. if(get_option('wiki_cron_email') == 0){
  803. $cron_checked = '';
  804. }else if(get_option('wiki_cron_email') == 1){
  805. $cron_checked = 'checked=checked';
  806. }
  807. ?>
  808. <div class="wrap">
  809. <h2>Wiki Settings</h2>
  810. <br />
  811. <?php
  812. if(isset($_POST['submit'])){
  813. ?>
  814. <p>Update Complete.</p>
  815. <?php
  816. }
  817. ?>
  818. <form action='' method='post'>
  819. <label for='numberOfRevisions'>Number of Revisions per page: </label>
  820. <p><input type='text' name='numberOfRevisions' size='10' value="<?php echo get_option('numberOfRevisions');?>" /></p>
  821. <p><input type='checkbox' name='emailnotification' <?php echo $checked; ?> /> Notify Administrators via Email when wiki pages have been editted.</p>
  822. <p><input type='checkbox' name='showonfrontpage' <?php echo $checked_toc; ?> /> Show Table of Content on the front page posts.</p>
  823. <p><input type='checkbox' name='cronnotify' <?php echo $cron_checked; ?> /> Notify Administrators via Email when wiki pages have been editted. (only once in a week)</p>
  824. <input class='button-primary' type='submit' name='submit' value='Submit' />
  825. </form>
  826. </div>
  827. <?php
  828. }
  829. ?>