fetchAccessToken( $_GET['oauth_token'], $_GET['oauth_verifier'] ); update_option( ArkliPluginCommon::AccessData, array( 'key' => $accessToken->key, 'secret' => $accessToken->secret, 'channel_id' => 0, 'channel_name' => '', ) ); return true; } catch ( ArkliApiException $e ) { if ( ! add_post_meta( $post_id, ArkliPluginCommon::PostMetaError, $e->getMessage(), true ) ) { update_post_meta( $post_id, ArkliPluginCommon::PostMetaError, $e->getMessage() ); } wp_redirect($redirect_url); return false; } } elseif ( ( ! isset( $_GET['oauth_token'] ) ) || ( ! isset( $_GET['oauth_token_secret'] ) ) ) { try { update_option( ArkliPluginCommon::AccessData, array( 'key' => '', 'secret' => '', 'channel_id' => 0, 'channel_name' => '', ) ); $return_url = WP_PLUGIN_URL . '/arkli/create.php?post_id=' . $post_id . '&nonce=' . $_REQUEST['nonce']; $authorization_url = $api->getAuthorizationUrl( $return_url ); wp_redirect($authorization_url); return false; } catch ( ArkliApiException $e ) { $message = 'Probably api key or api secret is invalid.'; if ( ! add_post_meta( $post_id, ArkliPluginCommon::PostMetaError, $message, true ) ) { update_post_meta( $post_id, ArkliPluginCommon::PostMetaError, $message ); } wp_redirect($redirect_url); return false; } } // just for case return true; } public static function init() { ArkliPluginCommon::check_permissions(); $post_id = ( isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : '' ); if ( ! $post_id ) { ArkliPluginCommon::show_error(); } ArkliPluginCommon::check_nonce("arkli-plugin-post-{$post_id}"); $post = get_post($post_id); if ( ! $post ) { ArkliPluginCommon::show_error(); } $redirect_url = admin_url('post.php') . "?post={$post_id}&action=edit"; if ( isset( $_GET['oauth_token'] ) && isset( $_GET['oauth_verifier'] ) ) { if ( ! self::authorize( $post->ID, $redirect_url ) ) { return; } } $access_data = get_option(ArkliPluginCommon::AccessData); if ( ( ! $access_data['key'] ) || ( ! $access_data['secret'] ) ) { if ( ! self::authorize( $post->ID, $redirect_url ) ) { return; } } $api = ArkliPluginCommon::get_api($access_data); try { // make test call to api, and ensure that user is authorized $api->getMoo(); } catch ( ArkliApiException $e ) { if ( $e->getCode() == ArkliApiException::ErrorCodeOAuth || strpos($e->getMessage(), 'Invalid access token: ') === 0 ) { if ( ! self::authorize( $post->ID, $redirect_url ) ) { return; } } else { if ( ! add_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage(), true ) ) { update_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage() ); } wp_redirect($redirect_url); return; } } if ( ! $access_data['channel_id'] ) { try { $channel = $api->getBlogChannelId( home_url() ); } catch ( ArkliApiException $e ) { if ( ! add_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage(), true ) ) { update_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage() ); } wp_redirect($redirect_url); return; } if ($channel) { $access_data['channel_id'] = $channel['id']; $access_data['channel_name'] = $channel['name']; update_option( ArkliPluginCommon::AccessData, $access_data ); } else { $message = 'No channel selected — please sign in to Arkli and add your blog (' . home_url() . ').'; if ( ! add_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $message, true ) ) { update_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $message ); } wp_redirect($redirect_url); return; } } $categories = wp_get_post_categories($post_id); $tags = join( ', ', array_map( create_function('$t', 'return $t->name;'), wp_get_post_tags($post_id) ) ); $data = get_post_meta($post->ID, ArkliPluginCommon::PostMetaData, true); $params = array( 'title' => $post->post_title, 'text' => nl2br($post->post_content), 'tags' => $tags, 'categories' => (count($categories) ? $categories[0] : ''), 'origPostId' => $post->ID, ); if ($data) { try { $result = $api->getCampaigns(null, $data['campaignId']); } catch ( ArkliApiException $e ) { if ( ! add_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage(), true ) ) { update_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage() ); } wp_redirect($redirect_url); return; } if (count($result)) { $params += $data; } else { $data = null; } } if (!$data) { $params += array( 'campaignName' => $post->post_title, 'campaignTags' => $tags, ); } try { if ($data) { $post_at = null; } elseif ( strtotime($post->post_date_gmt) > 0 ) { $post_at = preg_replace('/:\d\d$/', ':00', $post->post_date_gmt); } else { $post_at = strtotime('+1 year'); } $result = $api->createPost($access_data['channel_id'], $post_at, $params); if ( ! add_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, '', true ) ) { update_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, '' ); } if ( ! add_post_meta( $post->ID, ArkliPluginCommon::PostMetaData, $result, true ) ) { update_post_meta( $post->ID, ArkliPluginCommon::PostMetaData, $result ); } } catch ( ArkliApiException $e ) { if ( ! add_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage(), true ) ) { update_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage() ); } } wp_redirect($redirect_url); } } ArkliPluginCreate::init();