Skip to content

Commit

Permalink
Merge pull request #304 from Codeinwp/development
Browse files Browse the repository at this point in the history
Added filter to enable users to change schedule of charts.
Fixed bug with line chart with timeofday column.
Fixed bug with scheduled charts that sometimes did not show updated data.
Javascript can be customized on a per user basis that will not be wiped out on update.
  • Loading branch information
contactashish13 authored Oct 11, 2018
2 parents 7088096 + 4dd54f3 commit 5f00c83
Show file tree
Hide file tree
Showing 16 changed files with 200 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ deploy:
after_deploy:
- chmod +x bin/deploy.sh
- ". ./bin/deploy.sh"
after_failure:
- cat "logs/phpcs.log"
50 changes: 50 additions & 0 deletions classes/Visualizer/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,54 @@ protected function get_language() {
return reset( $array );
}

/**
* Gets/creates the JS where user-specific customizations can be/have been added.
*/
protected function get_user_customization_js() {
// use this as the JS file in case we are not able to create the file in uploads.
$default = VISUALIZER_ABSURL . 'js/customization.js';

$uploads = wp_get_upload_dir();
$specific = $uploads['baseurl'] . '/visualizer/customization.js';

// for testing on user sites (before we send them the correctly customized file).
if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) {
return $default;
}

require_once( ABSPATH . 'wp-admin/includes/file.php' );
WP_Filesystem();
global $wp_filesystem;

$dir = $wp_filesystem->wp_content_dir() . 'uploads/visualizer';
$file = $wp_filesystem->wp_content_dir() . 'uploads/visualizer/customization.js';

if ( $wp_filesystem->is_readable( $file ) ) {
return $specific;
}

if ( $wp_filesystem->exists( $file ) && ! $wp_filesystem->is_readable( $file ) ) {
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to read file %s', $file ), 'error', __FILE__, __LINE__ );
return $default;
}

if ( ! $wp_filesystem->exists( $dir ) ) {
if ( ( $done = $wp_filesystem->mkdir( $dir ) ) === false ) {
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ );
return $default;
}
}

// if file does not exist, copy.
if ( ! $wp_filesystem->exists( $file ) ) {
$src = str_replace( ABSPATH, $wp_filesystem->abspath(), VISUALIZER_ABSPATH . '/js/customization.js' );
if ( ( $done = $wp_filesystem->copy( $src, $file ) ) === false ) {
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to copy file %s to %s', $src, $file ), 'error', __FILE__, __LINE__ );
return $default;
}
}

return $specific;
}

}
40 changes: 30 additions & 10 deletions classes/Visualizer/Module/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ public function enqueueMediaScripts() {
wp_enqueue_script( 'visualizer-media-controller', VISUALIZER_ABSURL . 'js/media/controller.js', array( 'visualizer-media-collection' ), Visualizer_Plugin::VERSION, true );
wp_enqueue_script( 'visualizer-media-view', VISUALIZER_ABSURL . 'js/media/view.js', array( 'visualizer-media-controller' ), Visualizer_Plugin::VERSION, true );
wp_localize_script(
'visualizer-media-view', 'visualizer', array(
'visualizer-media-view',
'visualizer',
array(
'i10n' => array(
'insert' => __( 'Insert', 'visualizer' ),
),
Expand Down Expand Up @@ -286,7 +288,8 @@ public static function _getChartTypesLocalized( $enabledOnly = false, $get2Darra
}

$types = array_merge(
$additional, array(
$additional,
array(
'pie' => array(
'name' => esc_html__( 'Pie', 'visualizer' ),
'enabled' => true,
Expand Down Expand Up @@ -403,18 +406,28 @@ public function enqueueLibraryScripts( $suffix ) {
$this->_addFilter( 'media_upload_tabs', 'setupVisualizerTab' );
wp_enqueue_media();
wp_enqueue_script(
'visualizer-library', VISUALIZER_ABSURL . 'js/library.js', array(
'visualizer-library',
VISUALIZER_ABSURL . 'js/library.js',
array(
'jquery',
'media-views',
), Visualizer_Plugin::VERSION, true
),
Visualizer_Plugin::VERSION,
true
);
wp_enqueue_script( 'google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
wp_enqueue_script( 'google-jsapi-old', '//www.google.com/jsapi', array( 'google-jsapi-new' ), null, true );
wp_enqueue_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
wp_enqueue_script(
'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array(
'visualizer-render',
VISUALIZER_ABSURL . 'js/render.js',
array(
'google-jsapi-old',
'visualizer-library',
), Visualizer_Plugin::VERSION, true
'visualizer-customization',
),
Visualizer_Plugin::VERSION,
true
);
}
}
Expand Down Expand Up @@ -459,7 +472,10 @@ public function registerAdminMenu() {
public function renderLibraryPage() {
// get current page
$page = filter_input(
INPUT_GET, 'vpage', FILTER_VALIDATE_INT, array(
INPUT_GET,
'vpage',
FILTER_VALIDATE_INT,
array(
'options' => array(
'min_range' => 1,
'default' => 1,
Expand Down Expand Up @@ -525,7 +541,9 @@ public function renderLibraryPage() {
// enqueue charts array
$ajaxurl = admin_url( 'admin-ajax.php' );
wp_localize_script(
'visualizer-library', 'visualizer', array(
'visualizer-library',
'visualizer',
array(
'language' => $this->get_language(),
'map_api_key' => get_option( 'visualizer-map-api-key' ),
'charts' => $charts,
Expand All @@ -536,13 +554,15 @@ public function renderLibraryPage() {
'action' => Visualizer_Plugin::ACTION_CREATE_CHART,
'library' => 'yes',
'type' => isset( $_GET['type'] ) ? $_GET['type'] : '',
), $ajaxurl
),
$ajaxurl
),
'edit' => add_query_arg(
array(
'action' => Visualizer_Plugin::ACTION_EDIT_CHART,
'library' => 'yes',
), $ajaxurl
),
$ajaxurl
),
),
)
Expand Down
52 changes: 40 additions & 12 deletions classes/Visualizer/Module/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public function getCharts() {
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
'posts_per_page' => 9,
'paged' => filter_input(
INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
INPUT_GET,
'page',
FILTER_VALIDATE_INT,
array(
'options' => array(
'min_range' => 1,
'default' => 1,
Expand Down Expand Up @@ -171,7 +174,10 @@ public function deleteChart() {
$capable = current_user_can( 'delete_posts' );
if ( $nonce && $capable ) {
$chart_id = filter_input(
$input_method, 'chart', FILTER_VALIDATE_INT, array(
$input_method,
'chart',
FILTER_VALIDATE_INT,
array(
'options' => array(
'min_range' => 1,
),
Expand Down Expand Up @@ -227,7 +233,9 @@ public function renderChartPages() {
add_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
add_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
add_post_meta(
$chart_id, Visualizer_Plugin::CF_SETTINGS, array(
$chart_id,
Visualizer_Plugin::CF_SETTINGS,
array(
'focusTarget' => 'datum',
)
);
Expand All @@ -244,18 +252,28 @@ public function renderChartPages() {
wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION, true );
wp_register_script( 'google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
wp_register_script( 'google-jsapi-old', '//www.google.com/jsapi', array( 'google-jsapi-new' ), null, true );
wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
wp_register_script(
'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array(
'visualizer-render',
VISUALIZER_ABSURL . 'js/render.js',
array(
'google-jsapi-old',
'google-jsapi-new',
'visualizer-frame',
), Visualizer_Plugin::VERSION, true
'visualizer-customization',
),
Visualizer_Plugin::VERSION,
true
);
wp_register_script(
'visualizer-preview', VISUALIZER_ABSURL . 'js/preview.js', array(
'visualizer-preview',
VISUALIZER_ABSURL . 'js/preview.js',
array(
'wp-color-picker',
'visualizer-render',
), Visualizer_Plugin::VERSION, true
),
Visualizer_Plugin::VERSION,
true
);
// added by Ash/Upwork
if ( VISUALIZER_PRO ) {
Expand Down Expand Up @@ -344,7 +362,9 @@ private function _handleDataAndSettingsPage() {
wp_enqueue_script( 'visualizer-preview' );
wp_enqueue_script( 'visualizer-render' );
wp_localize_script(
'visualizer-render', 'visualizer', array(
'visualizer-render',
'visualizer',
array(
'l10n' => array(
'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
'loading' => esc_html__( 'Loading...', 'visualizer' ),
Expand Down Expand Up @@ -538,7 +558,10 @@ public function cloneChart() {
$capable = current_user_can( 'edit_posts' );
if ( $nonce && $capable ) {
$chart_id = filter_input(
INPUT_GET, 'chart', FILTER_VALIDATE_INT, array(
INPUT_GET,
'chart',
FILTER_VALIDATE_INT,
array(
'options' => array(
'min_range' => 1,
),
Expand Down Expand Up @@ -570,7 +593,8 @@ public function cloneChart() {
array(
'page' => 'visualizer',
'type' => filter_input( INPUT_GET, 'type' ),
), admin_url( 'upload.php' )
),
admin_url( 'upload.php' )
);
}
}
Expand All @@ -590,7 +614,9 @@ public function exportData() {
$capable = current_user_can( 'edit_posts' );
if ( $capable ) {
$chart_id = isset( $_GET['chart'] ) ? filter_var(
$_GET['chart'], FILTER_VALIDATE_INT, array(
$_GET['chart'],
FILTER_VALIDATE_INT,
array(
'options' => array(
'min_range' => 1,
),
Expand Down Expand Up @@ -623,7 +649,9 @@ private function _handleDataPage() {
wp_enqueue_style( 'visualizer-frame' );
wp_enqueue_script( 'visualizer-render' );
wp_localize_script(
'visualizer-render', 'visualizer', array(
'visualizer-render',
'visualizer',
array(
'l10n' => array(
'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
'loading' => esc_html__( 'Loading...', 'visualizer' ),
Expand Down
16 changes: 11 additions & 5 deletions classes/Visualizer/Module/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function __construct( Visualizer_Plugin $plugin ) {
parent::__construct( $plugin );

$this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' );
$this->_addAction( 'visualizer_enqueue_scripts', 'enqueueScripts' );
$this->_addFilter( 'visualizer_get_language', 'getLanguage' );
$this->_addShortcode( 'visualizer', 'renderChart' );

Expand Down Expand Up @@ -99,7 +100,8 @@ function endpoint_register() {
*/
private function get_actions() {
return apply_filters(
'visualizer_action_buttons', array(
'visualizer_action_buttons',
array(
'print' => __( 'Print', 'visualizer' ),
'csv' => __( 'CSV', 'visualizer' ),
'xls' => __( 'Excel', 'visualizer' ),
Expand Down Expand Up @@ -157,7 +159,8 @@ public function perform_action( WP_REST_Request $params ) {
public function enqueueScripts() {
wp_register_script( 'visualizer-google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
wp_register_script( 'visualizer-google-jsapi-old', '//www.google.com/jsapi', array( 'visualizer-google-jsapi-new' ), null, true );
wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'visualizer-google-jsapi-old', 'jquery' ), Visualizer_Plugin::VERSION, true );
wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'visualizer-google-jsapi-old', 'jquery', 'visualizer-customization' ), Visualizer_Plugin::VERSION, true );
wp_register_script( 'visualizer-clipboardjs', VISUALIZER_ABSURL . 'js/lib/clipboardjs/clipboard.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true );
wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION );
do_action( 'visualizer_pro_frontend_load_resources' );
Expand Down Expand Up @@ -185,7 +188,8 @@ public function renderChart( $atts ) {
'series' => false, // series filter hook
'data' => false, // data filter hook
'settings' => false, // data filter hook
), $atts
),
$atts
);

// if empty id or chart does not exists, then return empty string
Expand Down Expand Up @@ -228,7 +232,7 @@ public function renderChart( $atts ) {
}

// handle data filter hooks
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type );
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type );
if ( ! empty( $atts['data'] ) ) {
$data = apply_filters( $atts['data'], $data, $chart->ID, $type );
}
Expand All @@ -253,7 +257,9 @@ public function renderChart( $atts ) {
// enqueue visualizer render and update render localizations
wp_enqueue_script( 'visualizer-render' );
wp_localize_script(
'visualizer-render', 'visualizer', array(
'visualizer-render',
'visualizer',
array(
'charts' => $this->_charts,
'language' => $this->get_language(),
'map_api_key' => get_option( 'visualizer-map-api-key' ),
Expand Down
3 changes: 2 additions & 1 deletion classes/Visualizer/Module/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public function getChartCountsByTypeAndMeta( $meta_keys = array() ) {
*/
public function setupCustomPostTypes() {
register_post_type(
Visualizer_Plugin::CPT_VISUALIZER, array(
Visualizer_Plugin::CPT_VISUALIZER,
array(
'label' => 'Visualizer Charts',
'public' => false,
'supports' => array( 'revisions' ),
Expand Down
2 changes: 1 addition & 1 deletion classes/Visualizer/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class Visualizer_Plugin {

const NAME = 'visualizer';
const VERSION = '3.0.11';
const VERSION = '3.0.12';

// custom post types
const CPT_VISUALIZER = 'visualizer';
Expand Down
9 changes: 6 additions & 3 deletions classes/Visualizer/Render/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,25 @@ private function _renderChartBox( $placeholder_id, $chart_id ) {
'action' => Visualizer_Plugin::ACTION_DELETE_CHART,
'nonce' => wp_create_nonce(),
'chart' => $chart_id,
), $ajax_url
),
$ajax_url
);
$clone_url = add_query_arg(
array(
'action' => Visualizer_Plugin::ACTION_CLONE_CHART,
'nonce' => wp_create_nonce( Visualizer_Plugin::ACTION_CLONE_CHART ),
'chart' => $chart_id,
'type' => $this->type,
), $ajax_url
),
$ajax_url
);
$export_link = add_query_arg(
array(
'action' => Visualizer_Plugin::ACTION_EXPORT_DATA,
'chart' => $chart_id,
'security' => wp_create_nonce( Visualizer_Plugin::ACTION_EXPORT_DATA . Visualizer_Plugin::VERSION ),
), admin_url( 'admin-ajax.php' )
),
admin_url( 'admin-ajax.php' )
);
echo '<div class="visualizer-chart"><div class="visualizer-chart-title">', esc_html( $title ), '</div>';
echo '<div id="', $placeholder_id, '" class="visualizer-chart-canvas">';
Expand Down
Loading

0 comments on commit 5f00c83

Please sign in to comment.