Plugin Directory

Changeset 2392209


Ignore:
Timestamp:
10/02/2020 08:27:29 AM (6 years ago)
Author:
contentstudio
Message:

Added Post Update Method and fixed some posting issues

File:
1 edited

Legend:

Unmodified
Added
Removed
  • contentstudio/trunk/contentstudio-plugin.php

    r2387022 r2392209  
    33Plugin Name: ContentStudio
    44Description: ContentStudio provides you with powerful blogging & social media tools to keep your audience hooked by streamlining the process for you to discover and share engaging content on multiple blogging & social media networks
    5 Version: 1.0.5
     5Version: 1.0.6
    66Author: ContentStudio
    77Author URI: http://contentstudio.io/
     
    2020        if (isset($post->ID)) {
    2121            if ($post->post_type == 'post') {
    22                 $meta_title = get_post_meta($post->ID, 'wpseo_title');
     22                $meta_title = get_post_meta($post->ID, 'contentstudio_wpseo_title');
    2323
    2424                return $meta_title[0];
     
    2929
    3030add_filter('pre_get_document_title', 'cstu_add_wpseo_title');
    31 add_filter('wpseo_title', 'filter_product_wpseo_title');
    32         function filter_product_wpseo_title($title) {
    33             if(  is_singular( 'product') ) {
    34                 $title = $meta_title = sanitize_text_field($_REQUEST['post']['post_meta_title']);
    35             }
    36             return $title;
    37         }
    3831
    3932// Check for existing class
     
    126119            add_action('init', [$this, 'cstu_get_blog_categories']);
    127120            add_action('init', [$this, 'cstu_create_new_post']);
     121            add_action('init', [$this, 'cstu_update_post']);
    128122            add_action('init', [$this, 'cstu_is_installed']);
    129123            add_action('init', [$this, 'cstu_unset_token']);
     
    146140            if ($post) {
    147141                if (isset($post->ID)) {
    148                     $meta_description = get_post_meta($post->ID, 'wpseo_description');
     142                    $meta_description = get_post_meta($post->ID, 'contentstudio_wpseo_description');
    149143                    if ($meta_description) {
    150144                        echo '<meta name="description" content="'.$meta_description[0].'" />'."\n";
     
    662656                            $get_posts_list = $wpdb->get_results($sql);
    663657                            if (count($get_posts_list)) {
     658                                $cstu_post_update = get_page_by_title( $post_title, '', 'post' );
     659                                $getid = 0;
     660                                $getid = $cstu_post_update->ID;
    664661                                echo json_encode([
    665662                                    'status' => false,
    666                                     'message' => "Post already exists on your blog with title '$post_title'.",
     663                                    'message' => "Post already exists on your blog with title '$getid'.",
    667664                                ]);
    668665                                die();
     
    723720
    724721                    if ($post && $post > 0) {
    725                         $this->cstu_download_images($post);
     722                        $this->cstu_download_images($post_id);
    726723                    }
    727724
     
    736733                    if (isset($_REQUEST['post']['featured_image']) && $_REQUEST['post']['featured_image']) {
    737734                        // perform http request to see the status code of the image.
     735                        $checker = $_REQUEST['post']['featured_image'];
     736                        //error_log( print_r( $checker, true ) );
    738737                        $status_code = wp_remote_get($_REQUEST['post']['featured_image'])['response']['code'];
    739738
     
    789788        }
    790789
     790
     791        /**
     792         * Updates an existing WordPress post, action is called from the REMOTE ContentStudio Server.
     793         *
     794         */
     795        public function cstu_update_post()
     796        {
     797            if (isset($_REQUEST) && isset($_REQUEST['cstu_update_post']) && isset($_REQUEST['token'])) {
     798
     799                // validate the token
     800
     801                $valid = $this->do_validate_cstu_token($_REQUEST['token']);
     802                if ($valid) {
     803
     804                    // request post title is available
     805
     806                    // if (isset($_REQUEST['post'])) {
     807                    //     $post_title = wp_strip_all_tags($_REQUEST['post']['post_title']);
     808                    //     // check for the post title and make sure it does not exists.
     809
     810                    //     if (isset($post_title) && $post_title) {
     811                    //         global $wpdb;
     812                    //         $post_title = wp_strip_all_tags($post_title);
     813                    //         $sql = $wpdb->prepare("select ID from ".$wpdb->posts." where post_title='%s' AND  post_status = 'publish'", $post_title);
     814                    //         $get_posts_list = $wpdb->get_results($sql);
     815                    //         if (count($get_posts_list)) {
     816                    //             $cstu_post_update = get_page_by_title( $post_title, '', 'post' );
     817                    //             $getid = 0;
     818                    //             $getid = $cstu_post_update->ID;
     819                    //             echo json_encode([
     820                    //                 'status' => false,
     821                    //                 'message' => "Post already exists on your blog with title '$getid'.",
     822                    //             ]);
     823                    //             die();
     824                    //         }
     825                    //     }
     826                    // }
     827
     828                    // get list of categories
     829
     830                    $categories = explode(',', sanitize_text_field($_REQUEST['post']['post_category']));
     831
     832                    $this->kses_remove_filters();
     833                    $post_id = 0;
     834                    if (isset($_REQUEST['post']['post_id']) && $_REQUEST['post']['post_id']) {
     835                        $post_id = (int) sanitize_text_field($_REQUEST['post']['post_id']);
     836                    }
     837
     838                    // update the post
     839
     840                    $post_author = sanitize_text_field($_REQUEST['post']['post_author']);
     841                    $post_content = $_REQUEST['post']['post_content'];
     842                    $post_status = sanitize_text_field($_REQUEST['post']['post_status']);
     843
     844                    // sanitize title per https://codex.wordpress.org/Function_Reference/wp_insert_post#Security //
     845
     846                    $post = wp_update_post([
     847                        'ID' => $post_id,
     848                        'post_title' => $_REQUEST['post']['post_title'],
     849                        'post_author' => $post_author,
     850                        'post_content' => $post_content,
     851                        'post_status' => $post_status,
     852                        'post_category' => $categories,
     853                    ]);
     854
     855                    if (! $post or $post == 0) {
     856                        $post = wp_update_post([
     857                            'post_title' => $_REQUEST['post']['post_title'],
     858                            'post_author' => $post_author,
     859                            'post_content' => $post_content,
     860                            'post_status' => $post_status,
     861                            'post_category' => $categories,
     862                        ]);
     863                        global $wpdb;
     864                        $wpdb->update($wpdb->posts, ['post_title' => wp_strip_all_tags((string) $post_title)], ['ID' => $post]);
     865                        // slug scenario
     866                    }
     867
     868                    // get post
     869
     870                    $get_post = get_post($post);
     871
     872                    // set the tags
     873
     874                    if (isset($_REQUEST['post']['terms'])) {
     875                        $this->cstu_set_tags($get_post);
     876                    }
     877
     878                    // download the image to the user server.
     879
     880                    if ($post && $post > 0) {
     881                        $this->cstu_download_images($post);
     882                    }
     883
     884                    // seo settings
     885
     886                    $this->set_cstu_metadata_post($get_post);
     887                    $this->set_cstu_yoast_settinsg($get_post);
     888                    $this->set_cstu_all_in_one_seo($get_post);
     889
     890                    // reload the post again to get the latest url.
     891
     892                    if (isset($_REQUEST['post']['featured_image']) && $_REQUEST['post']['featured_image']) {
     893                        // perform http request to see the status code of the image.
     894                        $status_code = wp_remote_get($_REQUEST['post']['featured_image'])['response']['code'];
     895
     896                        // if the status is valid process for upload.
     897
     898                        if ($status_code == 301 || $status_code == 200) {
     899                            $img = $this->cstu_generate_image($_REQUEST['post']['featured_image'], $post);
     900                            if ($img['status']) {
     901                                echo json_encode([
     902                                    'status' => true,
     903                                    'post_id' => $get_post->ID,
     904                                    'link' => get_permalink($get_post->ID),
     905                                ]);
     906                                die();
     907                            } else {
     908                                echo json_encode([
     909                                    'status' => false,
     910                                    'warning_message' => $img['message'],
     911                                    'post_id' => $get_post->ID,
     912                                    'link' => get_permalink($get_post->ID),
     913                                ]);
     914                                die();
     915                            }
     916                        } else {
     917                            echo json_encode([
     918                                'status' => false,
     919                                'warning_message' => 'Post featured image seems to be down. Image HTTP status code is '.$status_code,
     920                                'post_id' => $get_post->ID,
     921                                'link' => get_permalink($get_post->ID)//get_post_permalink($get_post->ID),
     922                            ]);
     923                            die();
     924                        }
     925                    } else {
     926                        echo json_encode([
     927                            'status' => true,
     928                            'post_id' => $get_post->ID,
     929                            'link' => get_permalink($get_post->ID),
     930                        ]); // get_post_permalink($get_post->ID)
     931                        die();
     932                    }
     933                } else {
     934                    echo json_encode([
     935                        'status' => false,
     936                        'message' => self::INVALID_MESSAGE_POST_API,
     937                    ]);
     938                    die();
     939                }
     940            }
     941            /*else {
     942                echo json_encode(['status' => false, 'message' => "error"]);
     943                die();
     944            }*/
     945        }
     946
    791947        /**
    792948         * Set the meta description so that when we publish our content, we show that to the end-user instead of our personal one.
     
    803959            }
    804960            if ($meta_description) {
    805                 if (! get_post_meta($get_post->ID, 'wpseo_description')) {
    806                     add_post_meta($get_post->ID, 'wpseo_description', $meta_description, true);
     961                if (! get_post_meta($get_post->ID, 'contentstudio_wpseo_description')) {
     962                    add_post_meta($get_post->ID, 'contentstudio_wpseo_description', $meta_description, true);
    807963                } else {
    808                     update_post_meta($get_post->ID, 'wpseo_description', $meta_description);
     964                    update_post_meta($get_post->ID, 'contentstudio_wpseo_description', $meta_description);
    809965                }
    810966            }
     
    815971
    816972            if ($meta_title) {
    817                 if (! get_post_meta($get_post->ID, 'wpseo_title')) {
    818                     add_post_meta($get_post->ID, 'wpseo_title', $meta_title, true);
     973                if (! get_post_meta($get_post->ID, 'contentstudio_wpseo_title')) {
     974                    add_post_meta($get_post->ID, 'contentstudio_wpseo_title', $meta_title, true);
    819975                } else {
    820                     update_post_meta($get_post->ID, 'wpseo_title', $meta_title);
     976                    update_post_meta($get_post->ID, 'contentstudio_wpseo_title', $meta_title);
    821977                }
    822978            }
     
    8681024            //}
    8691025        }
    870 
    871        
    8721026
    8731027        /**
Note: See TracChangeset for help on using the changeset viewer.