Plugin Directory

Changeset 1862235


Ignore:
Timestamp:
04/21/2018 12:55:26 PM (8 years ago)
Author:
chaoxifer
Message:

기능 개선

Location:
tistorywriter/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • tistorywriter/trunk/assets/pages/metabox.php

    r1846025 r1862235  
    4747                $account = $api_mgr->getBlogAccount();
    4848                if (!isset($account)) {
    49                     echo "Access 토큰이 정상적으로 발급되지 않았습니다.";
     49                    echo "Access 토큰이 정상적으로 발급되지 않았습니다jf.";
    5050                } else {
    5151                    echo esc_html($account);
  • tistorywriter/trunk/features/api_manager.php

    r1859195 r1862235  
    3939            'access_token' => get_option(OPTION_KEY\ACCESS_TOKEN)
    4040        );
     41
     42        $xml = $this->requestPost($url, $data);
     43        if ($xml == null) {
     44            if (method_exists('\\tistory_writer\\Logger', 'log')) {
     45                Logger::log("getPostInfoWithTitle, Request에 실패했습니다.");
     46                return false;
     47            }
     48        }
     49
     50        return true;
     51    }
     52
     53    public function getBlogAccount()
     54    {
     55        $url = 'https://www.tistory.com/apis/blog/info';
     56        $data = array(
     57            'access_token' => get_option(OPTION_KEY\ACCESS_TOKEN)
     58        );
     59
     60        $xml = $this->requestPost($url, $data);
     61        if ($xml == null) {
     62            if (method_exists('\\tistory_writer\\Logger', 'log')) {
     63                Logger::log("getPostInfoWithTitle, Request에 실패했습니다.");
     64            }
     65            return null;
     66        }
     67
     68        return $xml->item->id;
     69    }
     70
     71    public function requestPost($url, $data)
     72    {
     73        // Step 1. POST
     74        $response = wp_remote_post($url, array(
     75            'body' => $data,
     76            'output' => 'xml',
     77        ));
     78
     79        $body = wp_remote_retrieve_body($response);
     80        $xml = simplexml_load_string($body);
     81
     82        if ($xml != null && $xml->status == 200) {
     83            return $xml;
     84        }
     85
     86        // Try 2. GET
     87        $xml = $this->requestGet($url, $data);
     88        if ($xml != null && $xml->status == 200) {
     89            return $xml;
     90        }
     91
     92        // Try 3. Request - RAW
     93        $xml = $this->requestFallback($url, $data);
     94        if ($xml != null && $xml->status == 200) {
     95            return $xml;
     96        } else {
     97            return null;
     98        }
     99    }
     100
     101    public function requestFallback($url, $data)
     102    {
    41103        $response = wp_remote_request($url, array(
    42104            'body' => $data,
    43105            'output' => 'xml',
    44106        ));
     107
    45108        $body = wp_remote_retrieve_body($response);
    46109        $xml = simplexml_load_string($body);
    47110
    48         if ($xml->status == 200) {
    49             return true;
    50         } else {
    51             return false;
    52         }
    53     }
    54 
    55     public function getBlogAccount()
    56     {
    57         $url = 'https://www.tistory.com/apis/blog/info';
    58         $data = array(
    59             'access_token' => get_option(OPTION_KEY\ACCESS_TOKEN)
    60         );
    61         $response = wp_remote_request($url, array(
     111        if ($xml != null && $xml->status == 200) {
     112            return $xml;
     113        } else {
     114            return null;
     115        }
     116    }
     117
     118    public function requestGet($url, $data)
     119    {
     120        $response = wp_remote_get($url, array(
    62121            'body' => $data,
    63122            'output' => 'xml',
    64123        ));
     124
    65125        $body = wp_remote_retrieve_body($response);
    66126        $xml = simplexml_load_string($body);
    67127
    68         return $xml->item->id;
     128        if ($xml != null && $xml->status == 200) {
     129            return $xml;
     130        } else {
     131            return null;
     132        }
    69133    }
    70134
     
    77141            'targetUrl' => get_option(OPTION_KEY\BLOG_NAME),
    78142        );
    79         $response = wp_remote_request($url, array(
    80             'body' => $data,
    81             'output' => 'xml',
    82         ));
    83         $body = wp_remote_retrieve_body($response);
    84         $xml = simplexml_load_string($body);
     143
     144        $xml = $this->requestPost($url, $data);
     145        if ($xml == null) {
     146            return null;
     147        }
     148
    85149        $array = json_decode(json_encode((array)$xml->item->categories), true);
    86150        return $array;
     
    102166            'tag' => $tag
    103167        );
    104         $response = wp_remote_post($url, array(
    105             'body' => $data,
    106             'output' => 'xml',
    107         ));
     168
     169        $this->requestPost($url, $data);
    108170    }
    109171
     
    127189                'tag' => $tag,
    128190            );
    129             $response = wp_remote_post($url, array(
    130                 'body' => $data,
    131                 'output' => 'xml',
    132             ));
     191
     192            $this->requestPost($url, $data);
    133193        }
    134194    }
     
    143203            'sort' => 'date',
    144204        );
    145         $response = wp_remote_post($url, array(
    146             'body' => $data,
    147             'output' => 'xml',
    148         ));
    149         $rValue = "";
    150 
    151         $body = wp_remote_retrieve_body($response);
    152         $xml = simplexml_load_string($body);
     205
     206        $xml = $this->requestPost($url, $data);
     207        if ($xml == null) {
     208            if (method_exists('\\tistory_writer\\Logger', 'log')) {
     209                Logger::log("getPostInfoWithTitle, Request에 실패했습니다.");
     210                return;
     211            }
     212        }
     213
    153214        $posts = json_decode(json_encode((array)$xml->item->posts), true);
    154215
    155         foreach ($posts as $k => $v) {
    156             foreach ($v as $key => $value) {
    157                 if (stripslashes($value['title']) === stripslashes($title)) {
    158                     $id = $value['id'];
     216        if (is_array($posts) && isset($posts)) {
     217            foreach ($posts as $k => $v) {
     218                if (is_array($v) && isset($v)) {
     219                    foreach ($v as $key => $value) {
     220                        if (stripslashes($value['title']) === stripslashes($title)) {
     221                            $id = $value['id'];
     222                        }
     223                    }
     224                } else {
     225                    if (method_exists('\\tistory_writer\\Logger', 'log')) {
     226                        Logger::log("getPostIdWithTitle, 얻어온 post id 값 이상");
     227                    }
    159228                }
    160             }
    161         }
     229            } // foreach ($post as $k => $v) ends here
     230        } // if statement ends here
    162231    }
    163232
     
    171240            'sort' => 'date',
    172241        );
    173         $response = wp_remote_request($url, array(
    174             'body' => $data,
    175             'output' => 'xml',
    176         ));
    177         $rValue = "";
    178 
    179         $body = wp_remote_retrieve_body($response);
    180         $xml = simplexml_load_string($body);
     242
     243        $xml = $this->requestPost($url, $data);
     244        if ($xml == null) {
     245            if (method_exists('\\tistory_writer\\Logger', 'log')) {
     246                Logger::log("getPostInfoWithTitle, Request에 실패했습니다.");
     247            }
     248            return;
     249        }
     250
    181251        $posts = json_decode(json_encode((array)$xml->item->posts), true);
    182252
    183         foreach ($posts as $k => $v) {
    184             foreach ($v as $key => $value) {
    185                 if ($this->decodeCharacters(stripslashes($value['title'])) === $this->decodeCharacters(stripslashes($title))) {
    186                     return array(
    187                         'id' => $value['id'],
    188                         'url' => $value['postUrl'],
    189                         'date' => $value['date'],
    190                         'visibility' => $value['visibility'],
    191                         'category_id' => $value['categoryId'],
    192                     );
     253        if (is_array($posts) && isset($posts)) {
     254            foreach ($posts as $k => $v) {
     255                if (is_array($v) && isset($v)) {
     256                    foreach ($v as $key => $value) {
     257                        if ($this->decodeCharacters(stripslashes($value['title'])) === $this->decodeCharacters(stripslashes($title))) {
     258                            return array(
     259                                'id' => $value['id'],
     260                                'url' => $value['postUrl'],
     261                                'date' => $value['date'],
     262                                'visibility' => $value['visibility'],
     263                                'category_id' => $value['categoryId'],
     264                            );
     265                        }
     266                    }
     267                } else {
     268                    if (method_exists('\\tistory_writer\\Logger', 'log')) {
     269                        Logger::log("getPostInfoWithTitle, 얻어온 post 반환값 이상");
     270                    }
    193271                }
    194272            }
     
    211289        );
    212290
    213         $response = wp_remote_request($url, array(
    214             'body' => $data,
    215             'output' => 'xml',
    216         ));
    217 
    218         $body = wp_remote_retrieve_body($response);
    219         $xml = simplexml_load_string($body);
     291        $xml = $this->requestPost($url, $data);
     292        if ($xml == null) {
     293            if (method_exists('\\tistory_writer\\Logger', 'log')) {
     294                Logger::log("getPostInfoWithTitle, Request에 실패했습니다.");
     295            }
     296            return null;
     297        }
    220298        return $xml->item->acceptComment;
    221299    }
     
    231309        );
    232310
    233         $response = wp_remote_request($url, array(
    234             'body' => $data,
    235             'output' => 'xml',
    236         ));
    237 
    238         $body = wp_remote_retrieve_body($response);
    239         $xml = simplexml_load_string($body);
     311        $xml = $this->requestPost($url, $data);
     312        if ($xml == null) {
     313            if (method_exists('\\tistory_writer\\Logger', 'log')) {
     314                Logger::log("getPostInfoWithTitle, Request에 실패했습니다.");
     315            }
     316            return null;
     317        }
    240318
    241319        $tags = json_decode(json_encode((array)$xml->item->tags), true);
  • tistorywriter/trunk/readme.txt

    r1859197 r1862235  
    44Tags: content, tistory, development, integration
    55Requires at least: 1.5
    6 Tested up to: 4.1.1
    7 Stable tag: 1.0.1
     6Tested up to: 4.9.5
     7Stable tag: 1.0.2
    88License: MIT License
    99License URI: https://mit-license.org/
     
    4242
    4343== Changelog ==
     44= 1.0.2 =
     45* 포스팅 글 작성 중 자동 저장 시마다 블로그에 글 포스팅되는 결함 해결
     46* 플러그인 내 API 사용 방식 개선
     47
    4448= 1.0.1 =
    4549* 티스토리 연동 문제 수정
  • tistorywriter/trunk/tistorywriter.php

    r1859202 r1862235  
    256256            $postId         = wp_kses_post($_POST['postId']);
    257257
    258             $apiMgr->updatePost($title, $content, $visibility, $category_id, $isProtected, $isAllowComment, $tag, $postId);
     258            if (!wp_is_post_autosave($postId)) {
     259                $apiMgr->updatePost($title, $content, $visibility, $category_id, $isProtected, $isAllowComment, $tag, $postId);
     260            }
    259261        }
    260262    }
Note: See TracChangeset for help on using the changeset viewer.