author | Anthony Ly <anthonyly.com@gmail.com> |
Tue, 11 Dec 2012 12:47:47 -0800 | |
changeset 198 | dcf82fd50cc6 |
parent 194 | 32102edaa81b |
child 204 | 09a1c134465b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Post Administration API. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Rename $_POST data from form names to DB post columns. |
|
11 |
* |
|
12 |
* Manipulates $_POST directly. |
|
13 |
* |
|
14 |
* @package WordPress |
|
15 |
* @since 2.6.0 |
|
16 |
* |
|
17 |
* @param bool $update Are we updating a pre-existing post? |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
18 |
* @param array $post_data Array of post data. Defaults to the contents of $_POST. |
136 | 19 |
* @return object|bool WP_Error on failure, true on success. |
20 |
*/ |
|
21 |
function _wp_translate_postdata( $update = false, $post_data = null ) { |
|
22 |
||
23 |
if ( empty($post_data) ) |
|
24 |
$post_data = &$_POST; |
|
25 |
||
26 |
if ( $update ) |
|
27 |
$post_data['ID'] = (int) $post_data['post_ID']; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
28 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
29 |
if ( isset( $post_data['content'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
30 |
$post_data['post_content'] = $post_data['content']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
31 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
32 |
if ( isset( $post_data['excerpt'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
33 |
$post_data['post_excerpt'] = $post_data['excerpt']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
34 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
35 |
if ( isset( $post_data['parent_id'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
36 |
$post_data['post_parent'] = (int) $post_data['parent_id']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
37 |
|
136 | 38 |
if ( isset($post_data['trackback_url']) ) |
39 |
$post_data['to_ping'] = $post_data['trackback_url']; |
|
40 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
41 |
if ( !isset($post_data['user_ID']) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
42 |
$post_data['user_ID'] = $GLOBALS['user_ID']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
43 |
|
136 | 44 |
if (!empty ( $post_data['post_author_override'] ) ) { |
45 |
$post_data['post_author'] = (int) $post_data['post_author_override']; |
|
46 |
} else { |
|
47 |
if (!empty ( $post_data['post_author'] ) ) { |
|
48 |
$post_data['post_author'] = (int) $post_data['post_author']; |
|
49 |
} else { |
|
50 |
$post_data['post_author'] = (int) $post_data['user_ID']; |
|
51 |
} |
|
52 |
} |
|
53 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
54 |
$ptype = get_post_type_object( $post_data['post_type'] ); |
136 | 55 |
if ( isset($post_data['user_ID']) && ($post_data['post_author'] != $post_data['user_ID']) ) { |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
56 |
if ( !current_user_can( $ptype->cap->edit_others_posts ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
57 |
if ( 'page' == $post_data['post_type'] ) { |
136 | 58 |
return new WP_Error( 'edit_others_pages', $update ? |
59 |
__( 'You are not allowed to edit pages as this user.' ) : |
|
60 |
__( 'You are not allowed to create pages as this user.' ) |
|
61 |
); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
62 |
} else { |
136 | 63 |
return new WP_Error( 'edit_others_posts', $update ? |
64 |
__( 'You are not allowed to edit posts as this user.' ) : |
|
65 |
__( 'You are not allowed to post as this user.' ) |
|
66 |
); |
|
67 |
} |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
// What to do based on which button they pressed |
|
72 |
if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] ) |
|
73 |
$post_data['post_status'] = 'draft'; |
|
74 |
if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] ) |
|
75 |
$post_data['post_status'] = 'private'; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
76 |
if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( !isset($post_data['post_status']) || $post_data['post_status'] != 'private' ) ) |
136 | 77 |
$post_data['post_status'] = 'publish'; |
78 |
if ( isset($post_data['advanced']) && '' != $post_data['advanced'] ) |
|
79 |
$post_data['post_status'] = 'draft'; |
|
80 |
if ( isset($post_data['pending']) && '' != $post_data['pending'] ) |
|
81 |
$post_data['post_status'] = 'pending'; |
|
82 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
83 |
if ( isset( $post_data['ID'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
84 |
$post_id = $post_data['ID']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
85 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
86 |
$post_id = false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
87 |
$previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false; |
136 | 88 |
|
89 |
// Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published. |
|
90 |
// Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
91 |
if ( isset($post_data['post_status']) && ('publish' == $post_data['post_status'] && !current_user_can( $ptype->cap->publish_posts )) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
92 |
if ( $previous_status != 'publish' || !current_user_can( 'edit_post', $post_id ) ) |
136 | 93 |
$post_data['post_status'] = 'pending'; |
94 |
||
95 |
if ( ! isset($post_data['post_status']) ) |
|
96 |
$post_data['post_status'] = $previous_status; |
|
97 |
||
98 |
if (!isset( $post_data['comment_status'] )) |
|
99 |
$post_data['comment_status'] = 'closed'; |
|
100 |
||
101 |
if (!isset( $post_data['ping_status'] )) |
|
102 |
$post_data['ping_status'] = 'closed'; |
|
103 |
||
104 |
foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) { |
|
105 |
if ( !empty( $post_data['hidden_' . $timeunit] ) && $post_data['hidden_' . $timeunit] != $post_data[$timeunit] ) { |
|
106 |
$post_data['edit_date'] = '1'; |
|
107 |
break; |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
if ( !empty( $post_data['edit_date'] ) ) { |
|
112 |
$aa = $post_data['aa']; |
|
113 |
$mm = $post_data['mm']; |
|
114 |
$jj = $post_data['jj']; |
|
115 |
$hh = $post_data['hh']; |
|
116 |
$mn = $post_data['mn']; |
|
117 |
$ss = $post_data['ss']; |
|
118 |
$aa = ($aa <= 0 ) ? date('Y') : $aa; |
|
119 |
$mm = ($mm <= 0 ) ? date('n') : $mm; |
|
120 |
$jj = ($jj > 31 ) ? 31 : $jj; |
|
121 |
$jj = ($jj <= 0 ) ? date('j') : $jj; |
|
122 |
$hh = ($hh > 23 ) ? $hh -24 : $hh; |
|
123 |
$mn = ($mn > 59 ) ? $mn -60 : $mn; |
|
124 |
$ss = ($ss > 59 ) ? $ss -60 : $ss; |
|
125 |
$post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss ); |
|
126 |
$post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] ); |
|
127 |
} |
|
128 |
||
129 |
return $post_data; |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Update an existing post with values provided in $_POST. |
|
134 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
135 |
* @since 1.5.0 |
136 | 136 |
* |
137 |
* @param array $post_data Optional. |
|
138 |
* @return int Post ID. |
|
139 |
*/ |
|
140 |
function edit_post( $post_data = null ) { |
|
141 |
||
142 |
if ( empty($post_data) ) |
|
143 |
$post_data = &$_POST; |
|
144 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
145 |
// Clear out any data in internal vars. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
146 |
unset( $post_data['filter'] ); |
136 | 147 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
148 |
$post_ID = (int) $post_data['post_ID']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
149 |
$post = get_post( $post_ID ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
150 |
$post_data['post_type'] = $post->post_type; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
151 |
$post_data['post_mime_type'] = $post->post_mime_type; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
152 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
153 |
$ptype = get_post_type_object($post_data['post_type']); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
154 |
if ( !current_user_can( $ptype->cap->edit_post, $post_ID ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
155 |
if ( 'page' == $post_data['post_type'] ) |
136 | 156 |
wp_die( __('You are not allowed to edit this page.' )); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
157 |
else |
136 | 158 |
wp_die( __('You are not allowed to edit this post.' )); |
159 |
} |
|
160 |
||
161 |
// Autosave shouldn't save too soon after a real save |
|
162 |
if ( 'autosave' == $post_data['action'] ) { |
|
163 |
$post =& get_post( $post_ID ); |
|
164 |
$now = time(); |
|
165 |
$then = strtotime($post->post_date_gmt . ' +0000'); |
|
166 |
$delta = AUTOSAVE_INTERVAL / 2; |
|
167 |
if ( ($now - $then) < $delta ) |
|
168 |
return $post_ID; |
|
169 |
} |
|
170 |
||
171 |
$post_data = _wp_translate_postdata( true, $post_data ); |
|
172 |
if ( is_wp_error($post_data) ) |
|
173 |
wp_die( $post_data->get_error_message() ); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
174 |
if ( 'autosave' != $post_data['action'] && 'auto-draft' == $post_data['post_status'] ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
175 |
$post_data['post_status'] = 'draft'; |
136 | 176 |
|
177 |
if ( isset($post_data['visibility']) ) { |
|
178 |
switch ( $post_data['visibility'] ) { |
|
179 |
case 'public' : |
|
180 |
$post_data['post_password'] = ''; |
|
181 |
break; |
|
182 |
case 'password' : |
|
183 |
unset( $post_data['sticky'] ); |
|
184 |
break; |
|
185 |
case 'private' : |
|
186 |
$post_data['post_status'] = 'private'; |
|
187 |
$post_data['post_password'] = ''; |
|
188 |
unset( $post_data['sticky'] ); |
|
189 |
break; |
|
190 |
} |
|
191 |
} |
|
192 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
193 |
// Post Formats |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
194 |
if ( isset( $post_data['post_format'] ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
195 |
if ( current_theme_supports( 'post-formats', $post_data['post_format'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
196 |
set_post_format( $post_ID, $post_data['post_format'] ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
197 |
elseif ( '0' == $post_data['post_format'] ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
198 |
set_post_format( $post_ID, false ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
199 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
200 |
|
136 | 201 |
// Meta Stuff |
202 |
if ( isset($post_data['meta']) && $post_data['meta'] ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
203 |
foreach ( $post_data['meta'] as $key => $value ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
204 |
if ( !$meta = get_post_meta_by_id( $key ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
205 |
continue; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
206 |
if ( $meta->post_id != $post_ID ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
207 |
continue; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
208 |
if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
209 |
continue; |
136 | 210 |
update_meta( $key, $value['key'], $value['value'] ); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
211 |
} |
136 | 212 |
} |
213 |
||
214 |
if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
215 |
foreach ( $post_data['deletemeta'] as $key => $value ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
216 |
if ( !$meta = get_post_meta_by_id( $key ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
217 |
continue; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
218 |
if ( $meta->post_id != $post_ID ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
219 |
continue; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
220 |
if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
221 |
continue; |
136 | 222 |
delete_meta( $key ); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
223 |
} |
136 | 224 |
} |
225 |
||
226 |
add_meta( $post_ID ); |
|
227 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
228 |
update_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID ); |
136 | 229 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
230 |
wp_update_post( $post_data ); |
136 | 231 |
|
232 |
// Now that we have an ID we can fix any attachment anchor hrefs |
|
233 |
_fix_attachment_links( $post_ID ); |
|
234 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
235 |
wp_set_post_lock( $post_ID ); |
136 | 236 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
237 |
if ( current_user_can( $ptype->cap->edit_others_posts ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
238 |
if ( ! empty( $post_data['sticky'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
239 |
stick_post( $post_ID ); |
136 | 240 |
else |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
241 |
unstick_post( $post_ID ); |
136 | 242 |
} |
243 |
||
244 |
return $post_ID; |
|
245 |
} |
|
246 |
||
247 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
248 |
* Process the post data for the bulk editing of posts. |
136 | 249 |
* |
250 |
* Updates all bulk edited posts/pages, adding (but not removing) tags and |
|
251 |
* categories. Skips pages when they would be their own parent or child. |
|
252 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
253 |
* @since 2.7.0 |
136 | 254 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
255 |
* @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal. |
136 | 256 |
* @return array |
257 |
*/ |
|
258 |
function bulk_edit_posts( $post_data = null ) { |
|
259 |
global $wpdb; |
|
260 |
||
261 |
if ( empty($post_data) ) |
|
262 |
$post_data = &$_POST; |
|
263 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
264 |
if ( isset($post_data['post_type']) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
265 |
$ptype = get_post_type_object($post_data['post_type']); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
266 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
267 |
$ptype = get_post_type_object('post'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
268 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
269 |
if ( !current_user_can( $ptype->cap->edit_posts ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
270 |
if ( 'page' == $ptype->name ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
271 |
wp_die( __('You are not allowed to edit pages.')); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
272 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
273 |
wp_die( __('You are not allowed to edit posts.')); |
136 | 274 |
} |
275 |
||
276 |
if ( -1 == $post_data['_status'] ) { |
|
277 |
$post_data['post_status'] = null; |
|
278 |
unset($post_data['post_status']); |
|
279 |
} else { |
|
280 |
$post_data['post_status'] = $post_data['_status']; |
|
281 |
} |
|
282 |
unset($post_data['_status']); |
|
283 |
||
284 |
$post_IDs = array_map( 'intval', (array) $post_data['post'] ); |
|
285 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
286 |
$reset = array( 'post_author', 'post_status', 'post_password', 'post_parent', 'page_template', 'comment_status', 'ping_status', 'keep_private', 'tax_input', 'post_category', 'sticky' ); |
136 | 287 |
foreach ( $reset as $field ) { |
288 |
if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) ) |
|
289 |
unset($post_data[$field]); |
|
290 |
} |
|
291 |
||
292 |
if ( isset($post_data['post_category']) ) { |
|
293 |
if ( is_array($post_data['post_category']) && ! empty($post_data['post_category']) ) |
|
294 |
$new_cats = array_map( 'absint', $post_data['post_category'] ); |
|
295 |
else |
|
296 |
unset($post_data['post_category']); |
|
297 |
} |
|
298 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
299 |
$tax_input = array(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
300 |
if ( isset($post_data['tax_input'])) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
301 |
foreach ( $post_data['tax_input'] as $tax_name => $terms ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
302 |
if ( empty($terms) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
303 |
continue; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
304 |
if ( is_taxonomy_hierarchical( $tax_name ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
305 |
$tax_input[ $tax_name ] = array_map( 'absint', $terms ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
306 |
} else { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
307 |
$comma = _x( ',', 'tag delimiter' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
308 |
if ( ',' !== $comma ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
309 |
$terms = str_replace( $comma, ',', $terms ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
310 |
$tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
311 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
312 |
} |
136 | 313 |
} |
314 |
||
315 |
if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) { |
|
316 |
$pages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'"); |
|
317 |
$children = array(); |
|
318 |
||
319 |
for ( $i = 0; $i < 50 && $parent > 0; $i++ ) { |
|
320 |
$children[] = $parent; |
|
321 |
||
322 |
foreach ( $pages as $page ) { |
|
323 |
if ( $page->ID == $parent ) { |
|
324 |
$parent = $page->post_parent; |
|
325 |
break; |
|
326 |
} |
|
327 |
} |
|
328 |
} |
|
329 |
} |
|
330 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
331 |
if ( isset( $post_data['post_format'] ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
332 |
if ( '0' == $post_data['post_format'] ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
333 |
$post_data['post_format'] = false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
334 |
// don't change the post format if it's not supported or not '0' (standard) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
335 |
elseif ( ! current_theme_supports( 'post-formats', $post_data['post_format'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
336 |
unset( $post_data['post_format'] ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
337 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
338 |
|
136 | 339 |
$updated = $skipped = $locked = array(); |
340 |
foreach ( $post_IDs as $post_ID ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
341 |
$post_type_object = get_post_type_object( get_post_type( $post_ID ) ); |
136 | 342 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
343 |
if ( !isset( $post_type_object ) || ( isset($children) && in_array($post_ID, $children) ) || !current_user_can( $post_type_object->cap->edit_post, $post_ID ) ) { |
136 | 344 |
$skipped[] = $post_ID; |
345 |
continue; |
|
346 |
} |
|
347 |
||
348 |
if ( wp_check_post_lock( $post_ID ) ) { |
|
349 |
$locked[] = $post_ID; |
|
350 |
continue; |
|
351 |
} |
|
352 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
353 |
$post = get_post( $post_ID ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
354 |
$tax_names = get_object_taxonomies( $post ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
355 |
foreach ( $tax_names as $tax_name ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
356 |
$taxonomy_obj = get_taxonomy($tax_name); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
357 |
if ( isset( $tax_input[$tax_name]) && current_user_can( $taxonomy_obj->cap->assign_terms ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
358 |
$new_terms = $tax_input[$tax_name]; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
359 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
360 |
$new_terms = array(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
361 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
362 |
if ( $taxonomy_obj->hierarchical ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
363 |
$current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'ids') ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
364 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
365 |
$current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'names') ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
366 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
367 |
$post_data['tax_input'][$tax_name] = array_merge( $current_terms, $new_terms ); |
136 | 368 |
} |
369 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
370 |
if ( isset($new_cats) && in_array( 'category', $tax_names ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
371 |
$cats = (array) wp_get_post_categories($post_ID); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
372 |
$post_data['post_category'] = array_unique( array_merge($cats, $new_cats) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
373 |
unset( $post_data['tax_input']['category'] ); |
136 | 374 |
} |
375 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
376 |
$post_data['post_mime_type'] = $post->post_mime_type; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
377 |
$post_data['guid'] = $post->guid; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
378 |
|
136 | 379 |
$post_data['ID'] = $post_ID; |
380 |
$updated[] = wp_update_post( $post_data ); |
|
381 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
382 |
if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) { |
136 | 383 |
if ( 'sticky' == $post_data['sticky'] ) |
384 |
stick_post( $post_ID ); |
|
385 |
else |
|
386 |
unstick_post( $post_ID ); |
|
387 |
} |
|
388 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
389 |
if ( isset( $post_data['post_format'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
390 |
set_post_format( $post_ID, $post_data['post_format'] ); |
136 | 391 |
} |
392 |
||
393 |
return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked ); |
|
394 |
} |
|
395 |
||
396 |
/** |
|
397 |
* Default post information to use when populating the "Write Post" form. |
|
398 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
399 |
* @since 2.0.0 |
136 | 400 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
401 |
* @param string $post_type A post type string, defaults to 'post'. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
402 |
* @return object stdClass object containing all the default post data as attributes |
136 | 403 |
*/ |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
404 |
function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
405 |
global $wpdb; |
136 | 406 |
|
407 |
$post_title = ''; |
|
408 |
if ( !empty( $_REQUEST['post_title'] ) ) |
|
409 |
$post_title = esc_html( stripslashes( $_REQUEST['post_title'] )); |
|
410 |
||
411 |
$post_content = ''; |
|
412 |
if ( !empty( $_REQUEST['content'] ) ) |
|
413 |
$post_content = esc_html( stripslashes( $_REQUEST['content'] )); |
|
414 |
||
415 |
$post_excerpt = ''; |
|
416 |
if ( !empty( $_REQUEST['excerpt'] ) ) |
|
417 |
$post_excerpt = esc_html( stripslashes( $_REQUEST['excerpt'] )); |
|
418 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
419 |
if ( $create_in_db ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
420 |
$post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
421 |
$post = get_post( $post_id ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
422 |
if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
423 |
set_post_format( $post, get_option( 'default_post_format' ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
424 |
} else { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
425 |
$post = new stdClass; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
426 |
$post->ID = 0; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
427 |
$post->post_author = ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
428 |
$post->post_date = ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
429 |
$post->post_date_gmt = ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
430 |
$post->post_password = ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
431 |
$post->post_type = $post_type; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
432 |
$post->post_status = 'draft'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
433 |
$post->to_ping = ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
434 |
$post->pinged = ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
435 |
$post->comment_status = get_option( 'default_comment_status' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
436 |
$post->ping_status = get_option( 'default_ping_status' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
437 |
$post->post_pingback = get_option( 'default_pingback_flag' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
438 |
$post->post_category = get_option( 'default_category' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
439 |
$post->page_template = 'default'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
440 |
$post->post_parent = 0; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
441 |
$post->menu_order = 0; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
442 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
443 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
444 |
$post->post_content = apply_filters( 'default_content', $post_content, $post ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
445 |
$post->post_title = apply_filters( 'default_title', $post_title, $post ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
446 |
$post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post ); |
136 | 447 |
$post->post_name = ''; |
448 |
||
449 |
return $post; |
|
450 |
} |
|
451 |
||
452 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
453 |
* Get the default page information to use. |
136 | 454 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
455 |
* @since 2.5.0 |
136 | 456 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
457 |
* @return object stdClass object containing all the default post data as attributes |
136 | 458 |
*/ |
459 |
function get_default_page_to_edit() { |
|
460 |
$page = get_default_post_to_edit(); |
|
461 |
$page->post_type = 'page'; |
|
462 |
return $page; |
|
463 |
} |
|
464 |
||
465 |
/** |
|
466 |
* Get an existing post and format it for editing. |
|
467 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
468 |
* @since 2.0.0 |
136 | 469 |
* |
470 |
* @param unknown_type $id |
|
471 |
* @return unknown |
|
472 |
*/ |
|
473 |
function get_post_to_edit( $id ) { |
|
474 |
||
475 |
$post = get_post( $id, OBJECT, 'edit' ); |
|
476 |
||
477 |
if ( $post->post_type == 'page' ) |
|
478 |
$post->page_template = get_post_meta( $id, '_wp_page_template', true ); |
|
479 |
||
480 |
return $post; |
|
481 |
} |
|
482 |
||
483 |
/** |
|
484 |
* Determine if a post exists based on title, content, and date |
|
485 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
486 |
* @since 2.0.0 |
136 | 487 |
* |
488 |
* @param string $title Post title |
|
489 |
* @param string $content Optional post content |
|
490 |
* @param string $date Optional post date |
|
491 |
* @return int Post ID if post exists, 0 otherwise. |
|
492 |
*/ |
|
493 |
function post_exists($title, $content = '', $date = '') { |
|
494 |
global $wpdb; |
|
495 |
||
496 |
$post_title = stripslashes( sanitize_post_field( 'post_title', $title, 0, 'db' ) ); |
|
497 |
$post_content = stripslashes( sanitize_post_field( 'post_content', $content, 0, 'db' ) ); |
|
498 |
$post_date = stripslashes( sanitize_post_field( 'post_date', $date, 0, 'db' ) ); |
|
499 |
||
500 |
$query = "SELECT ID FROM $wpdb->posts WHERE 1=1"; |
|
501 |
$args = array(); |
|
502 |
||
503 |
if ( !empty ( $date ) ) { |
|
504 |
$query .= ' AND post_date = %s'; |
|
505 |
$args[] = $post_date; |
|
506 |
} |
|
507 |
||
508 |
if ( !empty ( $title ) ) { |
|
509 |
$query .= ' AND post_title = %s'; |
|
510 |
$args[] = $post_title; |
|
511 |
} |
|
512 |
||
513 |
if ( !empty ( $content ) ) { |
|
514 |
$query .= 'AND post_content = %s'; |
|
515 |
$args[] = $post_content; |
|
516 |
} |
|
517 |
||
518 |
if ( !empty ( $args ) ) |
|
519 |
return $wpdb->get_var( $wpdb->prepare($query, $args) ); |
|
520 |
||
521 |
return 0; |
|
522 |
} |
|
523 |
||
524 |
/** |
|
525 |
* Creates a new post from the "Write Post" form using $_POST information. |
|
526 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
527 |
* @since 2.1.0 |
136 | 528 |
* |
529 |
* @return unknown |
|
530 |
*/ |
|
531 |
function wp_write_post() { |
|
532 |
global $user_ID; |
|
533 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
534 |
if ( isset($_POST['post_type']) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
535 |
$ptype = get_post_type_object($_POST['post_type']); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
536 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
537 |
$ptype = get_post_type_object('post'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
538 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
539 |
if ( !current_user_can( $ptype->cap->edit_posts ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
540 |
if ( 'page' == $ptype->name ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
541 |
return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this site.' ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
542 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
543 |
return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this site.' ) ); |
136 | 544 |
} |
545 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
546 |
$_POST['post_mime_type'] = ''; |
136 | 547 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
548 |
// Clear out any data in internal vars. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
549 |
unset( $_POST['filter'] ); |
136 | 550 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
551 |
// Edit don't write if we have a post id. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
552 |
if ( isset( $_POST['post_ID'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
553 |
return edit_post(); |
136 | 554 |
|
555 |
$translated = _wp_translate_postdata( false ); |
|
556 |
if ( is_wp_error($translated) ) |
|
557 |
return $translated; |
|
558 |
||
559 |
if ( isset($_POST['visibility']) ) { |
|
560 |
switch ( $_POST['visibility'] ) { |
|
561 |
case 'public' : |
|
562 |
$_POST['post_password'] = ''; |
|
563 |
break; |
|
564 |
case 'password' : |
|
565 |
unset( $_POST['sticky'] ); |
|
566 |
break; |
|
567 |
case 'private' : |
|
568 |
$_POST['post_status'] = 'private'; |
|
569 |
$_POST['post_password'] = ''; |
|
570 |
unset( $_POST['sticky'] ); |
|
571 |
break; |
|
572 |
} |
|
573 |
} |
|
574 |
||
575 |
// Create the post. |
|
576 |
$post_ID = wp_insert_post( $_POST ); |
|
577 |
if ( is_wp_error( $post_ID ) ) |
|
578 |
return $post_ID; |
|
579 |
||
580 |
if ( empty($post_ID) ) |
|
581 |
return 0; |
|
582 |
||
583 |
add_meta( $post_ID ); |
|
584 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
585 |
add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID ); |
136 | 586 |
|
587 |
// Now that we have an ID we can fix any attachment anchor hrefs |
|
588 |
_fix_attachment_links( $post_ID ); |
|
589 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
590 |
wp_set_post_lock( $post_ID ); |
136 | 591 |
|
592 |
return $post_ID; |
|
593 |
} |
|
594 |
||
595 |
/** |
|
596 |
* Calls wp_write_post() and handles the errors. |
|
597 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
598 |
* @since 2.0.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
599 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
600 |
* @uses wp_write_post() |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
601 |
* @uses is_wp_error() |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
602 |
* @uses wp_die() |
136 | 603 |
* @return unknown |
604 |
*/ |
|
605 |
function write_post() { |
|
606 |
$result = wp_write_post(); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
607 |
if ( is_wp_error( $result ) ) |
136 | 608 |
wp_die( $result->get_error_message() ); |
609 |
else |
|
610 |
return $result; |
|
611 |
} |
|
612 |
||
613 |
// |
|
614 |
// Post Meta |
|
615 |
// |
|
616 |
||
617 |
/** |
|
618 |
* {@internal Missing Short Description}} |
|
619 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
620 |
* @since 1.2.0 |
136 | 621 |
* |
622 |
* @param unknown_type $post_ID |
|
623 |
* @return unknown |
|
624 |
*/ |
|
625 |
function add_meta( $post_ID ) { |
|
626 |
global $wpdb; |
|
627 |
$post_ID = (int) $post_ID; |
|
628 |
||
629 |
$metakeyselect = isset($_POST['metakeyselect']) ? stripslashes( trim( $_POST['metakeyselect'] ) ) : ''; |
|
630 |
$metakeyinput = isset($_POST['metakeyinput']) ? stripslashes( trim( $_POST['metakeyinput'] ) ) : ''; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
631 |
$metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
632 |
if ( is_string( $metavalue ) ) |
136 | 633 |
$metavalue = trim( $metavalue ); |
634 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
635 |
if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) { |
136 | 636 |
// We have a key/value pair. If both the select and the |
637 |
// input for the key have data, the input takes precedence: |
|
638 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
639 |
if ( '#NONE#' != $metakeyselect ) |
136 | 640 |
$metakey = $metakeyselect; |
641 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
642 |
if ( $metakeyinput ) |
136 | 643 |
$metakey = $metakeyinput; // default |
644 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
645 |
if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) ) |
136 | 646 |
return false; |
647 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
648 |
$metakey = esc_sql( $metakey ); |
136 | 649 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
650 |
return add_post_meta( $post_ID, $metakey, $metavalue ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
651 |
} |
136 | 652 |
|
653 |
return false; |
|
654 |
} // add_meta |
|
655 |
||
656 |
/** |
|
657 |
* {@internal Missing Short Description}} |
|
658 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
659 |
* @since 1.2.0 |
136 | 660 |
* |
661 |
* @param unknown_type $mid |
|
662 |
* @return unknown |
|
663 |
*/ |
|
664 |
function delete_meta( $mid ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
665 |
return delete_metadata_by_mid( 'post' , $mid ); |
136 | 666 |
} |
667 |
||
668 |
/** |
|
669 |
* Get a list of previously defined keys. |
|
670 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
671 |
* @since 1.2.0 |
136 | 672 |
* |
673 |
* @return unknown |
|
674 |
*/ |
|
675 |
function get_meta_keys() { |
|
676 |
global $wpdb; |
|
677 |
||
678 |
$keys = $wpdb->get_col( " |
|
679 |
SELECT meta_key |
|
680 |
FROM $wpdb->postmeta |
|
681 |
GROUP BY meta_key |
|
682 |
ORDER BY meta_key" ); |
|
683 |
||
684 |
return $keys; |
|
685 |
} |
|
686 |
||
687 |
/** |
|
688 |
* {@internal Missing Short Description}} |
|
689 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
690 |
* @since 2.1.0 |
136 | 691 |
* |
692 |
* @param unknown_type $mid |
|
693 |
* @return unknown |
|
694 |
*/ |
|
695 |
function get_post_meta_by_id( $mid ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
696 |
return get_metadata_by_mid( 'post', $mid ); |
136 | 697 |
} |
698 |
||
699 |
/** |
|
700 |
* {@internal Missing Short Description}} |
|
701 |
* |
|
702 |
* Some postmeta stuff. |
|
703 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
704 |
* @since 1.2.0 |
136 | 705 |
* |
706 |
* @param unknown_type $postid |
|
707 |
* @return unknown |
|
708 |
*/ |
|
709 |
function has_meta( $postid ) { |
|
710 |
global $wpdb; |
|
711 |
||
712 |
return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id |
|
713 |
FROM $wpdb->postmeta WHERE post_id = %d |
|
714 |
ORDER BY meta_key,meta_id", $postid), ARRAY_A ); |
|
715 |
} |
|
716 |
||
717 |
/** |
|
718 |
* {@internal Missing Short Description}} |
|
719 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
720 |
* @since 1.2.0 |
136 | 721 |
* |
722 |
* @param unknown_type $meta_id |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
723 |
* @param unknown_type $meta_key Expect Slashed |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
724 |
* @param unknown_type $meta_value Expect Slashed |
136 | 725 |
* @return unknown |
726 |
*/ |
|
727 |
function update_meta( $meta_id, $meta_key, $meta_value ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
728 |
$meta_key = stripslashes( $meta_key ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
729 |
$meta_value = stripslashes_deep( $meta_value ); |
136 | 730 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
731 |
return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key ); |
136 | 732 |
} |
733 |
||
734 |
// |
|
735 |
// Private |
|
736 |
// |
|
737 |
||
738 |
/** |
|
739 |
* Replace hrefs of attachment anchors with up-to-date permalinks. |
|
740 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
741 |
* @since 2.3.0 |
136 | 742 |
* @access private |
743 |
* |
|
744 |
* @param unknown_type $post_ID |
|
745 |
* @return unknown |
|
746 |
*/ |
|
747 |
function _fix_attachment_links( $post_ID ) { |
|
748 |
$post = & get_post( $post_ID, ARRAY_A ); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
749 |
$content = $post['post_content']; |
136 | 750 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
751 |
// quick sanity check, don't run if no pretty permalinks or post is not published |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
752 |
if ( !get_option('permalink_structure') || $post['post_status'] != 'publish' ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
753 |
return; |
136 | 754 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
755 |
// Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
756 |
if ( !strpos($content, '?attachment_id=') || !preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) ) |
136 | 757 |
return; |
758 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
759 |
$site_url = get_bloginfo('url'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
760 |
$site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
761 |
$replace = ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
762 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
763 |
foreach ( $link_matches[1] as $key => $value ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
764 |
if ( !strpos($value, '?attachment_id=') || !strpos($value, 'wp-att-') |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
765 |
|| !preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
766 |
|| !preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
767 |
continue; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
768 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
769 |
$quote = $url_match[1]; // the quote (single or double) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
770 |
$url_id = (int) $url_match[2]; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
771 |
$rel_id = (int) $rel_match[1]; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
772 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
773 |
if ( !$url_id || !$rel_id || $url_id != $rel_id || strpos($url_match[0], $site_url) === false ) |
136 | 774 |
continue; |
775 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
776 |
$link = $link_matches[0][$key]; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
777 |
$replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link ); |
136 | 778 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
779 |
$content = str_replace( $link, $replace, $content ); |
136 | 780 |
} |
781 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
782 |
if ( $replace ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
783 |
$post['post_content'] = $content; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
784 |
// Escape data pulled from DB. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
785 |
$post = add_magic_quotes($post); |
136 | 786 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
787 |
return wp_update_post($post); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
788 |
} |
136 | 789 |
} |
790 |
||
791 |
/** |
|
792 |
* Move child posts to a new parent. |
|
793 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
794 |
* @since 2.3.0 |
136 | 795 |
* @access private |
796 |
* |
|
797 |
* @param unknown_type $old_ID |
|
798 |
* @param unknown_type $new_ID |
|
799 |
* @return unknown |
|
800 |
*/ |
|
801 |
function _relocate_children( $old_ID, $new_ID ) { |
|
802 |
global $wpdb; |
|
803 |
$old_ID = (int) $old_ID; |
|
804 |
$new_ID = (int) $new_ID; |
|
805 |
||
806 |
$children = $wpdb->get_col( $wpdb->prepare(" |
|
807 |
SELECT post_id |
|
808 |
FROM $wpdb->postmeta |
|
809 |
WHERE meta_key = '_wp_attachment_temp_parent' |
|
810 |
AND meta_value = %d", $old_ID) ); |
|
811 |
||
812 |
foreach ( $children as $child_id ) { |
|
813 |
$wpdb->update($wpdb->posts, array('post_parent' => $new_ID), array('ID' => $child_id) ); |
|
814 |
delete_post_meta($child_id, '_wp_attachment_temp_parent'); |
|
815 |
} |
|
816 |
} |
|
817 |
||
818 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
819 |
* Get all the possible statuses for a post_type |
136 | 820 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
821 |
* @since 2.5.0 |
136 | 822 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
823 |
* @param string $type The post_type you want the statuses for |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
824 |
* @return array As array of all the statuses for the supplied post type |
136 | 825 |
*/ |
826 |
function get_available_post_statuses($type = 'post') { |
|
827 |
$stati = wp_count_posts($type); |
|
828 |
||
829 |
return array_keys(get_object_vars($stati)); |
|
830 |
} |
|
831 |
||
832 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
833 |
* Run the wp query to fetch the posts for listing on the edit posts page |
136 | 834 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
835 |
* @since 2.5.0 |
136 | 836 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
837 |
* @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
838 |
* @return array |
136 | 839 |
*/ |
840 |
function wp_edit_posts_query( $q = false ) { |
|
841 |
if ( false === $q ) |
|
842 |
$q = $_GET; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
843 |
$q['m'] = isset($q['m']) ? (int) $q['m'] : 0; |
136 | 844 |
$q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
845 |
$post_stati = get_post_stati(); |
136 | 846 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
847 |
if ( isset($q['post_type']) && in_array( $q['post_type'], get_post_types() ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
848 |
$post_type = $q['post_type']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
849 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
850 |
$post_type = 'post'; |
136 | 851 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
852 |
$avail_post_stati = get_available_post_statuses($post_type); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
853 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
854 |
if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
855 |
$post_status = $q['post_status']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
856 |
$perm = 'readable'; |
136 | 857 |
} |
858 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
859 |
if ( isset($q['orderby']) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
860 |
$orderby = $q['orderby']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
861 |
elseif ( isset($q['post_status']) && in_array($q['post_status'], array('pending', 'draft')) ) |
136 | 862 |
$orderby = 'modified'; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
863 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
864 |
if ( isset($q['order']) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
865 |
$order = $q['order']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
866 |
elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
867 |
$order = 'ASC'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
868 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
869 |
$per_page = 'edit_' . $post_type . '_per_page'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
870 |
$posts_per_page = (int) get_user_option( $per_page ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
871 |
if ( empty( $posts_per_page ) || $posts_per_page < 1 ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
872 |
$posts_per_page = 20; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
873 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
874 |
$posts_per_page = apply_filters( $per_page, $posts_per_page ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
875 |
$posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
876 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
877 |
$query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
878 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
879 |
// Hierarchical types require special args. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
880 |
if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
881 |
$query['orderby'] = 'menu_order title'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
882 |
$query['order'] = 'asc'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
883 |
$query['posts_per_page'] = -1; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
884 |
$query['posts_per_archive_page'] = -1; |
136 | 885 |
} |
886 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
887 |
if ( ! empty( $q['show_sticky'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
888 |
$query['post__in'] = (array) get_option( 'sticky_posts' ); |
136 | 889 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
890 |
wp( $query ); |
136 | 891 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
892 |
return $avail_post_stati; |
136 | 893 |
} |
894 |
||
895 |
/** |
|
896 |
* Get default post mime types |
|
897 |
* |
|
898 |
* @since 2.9.0 |
|
899 |
* |
|
900 |
* @return array |
|
901 |
*/ |
|
902 |
function get_post_mime_types() { |
|
903 |
$post_mime_types = array( // array( adj, noun ) |
|
904 |
'image' => array(__('Images'), __('Manage Images'), _n_noop('Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>')), |
|
905 |
'audio' => array(__('Audio'), __('Manage Audio'), _n_noop('Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>')), |
|
906 |
'video' => array(__('Video'), __('Manage Video'), _n_noop('Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>')), |
|
907 |
); |
|
908 |
||
909 |
return apply_filters('post_mime_types', $post_mime_types); |
|
910 |
} |
|
911 |
||
912 |
/** |
|
913 |
* {@internal Missing Short Description}} |
|
914 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
915 |
* @since 2.5.0 |
136 | 916 |
* |
917 |
* @param unknown_type $type |
|
918 |
* @return unknown |
|
919 |
*/ |
|
920 |
function get_available_post_mime_types($type = 'attachment') { |
|
921 |
global $wpdb; |
|
922 |
||
923 |
$types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type)); |
|
924 |
return $types; |
|
925 |
} |
|
926 |
||
927 |
/** |
|
928 |
* {@internal Missing Short Description}} |
|
929 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
930 |
* @since 2.5.0 |
136 | 931 |
* |
932 |
* @param unknown_type $q |
|
933 |
* @return unknown |
|
934 |
*/ |
|
935 |
function wp_edit_attachments_query( $q = false ) { |
|
936 |
if ( false === $q ) |
|
937 |
$q = $_GET; |
|
938 |
||
939 |
$q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0; |
|
940 |
$q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0; |
|
941 |
$q['post_type'] = 'attachment'; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
942 |
$post_type = get_post_type_object( 'attachment' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
943 |
$states = 'inherit'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
944 |
if ( current_user_can( $post_type->cap->read_private_posts ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
945 |
$states .= ',private'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
946 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
947 |
$q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
948 |
$media_per_page = (int) get_user_option( 'upload_per_page' ); |
136 | 949 |
if ( empty( $media_per_page ) || $media_per_page < 1 ) |
950 |
$media_per_page = 20; |
|
951 |
$q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page ); |
|
952 |
||
953 |
$post_mime_types = get_post_mime_types(); |
|
954 |
$avail_post_mime_types = get_available_post_mime_types('attachment'); |
|
955 |
||
956 |
if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) ) |
|
957 |
unset($q['post_mime_type']); |
|
958 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
959 |
if ( isset($q['detached']) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
960 |
add_filter('posts_where', '_edit_attachments_query_helper'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
961 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
962 |
wp( $q ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
963 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
964 |
if ( isset($q['detached']) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
965 |
remove_filter('posts_where', '_edit_attachments_query_helper'); |
136 | 966 |
|
967 |
return array($post_mime_types, $avail_post_mime_types); |
|
968 |
} |
|
969 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
970 |
function _edit_attachments_query_helper($where) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
971 |
global $wpdb; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
972 |
return $where .= " AND {$wpdb->posts}.post_parent < 1"; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
973 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
974 |
|
136 | 975 |
/** |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
976 |
* Returns the list of classes to be used by a metabox |
136 | 977 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
978 |
* @uses get_user_option() |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
979 |
* @since 2.5.0 |
136 | 980 |
* |
981 |
* @param unknown_type $id |
|
982 |
* @param unknown_type $page |
|
983 |
* @return unknown |
|
984 |
*/ |
|
985 |
function postbox_classes( $id, $page ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
986 |
if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
987 |
$classes = array( '' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
988 |
} elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
989 |
if ( !is_array( $closed ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
990 |
$classes = array( '' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
991 |
} else { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
992 |
$classes = in_array( $id, $closed ) ? array( 'closed' ) : array( '' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
993 |
} |
136 | 994 |
} else { |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
995 |
$classes = array( '' ); |
136 | 996 |
} |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
997 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
998 |
$classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
999 |
return implode( ' ', $classes ); |
136 | 1000 |
} |
1001 |
||
1002 |
/** |
|
1003 |
* {@internal Missing Short Description}} |
|
1004 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1005 |
* @since 2.5.0 |
136 | 1006 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1007 |
* @param int|object $id Post ID or post object. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1008 |
* @param string $title (optional) Title |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1009 |
* @param string $name (optional) Name |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1010 |
* @return array With two entries of type string |
136 | 1011 |
*/ |
1012 |
function get_sample_permalink($id, $title = null, $name = null) { |
|
1013 |
$post = &get_post($id); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1014 |
if ( !$post->ID ) |
136 | 1015 |
return array('', ''); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1016 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1017 |
$ptype = get_post_type_object($post->post_type); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1018 |
|
136 | 1019 |
$original_status = $post->post_status; |
1020 |
$original_date = $post->post_date; |
|
1021 |
$original_name = $post->post_name; |
|
1022 |
||
1023 |
// Hack: get_permalink would return ugly permalink for |
|
1024 |
// drafts, so we will fake, that our post is published |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1025 |
if ( in_array($post->post_status, array('draft', 'pending')) ) { |
136 | 1026 |
$post->post_status = 'publish'; |
1027 |
$post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID); |
|
1028 |
} |
|
1029 |
||
1030 |
// If the user wants to set a new name -- override the current one |
|
1031 |
// Note: if empty name is supplied -- use the title instead, see #6072 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1032 |
if ( !is_null($name) ) |
136 | 1033 |
$post->post_name = sanitize_title($name ? $name : $title, $post->ID); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1034 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1035 |
$post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent); |
136 | 1036 |
|
1037 |
$post->filter = 'sample'; |
|
1038 |
||
1039 |
$permalink = get_permalink($post, true); |
|
1040 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1041 |
// Replace custom post_type Token with generic pagename token for ease of use. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1042 |
$permalink = str_replace("%$post->post_type%", '%pagename%', $permalink); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1043 |
|
136 | 1044 |
// Handle page hierarchy |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1045 |
if ( $ptype->hierarchical ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1046 |
$uri = get_page_uri($post); |
136 | 1047 |
$uri = untrailingslashit($uri); |
1048 |
$uri = strrev( stristr( strrev( $uri ), '/' ) ); |
|
1049 |
$uri = untrailingslashit($uri); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1050 |
$uri = apply_filters( 'editable_slug', $uri ); |
136 | 1051 |
if ( !empty($uri) ) |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1052 |
$uri .= '/'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1053 |
$permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink); |
136 | 1054 |
} |
1055 |
||
1056 |
$permalink = array($permalink, apply_filters('editable_slug', $post->post_name)); |
|
1057 |
$post->post_status = $original_status; |
|
1058 |
$post->post_date = $original_date; |
|
1059 |
$post->post_name = $original_name; |
|
1060 |
unset($post->filter); |
|
1061 |
||
1062 |
return $permalink; |
|
1063 |
} |
|
1064 |
||
1065 |
/** |
|
1066 |
* sample permalink html |
|
1067 |
* |
|
1068 |
* intended to be used for the inplace editor of the permalink post slug on in the post (and page?) editor. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1069 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1070 |
* @since 2.5.0 |
136 | 1071 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1072 |
* @param int|object $id Post ID or post object. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1073 |
* @param string $new_title (optional) New title |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1074 |
* @param string $new_slug (optional) New slug |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1075 |
* @return string intended to be used for the inplace editor of the permalink post slug on in the post (and page?) editor. |
136 | 1076 |
*/ |
1077 |
function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1078 |
global $wpdb; |
136 | 1079 |
$post = &get_post($id); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1080 |
|
136 | 1081 |
list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug); |
1082 |
||
1083 |
if ( 'publish' == $post->post_status ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1084 |
$ptype = get_post_type_object($post->post_type); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1085 |
$view_post = $ptype->labels->view_item; |
136 | 1086 |
$title = __('Click to edit this part of the permalink'); |
1087 |
} else { |
|
1088 |
$title = __('Temporary permalink. Click to edit this part.'); |
|
1089 |
} |
|
1090 |
||
1091 |
if ( false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%') ) { |
|
1092 |
$return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink">' . $permalink . "</span>\n"; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1093 |
if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) |
136 | 1094 |
$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button" target="_blank">' . __('Change Permalinks') . "</a></span>\n"; |
1095 |
if ( isset($view_post) ) |
|
1096 |
$return .= "<span id='view-post-btn'><a href='$permalink' class='button' target='_blank'>$view_post</a></span>\n"; |
|
1097 |
||
1098 |
$return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug); |
|
1099 |
||
1100 |
return $return; |
|
1101 |
} |
|
1102 |
||
1103 |
if ( function_exists('mb_strlen') ) { |
|
1104 |
if ( mb_strlen($post_name) > 30 ) { |
|
1105 |
$post_name_abridged = mb_substr($post_name, 0, 14). '…' . mb_substr($post_name, -14); |
|
1106 |
} else { |
|
1107 |
$post_name_abridged = $post_name; |
|
1108 |
} |
|
1109 |
} else { |
|
1110 |
if ( strlen($post_name) > 30 ) { |
|
1111 |
$post_name_abridged = substr($post_name, 0, 14). '…' . substr($post_name, -14); |
|
1112 |
} else { |
|
1113 |
$post_name_abridged = $post_name; |
|
1114 |
} |
|
1115 |
} |
|
1116 |
||
1117 |
$post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>'; |
|
1118 |
$display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink); |
|
1119 |
$view_link = str_replace(array('%pagename%','%postname%'), $post_name, $permalink); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1120 |
$return = '<strong>' . __('Permalink:') . "</strong>\n"; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1121 |
$return .= '<span id="sample-permalink">' . $display_link . "</span>\n"; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1122 |
$return .= '‎'; // Fix bi-directional text display defect in RTL languages. |
136 | 1123 |
$return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __('Edit') . "</a></span>\n"; |
1124 |
$return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n"; |
|
1125 |
if ( isset($view_post) ) |
|
1126 |
$return .= "<span id='view-post-btn'><a href='$view_link' class='button' target='_blank'>$view_post</a></span>\n"; |
|
1127 |
||
1128 |
$return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug); |
|
1129 |
||
1130 |
return $return; |
|
1131 |
} |
|
1132 |
||
1133 |
/** |
|
1134 |
* Output HTML for the post thumbnail meta-box. |
|
1135 |
* |
|
1136 |
* @since 2.9.0 |
|
1137 |
* |
|
1138 |
* @param int $thumbnail_id ID of the attachment used for thumbnail |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1139 |
* @param int $post_id ID of the post associated with the thumbnail, defaults to global $post_ID |
136 | 1140 |
* @return string html |
1141 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1142 |
function _wp_post_thumbnail_html( $thumbnail_id = null, $post_id = null ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1143 |
global $content_width, $_wp_additional_image_sizes, $post_ID; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1144 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1145 |
if ( empty( $post_id ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1146 |
$post_id = $post_ID; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1147 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1148 |
$upload_iframe_src = esc_url( get_upload_iframe_src('image', $post_id) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1149 |
$set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1150 |
$content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) ); |
136 | 1151 |
|
1152 |
if ( $thumbnail_id && get_post( $thumbnail_id ) ) { |
|
1153 |
$old_content_width = $content_width; |
|
1154 |
$content_width = 266; |
|
1155 |
if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) ) |
|
1156 |
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) ); |
|
1157 |
else |
|
1158 |
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' ); |
|
1159 |
if ( !empty( $thumbnail_html ) ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1160 |
$ajax_nonce = wp_create_nonce( "set_post_thumbnail-$post_id" ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1161 |
$content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1162 |
$content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>'; |
136 | 1163 |
} |
1164 |
$content_width = $old_content_width; |
|
1165 |
} |
|
1166 |
||
1167 |
return apply_filters( 'admin_post_thumbnail_html', $content ); |
|
1168 |
} |
|
1169 |
||
1170 |
/** |
|
1171 |
* Check to see if the post is currently being edited by another user. |
|
1172 |
* |
|
1173 |
* @since 2.5.0 |
|
1174 |
* |
|
1175 |
* @param int $post_id ID of the post to check for editing |
|
1176 |
* @return bool|int False: not locked or locked by current user. Int: user ID of user with lock. |
|
1177 |
*/ |
|
1178 |
function wp_check_post_lock( $post_id ) { |
|
1179 |
if ( !$post = get_post( $post_id ) ) |
|
1180 |
return false; |
|
1181 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1182 |
if ( !$lock = get_post_meta( $post->ID, '_edit_lock', true ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1183 |
return false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1184 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1185 |
$lock = explode( ':', $lock ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1186 |
$time = $lock[0]; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1187 |
$user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true ); |
136 | 1188 |
|
1189 |
$time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 ); |
|
1190 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1191 |
if ( $time && $time > time() - $time_window && $user != get_current_user_id() ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1192 |
return $user; |
136 | 1193 |
return false; |
1194 |
} |
|
1195 |
||
1196 |
/** |
|
1197 |
* Mark the post as currently being edited by the current user |
|
1198 |
* |
|
1199 |
* @since 2.5.0 |
|
1200 |
* |
|
1201 |
* @param int $post_id ID of the post to being edited |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1202 |
* @return bool|array Returns false if the post doesn't exist of there is no current user, or |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1203 |
* an array of the lock time and the user ID. |
136 | 1204 |
*/ |
1205 |
function wp_set_post_lock( $post_id ) { |
|
1206 |
if ( !$post = get_post( $post_id ) ) |
|
1207 |
return false; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1208 |
if ( 0 == ($user_id = get_current_user_id()) ) |
136 | 1209 |
return false; |
1210 |
||
1211 |
$now = time(); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1212 |
$lock = "$now:$user_id"; |
136 | 1213 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1214 |
update_post_meta( $post->ID, '_edit_lock', $lock ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1215 |
return array( $now, $user_id ); |
136 | 1216 |
} |
1217 |
||
1218 |
/** |
|
1219 |
* Outputs the notice message to say that someone else is editing this post at the moment. |
|
1220 |
* |
|
1221 |
* @since 2.8.5 |
|
1222 |
* @return none |
|
1223 |
*/ |
|
1224 |
function _admin_notice_post_locked() { |
|
1225 |
global $post; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1226 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1227 |
$lock = explode( ':', get_post_meta( $post->ID, '_edit_lock', true ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1228 |
$user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1229 |
$last_user = get_userdata( $user ); |
136 | 1230 |
$last_user_name = $last_user ? $last_user->display_name : __('Somebody'); |
1231 |
||
1232 |
switch ($post->post_type) { |
|
1233 |
case 'post': |
|
1234 |
$message = __( 'Warning: %s is currently editing this post' ); |
|
1235 |
break; |
|
1236 |
case 'page': |
|
1237 |
$message = __( 'Warning: %s is currently editing this page' ); |
|
1238 |
break; |
|
1239 |
default: |
|
1240 |
$message = __( 'Warning: %s is currently editing this.' ); |
|
1241 |
} |
|
1242 |
||
1243 |
$message = sprintf( $message, esc_html( $last_user_name ) ); |
|
1244 |
echo "<div class='error'><p>$message</p></div>"; |
|
1245 |
} |
|
1246 |
||
1247 |
/** |
|
1248 |
* Creates autosave data for the specified post from $_POST data. |
|
1249 |
* |
|
1250 |
* @package WordPress |
|
1251 |
* @subpackage Post_Revisions |
|
1252 |
* @since 2.6.0 |
|
1253 |
* |
|
1254 |
* @uses _wp_translate_postdata() |
|
1255 |
* @uses _wp_post_revision_fields() |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1256 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1257 |
* @return unknown |
136 | 1258 |
*/ |
1259 |
function wp_create_post_autosave( $post_id ) { |
|
1260 |
$translated = _wp_translate_postdata( true ); |
|
1261 |
if ( is_wp_error( $translated ) ) |
|
1262 |
return $translated; |
|
1263 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1264 |
// Only store one autosave. If there is already an autosave, overwrite it. |
136 | 1265 |
if ( $old_autosave = wp_get_post_autosave( $post_id ) ) { |
1266 |
$new_autosave = _wp_post_revision_fields( $_POST, true ); |
|
1267 |
$new_autosave['ID'] = $old_autosave->ID; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1268 |
$new_autosave['post_author'] = get_current_user_id(); |
136 | 1269 |
return wp_update_post( $new_autosave ); |
1270 |
} |
|
1271 |
||
1272 |
// _wp_put_post_revision() expects unescaped. |
|
1273 |
$_POST = stripslashes_deep($_POST); |
|
1274 |
||
1275 |
// Otherwise create the new autosave as a special post revision |
|
1276 |
return _wp_put_post_revision( $_POST, true ); |
|
1277 |
} |
|
1278 |
||
1279 |
/** |
|
1280 |
* Save draft or manually autosave for showing preview. |
|
1281 |
* |
|
1282 |
* @package WordPress |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1283 |
* @since 2.7.0 |
136 | 1284 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1285 |
* @uses get_post_status() |
136 | 1286 |
* @uses edit_post() |
1287 |
* @uses get_post() |
|
1288 |
* @uses current_user_can() |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1289 |
* @uses wp_die() |
136 | 1290 |
* @uses wp_create_post_autosave() |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1291 |
* @uses add_query_arg() |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1292 |
* @uses wp_create_nonce() |
136 | 1293 |
* |
1294 |
* @return str URL to redirect to show the preview |
|
1295 |
*/ |
|
1296 |
function post_preview() { |
|
1297 |
||
1298 |
$post_ID = (int) $_POST['post_ID']; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1299 |
$status = get_post_status( $post_ID ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1300 |
if ( 'auto-draft' == $status ) |
136 | 1301 |
wp_die( __('Preview not available. Please save as a draft first.') ); |
1302 |
||
1303 |
if ( isset($_POST['catslist']) ) |
|
1304 |
$_POST['post_category'] = explode(",", $_POST['catslist']); |
|
1305 |
||
1306 |
if ( isset($_POST['tags_input']) ) |
|
1307 |
$_POST['tags_input'] = explode(",", $_POST['tags_input']); |
|
1308 |
||
1309 |
if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) ) |
|
1310 |
unset($_POST['post_category']); |
|
1311 |
||
1312 |
$_POST['ID'] = $post_ID; |
|
1313 |
$post = get_post($post_ID); |
|
1314 |
||
1315 |
if ( 'page' == $post->post_type ) { |
|
1316 |
if ( !current_user_can('edit_page', $post_ID) ) |
|
1317 |
wp_die(__('You are not allowed to edit this page.')); |
|
1318 |
} else { |
|
1319 |
if ( !current_user_can('edit_post', $post_ID) ) |
|
1320 |
wp_die(__('You are not allowed to edit this post.')); |
|
1321 |
} |
|
1322 |
||
1323 |
if ( 'draft' == $post->post_status ) { |
|
1324 |
$id = edit_post(); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
1325 |
} else { // Non drafts are not overwritten. The autosave is stored in a special post revision. |
136 | 1326 |
$id = wp_create_post_autosave( $post->ID ); |
1327 |
if ( ! is_wp_error($id) ) |
|
1328 |
$id = $post->ID; |
|
1329 |
} |
|
1330 |
||
1331 |
if ( is_wp_error($id) ) |
|
1332 |
wp_die( $id->get_error_message() ); |
|
1333 |
||
1334 |
if ( $_POST['post_status'] == 'draft' ) { |
|
1335 |
$url = add_query_arg( 'preview', 'true', get_permalink($id) ); |
|
1336 |
} else { |
|
1337 |
$nonce = wp_create_nonce('post_preview_' . $id); |
|
1338 |
$url = add_query_arg( array( 'preview' => 'true', 'preview_id' => $id, 'preview_nonce' => $nonce ), get_permalink($id) ); |
|
1339 |
} |
|
1340 |
||
1341 |
return $url; |
|
1342 |
} |