PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

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