PluginProbe
Chartify – WordPress Chart Plugin / 3.1.0
Chartify – WordPress Chart Plugin v3.1.0
3.8.0 3.7.9 3.7.8 3.7.7 3.7.6 3.7.5 trunk 1.0.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 All 83 releases
chart-builder / chart / chart-builder-block.php

chart-builder-block.php in Chartify – WordPress Chart Plugin 3.1.0, at chart/chart-builder-block.php

152 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Enqueue front end and editor JavaScript
4 */
5
6 function ays_chart_gutenberg_scripts() {
7 global $current_screen;
8 global $wp_version;
9 $version1 = $wp_version;
10 $operator = '>=';
11 $version2 = '5.3.12';
12 $versionCompare = aysChartBuilderVersionCompare($version1, $operator, $version2);
13 if( ! $current_screen ){
14 return null;
15 }
16
17 if( ! $current_screen->is_block_editor ){
18 return null;
19 }
20
21 // wp_enqueue_script( CHART_BUILDER_NAME . "-plugin", CHART_BUILDER_PUBLIC_URL . '/js/chart-builder-public-plugin.js', array('jquery'), CHART_BUILDER_VERSION, true);
22
23 // Enqueue the bundled block JS file
24 if($versionCompare){
25 wp_enqueue_script(
26 'chart-builder-block-js',
27 CHART_BUILDER_BASE_URL ."/chart/chart-builder-block-new.js",
28 array( 'jquery', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor' ),
29 CHART_BUILDER_VERSION, true
30 );
31 }
32 else{
33 wp_enqueue_script(
34 'chart-builder-block-js',
35 CHART_BUILDER_BASE_URL ."/chart/chart-builder-block.js",
36 array( 'jquery', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor' ),
37 CHART_BUILDER_VERSION, true
38 );
39 }
40
41 wp_enqueue_style( CHART_BUILDER_NAME, CHART_BUILDER_PUBLIC_URL . '/css/chart-builder-public.css', array(), CHART_BUILDER_VERSION, 'all');
42
43 // Enqueue the bundled block CSS file
44 if($versionCompare){
45 wp_enqueue_style(
46 'chart-builder-block-css',
47 CHART_BUILDER_BASE_URL ."/chart/chart-builder-block-new.css",
48 array(),
49 CHART_BUILDER_VERSION, 'all'
50 );
51 }
52 else{
53 wp_enqueue_style(
54 'chart-builder-block-css',
55 CHART_BUILDER_BASE_URL ."/chart/chart-builder-block.css",
56 array(),
57 CHART_BUILDER_VERSION, 'all'
58 );
59 }
60 }
61
62 function ays_chart_gutenberg_block_register() {
63
64 global $wpdb;
65 $block_name = 'chart';
66 $block_namespace = 'chart-builder/' . $block_name;
67
68 $current_user = get_current_user_id();
69 $sql = "SELECT * FROM ". $wpdb->prefix . CHART_BUILDER_DB_PREFIX . "charts WHERE status = 'published'";
70 if( ! current_user_can( 'manage_options' ) ){
71 $sql .= " AND author_id = ". absint( $current_user ) ." ";
72 }
73 $results = $wpdb->get_results($sql, "ARRAY_A");
74
75 register_block_type(
76 $block_namespace,
77 array(
78 'render_callback' => 'chart_builder_render_callback',
79 'editor_script' => 'chart-builder-block-js',
80 'style' => 'chart-builder-block-css',
81 'category' => 'media',
82 'keywords' => array(
83 'Chart',
84 'Visual Data',
85 'Graph',
86 'Chart Maker',
87 'Data Charts',
88 'Dynamic Charts'
89 ),
90 'attributes' => array(
91 'idner' => $results,
92 'metaFieldValue' => array(
93 'type' => 'integer',
94 ),
95 'shortcode' => array(
96 'type' => 'string',
97 ),
98 'className' => array(
99 'type' => 'string',
100 )
101 ),
102 )
103 );
104 }
105
106 function chart_builder_render_callback( $attributes ) {
107 global $current_screen;
108 $is_front = true;
109
110 if( ! empty( $current_screen ) ){
111 if( isset( $current_screen->is_block_editor ) && $current_screen->is_block_editor === true ){
112 $is_front = false;
113 }
114 }elseif ( wp_is_json_request() ) {
115 $is_front = false;
116 }
117
118 $ays_html = "<div></div>";
119
120 if( ! empty( $attributes["shortcode"] ) ) {
121 $ays_html = $attributes["shortcode"];
122 }else{
123 if( $is_front === true ){
124 $ays_html = '';
125 }
126 }
127
128 return $ays_html;
129 }
130
131 if(function_exists("register_block_type")){
132 // Hook scripts function into block editor hook
133 add_action( 'enqueue_block_editor_assets', 'ays_chart_gutenberg_scripts' );
134 add_action( 'init', 'ays_chart_gutenberg_block_register' );
135 }
136
137 function aysChartBuilderVersionCompare($version1, $operator, $version2) {
138
139 $_fv = intval ( trim ( str_replace ( '.', '', $version1 ) ) );
140 $_sv = intval ( trim ( str_replace ( '.', '', $version2 ) ) );
141
142 if (strlen ( $_fv ) > strlen ( $_sv )) {
143 $_sv = str_pad ( $_sv, strlen ( $_fv ), 0 );
144 }
145
146 if (strlen ( $_fv ) < strlen ( $_sv )) {
147 $_fv = str_pad ( $_fv, strlen ( $_sv ), 0 );
148 }
149
150 return version_compare ( ( string ) $_fv, ( string ) $_sv, $operator );
151 }
152