-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv-it.php
More file actions
120 lines (93 loc) · 3.03 KB
/
csv-it.php
File metadata and controls
120 lines (93 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/*
* CSV-converter class
* @link
* @since 1.0
*
* @package csv-it-plugin
* @subpackage csv-it-plugin
*/
namespace CsvItPlugin;
class Csv_It {
public function __construct() {
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_styles'));
add_shortcode('csv_it',array($this, 'shortcode'));
}
/*
This method implements an injection of the plugin's style sheet
*/
public function enqueue_styles() {
wp_enqueue_style(
"csv-it-css",
sprintf("%scss/csv-it.css", plugin_dir_url(__FILE__)),
null, null, 'all'
);
}
/*
* The method implements an injection of JavaScripts
*/
public function enqueue_scripts() {
wp_enqueue_script(
"scv-it-script",
sprintf("%sjs/csv-it.js", plugin_dir_url(__FILE__)),
array("jquery"),
false, false
);
}
/*
The method implements a shortcode functionality
*/
public function shortcode($atts = [], $content = "", $tag = null) {
$atts = array_change_key_case($atts, CASE_LOWER);
$content = do_shortcode($content);
$separator = isset($atts["separator"]) ? $atts["separator"] : ",";
return sprintf(
'<div class="csv-it-block">
<div class="csv-it-header">
<a class="csv-it-link">
<img src="%s" alt="CSV" title="CSV" width="20" height="20" />
</a>
</div>
<div class="csv-it-source" name="csv-it-source">%s</div>
<div class="csv-it-result" name="csv-it-result">%s</div>
</div>',
sprintf("%simg/csv-it.png", plugin_dir_url(__FILE__)), // image source file
$content, // content (source)
$this->prepare_csv($content,$separator) // Transformed content - the result of the convertion
);
}
/*
The method takes a content of the shortcode,
obrains table rows and cells
and create a csv version of the table data
*/
public function prepare_csv($content, $separator) {
$res = "<div class=''>";
// simplify tags
$content = preg_replace("/<table[^>]*>/", "<table>", $content);
$content = preg_replace("/<tr[^>]*>/", "<tr>", $content);
$content = preg_replace("/<td[^>]*>/", "<td>", $content);
$content = preg_replace("/<th[^>]*>/", "<th>", $content);
$content = preg_replace("/<thead[^>]*>/", "<thead>", $content);
$content = preg_replace("/<tbody[^>]*>/", "<tbody>", $content);
// strip all other tags
$content = strip_tags($content,"<table><tr><td><th><thead><tbody>");
// replace th tags with td, because they are equal for CSV format
$content = preg_replace("/<th>/","<td>", $content);
$content = preg_replace("/<\/th>/","</td>", $content);
$content = str_replace(" ","",$content);
// get all rows in content
preg_match_all("/<tr>(.*?)<\/tr>/is", $content, $rows);
foreach($rows[1] as $row) {
// get all cells in each row
preg_match_all("/<td>(.*?)<\/td>/is",$row, $cols);
// create a CSV-row using table cells values
$res .= implode($separator, $cols[1]);
$res .= "\n";
}
$res .= "</div>";
return $res;
}
}
?>