PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/black-studio-tinymce-widget/includes/class-admin.php

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