| 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 |
* Frontend module class. |
| 24 |
* |
| 25 |
* @category Visualizer |
| 26 |
* @package Module |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
class Visualizer_Module_Frontend extends Visualizer_Module { |
| 31 |
|
| 32 |
const NAME = __CLASS__; |
| 33 |
|
| 34 |
/** |
| 35 |
* The array of charts to render. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* |
| 39 |
* @access private |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
private $_charts = array(); |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor. |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
* @uses add_filter() To add "do_shortcode" hook for "widget_text" and "term_description" filters. |
| 49 |
* |
| 50 |
* @access public |
| 51 |
* @param Visualizer_Plugin $plugin The instance of the plugin. |
| 52 |
*/ |
| 53 |
public function __construct( Visualizer_Plugin $plugin ) { |
| 54 |
parent::__construct( $plugin ); |
| 55 |
|
| 56 |
$this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' ); |
| 57 |
$this->_addShortcode( 'visualizer', 'renderChart' ); |
| 58 |
|
| 59 |
// add do_shortocde hook for widget_text filter |
| 60 |
if ( ! has_filter( 'widget_text', 'do_shortcode' ) ) { |
| 61 |
add_filter( 'widget_text', 'do_shortcode' ); |
| 62 |
} |
| 63 |
|
| 64 |
// add do_shortcode hook for term_description filter |
| 65 |
if ( ! has_filter( 'term_description', 'do_shortcode' ) ) { |
| 66 |
add_filter( 'term_description', 'do_shortcode' ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Registers scripts for charts rendering. |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
* @uses wp_register_script To register javascript file. |
| 75 |
* |
| 76 |
* @access public |
| 77 |
*/ |
| 78 |
public function enqueueScripts() { |
| 79 |
wp_register_script( 'visualizer-google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true ); |
| 80 |
wp_register_script( 'visualizer-google-jsapi-old', '//www.google.com/jsapi', array( 'visualizer-google-jsapi-new' ), null, true ); |
| 81 |
wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'visualizer-google-jsapi-old', 'jquery' ), Visualizer_Plugin::VERSION, true ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns placeholder for chart according to visualizer shortcode attributes. |
| 86 |
* |
| 87 |
* @since 1.0.0 |
| 88 |
* @uses shortcode_atts() To parse income shortocdes. |
| 89 |
* @uses apply_filters() To filter chart's data and series arrays. |
| 90 |
* @uses get_post_meta() To fetch chart's meta information. |
| 91 |
* @uses wp_enqueue_script() To enqueue charts render script. |
| 92 |
* @uses wp_localize_script() To add chart data to the page inline script. |
| 93 |
* |
| 94 |
* @access public |
| 95 |
* @param array $atts The array of shortcode attributes. |
| 96 |
*/ |
| 97 |
public function renderChart( $atts ) { |
| 98 |
$atts = shortcode_atts( array( |
| 99 |
'id' => false, // chart id |
| 100 |
'class' => false, // chart class |
| 101 |
'series' => false, // series filter hook |
| 102 |
'data' => false, // data filter hook |
| 103 |
'settings' => false, // data filter hook |
| 104 |
), $atts ); |
| 105 |
|
| 106 |
// if empty id or chart does not exists, then return empty string |
| 107 |
if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { |
| 108 |
return ''; |
| 109 |
} |
| 110 |
|
| 111 |
$id = 'visualizer-' . $atts['id']; |
| 112 |
$defaultClass = 'visualizer-front'; |
| 113 |
$class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] ); |
| 114 |
$class = $defaultClass . ' ' . $class . ' ' . 'visualizer-front-' . $atts['id']; |
| 115 |
$class = ! empty( $class ) ? ' class="' . trim( $class ) . '"' : ''; |
| 116 |
|
| 117 |
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 118 |
|
| 119 |
// faetch and update settings |
| 120 |
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); |
| 121 |
if ( empty( $settings['height'] ) ) { |
| 122 |
$settings['height'] = '400'; |
| 123 |
} |
| 124 |
|
| 125 |
// handle series filter hooks |
| 126 |
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type ); |
| 127 |
if ( ! empty( $atts['series'] ) ) { |
| 128 |
$series = apply_filters( $atts['series'], $series, $chart->ID, $type ); |
| 129 |
} |
| 130 |
// handle settings filter hooks |
| 131 |
$settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type ); |
| 132 |
if ( ! empty( $atts['settings'] ) ) { |
| 133 |
$settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type ); |
| 134 |
} |
| 135 |
|
| 136 |
// handle data filter hooks |
| 137 |
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type ); |
| 138 |
if ( ! empty( $atts['data'] ) ) { |
| 139 |
$data = apply_filters( $atts['data'], $data, $chart->ID, $type ); |
| 140 |
} |
| 141 |
|
| 142 |
$id = $id . '-' . rand(); |
| 143 |
|
| 144 |
// add chart to the array |
| 145 |
$this->_charts[ $id ] = array( |
| 146 |
'type' => $type, |
| 147 |
'series' => $series, |
| 148 |
'settings' => $settings, |
| 149 |
'data' => $data, |
| 150 |
); |
| 151 |
|
| 152 |
// enqueue visualizer render and update render localizations |
| 153 |
wp_enqueue_script( 'visualizer-render' ); |
| 154 |
wp_localize_script( 'visualizer-render', 'visualizer', array( |
| 155 |
'charts' => $this->_charts, |
| 156 |
) ); |
| 157 |
|
| 158 |
// return placeholder div |
| 159 |
return '<div id="' . $id . '"' . $class . '></div>'; |
| 160 |
} |
| 161 |
|
| 162 |
} |
| 163 |
|