Plugin Directory

Changeset 2483930


Ignore:
Timestamp:
03/02/2021 12:45:03 AM (5 years ago)
Author:
shorthandconnect
Message:

Updated API handling and php tag syntax

Location:
shorthand-connect/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • shorthand-connect/trunk/README.txt

    r2375587 r2483930  
    55Requires at least: 4
    66Tested up to: 5.0.3
    7 Stable tag: 1.3.6
     7Stable tag: 1.3.7
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • shorthand-connect/trunk/config.default.php

    r2026714 r2483930  
    1212    $noabstract = false;
    1313}
    14 
    15 ?>
  • shorthand-connect/trunk/includes/api-v2.php

    r2009514 r2483930  
    33// VERSION 2 API
    44
    5 function sh_get_profile() {
    6 
     5function sh_v2_api_get($url, $options) {
    76    global $serverv2URL;
    87    $token = get_option('sh_v2_token');
    98
    10     $tokeninfo = array();
     9    if (!$token) {
     10        return false;
     11    }
    1112
    12     if($token) {
    13         $url = $serverv2URL.'/v2/token-info';
    14         $response = wp_remote_get( $url, array(
    15             headers => array(
     13    $url = $serverv2URL.$url;
     14    $request_options = array_merge(
     15        array(
     16            'headers' => array(
    1617                'Authorization' => 'Token '.$token,
    1718                'user-agent'  =>  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8) AppleWebKit/535.6.2 (KHTML, like Gecko) Version/5.2 Safari/535.6.2',
    1819            )
    19         ) );
    20         $body = wp_remote_retrieve_body( $response );
    21         $data = json_decode($body);
    22         if(isset($data) && isset($data->name)) {
    23             $tokeninfo['username'] = $data->name . ' ('.$data->token_type.' Token)';
    24             $tokeninfo['gravatar'] = $data->logo;
    25             $tokeninfo = (object)$tokeninfo;
    26         }
     20        ),
     21        $options
     22    );
     23
     24    return wp_remote_get($url, $request_options);
     25}
     26
     27function sh_v2_api_get_json($url, $options) {
     28    $response = sh_v2_api_get($url, $options);
     29    $body = wp_remote_retrieve_body($response);
     30    return json_decode($body);
     31}
     32
     33function sh_get_profile() {
     34    $tokeninfo = array();
     35
     36    $data = sh_v2_api_get_json('/v2/token-info', array());
     37    if ($data && isset($data->name)) {
     38        $tokeninfo['username'] = $data->name . ' ('.$data->token_type.' Token)';
     39        $tokeninfo['gravatar'] = $data->logo;
     40        $tokeninfo = (object)$tokeninfo;
    2741    }
    2842
     
    3145
    3246function sh_get_stories() {
    33     global $serverv2URL;
    34     $token = get_option('sh_v2_token');
    35 
    36     $valid_token = true;
    37 
    3847    $stories = null;
    3948
    40     //Attempt to connect to the server
    41     if($token) {
    42         $url = $serverv2URL.'/v2/stories';
    43         $response = wp_remote_get( $url, array(
    44             'headers' => array(
    45                 'Authorization' => 'Token '.$token,
    46                 'user-agent'  =>  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8) AppleWebKit/535.6.2 (KHTML, like Gecko) Version/5.2 Safari/535.6.2',
    47             ),
    48             'timeout' => '240'
    49         ) );
    50         $body = wp_remote_retrieve_body( $response );
    51         $data = json_decode($body);
    52         if(isset($data)) {
    53             $stories = array();
    54             $valid_token = true;
    55             //Something went wrong
    56             if ($data->status) {
    57                 return null;
    58             }
    59             foreach($data as $storydata) {
    60                 $story = array(
    61                     'image' => $storydata->cover,
    62                     'id' => $storydata->id,
    63                     'metadata' => (object)array(
    64                         'description' => $storydata->description
    65                     ),
    66                     'title' => $storydata->title,
    67                     'story_version' => ''.$storydata->version
    68                 );
    69                 $stories[] = (object)$story;
    70             }
     49    $data = sh_v2_api_get_json('/v2/stories', array('timeout' => '240'));
     50    if($data) {
     51        $stories = array();
     52        //Something went wrong
     53        if ($data->status) {
     54            return null;
     55        }
     56        foreach($data as $storydata) {
     57            $story = array(
     58                'image' => $storydata->cover,
     59                'id' => $storydata->id,
     60                'metadata' => (object)array(
     61                    'description' => $storydata->description
     62                ),
     63                'title' => $storydata->title,
     64                'story_version' => ''.$storydata->version
     65            );
     66            $stories[] = (object)$story;
    7167        }
    7268    }
     69   
    7370    return $stories;
    7471}
     
    10299    $destination_path = $destination['path'].'/shorthand/'.$post_id;
    103100
    104     global $serverv2URL;
    105     $token = get_option('sh_v2_token');
    106 
    107     $valid_token = false;
    108 
    109101    $story = array();
    110102
    111103    //Attempt to connect to the server
    112     if($token) {
    113         $url = $serverv2URL.'/v2/stories/'.$story_id;
    114         $zipfile = tempnam($tmpdir, 'sh_zip');
    115         $unzipdir = tempnam($tmpdir, 'sh_unzip').'_dir';
    116         $response = wp_remote_get( $url, array(
    117             'headers' => array(
    118                 'Authorization' => 'Token '.$token,
    119                 'user-agent'  =>  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8) AppleWebKit/535.6.2 (KHTML, like Gecko) Version/5.2 Safari/535.6.2',
    120             ),
    121             'timeout' => '600',
    122             'stream' => true,
    123             'filename' => $zipfile
    124         ) );
    125         if($response['response']['code'] == 200) {
    126             $unzipfile = unzip_file( $zipfile, $unzipdir);
    127                 if ( $unzipfile == 1 ) {
    128                     wp_mkdir_p($destination_path.'/'.$story_id);
    129                     $err = copy_dir($unzipdir, $destination_path.'/'.$story_id);
    130                     $story['path'] = $destination_path.'/'.$story_id;
    131                 } else {
    132                     $story['error'] = array(
     104    $zipfile = tempnam($tmpdir, 'sh_zip');
     105    $unzipdir = tempnam($tmpdir, 'sh_unzip').'_dir';
     106    $response = sh_v2_api_get('/v2/stories/'.$story_id, array(
     107        'timeout' => '600',
     108        'stream' => true,
     109        'filename' => $zipfile
     110    ));
     111    if($response['response']['code'] == 200) {
     112        $unzipfile = unzip_file( $zipfile, $unzipdir);
     113            if ( $unzipfile == 1 ) {
     114                wp_mkdir_p($destination_path.'/'.$story_id);
     115                $err = copy_dir($unzipdir, $destination_path.'/'.$story_id);
     116                $story['path'] = $destination_path.'/'.$story_id;
     117            } else {
     118                $story['error'] = array(
    133119                    'pretty' => 'Could not unzip file'
    134120                );
    135                 }
    136         } else {
    137             $story['error'] = array(
    138                 'pretty' => 'Could not get zip file',
    139                 'error' => curl_error($ch),
    140                 'response' => $response
    141             );
    142         }
     121            }
     122    } else {
     123        $story['error'] = array(
     124            'pretty' => 'Could not get zip file',
     125            'response' => $response
     126        );
    143127    }
     128
    144129    return $story;
    145130}
    146 
    147 ?>
  • shorthand-connect/trunk/includes/api.php

    r2009514 r2483930  
    138138    return $story;
    139139}
    140 
    141 ?>
  • shorthand-connect/trunk/shorthand_connect.php

    r2375587 r2483930  
    33/**
    44 * @package Shorthand Connect
    5  * @version 1.3.6
     5 * @version 1.3.7
    66 */
    77/*
     
    1010Description: Import your Shorthand stories into your Wordpress CMS as simply as possible - magic!
    1111Author: Shorthand
    12 Version: 1.3.6
     12Version: 1.3.7
    1313Author URI: http://shorthand.com
    1414*/
  • shorthand-connect/trunk/templates/abstract.php

    r1522745 r2483930  
    99    return $abstract;
    1010}
    11 
    12 ?>
Note: See TracChangeset for help on using the changeset viewer.