PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/library/functions/partner-multicontent.php

https://github.com/JustinHop/fya
PHP | 233 lines | 167 code | 43 blank | 23 comment | 46 complexity | 0baa9e2c7f45b66febe22403c33ee1e4 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Multiple content blocks modified.
  4. Plugin URI: http://justinmademysite.com
  5. Description: Lets you use more than one content "block" on a template. You only have to insert one tag inside the template, so it's easy to use.
  6. Version: 1.4.5
  7. Author: Justin Hoppensteadt
  8. */
  9. function init_multiplecontent() {
  10. #add_meta_box('multi_content',__('Multiple content blocks','trendwerk'),'add_multiplecontent_box','page','normal','high');
  11. #add_meta_box('multi_content',__('Multiple content blocks','trendwerk'),'add_multiplecontent_box','post','normal','high');
  12. add_meta_box('multi_content',__('Partner Information','trendwerk'),'add_multiplecontent_box','partner','normal','high');
  13. add_action('admin_head','multiplecontent_css');
  14. add_action('save_post', 'save_multiplecontent_box');
  15. add_action('admin_init','init_multiplecontent');
  16. }
  17. function multiplecontent_css() {
  18. echo '
  19. <style type="text/css">
  20. .js .theEditor, #editorcontainer #content {
  21. color: #000 !important;
  22. width: 100%;
  23. }
  24. </style>
  25. ';
  26. }
  27. function add_multiplecontent_box() {
  28. //check which template is used
  29. global $post;
  30. $fileToRead = get_template_directory_uri().'/'.$post->page_template;
  31. //read the template
  32. $fileToRead = strstr($fileToRead,'/themes/');
  33. if(substr(strrchr($fileToRead,'/'),1) == 'default' || substr(strrchr($fileToRead,'/'),1) == '') { //fix for 2.9
  34. if(substr(strrchr($fileToRead,'/'),1)) {
  35. if($post->post_type == 'post') {
  36. $fileToRead = substr($fileToRead, 0 ,-7) . 'single.php';
  37. } else {
  38. $fileToRead = substr($fileToRead, 0 ,-7) . 'page.php';
  39. }
  40. } elseif($post->post_type == 'post') {
  41. if ( is_single( ) ) {
  42. $fileToRead .= 'single.php';
  43. } else {
  44. $fileToRead .= 'page.php';
  45. }
  46. }
  47. else {
  48. if($post->post_type == 'partner') {
  49. $fileToRead .= 'partner-single.php';
  50. } else {
  51. $fileToRead .= 'page.php';
  52. }
  53. }
  54. }
  55. $fileToRead = validate_file_to_edit($fileToRead, $allowed_files);
  56. $fileToRead = get_real_file_to_edit($fileToRead);
  57. //first try to read the child theme, otherwise use the normal theme
  58. $themes = get_themes();
  59. $theme = $themes[get_current_theme()];
  60. $current_theme_url = $theme['Template'];
  61. $child_theme_url = str_replace('themes/','',strstr(get_stylesheet_directory_uri(),'themes/'));
  62. if(fopen(str_replace($current_theme_url,$child_theme_url,$fileToRead), 'r')) { //child theme exists
  63. $fileToRead = str_replace($current_theme_url,$child_theme_url,$fileToRead);
  64. $f = fopen($fileToRead, 'r');
  65. } else {
  66. $f = fopen($fileToRead, 'r');
  67. }
  68. $contents = fread($f, filesize($fileToRead));
  69. $contents = htmlspecialchars( $contents );
  70. //read the templates header, sidebar and footer, added in v1.1
  71. $headercontents = read_tag('header',$contents);
  72. $footercontents = read_tag('footer',$contents);
  73. //multiple sidebars, v1.2
  74. $amount_sidebars = substr_count($contents,'get_sidebar(');
  75. $nextContent = $contents;
  76. for($i=0;$i<$amount_sidebars;$i++) {
  77. $sidebarcontents .= read_tag('sidebar',$nextContent);
  78. $nextContent = substr(strstr($contents,'get_sidebar('),13);
  79. }
  80. $contents = $headercontents.$contents.$sidebarcontents.$footercontents;
  81. //check how many content field there have to be
  82. $editors = substr_count($contents,"the_block(");
  83. $nextString = $contents;
  84. for($i=0;$i<$editors;$i++) {
  85. //check whether the next one is a get_the_block or the_block
  86. $get = false;
  87. $firstThe = strpos($nextString,' the_block');
  88. $firstGet = strpos($nextString,'get_the_block');
  89. if(($firstThe > $firstGet && $firstGet != 0) || $firstThe == 0) { //get_the_block is first
  90. $get = true;
  91. }
  92. //get the name from it
  93. $stringFirst = strstr($nextString,' the_block(');
  94. if($get) {
  95. $stringFirst = " ".strstr($nextString,'get_the_block(');
  96. }
  97. $stringFirst = substr($stringFirst,1);
  98. $stringLast = strstr($stringFirst,')');
  99. //remove single and double quotes
  100. if(!$get) {
  101. $editorName = str_replace('\'','', str_replace('&quot;','',str_replace('the_block(','',str_replace($stringLast,'',$stringFirst))));
  102. } else {
  103. $editorName = str_replace('\'','', str_replace('&quot;','',str_replace('get_the_block(','',str_replace($stringLast,'',$stringFirst))));
  104. }
  105. $nextString = $stringLast;
  106. //add editor
  107. echo '<p><strong>'.ucfirst($editorName).'</strong></p>';
  108. echo '<input type="hidden" name="multiplecontent_box-'.$i.'" value="'.$editorName.'" />';
  109. global $current_user;
  110. get_currentuserinfo();
  111. if(get_usermeta($current_user->ID,'rich_editing') == 'true') {
  112. //leave this away when wysigwyg is disabled
  113. echo '<a id="edButtonHTML" class="hide-if-no-js" onclick="switchEditors.go(\'multiplecontent_box-'.$editorName.'\', \'html\');">HTML</a><a id="edButtonPreview" class="active hide-if-no-js" onclick="switchEditors.go(\'multiplecontent_box-'.$editorName.'\', \'tinymce\');">Wysiwyg</a>';
  114. }
  115. echo '<input type="hidden" name="multiplecontent_box-'.$editorName.'-nonce" id="multiplecontent_box-'.$editorName.'-nonce" value="'.wp_create_nonce("multiplecontent_box-".$editorName."-nonce").'" />'."\n"; //nonce
  116. echo '<textarea id="multiplecontent_box-'.$editorName.'" tabindex="2" name="multiplecontent_box-'.$editorName.'" cols="158" class="theEditor" rows="15">';
  117. $content = get_post_meta($post->ID, '_ot_multiplecontent_box-'.$editorName , true);
  118. echo apply_filters('the_editor_content', $content);
  119. echo '</textarea>';
  120. echo '<p>&nbsp;</p>';
  121. }
  122. if($editors == 0) {
  123. _e('There are no content blocks in this template.','trendwerk');
  124. }
  125. }
  126. function read_tag($tag,$contents) {
  127. $theTag = strstr($contents,'get_'.$tag.'(');
  128. //when the tag doesnt exist, return nothing, or it will take the standard file
  129. if(!$theTag) {
  130. return '';
  131. }
  132. $theTag = str_replace('get_'.$tag.'(','',$theTag);
  133. if(strpos($theTag,')') != 0) {
  134. $theTag = substr($theTag,1, strpos($theTag,')')-2);
  135. } else {
  136. $theTag = '';
  137. }
  138. $fileToRead = get_template_directory_uri().'/';
  139. $fileToRead .= $tag;
  140. if($theTag) {
  141. $fileToRead .= '-'.$theTag;
  142. }
  143. $fileToRead .= '.php';
  144. $fileToRead = strstr($fileToRead,'/themes/');
  145. $fileToRead = validate_file_to_edit($fileToRead, $allowed_files);
  146. $fileToRead = get_real_file_to_edit($fileToRead);
  147. //first try to read the child theme, otherwise use the normal theme
  148. $themes = get_themes();
  149. $theme = $themes[get_current_theme()];
  150. $current_theme_url = $theme['Template'];
  151. $child_theme_url = str_replace('themes/','',strstr(get_stylesheet_directory_uri(),'themes/'));
  152. if(fopen(str_replace($current_theme_url,$child_theme_url,$fileToRead), 'r')) { //child theme exists
  153. $fileToRead = str_replace($current_theme_url,$child_theme_url,$fileToRead);
  154. $f = fopen($fileToRead, 'r');
  155. } else {
  156. $f = fopen($fileToRead, 'r');
  157. }
  158. $tagContents = fread($f, filesize($fileToRead));
  159. $tagContents = htmlspecialchars( $tagContents );
  160. return $tagContents;
  161. }
  162. function save_multiplecontent_box($id) {
  163. for($i=0;$i>-1;$i++) {
  164. if (!wp_verify_nonce($_POST['multiplecontent_box-'.$_POST['multiplecontent_box-'.$i].'-nonce'],"multiplecontent_box-".$_POST['multiplecontent_box-'.$i]."-nonce")) return $id; //nonce
  165. if(isset($_POST['multiplecontent_box-'.$_POST['multiplecontent_box-'.$i]])) {
  166. $contents = '';
  167. $contents = apply_filters('content_save_pre',$_POST['multiplecontent_box-'.$_POST['multiplecontent_box-'.$i]]);
  168. if($contents) update_post_meta($id, "_ot_multiplecontent_box-".$_POST['multiplecontent_box-'.$i] , $contents);
  169. else delete_post_meta($id,"_ot_multiplecontent_box-".$_POST['multiplecontent_box-'.$i]);
  170. } else {
  171. break;
  172. }
  173. }
  174. }
  175. //front end
  176. function the_block($blockName) {
  177. if($blockName) {
  178. global $post;
  179. $content = get_post_meta($post->ID, '_ot_multiplecontent_box-'.$blockName , true);
  180. echo apply_filters('the_content', $content);
  181. }
  182. }
  183. function get_the_block($blockName) {
  184. if($blockName) {
  185. global $post;
  186. $content = get_post_meta($post->ID, '_ot_multiplecontent_box-'.$blockName , true);
  187. return apply_filters('the_content', $content);
  188. }
  189. }
  190. ?>