Skip to content

Commit a8cf390

Browse files
Integrate https://www.datatables.net as new table type
1 parent 1466d2b commit a8cf390

File tree

18 files changed

+635
-376
lines changed

18 files changed

+635
-376
lines changed

classes/Visualizer/Module.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,78 @@ protected function get_user_customization_js() {
445445
return $specific;
446446
}
447447

448+
/**
449+
* Load the class for the given chart's chart type so that its assets can be loaded.
450+
*/
451+
protected function load_chart_type( $chart_id ) {
452+
$type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
453+
$name = 'Visualizer_Render_Sidebar_Type_' . ucwords( $type );
454+
$class = null;
455+
if ( class_exists( $name ) ) {
456+
$class = new $name;
457+
} else if ( true === apply_filters( 'visualizer_load_chart', false, $name ) ) {
458+
$class = new $name;
459+
}
460+
return is_null( $class ) ? null : $class->getLibrary();
461+
}
462+
463+
/**
464+
* Generates the inline CSS to apply to the chart and adds these classes to the settings.
465+
*
466+
* @access public
467+
* @param int $id The id of the chart.
468+
* @param array $settings The settings of the chart.
469+
*/
470+
protected function get_inline_custom_css( $id, $settings ) {
471+
$css = '';
472+
473+
$arguments = array( '', $settings );
474+
if ( ! isset( $settings['customcss'] ) ) {
475+
return $arguments;
476+
}
477+
478+
$classes = array();
479+
$css = '<style type="text/css" name="visualizer-custom-css" id="customcss-' . $id . '">';
480+
foreach ( $settings['customcss'] as $name => $element ) {
481+
$attributes = array();
482+
foreach ( $element as $property => $value ) {
483+
$attributes[] = $this->handle_css_property( $property, $value );
484+
}
485+
$class_name = $id . $name;
486+
$properties = implode( '; ', array_filter( $attributes ) );
487+
if ( ! empty( $properties ) ) {
488+
$css .= '.' . $class_name . ' {' . $properties . ' !important;}';
489+
$classes[ $name ] = $class_name;
490+
}
491+
}
492+
$css .= '</style>';
493+
494+
$settings['cssClassNames'] = $classes;
495+
496+
$arguments = array( $css, $settings );
497+
apply_filters_ref_array( 'visualizer_inline_css', array( &$arguments ) );
498+
499+
return $arguments;
500+
}
501+
502+
/**
503+
* Handles CSS properties that might need special syntax.
504+
*
505+
* @access private
506+
* @param string $property The name of the css property.
507+
* @param string $value The value of the css property.
508+
*/
509+
private function handle_css_property( $property, $value ) {
510+
if ( empty( $property ) || empty( $value ) ) {
511+
return '';
512+
}
513+
514+
switch ( $property ) {
515+
case 'transform':
516+
$value = 'rotate(' . $value . 'deg)';
517+
break;
518+
}
519+
return $property . ': ' . $value;
520+
}
521+
448522
}

classes/Visualizer/Module/Admin.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public static function _getChartTypesLocalized( $enabledOnly = false, $get2Darra
326326
'name' => esc_html__( 'Candlestick', 'visualizer' ),
327327
'enabled' => true,
328328
),
329-
'htmltable' => array(
329+
'dataTable' => array(
330330
'name' => esc_html__( 'Table (New)', 'visualizer' ),
331331
'enabled' => true,
332332
),
@@ -425,14 +425,12 @@ public function enqueueLibraryScripts( $suffix ) {
425425
$query = $this->getQuery();
426426
while ( $query->have_posts() ) {
427427
$chart = $query->next_post();
428-
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
429-
$name = 'Visualizer_Render_Sidebar_Type_' . ucwords( $type );
430-
if ( ! class_exists( $name ) ) {
428+
$library = $this->load_chart_type( $chart->ID );
429+
if ( is_null( $library ) ) {
431430
continue;
432431
}
433-
$class = new $name;
434432
wp_enqueue_script(
435-
"visualizer-render-$type",
433+
"visualizer-render-$library",
436434
VISUALIZER_ABSURL . 'js/render-facade.js',
437435
apply_filters( 'visualizer_assets_render', array( 'visualizer-library', 'visualizer-customization' ), true ),
438436
Visualizer_Plugin::VERSION,
@@ -554,25 +552,43 @@ public function renderLibraryPage() {
554552
$filter = 'all';
555553
}
556554

555+
$css = '';
557556
while ( $query->have_posts() ) {
558557
$chart = $query->next_post();
559558

560559
// if the user has updated a chart and instead of saving it, has closed the modal. If the user refreshes, they should get the original chart.
561560
$chart = $this->handleExistingRevisions( $chart->ID, $chart );
562561

562+
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
563+
563564
// fetch and update settings
564565
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
566+
567+
$settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
568+
if ( ! empty( $atts['settings'] ) ) {
569+
$settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type );
570+
}
571+
565572
unset( $settings['height'], $settings['width'] );
566-
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
567573
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
568574
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type );
575+
$library = $this->load_chart_type( $chart->ID );
576+
577+
$id = 'visualizer-' . $chart->ID;
578+
$arguments = $this->get_inline_custom_css( $id, $settings );
579+
if ( ! empty( $arguments ) ) {
580+
$css .= $arguments[0];
581+
$settings = $arguments[1];
582+
}
583+
569584
// add chart to the array
570-
$charts[ 'visualizer-' . $chart->ID ] = array(
585+
$charts[ $id ] = array(
571586
'id' => $chart->ID,
572587
'type' => $type,
573588
'series' => $series,
574589
'settings' => $settings,
575590
'data' => $data,
591+
'library' => $library,
576592
);
577593
}
578594
// enqueue charts array
@@ -602,13 +618,15 @@ public function renderLibraryPage() {
602618
$ajaxurl
603619
),
604620
),
621+
'page_type' => 'library',
605622
)
606623
);
607624
// render library page
608625
$render = new Visualizer_Render_Library();
609626
$render->charts = $charts;
610627
$render->type = $filter;
611628
$render->types = self::_getChartTypesLocalized();
629+
$render->custom_css = $css;
612630
$render->pagination = paginate_links(
613631
array(
614632
'base' => add_query_arg( 'vpage', '%#%' ),

classes/Visualizer/Module/Chart.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,28 @@ private function _getChartArray( WP_Post $chart = null ) {
133133
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
134134
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
135135
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type );
136+
$library = $this->load_chart_type( $chart->ID );
137+
138+
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
139+
$settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
140+
if ( ! empty( $atts['settings'] ) ) {
141+
$settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type );
142+
}
143+
144+
$css = '';
145+
$arguments = $this->get_inline_custom_css( 'visualizer-' . $chart->ID, $settings );
146+
if ( ! empty( $arguments ) ) {
147+
$css = $arguments[0];
148+
$settings = $arguments[1];
149+
}
136150

137151
return array(
138152
'type' => $type,
139153
'series' => $series,
140-
'settings' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ),
154+
'settings' => $settings,
141155
'data' => $data,
156+
'library' => $library,
157+
'css' => $css,
142158
);
143159
}
144160

@@ -244,6 +260,9 @@ public function renderChartPages() {
244260
wp_redirect( add_query_arg( 'chart', (int) $chart_id ) );
245261
defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
246262
}
263+
264+
$this->load_chart_type( $chart_id );
265+
247266
// enqueue and register scripts and styles
248267
wp_register_script( 'visualizer-chosen', VISUALIZER_ABSURL . 'js/lib/chosen.jquery.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
249268
wp_register_style( 'visualizer-chosen', VISUALIZER_ABSURL . 'css/lib/chosen.min.css', array(), Visualizer_Plugin::VERSION );
@@ -375,12 +394,14 @@ private function _handleDataAndSettingsPage() {
375394
'permissions' => Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA,
376395
),
377396
),
397+
'page_type' => 'chart',
378398
)
379399
);
380400

381401
$render = new Visualizer_Render_Page_Data();
382402
$render->chart = $this->_chart;
383403
$render->type = $data['type'];
404+
$render->custom_css = $data['css'];
384405
$render->sidebar = $sidebar;
385406
if ( filter_input( INPUT_GET, 'library', FILTER_VALIDATE_BOOLEAN ) ) {
386407
$render->button = filter_input( INPUT_GET, 'action' ) == Visualizer_Plugin::ACTION_EDIT_CHART
@@ -525,6 +546,7 @@ public function uploadData() {
525546
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
526547
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
527548
update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
549+
$render->id = $chart->ID;
528550
$render->data = json_encode( $source->getRawData() );
529551
$render->series = json_encode( $source->getSeries() );
530552
} else {

classes/Visualizer/Module/Frontend.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,39 +234,35 @@ public function renderChart( $atts ) {
234234
$data = apply_filters( $atts['data'], $data, $chart->ID, $type );
235235
}
236236

237-
$id = $id . '-' . rand();
238-
$arguments = array( '', $id, $settings );
239237
$css = '';
240-
apply_filters_ref_array( 'visualizer_pro_inline_css', array( &$arguments ) );
238+
$arguments = $this->get_inline_custom_css( $id, $settings );
241239
if ( ! empty( $arguments ) ) {
242240
$css = $arguments[0];
243-
$settings = $arguments[2];
241+
$settings = $arguments[1];
244242
}
245243

244+
$library = $this->load_chart_type( $chart->ID );
245+
246246
// add chart to the array
247247
$this->_charts[ $id ] = array(
248248
'type' => $type,
249249
'series' => $series,
250250
'settings' => $settings,
251251
'data' => $data,
252+
'library' => $library,
252253
);
253254

254-
$name = 'Visualizer_Render_Sidebar_Type_' . ucwords( $type );
255-
if ( class_exists( $name ) ) {
256-
$classss = new $name;
257-
}
258-
259255
wp_register_script(
260-
"visualizer-render-$type",
256+
"visualizer-render-$library",
261257
VISUALIZER_ABSURL . 'js/render-facade.js',
262258
apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ),
263259
Visualizer_Plugin::VERSION,
264260
true
265261
);
266262

267-
wp_enqueue_script( "visualizer-render-$type" );
263+
wp_enqueue_script( "visualizer-render-$library" );
268264
wp_localize_script(
269-
"visualizer-render-$type",
265+
"visualizer-render-$library",
270266
'visualizer',
271267
array(
272268
'charts' => $this->_charts,
@@ -276,6 +272,7 @@ public function renderChart( $atts ) {
276272
'i10n' => array(
277273
'copied' => __( 'Copied!', 'visualizer' ),
278274
),
275+
'page_type' => 'frontend',
279276
)
280277
);
281278
wp_enqueue_style( 'visualizer-front' );

classes/Visualizer/Render/Library.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private function _renderLibrary() {
7878
$filterBy = filter_input( INPUT_GET, 's', FILTER_SANITIZE_STRING );
7979
}
8080
// Added by Ash/Upwork
81+
echo $this->custom_css;
8182
echo '<div id="visualizer-types" class="visualizer-clearfix">';
8283
echo '<ul class="subsubsub">';
8384
foreach ( $this->types as $type => $array ) {

classes/Visualizer/Render/Page/Data.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ protected function _renderContent() {
4949
echo '<div id="canvas">';
5050
echo '<img src="', VISUALIZER_ABSURL, 'images/ajax-loader.gif" class="loader">';
5151
echo '</div>';
52+
echo $this->custom_css;
5253
}
5354

5455
/**

classes/Visualizer/Render/Page/Update.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function _toHTML() {
4848
echo 'if (win.visualizer) {';
4949
echo 'win.visualizer.charts.canvas.series = ', $this->series, ';';
5050
echo 'win.visualizer.charts.canvas.data = ', $this->data, ';';
51-
echo 'win.visualizer.render();';
51+
echo 'win.visualizer.update();';
5252
echo '}';
5353

5454
// added by Ash/Upwork

classes/Visualizer/Render/Sidebar.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ abstract class Visualizer_Render_Sidebar extends Visualizer_Render {
9191
*/
9292
protected $_supportsAnimation = true;
9393

94+
/**
95+
* Which library does this this chart implement?
96+
*
97+
* @access protected
98+
* @var string
99+
*/
100+
protected $_library = null;
101+
94102
/**
95103
* Constructor.
96104
*
@@ -124,6 +132,8 @@ public function __construct( $data = array() ) {
124132
'1' => esc_html__( 'Yes', 'visualizer' ),
125133
'0' => esc_html__( 'No', 'visualizer' ),
126134
);
135+
136+
$this->hooks();
127137
}
128138

129139
/**
@@ -749,4 +759,11 @@ protected static function _renderTextAreaItem( $title, $name, $value, $desc, $pl
749759
echo '</div>';
750760
}
751761

762+
/**
763+
* Returns the library this chart implements.
764+
*/
765+
public function getLibrary() {
766+
return $this->_library;
767+
}
768+
752769
}

0 commit comments

Comments
 (0)