forked from ellatrix/wp-front-end-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-wp-front-end-editor.php
264 lines (159 loc) · 6.75 KB
/
class-wp-front-end-editor.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
<?php
class WP_Front_End_Editor {
public $version = '0.3';
public $plugin = 'wp-front-end-editor/wp-front-end-editor.php';
private static $instance;
private function url() {
return plugin_dir_url( __FILE__ );
}
private function path() {
return dirname( __FILE__ ) . '/';
}
private function response( $response ) {
echo $response;
die();
}
public static function is_edit() {
global $wp_query;
return ( isset( $wp_query->query_vars['edit'] ) ) ? true : false;
}
public static function instance() {
if ( ! self::$instance )
self::$instance = new self;
return self::$instance;
}
private function __construct() {
register_activation_hook( $this->plugin, array( $this, 'activate' ) );
add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) ); // temporary
add_action( 'init', array( $this, 'init' ) );
}
public function activate() {
add_rewrite_endpoint( 'edit', EP_PERMALINK | EP_PAGES );
flush_rewrite_rules();
}
public function after_setup_theme() {
add_theme_support( 'front-end-editor' );
}
public function init() {
if ( ! current_theme_supports( 'front-end-editor' ) )
return;
// TODO add endpoints for custom post types that support the front-end editor
add_rewrite_endpoint( 'edit', EP_PERMALINK | EP_PAGES );
add_action( 'wp_ajax_wpfee_post', array( $this, 'wpfee_post' ) );
add_action( 'wp_ajax_wpfee_shortcode', array( $this, 'wpfee_shortcode' ) );
add_action( 'wp_ajax_wpfee_thumbnail', array( $this, 'wpfee_thumbnail' ) );
add_action( 'wp', array( $this, 'wp' ) );
}
public function wp() {
global $post;
add_filter( 'get_edit_post_link', array( $this, 'get_edit_post_link' ), 10, 3 );
add_filter( 'edit_post_link', array( $this, 'edit_post_link' ), 10, 2 );
if ( ! $this->is_edit() )
return;
if ( ! $post )
wp_die( __( 'You attempted to edit an item that doesn’t exist. Perhaps it was deleted?' ) );
if ( ! current_user_can( 'edit_post', $post->ID ) )
wp_die( __( 'You are not allowed to edit this item.' ) );
require_once( ABSPATH . '/wp-admin/includes/media.php' );
add_action( 'wp_head', array( $this, 'wp_head' ) );
add_action( 'wp_footer', array( $this, 'wp_footer' ), 1 );
add_filter( 'the_title', array( $this, 'the_title' ), 20, 2 );
add_filter( 'the_content', array( $this, 'the_content' ), 20 );
add_filter( 'wp_link_pages', array( $this, 'wp_link_pages' ), 20 );
add_filter( 'post_thumbnail_html', array( $this, 'post_thumbnail_html' ), 10, 5 );
add_filter( 'comments_template', array( $this, 'comments_template' ) );
}
public function get_edit_post_link( $link, $id, $context ) {
$post = get_post( $id );
$post_type = get_post_type_object( $post->post_type );
if( !$post_type->public)
return $link;
if ( $this->is_edit() )
return get_permalink( $id );
if ( ! is_admin() )
return ( ! get_option( 'permalink_structure' ) || $post->post_status == 'draft' ) ? get_permalink( $id ) . '&edit' : get_permalink( $id ) . 'edit/';
return $link;
}
public function edit_post_link( $link, $id ) {
if ( $this->is_edit() )
return '<a class="post-edit-link" href="' . get_permalink( $id ) . '">' . __('Cancel') . '</a>';
return $link;
}
public function wp_head() {
global $post;
wp_enqueue_style( 'fee-style' , $this->url() . 'css/fee.css', false, $this->version, 'screen' );
wp_enqueue_style( 'fee-buttons', $this->url() . 'css/buttons.css', false, $this->version, 'screen' );
wp_enqueue_style( 'genericons', $this->url() . 'css/genericons.css', false, $this->version, 'screen' );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-ui-draggable' );
wp_enqueue_script( 'tinymce-4', $this->url() . 'js/tinymce/tinymce.min.js', array(), '4.0.6', true );
wp_enqueue_script( 'wp-front-end-editor', $this->url() . 'js/wp-front-end-editor.js', array(), $this->version, true );
wp_localize_script( 'wp-front-end-editor', 'wp_front_end_editor', array('post_id' => $post->ID, 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
public function wp_footer() {
global $post;
require_once( 'editor-template.php' );
}
public function the_title( $title, $id ) {
global $post;
if ( $post->ID === $id && in_the_loop() )
return '<div id="fee-edit-title-' . $post->ID . '">' . $title . '</div>';
return $title;
}
public function the_content( $content ) {
global $post;
$content = $post->post_content;
$content = str_replace( '<!--nextpage-->' , esc_html( '<!--nextpage-->' ) , $content );
$content = '<div id="fee-edit-content-' . $post->ID . '" class="contenteditable">' . $content . '</div>';
return $content;
}
public function wp_link_pages( $output ) {
return '';
}
public function post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
global $post;
if ( $post->ID === $post_id && in_the_loop() )
return '
<div id="fee-edit-thumbnail-' . $post->ID . '" class="fee-edit-thumbnail">
<div id="postimagediv">
<div class="inside">
' . $html . '
</div>
<div id="set-post-thumbnail" class="fee-edit-thumbnail-button">
<div id="fee-edit-thumbnail-icon" class="has-dashicon"></div>
</div>
</div>
</div>
<input type="hidden" name="fee_post_thumbnail_size" value="' . $size . '" />
';
}
public function comments_template( $file ) {
return $this->path() . 'meta.php';
}
public function wpfee_post() {
global $wpdb;
if ( ! current_user_can( 'edit_post', $_POST['ID'] ) )
$this->response( __( 'You are not allowed to edit this item.' ) );
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'fee_update_post_' . $_POST['ID'] ) )
$this->response( __( 'You are not allowed to edit this item.' ) );
if ( ! isset( $_POST['ID'] ) )
$this->response( __( 'You attempted to edit an item that doesn’t exist. Perhaps it was deleted?' ) );
$post['ID'] = $_POST['ID'];
$post['post_title'] = $_POST['post_title'];
$post['post_title'] = strip_tags($post['post_title']);
$post['post_content'] = $_POST['post_content'];
$post['post_content'] = str_replace(esc_html('<!--nextpage-->'), '<!--nextpage-->', $post['post_content']);
$post['post_category'] = $_POST['post_category'];
$post['tags_input'] = $_POST['tags_input'];
$id = wp_update_post( $post, true );
if ( is_wp_error( $id ) )
$this->response( __( $id->get_error_message() ) );
$this->response( $id );
}
public function wpfee_shortcode() {
$this->response( do_shortcode( wp_unslash( $_POST['shortcode'] ) ) );
}
public function wpfee_thumbnail() {
$this->response( wp_get_attachment_image_src( get_post_thumbnail_id( $_POST['ID'] ), $_POST['size'] ) );
}
}