| 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 |
|
| 70 |
|
| 71 |
$sql = "SELECT * FROM " . $wpdb->prefix . CHART_BUILDER_DB_PREFIX . "charts WHERE status = %s"; |
| 72 |
|
| 73 |
$params = ['published']; |
| 74 |
|
| 75 |
if (!current_user_can('manage_options')) { |
| 76 |
$sql .= " AND author_id = %d"; |
| 77 |
$params[] = $current_user; |
| 78 |
} |
| 79 |
// phpcs:ignore |
| 80 |
$results = $wpdb->get_results($wpdb->prepare($sql, ...$params), ARRAY_A); |
| 81 |
|
| 82 |
register_block_type( |
| 83 |
$block_namespace, |
| 84 |
array( |
| 85 |
'render_callback' => 'chart_builder_render_callback', |
| 86 |
'editor_script' => 'chart-builder-block-js', |
| 87 |
'style' => 'chart-builder-block-css', |
| 88 |
'category' => 'media', |
| 89 |
'keywords' => array( |
| 90 |
'Chart', |
| 91 |
'Visual Data', |
| 92 |
'Graph', |
| 93 |
'Chart Maker', |
| 94 |
'Data Charts', |
| 95 |
'Dynamic Charts' |
| 96 |
), |
| 97 |
'attributes' => array( |
| 98 |
'idner' => $results, |
| 99 |
'metaFieldValue' => array( |
| 100 |
'type' => 'integer', |
| 101 |
), |
| 102 |
'shortcode' => array( |
| 103 |
'type' => 'string', |
| 104 |
), |
| 105 |
'className' => array( |
| 106 |
'type' => 'string', |
| 107 |
) |
| 108 |
), |
| 109 |
) |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
function chart_builder_render_callback( $attributes ) { |
| 114 |
global $current_screen; |
| 115 |
$is_front = true; |
| 116 |
|
| 117 |
if( ! empty( $current_screen ) ){ |
| 118 |
if( isset( $current_screen->is_block_editor ) && $current_screen->is_block_editor === true ){ |
| 119 |
$is_front = false; |
| 120 |
} |
| 121 |
}elseif ( wp_is_json_request() ) { |
| 122 |
$is_front = false; |
| 123 |
} |
| 124 |
|
| 125 |
$ays_html = "<div></div>"; |
| 126 |
|
| 127 |
if( ! empty( $attributes["shortcode"] ) ) { |
| 128 |
$ays_html = $attributes["shortcode"]; |
| 129 |
}else{ |
| 130 |
if( $is_front === true ){ |
| 131 |
$ays_html = ''; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return $ays_html; |
| 136 |
} |
| 137 |
|
| 138 |
if(function_exists("register_block_type")){ |
| 139 |
// Hook scripts function into block editor hook |
| 140 |
add_action( 'enqueue_block_editor_assets', 'ays_chart_gutenberg_scripts' ); |
| 141 |
add_action( 'init', 'ays_chart_gutenberg_block_register' ); |
| 142 |
} |
| 143 |
|
| 144 |
function aysChartBuilderVersionCompare($version1, $operator, $version2) { |
| 145 |
|
| 146 |
$_fv = intval ( trim ( str_replace ( '.', '', $version1 ) ) ); |
| 147 |
$_sv = intval ( trim ( str_replace ( '.', '', $version2 ) ) ); |
| 148 |
|
| 149 |
if (strlen ( $_fv ) > strlen ( $_sv )) { |
| 150 |
$_sv = str_pad ( $_sv, strlen ( $_fv ), 0 ); |
| 151 |
} |
| 152 |
|
| 153 |
if (strlen ( $_fv ) < strlen ( $_sv )) { |
| 154 |
$_fv = str_pad ( $_fv, strlen ( $_sv ), 0 ); |
| 155 |
} |
| 156 |
|
| 157 |
return version_compare ( ( string ) $_fv, ( string ) $_sv, $operator ); |
| 158 |
} |
| 159 |
|