1 <?php
  2 /**
  3  * Admin Editor
  4  *
  5  * Methods which tweak the WP Editor.
  6  *
  7  * @author      WooThemes
  8  * @category    Admin
  9  * @package     WooCommerce/Admin
 10  * @version     2.1.0
 11 */
 12 
 13 if ( ! defined( 'ABSPATH' ) ) {
 14     exit; // Exit if accessed directly
 15 }
 16 
 17 /**
 18  * WC_Admin_Editor class.
 19  *
 20  * @since 2.0
 21  */
 22 class WC_Admin_Editor {
 23 
 24     /**
 25      * Constructor
 26      */
 27     public function __construct() {
 28         add_action( 'admin_head', array( $this, 'add_shortcode_button' ) );
 29         add_filter( 'tiny_mce_version', array( $this, 'refresh_mce' ) );
 30         add_filter( 'mce_external_languages', array( $this, 'add_tinymce_lang' ), 10, 1 );
 31     }
 32 
 33     /**
 34      * Add a button for shortcodes to the WP editor.
 35      */
 36     public function add_shortcode_button() {
 37         if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
 38             return;
 39         }
 40 
 41         if ( 'true' == get_user_option( 'rich_editing' ) ) {
 42             add_filter( 'mce_external_plugins', array( $this, 'add_shortcode_tinymce_plugin' ) );
 43             add_filter( 'mce_buttons', array( $this, 'register_shortcode_button' ) );
 44         }
 45     }
 46 
 47     /**
 48      * woocommerce_add_tinymce_lang function.
 49      *
 50      * @param array $arr
 51      * @return array
 52      */
 53     public function add_tinymce_lang( $arr ) {
 54         $arr['wc_shortcodes_button'] = WC()->plugin_path() . '/assets/js/admin/editor_plugin_lang.php';
 55         return $arr;
 56     }
 57 
 58     /**
 59      * Register the shortcode button.
 60      *
 61      * @param array $buttons
 62      * @return array
 63      */
 64     public function register_shortcode_button( $buttons ) {
 65         array_push( $buttons, '|', 'wc_shortcodes_button' );
 66         return $buttons;
 67     }
 68 
 69     /**
 70      * Add the shortcode button to TinyMCE
 71      *
 72      * @param array $plugin_array
 73      * @return array
 74      */
 75     public function add_shortcode_tinymce_plugin( $plugin_array ) {
 76         $wp_version = get_bloginfo( 'version' );
 77         $suffix     = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
 78 
 79         if ( version_compare( $wp_version, '3.9', '>=' ) ) {
 80             $plugin_array['wc_shortcodes_button'] = WC()->plugin_url() . '/assets/js/admin/editor_plugin' . $suffix . '.js';
 81         } else {
 82             $plugin_array['wc_shortcodes_button'] = WC()->plugin_url() . '/assets/js/admin/editor_plugin_legacy' . $suffix . '.js';
 83         }
 84 
 85         return $plugin_array;
 86     }
 87 
 88     /**
 89      * Force TinyMCE to refresh.
 90      *
 91      * @param int $ver
 92      * @return int
 93      */
 94     public function refresh_mce( $ver ) {
 95         $ver += 3;
 96         return $ver;
 97     }
 98 
 99 }
100 
101 new WC_Admin_Editor();
102 
WooCommerce API documentation generated by ApiGen 2.8.0