Plugin Directory

Changeset 445382


Ignore:
Timestamp:
09/29/2011 06:58:25 PM (15 years ago)
Author:
sebstein
Message:
Location:
g-crossposting/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • g-crossposting/trunk/admin.php

    r445011 r445382  
    174174        $new_input['apikey'] = '';
    175175        g_crossposting_add_error('g_crossposting_err_apikey', __("You provided an invalid Google API key."));
     176    }
     177
     178    // if API key and Google+ are given, check if they are valid
     179    if (! empty($new_input['gplusid']) && ! empty($new_input['apikey'])) {
     180        if (g_crossposting_api_activities_list($new_input['gplusid'], $new_input['apikey'], 1) == null) {
     181            // at least one of both must be wrong
     182            $new_input['gplusid'] = '';
     183            $new_input['apikey'] = '';
     184        }
    176185    }
    177186
  • g-crossposting/trunk/gplus-crosspost.php

    r445367 r445382  
    148148
    149149    if (! g_crossposting_check_gplusid($options['gplusid'])) {
     150        $options['gplusid'] = '';
     151        update_option('g_crossposting_options', $options);
    150152        return FALSE;
    151153    }
    152154
    153155    if (! g_crossposting_check_apikey($options['apikey'])) {
    154         return FALSE;
    155     }
    156 
     156        $options['apikey'] = '';
     157        update_option('g_crossposting_options', $options);
     158        return FALSE;
     159    }
     160
     161    // basic validation showed that we might have valid connection settings
     162    // now do an actual connect to see if we are able to connect
     163    if (g_crossposting_api_activities_list($options['gplusid'], $options['apikey'], 1) == null) {
     164        // in the end we can't say which of both settings is wrong
     165        // so we reset both
     166        $options['gplusid'] = '';
     167        $options['apikey'] = '';
     168        update_option('g_crossposting_options', $options);
     169
     170        return FALSE;
     171    }
     172
     173    // great, we are able to connect to the API
     174    //
     175    // go on validating remaining settings
    157176    if (! g_crossposting_check_maxactivities($options['maxactivities'])) {
    158177        return FALSE;
     
    179198
    180199/**
    181  *  returns new activities not yet posted on blog
    182  *
    183  *  @return array of new activities or null if no new activities were
    184  *          found on Google+
    185  */
    186 function g_crossposting_get_new_activities() {
    187     $options = get_option('g_crossposting_options');
    188     $last_activity_id = get_option('g_crossposting_last_activity_id', null);
    189 
    190     // get list of 100 recent activities
    191     $ch = curl_init("https://www.googleapis.com/plus/v1/people/{$options['gplusid']}/activities/public?alt=json&maxResults={$options['maxactivities']}&key={$options['apikey']}");
     200 *  query Google+ API activities.list function
     201 *
     202 *  @return array with activities or null
     203 */
     204function g_crossposting_api_activities_list($p_gplusid, $p_apikey, $p_maxactivities) {
     205    // get list of latest p_maxactivities activities
     206    $ch = curl_init("https://www.googleapis.com/plus/v1/people/{$p_gplusid}/activities/public?alt=json&maxResults={$p_maxactivities}&key={$p_apikey}");
    192207    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    193208    $activities = curl_exec($ch);
    194209    curl_close($ch);
    195210    $activities = json_decode($activities);
     211
     212    // check if we got errors
     213    if ($activities->error) {
     214        return null;
     215    }
     216
     217    // looks fine, return activities
     218    return $activities;
     219}
     220
     221/**
     222 *  returns new activities not yet posted on blog
     223 *
     224 *  @return array of new activities or null if no new activities were
     225 *          found on Google+
     226 */
     227function g_crossposting_get_new_activities() {
     228    // load options
     229    $options = get_option('g_crossposting_options');
     230    $last_activity_id = get_option('g_crossposting_last_activity_id', null);
     231
     232    // get activities from Google+
     233    $activities = g_crossposting_api_activities_list($options['gplusid'],
     234                            $options['apikey'], $options['maxactivities']);
     235    if ($activities == null) {
     236        return null;
     237    }
    196238
    197239    // go through activities to see if we got some not posted yet
  • g-crossposting/trunk/readme.txt

    r445367 r445382  
    6262= 1.0.1 =
    6363* fix: correctly validate Google API key as it may also contain a dash
     64* fix: don't produce PHP warning if Google API is not available for some reason
    6465
    6566= 1.0 =
Note: See TracChangeset for help on using the changeset viewer.