Skip to content

Commit 1cb2994

Browse files
authored
Merge pull request #148 from contactashish13/issue-47
make sure import from chart is working fine #47
2 parents ec1eaca + 52fabde commit 1cb2994

File tree

4 files changed

+125
-72
lines changed

4 files changed

+125
-72
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ public function __construct( Visualizer_Plugin $plugin ) {
6161

6262
// Added by Ash/Upwork
6363
$this->_addAjaxAction( Visualizer_Plugin::ACTION_EXPORT_DATA, 'exportData' );
64-
if ( defined( 'Visualizer_Pro' ) ) {
65-
global $Visualizer_Pro;
66-
list($action, $name, $class) = $Visualizer_Pro->_getAjaxAction( $this );
67-
$this->_addAjaxAction( $action, $name, $class );
68-
}
6964
// Added by Ash/Upwork
7065
}
7166

@@ -77,12 +72,12 @@ public function __construct( Visualizer_Plugin $plugin ) {
7772
* @access private
7873
* @param array $results The response array.
7974
*/
80-
public function _sendResponse( $results ) {
75+
public static function _sendResponse( $results ) {
8176
header( 'Content-type: application/json' );
8277
nocache_headers();
8378

8479
echo json_encode( $results );
85-
exit;
80+
wp_die();
8681
}
8782

8883
/**
@@ -153,7 +148,7 @@ public function getCharts() {
153148
$charts[] = $chart_data;
154149
}
155150

156-
$this->_sendResponse( array(
151+
self::_sendResponse( array(
157152
'success' => true,
158153
'data' => $charts,
159154
'total' => $query->max_num_pages,
@@ -188,7 +183,7 @@ public function deleteChart() {
188183
}
189184

190185
if ( $is_post ) {
191-
$this->_sendResponse( array( 'success' => $success ) );
186+
self::_sendResponse( array( 'success' => $success ) );
192187
}
193188

194189
wp_redirect( wp_get_referer() );

classes/Visualizer/Render/Page/Data.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,33 @@ class="dashicons dashicons-lock"></span></h2>
150150
<div>
151151
<p class="group-description"><?php _e( 'You can import here data from your previously created charts', 'visualizer' ); ?></p>
152152
<form>
153-
<select name="vz-import-from-chart" id="vz-import-from-chart"
154-
class="visualizer-select">
155-
<option value="#"><?php _e( 'Chart #123', 'visualizer' ); ?></option>
153+
<select name="vz-import-from-chart" id="chart-id" class="visualizer-select">
154+
<?php
155+
$fetch_link = add_query_arg( array(
156+
'action' => Visualizer_Pro::ACTION_FETCH_DATA,
157+
'nonce' => wp_create_nonce(),
158+
), admin_url( 'admin-ajax.php' ) );
159+
160+
$query_args_charts = array(
161+
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
162+
'posts_per_page' => -1,
163+
'no_found_rows' => true,
164+
);
165+
$charts = array();
166+
$query = new WP_Query( $query_args_charts );
167+
while ( $query->have_posts() ) {
168+
$chart = $query->next_post();
169+
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
170+
?>
171+
<option value="<?php echo $chart->ID; ?>"><?php echo empty( $settings['title'] ) ? '#' . $chart->ID : $settings['title']; ?></option>
172+
<?php
173+
}
174+
?>
175+
156176
</select>
157177
</form>
178+
<input type="button" id="existing-chart" class="button button-primary"
179+
value="<?php _e( 'Import Chart', 'visualizer' ); ?>" data-viz-link="<?php echo $fetch_link;?>">
158180
<?php echo apply_filters( 'visualizer_pro_upsell', '' ); ?>
159181
</div>
160182
</div>

languages/visualizer.pot

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ msgid ""
44
msgstr ""
55
"Project-Id-Version: Visualizer: Charts and Graphs Lite 2.0.0\n"
66
"Report-Msgid-Bugs-To: https://github.com/Codeinwp/visualizer/issues\n"
7-
"POT-Creation-Date: 2017-03-16 09:39:48+00:00\n"
7+
"POT-Creation-Date: 2017-03-16 15:47:05+00:00\n"
88
"MIME-Version: 1.0\n"
99
"Content-Type: text/plain; charset=utf-8\n"
1010
"Content-Transfer-Encoding: 8bit\n"
@@ -101,28 +101,28 @@ msgstr ""
101101
msgid "Pro Addon"
102102
msgstr ""
103103

104-
#: classes/Visualizer/Module/Chart.php:332
105-
#: classes/Visualizer/Module/Chart.php:395
104+
#: classes/Visualizer/Module/Chart.php:327
105+
#: classes/Visualizer/Module/Chart.php:390
106106
msgid "You have entered invalid URL. Please, insert proper URL."
107107
msgstr ""
108108

109-
#: classes/Visualizer/Module/Chart.php:409
109+
#: classes/Visualizer/Module/Chart.php:404
110110
msgid "Save Chart"
111111
msgstr ""
112112

113-
#: classes/Visualizer/Module/Chart.php:410
113+
#: classes/Visualizer/Module/Chart.php:405
114114
msgid "Create Chart"
115115
msgstr ""
116116

117-
#: classes/Visualizer/Module/Chart.php:412
117+
#: classes/Visualizer/Module/Chart.php:407
118118
msgid "Insert Chart"
119119
msgstr ""
120120

121-
#: classes/Visualizer/Module/Chart.php:470
121+
#: classes/Visualizer/Module/Chart.php:465
122122
msgid "CSV file with chart data was not uploaded. Please, try again."
123123
msgstr ""
124124

125-
#: classes/Visualizer/Module/Chart.php:485
125+
#: classes/Visualizer/Module/Chart.php:480
126126
msgid "CSV file is broken or invalid. Please, try again."
127127
msgstr ""
128128

@@ -282,39 +282,39 @@ msgstr ""
282282
msgid "You can import here data from your previously created charts"
283283
msgstr ""
284284

285-
#: classes/Visualizer/Render/Page/Data.php:155
286-
msgid "Chart #123"
285+
#: classes/Visualizer/Render/Page/Data.php:179
286+
msgid "Import Chart"
287287
msgstr ""
288288

289-
#: classes/Visualizer/Render/Page/Data.php:164
289+
#: classes/Visualizer/Render/Page/Data.php:186
290290
msgid "Edit current data"
291291
msgstr ""
292292

293-
#: classes/Visualizer/Render/Page/Data.php:172
293+
#: classes/Visualizer/Render/Page/Data.php:194
294294
msgid "You can manually edit the chart data using the spreadsheet like editor."
295295
msgstr ""
296296

297-
#: classes/Visualizer/Render/Page/Data.php:174
297+
#: classes/Visualizer/Render/Page/Data.php:196
298298
msgid "View Editor"
299299
msgstr ""
300300

301-
#: classes/Visualizer/Render/Page/Data.php:174
301+
#: classes/Visualizer/Render/Page/Data.php:196
302302
msgid "Show Chart"
303303
msgstr ""
304304

305-
#: classes/Visualizer/Render/Page/Data.php:184
305+
#: classes/Visualizer/Render/Page/Data.php:206
306306
msgid "Advanced Settings"
307307
msgstr ""
308308

309-
#: classes/Visualizer/Render/Page/Data.php:187
309+
#: classes/Visualizer/Render/Page/Data.php:209
310310
msgid "Chart Settings"
311311
msgstr ""
312312

313-
#: classes/Visualizer/Render/Page/Data.php:198
313+
#: classes/Visualizer/Render/Page/Data.php:220
314314
msgid "Rate our plugin"
315315
msgstr ""
316316

317-
#: classes/Visualizer/Render/Page/Data.php:217
317+
#: classes/Visualizer/Render/Page/Data.php:239
318318
#: classes/Visualizer/Render/Page/Settings.php:55
319319
msgid "Back"
320320
msgstr ""

tests/test-import.php

Lines changed: 78 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,22 @@ public function test_file_import( $file ) {
156156
* @access public
157157
* @dataProvider editorDataProvider
158158
*/
159-
public function test_pro_editor( $data ) {
160-
if ( ! defined( 'VISUALIZER_PRO_VERSION' ) ) {
161-
$this->markTestSkipped( 'PRO not installed/available, skipping test' );
162-
}
159+
public function test_pro_editor($data) {
160+
if ( !defined( 'VISUALIZER_PRO_VERSION' ) ) {
161+
$this->markTestSkipped( 'PRO not installed/available, skipping test');
162+
}
163163

164164
$this->create_chart();
165165
$this->_setRole( 'administrator' );
166166

167167
$_POST = array(
168-
'chart_data' => $data,
168+
'chart_data' => $data
169169
);
170170
$_GET = array(
171171
'nonce' => wp_create_nonce(),
172172
'chart' => $this->chart,
173173
);
174-
$_FILES = array();
174+
$_FILES = array();
175175

176176
// swallow the output
177177
ob_start();
@@ -192,45 +192,81 @@ public function test_pro_editor( $data ) {
192192
$this->assertEquals( $content, $content_line );
193193
}
194194

195+
/**
196+
* Testing fetch from chat feature. We only need to test fetching, because we already have a test case for uploading data
197+
*
198+
* @access public
199+
*/
200+
public function test_pro_fetch_from_chart() {
201+
if ( !defined( 'VISUALIZER_PRO_VERSION' ) ) {
202+
$this->markTestSkipped( 'PRO not installed/available, skipping test');
203+
}
204+
205+
$this->create_chart();
206+
$this->_setRole( 'administrator' );
207+
208+
$_GET = array(
209+
'nonce' => wp_create_nonce(),
210+
'chart_id' => $this->chart,
211+
);
212+
213+
// swallow the output
214+
ob_start();
215+
try {
216+
$this->_handleAjax( 'visualizer-fetch-data' );
217+
} catch ( WPAjaxDieContinueException $e ) {
218+
// We expected this, do nothing.
219+
} catch ( WPAjaxDieStopException $ee) {
220+
// We expected this, do nothing.
221+
}
222+
ob_end_clean();
223+
224+
$response = json_decode( $this->_last_response );
225+
$this->assertInternalType( 'object', $response );
226+
$this->assertObjectHasAttribute( 'success', $response );
227+
$this->assertObjectHasAttribute( 'data', $response );
228+
$this->assertTrue( $response->success );
229+
}
230+
195231
/**
196232
* Provide the "edited" data
197233
*
198234
* @access public
199235
*/
200236
public function editorDataProvider() {
201-
$data = array();
202-
$file = VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'line.csv';
203-
if ( ($handle = fopen( $file, 'r' )) !== false ) {
204-
$row = 0;
205-
while ( ($line = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE )) !== false ) {
206-
if ( $row++ <= 1 ) {
207-
$cols = count( $line );
208-
$datum = array();
209-
for ( $col = 0; $col < $cols; $col++ ) {
210-
$datum[] = '"' . $line[ $col ] . '"';
211-
}
212-
} else {
213-
$cols = count( $line );
214-
$datum = array();
215-
for ( $col = 0; $col < $cols; $col++ ) {
216-
if ( is_numeric( $line[ $col ] ) ) {
217-
// multiply all numbers by 10
218-
$datum[] = $line[ $col ] * 10;
219-
} else {
220-
$datum[] = '"' . $line[ $col ] . '"';
221-
}
222-
}
223-
}
224-
$data[] = $datum;
225-
}
226-
}
237+
$data = array();
238+
$file = VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'line.csv';
239+
if (($handle = fopen($file, "r")) !== FALSE) {
240+
$row = 0;
241+
while (($line = fgetcsv($handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE)) !== FALSE) {
242+
if ($row++ <= 1) {
243+
$cols = count($line);
244+
$datum = array();
245+
for ($col = 0; $col < $cols; $col++) {
246+
$datum[] = '"'. $line[$col] . '"';
247+
}
248+
} else {
249+
$cols = count($line);
250+
$datum = array();
251+
for ($col = 0; $col < $cols; $col++) {
252+
if (is_numeric($line[$col])) {
253+
// multiply all numbers by 10
254+
$datum[] = $line[$col] * 10;
255+
} else {
256+
$datum[] = '"' . $line[$col] . '"';
257+
}
258+
}
259+
}
260+
$data[] = $datum;
261+
}
262+
}
227263

228-
$csv = array();
229-
foreach ( $data as $row ) {
230-
$csv[] = '[' . implode( ',', $row ) . ']';
231-
}
232-
$csv = '[' . implode( ',', $csv ) . ']';
233-
return array( array( $csv ) );
264+
$csv = array();
265+
foreach ($data as $row) {
266+
$csv[] = "[" . implode(",", $row) . "]";
267+
}
268+
$csv = "[" . implode(",", $csv) . "]";
269+
return array(array($csv));
234270
}
235271
/**
236272
* Provide the fileURL for uploading the file
@@ -239,8 +275,8 @@ public function editorDataProvider() {
239275
*/
240276
public function fileProvider() {
241277
return array(
242-
array( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv' ),
243-
);
278+
array(VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . 'bar.csv')
279+
);
244280
}
245281

246282
/**
@@ -250,7 +286,7 @@ public function fileProvider() {
250286
*/
251287
public function urlProvider() {
252288
return array(
253-
array( 'http://localhost/wp-content/plugins/wp-visualizer/samples/bar.csv' ),
254-
);
289+
array('http://localhost/wp-content/plugins/wp-visualizer/samples/bar.csv')
290+
);
255291
}
256292
}

0 commit comments

Comments
 (0)