Advertisement
Guest User

class-wc-shortcodes-admin

a guest
Aug 10th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3.     exit; // Exit if accessed directly
  4. }
  5.  
  6. /**
  7.  * WooCommerce Shortcodes Admin class.
  8.  */
  9. class WC_Shortcodes_Admin {
  10.  
  11.     /**
  12.      * Constructor.
  13.      */
  14.     public function __construct() {
  15.         add_action( 'admin_head', array( $this, 'add_shortcode_button' ) );
  16.         add_filter( 'tiny_mce_version', array( $this, 'refresh_mce' ) );
  17.         add_filter( 'mce_external_languages', array( $this, 'add_tinymce_locales' ), 10, 1 );
  18.         add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
  19.     }
  20.  
  21.     /**
  22.      * Add a button for shortcodes to the WP editor.
  23.      */
  24.     public function add_shortcode_button() {
  25.         if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
  26.             return;
  27.         }
  28.  
  29.         if ( 'true' == get_user_option( 'rich_editing' ) ) {
  30.             add_filter( 'mce_external_plugins', array( $this, 'add_shortcode_tinymce_plugin' ) );
  31.             add_filter( 'mce_buttons', array( $this, 'register_shortcode_button' ) );
  32.         }
  33.     }
  34.  
  35.     /**
  36.      * TinyMCE locales function.
  37.      *
  38.      * @param  array $locales TinyMCE locales.
  39.      *
  40.      * @return array
  41.      */
  42.     public function add_tinymce_locales( $locales ) {
  43.         $locales['woocommerce_shortcodes'] = get_stylesheet_directory_uri() . '/inc/woocommerce-shortcodes/wc-shortcodes-editor-i18n.php';
  44.  
  45.         return $locales;
  46.     }
  47.  
  48.     /**
  49.      * Register the shortcode button.
  50.      *
  51.      * @param array $buttons
  52.      * @return array
  53.      */
  54.     public function register_shortcode_button( $buttons ) {
  55.         array_push( $buttons, '|', 'woocommerce_shortcodes' );
  56.  
  57.         return $buttons;
  58.     }
  59.  
  60.     /**
  61.      * Add the shortcode button to TinyMCE.
  62.      *
  63.      * @param  array $plugins TinyMCE plugins.
  64.      *
  65.      * @return array          WooCommerce TinyMCE plugin.
  66.      */
  67.     public function add_shortcode_tinymce_plugin( $plugins ) {
  68.         $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  69.  
  70.         $plugins['woocommerce_shortcodes'] = get_stylesheet_directory_uri(). '/assets/js/editor' . $suffix . '.js';
  71.  
  72.         return $plugins;
  73.     }
  74.  
  75.     /**
  76.      * Force TinyMCE to refresh.
  77.      *
  78.      * @param  int $version
  79.      *
  80.      * @return int
  81.      */
  82.     public function refresh_mce( $version ) {
  83.         $version += 3;
  84.  
  85.         return $version;
  86.     }
  87.  
  88.     /**
  89.      * Admin scripts.
  90.      *
  91.      * @param  string $hook Page slug.
  92.      *
  93.      * @return void
  94.      */
  95.     public function admin_scripts( $hook ) {
  96.         wp_enqueue_style( 'woocommerce-shortcodes', get_stylesheet_directory_uri(). '/assets/css/editor.css', array( 'woocommerce_admin_menu_styles' ), '1.0.0', 'all' );
  97.     }
  98. }
  99.  
  100. new WC_Shortcodes_Admin();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement