Black_Studio_TinyMCE_Admin

The Black Studio TinyMCE Widget Black Studio TinyMCE Admin class.

Defined (1)

The class is defined in the following location(s).

/includes/class-admin.php  
  1. final class Black_Studio_TinyMCE_Admin { 
  2.  
  3. /** 
  4. * The single instance of the class 
  5. * @var object 
  6. * @since 2.0.0 
  7. */ 
  8. protected static $_instance = null; 
  9.  
  10. /** 
  11. * Array containing the plugin links 
  12. * @var array 
  13. * @since 2.0.0 
  14. */ 
  15. protected $links; 
  16.  
  17. /** 
  18. * Return the single class instance 
  19. * @return object 
  20. * @since 2.0.0 
  21. */ 
  22. public static function instance() { 
  23. if ( is_null( self::$_instance ) ) { 
  24. self::$_instance = new self(); 
  25. return self::$_instance; 
  26.  
  27. /** 
  28. * Class constructor 
  29. * @uses add_action() 
  30. * @uses add_filter() 
  31. * @uses get_option() 
  32. * @uses get_bloginfo() 
  33. * @global object $wp_embed 
  34. * @since 2.0.0 
  35. */ 
  36. protected function __construct() { 
  37. // Register action and filter hooks 
  38. add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); 
  39. add_action( 'admin_init', array( $this, 'admin_init' ), 20 ); 
  40.  
  41. /** 
  42. * Prevent the class from being cloned 
  43. * @return void 
  44. * @since 2.0.0 
  45. */ 
  46. protected function __clone() { 
  47. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ uh?' ), '2.0' ); 
  48.  
  49. /** 
  50. * Load language files 
  51. * @uses load_plugin_textdomain() 
  52. * @return void 
  53. * @since 2.0.0 
  54. */ 
  55. public function load_textdomain() { 
  56. load_plugin_textdomain( 'black-studio-tinymce-widget', false, dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' ); 
  57.  
  58. /** 
  59. * Checks if the plugin admin code should be loaded 
  60. * @uses apply_filters() 
  61. * @global string $pagenow 
  62. * @return void 
  63. * @since 2.0.0 
  64. */ 
  65. public function enabled() { 
  66. global $pagenow; 
  67. $enabled_pages = apply_filters( 'black_studio_tinymce_enable_pages', array( 'widgets.php', 'customize.php', 'admin-ajax.php' ) ); 
  68. return apply_filters( 'black_studio_tinymce_enable', in_array( $pagenow, $enabled_pages ) ); 
  69.  
  70. /** 
  71. * Add actions and filters (only in widgets admin page) 
  72. * @uses add_action() 
  73. * @uses add_filter() 
  74. * @uses do_action() 
  75. * @return void 
  76. * @since 2.0.0 
  77. */ 
  78. public function admin_init() { 
  79. $this->init_links(); 
  80. add_action( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 ); 
  81. if ( $this->enabled() ) { 
  82. add_action( 'admin_head', array( $this, 'enqueue_media' ) ); 
  83. add_action( 'admin_print_scripts', array( $this, 'admin_print_scripts' ) ); 
  84. add_action( 'admin_print_styles', array( $this, 'admin_print_styles' ) ); 
  85. add_action( 'admin_print_footer_scripts', array( $this, 'admin_print_footer_scripts' ) ); 
  86. add_action( 'black_studio_tinymce_before_editor', array( $this, 'display_links' ) ); // consider donating if you remove links 
  87. add_action( 'black_studio_tinymce_editor', array( $this, 'editor' ), 10, 4 ); 
  88. add_action( 'black_studio_tinymce_after_editor', array( $this, 'fix_the_editor_content_filter' ) ); 
  89. add_action( 'wp_tiny_mce_init', array( $this, 'wp_tiny_mce_init' ) ); 
  90. add_filter( 'wp_editor_settings', array( $this, 'editor_settings' ), 5, 2 ); 
  91. add_filter( 'tiny_mce_before_init', array( $this, 'tinymce_fix_rtl' ), 10 ); 
  92. add_filter( 'tiny_mce_before_init', array( $this, 'tinymce_fullscreen' ), 10, 2 ); 
  93. add_filter( 'quicktags_settings', array( $this, 'quicktags_fullscreen' ), 10, 2 ); 
  94. do_action( 'black_studio_tinymce_load' ); 
  95.  
  96. /** 
  97. * Instantiate tinyMCE editor 
  98. * @uses add_thickbox() 
  99. * @uses wp_enqueue_media() 
  100. * @return void 
  101. * @since 2.0.0 
  102. */ 
  103. public function enqueue_media() { 
  104. // Add support for thickbox media dialog 
  105. add_thickbox(); 
  106. // New media modal dialog (WP 3.5+) 
  107. if ( function_exists( 'wp_enqueue_media' ) ) { 
  108. wp_enqueue_media(); 
  109.  
  110. /** 
  111. * Enqueue styles 
  112. * @uses wp_enqueue_style() 
  113. * @uses Black_Studio_TinyMCE_Plugin::enqueue_style() 
  114. * @return void 
  115. * @since 2.0.0 
  116. */ 
  117. public function admin_print_styles() { 
  118. wp_enqueue_style( 'wp-jquery-ui-dialog' ); 
  119. wp_enqueue_style( 'editor-buttons' ); 
  120. $this->enqueue_style(); 
  121.  
  122. /** 
  123. * Helper function to enqueue style 
  124. * @uses apply_filters() 
  125. * @uses wp_enqueue_style() 
  126. * @uses plugins_url() 
  127. * @uses SCRIPT_DEBUG 
  128. * @return void 
  129. * @since 2.0.0 
  130. */ 
  131. public function enqueue_style() { 
  132. $style = apply_filters( 'black-studio-tinymce-widget-style', 'black-studio-tinymce-widget' ); 
  133. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  134. wp_enqueue_style( 
  135. $style,  
  136. plugins_url( 'css/' . $style . $suffix. '.css', dirname( __FILE__ ) ),  
  137. array(),  
  138. bstw()->get_version() 
  139. ); 
  140.  
  141. /** 
  142. * Enqueue header scripts 
  143. * @uses wp_enqueue_script() 
  144. * @uses do_action() 
  145. * @return void 
  146. * @since 2.0.0 
  147. */ 
  148. public function admin_print_scripts() { 
  149. wp_enqueue_script( 'media-upload' ); 
  150. wp_enqueue_script( 'wplink' ); 
  151. wp_enqueue_script( 'wpdialogs-popup' ); 
  152. $this->enqueue_script(); 
  153. $this->localize_script(); 
  154. do_action( 'wp_enqueue_editor', array( 'tinymce' => true ) ); 
  155.  
  156. /** 
  157. * Helper function to enqueue script 
  158. * @uses apply_filters() 
  159. * @uses wp_enqueue_script() 
  160. * @uses plugins_url() 
  161. * @uses SCRIPT_DEBUG 
  162. * @return void 
  163. * @since 2.0.0 
  164. */ 
  165. public function enqueue_script() { 
  166. $script = apply_filters( 'black-studio-tinymce-widget-script', 'black-studio-tinymce-widget' ); 
  167. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  168. wp_enqueue_script( 
  169. $script,  
  170. plugins_url( 'js/' . $script . $suffix . '.js', dirname( __FILE__ ) ),  
  171. array( 'jquery', 'editor', 'quicktags' ),  
  172. bstw()->get_version(),  
  173. true 
  174. ); 
  175.  
  176. /** 
  177. * Helper function to enqueue localized script 
  178. * @uses apply_filters() 
  179. * @uses wp_localize_script() 
  180. * @return void 
  181. * @since 2.0.0 
  182. */ 
  183. public function localize_script() { 
  184. $container_selectors = apply_filters( 'black_studio_tinymce_container_selectors', array( 'div.widget', 'div.widget-inside' ) ); 
  185. $activate_events = apply_filters( 'black_studio_tinymce_activate_events', array() ); 
  186. $deactivate_events = apply_filters( 'black_studio_tinymce_deactivate_events', array() ); 
  187. $data = array( 
  188. 'container_selectors' => implode( ', ', $container_selectors ),  
  189. 'activate_events' => $activate_events,  
  190. 'deactivate_events' => $deactivate_events,  
  191. /** translators: error message shown when a duplicated widget ID is detected */ 
  192. 'error_duplicate_id' => __( 'ERROR: Duplicate widget ID detected. To avoid content loss, please create a new widget with the same content and then delete this one.', 'black-studio-tinymce-widget' ) 
  193. ); 
  194. wp_localize_script( apply_filters( 'black-studio-tinymce-widget-script', 'black-studio-tinymce-widget' ), 'bstw_data', $data ); 
  195.  
  196. /** 
  197. * Enqueue footer scripts 
  198. * @return void 
  199. * @since 2.0.0 
  200. */ 
  201. public function admin_print_footer_scripts() { 
  202. $this->editor( '', 'black-studio-tinymce-widget', 'black-studio-tinymce-widget' ); 
  203.  
  204. /** 
  205. * Output the visual editor 
  206. * @uses wp_editor() 
  207. * @param string $text 
  208. * @param string $editor_id 
  209. * @param string $name 
  210. * @param string $type 
  211. * @return void 
  212. * @since 2.0.0 
  213. */ 
  214. public function editor( $text, $editor_id, $name = '', $type = 'visual' ) { 
  215. wp_editor( $text, $editor_id, array( 'textarea_name' => $name, 'default_editor' => $type == 'visual' ? 'tmce' : 'html' ) ); 
  216.  
  217. /** 
  218. * Remove editor content filters for multiple editor instances 
  219. * Workaround for WordPress Core bug #28403 https://core.trac.wordpress.org/ticket/28403 
  220. * @uses remove_filter 
  221. * @return void 
  222. * @since 2.1.7 
  223. */ 
  224. public function fix_the_editor_content_filter() { 
  225. remove_filter( 'the_editor_content', 'wp_htmledit_pre' ); 
  226. remove_filter( 'the_editor_content', 'wp_richedit_pre' ); 
  227.  
  228. /** 
  229. * Setup editor instance for event handling 
  230. * @return void 
  231. * @since 2.2.1 
  232. */ 
  233. public function wp_tiny_mce_init() { 
  234. $script = 'black-studio-tinymce-widget-setup'; 
  235. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  236. echo "\t\t" . '<script type="text/javascript" src="' . plugins_url( 'js/' . $script . $suffix . '.js', dirname( __FILE__ ) ) . '"></script>' . "\n"; // xss ok 
  237.  
  238. /** 
  239. * Set editor settings 
  240. * @param mixed[] $settings 
  241. * @param string $editor_id 
  242. * @return mixed[] 
  243. * @since 2.0.0 
  244. */ 
  245. public function editor_settings( $settings, $editor_id ) { 
  246. if ( strstr( $editor_id, 'black-studio-tinymce' ) ) { 
  247. $settings['tinymce'] = array( 
  248. 'wp_skip_init' => 'widget-black-studio-tinymce-__i__-text' == $editor_id,  
  249. 'add_unload_trigger' => false,  
  250. 'wp_autoresize_on' => false,  
  251. ); 
  252. $settings['editor_height'] = 350; 
  253. $settings['dfw'] = true; 
  254. $settings['editor_class'] = 'black-studio-tinymce'; 
  255. return $settings; 
  256.  
  257. /** 
  258. * Initialize plugin links 
  259. * @return void 
  260. * @since 2.0.0 
  261. */ 
  262. public function init_links() { 
  263. $this->links = array( 
  264. /** translators: text used for plugin home link */ 
  265. 'https://wordpress.org/plugins/black-studio-tinymce-widget/' => __( 'Home', 'black-studio-tinymce-widget' ),  
  266. /** translators: text used for support faq link */ 
  267. 'https://wordpress.org/plugins/black-studio-tinymce-widget/faq/' => __( 'FAQ', 'black-studio-tinymce-widget' ),  
  268. /** translators: text used for support forum link */ 
  269. 'https://wordpress.org/support/plugin/black-studio-tinymce-widget' => __( 'Support', 'black-studio-tinymce-widget' ),  
  270. /** translators: text used for reviews link */ 
  271. 'https://wordpress.org/support/view/plugin-reviews/black-studio-tinymce-widget' => __( 'Rate', 'black-studio-tinymce-widget' ),  
  272. /** translators: text used for follow on twitter link */ 
  273. 'https://twitter.com/blackstudioita' => __( 'Follow', 'black-studio-tinymce-widget' ),  
  274. /** translators: text used for donation link */ 
  275. 'http://www.blackstudio.it/en/wordpress-plugins/black-studio-tinymce-widget/' => __( 'Donate', 'black-studio-tinymce-widget' ),  
  276. ); 
  277.  
  278. /** 
  279. * Display plugin links 
  280. * @return void 
  281. * @since 2.0.0 
  282. */ 
  283. public function display_links() { 
  284. echo "\t<div class='bstw-links'>\n"; 
  285. echo "\t\t<span class='bstw-links-list'>\n"; 
  286. $counter = count( $this->links ) - 1; 
  287. foreach ( $this->links as $url => $label ) { 
  288. $separator = ( $counter-- > 0 ? ' | ' : '' ); 
  289. echo "\t\t\t<a href='" . esc_url( $url ) . "' target='_blank'>" . esc_html( $label ) . "</a>$separator\n"; // xss ok 
  290. echo "\t\t</span>\n"; 
  291. /** translators: text used for the icon that shows the plugin links */ 
  292. echo "\t\t<a class='bstw-links-icon icon16 icon-plugins' href='#' title='" . esc_attr( __( 'About Black Studio TinyMCE Widget plugin', 'black-studio-tinymce-widget' ) ) . "'></a>\n"; 
  293. echo "\t</div>\n"; 
  294.  
  295. /** 
  296. * Show row meta on the plugin screen 
  297. * @uses esc_html() 
  298. * @uses esc_url() 
  299. * @param string[] $links 
  300. * @param string $file 
  301. * @return string[] 
  302. * @since 2.0.0 
  303. */ 
  304. public function plugin_row_meta( $links, $file ) { 
  305. if ( $file == bstw()->get_basename() ) { 
  306. foreach ( $this->links as $url => $label ) { 
  307. $links[ $label ] = '<a href="' . esc_url( $url ) . '" target="_blank">' . esc_html( $label ) . '</a>'; 
  308. return $links; 
  309.  
  310. /** 
  311. * Fix for rtl languages 
  312. * @param mixed[] $settings 
  313. * @return mixed[] 
  314. * @since 2.1.0 
  315. */ 
  316. public function tinymce_fix_rtl( $settings ) { 
  317. // This fix has to be applied to all editor instances (not just BSTW ones) 
  318. if ( is_rtl() && isset( $settings['plugins'] ) && ', directionality' == $settings['plugins'] ) { 
  319. unset( $settings['plugins'] ); 
  320. return $settings; 
  321.  
  322. /** 
  323. * Apply TinyMCE default fullscreen 
  324. * @param mixed[] $settings 
  325. * @param string $editor_id 
  326. * @return mixed[] 
  327. * @since 2.1.2 
  328. */ 
  329. public function tinymce_fullscreen( $settings, $editor_id ) { 
  330. if ( strstr( $editor_id, 'black-studio-tinymce' ) ) { 
  331. for ( $i = 1; $i <= 4; $i++ ) { 
  332. $toolbar = 'toolbar' . $i; 
  333. if ( isset( $settings[ $toolbar ] ) ) { 
  334. $settings[ $toolbar ] = str_replace( 'wp_fullscreen', 'wp_fullscreen, fullscreen', $settings[ $toolbar ] ); 
  335. return $settings; 
  336.  
  337. /** 
  338. * Disable Quicktags default fullscreen 
  339. * @param mixed[] $settings 
  340. * @param string $editor_id 
  341. * @return mixed[] 
  342. * @since 2.1.2 
  343. */ 
  344. public function quicktags_fullscreen( $settings, $editor_id ) { 
  345. if ( strstr( $editor_id, 'black-studio-tinymce' ) ) { 
  346. $settings['buttons'] = str_replace( ', fullscreen', '', $settings['buttons'] ); 
  347. return $settings; 
  348.  
  349. } // END class Black_Studio_TinyMCE_Admin