| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* REST API logic for posts. |
| 5 |
*/ |
| 6 |
final class FL_Assistant_REST_Posts { |
| 7 |
|
| 8 |
/** |
| 9 |
* Register routes. |
| 10 |
*/ |
| 11 |
static public function register_routes() { |
| 12 |
register_rest_route( |
| 13 |
FL_Assistant_REST::$namespace, '/posts', array( |
| 14 |
array( |
| 15 |
'methods' => WP_REST_Server::READABLE, |
| 16 |
'callback' => __CLASS__ . '::posts', |
| 17 |
'permission_callback' => function() { |
| 18 |
return current_user_can( 'edit_published_posts' ); |
| 19 |
}, |
| 20 |
), |
| 21 |
) |
| 22 |
); |
| 23 |
|
| 24 |
register_rest_route( |
| 25 |
FL_Assistant_REST::$namespace, '/posts/hierarchical', array( |
| 26 |
array( |
| 27 |
'methods' => WP_REST_Server::READABLE, |
| 28 |
'callback' => __CLASS__ . '::hierarchical_posts', |
| 29 |
'permission_callback' => function() { |
| 30 |
return current_user_can( 'edit_published_posts' ); |
| 31 |
}, |
| 32 |
), |
| 33 |
) |
| 34 |
); |
| 35 |
|
| 36 |
register_rest_route( |
| 37 |
FL_Assistant_REST::$namespace, '/posts/count', array( |
| 38 |
array( |
| 39 |
'methods' => WP_REST_Server::READABLE, |
| 40 |
'callback' => __CLASS__ . '::posts_count', |
| 41 |
'permission_callback' => function() { |
| 42 |
return current_user_can( 'edit_published_posts' ); |
| 43 |
}, |
| 44 |
), |
| 45 |
) |
| 46 |
); |
| 47 |
|
| 48 |
register_rest_route( |
| 49 |
FL_Assistant_REST::$namespace, '/post/(?P<id>\d+)', array( |
| 50 |
array( |
| 51 |
'methods' => WP_REST_Server::READABLE, |
| 52 |
'callback' => __CLASS__ . '::post', |
| 53 |
'args' => array( |
| 54 |
'id' => array( |
| 55 |
'required' => true, |
| 56 |
'type' => 'number', |
| 57 |
), |
| 58 |
), |
| 59 |
'permission_callback' => function() { |
| 60 |
return current_user_can( 'edit_published_posts' ); |
| 61 |
}, |
| 62 |
), |
| 63 |
array( |
| 64 |
'methods' => WP_REST_Server::CREATABLE, |
| 65 |
'callback' => __CLASS__ . '::update_post', |
| 66 |
'args' => array( |
| 67 |
'id' => array( |
| 68 |
'required' => true, |
| 69 |
'type' => 'number', |
| 70 |
), |
| 71 |
'action' => array( |
| 72 |
'required' => true, |
| 73 |
'type' => 'string', |
| 74 |
), |
| 75 |
), |
| 76 |
'permission_callback' => function() { |
| 77 |
return current_user_can( 'edit_published_posts' ); |
| 78 |
}, |
| 79 |
), |
| 80 |
) |
| 81 |
); |
| 82 |
|
| 83 |
register_rest_route( |
| 84 |
FL_Assistant_REST::$namespace, '/post', array( |
| 85 |
array( |
| 86 |
'methods' => WP_REST_Server::CREATABLE, |
| 87 |
'callback' => __CLASS__ . '::create_post', |
| 88 |
'permission_callback' => function() { |
| 89 |
return current_user_can( 'edit_published_posts' ); |
| 90 |
}, |
| 91 |
), |
| 92 |
) |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Returns an array of response data for a single post. |
| 98 |
*/ |
| 99 |
static public function get_post_response_data( $post ) { |
| 100 |
$author = get_the_author_meta( 'display_name', $post->post_author ); |
| 101 |
$date = get_the_date( '', $post ); |
| 102 |
$response = array( |
| 103 |
'author' => $author, |
| 104 |
'commentsAllowed' => 'open' === $post->comment_status ? true : false, |
| 105 |
'content' => $post->post_content, |
| 106 |
'excerpt' => $post->post_excerpt, |
| 107 |
'date' => $date, |
| 108 |
'editUrl' => get_edit_post_link( $post->ID, '' ), |
| 109 |
'id' => $post->ID, |
| 110 |
'meta' => $author . ' - ' . $date, |
| 111 |
'parent' => $post->post_parent, |
| 112 |
'slug' => $post->post_name, |
| 113 |
'status' => $post->post_status, |
| 114 |
'thumbnail' => get_the_post_thumbnail_url( $post, 'thumbnail' ), |
| 115 |
'title' => empty( $post->post_title ) ? __( '(no title)', 'fl-assistant' ) : $post->post_title, |
| 116 |
'type' => $post->post_type, |
| 117 |
'url' => get_permalink( $post ), |
| 118 |
'visibility' => __( 'Public', 'fl-assistant' ), |
| 119 |
); |
| 120 |
|
| 121 |
// Post visibility. |
| 122 |
if ( 'private' == $post->post_status ) { |
| 123 |
$response['visibility'] = __( 'Private', 'fl-assistant' ); |
| 124 |
} elseif ( ! empty( $post->post_password ) ) { |
| 125 |
$response['visibility'] = __( 'Password Protected', 'fl-assistant' ); |
| 126 |
} |
| 127 |
|
| 128 |
// Beaver Builder data. |
| 129 |
if ( class_exists( 'FLBuilderModel' ) ) { |
| 130 |
$response['bbCanEdit'] = FL_Assistant_Data::bb_can_edit_post( $post->ID ); |
| 131 |
$response['bbIsEnabled'] = FLBuilderModel::is_builder_enabled( $post->ID ); |
| 132 |
$response['bbBranding'] = FLBuilderModel::get_branding(); |
| 133 |
$response['bbEditUrl'] = FLBuilderModel::get_edit_url( $post->ID ); |
| 134 |
} |
| 135 |
|
| 136 |
return $response; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Returns an array of posts and related data. |
| 141 |
*/ |
| 142 |
static public function posts( $request ) { |
| 143 |
$response = array(); |
| 144 |
$params = $request->get_params(); |
| 145 |
$posts = get_posts( |
| 146 |
array_merge( |
| 147 |
$params, array( |
| 148 |
'perm' => 'editable', |
| 149 |
) |
| 150 |
) |
| 151 |
); |
| 152 |
|
| 153 |
foreach ( $posts as $post ) { |
| 154 |
if ( current_user_can( 'edit_post', $post->ID ) ) { |
| 155 |
$response[] = self::get_post_response_data( $post ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return rest_ensure_response( $response ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Returns an array of posts and related data |
| 164 |
* with child posts contained in the parent |
| 165 |
* post's data array. |
| 166 |
*/ |
| 167 |
static public function hierarchical_posts( $request ) { |
| 168 |
$response = array(); |
| 169 |
$children = array(); |
| 170 |
$params = $request->get_params(); |
| 171 |
$posts = get_posts( |
| 172 |
array_merge( |
| 173 |
$params, array( |
| 174 |
'perm' => 'editable', |
| 175 |
) |
| 176 |
) |
| 177 |
); |
| 178 |
|
| 179 |
foreach ( $posts as $post ) { |
| 180 |
if ( $post->post_parent ) { |
| 181 |
if ( ! isset( $children[ $post->post_parent ] ) ) { |
| 182 |
$children[ $post->post_parent ] = array(); |
| 183 |
} |
| 184 |
$children[ $post->post_parent ][] = $post; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
foreach ( $posts as $post ) { |
| 189 |
if ( ! $post->post_parent ) { |
| 190 |
$parent = self::get_post_response_data( $post ); |
| 191 |
$parent['children'] = self::get_child_posts( $post, $children ); |
| 192 |
$response[] = $parent; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
return rest_ensure_response( $response ); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Returns an array of child posts for the given post. |
| 201 |
* A $children array must be passed to search for children. |
| 202 |
*/ |
| 203 |
static public function get_child_posts( $post, $children ) { |
| 204 |
if ( isset( $children[ $post->ID ] ) ) { |
| 205 |
$post_children = $children[ $post->ID ]; |
| 206 |
foreach ( $post_children as $i => $child ) { |
| 207 |
$post_children[ $i ] = self::get_post_response_data( $child ); |
| 208 |
$post_children[ $i ]['children'] = self::get_child_posts( $child, $children ); |
| 209 |
} |
| 210 |
return $post_children; |
| 211 |
} |
| 212 |
return array(); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Returns an array of counts by post type. |
| 217 |
*/ |
| 218 |
static public function posts_count( $request ) { |
| 219 |
$post_types = FL_Assistant_Data::get_post_types(); |
| 220 |
$response = array(); |
| 221 |
|
| 222 |
foreach ( $post_types as $slug => $label ) { |
| 223 |
$counts = wp_count_posts( $slug ); |
| 224 |
$counts->total = $counts->publish + $counts->draft + $counts->pending + $counts->private + $counts->future; |
| 225 |
$response[ $slug ] = $counts; |
| 226 |
} |
| 227 |
|
| 228 |
return rest_ensure_response( $response ); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Returns data for a single post. |
| 233 |
*/ |
| 234 |
static public function post( $request ) { |
| 235 |
$id = $request->get_param( 'id' ); |
| 236 |
|
| 237 |
if ( current_user_can( 'edit_post', $id ) ) { |
| 238 |
$post = get_post( $id ); |
| 239 |
return self::get_post_response_data( $post ); |
| 240 |
} |
| 241 |
|
| 242 |
return array(); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Creates a single post. |
| 247 |
*/ |
| 248 |
static public function create_post( $request ) { |
| 249 |
$id = wp_insert_post( $request->get_params() ); |
| 250 |
|
| 251 |
if ( ! $id || is_wp_error( $id ) ) { |
| 252 |
return array( |
| 253 |
'error' => true, |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
return self::get_post_response_data( get_post( $id ) ); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Updates a single post based on the specified action. |
| 262 |
*/ |
| 263 |
static public function update_post( $request ) { |
| 264 |
$id = $request->get_param( 'id' ); |
| 265 |
$action = $request->get_param( 'action' ); |
| 266 |
|
| 267 |
if ( ! current_user_can( 'edit_post', $id ) ) { |
| 268 |
return rest_ensure_response( |
| 269 |
array( |
| 270 |
'error' => true, |
| 271 |
) |
| 272 |
); |
| 273 |
} |
| 274 |
|
| 275 |
switch ( $action ) { |
| 276 |
case 'data': |
| 277 |
$data = (array) json_decode( $request->get_param( 'data' ) ); |
| 278 |
wp_update_post( |
| 279 |
array_merge( |
| 280 |
$data, array( |
| 281 |
'ID' => $id, |
| 282 |
) |
| 283 |
) |
| 284 |
); |
| 285 |
break; |
| 286 |
case 'trash': |
| 287 |
if ( ! EMPTY_TRASH_DAYS ) { |
| 288 |
wp_delete_post( $id ); |
| 289 |
} else { |
| 290 |
wp_trash_post( $id ); |
| 291 |
} |
| 292 |
break; |
| 293 |
case 'untrash': |
| 294 |
wp_untrash_post( $id ); |
| 295 |
break; |
| 296 |
} |
| 297 |
|
| 298 |
return rest_ensure_response( |
| 299 |
array( |
| 300 |
'success' => true, |
| 301 |
) |
| 302 |
); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
FL_Assistant_REST_Posts::register_routes(); |
| 307 |
|