class-easy-charts-public.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * The public-facing functionality of the plugin.
  4. *
  5. * @link https://kiranpotphode.com/
  6. * @since 1.0.0
  7. *
  8. * @package Easy_Charts
  9. * @subpackage Easy_Charts/public
  10. */
  11. /**
  12. * The public-facing functionality of the plugin.
  13. *
  14. * Defines the plugin name, version, and two examples hooks for how to
  15. * enqueue the admin-specific stylesheet and JavaScript.
  16. *
  17. * @package Easy_Charts
  18. * @subpackage Easy_Charts/public
  19. * @author Kiran Potphode <kiranpotphode15@gmail.com>
  20. */
  21. class Easy_Charts_Public {
  22. /**
  23. * The ID of this plugin.
  24. *
  25. * @since 1.0.0
  26. * @access private
  27. * @var string $plugin_name The ID of this plugin.
  28. */
  29. private $plugin_name;
  30. /**
  31. * The version of this plugin.
  32. *
  33. * @since 1.0.0
  34. * @access private
  35. * @var string $version The current version of this plugin.
  36. */
  37. private $version;
  38. /**
  39. * Initialize the class and set its properties.
  40. *
  41. * @since 1.0.0
  42. *
  43. * @param string $plugin_name The name of the plugin.
  44. * @param string $version The version of this plugin.
  45. */
  46. public function __construct( $plugin_name, $version ) {
  47. $this->plugin_name = $plugin_name;
  48. $this->version = $version;
  49. }
  50. /**
  51. * Register the stylesheets for the public-facing side of the site.
  52. *
  53. * @since 1.0.0
  54. */
  55. public function enqueue_styles() {
  56. /**
  57. * This function is provided for demonstration purposes only.
  58. *
  59. * An instance of this class should be passed to the run() function
  60. * defined in Easy_Charts_Loader as all of the hooks are defined
  61. * in that particular class.
  62. *
  63. * The Easy_Charts_Loader will then create the relationship
  64. * between the defined hooks and the functions defined in this
  65. * class.
  66. */
  67. wp_register_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/easy-charts-public.css', array(), $this->version, 'all' );
  68. }
  69. /**
  70. * Register the JavaScript for the public-facing side of the site.
  71. *
  72. * @since 1.0.0
  73. */
  74. public function enqueue_scripts() {
  75. /**
  76. * This function is provided for demonstration purposes only.
  77. *
  78. * An instance of this class should be passed to the run() function
  79. * defined in Easy_Charts_Loader as all of the hooks are defined
  80. * in that particular class.
  81. *
  82. * The Easy_Charts_Loader will then create the relationship
  83. * between the defined hooks and the functions defined in this
  84. * class.
  85. */
  86. wp_register_script( 'easy-charts-public-js', plugin_dir_url( __FILE__ ) . 'js/easy-charts-public.js', array( 'jquery' ), $this->version, true );
  87. wp_register_script( 'd3-js', plugins_url( 'includes/js/d3.min.js', dirname( __FILE__ ) ), array( 'jquery' ), $this->version, false );
  88. wp_register_script( 'uvhcharts-js', plugins_url( 'includes/js/uvcharts.min.js', dirname( __FILE__ ) ), array( 'jquery' ), $this->version, false );
  89. wp_register_script( 'filesaver-js', plugins_url( 'includes/js/filesaver.js', dirname( __FILE__ ) ), array( 'jquery' ), $this->version, false );
  90. wp_register_script( 'canvg-js', plugins_url( 'includes/js/canvg.js', dirname( __FILE__ ) ), array( 'jquery' ), $this->version, false );
  91. wp_register_script( 'canvas-toblob-js', plugins_url( 'includes/js/canvas-toblob.js', dirname( __FILE__ ) ), array( 'jquery' ), $this->version, false );
  92. }
  93. /**
  94. * Add shortcode callback for chart shortcode.
  95. *
  96. * @since 1.0.0
  97. *
  98. * @param string $atts Attributes for shortcode.
  99. * @param string $content Content inside Shortcode enclosing tags. Default is ''.
  100. * @return string Parsed Shortcode html markup.
  101. */
  102. public static function easy_chart_shortcode_callback( $atts, $content = '' ) {
  103. $atts = shortcode_atts( array(
  104. 'chart_id' => null,
  105. ), $atts, 'easy_chart' );
  106. extract( $atts );
  107. if ( $chart_id ) {
  108. $plugin = new Easy_Charts();
  109. wp_enqueue_script( 'easy-charts-public-js' );
  110. wp_enqueue_script( 'd3-js' );
  111. wp_enqueue_script( 'uvhcharts-js' );
  112. wp_enqueue_script( 'filesaver-js' );
  113. wp_enqueue_script( 'canvg-js' );
  114. wp_enqueue_script( 'canvas-toblob-js' );
  115. return $plugin->ec_render_chart( $chart_id );
  116. }
  117. return '';
  118. }
  119. /**
  120. * Register shortcode on init hook.
  121. *
  122. * @since 1.0.0
  123. */
  124. public function init() {
  125. add_shortcode( 'easy_chart', array( $this, 'easy_chart_shortcode_callback' ) );
  126. }
  127. }