PageRenderTime 25ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/contact-form-7-to-database-extension/ExportToHtmlTemplate.php

https://gitlab.com/mattswann/launch-housing
PHP | 232 lines | 151 code | 26 blank | 55 comment | 46 complexity | e2570eabe4da526e5e0b59f215cba3cf MD5 | raw file
  1. <?php
  2. /*
  3. "Contact Form to Database" Copyright (C) 2011-2012 Michael Simpson (email : michael.d.simpson@gmail.com)
  4. This file is part of Contact Form to Database.
  5. Contact Form to Database is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Contact Form to Database is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Contact Form to Database.
  15. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. require_once('ExportBase.php');
  18. require_once('CFDBExport.php');
  19. require_once('CFDBShortCodeContentParser.php');
  20. class ExportToHtmlTemplate extends ExportBase implements CFDBExport {
  21. /**
  22. * @param $formName string
  23. * @param $options array of option_name => option_value
  24. * @return void|string
  25. */
  26. public function export($formName, $options = null) {
  27. $this->setOptions($options);
  28. $this->setCommonOptions(true);
  29. $filelinks = '';
  30. $wpautop = false;
  31. $stripBR = false;
  32. if ($this->options && is_array($this->options)) {
  33. if (isset($this->options['filelinks'])) {
  34. $filelinks = $this->options['filelinks'];
  35. }
  36. if (isset($this->options['wpautop'])) {
  37. $wpautop = $this->options['wpautop'] == 'true';
  38. }
  39. if (isset($this->options['stripbr'])) {
  40. $stripBR = $this->options['stripbr'] == 'true';
  41. }
  42. }
  43. // Security Check
  44. if (!$this->isAuthorized()) {
  45. $this->assertSecurityErrorMessage();
  46. return;
  47. }
  48. // Headers
  49. $this->echoHeaders('Content-Type: text/html; charset=UTF-8');
  50. if (empty($options) || !isset($options['content'])) {
  51. return;
  52. }
  53. // Get the data
  54. $submitTimeKeyName = 'Submit_Time_Key';
  55. $this->setDataIterator($formName, $submitTimeKeyName);
  56. //$this->clearAllOutputBuffers(); // will mess up admin view of single entry
  57. if ($this->isFromShortCode) {
  58. ob_start();
  59. }
  60. $options['content'] = $this->modifyContent($options['content']);
  61. $matches = array();
  62. preg_match_all('/\$\{([^}]+)\}/', $options['content'], $matches);
  63. $colNamesToSub = array();
  64. $varNamesToSub = array();
  65. if (!empty($matches) && is_array($matches[1])) {
  66. foreach ($matches[1] as $aSubVar) {
  67. // Each is expected to be a name of a column
  68. if (in_array($aSubVar, $this->dataIterator->getDisplayColumns())) {
  69. $colNamesToSub[] = $aSubVar;
  70. $varNamesToSub[] = '${' . $aSubVar . '}';
  71. }
  72. else if ($aSubVar == 'submit_time') {
  73. $colNamesToSub[] = 'submit_time';
  74. $varNamesToSub[] = '${submit_time}';
  75. }
  76. }
  77. }
  78. // WordPress likes to wrap the content in <br />content<p> which messes up things when
  79. // you are putting
  80. // <tr><td>stuff<td></tr>
  81. // as the content because it comes out
  82. // <br /><tr><td>stuff<td></tr><p>
  83. // which messed up the table html.
  84. // So we try to identify that and strip it out.
  85. // This is related to http://codex.wordpress.org/Function_Reference/wpautop
  86. // see also http://wordpress.org/support/topic/shortcodes-are-wrapped-in-paragraph-tags?replies=4
  87. if (!$wpautop) {
  88. //echo 'Initial: \'' . htmlentities($options['content']) . '\'';
  89. if (substr($options['content'], 0, 6) == '<br />' &&
  90. substr($options['content'], -3, 3) == '<p>') {
  91. $options['content'] = substr($options['content'], 6, strlen($options['content']) - 6 - 3);
  92. }
  93. if (substr($options['content'], 0, 4) == '</p>' &&
  94. substr($options['content'], -3, 3) == '<p>') {
  95. $options['content'] = substr($options['content'], 4, strlen($options['content']) - 4 - 3);
  96. }
  97. //echo '<br/>Stripped: \'' . htmlentities($options['content']) . '\'';
  98. }
  99. if ($stripBR) {
  100. // Strip out BR tags presumably injected by wpautop
  101. $options['content'] = str_replace('<br />', '', $options['content']);
  102. }
  103. // Break out sections: Before, Template, After
  104. $before = '';
  105. $template = '';
  106. $after = '';
  107. if (isset($options['content'])) {
  108. $contentParser = new CFDBShortCodeContentParser;
  109. list($before, $template, $after) = $contentParser->parseBeforeContentAfter($options['content']);
  110. }
  111. if ($before) {
  112. // Allow for short codes in "before"
  113. echo do_shortcode($before);
  114. }
  115. while ($this->dataIterator->nextRow()) {
  116. // todo: Evaluation IF-expressions
  117. if (empty($colNamesToSub)) {
  118. // Process nested short codes
  119. echo do_shortcode($template);
  120. }
  121. else {
  122. $fields_with_file = null;
  123. if ($filelinks != 'name' &&
  124. isset($this->dataIterator->row['fields_with_file']) &&
  125. $this->dataIterator->row['fields_with_file'] != null) {
  126. $fields_with_file = explode(',', $this->dataIterator->row['fields_with_file']);
  127. }
  128. $replacements = array();
  129. foreach ($colNamesToSub as $aCol) {
  130. if ($fields_with_file && in_array($aCol, $fields_with_file)) {
  131. switch ($filelinks) {
  132. case 'url':
  133. $replacements[] = $this->plugin->getFileUrl($this->dataIterator->row[$submitTimeKeyName], $formName, $aCol);
  134. break;
  135. case 'link':
  136. if (isset($this->dataIterator->row[$aCol])) {
  137. $replacements[] =
  138. '<a href="' .
  139. $this->plugin->getFileUrl($this->dataIterator->row[$submitTimeKeyName], $formName, $aCol) .
  140. '">' .
  141. htmlentities($this->dataIterator->row[$aCol], null, 'UTF-8') .
  142. '</a>';
  143. } else {
  144. $replacements[] = '';
  145. }
  146. break;
  147. case 'image':
  148. case 'img':
  149. if (isset($this->dataIterator->row[$aCol])) {
  150. $replacements[] =
  151. '<img src="' .
  152. $this->plugin->getFileUrl($this->dataIterator->row[$submitTimeKeyName], $formName, $aCol) .
  153. '" alt="' .
  154. htmlentities($this->dataIterator->row[$aCol], null, 'UTF-8') .
  155. '" />';
  156. } else {
  157. $replacements[] = '';
  158. }
  159. break;
  160. case 'name':
  161. default:
  162. if (isset($this->dataIterator->row[$aCol])) {
  163. $replacements[] = htmlentities($this->dataIterator->row[$aCol], null, 'UTF-8');
  164. }
  165. }
  166. } else {
  167. if (isset($this->dataIterator->row[$aCol])) {
  168. $replacements[] = htmlentities($this->dataIterator->row[$aCol], null, 'UTF-8');
  169. } else {
  170. $replacements[] = '';
  171. }
  172. }
  173. }
  174. // Preserve line breaks in the field
  175. foreach ($replacements as $i => $repl) {
  176. $replacements[$i] = nl2br($replacements[$i]); // preserve line breaks
  177. }
  178. // Process nested short codes
  179. echo do_shortcode(str_replace($varNamesToSub, $replacements, $template));
  180. }
  181. }
  182. if ($after) {
  183. // Allow for short codes in "after"
  184. echo do_shortcode($after);
  185. }
  186. if ($this->isFromShortCode) {
  187. // If called from a shortcode, need to return the text,
  188. // otherwise it can appear out of order on the page
  189. $output = ob_get_contents();
  190. ob_end_clean();
  191. return $output;
  192. }
  193. }
  194. /**
  195. * Intended to be overridden
  196. * @param $template string
  197. * @return string
  198. */
  199. public function modifyContent($template) {
  200. return $template;
  201. }
  202. }