-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjck-easy-custom-styles.php
414 lines (290 loc) · 15.7 KB
/
jck-easy-custom-styles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
/*
Plugin Name: Easy Custom Styles
Plugin URI: http://www.jckemp.com
Description: Easily add any number of custom text styles to the WYSIWYG editor.
Version: 1.0.2
Author: James Kemp
Author URI: http://www.jckemp.com
License: GPL2
Copyright 2014, James Kemp
*/
class JCK_Custom_Styles {
/**
* Constructor
*/
public function __construct() {
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'add_stylesheets' ) );
add_action( 'admin_print_scripts-post-new.php', array( __CLASS__, 'add_admin_scripts' ), 10, 1 );
add_action('admin_print_scripts-post.php', array( __CLASS__, 'add_admin_scripts' ), 10, 1 );
add_action('add_meta_boxes', array( $this, 'metaboxes' ) );
add_action('init', array( $this, 'post_type' ) );
add_filter('mce_css', array( $this, 'plugin_mce_css' ) );
add_action('wp_enqueue_scripts', array( $this, 'add_dynamic_stylesheet' ) );
add_action('template_redirect', array( $this, 'trigger_check' ) );
add_filter('query_vars', array( $this, 'add_trigger' ) );
add_filter('tiny_mce_before_init', array( $this, 'mce_before_init' ), 1000);
add_filter('mce_buttons_2', array( $this, 'mce_buttons' ), 1000);
add_action('save_post', array( $this, 'save_custom_styles_meta' ) );
add_filter('post_updated_messages', array( $this, 'message' ) );
add_filter('post_row_actions', array( $this, 'remove_quick_edit' ), 10, 2);
}
public static function getValue($key, $default = '', $id = '') {
if ($id == '') {
global $post;
$jck_custom_styles = get_post_meta($post->ID, 'jck_custom_styles', true);
} else {
$jck_custom_styles = get_post_meta($id, 'jck_custom_styles', true);
}
if (isset($jck_custom_styles[$key])) {
return $jck_custom_styles[$key];
} else {
return $default;
}
}
public static function add_stylesheets() {
global $post_type;
if( 'custom-styles' !== $post_type )
return;
wp_register_style('colorpicker_css', plugins_url('/assets/colorpicker/css/colorpicker.css', __FILE__));
wp_enqueue_style('colorpicker_css');
wp_register_style('jck_cs_styles', plugins_url('/assets/styles.css', __FILE__));
wp_enqueue_style('jck_cs_styles');
}
// Enque admin scripts
public static function add_admin_scripts() {
global $post_type;
if( 'custom-styles' !== $post_type )
return;
wp_enqueue_script('colorpicker_script', plugins_url('/assets/colorpicker/js/colorpicker.js', __FILE__), 'jquery');
wp_enqueue_script('scroll_script', plugins_url('/assets/scroll.js', __FILE__), 'colorpicker_script');
wp_enqueue_script('custom_style_editor_scripts', plugins_url('/assets/scripts.js', __FILE__), 'scroll_script');
}
public static function get_styles() {
global $wpdb;
$jck_posts = $wpdb->get_results("
SELECT *
FROM $wpdb->posts
WHERE $wpdb->posts.post_type = 'custom-styles'
AND $wpdb->posts.post_status = 'publish'
ORDER BY ID DESC
");
return $jck_posts;
}
/* Custom CSS styles on WYSIWYG Editor – Start
======================================= */
public function mce_buttons($buttons) {
$jck_posts = self::get_styles();
if (!in_array('styleselect', $buttons) && $jck_posts) {
array_unshift($buttons, 'styleselect');
}
return $buttons;
}
// Add Styles to mce
public function mce_before_init($settings) {
// Get existing styles
if (isset($settings['style_formats'])) {
$existing_styles = json_decode($settings['style_formats']);
} else {
$existing_styles = array();
}
$jck_posts = self::get_styles(); // Get All Custom Styles
if ( $jck_posts ) {
foreach ( $jck_posts as $jck_post ) {
//$jck_custom_styles = get_post_meta($jck_post->ID, 'jck_custom_styles', true);
$custom_style_settings = array(
'title' => $jck_post->post_title,
'attributes' => array(
'class' => $jck_post->post_name
),
'wrapper' => false,
'name' => $jck_post->post_name
);
if (self::getValue('inline', '', $jck_post->ID) == 'inline') {
$custom_style_settings['inline'] = 'span';
} //self::getValue('inline') == 'inline'
else {
$custom_style_settings['block'] = 'p';
}
$style_formats[] = $custom_style_settings;
}
// Merge existing styles with new ones.
$merge = array_merge($style_formats, $existing_styles);
// Output merged styles as javascript
$settings['style_formats'] = json_encode($merge);
} // End if posts
return $settings;
}
// Add dynamic CSS
public function add_trigger($vars) {
$vars[] = 'custom_styles_trigger';
return $vars;
}
// Get the slug function
public function the_slug() {
$post_data = get_post($post->ID, ARRAY_A);
$slug = $post_data['post_name'];
return $slug;
}
public function trigger_check() {
if (intval(get_query_var('custom_styles_trigger')) == 1) {
header("Content-type: text/css");
$jck_posts = self::get_styles(); // Get All Custom Styles
if ( $jck_posts ) {
foreach ( $jck_posts as $jck_post ) {
$current_styles = $this->compile_styles(get_post_meta($jck_post->ID, 'jck_custom_styles', true), $jck_post->ID);
$slug = $jck_post->post_name;
echo '.' . $slug . ' {' . $current_styles . '}' . "\n";
}
} // End if posts
exit;
} //intval(get_query_var('custom_styles_trigger')) == 1
}
// Add Stylesheet to frontend
public function add_dynamic_stylesheet() {
$mce_css = get_bloginfo('url') . '?custom_styles_trigger=1';
wp_register_style('custom_styles_css', $mce_css);
wp_enqueue_style('custom_styles_css');
}
// Add Stylesheet to editor
public function plugin_mce_css($mce_css) {
if (!empty($mce_css))
$mce_css .= ',';
$mce_css .= get_bloginfo('url') . '?custom_styles_trigger=1';
return $mce_css;
}
/* =====
Add Custom Post Type
===== */
public function post_type() {
$labels = array(
'name' => _x('Custom Styles', 'post type general name'),
'singular_name' => _x('Custom Style', 'post type singular name'),
'add_new' => _x('Add New', 'Custom Style'),
'add_new_item' => __('Add New Custom Style'),
'edit_item' => __('Edit Custom Style'),
'new_item' => __('New Custom Style'),
'view_item' => __('View Custom Stylea'),
'search_items' => __('Search Custom Stylea'),
'not_found' => __('No Custom Styles found'),
'not_found_in_trash' => __('No Custom Styles found in Trash'),
'parent_item_colon' => ''
);
register_post_type('custom-styles', array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_menu' => 'options-general.php',
'supports' => array(
'title'
)
));
}
// Add meta box
public function metaboxes() {
add_meta_box('style_selector', 'Style Selector', array(
&$this,
'custom_styles_meta'
), 'custom-styles', 'normal', 'high');
}
public function custom_styles_meta() {
global $post;
$current_styles = $this->compile_styles(get_post_meta($post->ID, 'jck_custom_styles', true));
wp_nonce_field('custom_styles_nonce', 'meta_box_nonce');
include 'inc/admin-editor.php';
}
public function save_custom_styles_meta($post_id) {
// Bail if we're doing an auto save
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
// if our nonce isn't there, or we can't verify it, bail
if (!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], 'custom_styles_nonce'))
return;
// if our current user can't edit this post, bail
if (!current_user_can('edit_posts'))
return;
// now we can actually save the data
// Make sure your data is set before trying to save it
if (isset($_POST['jck_custom_styles']))
update_post_meta($post_id, 'jck_custom_styles', $_POST['jck_custom_styles']);
}
// =======================
// Compile Saved Styles
public function compile_styles($jck_custom_styles, $id = '') {
$current_styles = '';
if (self::getValue('fontFamily', '', $id) != "inherit") {
$current_styles .= (self::getValue('fontFamily', '', $id) != '') ? 'font-family:' . self::getValue('fontFamily', '', $id) . '; ' : '';
}
$current_styles .= (self::getValue('fontSize', '', $id) != '') ? 'font-size:' . self::getValue('fontSize', '', $id) . self::getValue('fontSizeMeas', '', $id) . '; ' : '';
$current_styles .= (self::getValue('color', '', $id) != '') ? 'color:#' . self::getValue('color', '', $id) . '; ' : '';
$current_styles .= (self::getValue('fontWeight', '', $id) != '') ? 'font-weight:' . self::getValue('fontWeight', '', $id) . '; ' : '';
$current_styles .= (self::getValue('fontStyle', '', $id) != '') ? 'font-style:' . self::getValue('fontStyle', '', $id) . '; ' : '';
$current_styles .= (self::getValue('textDecoration', '', $id) != '') ? 'text-decoration:' . self::getValue('textDecoration', '', $id) . '; ' : '';
$current_styles .= (self::getValue('textTransform', '', $id) != '') ? 'text-transform:' . self::getValue('textTransform', '', $id) . '; ' : '';
$current_styles .= (self::getValue('textAlign', '', $id) != '') ? 'text-align:' . self::getValue('textAlign', '', $id) . '; ' : '';
$current_styles .= (self::getValue('letterSpacing', '', $id) != '') ? 'letter-spacing:' . self::getValue('letterSpacing', '', $id) . 'px; ' : '';
$current_styles .= (self::getValue('wordSpacing', '', $id) != '') ? 'word-spacing:' . self::getValue('wordSpacing', '', $id) . 'px; ' : '';
$current_styles .= (self::getValue('lineHeight', '', $id) != '') ? 'line-height:' . self::getValue('lineHeight', '', $id) . self::getValue('lineHeightMeas', '', $id) . '; ' : '';
$current_styles .= (self::getValue('backgroundColor', '', $id) != '') ? 'background-color:#' . self::getValue('backgroundColor', '', $id) . '; ' : '';
if (self::getValue('margin_ind', '', $id) == 'margin_ind') {
$current_styles .= (self::getValue('margin-top', '', $id) != '') ? 'margin-top:' . self::getValue('margin-top', '', $id) . 'px; ' : '';
$current_styles .= (self::getValue('margin-right', '', $id) != '') ? 'margin-right:' . self::getValue('margin-right', '', $id) . 'px; ' : '';
$current_styles .= (self::getValue('margin-bottom', '', $id) != '') ? 'margin-bottom:' . self::getValue('margin-bottom', '', $id) . 'px; ' : '';
$current_styles .= (self::getValue('margin-left', '', $id) != '') ? 'margin-left:' . self::getValue('margin-left', '', $id) . 'px; ' : '';
} //self::getValue('margin_ind','',$id) == 'margin_ind'
else {
$current_styles .= (self::getValue('margin', '', $id) != '') ? 'margin:' . self::getValue('margin', '', $id) . 'px; ' : '';
}
if (self::getValue('padding_ind', '', $id) == 'padding_ind') {
$current_styles .= (self::getValue('padding-top', '', $id) != '') ? 'padding-top:' . self::getValue('padding-top', '', $id) . 'px; ' : '';
$current_styles .= (self::getValue('padding-right', '', $id) != '') ? 'padding-right:' . self::getValue('padding-right', '', $id) . 'px; ' : '';
$current_styles .= (self::getValue('padding-bottom', '', $id) != '') ? 'padding-bottom:' . self::getValue('padding-bottom', '', $id) . 'px; ' : '';
$current_styles .= (self::getValue('padding-left', '', $id) != '') ? 'padding-left:' . self::getValue('padding-left', '', $id) . 'px; ' : '';
} //self::getValue('padding_ind','',$id) == 'padding_ind'
else {
$current_styles .= (self::getValue('padding', '', $id) != '') ? 'padding:' . self::getValue('padding', '', $id) . 'px; ' : '';
}
$current_styles .= (self::getValue('borderStyle', '', $id) != '') ? 'border-style:' . self::getValue('borderStyle', '', $id) . '; ' : '';
$current_styles .= (self::getValue('borderColor', '', $id) != '') ? 'border-color:#' . self::getValue('borderColor', '', $id) . '; ' : '';
if (self::getValue('border_ind', '', $id) == 'border_ind') {
$current_styles .= (self::getValue('border-top-width', '', $id) != '') ? 'border-top-width:' . self::getValue('border-top-width', '', $id) . 'px; ' : 'border-top-width:0px; ';
$current_styles .= (self::getValue('border-right-width', '', $id) != '') ? 'border-right-width:' . self::getValue('border-right-width', '', $id) . 'px; ' : 'border-right-width:0px; ';
$current_styles .= (self::getValue('border-bottom-width', '', $id) != '') ? 'border-bottom-width:' . self::getValue('border-bottom-width', '', $id) . 'px; ' : 'border-bottom-width:0px; ';
$current_styles .= (self::getValue('border-left-width', '', $id) != '') ? 'border-left-width:' . self::getValue('border-left-width', '', $id) . 'px; ' : 'border-left-width:0px; ';
} //self::getValue('border_ind','',$id) == 'border_ind'
else {
$current_styles .= (self::getValue('border-width', '', $id) != '') ? 'border-width:' . self::getValue('border-width', '', $id) . 'px; ' : 'border-width:0px; ';
}
return $current_styles;
}
public function message($messages) {
global $post;
$post_ID = $post->ID;
$messages['custom-styles'] = array(
0 => '', // Unused. Messages start at index 1.
1 => sprintf(__('Custom Style updated.')),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Custom Style updated.'),
/* translators: %s: date and time of the revision */
5 => isset($_GET['revision']) ? sprintf(__('Custom Style restored to revision from %s'), wp_post_revision_title((int) $_GET['revision'], false)) : false,
6 => sprintf(__('Custom Style created.')),
7 => __('Custom Style saved.'),
8 => sprintf(__('Custom Style submitted. <a target="_blank" href="%s">Preview post</a>'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID)))),
9 => sprintf(__('Custom Style scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview post</a>'),
// translators: Publish box date format, see http://php.net/date
date_i18n(__('M j, Y @ G:i'), strtotime($post->post_date)), esc_url(get_permalink($post_ID))),
10 => sprintf(__('Custom Style draft updated.'))
);
return $messages;
}
// Remove Quickedit
public function remove_quick_edit( $actions ) {
global $post;
if ( $post->post_type == 'custom-styles' ) {
unset($actions['inline hide-if-no-js']);
}
return $actions;
}
} // End jck_custom_styles Class
$jck_custom_styles = new JCK_Custom_Styles(); // Start an instance of the plugin class