Changeset 2148072
- Timestamp:
- 08/29/2019 09:02:18 PM (7 years ago)
- Location:
- canvasflow-export/trunk
- Files:
-
- 5 edited
-
README.md (modified) (3 diffs)
-
canvasflow-export.php (modified) (1 diff)
-
includes/canvasflow-export-controller.php (modified) (23 diffs)
-
includes/canvasflow-export-db.php (modified) (3 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
canvasflow-export/trunk/README.md
r2079383 r2148072 1 1 # Canvasflow Export 2 2 3 This plugin integrates Canvasflow into WordPress allowing Canvasflow to export articles directly to WordPress.3 This plugin integrates Canvasflow into WordPress allowing Canvasflow to post articles directly to WordPress. 4 4 5 5 #### Features 6 6 7 * One click publishing from your Canvasflow account to WordPress. 8 * Automatic format conversion. 7 * One click publishing from your Canvasflow publication to WordPress. 8 * Automatic Canvasflow to WordPress format conversion. 9 * Publish clean, semantic HTML5 mark-up. 10 * WordPress Shortcode support. 11 * Support for all HTML5 tags. 9 12 * Full control over how text components are converted. 10 * Publish clean, semantic HTML5 mark-up.11 13 12 14 #### Prerequisite Knowledge 13 15 14 Basic experience of WordPress and Canvasflow is required. This plugin a lso assumes you have access to an active Canvasflow account with a licence that supports exporting to WordPress.16 Basic experience of WordPress and Canvasflow is required. This plugin assumes you have access to an active, API enabled Canvasflow account. 15 17 16 18 #### Requirements … … 57 59 #### Changelog 58 60 61 ##### 1.2.0 62 * Added support for category meta data. 63 * Added support for articles with a specific publish status 64 * Improved support for multi-site installations 65 * Added support for feature image when creating / updating an article 66 59 67 ##### 1.1.0 60 68 * Replace update article method from PUT to POST … … 65 73 #### Upgrade Notice 66 74 75 ##### 1.2.0 76 * Added support for category meta data. 77 * Added support for articles with a specific publish status 78 * Improved support for multi-site installations 79 * Added support for feature image when creating / updating an article 80 67 81 ##### 1.1.0 68 82 * Replace update article method from PUT to POST -
canvasflow-export/trunk/canvasflow-export.php
r2079385 r2148072 3 3 Plugin Name: Canvasflow Export 4 4 Description: This plugin provides a quick, simple and secure way to push Canvasflow articles directly to WordPress. 5 Version: 1. 1.05 Version: 1.2.0 6 6 Developer: Canvasflow 7 7 Developer URI: https://canvasflow.io -
canvasflow-export/trunk/includes/canvasflow-export-controller.php
r2079383 r2148072 26 26 $posts = 'posts'; 27 27 $media = 'media'; 28 $categories = 'categories'; 29 register_rest_route( $namespace, '/' . $categories, array( 30 array( 31 'methods' => WP_REST_Server::READABLE, 32 'callback' => array( $this, 'get_all_categories' ), 33 'permission_callback' => array( $this, 'get_articles_permissions_check' ) 34 ) 35 ) ); 28 36 register_rest_route( $namespace, '/' . $authenticate, array( 29 37 array( … … 94 102 } 95 103 } 104 105 /** 106 * Get a collection of categories 107 * 108 * @param WP_REST_Request $request Full data about the request. 109 * @return WP_Error|WP_REST_Response 110 */ 111 public function get_all_categories( $request ) { 112 $categories = array(); 113 114 // Remove comment below if you wish to only display categories without parents 115 foreach(get_categories(array( 'hide_empty' => 0/*, 'parent' => 0*/ )) as $category) { 116 array_push($categories , $category); 117 } 118 return new WP_REST_Response( $categories , 200 ); 119 } 96 120 97 121 /** … … 103 127 public function get_articles( $request ) { 104 128 $posts = array(); 129 130 $category_map = array(); 131 foreach(get_categories(array( 'hide_empty' => 0/*, 'parent' => 0*/ )) as $category) { 132 $category_map[(string)$category->term_id] = $category; 133 } 134 105 135 foreach($this->canvasflow_db->get_posts() as $item) { 106 136 $metadata = array(); 137 138 $categories = array(); 139 foreach(wp_get_post_categories((int)$item->id) as $category_id) { 140 array_push($categories, $category_map[(string)$category_id]); 141 } 107 142 108 143 $post = array( … … 111 146 'rendered' => $item->title 112 147 ), 113 'link' => get_permalink($item->id), 148 'excerpt' => $item->excerpt, 149 'link' => get_the_permalink($item->id), 114 150 'date' => $item->post_modified_date, 115 151 'metadata' => $this->get_meta_data((int)$item->id), 116 'status' => $item->status 152 'status' => $item->status, 153 'type' => $item->type, 154 'categories' => $categories 117 155 ); 156 157 $thumbnail = get_the_post_thumbnail_url($post['id']); 158 if($thumbnail) { 159 $post['thumbnail'] = $thumbnail; 160 } 161 118 162 array_push($posts, $post); 119 163 } … … 158 202 159 203 if($response == null) { 160 return new WP_Error( 'not-found', __( ' Articlenot found'), array( 'status' => 404 ) );204 return new WP_Error( 'not-found', __( 'Post not found'), array( 'status' => 404 ) ); 161 205 } else { 206 $category_map = array(); 207 foreach(get_categories(array( 'hide_empty' => 0/*, 'parent' => 0*/ )) as $category) { 208 $category_map[(string)$category->term_id] = $category; 209 } 210 211 $categories = array(); 212 foreach(wp_get_post_categories((int)$response->id) as $category_id) { 213 array_push($categories, $category_map[(string)$category_id]); 214 } 215 162 216 $post = array( 163 217 'id' => (int)$response->id, … … 165 219 'rendered' => $response->title 166 220 ), 167 'link' => get_permalink($response->id), 221 'excerpt' => $response->excerpt, 222 'link' => get_the_permalink($response->id), 168 223 'metadata' => $this->get_meta_data((int)$response->id), 169 224 'date' => $response->post_modified_date, 170 'status' => $response->status 225 'status' => $response->status, 226 'type' => $response->type, 227 'categories' => $categories 171 228 ); 229 230 $thumbnail = get_the_post_thumbnail_url($post['id']); 231 if($thumbnail) { 232 $post['thumbnail'] = $thumbnail; 233 } 234 172 235 return new WP_REST_Response( $post, 200 ); 173 236 } … … 182 245 */ 183 246 public function create_article( $request ) { 184 $t itle = $_POST['title'];185 $ content = $_POST['content'];247 $type = 'post'; 248 $excerpt = ''; 186 249 $metadata; 187 250 $status = 'publish'; … … 190 253 return new WP_Error( 'invalid-title', __( 'Title is invalid' ), array( 'status' => 409 ) ); 191 254 } 255 256 $title = $_POST['title']; 257 258 if (isset($_POST['type'])) { 259 if($_POST['type'] == 'post' || $_POST['type'] == 'page') { 260 $type = $_POST['type']; 261 } 262 } 192 263 193 264 if (!isset($_POST['content'])) { 194 265 return new WP_Error( 'invalid-content', __( 'Content is invalid' ), array( 'status' => 409 ) ); 195 266 } 267 268 $content = $_POST['content']; 196 269 197 270 if (isset($_POST['metadata'])) { 198 271 $metadata = $_POST['metadata']; 199 272 } 200 201 /*if (!isset($_POST['status'])) {202 $ status = $_POST['status'];203 } */204 205 $post _content= array (206 'post_type' => 'post',273 274 if (isset($_POST['excerpt'])) { 275 $excerpt = $_POST['excerpt']; 276 } 277 278 $post = array ( 279 'post_type' => $type, 207 280 'post_author' => $this->get_user_from_api_key($this->get_api_key_from_request()), 208 281 'post_title' => $title, 209 282 'post_content' => $content, 210 'post_status' => $status 283 'post_status' => $status, 284 'post_excerpt' => $excerpt 211 285 ); 286 287 if (isset($_POST['status'])) { 288 if($_POST['status'] == 'publish' || $_POST['status'] == 'future' || $_POST['status'] == 'draft' || $_POST['status'] == 'pending' 289 || $_POST['status'] == 'private' || $_POST['status'] == 'inherit') { 290 $post['post_status'] = $_POST['status']; 291 } 292 } 293 294 if(isset($_POST['categories'])) { 295 $post['post_category'] = json_decode($_POST['categories'], true); 296 } 212 297 213 298 remove_filter('content_save_pre', 'wp_filter_post_kses'); 214 299 remove_filter('content_filtered_save_pre', 'wp_filter_post_kses'); 215 300 216 $result = wp_insert_post($post _content, true);301 $result = wp_insert_post($post, true); 217 302 218 303 add_filter('content_save_pre', 'wp_filter_post_kses'); … … 222 307 return new WP_Error( $result->get_error_code(), __($result->get_error_message()), array( 'status' => 500 ) ); 223 308 } 309 310 $category_map = array(); 311 foreach(get_categories(array( 'hide_empty' => 0)) as $category) { 312 $category_map[(string)$category->term_id] = $category; 313 } 224 314 225 315 $response = $this->canvasflow_db->get_post($result); 316 317 $categories = array(); 318 foreach(wp_get_post_categories((int)$response->id) as $category_id) { 319 array_push($categories, $category_map[(string)$category_id]); 320 } 321 226 322 $post = array( 227 323 'id' => (int)$response->id, … … 229 325 'rendered' => $response->title 230 326 ), 231 'link' => get_permalink($response->id), 327 'excerpt' => $response->excerpt, 328 'link' => get_the_permalink($response->id), 232 329 'date' => $response->post_modified_date, 233 'status' => $response->status 330 'status' => $response->status, 331 'type' => $response->type, 332 'categories' => $categories 234 333 ); 235 334 … … 248 347 } 249 348 } 349 350 if(isset($_FILES["thumbnail"]["name"])) { 351 $this->get_thumbnail_url($post); 352 } 353 354 $thumbnail = get_the_post_thumbnail_url($post['id']); 355 if($thumbnail) { 356 $post['thumbnail'] = $thumbnail; 357 } 358 250 359 return new WP_REST_Response( $post, 200 ); 251 360 } … … 259 368 public function update_article( $request ) { 260 369 //get parameters from request 261 $params = $request->get_params(); 370 $params = $request->get_params(); 371 $type = 'post'; 262 372 $id = $params['id']; 263 373 $status = 'publish'; 264 $ metadata;374 $excerpt = ''; 265 375 266 376 $set_content = true; 267 268 269 270 271 272 377 // $_PUT = json_decode($_PUT, true); 273 378 … … 275 380 return new WP_Error( 'invalid-title', __( 'Title is invalid' ), array( 'status' => 409 ) ); 276 381 } 277 382 278 383 if (!isset($_POST['content'])) { 279 384 $set_content = false; … … 283 388 $metadata = $_POST['status']; 284 389 } 285 286 if (isset($_POST[' metadata'])) {287 $ metadata = $_POST['metadata'];390 391 if (isset($_POST['excerpt'])) { 392 $excerpt = $_POST['excerpt']; 288 393 } 289 394 … … 301 406 302 407 if(!is_numeric($id)) { 303 return new WP_Error( 'invalid-id', __( ' Articleid is invalid' ), array( 'status' => 409 ) );408 return new WP_Error( 'invalid-id', __( 'Post id is invalid' ), array( 'status' => 409 ) ); 304 409 } 305 410 … … 309 414 $post = array( 310 415 'ID' => (int)$id, 311 'post_type' => 'post',312 416 'post_author' => $this->get_user_from_api_key($this->get_api_key_from_request()), 313 417 'post_title' => $title, 314 'post_ status' => $status418 'post_excerpt' => $excerpt 315 419 ); 420 421 if (isset($_POST['type'])) { 422 if($_POST['type'] == 'post' || $_POST['type'] == 'page') { 423 $post['post_type'] = $_POST['type']; 424 } 425 } 426 427 if (isset($_POST['status'])) { 428 if($_POST['status'] == 'publish' || $_POST['status'] == 'future' || $_POST['status'] == 'draft' || $_POST['status'] == 'pending' 429 || $_POST['status'] == 'private' || $_POST['status'] == 'inherit') { 430 $post['post_status'] = $_POST['status']; 431 } 432 } 433 434 if(isset($_POST['categories'])) { 435 $post['post_category'] = json_decode($_POST['categories'], true); 436 } 316 437 317 438 $result = ''; 318 439 319 440 if(strlen($content) > 0) { 320 $post = array( 321 'ID' => (int)$id, 322 'post_author' => $this->get_user_from_api_key($this->get_api_key_from_request()), 323 'post_content' => $content, 324 'post_title' => $title, 325 'post_status' => $status 326 ); 327 $result = wp_insert_post($post, true); 328 } else { 329 $result = wp_update_post($post, true); 330 } 331 332 if(isset($metadata)) { 441 $post['post_content'] = $content; 442 } 443 444 $result = wp_update_post($post, true); 445 446 if($result == false || $result == null) { 447 return new WP_Error( 'not-found', __( 'Post not found'), array( 'status' => 404 ) ); 448 } 449 450 if(isset($_POST['metadata'])) { 333 451 try { 334 $metadata = json_decode(stripslashes($ metadata), true);452 $metadata = json_decode(stripslashes($_POST['metadata']), true); 335 453 $keys = array_keys($metadata); 336 454 for($i=0; $i < count($keys); ++$i) { … … 354 472 return new WP_Error( $result->get_error_code(), __($result->get_error_message()), array( 'status' => 500 ) ); 355 473 } 474 475 $category_map = array(); 476 foreach(get_categories(array( 'hide_empty' => 0)) as $category) { 477 $category_map[(string)$category->term_id] = $category; 478 } 356 479 357 480 $response = $this->canvasflow_db->get_post($id); 481 482 $categories = array(); 483 foreach(wp_get_post_categories((int)$response->id) as $category_id) { 484 array_push($categories, $category_map[(string)$category_id]); 485 } 486 358 487 $post = array( 359 488 'id' => (int)$response->id, … … 361 490 'rendered' => $response->title 362 491 ), 363 'link' => get_permalink($response->id), 492 'excerpt' => $response->excerpt, 493 'link' => get_the_permalink($response->id), 364 494 'date' => $response->post_modified_date, 365 'status' => $response->status 495 'status' => $response->status, 496 'type' => $response->type, 497 'categories' => $categories 366 498 ); 499 500 if(isset($_FILES["thumbnail"]["name"])) { 501 $this->get_thumbnail_url($post); 502 } 503 504 $thumbnail = get_the_post_thumbnail_url($post['id']); 505 if($thumbnail) { 506 $post['thumbnail'] = $thumbnail; 507 } 508 367 509 return new WP_REST_Response( $post, 200 ); 368 510 } … … 379 521 380 522 $response = $this->canvasflow_db->get_post($id); 523 if($response == false || $response == null) { 524 return new WP_Error( 'not-found', __( 'Post not found'), array( 'status' => 404 ) ); 525 } 526 381 527 $post = array( 382 528 'id' => (int)$response->id, … … 384 530 'rendered' => $response->title 385 531 ), 386 'link' => get_permalink($response->id), 532 'excerpt' => $response->excerpt, 533 'link' => get_the_permalink($response->id), 387 534 'date' => $response->post_modified_date, 388 'status' => $response->status 535 'status' => $response->status, 536 'type' => $response->type 389 537 ); 538 539 $thumbnail = get_the_post_thumbnail_url($post['id']); 540 if($thumbnail) { 541 $post['thumbnail'] = $thumbnail; 542 } 390 543 391 544 $result = wp_delete_post( $id, false ); … … 415 568 'post_status' => null, 416 569 'post_parent' => null, // any parent 417 );570 ); 418 571 $attachments = get_posts($args); 419 572 if ($attachments) { … … 490 643 'title' => array( 491 644 'rendered' => $post->post_title 492 493 645 ), 494 646 'slug' => $post->post_name, … … 672 824 return $this->canvasflow_db->get_user_from_api_key($api_key); 673 825 } 826 827 828 private function get_thumbnail_url($post) { 829 $upload_dir = wp_upload_dir(); 830 $post_id = $post['id']; 831 $tmp_file = $upload_dir['path'] . DIRECTORY_SEPARATOR . basename($_FILES["thumbnail"]["tmp_name"]); 832 $ext = pathinfo($_FILES["thumbnail"]["name"], PATHINFO_EXTENSION); 833 $target_file = $upload_dir['path'] . DIRECTORY_SEPARATOR . basename($post_id.'-thumbnail.'.$ext); 834 $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); 835 836 // Check if image file is a actual image or fake image 837 if ($_FILES["thumbnail"]["size"] > 500000000) { 838 unlink($tmp_file); 839 return ''; 840 } 841 842 // Allow certain file formats 843 if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { 844 unlink($tmp_file); 845 return ''; 846 } 847 848 if (move_uploaded_file($_FILES["thumbnail"]["tmp_name"], $target_file)) { 849 $filename = $target_file; 850 $wp_upload_dir = $upload_dir; 851 852 $filetype = wp_check_filetype( basename( $filename ), null ); 853 $thumbnail_url = $wp_upload_dir['url'] . '/' . basename( $filename ); 854 $attachment = array( 855 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), 856 'post_mime_type' => $filetype['type'], 857 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), 858 'post_content' => '', 859 'post_status' => 'inherit' 860 ); 861 862 $attach_id = wp_insert_attachment( $attachment, $filename, $post_id, true); 863 if (is_wp_error($attach_id)){ 864 unlink($filename); 865 return ''; 866 } 867 868 require_once( ABSPATH . 'wp-admin/includes/image.php' ); 869 $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); 870 wp_update_attachment_metadata( $attach_id, $attach_data ); 871 set_post_thumbnail( $post_id, $attach_id ); 872 return $thumbnail_url; 873 } 874 } 674 875 } 675 876 ?> -
canvasflow-export/trunk/includes/canvasflow-export-db.php
r2057783 r2148072 13 13 $this->wpdb = $GLOBALS['wpdb']; 14 14 $this->cf_rest_credentials_table_name = $this->wpdb->prefix."canvasflow_rest_credentials"; 15 $this->wp_posts_table_name = $this->wpdb->prefix."posts"; 16 $this->wp_users_table_name = $this->wpdb->prefix."users"; 15 16 $this->wp_users_table_name = $this->wpdb->users; 17 $this->wp_posts_table_name = $this->wpdb->posts; 17 18 } 18 19 … … 22 23 $query = "SELECT post.id as id , post.post_title as title, post.post_content as content, 23 24 users.display_name as display_name, users.ID as user_id, DATE_FORMAT(post.post_date, '%Y-%m-%dT%TZ') as post_modified_date, 24 post.post_type as type, post.post_status as status 25 post.post_type as type, post.post_status as status, post.post_excerpt as excerpt 25 26 FROM {$this->wp_posts_table_name} as post 26 27 LEFT JOIN {$this->wp_users_table_name} as users ON(post.post_author=users.ID) … … 42 43 $query = "SELECT post.id as id , post.post_title as title, post.post_content as content, 43 44 users.display_name as display_name, users.ID as user_id, DATE_FORMAT(post.post_modified, '%Y-%m-%dT%TZ') as post_modified_date, 44 post.post_type as type, post.post_status as status 45 post.post_type as type, post.post_status as status, post.post_excerpt as excerpt 45 46 FROM {$this->wp_posts_table_name} as post 46 47 LEFT JOIN {$this->wp_users_table_name} as users ON(post.post_author=users.ID) -
canvasflow-export/trunk/readme.txt
r2079383 r2148072 67 67 68 68 == Changelog == 69 = 1.2.0 = 70 * Added support for category meta data. 71 * Added support for articles with a specific publish status 72 * Improved support for multi-site installations 73 * Added support for feature image when creating / updating an article 74 69 75 = 1.1.0 = 70 76 * Replace update article method from PUT to POST … … 74 80 75 81 ==Upgrade Notice== 82 = 1.2.0 = 83 * Added support for category meta data. 84 * Added support for articles with a specific publish status 85 * Improved support for multi-site installations 86 * Added support for feature image when creating / updating an article 87 76 88 = 1.1.0 = 77 89 * Replace update article method from PUT to POST
Note: See TracChangeset
for help on using the changeset viewer.