-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtwd-mce-button.php
187 lines (164 loc) · 4.32 KB
/
twd-mce-button.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
<?php
/*
Plugin Name: TWD TinyMCE Example
Plugin URI: https://www.thewebsitedev.com/
Description: An example plugin to load dynamic content in tinymce popup.
Version: 0.3
Author: Gautam Thapar
Author URI: https://www.thewebsitedev.com/
License: GPLv2 or later
Text Domain: twd
Domain Path: /languages
*/
// Make sure we don't expose any info if called directly
if ( !defined( 'ABSPATH' ) ){
exit;
}
define( 'TWD__VERSION', '0.3' );
define( 'TWD__PLUGIN_URL', plugin_dir_url( __FILE__ ) );
/**
* Register post type to use in example
* @return
*/
function codex_custom_init() {
$args = array(
'public' => true,
'label' => 'Books'
);
register_post_type( 'book', $args );
}
add_action( 'init', 'codex_custom_init' );
function book_title( $atts ) {
$atts = shortcode_atts( array(
'id' => ''
), $atts, 'twd_post_title' );
$title = get_the_title($atts['id']);
return $title;
}
add_shortcode( 'twd_post_title', 'book_title' );
if( !class_exists( 'TWDTinymceExample' ) ) {
class TWDTinymceExample {
private static $instance;
/**
* Initiator
* @since 0.1
*/
public static function init() {
return self::$instance;
}
/**
* Constructor
* @since 0.1
*/
public function __construct() {
add_action( 'wp_ajax_cpt_list', array( $this, 'list_ajax' ) );
add_action( 'admin_footer', array( $this, 'cpt_list' ) );
add_action( 'admin_head', array( $this, 'mce_button' ) );
}
// Hooks your functions into the correct filters
function mce_button() {
// check user permissions
if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) {
return;
}
// check if WYSIWYG is enabled
if ( 'true' == get_user_option( 'rich_editing' ) ) {
add_filter( 'mce_external_plugins', array( $this, 'add_mce_plugin' ) );
add_filter( 'mce_buttons', array( $this, 'register_mce_button' ) );
}
}
// Script for our mce button
function add_mce_plugin( $plugin_array ) {
$plugin_array['twd_mce_button'] = TWD__PLUGIN_URL . 'mce.js';
return $plugin_array;
}
// Register our button in the editor
function register_mce_button( $buttons ) {
array_push( $buttons, 'twd_mce_button' );
return $buttons;
}
/**
* Function to fetch cpt posts list
* @since 1.7
* @return string
*/
public function posts( $post_type ) {
global $wpdb;
$cpt_type = $post_type;
$cpt_post_status = 'publish';
$cpt = $wpdb->get_results( $wpdb->prepare(
"SELECT ID, post_title
FROM $wpdb->posts
WHERE $wpdb->posts.post_type = %s
AND $wpdb->posts.post_status = %s
ORDER BY ID DESC",
$cpt_type,
$cpt_post_status
) );
$list = array();
foreach ( $cpt as $post ) {
$selected = '';
$post_id = $post->ID;
$post_name = $post->post_title;
$list[] = array(
'text' => $post_name,
'value' => $post_id
);
}
wp_send_json( $list );
}
/**
* Function to fetch buttons
* @since 1.6
* @return string
*/
public function list_ajax() {
// check for nonce
check_ajax_referer( 'twd-nonce', 'security' );
$posts = $this->posts( 'book' ); // change 'book' to 'post' if you need posts list
return $posts;
}
/**
* Function to output button list ajax script
* @since 1.6
* @return string
*/
public function cpt_list() {
// create nonce
global $pagenow;
var_dump($pagenow);
if( $pagenow != 'admin.php' ){
$nonce = wp_create_nonce( 'twd-nonce' );
?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
var data = {
'action' : 'cpt_list', // wp ajax action
'security' : '<?php echo $nonce; ?>' // nonce value created earlier
};
// fire ajax
jQuery.post( ajaxurl, data, function( response ) {
// if nonce fails then not authorized else settings saved
if( response === '-1' ){
// do nothing
console.log('error');
} else {
if (typeof(tinyMCE) != 'undefined') {
if (tinyMCE.DOM != null) {
tinyMCE.DOM.cptPostsList = response;
}
}
}
});
});
</script>
<?php
}
}
} // Mce Class
}
/**
* Kicking this off
*/
$twd_mce = new TWDTinymceExample();
$twd_mce->init();