Plugin to allow visitor contributions to WordPress posts, wiki style.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
6.3KB

  1. <?php
  2. if(isset($_GET["edit"])){
  3. global $wpdb;
  4. $table_name = $wpdb->prefix . 'visitors_edits';
  5. $edit = $wpdb->get_row("SELECT * FROM ".$table_name." WHERE edit_id=".$_GET["edit"]);
  6. $edit->edit_content=stripcslashes($edit->edit_content);
  7. $edit->post=get_post($edit->post_id);
  8. $options=get_option( "visitors_edits_options", [
  9. "advanced_merge"=>null
  10. ]);
  11. //Fix nl
  12. //$edit->post->post_content=str_replace(["\r\n", "\r", "\n"], "<br/>",$edit->post->post_content);
  13. //$edit->post_content=str_replace(["\r\n", "\r", "\n"], "<br/>",$edit->post_content);
  14. //$edit->edit_content=str_replace(["\r\n", "\r", "\n"], "<br/>",$edit->edit_content);
  15. $edit->post->post_content=nl2br($edit->post->post_content);
  16. $edit->post_content=nl2br($edit->post_content);
  17. $edit->edit_content=nl2br($edit->edit_content);
  18. if($options["advanced_merge"]!=null){
  19. $diff1=visitors_editsDIFF($edit->post_content,$edit->edit_content);
  20. $conv_diff1=encodeDiff($diff1);
  21. $diff=visitors_editsDIFF($conv_diff1["html"],$edit->post->post_content);
  22. $diff=cleanEncodedDiff($diff);
  23. $diff=decodeDiff($diff,$conv_diff1["codes"]);
  24. }else{
  25. $diff=visitors_editsDIFF($edit->post->post_content,$edit->edit_content);
  26. }
  27. $diff=cleanDiff($diff);
  28. echo '<div style="display:none" id="visitors_edits_diff">'.$diff.'</div>';
  29. showEditor($edit);
  30. }else{
  31. noData("Select a visitor contribution");
  32. ?>
  33. <script type="text/javascript">
  34. setTimeout(function(){
  35. window.location="<?php echo menu_page_url('visitors_edits_main',false);?>";
  36. },2000)
  37. </script>
  38. <?php
  39. }
  40. function showEditor($edit,$editor_content=""){
  41. $options=get_option( "visitors_edits_options", [
  42. "edit_notify_message"=>"Thanks for your contribution to our blog, your contribution was reviewed and approved."
  43. ]);
  44. ?>
  45. <div class="visitors_edits_review_editor">
  46. <h1>Review a contribution</h1><a class="delete_edit" href="<?php echo menu_page_url('visitors_edits_main',false);?>&delete=<?php echo $edit->edit_id;?>">Delete this contribution</a>
  47. <ul class="edit_info">
  48. <li><a target="_blank" href="<?php echo get_permalink($edit->post->ID);?>">View original post</a></li>
  49. <li>Author : <?php echo $edit->visitor_name; ?> <span class="mail"><?php echo $edit->visitor_email; ?></span></li>
  50. <li>Submited on :
  51. <?php
  52. $creationDate=date_create($edit->edit_time);
  53. echo date_format($creationDate,"m/d/Y")." at ".date_format($creationDate,"h:i a");
  54. ?>
  55. </li>
  56. <li>Author comment :</li>
  57. <p class="comment">
  58. <?php echo stripcslashes($edit->visitor_comment); ?>
  59. </p>
  60. </ul>
  61. <div class="cb"></div>
  62. <form action="<?php echo menu_page_url('visitors_edits_main',false);?>" method="POST" onsubmit="return editSubmit()">
  63. <div class="editor">
  64. <?php
  65. wp_editor($editor_content,"diff_editor",[
  66. "media_buttons"=>false,
  67. "quicktags"=>false,
  68. "textarea_name"=>"post_content",
  69. "tinymce"=>[
  70. "mode" => "textareas",
  71. "theme" => "modern"
  72. ]
  73. ]);
  74. ?>
  75. </div>
  76. <style>
  77. .diff_editor ins{
  78. background: #44B3FD !important;
  79. }
  80. </style>
  81. <div class="controls">
  82. <input type="hidden" value="<?php echo $edit->visitor_name; ?>" name="visitor_name">
  83. <input type="hidden" value="<?php echo $edit->visitor_email; ?>" name="visitor_email">
  84. <input type="hidden" value="<?php echo $edit->edit_id; ?>" name="edit_id">
  85. <input type="hidden" value="<?php echo $edit->post_id; ?>" name="ID">
  86. <div class="edit_notify">
  87. <label><input type="checkbox" name="notify_visitor" id="edit_notify_activate"> Notify the author.</label>
  88. <textarea name="admin_message" id="edit_notify_message"><?php echo $options["edit_notify_message"]; ?></textarea>
  89. </div>
  90. <button class="button-primary" type="submit">Save changes</button>
  91. </div>
  92. </form>
  93. <script src="<?php echo visitors_edits::scriptUrl('admin');?>"></script>
  94. </div>
  95. <?php
  96. }
  97. function noData($msg){
  98. ?>
  99. <h1 class="visitors_edits_no_data" style="display:block">
  100. <?php echo $msg; ?>
  101. </h1>
  102. <?php
  103. }
  104. function clearTag($tag,$content){
  105. return preg_replace("#<".$tag.".*?>.*?</".$tag.">#i","", $content);
  106. }
  107. function addAttrTag($tag,$attr,$content){
  108. return preg_replace("#<".$tag."(.*?)>#i","<".$tag." ".$attr."='1'$1>", $content);
  109. }
  110. function clearTagName($tag,$content){
  111. return preg_replace("#</*".$tag.".*?>#i", "", $content);
  112. }
  113. function replaceTag($origin,$replace,$content){
  114. return preg_replace("#(<(/*)(".$origin.")(.*?)>)#i", '[$2'.$replace.'$4]', $content);
  115. }
  116. function encodeDiff($diff){
  117. //composer require paquettg/php-html-parser
  118. require_once "parser.php";
  119. $html=str_get_html($diff);
  120. $encoded=[
  121. "html"=>"",
  122. "codes"=>[
  123. "INS"=>[],
  124. "DEL"=>[]
  125. ]
  126. ];
  127. $id=0;
  128. foreach($html->find('ins') as $ins){
  129. $encoded["codes"]["INS"][$id]=$ins->outertext;
  130. $ins->outertext="%INS".$id."%";
  131. $id++;
  132. }
  133. $id=0;
  134. foreach($html->find('del') as $del){
  135. $encoded["codes"]["DEL"][$id]=$del->outertext;
  136. $del->outertext="%DEL".$id."%";
  137. $id++;
  138. }
  139. $encoded["html"]=$html->outertext;
  140. return $encoded;
  141. }
  142. function cleanDiff($content){
  143. require_once "parser.php";
  144. $html=str_get_html($content);
  145. foreach($html->find('ul') as $ul){
  146. foreach($ul->find("br") as $br){
  147. $br->outertext="";
  148. }
  149. }
  150. foreach($html->find('ol') as $ol){
  151. foreach($ol->find("br") as $br){
  152. $br->outertext="";
  153. }
  154. }
  155. return $html;
  156. }
  157. function cleanEncodedDiff($content){
  158. $html=str_get_html($content);
  159. $delete_next=false;
  160. $diffs = $html->find('ins, del');
  161. for ($i=0; $i < count($diffs); $i++) {
  162. $diff=$diffs[$i];
  163. if($delete_next){
  164. $diff->outertext="";
  165. $delete_next=false;
  166. coutinue;
  167. }else{
  168. $isSpecial=preg_match("#%(INS|DEL)\d*?%#", $diff->innertext);
  169. if ($isSpecial){
  170. //is important
  171. if(preg_match("#diffmod#",$diff->outertext)){
  172. $delete_next=true;
  173. }
  174. $diff->outertext=$diff->innertext;
  175. }else{
  176. if($diff->tag==="ins"){
  177. $diff->outertext=$diff->innertext;
  178. }else{
  179. $diff->outertext="";
  180. }
  181. }
  182. }
  183. }
  184. return $html;
  185. }
  186. function decodeDiff($content,$codes){
  187. foreach ($codes["INS"] as $id => $code) {
  188. $content=str_replace("%INS".$id."%", $code, $content);
  189. }
  190. foreach ($codes["DEL"] as $id => $code) {
  191. $content=str_replace("%DEL".$id."%", $code, $content);
  192. }
  193. return $content;
  194. }