1- <?php
2- /**
3- * Plugin Name: MCE Table Buttons
4- * Plugin URI: http://10up.com/plugins-modules/wordpress-mce-table-buttons/
5- * Description: Add <strong>controls for table editing</strong> to the visual content editor with this <strong>light weight</strong> plug-in.
6- * Version: 3.3
7- * Author: Jake Goldman, 10up, Oomph
8- * Author URI: http://10up.com
9- * License: GPLv2 or later
10- */
11-
12- class MCE_Table_Buttons {
13-
14- /**
15- * Handles initializing this class and returning the singleton instance after it's been cached.
16- *
17- * @return null|MCE_Table_Buttons
18- */
19- public static function get_instance () {
20- // Store the instance locally to avoid private static replication
21- static $ instance = null ;
22-
23- if ( null === $ instance ) {
24- $ instance = new self ();
25- self ::_setup_plugin ();
26- }
27-
28- return $ instance ;
29- }
30-
31- /**
32- * An empty constructor
33- */
34- public function __construct () { /* Purposely do nothing here */ }
35-
36- /**
37- * Handles registering hooks that initialize this plugin.
38- */
39- public static function _setup_plugin () {
40- add_filter ( 'mce_external_plugins ' , array ( __CLASS__ , 'mce_external_plugins ' ) );
41- add_filter ( 'mce_buttons_2 ' , array ( __CLASS__ , 'mce_buttons_2 ' ) );
42- add_filter ( 'content_save_pre ' , array ( __CLASS__ , 'content_save_pre ' ), 20 );
43- add_filter ( 'tiny_mce_before_init ' , array ( __CLASS__ , 'tiny_mce_before_init ' ), 10 , 2 );
44- }
45-
46- /**
47- * Initialize TinyMCE table plugin and custom TinyMCE plugin
48- *
49- * @param array $plugin_array Array of TinyMCE plugins
50- * @return array Array of TinyMCE plugins
51- */
52- public static function mce_external_plugins ( $ plugin_array ) {
53- global $ tinymce_version ;
54- $ variant = ( defined ('SCRIPT_DEBUG ' ) && SCRIPT_DEBUG ) ? '' : '.min ' ;
55-
56- if ( version_compare ( $ tinymce_version , '4700 ' , '< ' ) ) {
57-
58- $ plugin_array ['table ' ] = plugin_dir_url ( __FILE__ ) . 'tinymce41-table/plugin ' . $ variant . '.js ' ;
59-
60- } else {
61-
62- $ plugin_array ['table ' ] = plugin_dir_url ( __FILE__ ) . 'tinymce47-table/plugin ' . $ variant . '.js ' ;
63-
64- }
65-
66- return $ plugin_array ;
67- }
68-
69- /**
70- * Add TinyMCE table control buttons
71- *
72- * @param array $buttons Buttons for the second row
73- * @return array Buttons for the second row
74- */
75- public static function mce_buttons_2 ( $ buttons ) {
76- // in case someone is manipulating other buttons, drop table controls at the end of the row
77- if ( ! $ pos = array_search ( 'undo ' , $ buttons ) ) {
78- array_push ( $ buttons , 'table ' );
79- return $ buttons ;
80- }
81-
82- $ buttons = array_merge ( array_slice ( $ buttons , 0 , $ pos ), array ( 'table ' ), array_slice ( $ buttons , $ pos ) );
83-
84- return $ buttons ;
85- }
86-
87- /**
88- * Fixes weirdness resulting from wpautop and formatting clean up not built for tables
89- *
90- * @param string $content Editor content before WordPress massaging
91- * @return string Editor content before WordPress massaging
92- */
93- public static function content_save_pre ( $ content ) {
94- if ( false !== strpos ( $ content , '<table ' ) ) {
95- // paragraphed content inside of a td requires first paragraph to have extra line breaks (or else autop breaks)
96- $ content = preg_replace ( "/<td([^>]*)>(.+ \r? \n\r? \n)/m " , "<td$1> \n\n$2 " , $ content );
97-
98- // make sure there's space around the table
99- if ( substr ( $ content , -8 ) == '</table> ' ) {
100- $ content .= "\n<br /> " ;
101- }
102- }
103-
104- return $ content ;
105- }
106-
107- /**
108- * Remove the table toolbar introduced in TinyMCE 4.3.0.
109- *
110- * Its positioning does not work correctly inside WordPress and blocks editing.
111- *
112- * @param array $mceInit An array with TinyMCE config.
113- * @param string $editor_id Unique editor identifier, e.g. 'content'.
114- *
115- * @return array TinyMCE config array
116- */
117- public static function tiny_mce_before_init ( $ mceInit , $ editor_id ) {
118- $ mceInit ['table_toolbar ' ] = '' ;
119-
120- return $ mceInit ;
121- }
122- }
123-
124- MCE_Table_Buttons::get_instance ();
1+ <?php
2+ /**
3+ * Plugin Name: MCE Table Buttons
4+ * Plugin URI: http://10up.com/plugins-modules/wordpress-mce-table-buttons/
5+ * Description: Add <strong>controls for table editing</strong> to the visual content editor with this <strong>light weight</strong> plug-in.
6+ * Version: 3.3
7+ * Author: Jake Goldman, 10up, Oomph
8+ * Author URI: http://10up.com
9+ * License: GPLv2 or later
10+ *
11+ * @package MCETableButtons
12+ */
13+
14+ /**
15+ * MCE Table Buttons main class.
16+ */
17+ class MCE_Table_Buttons {
18+ /**
19+ * Handles initializing this class and returning the singleton instance after it's been cached.
20+ *
21+ * @return null|MCE_Table_Buttons
22+ */
23+ public static function get_instance () {
24+ // Store the instance locally to avoid private static replication.
25+ static $ instance = null ;
26+
27+ if ( null === $ instance ) {
28+ $ instance = new self ();
29+ self ::setup_plugin ();
30+ }
31+
32+ return $ instance ;
33+ }
34+
35+ /**
36+ * An empty constructor
37+ */
38+ public function __construct () {
39+ /* Purposely do nothing here. */
40+ }
41+
42+ /**
43+ * Handles registering hooks that initialize this plugin.
44+ */
45+ public static function setup_plugin () {
46+ add_filter ( 'mce_external_plugins ' , array ( __CLASS__ , 'mce_external_plugins ' ) );
47+ add_filter ( 'mce_buttons_2 ' , array ( __CLASS__ , 'mce_buttons_2 ' ) );
48+ add_filter ( 'content_save_pre ' , array ( __CLASS__ , 'content_save_pre ' ), 20 );
49+ add_filter ( 'tiny_mce_before_init ' , array ( __CLASS__ , 'tiny_mce_before_init ' ), 10 , 2 );
50+ }
51+
52+ /**
53+ * Initialize TinyMCE table plugin and custom TinyMCE plugin
54+ *
55+ * @param array $plugin_array Array of TinyMCE plugins.
56+ * @return array Array of TinyMCE plugins
57+ */
58+ public static function mce_external_plugins ( $ plugin_array ) {
59+ global $ tinymce_version ;
60+ $ variant = ( defined ( 'SCRIPT_DEBUG ' ) && SCRIPT_DEBUG ) ? '' : '.min ' ;
61+
62+ if ( version_compare ( $ tinymce_version , '4700 ' , '< ' ) ) {
63+
64+ $ plugin_array ['table ' ] = plugin_dir_url ( __FILE__ ) . 'tinymce41-table/plugin ' . $ variant . '.js ' ;
65+
66+ } else {
67+
68+ $ plugin_array ['table ' ] = plugin_dir_url ( __FILE__ ) . 'tinymce47-table/plugin ' . $ variant . '.js ' ;
69+
70+ }
71+
72+ return $ plugin_array ;
73+ }
74+
75+ /**
76+ * Add TinyMCE table control buttons
77+ *
78+ * @param array $buttons Buttons for the second row.
79+ * @return array Buttons for the second row
80+ */
81+ public static function mce_buttons_2 ( $ buttons ) {
82+ // in case someone is manipulating other buttons, drop table controls at the end of the row.
83+ $ pos = array_search ( 'undo ' , $ buttons , true );
84+ if ( ! $ pos ) {
85+ array_push ( $ buttons , 'table ' );
86+ return $ buttons ;
87+ }
88+
89+ $ buttons = array_merge ( array_slice ( $ buttons , 0 , $ pos ), array ( 'table ' ), array_slice ( $ buttons , $ pos ) );
90+
91+ return $ buttons ;
92+ }
93+
94+ /**
95+ * Fixes weirdness resulting from wpautop and formatting clean up not built for tables
96+ *
97+ * @param string $content Editor content before WordPress massaging.
98+ * @return string Editor content before WordPress massaging
99+ */
100+ public static function content_save_pre ( $ content ) {
101+ if ( false !== strpos ( $ content , '<table ' ) ) {
102+ // paragraphed content inside of a td requires first paragraph to have extra line breaks (or else autop breaks).
103+ $ content = preg_replace ( "/<td([^>]*)>(.+ \r? \n\r? \n)/m " , "<td$1> \n\n$2 " , $ content );
104+
105+ // make sure there's space around the table.
106+ if ( '</table> ' === substr ( $ content , -8 ) ) {
107+ $ content .= "\n<br /> " ;
108+ }
109+ }
110+
111+ return $ content ;
112+ }
113+
114+ /**
115+ * Remove the table toolbar introduced in TinyMCE 4.3.0.
116+ *
117+ * Its positioning does not work correctly inside WordPress and blocks editing.
118+ *
119+ * @param array $mce_init An array with TinyMCE config.
120+ * @param string $editor_id Unique editor identifier, e.g. 'content'.
121+ *
122+ * @return array TinyMCE config array
123+ */
124+ public static function tiny_mce_before_init ( $ mce_init , $ editor_id ) {
125+ $ mce_init ['table_toolbar ' ] = '' ;
126+
127+ return $ mce_init ;
128+ }
129+ }
130+
131+ MCE_Table_Buttons::get_instance ();
0 commit comments