forked from miya0001/tinymce-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinymce-templates.php
639 lines (577 loc) · 17.5 KB
/
tinymce-templates.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<?php
/*
Plugin Name: TinyMCE Templates
Plugin URI: http://miya0001.github.io/tinymce-templates/
Description: TinyMCE Templates plugin will enable to use HTML template on WordPress Visual Editor.
Author: Takayuki Miyauchi
Version: 4.3.5
Author URI: http://miya0001.github.io/tinymce-templates/
Domain Path: /languages
Text Domain: tinymce_templates
*/
/*
Copyright (c) 2010 Takayuki Miyauchi (THETA NETWORKS Co,.Ltd).
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
$tinymce_templates = new TinyMCE_Templates();
$tinymce_templates->register();
class TinyMCE_Templates
{
private $post_type = 'tinymcetemplates';
private $base_url;
private $translators = array(
'Takayuki Miyauchi' => array(
'lang' => 'Japanese',
'url' => 'http://wpist.me/',
),
'Andrea Bersi' => array(
'lang' => 'Italian',
'url' => 'http://www.andreabersi.com/',
),
'Tobias Bergius' => array(
'lang' => 'Swedish',
'url' => '',
),
'Martin Lettner' => array(
'lang' => 'German',
'url' => 'http://www.martinlettner.info/',
),
'David Bravo' => array(
'lang' => 'Spanish',
'url' => 'http://www.dimensionmultimedia.com/',
),
'Frank Groeneveld' => array(
'lang' => 'Dutch',
'url' => 'http://ivaldi.nl/',
),
'HAROUY Jean-Michel' => array(
'lang' => 'French',
'url' => 'http://www.laposte.net/',
),
'Rafael Funchal' => array(
'lang' => 'Brazilian Portuguese',
'url' => 'http://www.rafaelfunchal.com.br/',
),
'Morten Elm' => array(
'lang' => 'Filipino',
'url' => 'http://www.storbyfan.dk/',
),
'ihtimir' => array(
'lang' => 'Russian',
'url' => '',
),
'Branco' => array(
'lang' => 'Slovak',
'url' => 'http://webhostinggeeks.com/user-reviews/',
),
'Ahrale' => array(
'lang' => 'he_IL',
'url' => 'http://atar4u.com/',
),
'Riant' => array(
'lang' => 'zh_CN',
'url' => 'http://www.notidea.com/'
),
);
/**
* Initializing the plugin.
*
* @param none
* @return none
*/
public function register()
{
$this->base_url = plugins_url( dirname( plugin_basename( __FILE__ ) ) );
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
}
/**
* Fires on plugins_loaded hook.
*
* @param none
* @return none
*/
public function plugins_loaded()
{
load_plugin_textdomain(
'tinymce_templates',
false,
dirname( plugin_basename( __FILE__ ) ).'/languages'
);
$this->register_post_type();
add_filter( 'post_row_actions', array( $this, 'row_actions' ), 10, 2 );
add_filter( 'page_row_actions', array( $this, 'row_actions' ), 10, 2 );
add_action( 'admin_head-post-new.php', array( $this, 'admin_head' ) );
add_action( 'admin_head-post.php', array( $this, 'admin_head' ) );
add_action( 'admin_footer-post-new.php', array( $this, 'admin_footer' ) );
add_action( 'admin_footer-post.php', array( $this, 'admin_footer' ) );
add_action( 'wp_ajax_tinymce_templates', array( $this, 'wp_ajax_tinymce_templates' ) );
add_action( 'post_submitbox_start', array( $this, 'post_submitbox_start' ) );
add_action( 'wp_before_admin_bar_render', array( $this, 'wp_before_admin_bar_render' ) );
add_action( 'save_post', array( $this, 'save_post' ) );
add_action( 'media_buttons', array( $this, 'media_buttons' ), 11 );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_filter( 'tinymce_templates_content', 'wptexturize' );
add_filter( 'tinymce_templates_content', 'convert_smilies' );
add_filter( 'tinymce_templates_content', 'convert_chars' );
add_filter( 'tinymce_templates_content', 'wpautop' );
add_filter( 'tinymce_templates_content', 'shortcode_unautop' );
add_filter( 'tinymce_templates_content', 'prepend_attachment' );
add_filter( 'tinymce_templates_content', 'do_shortcode', 11 );
add_filter( 'tinymce_templates_content', array( $GLOBALS['wp_embed'], 'run_shortcode' ), 8 );
add_filter( 'tinymce_templates_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );
add_filter( 'tinymce_templates_preview', 'wptexturize' );
add_filter( 'tinymce_templates_preview', 'convert_smilies' );
add_filter( 'tinymce_templates_preview', 'convert_chars' );
add_filter( 'tinymce_templates_preview', 'wpautop' );
add_filter( 'tinymce_templates_preview', 'shortcode_unautop' );
add_filter( 'tinymce_templates_preview', 'prepend_attachment' );
add_filter( 'tinymce_templates_preview', 'do_shortcode', 11 );
// add_filter( 'tinymce_templates_preview', array( $GLOBALS['wp_embed'], 'run_shortcode' ), 8 );
// add_filter( 'tinymce_templates_preview', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );
add_shortcode( 'template', array( $this, 'template_shortcode' ) );
}
/**
* Fires on admin_enqueue_scripts hook
*
* @param none
* @return none
*/
public function admin_enqueue_scripts( $hook_suffix )
{
if ( 'post-new.php' === $hook_suffix || 'post.php' === $hook_suffix ) {
wp_enqueue_script(
'tinymce-templates',
plugins_url( 'js/tinymce-templates.js', __FILE__ ),
array( 'jquery' ),
filemtime( dirname( __FILE__ ) . '/js/tinymce-templates.js' ),
true
);
wp_enqueue_style(
'tinymce-templates',
plugins_url( 'css/tinymce-templates.css', __FILE__ ),
array(),
filemtime( dirname( __FILE__ ) . '/css/tinymce-templates.css' )
);
}
}
/**
* Fires on media_buttons hook
*
* @param none
* @return none
*/
public function media_buttons( $editor_id = 'content' )
{
if ( 'content' === $editor_id ) {
$button_html = '<a id="%s" class="%s" href="#" data-editor="%s" title="%s">';
$button_html .= '<span class="%s" style="%s"></span> %s';
$button_html .= '</a>';
printf(
$button_html,
'button-tinymce-templates',
'button',
esc_attr( $editor_id ),
esc_attr( __( 'Insert Template', 'tinymce_templates' ) ),
'dashicons dashicons-edit',
'margin-top: 3px;',
esc_html( __( 'Insert Template', 'tinymce_templates' ) )
);
}
}
/**
* Shortcode for templates.
*
* @param array $p Shortcode parameters.
* @return none Shortcode output.
*/
public function template_shortcode( $p, $content )
{
$post_content = '';
if ( isset( $p['id'] ) && intval( $p['id'] ) ) {
$args = array(
'ID' => $p['id'],
'post_status' => 'publish',
'post_type' => 'tinymcetemplates',
);
$post = get_post( $p['id'] );
if ( is_a( $post, 'WP_Post' ) ) {
if ( get_post_meta( $p['id'], 'insert_as_shortcode', true ) ) {
$post_content = $post->post_content;
}
}
}
return apply_filters( 'tinymce_templates_content', $post_content, $p, $content );
}
/**
* Fires on wp_before_admin_bar_render hook.
*
* @param none
* @return none
*/
public function wp_before_admin_bar_render() {
global $wp_admin_bar;
if ( is_single() || is_page() ) {
/*
* Adding menu to the admin bar.
*/
$wp_admin_bar->add_menu( array(
'parent' => 'edit',
'id' => 'new_template',
'title' => __( 'Copy to a new template', 'tinymce_templates' ),
'href' => $this->get_copy_template_url( get_the_ID() )
) );
}
}
/**
* Filters the pages/posts list menu in admin.
*
* @param array $actions Menu items of the pages/posts list.
* @param object $post Current post object.
* @return array Menu items.
*/
public function row_actions( $actions, $post )
{
$actions['copy_to_template'] = sprintf(
'<a href="%s">%s</a>',
$this->get_copy_template_url( $post->ID ),
__( 'Copy to a new template', 'tinymce_templates' )
);
return $actions;
}
/**
* Adding copy to temsplate link to post submit box.
*
* @param none
* @return none
*/
public function post_submitbox_start()
{
if ( isset( $_GET['post'] ) && intval( $_GET['post'] ) ) {
?>
<div id="duplicate-action">
<a class="submitduplicate duplication"
href="<?php echo esc_url( $this->get_copy_template_url( $_GET['post'] ) ) ?>"><?php _e( 'Copy to a new template', 'tinymce_templates' ); ?></a>
</div>
<?php
}
}
/**
* Fires on admin_head-post.php or admin_head-post-new.php hook.
*
* @param none
* @return none
*/
public function admin_head()
{
/**
* Hide some stuff in the templates editor panel.
*/
if ( get_post_type() === $this->post_type ) {
remove_meta_box( 'slugdiv', $this->post_type, 'normal' );
echo '<style>#visibility{display:none;} #message a{display: none;}</style>';
/**
* Add editor style to the editor.
*/
$ver = filemtime( dirname( __FILE__ ) . '/css/editor-style.css' );
$editor_style = plugins_url( 'css/editor-style.css?ver=' . $ver, __FILE__ );
add_editor_style( $editor_style );
}
global $content_width;
if ( isset( $content_width ) && intval( $content_width ) ) {
/**
* I want to set same width to preview with $content_width
*/
echo '<style type="text/css">';
$preview_width = $content_width + 40; // should be same with padding * 2
echo '#tinymce-templates-preview{ max-width: '.$preview_width.'px; }';
$wrap_width = $content_width + 80; // should be same with padding * 4
echo '#tinymce-templates-wrap{ max-width: '.$wrap_width.'px; }';
echo '</style>';
}
}
/**
* Register custom post type.
*
* @param none
* @return none
*/
private function register_post_type()
{
$args = array(
'label' => __( 'Templates', 'tinymce_templates' ),
'labels' => array(
'singular_name' => __( 'Templates', 'tinymce_templates' ),
'add_new_item' => __( 'Add New Template', 'tinymce_templates' ),
'edit_item' => __( 'Edit Template', 'tinymce_templates' ),
'add_new' => __( 'Add New', 'tinymce_templates' ),
'new_item' => __( 'New Template', 'tinymce_templates' ),
'view_item' => __( 'View Template', 'tinymce_templates' ),
'not_found' => __( 'No templatess found.', 'tinymce_templates' ),
'not_found_in_trash' => __(
'No templates found in Trash.',
'tinymce_templates'
),
'search_items' => __( 'Search Templates', 'tinymce_templates' ),
),
'public' => false,
'menu_icon' => 'dashicons-edit',
'publicly_queryable' => false,
'exclude_from_search' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 100,
'rewrite' => false,
'show_in_nav_menus' => false,
'register_meta_box_cb' => array( $this, 'add_meta_box' ),
'supports' => array(
'title',
'editor',
'revisions',
'author',
)
);
register_post_type( $this->post_type, $args );
}
/**
* Adding meta box callback function.
*
* @param none
* @return none
*/
public function add_meta_box()
{
add_meta_box(
'tinymce_templates-is-shortcode',
__( 'Insert as Shortcode', 'tinymce_templates' ),
array( $this, 'insert_as_shortcode_meta_box' ),
$this->post_type,
'side',
'low'
);
add_meta_box(
'tinymce_templates-translators',
__( 'Translators', 'tinymce_templates' ),
array( $this, 'translators_meta_box' ),
$this->post_type,
'side',
'low'
);
}
/**
* Adding meta box `Insert as shortcode`.
*
* @param none
* @return none
*/
public function insert_as_shortcode_meta_box( $post, $box )
{
$res = get_post_meta( $post->ID, 'insert_as_shortcode', true );
if ( $res ) {
echo '<label><input type="radio" name="is_shortcode" value="1" checked> '.__( 'Yes' ).'</label><br />';
echo '<label><input type="radio" name="is_shortcode" value="0"> '.__( 'No' ).'</label>';
} else {
echo '<label><input type="radio" name="is_shortcode" value="1"> '.__( 'Yes' ).'</label><br />';
echo '<label><input type="radio" name="is_shortcode" value="0" checked> '.__( 'No' ).'</label>';
}
}
/**
* Adding meta box `Translators`.
*
* @param none
* @return none
*/
public function translators_meta_box( $post, $box )
{
echo '<ul>';
foreach ( $this->translators as $u => $p ) {
if ( $p['url'] ) {
printf(
'<li><a href="%s">%s</a> ( %s )</li>',
esc_attr( $p['url'] ),
esc_html( $u ),
esc_html( $p['lang'] )
);
} else {
printf(
'<li>%s ( %s )</li>',
esc_html( $u ),
esc_html( $p['lang'] )
);
}
}
echo '</ul>';
}
/**
* Saving post meta to template post type.
*
* @param int $id The ID of the post.
* @return none
*/
public function save_post( $id )
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $id;
}
if ( isset( $_POST['action'] ) && $_POST['action'] == 'inline-save' ) {
return $id;
}
$p = get_post( $id );
/**
* Save post_meta
*/
if ( $p->post_type === $this->post_type ) {
if ( isset( $_POST['is_shortcode'] ) && $_POST['is_shortcode'] ) {
update_post_meta( $id, 'insert_as_shortcode', true );
} else {
delete_post_meta( $id, 'insert_as_shortcode' );
}
}
}
/**
* Generate javascript for the copying to the template.
*
* @param none
* @return none
*/
public function admin_footer()
{
global $hook_suffix;
if ( 'post-new.php' === $hook_suffix ) {
if ( get_post_type() === $this->post_type ) {
if ( isset( $_GET['origin'] ) && intval( $_GET['origin'] ) ) {
$origin = get_post( intval( $_GET['origin'] ) );
if ( $origin ) {
$template = array(
'post_title' => $origin->post_title,
'post_content' => wpautop( $origin->post_content ),
);
?>
<script type="text/javascript">
var origin = <?php echo json_encode( $template ); ?>;
jQuery( '#title').val(origin.post_title );
jQuery( '#content').val(origin.post_content );
</script>
<?php
}
}
}
}
?>
<div id="tinymce-templates-backdrop" style="desplay: none;"></div>
<div id="tinymce-templates-wrap" class="wp-core-ui search-panel-visible" style="desplay: none;">
<div class="modal">
<div class="header">
<h1><span class="dashicons dashicons-edit"></span> <?php _e( 'Insert Template', 'tinymce_templates' ); ?></h1>
<a href="#" class="close"><span class="dashicons dashicons-no-alt"></span></a>
</div>
<div class="container">
<select id="tinymce-templates-list"></select>
<iframe id="tinymce-templates-preview"></iframe>
</div>
<div class="footer">
<div id="tinymce-templates-message"><?php _e( 'Note: The template will be inserted as shortcode.', 'tinymce_templates' ); ?></div>
<a href="#" id="tinymce-templates-insert" class="button button-primary button-large template-button-insert" disabled><?php _e( 'Insert Template', 'tinymce_templates' ); ?></a>
</div>
</div>
</div>
<?php
$url = admin_url( 'admin-ajax.php' );
$nonce = wp_create_nonce( 'tinymce_templates' );
$args = array(
'action' => 'tinymce_templates',
'nonce' => $nonce,
);
?>
<script type="text/javascript">
var tinymce_templates_list_uri = '<?php echo $url; ?>';
var tinymce_templates_list_args = <?php echo json_encode($args); ?>;
</script>
<?php
}
/**
* Output json of the templates.
*
* @param none
* @return none
*/
public function wp_ajax_tinymce_templates()
{
nocache_headers();
if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( $_GET['nonce'], 'tinymce_templates' ) ) {
return;
}
header( 'Content-Type: application/javascript; charset=UTF-8' );
$templates = $this->get_templates();
echo json_encode( $templates );
exit;
}
public function get_templates()
{
$p = array(
'post_status' => 'publish',
'post_type' => $this->post_type,
'orderby' => 'date',
'order' => 'DESC',
'numberposts' => -1,
);
$posts = get_posts( $p );
$templates = array();
foreach ( $posts as $p ) {
$ID = intval( $p->ID );
$name = esc_html( apply_filters( 'tinymce_template_title', $p->post_title ) );
$desc = esc_html( apply_filters( 'tinymce_template_excerpt', $p->post_excerpt ) );
$templates[ $ID ] = array(
'title' => $name,
'is_shortcode' => get_post_meta( $ID, 'insert_as_shortcode', true ),
'content' => $p->post_content,
);
}
$templates = apply_filters( 'tinymce_templates_post_objects', $templates );
if ( isset( $_GET['template_id'] ) && $_GET['template_id'] ) {
if ( isset( $templates[ $_GET['template_id'] ] ) && $templates[ $_GET['template_id'] ] ) {
$p = $templates[ $_GET['template_id'] ];
$content = apply_filters(
'tinymce_templates',
$p['content'],
$p['content']
);
$preview = apply_filters(
'tinymce_templates_preview',
$p['content']
);
return array(
'content' => wpautop( $content ),
'preview' => $preview,
'is_shortcode' => $p['is_shortcode'],
);
}
}
return $templates;
}
/**
* Returns the url for copying templates.
*
* @param none
* @return none
*/
private function get_copy_template_url( $id )
{
return admin_url( 'post-new.php?post_type=tinymcetemplates&origin='.intval( $id ) );
}
} // end class tinymceTemplates
// eof