Plugin Directory

Changeset 2148072


Ignore:
Timestamp:
08/29/2019 09:02:18 PM (7 years ago)
Author:
canvasflow
Message:

Version 1.2.0

Location:
canvasflow-export/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • canvasflow-export/trunk/README.md

    r2079383 r2148072  
    11# Canvasflow Export
    22
    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.
    44
    55#### Features
    66
    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.
    912* Full control over how text components are converted.
    10 * Publish clean, semantic HTML5 mark-up.
    1113
    1214#### Prerequisite Knowledge
    1315
    14 Basic experience of WordPress and Canvasflow is required. This plugin also assumes you have access to an active Canvasflow account with a licence that supports exporting to WordPress.
     16Basic experience of WordPress and Canvasflow is required. This plugin assumes you have access to an active, API enabled Canvasflow account.
    1517
    1618#### Requirements
     
    5759#### Changelog
    5860
     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
    5967##### 1.1.0
    6068* Replace update article method from PUT to POST
     
    6573#### Upgrade Notice
    6674
     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
    6781##### 1.1.0
    6882* Replace update article method from PUT to POST
  • canvasflow-export/trunk/canvasflow-export.php

    r2079385 r2148072  
    33  Plugin Name: Canvasflow Export
    44  Description: This plugin provides a quick, simple and secure way to push Canvasflow articles directly to WordPress.
    5   Version: 1.1.0
     5  Version: 1.2.0
    66    Developer:  Canvasflow
    77    Developer URI: https://canvasflow.io
  • canvasflow-export/trunk/includes/canvasflow-export-controller.php

    r2079383 r2148072  
    2626            $posts = 'posts';
    2727            $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            ) );
    2836            register_rest_route( $namespace, '/' . $authenticate, array(
    2937                array(
     
    94102            }       
    95103        }
     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        }
    96120   
    97121        /**
     
    103127        public function get_articles( $request ) {
    104128            $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
    105135            foreach($this->canvasflow_db->get_posts() as $item) {
    106136                $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                }   
    107142               
    108143                $post = array(
     
    111146                        'rendered' => $item->title
    112147                    ),
    113                     'link' => get_permalink($item->id),
     148                    'excerpt' => $item->excerpt,
     149                    'link' => get_the_permalink($item->id),
    114150                    'date' => $item->post_modified_date,
    115151                    'metadata' => $this->get_meta_data((int)$item->id),
    116                     'status' => $item->status
     152                    'status' => $item->status,
     153                    'type' => $item->type,
     154                    'categories' => $categories
    117155                );
     156
     157                $thumbnail = get_the_post_thumbnail_url($post['id']);
     158                if($thumbnail) {
     159                    $post['thumbnail'] = $thumbnail;
     160                }
     161
    118162                array_push($posts, $post);
    119163            }
     
    158202   
    159203            if($response == null) {
    160                 return new WP_Error( 'not-found', __( 'Article not found'), array( 'status' => 404 ) );
     204                return new WP_Error( 'not-found', __( 'Post not found'), array( 'status' => 404 ) );
    161205            } 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
    162216                $post = array(
    163217                    'id' => (int)$response->id,
     
    165219                        'rendered' => $response->title
    166220                    ),
    167                     'link' => get_permalink($response->id),
     221                    'excerpt' => $response->excerpt,
     222                    'link' => get_the_permalink($response->id),
    168223                    'metadata' => $this->get_meta_data((int)$response->id),
    169224                    'date' => $response->post_modified_date,
    170                     'status' => $response->status
     225                    'status' => $response->status,
     226                    'type' => $response->type,
     227                    'categories' => $categories
    171228                );
     229
     230                $thumbnail = get_the_post_thumbnail_url($post['id']);
     231                if($thumbnail) {
     232                    $post['thumbnail'] = $thumbnail;
     233                }
     234
    172235                return new WP_REST_Response( $post, 200 );
    173236            }
     
    182245         */
    183246        public function create_article( $request ) {
    184             $title = $_POST['title'];
    185             $content = $_POST['content'];
     247            $type = 'post';
     248            $excerpt = '';
    186249            $metadata;
    187250            $status = 'publish';
     
    190253                return new WP_Error( 'invalid-title', __( 'Title is invalid' ), array( 'status' => 409 ) );
    191254            }
     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            }
    192263   
    193264            if (!isset($_POST['content'])) {
    194265                return new WP_Error( 'invalid-content', __( 'Content is invalid' ), array( 'status' => 409 ) );
    195266            }
     267
     268            $content = $_POST['content'];
    196269           
    197270            if (isset($_POST['metadata'])) {
    198271                $metadata = $_POST['metadata'];
    199272            }
    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,
    207280                'post_author' => $this->get_user_from_api_key($this->get_api_key_from_request()),
    208281                'post_title' => $title,
    209282                'post_content' => $content,
    210                 'post_status' => $status
     283                'post_status' => $status,
     284                'post_excerpt' => $excerpt
    211285            );
     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            }
    212297           
    213298            remove_filter('content_save_pre', 'wp_filter_post_kses');
    214299            remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
    215300           
    216             $result = wp_insert_post($post_content, true);
     301            $result = wp_insert_post($post, true);
    217302           
    218303            add_filter('content_save_pre', 'wp_filter_post_kses');
     
    222307                return new WP_Error( $result->get_error_code(), __($result->get_error_message()), array( 'status' => 500 ) );
    223308            }
     309
     310            $category_map = array();
     311            foreach(get_categories(array( 'hide_empty' => 0)) as $category) {
     312                $category_map[(string)$category->term_id] = $category;
     313            }
    224314   
    225315            $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
    226322            $post = array(
    227323                'id' => (int)$response->id,
     
    229325                    'rendered' => $response->title
    230326                ),
    231                 'link' => get_permalink($response->id),
     327                'excerpt' => $response->excerpt,
     328                'link' => get_the_permalink($response->id),
    232329                'date' => $response->post_modified_date,
    233                 'status' => $response->status
     330                'status' => $response->status,
     331                'type' => $response->type,
     332                'categories' => $categories
    234333            );
    235334
     
    248347                }   
    249348            }
     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
    250359            return new WP_REST_Response( $post, 200 );
    251360        }
     
    259368        public function update_article( $request ) {
    260369            //get parameters from request
    261             $params = $request->get_params();       
     370            $params = $request->get_params();   
     371            $type = 'post';
    262372            $id = $params['id'];
    263373            $status = 'publish';
    264             $metadata;
     374            $excerpt = '';
    265375           
    266376            $set_content = true;
    267    
    268            
    269            
    270            
    271            
    272377            // $_PUT = json_decode($_PUT, true);
    273378
     
    275380                return new WP_Error( 'invalid-title', __( 'Title is invalid' ), array( 'status' => 409 ) );
    276381            }
    277    
     382
    278383            if (!isset($_POST['content'])) {
    279384                $set_content = false;
     
    283388                $metadata = $_POST['status'];
    284389            }
    285            
    286             if (isset($_POST['metadata'])) {
    287                 $metadata = $_POST['metadata'];
     390
     391            if (isset($_POST['excerpt'])) {
     392                $excerpt = $_POST['excerpt'];
    288393            }
    289394
     
    301406           
    302407            if(!is_numeric($id)) {
    303                 return new WP_Error( 'invalid-id', __( 'Article id is invalid' ), array( 'status' => 409 ) );
     408                return new WP_Error( 'invalid-id', __( 'Post id is invalid' ), array( 'status' => 409 ) );
    304409            }
    305410   
     
    309414            $post = array(
    310415                'ID' => (int)$id,
    311                 'post_type' => 'post',
    312416                'post_author' => $this->get_user_from_api_key($this->get_api_key_from_request()),
    313417                'post_title' => $title,
    314                 'post_status' => $status
     418                'post_excerpt' => $excerpt
    315419            );
     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            }
    316437           
    317438            $result = '';
    318439           
    319440            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'])) {
    333451                try {
    334                     $metadata = json_decode(stripslashes($metadata), true);
     452                    $metadata = json_decode(stripslashes($_POST['metadata']), true);
    335453                    $keys = array_keys($metadata);
    336454                    for($i=0; $i < count($keys); ++$i) {
     
    354472                return new WP_Error( $result->get_error_code(), __($result->get_error_message()), array( 'status' => 500 ) );
    355473            }
     474
     475            $category_map = array();
     476            foreach(get_categories(array( 'hide_empty' => 0)) as $category) {
     477                $category_map[(string)$category->term_id] = $category;
     478            }
    356479   
    357480            $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           
    358487            $post = array(
    359488                'id' => (int)$response->id,
     
    361490                    'rendered' => $response->title
    362491                ),
    363                 'link' => get_permalink($response->id),
     492                'excerpt' => $response->excerpt,
     493                'link' => get_the_permalink($response->id),
    364494                'date' => $response->post_modified_date,
    365                 'status' => $response->status
     495                'status' => $response->status,
     496                'type' => $response->type,
     497                'categories' => $categories
    366498            );
     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
    367509            return new WP_REST_Response( $post, 200 );
    368510        }
     
    379521   
    380522            $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
    381527            $post = array(
    382528                'id' => (int)$response->id,
     
    384530                    'rendered' => $response->title
    385531                ),
    386                 'link' => get_permalink($response->id),
     532                'excerpt' => $response->excerpt,
     533                'link' => get_the_permalink($response->id),
    387534                'date' => $response->post_modified_date,
    388                 'status' => $response->status
     535                'status' => $response->status,
     536                'type' => $response->type
    389537            );
     538
     539            $thumbnail = get_the_post_thumbnail_url($post['id']);
     540            if($thumbnail) {
     541                $post['thumbnail'] = $thumbnail;
     542            }
    390543   
    391544            $result = wp_delete_post( $id, false );
     
    415568                'post_status' => null,
    416569                'post_parent' => null, // any parent
    417                 );
     570            );
    418571            $attachments = get_posts($args);
    419572            if ($attachments) {
     
    490643                'title' => array(
    491644                    'rendered' => $post->post_title
    492                
    493645                ),
    494646                'slug' => $post->post_name,
     
    672824            return $this->canvasflow_db->get_user_from_api_key($api_key);
    673825        }
     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        }
    674875    }
    675876?>
  • canvasflow-export/trunk/includes/canvasflow-export-db.php

    r2057783 r2148072  
    1313            $this->wpdb = $GLOBALS['wpdb'];
    1414            $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;
    1718        }
    1819
     
    2223            $query = "SELECT post.id as id , post.post_title as title, post.post_content as content, 
    2324            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
    2526            FROM {$this->wp_posts_table_name} as post
    2627            LEFT JOIN {$this->wp_users_table_name} as users ON(post.post_author=users.ID)
     
    4243            $query = "SELECT post.id as id , post.post_title as title, post.post_content as content, 
    4344            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
    4546            FROM {$this->wp_posts_table_name} as post
    4647            LEFT JOIN {$this->wp_users_table_name} as users ON(post.post_author=users.ID)
  • canvasflow-export/trunk/readme.txt

    r2079383 r2148072  
    6767
    6868== 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
    6975= 1.1.0 =
    7076* Replace update article method from PUT to POST
     
    7480
    7581==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
    7688= 1.1.0 =
    7789* Replace update article method from PUT to POST
Note: See TracChangeset for help on using the changeset viewer.