function share($post) { if (self::debug()) { self::log(sprintf("share(%s)", is_object($post) ? $post->post_title : $post)); } if (!is_object($post)) { $post = get_post($post); } if (!in_array($post->post_type, self::supported_post_types())) { return false; } $posted = $error = false; if ($meta = $this->can_post_on_facebook($post)) { // determine if this should be delayed if ($meta['delay_length']) { self::log("Sharing of this post has been delayed {$meta['delay_length']} {$meta['delay_unit']}({$post->ID})"); $time = strtotime("+{$meta['delay_length']} {$meta['delay_unit']}", current_time('timestamp')); update_post_meta($post->ID, self::META_SCHEDULED, $time); $meta['delay_length'] = 0; update_post_meta($post->ID, self::META, $meta); return false; } if (!empty($meta['append_link'])) { if ($meta['message']) { $meta['message'] .= ' - '; } $meta['message'] .= $this->get_permalink($post->ID); } try { // flush the fb cache if (apply_filters('sp_auto_flush_fb', true)) { $poke = self::api('/', 'POST', array('id' => $meta['link'], 'scrape' => 'true')); } // no targets? error. if (!$meta['targets'] && !self::is_business()) { throw new Exception("No publishing Targets selected."); } // first, should we post to the wall? if (self::is_business() || in_array('wall', $meta['targets'])) { $result = self::api(self::me('id') . '/links', 'POST', array('name' => $meta['name'], 'message' => $meta['message'], 'description' => $meta['description'], 'picture' => $meta['picture'], 'link' => $this->get_permalink($post->ID))); self::log(sprintf("posted to the wall: %s", serialize($result))); // store the ID and published date for queuing $result['published'] = time(); $result['message'] = $meta['message']; add_post_meta($post->ID, Sharepress::META_RESULT, $result); } // next, fire the sharepress_post action // the pro version picks this up do_action('sharepress_post', $meta, $post); $this->success($post, $meta); delete_post_meta($post->ID, self::META_SCHEDULED); $posted = true; } catch (Exception $e) { self::err(sprintf("Exception thrown while in share: %s", print_r($e->getResult(), true))); $this->error($post, $meta, $e); $error = true; } } if ($twitter_meta = $this->can_post_on_twitter($post)) { $client = new SharePress_TwitterClient(get_option(self::OPTION_SETTINGS)); $tweet = sprintf('%s %s', $post->post_title, $this->get_bitly_link($post)); if ($hash_tag = trim($twitter_meta['hash_tag'])) { $tweet .= ' ' . $hash_tag; } $result = $client->post($tweet); SharePress::log(sprintf("Tweet Result for Post #{$post->ID}: %s", json_encode($result))); add_post_meta($post->ID, Sharepress::META_TWITTER_RESULT, $result); $posted = true; } if ($posted && !$error) { // success: update_post_meta($post->ID, self::META_POSTED, gmdate('Y-m-d H:i:s')); } }
public function getUserAccessToken($read_only = false) { // first, consider using the stored access token, // so long as the session storage is not $_SESSION if (!$this->use_session && ($access_token = $this->getPersistentData('access_token'))) { if (class_exists('Sharepress')) { SharePress::log("Using stored access token: {$access_token}"); } return $access_token; } else { if (class_exists('Sharepress')) { if ($this->use_session) { SharePress::log(sprintf('Facebook SDK is in session mode - not using stored access token. %s %s', json_encode($_SESSION), json_encode($_REQUEST)), 'WARN'); } else { // SharePress::log('No access token on file.', 'WARN'); } } } if ($read_only) { return; } // first, consider a signed request if it's supplied. // if there is a signed request, then it alone determines // the access token. $signed_request = $this->getSignedRequest(); if ($signed_request) { // apps.facebook.com hands the access_token in the signed_request if (array_key_exists('oauth_token', $signed_request)) { $access_token = $signed_request['oauth_token']; $this->setPersistentData('access_token', $access_token); return $access_token; } // the JS SDK puts a code in with the redirect_uri of '' if (array_key_exists('code', $signed_request)) { $code = $signed_request['code']; $access_token = $this->getAccessTokenFromCode($code, ''); if ($access_token) { $this->setPersistentData('code', $code); $this->setPersistentData('access_token', $access_token); return $access_token; } } // signed request states there's no access token, so anything // stored should be cleared. $this->clearAllPersistentData(); return false; // respect the signed request's data, even // if there's an authorization code or something else } $code = $this->getCode(); if ($code && $code != $this->getPersistentData('code')) { $access_token = $this->getAccessTokenFromCode($code); if ($access_token) { $this->setPersistentData('code', $code); $this->setPersistentData('access_token', $access_token); return $access_token; } // code was bogus, so everything based on it should be invalidated. $this->clearAllPersistentData(); return false; } // as a fallback, just return whatever is in the persistent // store, knowing nothing explicit (signed request, authorization // code, etc.) was present to shadow it (or we saw a code in $_REQUEST, // but it's the same as what's in the persistent store) return $this->getPersistentData('access_token'); }