| 1 |
<?php |
| 2 |
|
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | This program is free software; you can redistribute it and/or modify | |
| 7 |
// | it under the terms of the GNU General Public License, version 2, as | |
| 8 |
// | published by the Free Software Foundation. | |
| 9 |
// | | |
| 10 |
// | This program is distributed in the hope that it will be useful, | |
| 11 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 |
// | GNU General Public License for more details. | |
| 14 |
// | | |
| 15 |
// | You should have received a copy of the GNU General Public License | |
| 16 |
// | along with this program; if not, write to the Free Software | |
| 17 |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 18 |
// | MA 02110-1301 USA | |
| 19 |
// +----------------------------------------------------------------------+ |
| 20 |
// | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 21 |
// +----------------------------------------------------------------------+ |
| 22 |
|
| 23 |
/** |
| 24 |
* Frontend module class. |
| 25 |
* |
| 26 |
* @category Visualizer |
| 27 |
* @package Module |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
*/ |
| 31 |
class Visualizer_Module_Frontend extends Visualizer_Module { |
| 32 |
|
| 33 |
const NAME = __CLASS__; |
| 34 |
|
| 35 |
/** |
| 36 |
* The array of charts to render. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
* |
| 40 |
* @access private |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
private $_charts = array(); |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructor. |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
* @uses add_filter() To add "do_shortcode" hook for "widget_text" and "term_description" filters. |
| 50 |
* |
| 51 |
* @access public |
| 52 |
* @param Visualizer_Plugin $plugin The instance of the plugin. |
| 53 |
*/ |
| 54 |
public function __construct( Visualizer_Plugin $plugin ) { |
| 55 |
parent::__construct( $plugin ); |
| 56 |
|
| 57 |
$this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' ); |
| 58 |
$this->_addShortcode( 'visualizer', 'renderChart' ); |
| 59 |
|
| 60 |
add_filter( 'widget_text', 'do_shortcode' ); |
| 61 |
add_filter( 'term_description', 'do_shortcode' ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Registers scripts for charts rendering. |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
* @uses wp_register_script To register javascript file. |
| 69 |
* |
| 70 |
* @access public |
| 71 |
*/ |
| 72 |
public function enqueueScripts() { |
| 73 |
wp_register_script( 'google-jsapi', '//www.google.com/jsapi', array(), null, true ); |
| 74 |
wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'google-jsapi', 'jquery' ), Visualizer_Plugin::VERSION, true ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns placeholder for chart according to visualizer shortcode attributes. |
| 79 |
* |
| 80 |
* @since 1.0.0 |
| 81 |
* @uses shortcode_atts() To parse income shortocdes. |
| 82 |
* |
| 83 |
* @access public |
| 84 |
* @param array $atts The array of shortcode attributes. |
| 85 |
*/ |
| 86 |
public function renderChart( $atts ) { |
| 87 |
$atts = shortcode_atts( array( 'id' => false ), $atts ); |
| 88 |
|
| 89 |
// if empty id or chart does not exists, then return empty string |
| 90 |
if ( !$atts['id'] || !( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { |
| 91 |
return ''; |
| 92 |
} |
| 93 |
|
| 94 |
$id = 'visualizer-' . $atts['id']; |
| 95 |
|
| 96 |
// faetch and update settings |
| 97 |
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); |
| 98 |
if ( empty( $settings['height'] ) ) { |
| 99 |
$settings['height'] = '400'; |
| 100 |
} |
| 101 |
|
| 102 |
// add chart to the array |
| 103 |
$this->_charts[$id] = array( |
| 104 |
'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ), |
| 105 |
'series' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), |
| 106 |
'settings' => $settings, |
| 107 |
'data' => unserialize( $chart->post_content ), |
| 108 |
); |
| 109 |
|
| 110 |
// enqueue visualizer render and update render localizations |
| 111 |
wp_enqueue_script( 'visualizer-render' ); |
| 112 |
wp_localize_script( 'visualizer-render', 'visualizer', array( 'charts' => $this->_charts ) ); |
| 113 |
|
| 114 |
// return placeholder div |
| 115 |
return '<div id="' . $id . '"></div>'; |
| 116 |
} |
| 117 |
|
| 118 |
} |