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 ) { self::set_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?' . $_SERVER['QUERY_STRING']; $authorization_url = $api->getAuthorizationUrl( $return_url ); wp_redirect($authorization_url); return false; } catch ( ArkliApiException $e ) { self::set_post_meta( $post_id, ArkliPluginCommon::PostMetaError, 'Probably api key or api secret is invalid.' ); wp_redirect($redirect_url); return false; } } // just for case return true; } protected static function _prepare() { 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 false; } } $access_data = get_option(ArkliPluginCommon::AccessData); if ( ( ! $access_data['key'] ) || ( ! $access_data['secret'] ) ) { if ( ! self::_authorize( $post->ID, $redirect_url ) ) { return false; } } $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 false; } } else { self::set_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage() ); wp_redirect($redirect_url); return false; } } if ( ! $access_data['channel_id'] ) { try { $channel = $api->getBlogChannelId( home_url() ); } catch ( ArkliApiException $e ) { self::set_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage() ); wp_redirect($redirect_url); return false; } 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() . ').'; self::set_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $message ); wp_redirect($redirect_url); return false; } } return array( $api, $post, $access_data, $redirect_url ); } public static function _create_post($api, $post, $access_data, $campaign_id) { $categories = wp_get_post_categories($post->ID); $tags = join( ', ', array_map( create_function('$t', 'return $t->name;'), wp_get_post_tags($post->ID) ) ); $params = array( 'title' => $post->post_title, 'text' => nl2br($post->post_content), 'tags' => $tags, 'categories' => (count($categories) ? $categories[0] : ''), 'origPostId' => $post->ID, ); $data = get_post_meta($post->ID, ArkliPluginCommon::PostMetaData, true); $force_post_at = false; if ( ( ! $data ) && $campaign_id ) { $data = array( 'campaignId' => $campaign_id, ); $force_post_at = true; } if ($data) { try { $result = $api->getCampaigns(null, $data['campaignId']); } catch ( ArkliApiException $e ) { self::set_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage() ); return; } if (count($result)) { $params += $data; } else { $data = null; } } if ( ! $data ) { $params += array( 'campaignName' => $post->post_title, 'campaignTags' => $tags, ); } try { if ( $data && ( ! $force_post_at ) ) { $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); self::set_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, '' ); self::set_post_meta( $post->ID, ArkliPluginCommon::PostMetaData, $result ); } catch ( ArkliApiException $e ) { self::set_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage() ); } } protected static function _select_campaign($api, $post, $access_data) { try { $result = $api->getCampaigns(true); } catch ( ArkliApiException $e ) { self::set_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, $e->getMessage() ); return; } self::set_post_meta( $post->ID, ArkliPluginCommon::PostMetaCampaigns, $result ); if ( count($result) ) { self::set_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, '' ); } else { self::set_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, "You didn't have existing campaigns in Arkli." ); } } public static function init() { $data = self::_prepare(); if ( ! $data ) { return; } list( $api, $post, $access_data, $redirect_url ) = $data; $mode = ( isset( $_REQUEST['mode'] ) ? $_REQUEST['mode'] : '' ); if ( $mode == 'create_new' || $mode == 'update' ) { self::_create_post( $api, $post, $access_data ); } elseif ( $mode == 'create_existing' ) { $campaign_id = ( isset( $_REQUEST['campaign_id'] ) ? $_REQUEST['campaign_id'] : '' ); self::_create_post( $api, $post, $access_data, $campaign_id ); } elseif ( $mode == 'select') { self::_select_campaign( $api, $post, $access_data ); } else { self::set_post_meta( $post->ID, ArkliPluginCommon::PostMetaError, 'Internal error: create mode missing' ); } wp_redirect($redirect_url); } } ArkliPluginCreate::init();