PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/table-it.php

https://github.com/seankelly/wp-sports-reference
PHP | 225 lines | 139 code | 41 blank | 45 comment | 18 complexity | ee05f77c50892f7fd5640b197fb18ee7 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. Copyright (C) 2012-2014 Sean Kelly
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. */
  16. /**
  17. * @package table-it
  18. * @version 2.0
  19. */
  20. /*
  21. Plugin Name: Table It
  22. Plugin URI:
  23. Description: Converts CSV formatted data to a table.
  24. Author: Sean Kelly
  25. Version: 2.0
  26. Author URI:
  27. License: GPL2
  28. */
  29. namespace WGOM;
  30. class TableIt {
  31. const VERSION = 2.0;
  32. public function init() {
  33. // Add the shortcode for posts.
  34. \add_shortcode('sr', array('WGOM\TableIt', 'sr_shortcode_cb'));
  35. \add_shortcode('table', array('WGOM\TableIt', 'table_shortcode_cb'));
  36. // This is for comment text. The shortcode is safe for including in
  37. // comments, plus I think most people would want to use it there.
  38. \add_filter('comment_text', array('WGOM\TableIt', 'shortcode_in_comment'));
  39. \add_action('wp_enqueue_scripts', array('WGOM\TableIt', 'enqueue_scripts'));
  40. }
  41. public function enqueue_scripts() {
  42. \wp_enqueue_style('table-it', plugins_url('sr-table.css', __FILE__));
  43. \wp_enqueue_script('table-it',
  44. \plugins_url('table-sort.js', __FILE__),
  45. array('jquery'),
  46. TableIt::VERSION,
  47. true
  48. );
  49. }
  50. public function shortcode_in_comment($content) {
  51. global $shortcode_tags;
  52. if (empty($shortcode_tags) || !is_array($shortcode_tags)) {
  53. return $content;
  54. }
  55. $_shortcode_tags = $shortcode_tags;
  56. $shortcode_tags = array();
  57. \add_shortcode('sr', array('WGOM\TableIt', 'sr_shortcode_cb'));
  58. \add_shortcode('table', array('WGOM\TableIt', 'table_shortcode_cb'));
  59. $new_content = \do_shortcode($content);
  60. $shortcode_tags = $_shortcode_tags;
  61. return $new_content;
  62. }
  63. public function sr_shortcode_cb($attrs, $content=null) {
  64. if (is_null($content)) {
  65. return '';
  66. }
  67. $thead = FALSE;
  68. $final_html = array();
  69. // Remove any '<br />' from the content.
  70. $content = preg_replace('|<br />$|m', '', $content);
  71. // Content is not null. Assume it's CSV data and try to make sense of it.
  72. $rows = str_getcsv($content, "\n");
  73. foreach ($rows as &$row) {
  74. $len = strlen($row);
  75. if ($len === 0) {
  76. continue;
  77. }
  78. $fields = str_getcsv($row);
  79. // Count the number of a-z character to try guess if it's a header.
  80. $count = preg_match_all('/[a-z]/i', $row, $matches);
  81. $is_header = (($count / $len) > 0.5);
  82. array_push($final_html, TableIt::make_row($is_header, $fields, $thead));
  83. }
  84. return ('<table class="sports-reference nozebra">'
  85. . implode("", $final_html)
  86. . '</table>');
  87. }
  88. public function table_shortcode_cb($attrs, $content=null) {
  89. if (is_null($content)) {
  90. return '';
  91. }
  92. $delimiter = ',';
  93. if (array_key_exists($attrs, 'delimiter')) {
  94. $delimiter = $attrs['delimiter'];
  95. }
  96. $thead = FALSE;
  97. $final_html = array();
  98. // Remove any '<br />' from the content.
  99. $content = preg_replace('|<br />$|m', '', $content);
  100. // Content is not null. Assume it's CSV data and try to make sense of it.
  101. $rows = str_getcsv($content, "\n");
  102. foreach ($rows as &$row) {
  103. $len = strlen($row);
  104. if ($len === 0) {
  105. continue;
  106. }
  107. $fields = str_getcsv($row, $delimiter);
  108. // Count the number of a-z character to try guess if it's a header.
  109. $count = preg_match_all('/[a-z]/i', $row, $matches);
  110. $is_header = (($count / $len) > 0.5);
  111. array_push($final_html, TableIt::make_row($is_header, $fields, $thead));
  112. }
  113. return ('<table>'
  114. . implode("", $final_html)
  115. . '</table>');
  116. }
  117. private function make_row($is_header, $columns, &$thead) {
  118. $row_classes = array();
  119. // Loop through the columns twice in order to count the number of cells
  120. // that have non-numeric characters in them.
  121. $alpha_cells = 0;
  122. $cells = count($columns);
  123. $data_len = 0;
  124. foreach ($columns as &$col) {
  125. $count = preg_match_all('/[-+,.0-9]/i', $col, $matches);
  126. $len = strlen($col);
  127. $data_len += $len;
  128. if ($len > 0) {
  129. $alpha_cells += (($count / $len) <= 0.25);
  130. }
  131. }
  132. $row_classes[] = "alpha-cells-$alpha_cells";
  133. if ($alpha_cells >= (0.80 * $cells)) {
  134. $tag = 'th';
  135. if ($thead === TRUE) {
  136. $row_classes[] = 'thead';
  137. }
  138. }
  139. else {
  140. $tag = 'td';
  141. }
  142. foreach ($columns as &$col) {
  143. $col = TableIt::wrap_column($col, $tag);
  144. }
  145. // If there is no data in the row, mark it as blank to
  146. // de-emphasize the row.
  147. if ($data_len === 0) {
  148. $row_classes[] = 'blank_row';
  149. }
  150. $row_class = '';
  151. if (count($row_classes) > 0) {
  152. $row_class = ' class="' . implode(' ', $row_classes) . '"';
  153. }
  154. $html = '<tr' . $row_class . '>' . implode('', $columns) . '</tr>';
  155. // Mark the first row as part of the thead section. This makes
  156. // sorting much easier, as it can handle just the body rows.
  157. if ($thead === FALSE) {
  158. $thead = TRUE;
  159. $html = '<thead>' . $html . '</thead>';
  160. }
  161. return $html;
  162. }
  163. private function wrap_column($column, $tag) {
  164. $align = '';
  165. $count = preg_match_all('/[a-z]/i', $column, $matches);
  166. $len = strlen($column);
  167. if (($len > 0) && (($count / $len) >= 0.5)) {
  168. $align = ' align="left"';
  169. }
  170. else {
  171. $align = ' align="right"';
  172. }
  173. if ($tag === 'th') {
  174. $align = ' align="center"';
  175. }
  176. return ('<' . $tag . $align . '>'
  177. . $column
  178. . '</' . $tag . '>');
  179. }
  180. }
  181. TableIt::init();