1 <?php
2
3 /**
4 * Convenience functions for TinyMCE enhancements in the WordPress /wp-admin
5 * area by the Vanilla framework.
6 *
7 * @author Nevma, http://www.nevma.gr, info@nevma.gr
8 *
9 * @license http://www.gnu.org/licenses/gpl-3.0.en.html GPLv3
10 */
11
12
13
14 /**
15 * Sets actions and filters necessary for the Vanilla TinyMCE .
16 *
17 * @return void
18 */
19
20 function vanilla_tinymce_add_action () {
21
22 add_action( 'admin_head', 'vanilla_tinymce_general_css' );
23
24 }
25
26 add_action( 'admin_init', 'vanilla_tinymce_add_action' );
27
28
29
30 /**
31 * Adds the general CSS styles for the Vanilla TinyMCE plugins.
32 *
33 * @return void
34 */
35
36 function vanilla_tinymce_general_css () {
37
38 wp_enqueue_style( 'vanilla-tinymce', get_template_directory_uri() . '/inc/vanilla/tinymce/css/vanilla-tinymce-admin.css', false );
39
40 }
41
42
43
44 /**
45 * Adds default stylings from "vanilla/tinymce/vanilla-tinymce-defaults.css"
46 * to the TinyMCE editor and imports theme specific styles as well from
47 * "css/style.tinymce.css".
48 *
49 * @return void
50 */
51
52 function vanilla_tinymce_add_editor_styles () {
53
54 // Vanilla default TinyMCE content stylings.
55
56 add_editor_style( 'inc/vanilla/tinymce/css/vanilla-tinymce-editor.css' );
57
58 // Theme-specific TinyMCE content stylings.
59
60 add_editor_style( 'css/style.tinymce.css' );
61
62 }
63
64 add_action( 'admin_init', 'vanilla_tinymce_add_editor_styles' );
65
66
67
68 /**
69 * Adds the "shortcode_unautop" function filter after the "the_content"
70 * to ensure that the extraneous opening and closing paragraphs, which are
71 * added around shortcodes in tinyMCE, are removed.
72 *
73 * @return void
74 */
75
76 function vanilla_tinymce_remove_wpautop_filters () {
77
78 remove_filter( 'the_content', 'wpautop' );
79 add_filter( 'the_content', 'wpautop' , 999 );
80 add_filter( 'the_content', 'shortcode_unautop', 1000 );
81
82 }
83
84 add_filter( 'theme_init', 'vanilla_tinymce_remove_wpautop_filters' );
85
86
87
88 /**
89 * Takes a string which is meant to have been produced by TinyMCE and
90 * possibly contains shortcodes and removes any extraneous opening and
91 * closing paragrpahs and/or breaking lines from its beginning or end.
92 *
93 * @param string $content The input string to clean.
94 *
95 * @return string The input string cleaned.
96 */
97
98 function vanilla_tinymce_wpautopbr ( $content ) {
99
100 $regexp =
101 '/'.
102 '\A' . '\s*<\/p>\s*' . '|' . // Closing paragraph in the beginning.
103 '\A' . '\s*<br\s*\/>\s*' . '|' . // Line break in the beginning.
104 '\s*<p>\s*' . '\z' . '|' . // Closing paragraph in the end.
105 '\s*<br\s*\/>\s*' . '\z' . // Line break in the end.
106 '/im';
107
108 $content = preg_replace( $regexp, '', $content );
109
110 return $content;
111
112 }
113
114
115
116 /**
117 * Takes a string which is meant to have been produced by TinyMCE after the
118 * shortcodes have been applied and replaces P elements that erroneously
119 * enclose DIV elements. For instance, TinyMCE will wrap a [gallery...]
120 * shortcode inside a P element and then the resulting DIV of the gallery
121 * will be found inside that P, which is just not very nice.
122 *
123 * @param string $content The input string to fix.
124 *
125 * @return string The input string fixed.
126 */
127
128 function vanilla_tinymce_fixpdiv ( $content ) {
129
130 // Take a <p><div> and replace it with a <div>.
131
132 $regexp1 = '/\A\s*<p>\s*<div/im';
133 $content = preg_replace( $regexp1, '<div', $content );
134
135 // Take a </div></p> and replace it with a </div>.
136
137 $regexp2 = '/\s*<\/div>\s*<\/p>\s*\z/im';
138 $content = preg_replace( $regexp2, '</div>', $content );
139
140 return $content;
141
142 }
143
144 ?>