class-easy-charts-admin.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. <?php
  2. /**
  3. * The admin-specific functionality of the plugin.
  4. *
  5. * @link https://kiranpotphode.com/
  6. * @since 1.0.0
  7. *
  8. * @package Easy_Charts
  9. * @subpackage Easy_Charts/admin
  10. */
  11. /**
  12. * The admin-specific 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/admin
  19. * @author Kiran Potphode <kiranpotphode15@gmail.com>
  20. */
  21. class Easy_Charts_Admin {
  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. * @todo Move ajax action to separate location
  42. *
  43. * @since 1.0.0
  44. * @param string $plugin_name The name of this plugin.
  45. * @param string $version The version of this plugin.
  46. */
  47. public function __construct( $plugin_name, $version ) {
  48. $this->plugin_name = $plugin_name;
  49. $this->version = $version;
  50. }
  51. /**
  52. * Register the stylesheets for the admin area.
  53. *
  54. * @since 1.0.0
  55. */
  56. public function enqueue_styles() {
  57. /**
  58. * This function is provided for demonstration purposes only.
  59. *
  60. * An instance of this class should be passed to the run() function
  61. * defined in Easy_Charts_Loader as all of the hooks are defined
  62. * in that particular class.
  63. *
  64. * The Easy_Charts_Loader will then create the relationship
  65. * between the defined hooks and the functions defined in this
  66. * class.
  67. */
  68. global $pagenow, $typenow;
  69. wp_enqueue_style( 'insert-chart-button-tc-css', plugin_dir_url( __FILE__ ) . 'css/insert-chart.css', array(), $this->version, 'all' );
  70. if ( 'post-new.php' === $pagenow || 'post.php' === $pagenow && 'easy_charts' === $typenow ) {
  71. wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/easy-charts-admin.css', array(), $this->version, 'all' );
  72. wp_enqueue_style( 'handsontable-css', plugin_dir_url( __FILE__ ) . 'css/handsontable/handsontable.full.css', array(), $this->version, 'all' );
  73. wp_enqueue_style( 'jquery-ui-css', plugin_dir_url( __FILE__ ) . 'css/jquery-ui.min.css', array(), $this->version, 'all' );
  74. wp_enqueue_style( 'responsive-tabs-css', plugin_dir_url( __FILE__ ) . 'css/jquery.pwstabs.min.css', array(), $this->version, 'all' );
  75. wp_enqueue_style( 'font-awesome-css', plugin_dir_url( __FILE__ ) . 'css/font-awesome.min.css', array(), $this->version, 'all' );
  76. wp_enqueue_style( 'wp-color-picker' );
  77. }
  78. }
  79. /**
  80. * Register the JavaScript for the admin area.
  81. *
  82. * @since 1.0.0
  83. */
  84. public function enqueue_scripts() {
  85. /**
  86. * This function is provided for demonstration purposes only.
  87. *
  88. * An instance of this class should be passed to the run() function
  89. * defined in Easy_Charts_Loader as all of the hooks are defined
  90. * in that particular class.
  91. *
  92. * The Easy_Charts_Loader will then create the relationship
  93. * between the defined hooks and the functions defined in this
  94. * class.
  95. */
  96. global $pagenow, $typenow;
  97. if ( 'post-new.php' === $pagenow || 'post.php' === $pagenow && 'easy_charts' === $typenow ) {
  98. wp_enqueue_script( 'easy-charts-admin-js', plugin_dir_url( __FILE__ ) . 'js/easy-charts-admin.js', array( 'jquery' ), $this->version, true );
  99. wp_enqueue_script( 'handsontable-js', plugin_dir_url( __FILE__ ) . 'js/handsontable/handsontable.full.js', array( 'jquery' ), $this->version, false );
  100. wp_enqueue_script( 'd3-js', plugins_url( 'includes/js/d3.min.js', dirname( __FILE__ ) ), array( 'jquery' ), $this->version, false );
  101. wp_enqueue_script( 'filesaver-js', plugins_url( 'includes/js/filesaver.js', dirname( __FILE__ ) ), array( 'jquery' ), $this->version, false );
  102. wp_enqueue_script( 'canvg-js', plugins_url( 'includes/js/canvg.js', dirname( __FILE__ ) ), array( 'jquery' ), $this->version, false );
  103. wp_enqueue_script( 'canvas-toblob-js', plugins_url( 'includes/js/canvas-toblob.js', dirname( __FILE__ ) ), array( 'jquery' ), $this->version, false );
  104. wp_enqueue_script( 'uvhcharts-js', plugins_url( 'includes/js/uvcharts.min.js', dirname( __FILE__ ) ), array( 'jquery' ), $this->version, false );
  105. wp_enqueue_script( 'responsive-tabs-js', plugin_dir_url( __FILE__ ) . 'js/jquery.pwstabs.min.js', array( 'jquery' ), $this->version, false );
  106. wp_enqueue_script( 'bootstrap-touchspin-js', plugin_dir_url( __FILE__ ) . 'js/jquery.bootstrap-touchspin.min.js', array( 'jquery' ), $this->version, false );
  107. wp_enqueue_script( 'jquery-ui-dialog' );
  108. wp_enqueue_script( 'jquery-ui-button' );
  109. wp_enqueue_script( 'jquery-ui-slider' );
  110. wp_enqueue_script( 'jquery-ui-selectmenu' );
  111. wp_enqueue_script( 'jquery-ui-spinner' );
  112. wp_enqueue_script( 'iris' );
  113. wp_enqueue_script( 'wp-color-picker' );
  114. }
  115. }
  116. public function admin_print_scripts() {
  117. echo "<script type='text/javascript'>\n";
  118. echo 'var ajaxurl = "'.esc_url( admin_url( 'admin-ajax.php' ) ).'"';
  119. echo "\n</script>";
  120. }
  121. /**
  122. * Init actions.
  123. *
  124. * This function does all actions hooked on "init" action.
  125. *
  126. * @since 1.0.0
  127. */
  128. public function init() {
  129. $labels = array(
  130. 'name' => _x( 'Easy Charts', 'post type general name', 'easy-charts' ),
  131. 'singular_name' => _x( 'Chart', 'post type singular name', 'easy-charts' ),
  132. 'menu_name' => _x( 'Easy Charts', 'admin menu', 'easy-charts' ),
  133. 'name_admin_bar' => _x( 'Chart', 'add new on admin bar', 'easy-charts' ),
  134. 'add_new' => _x( 'Add New', 'easy-charts' ),
  135. 'add_new_item' => __( 'Add New Chart', 'easy-charts' ),
  136. 'new_item' => __( 'New Chart', 'easy-charts' ),
  137. 'edit_item' => __( 'Edit Chart', 'easy-charts' ),
  138. 'view_item' => __( 'View Chart', 'easy-charts' ),
  139. 'all_items' => __( 'All Charts', 'easy-charts' ),
  140. 'search_items' => __( 'Search Charts', 'easy-charts' ),
  141. 'parent_item_colon' => __( 'Parent Charts:', 'easy-charts' ),
  142. 'not_found' => __( 'No Charts found.', 'easy-charts' ),
  143. 'not_found_in_trash' => __( 'No Charts found in Trash.', 'easy-charts' ),
  144. );
  145. $args = array(
  146. 'labels' => $labels,
  147. 'public' => false,
  148. 'show_ui' => true,
  149. '_builtin' => false,
  150. 'capability_type' => 'page',
  151. 'hierarchical' => true,
  152. 'menu_icon' => 'dashicons-chart-bar',
  153. 'rewrite' => false,
  154. 'query_var' => 'easy_charts',
  155. 'supports' => array(
  156. 'title',
  157. ),
  158. );
  159. register_post_type( 'easy_charts', $args );
  160. add_action( 'wp_ajax_easy_charts_save_chart_data', array( $this, 'easy_charts_save_chart_data_callback' ) );
  161. add_action( 'wp_ajax_easy_charts_get_published_charts', array( $this, 'easy_charts_get_published_charts_callback' ) );
  162. }
  163. /**
  164. * Adds meta box container.
  165. *
  166. * @since 1.0.0
  167. */
  168. public function add_meta_boxes() {
  169. $screens = array( 'easy_charts' );
  170. foreach ( $screens as $screen ) {
  171. add_meta_box(
  172. 'easy_charts_data_metabox',
  173. __( 'Data', 'easy-charts' ),
  174. array( $this, 'easy_charts_data_metabox_callback' ),
  175. $screen
  176. );
  177. add_meta_box(
  178. 'easy_charts_preview_metabox',
  179. __( 'Preview', 'easy-charts' ),
  180. array( $this, 'easy_charts_preview_metabox_callback' ),
  181. $screen
  182. );
  183. add_meta_box(
  184. 'easy_charts_configuration_metabox',
  185. __( 'Configuration', 'easy-charts' ),
  186. array( $this, 'easy_charts_configuration_metabox_callback' ),
  187. $screen
  188. );
  189. add_meta_box(
  190. 'easy_charts_shortcode_metabox',
  191. __( 'Shortcode', 'easy-charts' ),
  192. array( $this, 'easy_charts_shortcode_metabox_callback' ),
  193. $screen,
  194. 'side'
  195. );
  196. }
  197. }
  198. /**
  199. * Add settings menu to easy charts.
  200. *
  201. * @since 1.0.0
  202. */
  203. public function add_options_menu() {
  204. add_submenu_page( 'edit.php?post_type=easy_charts', __( 'Charts settings', 'easy-charts' ), __( 'Charts settings', 'easy-charts' ), 'manage_options', 'easy-charts-settings', array( $this, 'easy_charts_settings_page_callback' ) );
  205. }
  206. /**
  207. * Display settings page.
  208. *
  209. * @since 1.0.0
  210. */
  211. public function easy_charts_settings_page_callback() {
  212. require_once plugin_dir_path( __FILE__ ) . 'partials/easy-charts-settings-page-display.php';
  213. }
  214. /**
  215. * Render Data Meta Box content.
  216. *
  217. * @param WP_Post $post The post object.
  218. * @since 1.0.0
  219. */
  220. public function easy_charts_data_metabox_callback( $post ) {
  221. // Add an nonce field so we can check for it later.
  222. wp_nonce_field( 'easy_charts_save_meta_box_data', 'easy_charts_meta_box_nonce' );
  223. require_once plugin_dir_path( __FILE__ ) . 'partials/easy-charts-data-metabox-display.php';
  224. }
  225. /**
  226. * Render Shortcode Meta Box content.
  227. *
  228. * @param WP_Post $post The post object.
  229. * @since 1.0.0
  230. */
  231. public function easy_charts_shortcode_metabox_callback( $post ) {
  232. require_once plugin_dir_path( __FILE__ ) . 'partials/easy-charts-shortcode-metabox-display.php';
  233. }
  234. /**
  235. * Render Shortcode Meta Box content.
  236. *
  237. * @param WP_Post $post The post object.
  238. * @since 1.0.0
  239. */
  240. public function easy_charts_preview_metabox_callback( $post ) {
  241. require_once plugin_dir_path( __FILE__ ) . 'partials/easy-charts-preview-metabox-display.php';
  242. }
  243. /**
  244. * Render Shortcode Meta Box content.
  245. *
  246. * @param WP_Post $post The post object.
  247. * @since 1.0.0
  248. */
  249. public function easy_charts_configuration_metabox_callback( $post ) {
  250. require_once plugin_dir_path( __FILE__ ) . 'partials/easy-charts-configuration-metabox-display.php';
  251. }
  252. /**
  253. * When the post is saved, saves our custom data.
  254. *
  255. * @param int $post_id The ID of the post being saved.
  256. */
  257. public function easy_charts_save_meta_box_data( $post_id ) {
  258. // Check if our nonce is set.
  259. if ( ! isset( $_POST['easy_charts_meta_box_nonce'] ) ) {
  260. return;
  261. }
  262. // Verify that the nonce is valid.
  263. if ( ! wp_verify_nonce( $_POST['easy_charts_meta_box_nonce'], 'easy_charts_save_meta_box_data' ) ) {
  264. return;
  265. }
  266. // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  267. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  268. return;
  269. }
  270. // Check the user's permissions.
  271. if ( isset( $_POST['post_type'] ) && 'easy_charts' === $_POST['post_type'] ) {
  272. if ( ! current_user_can( 'edit_page', $post_id ) ) {
  273. return;
  274. }
  275. } else {
  276. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  277. return;
  278. }
  279. }
  280. $ec_chart_meta = array(
  281. 'position' => $_POST['ec_chart_meta_position'],
  282. 'caption' => $_POST['ec_chart_meta_caption'],
  283. 'subcaption' => $_POST['ec_chart_meta_subcaption'],
  284. 'hlabel' => $_POST['ec_chart_meta_hlabel'],
  285. 'hsublabel' => $_POST['ec_chart_meta_hsublabel'],
  286. 'vlabel' => $_POST['ec_chart_meta_vlabel'],
  287. 'vsublabel' => $_POST['ec_chart_meta_vsublabel'],
  288. 'isDownloadable' => (integer) $_POST['ec_chart_meta_isDownloadable'],
  289. 'downloadLabel' => $_POST['ec_chart_meta_downloadLabel'],
  290. );
  291. $ec_chart_graph = array(
  292. 'responsive' => (boolean) $_POST['ec_chart_graph_responsive'],
  293. 'palette' => $_POST['ec_chart_graph_palette'],
  294. 'bgcolor' => $_POST['ec_chart_graph_bgcolor'],
  295. 'orientation' => $_POST['ec_chart_graph_orientation'],
  296. 'opacity' => (float) $_POST['ec_chart_graph_opacity'],
  297. );
  298. $ec_chart_dimension = array(
  299. 'width' => (integer) $_POST['ec_chart_dimension_width'],
  300. 'height' => (integer) $_POST['ec_chart_dimension_height'],
  301. );
  302. $ec_chart_margin = array(
  303. 'top' => (integer) $_POST['ec_chart_margin_top'],
  304. 'bottom' => (integer) $_POST['ec_chart_margin_bottom'],
  305. 'left' => (integer) $_POST['ec_chart_margin_left'],
  306. 'right' => (integer) $_POST['ec_chart_margin_right'],
  307. );
  308. $ec_chart_frame = array(
  309. 'bgcolor' => $_POST['ec_chart_frame_bgcolor'],
  310. );
  311. $ec_chart_axis = array(
  312. 'opacity' => (float) $_POST['ec_chart_axis_opacity'],
  313. 'ticks' => (integer) $_POST['ec_chart_axis_ticks'],
  314. 'subticks' => (integer) $_POST['ec_chart_axis_subticks'],
  315. 'padding' => (integer) $_POST['ec_chart_axis_padding'],
  316. 'strokecolor' => $_POST['ec_chart_axis_strokecolor'],
  317. 'minor' => (integer) $_POST['ec_chart_axis_minor'],
  318. 'fontfamily' => $_POST['ec_chart_axis_fontfamily'],
  319. 'fontsize' => $_POST['ec_chart_axis_fontsize'],
  320. 'fontweight' => $_POST['ec_chart_axis_fontweight'],
  321. 'showticks' => (integer) $_POST['ec_chart_axis_showticks'],
  322. 'showsubticks' => (integer) $_POST['ec_chart_axis_showsubticks'],
  323. 'showtext' => (integer) $_POST['ec_chart_axis_showtext'],
  324. );
  325. $ec_chart_label = array(
  326. 'strokecolor' => $_POST['ec_chart_label_strokecolor'],
  327. 'fontfamily' => $_POST['ec_chart_label_fontfamily'],
  328. 'fontsize' => $_POST['ec_chart_label_fontsize'],
  329. 'fontweight' => $_POST['ec_chart_label_fontweight'],
  330. 'showlabel' => (integer) $_POST['ec_chart_label_showlabel'],
  331. 'precision' => (integer) $_POST['ec_chart_label_precision'],
  332. 'prefix' => $_POST['ec_chart_label_prefix'],
  333. 'suffix' => $_POST['ec_chart_label_suffix'],
  334. );
  335. $ec_chart_legend = array(
  336. 'position' => $_POST['ec_chart_legend_position'],
  337. 'fontfamily' => $_POST['ec_chart_legend_fontfamily'],
  338. 'fontsize' => $_POST['ec_chart_legend_fontsize'],
  339. 'fontweight' => $_POST['ec_chart_legend_fontweight'],
  340. 'color' => $_POST['ec_chart_legend_color'],
  341. 'strokewidth' => (float) $_POST['ec_chart_legend_strokewidth'],
  342. 'textmargin' => (integer) $_POST['ec_chart_legend_textmargin'],
  343. 'symbolsize' => (integer) $_POST['ec_chart_legend_symbolsize'],
  344. 'inactivecolor' => $_POST['ec_chart_legend_inactivecolor'],
  345. 'legendstart' => (integer) $_POST['ec_chart_legend_legendstart'],
  346. 'legendtype' => 'categories',
  347. 'showlegends' => (integer) $_POST['ec_chart_legend_showlegends'],
  348. );
  349. $ec_chart_scale = array(
  350. 'type' => $_POST['ec_chart_scale_type'],
  351. 'ordinality' => (float) $_POST['ec_chart_scale_ordinality'],
  352. );
  353. $ec_chart_tooltip = array(
  354. 'show' => (integer) $_POST['ec_chart_tooltip_show'],
  355. 'format' => $_POST['ec_chart_tooltip_format'],
  356. );
  357. $ec_chart_caption = array(
  358. 'fontfamily' => $_POST['ec_chart_caption_fontfamily'],
  359. 'fontsize' => $_POST['ec_chart_caption_fontsize'],
  360. 'fontweight' => $_POST['ec_chart_caption_fontweight'],
  361. 'textdecoration' => $_POST['ec_chart_caption_textdecoration'],
  362. 'strokecolor' => $_POST['ec_chart_caption_strokecolor'],
  363. 'cursor' => $_POST['ec_chart_caption_cursor'],
  364. );
  365. $ec_chart_subcaption = array(
  366. 'fontfamily' => $_POST['ec_chart_subcaption_fontfamily'],
  367. 'fontsize' => $_POST['ec_chart_subcaption_fontsize'],
  368. 'fontweight' => $_POST['ec_chart_subcaption_fontweight'],
  369. 'textdecoration' => $_POST['ec_chart_subcaption_textdecoration'],
  370. 'strokecolor' => $_POST['ec_chart_subcaption_strokecolor'],
  371. 'cursor' => $_POST['ec_chart_subcaption_cursor'],
  372. );
  373. $ec_chart_bar = array(
  374. 'fontfamily' => $_POST['ec_chart_bar_fontfamily'],
  375. 'fontsize' => $_POST['ec_chart_bar_fontsize'],
  376. 'fontweight' => $_POST['ec_chart_bar_fontweight'],
  377. 'strokecolor' => $_POST['ec_chart_bar_strokecolor'],
  378. 'textcolor' => $_POST['ec_chart_bar_textcolor'],
  379. );
  380. $ec_chart_line = array(
  381. 'interpolation' => $_POST['ec_chart_line_interpolation'],
  382. );
  383. $ec_chart_area = array(
  384. 'interpolation' => $_POST['ec_chart_area_interpolation'],
  385. 'opacity' => (float) $_POST['ec_chart_area_opacity'],
  386. 'offset' => $_POST['ec_chart_area_offset'],
  387. );
  388. $ec_chart_pie = array(
  389. 'fontfamily' => $_POST['ec_chart_pie_fontfamily'],
  390. 'fontsize' => $_POST['ec_chart_pie_fontsize'],
  391. 'fontweight' => $_POST['ec_chart_pie_fontweight'],
  392. 'fontvariant' => $_POST['ec_chart_pie_fontvariant'],
  393. 'fontfill' => $_POST['ec_chart_pie_fontfill'],
  394. 'strokecolor' => $_POST['ec_chart_pie_strokecolor'],
  395. 'strokewidth' => $_POST['ec_chart_pie_strokewidth'],
  396. );
  397. $ec_chart_donut = array(
  398. 'fontfamily' => $_POST['ec_chart_donut_fontfamily'],
  399. 'fontsize' => $_POST['ec_chart_donut_fontsize'],
  400. 'fontweight' => $_POST['ec_chart_donut_fontweight'],
  401. 'fontvariant' => $_POST['ec_chart_donut_fontvariant'],
  402. 'fontfill' => $_POST['ec_chart_donut_fontfill'],
  403. 'strokecolor' => $_POST['ec_chart_donut_strokecolor'],
  404. 'strokewidth' => $_POST['ec_chart_donut_strokewidth'],
  405. 'factor' => $_POST['ec_chart_donut_factor'],
  406. );
  407. update_post_meta( $post_id, '_ec_chart_type', $_POST['ec_chart_type'] );
  408. update_post_meta( $post_id, '_ec_chart_meta', $ec_chart_meta );
  409. update_post_meta( $post_id, '_ec_chart_graph', $ec_chart_graph );
  410. update_post_meta( $post_id, '_ec_chart_dimension', $ec_chart_dimension );
  411. update_post_meta( $post_id, '_ec_chart_margin', $ec_chart_margin );
  412. update_post_meta( $post_id, '_ec_chart_frame', $ec_chart_frame );
  413. update_post_meta( $post_id, '_ec_chart_axis', $ec_chart_axis );
  414. update_post_meta( $post_id, '_ec_chart_label', $ec_chart_label );
  415. update_post_meta( $post_id, '_ec_chart_legend', $ec_chart_legend );
  416. update_post_meta( $post_id, '_ec_chart_scale', $ec_chart_scale );
  417. update_post_meta( $post_id, '_ec_chart_tooltip', $ec_chart_tooltip );
  418. update_post_meta( $post_id, '_ec_chart_caption', $ec_chart_caption );
  419. update_post_meta( $post_id, '_ec_chart_subcaption', $ec_chart_subcaption );
  420. update_post_meta( $post_id, '_ec_chart_bar', $ec_chart_bar );
  421. update_post_meta( $post_id, '_ec_chart_line', $ec_chart_line );
  422. update_post_meta( $post_id, '_ec_chart_area', $ec_chart_area );
  423. update_post_meta( $post_id, '_ec_chart_pie', $ec_chart_pie );
  424. update_post_meta( $post_id, '_ec_chart_donut', $ec_chart_donut );
  425. }
  426. /**
  427. * Ajax callback for save chart data.
  428. *
  429. * @since 1.0.0
  430. */
  431. public function easy_charts_save_chart_data_callback() {
  432. $plugin = new Easy_Charts();
  433. check_ajax_referer( 'ec-ajax-nonce', '_nonce_check' );
  434. if ( 'easy_charts_save_chart_data' !== $_POST['action'] ) {
  435. exit( 0 );
  436. }
  437. update_post_meta( $_POST['chart_id'], '_easy_charts_chart_data', $_POST['chart_data'] );
  438. echo wp_json_encode( $plugin->get_ec_chart_data( $_POST['chart_id'] ) );
  439. exit( 0 );
  440. }
  441. /**
  442. * Get published charts.
  443. *
  444. * @since 1.0.0
  445. */
  446. public function easy_charts_get_published_charts_callback() {
  447. if ( 'easy_charts_get_published_charts' !== $_POST['action'] ) {
  448. exit( 0 );
  449. }
  450. $args = array(
  451. 'post_type' => 'easy_charts',
  452. 'post_status' => 'publish',
  453. 'posts_per_page' => -1,
  454. );
  455. $chart_query = new WP_Query( $args );
  456. $charts = array();
  457. if ( $chart_query->have_posts() ) {
  458. foreach ( $chart_query->posts as $chart_key => $chart ) {
  459. $chart_title = '';
  460. if ( '' === $chart->post_title ) {
  461. $chart_title = 'Chart-'.$chart->ID;
  462. } else {
  463. $chart_title = $chart->post_title;
  464. }
  465. $charts[] = array(
  466. 'text' => $chart_title,
  467. 'value' => "[easy_chart chart_id='".$chart->ID."']",
  468. );
  469. }
  470. }
  471. wp_reset_postdata();
  472. echo wp_json_encode( $charts );
  473. exit( 0 );
  474. }
  475. /**
  476. * Add insert chart button to editor.
  477. *
  478. * @since 1.0.0
  479. */
  480. public function easy_charts_add_insert_chart_button() {
  481. // check user permissions
  482. if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
  483. return;
  484. }
  485. // check if WYSIWYG is enabled
  486. if ( get_user_option( 'rich_editing' ) === 'true' ) {
  487. add_filter( 'mce_external_plugins', array( $this, 'easy_charts_add_tinymce_plugin' ) );
  488. add_filter( 'mce_buttons', array( $this, 'easy_charts_register_insert_chart_tc_button' ) );
  489. }
  490. }
  491. /**
  492. * Add Insert chart button js.
  493. *
  494. * @since 1.0.0
  495. *
  496. * @param array $plugin_array Array of plugins urls.
  497. * @return array Array of plugin urls with newly added plugin.
  498. */
  499. public function easy_charts_add_tinymce_plugin( $plugin_array ) {
  500. $plugin_array['easy_charts_insert_chart_tc_button'] = plugin_dir_url( __FILE__ ).'js/insert-chart-button.js';
  501. return $plugin_array;
  502. }
  503. /**
  504. * Add Insert chart button.
  505. *
  506. * @since 1.0.0
  507. *
  508. * @param array $buttons Array of button names.
  509. * @return array Array if buttons with newly added button name.
  510. */
  511. function easy_charts_register_insert_chart_tc_button( $buttons ) {
  512. array_push( $buttons, 'easy_charts_insert_chart_tc_button' );
  513. return $buttons;
  514. }
  515. }