/** * Instantiates the plugin and setup all modules. * * @since 1.0.0 */ function visualizer_launch() { // setup environment define('VISUALIZER_BASEFILE', __FILE__); define('VISUALIZER_ABSURL', plugins_url('/', __FILE__)); define('VISUALIZER_ABSPATH', dirname(__FILE__)); if (!defined('VISUALIZER_CSV_DELIMITER')) { define('VISUALIZER_CSV_DELIMITER', ','); } if (!defined('VISUALIZER_CSV_ENCLOSURE')) { define('VISUALIZER_CSV_ENCLOSURE', '"'); } // don't load the plugin if cron job is running or doing autosave $doing_autosave = defined('DOING_AUTOSAVE') && DOING_AUTOSAVE; $doing_cron = defined('DOING_CRON') && DOING_CRON; $doing_ajax = defined('DOING_AJAX') && DOING_AJAX; if ($doing_autosave || $doing_cron) { return; } // instantiate the plugin $plugin = Visualizer_Plugin::instance(); // set general modules $plugin->setModule(Visualizer_Module_Setup::NAME); $plugin->setModule(Visualizer_Module_Sources::NAME); if ($doing_ajax) { // set ajax modules $plugin->setModule(Visualizer_Module_Chart::NAME); } else { if (is_admin()) { // set admin modules $plugin->setModule(Visualizer_Module_Admin::NAME); } else { // set frontend modules $plugin->setModule(Visualizer_Module_Frontend::NAME); } } }
/** * Handles chart type selection page. * * @since 1.0.0 * * @access private */ private function _handleTypesPage() { // process post request if ($_SERVER['REQUEST_METHOD'] == 'POST' && wp_verify_nonce(filter_input(INPUT_POST, 'nonce'))) { $type = filter_input(INPUT_POST, 'type'); if (in_array($type, Visualizer_Plugin::getChartTypes())) { // save new chart type update_post_meta($this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, $type); // if the chart has default data, update it with appropriate default data for new type if (filter_var(get_post_meta($this->_chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, true), FILTER_VALIDATE_BOOLEAN)) { $source = new Visualizer_Source_Csv(VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $type . '.csv'); $source->fetch(); $this->_chart->post_content = $source->getData(); wp_update_post($this->_chart->to_array()); update_post_meta($this->_chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries()); } // redirect to next tab wp_redirect(add_query_arg('tab', 'data')); return; } } $render = new Visualizer_Render_Page_Types(); $render->type = get_post_meta($this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true); $render->types = Visualizer_Plugin::getChartTypes(); $render->chart = $this->_chart; wp_enqueue_style('visualizer-frame'); wp_enqueue_script('visualizer-frame'); wp_iframe(array($render, 'render')); }
/** * Returns singletone instance of the plugin. * * @since 1.0.0 * * @static * @access public * @return Visualizer_Plugin */ public static function instance() { if (is_null(self::$_instance)) { self::$_instance = new Visualizer_Plugin(); } return self::$_instance; }
/** * Renders visualizer library page. * * @since 1.0.0 * * @access public */ public function renderLibraryPage() { // get current page $page = filter_input(INPUT_GET, 'vpage', FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'default' => 1))); // the initial query arguments to fetch charts $query_args = array('post_type' => Visualizer_Plugin::CPT_VISUALIZER, 'posts_per_page' => 6, 'paged' => $page); // add chart type filter to the query arguments $filter = filter_input(INPUT_GET, 'type'); if ($filter && in_array($filter, Visualizer_Plugin::getChartTypes())) { $query_args['meta_query'] = array(array('key' => Visualizer_Plugin::CF_CHART_TYPE, 'value' => $filter, 'compare' => '=')); } else { $filter = 'all'; } // fetch charts $charts = array(); $query = new WP_Query($query_args); while ($query->have_posts()) { $chart = $query->next_post(); // fetch and update settings $settings = get_post_meta($chart->ID, Visualizer_Plugin::CF_SETTINGS, true); unset($settings['height'], $settings['width']); $type = get_post_meta($chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true); $series = apply_filters(Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta($chart->ID, Visualizer_Plugin::CF_SERIES, true), $chart->ID, $type); $data = apply_filters(Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize($chart->post_content), $chart->ID, $type); // add chart to the array $charts['visualizer-' . $chart->ID] = array('id' => $chart->ID, 'type' => $type, 'series' => $series, 'settings' => $settings, 'data' => $data); } // enqueue charts array $ajaxurl = admin_url('admin-ajax.php'); wp_localize_script('visualizer-library', 'visualizer', array('charts' => $charts, 'urls' => array('base' => add_query_arg('vpage', false), 'create' => add_query_arg(array('action' => Visualizer_Plugin::ACTION_CREATE_CHART, 'library' => 'yes'), $ajaxurl), 'edit' => add_query_arg(array('action' => Visualizer_Plugin::ACTION_EDIT_CHART, 'library' => 'yes'), $ajaxurl)))); // render library page $render = new Visualizer_Render_Library(); $render->charts = $charts; $render->type = $filter; $render->types = self::_getChartTypesLocalized(); $render->pagination = paginate_links(array('base' => add_query_arg('vpage', '%#%'), 'format' => '', 'current' => $page, 'total' => $query->max_num_pages, 'type' => 'array')); $render->render(); }