class-simple-job-board-admin-shortcode-generator.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Simple_Job_Board_Admin_Shortcodes_Generator class
  4. *
  5. * Add job board shortcode builder to TinyMCE. Define the shortcode button and
  6. * parameters in TinyMCE. Also creates shortcodes with given parameters.
  7. *
  8. * @link https://wordpress.org/plugins/simple-job-board
  9. * @since 2.2.3
  10. *
  11. * @package Simple_Job_Board
  12. * @subpackage Simple_Job_Board/admin
  13. * @author PressTigers <support@presstigers.com>
  14. */
  15. class Simple_Job_Board_Admin_Shortcodes_Generator {
  16. /**
  17. * Initilaize class.
  18. *
  19. * @since 2.2.3
  20. */
  21. public function __construct() {
  22. // Action -> Add SJB Button on TinyMCE Editor.
  23. add_action('admin_head', array($this, 'add_tinymce_button'));
  24. }
  25. /**
  26. * Add Filters for TinyMCE buttton.
  27. *
  28. * @since 2.2.3
  29. */
  30. public function add_tinymce_button() {
  31. global $typenow;
  32. // Check user permissions
  33. if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) {
  34. return;
  35. }
  36. // Verify the post type
  37. if (!in_array($typenow, array('post', 'page'))) {
  38. return;
  39. }
  40. // Check if WYSIWYG is enabled
  41. if ('true' === get_user_option('rich_editing')) {
  42. add_filter('mce_external_plugins', array($this, 'add_tinymce_plugin'));
  43. add_filter('mce_buttons', array($this, 'register_tinymce_button'));
  44. }
  45. // Enqueue Core Shortcode Generator Styles
  46. wp_enqueue_style( 'sjb-shortcode-generator', plugin_dir_url(__FILE__) . 'css/simple-job-board-admin-shortcode-generator.css', array(), '1.0.0', 'all', FALSE);
  47. }
  48. /**
  49. * Loads the TinyMCE Button js File.
  50. *
  51. * This function specifies the path to the script with Job Board for TinyMCE.
  52. *
  53. * @since 2.2.3
  54. *
  55. * @param array $plugin_array
  56. * @return array $plugin_array Load button script
  57. */
  58. function add_tinymce_plugin($plugin_array) {
  59. // Enqueue Simple Job Board Shortcode Builder JS File
  60. $plugin_array['sjb_shortcodes_mce_button'] = plugins_url('/js/simple-job-board-admin-shortcodes-generator.js', __FILE__);
  61. return $plugin_array;
  62. }
  63. /**
  64. * Adds the TinyMCE button to the post editor buttons
  65. *
  66. * @since 2.2.3
  67. *
  68. * @param array $buttons TinyMCE buttons
  69. * @return array $buttons Append shortcode builder button with TinyMCE buttons list.
  70. */
  71. function register_tinymce_button( $buttons ) {
  72. // Stack custom event to TinyMCE
  73. array_push( $buttons, 'sjb_shortcodes_mce_button' );
  74. return $buttons;
  75. }
  76. }
  77. new Simple_Job_Board_Admin_Shortcodes_Generator();