function amcharts_shortcode($atts) { extract(shortcode_atts(array('id' => ''), $atts)); // try loading by slug first if ($chart = get_posts(array('post_type' => 'amchart', 'fields' => 'ids', 'posts_per_page' => 1, 'meta_query' => array(array('key' => '_amcharts_slug', 'value' => $id))))) { $id = $chart[0]; } else { if (!($chart = get_post($id))) { return ''; } } // increment instance amcharts_increment_instance(); // get meta $resources = get_post_meta($id, '_amcharts_resources', true); $html = amcharts_parse_code(get_post_meta($id, '_amcharts_html', true)); $javascript = amcharts_parse_code(get_post_meta($id, '_amcharts_javascript', true)); // add data passed via shortcode $pass = array(); foreach ($atts as $att) { if (0 === strpos($att, 'data-')) { list($key, $val) = explode('=', $att); $pass[substr($key, 5)] = str_replace('"', '', $val); } } if (sizeof($pass)) { $javascript = "AmCharts.wpChartData = " . json_encode($pass) . ";\n" . $javascript; } // wrap with exception code if necessary $settings = get_option('amcharts_options'); if (isset($settings['wrap']) && '1' == $settings['wrap']) { $javascript = "try {\n" . $javascript . "\n}\ncatch( err ) { console.log( err ); }"; } // enqueue resources $libs = amcharts_split_libs($resources); foreach ($libs as $lib) { if (preg_match('/\\.css/i', $lib)) { wp_enqueue_style('amcharts-external-' . md5(basename($lib)), $lib, array(), AMCHARTS_VERSION); } else { wp_enqueue_script('amcharts-external-' . md5(basename($lib)), $lib, array(), AMCHARTS_VERSION, true); } } // enqueue JavaScript part amcharts_enqueue_javascript($javascript); return $html; }
<?php /** * The template for reviewing amCharts chart */ // enqueue resources $libs = amcharts_split_libs(amcharts_stripslashes($_POST['amcharts_resources'])); foreach ($libs as $lib) { if (preg_match('/\\.css/i', $lib)) { wp_enqueue_style('amcharts-external-' . md5(basename($lib)), $lib, array(), AMCHARTS_VERSION); } else { wp_enqueue_script('amcharts-external-' . md5(basename($lib)), $lib, array(), AMCHARTS_VERSION, true); } } // enqueue JavaScript part amcharts_increment_instance(); $javascript = amcharts_parse_code(amcharts_stripslashes($_POST['amcharts_javascript'])); $settings = get_option('amcharts_options'); if (isset($settings['wrap']) && '1' == $settings['wrap']) { $javascript = "try {\n" . $javascript . "\n}\ncatch( err ) { console.log( err ); }"; } amcharts_enqueue_javascript($javascript); ?> <!DOCTYPE html> <html <?php language_attributes(); ?> > <head> <meta charset="<?php