| @@ -1,150 +1,156 @@ | ||
| 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 do_shortocde hook for widget_text filter | |
| 61 | - if ( !has_filter( 'widget_text', 'do_shortcode' ) ) { | |
| 62 | - add_filter( 'widget_text', 'do_shortcode' ); | |
| 63 | - } | |
| 64 | - | |
| 65 | - // add do_shortcode hook for term_description filter | |
| 66 | - if ( !has_filter( 'term_description', 'do_shortcode' ) ) { | |
| 67 | - add_filter( 'term_description', 'do_shortcode' ); | |
| 68 | - } | |
| 69 | - } | |
| 70 | - | |
| 71 | - /** | |
| 72 | - * Registers scripts for charts rendering. | |
| 73 | - * | |
| 74 | - * @since 1.0.0 | |
| 75 | - * @uses wp_register_script To register javascript file. | |
| 76 | - * | |
| 77 | - * @access public | |
| 78 | - */ | |
| 79 | - public function enqueueScripts() { | |
| 80 | - wp_register_script( 'visualizer-google-jsapi', '//www.google.com/jsapi', array(), null, true ); | |
| 81 | - wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'visualizer-google-jsapi', '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 | - ), $atts ); | |
| 104 | - | |
| 105 | - // if empty id or chart does not exists, then return empty string | |
| 106 | - if ( !$atts['id'] || !( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 107 | - return ''; | |
| 108 | - } | |
| 109 | - | |
| 110 | - $id = 'visualizer-' . $atts['id']; | |
| 111 | - $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] ); | |
| 112 | - $class = !empty( $class ) ? ' class="' . $class . '"' : ''; | |
| 113 | - | |
| 114 | - $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); | |
| 115 | - | |
| 116 | - // faetch and update settings | |
| 117 | - $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 118 | - if ( empty( $settings['height'] ) ) { | |
| 119 | - $settings['height'] = '400'; | |
| 120 | - } | |
| 121 | - | |
| 122 | - // handle series filter hooks | |
| 123 | - $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type ); | |
| 124 | - if ( !empty( $atts['series'] ) ) { | |
| 125 | - $series = apply_filters( $atts['series'], $series, $chart->ID, $type ); | |
| 126 | - } | |
| 127 | - | |
| 128 | - // handle data filter hooks | |
| 129 | - $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type ); | |
| 130 | - if ( !empty( $atts['data'] ) ) { | |
| 131 | - $data = apply_filters( $atts['data'], $data, $chart->ID, $type ); | |
| 132 | - } | |
| 133 | - | |
| 134 | - // add chart to the array | |
| 135 | - $this->_charts[$id] = array( | |
| 136 | - 'type' => $type, | |
| 137 | - 'series' => $series, | |
| 138 | - 'settings' => $settings, | |
| 139 | - 'data' => $data, | |
| 140 | - ); | |
| 141 | - | |
| 142 | - // enqueue visualizer render and update render localizations | |
| 143 | - wp_enqueue_script( 'visualizer-render' ); | |
| 144 | - wp_localize_script( 'visualizer-render', 'visualizer', array( 'charts' => $this->_charts ) ); | |
| 145 | - | |
| 146 | - // return placeholder div | |
| 147 | - return '<div id="' . $id . '"' . $class . '></div>'; | |
| 148 | - } | |
| 149 | - | |
| 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 do_shortocde hook for widget_text filter | |
| 61 | + if ( !has_filter( 'widget_text', 'do_shortcode' ) ) { | |
| 62 | + add_filter( 'widget_text', 'do_shortcode' ); | |
| 63 | + } | |
| 64 | + | |
| 65 | + // add do_shortcode hook for term_description filter | |
| 66 | + if ( !has_filter( 'term_description', 'do_shortcode' ) ) { | |
| 67 | + add_filter( 'term_description', 'do_shortcode' ); | |
| 68 | + } | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Registers scripts for charts rendering. | |
| 73 | + * | |
| 74 | + * @since 1.0.0 | |
| 75 | + * @uses wp_register_script To register javascript file. | |
| 76 | + * | |
| 77 | + * @access public | |
| 78 | + */ | |
| 79 | + public function enqueueScripts() { | |
| 80 | + wp_register_script( 'visualizer-google-jsapi', '//www.google.com/jsapi', array(), null, true ); | |
| 81 | + wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'visualizer-google-jsapi', '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 | + $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] ); | |
| 113 | + $class = !empty( $class ) ? ' class="' . $class . '"' : ''; | |
| 114 | + | |
| 115 | + $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); | |
| 116 | + | |
| 117 | + // faetch and update settings | |
| 118 | + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 119 | + if ( empty( $settings['height'] ) ) { | |
| 120 | + $settings['height'] = '400'; | |
| 121 | + } | |
| 122 | + | |
| 123 | + // handle series filter hooks | |
| 124 | + $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type ); | |
| 125 | + if ( !empty( $atts['series'] ) ) { | |
| 126 | + $series = apply_filters( $atts['series'], $series, $chart->ID, $type ); | |
| 127 | + } | |
| 128 | + // handle settings filter hooks | |
| 129 | + $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type ); | |
| 130 | + if ( !empty( $atts['settings'] ) ) { | |
| 131 | + $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type ); | |
| 132 | + } | |
| 133 | + | |
| 134 | + // handle data filter hooks | |
| 135 | + $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type ); | |
| 136 | + if ( !empty( $atts['data'] ) ) { | |
| 137 | + $data = apply_filters( $atts['data'], $data, $chart->ID, $type ); | |
| 138 | + } | |
| 139 | + | |
| 140 | + // add chart to the array | |
| 141 | + $this->_charts[$id] = array( | |
| 142 | + 'type' => $type, | |
| 143 | + 'series' => $series, | |
| 144 | + 'settings' => $settings, | |
| 145 | + 'data' => $data, | |
| 146 | + ); | |
| 147 | + | |
| 148 | + // enqueue visualizer render and update render localizations | |
| 149 | + wp_enqueue_script( 'visualizer-render' ); | |
| 150 | + wp_localize_script( 'visualizer-render', 'visualizer', array( 'charts' => $this->_charts ) ); | |
| 151 | + | |
| 152 | + // return placeholder div | |
| 153 | + return '<div id="' . $id . '"' . $class . '></div>'; | |
| 154 | + } | |
| 155 | + | |
| 150 | 156 | } |