*/ class Chart_Builder_Public { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * @var string */ private $html_class_prefix = 'ays-chart-'; /** * @var string */ private $html_name_prefix = 'ays_chart_'; /** * @var string */ private $name_prefix = 'chart_'; /** * @var */ private $unique_id; /** * @var Chart_Builder_DB_Actions */ private $db_object; /** * @var array */ private $data; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; $this->db_object = new Chart_Builder_DB_Actions( $this->plugin_name ); add_shortcode( 'ays_chart', array( $this, 'ays_generate_chart_method' ) ); } /** * Register the stylesheets for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Chart_Builder_Loader as all of the hooks are defined * in that particular class. * * The Chart_Builder_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/chart-builder-public.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_scripts($type) { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Chart_Builder_Loader as all of the hooks are defined * in that particular class. * * The Chart_Builder_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ $is_elementor_exists = Chart_Builder_DB_Actions::ays_chart_is_elementor(); if( !$is_elementor_exists ) { if ($type === 'chart-js') { wp_enqueue_script( $this->plugin_name . '-chart-js', plugin_dir_url(__FILE__) . 'js/chart-js.js', array('jquery'), $this->version, true); wp_enqueue_script( $this->plugin_name . '-plugin-chartjs', plugin_dir_url( __FILE__ ) . 'js/chart-builder-public-chartjs.js', array( 'jquery' ), $this->version, false ); } else { wp_enqueue_script( $this->plugin_name . '-charts-google', plugin_dir_url(__FILE__) . 'js/google-chart.js', array('jquery'), $this->version, true); wp_enqueue_script( $this->plugin_name . '-plugin-google', plugin_dir_url( __FILE__ ) . 'js/chart-builder-public-google.js', array( 'jquery' ), $this->version, false ); } wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/chart-builder-public.js', array( 'jquery' ), $this->version, false ); } $this->get_encoded_options($type); } public function ays_generate_chart_method( $attr ) { $id = (isset($attr['id'])) ? absint(intval($attr['id'])) : null; if (is_null($id)) { return "
" . __( 'Wrong shortcode initialized', "chart-builder" ) . "
"; } $is_elementor = Chart_Builder_DB_Actions::ays_chart_is_elementor(); if ($is_elementor) { return "" . __( 'Note: The chart will be visible on the front end of your website.', "chart-builder" ) . "
"; } $chartData = CBActions()->get_chart_data( $id ); if ( is_null( $chartData ) ) { return "" . __('Wrong shortcode initialized', "chart-builder") . "
"; } $this->enqueue_styles(); $content = $this->show_chart( $id, $chartData, $attr ); $this->enqueue_scripts($chartData['chart']['type']); return str_replace( array( "\r\n", "\n", "\r" ), '', $content ); } public function show_chart( $id, $chartData, $attr ) { $chart = $chartData['chart']; $settings = $chartData['settings']; $unique_id = uniqid(); $this->unique_id = $unique_id; $data = array(); if ( is_null( $chart ) ) { return "" . __('Wrong shortcode initialized', "chart-builder") . "
"; } $source_type = $chartData['source_type']; $chart_source_type = $chartData['chart']['type']; $user_id = get_current_user_id(); if ($source_type == 'quiz_maker' && $user_id == 0 && $chart['quiz_query'] != 'q1') { return "" . __('You are not logged in. Please log in to view this chart.', $this->plugin_name) . "
"; } $status = isset( $chart['status'] ) && $chart['status'] != '' ? $chart['status'] : ''; if ( $status != 'published' ) { return ""; } if ($chart_source_type === "chart-js") { $settings = CBFunctions()->get_chart_settings_chartjs_public($settings); } else { $settings = CBFunctions()->get_chart_settings_google_public($settings, $chartData['source']); } $data['chart_type'] = $chartData['source_chart_type']; $data['source'] = $chartData['source']; $data['options'] = $settings; if ($chart_source_type === "chart-js") { $content = $this->chartJsContent($chart, $settings, $chartData, $id); } else { $content = $this->googleChartsContent($chart, $settings, $chartData, $id); } $this->data = $data; return implode( '', $content ); } public function get_encoded_options ($chart_source_type) { $data = $this->data; $encoded_data = base64_encode(json_encode($data)); $handle = $chart_source_type === 'chart-js' ? $this->plugin_name . '-chart-js' : $this->plugin_name . '-charts-google'; wp_localize_script($handle, 'aysChartOptions'.$this->unique_id, array('aysChartOptions' => $encoded_data)); } public function googleChartsContent ($chart, $settings, $chartData, $id) { $chart_title = (isset($chart['title']) && $chart['title'] != '') ? stripslashes ( sanitize_text_field( $chart['title'] ) ) : ''; $chart_description = (isset($chart['description']) && $chart['description'] != '') ? stripslashes ( sanitize_text_field( $chart['description'] ) ) : ''; $content = array(); $content[] = "