| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Chart Block |
| 4 |
* Description: Represent your data by symbols, such as bars, lines, or pie charts. |
| 5 |
* Version: 1.0.4 |
| 6 |
* Author: bPlugins LLC |
| 7 |
* Author URI: http://bplugins.com |
| 8 |
* License: GPLv3 |
| 9 |
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt |
| 10 |
* Text Domain: chart-block |
| 11 |
*/ |
| 12 |
|
| 13 |
// ABS PATH |
| 14 |
if ( !defined( 'ABSPATH' ) ) { exit; } |
| 15 |
|
| 16 |
// Constant |
| 17 |
define( 'BCHART_PLUGIN_VERSION', 'localhost' === $_SERVER['HTTP_HOST'] ? time() : '1.0.4' ); |
| 18 |
define( 'BCHART_ASSETS_DIR', plugin_dir_url( __FILE__ ) . 'assets/' ); |
| 19 |
|
| 20 |
// Generate Styles |
| 21 |
class BChartStyleGenerator { |
| 22 |
public static $styles = []; |
| 23 |
public static function addStyle( $selector, $styles ){ |
| 24 |
if( array_key_exists( $selector, self::$styles ) ){ |
| 25 |
self::$styles[$selector] = wp_parse_args( self::$styles[$selector], $styles ); |
| 26 |
}else { self::$styles[$selector] = $styles; } |
| 27 |
} |
| 28 |
public static function renderStyle(){ |
| 29 |
$output = ''; |
| 30 |
foreach( self::$styles as $selector => $style ){ |
| 31 |
$new = ''; |
| 32 |
foreach( $style as $property => $value ){ |
| 33 |
if( $value == '' ){ $new .= $property; }else { $new .= " $property: $value;"; } |
| 34 |
} |
| 35 |
$output .= "$selector { $new }"; |
| 36 |
} |
| 37 |
return $output; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// Chart Block |
| 42 |
class BChartBlock{ |
| 43 |
protected static $_instance = null; |
| 44 |
|
| 45 |
function __construct(){ |
| 46 |
add_action( 'enqueue_block_assets', [$this, 'enqueue_block_assets'] ); |
| 47 |
add_action( 'init', [$this, 'register'] ); |
| 48 |
} |
| 49 |
|
| 50 |
public static function instance(){ |
| 51 |
if( self::$_instance === null ){ |
| 52 |
self::$_instance = new self(); |
| 53 |
} |
| 54 |
return self::$_instance; |
| 55 |
} |
| 56 |
|
| 57 |
function enqueue_block_assets(){ wp_enqueue_script( 'chartJS', BCHART_ASSETS_DIR . 'js/chart.min.js', [], '3.5.1', true ); } |
| 58 |
|
| 59 |
function register() { |
| 60 |
wp_register_script( 'b_chart_editor_script', plugins_url( 'dist/editor.js', __FILE__ ), [ 'wp-blob', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-html-entities', 'wp-i18n', 'wp-rich-text', 'chartJS' ], BCHART_PLUGIN_VERSION, false ); // Backend Script |
| 61 |
wp_register_style( 'b_chart_editor_style', plugins_url( 'dist/editor.css', __FILE__ ), [ 'wp-edit-blocks' ], BCHART_PLUGIN_VERSION ); // Backend Style |
| 62 |
wp_register_script( 'b_chart_script', plugins_url( 'dist/script.js', __FILE__ ), [ 'chartJS' ], BCHART_PLUGIN_VERSION, true ); // Frontend Script |
| 63 |
wp_register_style( 'b_chart_style', plugins_url( 'dist/style.css', __FILE__ ), [ 'wp-editor' ], BCHART_PLUGIN_VERSION ); // Frontend Style |
| 64 |
|
| 65 |
register_block_type( 'b-chart/chart', [ |
| 66 |
'editor_script' => 'b_chart_editor_script', |
| 67 |
'editor_style' => 'b_chart_editor_style', |
| 68 |
'script' => 'b_chart_script', |
| 69 |
'style' => 'b_chart_style', |
| 70 |
'render_callback' => [$this, 'render'] |
| 71 |
] ); // Register Block |
| 72 |
|
| 73 |
wp_set_script_translations( 'b_chart_editor_script', 'chart-block', plugin_dir_path( __FILE__ ) . 'languages' ); // Translate |
| 74 |
} |
| 75 |
|
| 76 |
function render( $attributes ){ |
| 77 |
extract( $attributes ); |
| 78 |
$align = $align ?? ''; |
| 79 |
$cId = $cId ?? ''; |
| 80 |
$width = $width ?? '80%'; |
| 81 |
$background = $background ?? [ 'color' => '#0000' ]; |
| 82 |
$alignment = $alignment ?? 'center'; |
| 83 |
$isDownload = $isDownload ?? false; |
| 84 |
|
| 85 |
$chartStyle = new BChartStyleGenerator(); // Generate Styles |
| 86 |
$chartStyle::addStyle( "#bChart-$cId", [ |
| 87 |
'text-align' => $alignment |
| 88 |
] ); |
| 89 |
$chartStyle::addStyle( "#bChart-$cId .bChart, #bChart-$cId .downloadArea", [ |
| 90 |
'width' => $width |
| 91 |
] ); |
| 92 |
$chartStyle::addStyle( "#bChart-$cId .bChart canvas", [ |
| 93 |
$background['styles'] ?? 'background-color: #0000;' => '' |
| 94 |
] ); |
| 95 |
|
| 96 |
$jsonData = wp_json_encode( [ 'data' => $data ?? '', 'type' => $type ?? 'line', 'textColor' => $textColor ?? '#666', 'gridLineColor' => $gridLineColor ?? '#0000001a', 'isTitle' => $isTitle ?? true, 'title' => $title ?? 'Chart Title', 'titleFontSize' => $titleFontSize ?? 22, 'titleColor' => $titleColor ?? '#4527a4', 'isSubtitle' => $isSubtitle ?? false, 'subtitle' => $subtitle ?? 'Chart Subtitle', 'subtitleFontSize' => $subtitleFontSize ?? 13, 'subtitleColor' => $subtitleColor ?? '#8344c5', 'isDownload' => $isDownload ] ); |
| 97 |
|
| 98 |
ob_start(); ?> |
| 99 |
<div class='wp-block-b-chart-chart <?php echo 'align' . esc_attr( $align ); ?>' id='bChart-<?php echo esc_attr( $cId ) ?>' data-chart='<?php echo esc_attr( $jsonData ); ?>'> |
| 100 |
<style><?php echo wp_kses( $chartStyle::renderStyle(), [] ) ?></style> |
| 101 |
|
| 102 |
<?php echo $isDownload ? "<div class='downloadArea'> |
| 103 |
<div class='downloadChart'>Download Chart</div> |
| 104 |
</div>" : ''; ?> |
| 105 |
|
| 106 |
<div class='bChart'> |
| 107 |
<canvas>Your browser does not support the canvas element.</canvas> |
| 108 |
</div> |
| 109 |
</div> |
| 110 |
|
| 111 |
<?php $chartStyle::$styles = []; // Empty styles |
| 112 |
return ob_get_clean(); |
| 113 |
} // Render |
| 114 |
} |
| 115 |
BChartBlock::instance(); |