protected function PerformTest()
 {
     // Create the filter
     $filter = new Filter();
     // We want to include something that wouldn't be included by default
     // for answers - link and exclude something that's included by default - score
     $filter->SetIncludeItems('answer.link')->SetExcludeItems('answer.score');
     // Get the filter's ID
     $filter_id = $filter->GetID();
     // Now make a request for an answer
     $answer = API::Site('stackoverflow')->Answers()->Filter($filter_id)->Exec()->Fetch();
     // Make sure the answer contains / excludes what we specified
     if (!isset($answer['link'])) {
         throw new Exception('"link" missing from response.');
     }
     if (isset($answer['score'])) {
         throw new Exception('"score" included in response but should not be present.');
     }
     // Now lookup the filter by ID
     $filter = new Filter($filter_id);
     $included_items = $filter->GetIncludeItems();
     // Make sure that the included items match what we describe
     if (!in_array('answer.link', $included_items)) {
         throw new Exception('"link" missing from filter description.');
     }
     if (in_array('answer.score', $included_items)) {
         throw new Exception('"score" included in filter description but should not be present.');
     }
 }
 /**
  * \param $title the title for the page
  * \param $equiv_url the equivalent URL on Stack Exchange
  * \param $site the site for the current page
  *
  * Note: it is important to call this method since it also sets a number of
  * special view variables that the main template uses.
  */
 protected function SetPageInfo($title, $equiv_url = null, $site = null)
 {
     // Expose the document root as a view variable
     $this->SetViewVariable('document_root', URLManager::GetDocumentRoot());
     // Provide the view with access to the current page URL
     $this->SetViewVariable('page_url', URLManager::$current_url);
     // If the site is provided, include it in the view variables and substitute
     // the information in some of the other fields.
     if ($site !== null) {
         // Retrieve the site information from the API
         $this->site = API::Site($site)->Info()->Filter(new Filter('!-q8LLJA7'))->Exec()->Fetch();
         // Pass on the site name and API parameter to the views
         $this->SetViewVariable('site', $this->site['site']['api_site_parameter']);
         $this->SetViewVariable('site_name', $this->site['site']['name']);
         $this->SetViewVariable('site_prefix', URLManager::GetDocumentRoot() . '/' . $this->site['site']['api_site_parameter']);
         // Replace '{name}' in page title with the site name
         $title = str_replace('{name}', $this->site['site']['name'], $title);
         // Do the same for the page's equivalent URL
         if ($equiv_url !== null) {
             $equiv_url = str_replace('{url}', $this->site['site']['site_url'], $equiv_url);
         }
     }
     // Set the page title and equivalent URL
     $this->SetViewVariable('page_title', $title);
     if ($equiv_url !== null) {
         $this->SetViewVariable('equiv_url', $equiv_url);
     }
 }
Beispiel #3
0
 public function tags($site, $q)
 {
     $this->SetPageInfo('Search Results for "' . htmlentities($q) . '"', '{url}/search?q=' . urlencode($q), $site);
     // Check for a page number
     $page = $this->GetGETVariable('page', 1);
     // Filter tags by the search string
     $this->SetViewVariable('q', $q);
     $this->SetViewVariable('response', API::Site($site)->Tags()->Inname($q)->Exec()->Page($page));
     $this->SetViewVariable('page', $page);
 }
Beispiel #4
0
 public function view($site, $tag)
 {
     $this->SetPageInfo('Questions tagged "' . htmlentities($tag) . '"', '{url}/questions/tagged/' . urlencode($tag), $site);
     // Check for a page number
     $page = $this->GetGETVariable('page', 1);
     // Attempt to retreive questions with the specified tag
     $this->SetViewVariable('tag', $tag);
     $this->SetViewVariable('response', API::Site($site)->Questions()->Tagged($tag)->Filter('!-psgAvQU')->Exec()->Page($page));
     $this->SetViewVariable('page', $page);
 }
 public function view($site, $id)
 {
     // Begin by attempting to retrieve the question with the specified ID
     $question = API::Site($site)->Questions($id)->Filter('!-)dQB__A07Ku')->Exec()->Fetch();
     if ($question === FALSE) {
         throw new Exception("The question with ID #{$id} does not exist.");
     }
     // Set the page information and question data
     $this->SetPageInfo($question['title'], "{url}/q/{$id}", $site);
     $this->SetViewVariable('question', $question);
 }
Beispiel #6
0
 public function view($site, $id)
 {
     // Fetch the user's profile
     $user = API::Site($site)->Users($id)->Filter('!-psgDpsh')->Exec()->Fetch();
     if ($user === FALSE) {
         throw new Exception("The user with ID #{$id} does not exist.");
     }
     $this->SetPageInfo('User ' . $user['display_name'], '{url}/users/' . $user['user_id'], $site);
     // Now fetch the user's top 5 questions
     $questions = API::Site($site)->Users($id)->Questions()->Filter('!-psgAvQU')->Exec()->Pagesize(5);
     // ...and their top 5 answers
     $answers = API::Site($site)->Users($id)->Answers()->Filter('!9SwXE.JQY')->Exec()->Pagesize(5);
     $this->SetViewVariable('user', $user);
     $this->SetViewVariable('questions', $questions);
     $this->SetViewVariable('answers', $answers);
 }
Beispiel #7
0
 protected function PerformTest()
 {
     // Create the site object
     $superuser = API::Site('superuser');
     // Use the /me route (we have a user request returned)
     $request = $superuser->Me('dummyvalue');
     $this->CompareOutput($request->URL(), 'https://api.stackexchange.com/2.0/me?filter=' . urlencode(Filter::$default_filter) . '&key=' . urlencode(API::$key) . '&site=superuser&access_token=dummyvalue');
     // Get the user's inbox
     $request = $request->Inbox();
     $this->CompareOutput($request->URL(), 'https://api.stackexchange.com/2.0/me/inbox?filter=' . urlencode(Filter::$default_filter) . '&key=' . urlencode(API::$key) . '&site=superuser&access_token=dummyvalue');
     // Create a user request for user number 1
     $request = new UserRequest('serverfault');
     $this->CompareOutput($request->URL(), 'http://api.stackexchange.com/2.0/users?filter=' . urlencode(Filter::$default_filter) . '&key=' . urlencode(API::$key) . '&site=serverfault');
     // Try /me/associated
     $request = $superuser->Me('access_token')->Associated();
     $this->CompareOutput($request->URL(), 'https://api.stackexchange.com/2.0/me/associated?filter=' . urlencode(Filter::$default_filter) . '&key=' . urlencode(API::$key) . '&site=superuser&access_token=access_token');
 }
Beispiel #8
0
 private function TestCache($type, $cache)
 {
     echo "* Testing {$type} cache...\n";
     // Clear the cache just in case
     $cache->Clear();
     // Add two values to the cache - one of which
     // has a TTL value of 1
     $cache->AddToCache('a', 'b', 1);
     $cache->AddToCache('c', 'd');
     // Pause for 2 seconds
     sleep(2);
     if ($cache->RetrieveFromCache('a') !== FALSE) {
         throw new Exception('Value for "a" stored in cache still available after TTL expired.');
     }
     if ($cache->RetrieveFromCache('c') != 'd') {
         throw new Exception('Value for "c" in cache does not match stored value.');
     }
     // Register this cache with the API to ensure that
     // Stack.PHP methods are using it correctly.
     API::SetCache($cache);
     // Get the total number of questions on Stack Apps
     // and then count how many API requests were sent
     API::Site('stackapps')->Questions()->Exec()->Total();
     $num_api_requests = API::GetAPIRequests();
     // Now re-issue the same request and compare the number of API requests sent
     API::Site('stackapps')->Questions()->Exec()->Total();
     if ($num_api_requests != API::GetAPIRequests()) {
         throw new Exception('API response was not retrieved from the cache.');
     }
     // Clear the cache
     $cache->Clear();
     // Now perform the request again and make sure that the number
     // of API requests made increases.
     API::Site('stackapps')->Questions()->Exec()->Total();
     if ($num_api_requests == API::GetAPIRequests()) {
         throw new Exception('There was an error clearing the cache.');
     }
 }
Beispiel #9
0
 protected function PerformTest()
 {
     $au = API::Site('askubuntu');
     $meta = API::Site('meta.askubuntu');
     $this->AnalyzeResponse('/answers', $au->Answers()->Exec());
     // At the current time, one of the posts has a score
     // of 8. It's pretty safe to assume that that won't
     // drop below 6 as we test for here.
     $this->AnalyzeResponse('/answers/{IDs}', $au->Answers(array(2, 10))->Exec(), array('score' => '>6'), 2, 2);
     $this->AnalyzeResponse('/answers/{IDs}/comments', $au->Answers(array(2, 10))->Comments()->Exec(), array(), 6);
     // At the time of this writing, there were 85 badges -
     // this number will not likely decrease.
     $this->AnalyzeResponse('/badges', $au->Badges()->Exec(), array(), 85);
     // The Teacher and Student badge seem to consistently
     // have badges with ID #1 and #2.
     $this->AnalyzeResponse('/badges/{IDs}', $au->Badges(array(1, 2))->Exec(), array(), 2);
     // We are assuming hard numbers for the next two queries
     $this->AnalyzeResponse('/badges/name', $au->Badges()->Name()->Exec(), array('badge_type' => '=="named"'), 71);
     $this->AnalyzeResponse('/badges/recipients', $au->Badges()->Recipients()->Exec(), array(), 50000);
     $this->AnalyzeResponse('/badges/{IDs}/recipients', $au->Badges(1)->Recipients()->Exec(), array('badge_id' => '==1'), 5000);
     $this->AnalyzeResponse('/badges/tags', $au->Badges()->Tags()->Exec(), array('badge_type' => '=="tag_based"'), 14);
     $this->AnalyzeResponse('/comments', $au->Comments()->Exec(), array(), 30);
     // Use the specified comment IDs which have a score
     // of at least 8.
     $this->AnalyzeResponse('/comments/{IDs}', $au->Comments(array(54321, 9498, 32219))->Exec(), array('score' => '>=8'), 3, 3);
     $this->AnalyzeResponse('/errors', API::Errors()->Exec(), array('error_id' => '>=400'), 9);
     // /events would go here but it is an authenticated method
     $this->AnalyzeResponse('/posts', $au->Posts()->SortByCreation()->Ascending()->Exec(), array('creation_date' => '>=1231400875'), 70000);
     $this->AnalyzeResponse('/posts/{IDs}', $au->Posts(array(1, 2))->Exec(), array('creation_date' => '<=1280344530'), 2, 2);
     $this->AnalyzeResponse('/posts/{IDs}/comments', $au->Posts(array(2, 3))->Comments()->Exec(), array(), 6);
     $this->AnalyzeResponse('/posts/{IDs}/revisions', $au->Posts(5)->Revisions()->Exec(), array('post_id' => '==5'), 4);
     $this->AnalyzeResponse('/posts/{IDs}/suggested-edits', $au->Posts(1)->SuggestedEdits()->Exec());
     $this->AnalyzeResponse('/privileges', $au->Privileges()->Exec(), array(), 24);
     $this->AnalyzeResponse('/questions', $au->Questions()->SortByVotes()->Descending()->Min(40)->Exec(), array('score' => '>=40'), 24);
     // Send two question IDs and make sure
     // we get those two back.
     $this->AnalyzeResponse('/questions/{IDs}', $au->Questions(array(30334, 28086))->Exec(), array('score' => '>=40'), 2, 2);
     $this->AnalyzeResponse('/questions/{IDs}/answers', $au->Questions(array(30334))->Answers()->SortByVotes()->Min(16)->Exec(), array('score' => '>=16'), 9);
     // Fetch the comments for the question
     $this->AnalyzeResponse('/questions/{IDs}/comments', $au->Questions(array(6586))->Comments()->Exec(), array('post_id' => '==6586'), 4);
     $this->AnalyzeResponse('/questions/{IDs}/linked', $au->Questions(array(30334))->Linked()->Exec(), array(), 22);
     $this->AnalyzeResponse('/questions/{IDs}/related', $au->Questions(array(30334))->Related()->Exec(), array(), 25);
     $this->AnalyzeResponse('/questions/{IDs}/timeline', $au->Questions(array(30334))->Timeline()->Exec(), array('question_id' => '==30334'), 573);
     // There are more than 2000, but for now just use that
     // as the lower floor
     $this->AnalyzeResponse('/questions/no-answers', $au->Questions()->NoAnswers()->Exec(), array('answer_count' => '==0'), 2000);
     $this->AnalyzeResponse('/questions/unanswered', $au->Questions()->Unanswered()->Exec(), array(), 4000);
     $this->AnalyzeResponse('/revisions/{IDs}', $au->Revisions(array('669b80703cb44546919dfb94a988c809'))->Exec(), array('post_id' => '==2'), 1, 1);
     // Now for the search routes
     $this->AnalyzeResponse('/search', $au->Questions()->Search('install firefox')->Exec(), array(), 10);
     $this->AnalyzeResponse('/similar', $au->Questions()->Similar('i cannot boot ubuntu after installing windows')->Exec(), array(), 24);
     // Assume that all tags have a count >0
     $this->AnalyzeResponse('/tags', $au->Tags()->InName('window')->Exec(), array('count' => '>0'), 10);
     $this->AnalyzeResponse('/tags/{TAGS}/info', $au->Tags('compiz')->Info()->Exec(), array('name' => "=='compiz'", 'count' => '>500', 'is_required' => '==FALSE', 'is_moderator_only' => '==FALSE', 'has_synonyms' => '==TRUE'), 1, 1);
     $this->AnalyzeResponse('/tags/moderator-only', $meta->Tags()->ModeratorOnly()->Exec(), array('is_moderator_only' => '==TRUE'), 10);
     $this->AnalyzeResponse('/tags/required', $meta->Tags()->Required()->Exec(), array('is_required' => '==TRUE'), 3);
     $this->AnalyzeResponse('/tags/synonyms', $au->Tags()->Synonyms()->Exec(), array(), 70);
     $this->AnalyzeResponse('/tags/{TAGS}/related', $au->Tags('compiz')->Related()->Exec(), array(), 30);
     $this->AnalyzeResponse('/tags/{TAGS}/synonyms', $au->Tags('ubuntu-desktop')->Synonyms()->Exec(), array('to_tag' => "=='ubuntu-desktop'"), 1);
     $this->AnalyzeResponse('/tags/{TAG}/top-askers/{PERIOD}', $au->Tags('compiz')->TopAskers(Period::Month)->Exec(), array('score' => '>=0'));
     $this->AnalyzeResponse('/tags/{TAG}/top-answerers/{PERIOD}', $au->Tags('compiz')->TopAnswerers(Period::Month)->Exec(), array('score' => '>=0'));
     $this->AnalyzeResponse('/tags/{TAGS}/wikis', $au->Tags('compiz')->Wikis()->Exec(), array('tag_name' => "=='compiz'"), 1, 1);
     // User methods
     $this->AnalyzeResponse('/users', $au->Users()->SortByReputation()->InName('castro')->Exec(), array(), 1);
     $this->AnalyzeResponse('/users/{IDs}', $au->Users(235)->Exec(), array('user_id' => '==235', 'reputation' => '>15000'), 1, 1);
     $this->AnalyzeResponse('/users/{IDs}/answers', $au->Users(235)->Answers()->Exec(), array(), 443);
     $this->AnalyzeResponse('/users/{IDs}/badges', $au->Users(235)->Badges()->Exec(), array('award_count' => '>0'), 66);
     $this->AnalyzeResponse('/users/{IDs}/comments', $au->Users(235)->Comments()->SortByVotes()->Min(5)->Exec(), array('score' => '>=5'), 13);
     $this->AnalyzeResponse('/users/{IDs}/comments/{ID}', $au->Users(235)->CommentsTo(41)->Exec(), array(), 1);
     // We cannot assume any number of favorites
     // here since a user can modify them at will.
     // We also turn on our filter.
     $filter = new Filter();
     $filter->SetIncludeItems(array('.total', '.type', 'question.favorite_count'));
     $this->AnalyzeResponse('/users/{IDs}/favorites', $au->Users(235)->Favorites()->Filter($filter)->Exec(), array('favorite_count' => '>=1'));
     $this->AnalyzeResponse('/users/{IDs}/mentioned', $au->Users(235)->Mentioned()->Exec(), array(), 247);
     $this->AnalyzeResponse('/users/{ID}/privileges', $au->Users(235)->Privileges()->Exec(), array(), 23);
     $this->AnalyzeResponse('/users/{IDs}/questions', $au->Users(235)->Questions()->SortByVotes()->Min(10)->Exec(), array('score' => '>=10'), 27);
     // We can't assume a minimum here because anyone could answer the question
     // at any given time.
     $this->AnalyzeResponse('/users/{IDs}/questions/no-answers', $au->Users(235)->Questions()->NoAnswers()->Exec(), array('answer_count' => '==0'));
     $this->AnalyzeResponse('/users/{IDs}/questions/unaccepted', $au->Users(235)->Questions()->Unaccepted()->Exec(), array('answer_count' => '>0'));
     $this->AnalyzeResponse('/users/{IDs}/questions/unanswered', $au->Users(235)->Questions()->Unanswered()->Exec());
     $this->AnalyzeResponse('/users/{IDs}/reputation', $au->Users(235)->Reputation()->Exec(), array('user_id' => '==235'));
     // This user should never have any suggested edits
     $this->AnalyzeResponse('/users/{IDs}/suggested-edits', $au->Users(235)->SuggestedEdits()->Exec(), array(), 0, 0);
     $this->AnalyzeResponse('/users/{IDs}/tags', $au->Users(235)->Tags()->Exec(), array('user_id' => '==235'), 390);
     // There will never be more than 30, and right now there are
     // at least one of each.
     $this->AnalyzeResponse('/users/{ID}/tags/{TAGS}/top-answers', $au->Users(235)->Tags('compiz')->TopAnswers()->Exec(), array(), 1, 30);
     $this->AnalyzeResponse('/users/{ID}/tags/{TAGS}/top-questions', $au->Users(235)->Tags('compiz')->TopQuestions()->Exec(), array(), 1, 30);
     $this->AnalyzeResponse('/users/{IDs}/timeline', $au->Users(235)->Timeline()->Exec(), array('user_id' => '==235'), 7907);
     $this->AnalyzeResponse('/users/{ID}/top-answer-tags', $au->Users(235)->TopAnswerTags()->Exec(), array(), 1);
     $this->AnalyzeResponse('/users/{ID}/top-question-tags', $au->Users(235)->TopQuestionTags()->Exec(), array(), 1);
     $this->AnalyzeResponse('/users/moderators', $au->Users()->Moderators()->Exec(), array('user_type' => "=='moderator'"), 21);
     $this->AnalyzeResponse('/users/moderators/elected', $au->Users()->Moderators()->Elected()->Exec(), array('user_type' => "=='moderator'", 'is_employee' => '==FALSE'), 6);
     // We skip the /inbox method - it requires authentication
     // The /error route
     try {
         API::Errors(ErrorCode::Offline);
     } catch (APIException $e) {
         if ($e->ErrorCode() != ErrorCode::Offline) {
             throw new Exception('The error returned did match the error requested.');
         }
     }
 }
<?php

// Demonstrates the proper way to process errors when they
// occur while using Stack.PHP.
require_once 'config.php';
// We begin by creating a Site object for Stack Overflow.
$site = API::Site('stackoverflow');
// Now in order to demonstrate error handling technique, we
// will intentionally generate an error in the API by passing
// an invalid "number" to the /users/{ID} route.
$response = $site->Users('bob')->Exec();
// Nothing up until this point will throw an exception. But
// as soon as we begin fetching objects, the API may throw
// an exception as well as Stack.PHP's internal classes (like
// the cache). Therefore we wrap the following code in a
// try ... catch block.
try {
    // This will fail!
    $item = $response->Fetch();
} catch (APIException $e) {
    // We have lots of interesting information inside
    // $e which we can display below.
    $exception = $e;
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Stack.PHP - Error Handling</title>
  <link rel='stylesheet' type='text/css' href='../common/style.css' />
Beispiel #11
0
				<thead>
					<tr>
						<th>#</th>
						<th>Select</th>
						<th>Title</th>
						<th>Q_ID</th>
						<th>Q_link</th>
						<th>A_ID</th>
						<th>Score</th>
						<th>View count</th>
					</tr>
				</thead>
				<tbody id='table_body'>

					<?php 
$stackoverflow = API::Site('stackoverflow');
$QA_pairs = $stackoverflow->Questions()->Tagged(array('python', 'regex'))->Filter('!)Ef*FHQMIC8-*UcQg1daeoq0Al1KPcf(_m55tBjjW)5bOLFxS')->SortByVotes()->Exec();
$QA_pair_data = array();
$i = 0;
try {
    $QA_pair = $QA_pairs->Fetch();
} catch (Exception $e) {
    echo "<tr><td>" . $e . "</td></tr>";
}
while ($QA_pair and $i < 5) {
    try {
        //only use a question if there is an answer which has been selected as correct
        if (array_key_exists('accepted_answer_id', $QA_pair)) {
            $A_accepted = $stackoverflow->Answers($QA_pair['accepted_answer_id'])->Filter('!)Q29lpdRHRpfMsqolBGb(B2q')->Exec()->Fetch();
            echo "<tr>";
            echo "<td>" . $i . "</td>";
Beispiel #12
0
  <title>Stack.PHP - User List</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel='stylesheet' type='text/css' href='../common/style.css' />
</head>
<body>
  <form id='form'>
    <div id='site_selector'>
      <b>Select a Stack Exchange site:</b>
      <?php 
echo $site_html;
?>
      <input type="submit" value="Go" />
    </div>
    <?php 
if (isset($_GET['site'])) {
    $site = API::Site($_GET['site']);
    $request = $site->Users();
    if (isset($_GET['sort'])) {
        $request->SortBy($_GET['sort']);
    }
    if (isset($_GET['order']) && $_GET['order'] == 'asc') {
        $request->Ascending();
    } else {
        $request->Descending();
    }
    $response = $request->Exec();
    $table = OutputHelper::CreateTable($response);
    $table->SetSortImages('../common/sort_asc.png', '../common/sort_desc.png');
    // Create the method that will display the user's name with a link
    // (In PHP 5.3, we can just embed the function as a parameter to AddColumn)
    function DisplayUsername($item)
Beispiel #13
0
        $wpdb->query($sql2);
    }
}
/**
 * Cron job executor
 */
if (!empty($_POST['post_now'])) {
    function autoposting_exec($args = null)
    {
        global $wpdb, $table_prefix;
        $table_name = $table_prefix . 'autoposting_campaigns';
        // need to get the StackApps app id
        if (!isset($args['cron'])) {
            global $wp_rewrite;
            if (!is_object($wp_rewrite)) {
                $wp_rewrite = new WP_Rewrite();
            }
            do_action('init');
        }
        $app_id = $args['app_id'];
        if (empty($app_id)) {
            $app_id = $_POST['autoposting_stack_id'];
        }
        if (empty($app_id)) {
            return;
        }
        $campaign_id = $args['campaign_id'];
        $campaign = $wpdb->get_results("select * from " . $table_name . " where id='" . $args['campaign_id'] . "'");
        $campaign = $campaign[0];
        $table_name = $table_prefix . 'autoposting_status';
        $task_details = $wpdb->get_results("select * from " . $table_name . " where campaign_id='" . $args['campaign_id'] . "'");
        //Stack Exchange Fetching Questions
        require_once 'config.php';
        $site = API::Site($campaign->site);
        $request = $site->SearchAdvanced($campaign->search_term);
        $table_name = $table_prefix . 'autoposting_campaigns';
        $count = $wpdb->get_results("select * from " . $table_name . " where id='" . $campaign_id . "'");
        $x = $count['0']->page_count;
        $question = $request->Exec()->Pagesize((int) $_POST['posts'][$campaign_id])->Page($x);
        $x = $x + 5;
        $wpdb->query("UPDATE " . $table_name . " SET page_count=" . $x . " WHERE id='" . $campaign_id . "'");
        while ($questions = $question->Fetch(FALSE)) {
            // insert user
            $user_login = apply_filters('pre_user_login', $questions['owner']['display_name']);
            $user_login = preg_replace('/[^a-zA-Z0-9_]+/', '', $user_login);
            if (empty($user_login) || trim($user_login) == '') {
                $user_login = '******';
            }
            $tmp = $wpdb->get_results("select * from " . $wpdb->users . " where 1 and user_login='******'");
            if (empty($tmp)) {
                $userdata = array('user_pass' => wp_hash_password('wrandnswers+x971' . microtime()), 'user_login' => $user_login, 'display_name' => !empty($questions['owner']['display_name']) ? $questions['owner']['display_name'] : $user_login, 'user_url' => '', 'user_email' => strtolower($questions['owner']['display_name']) . '@email.null');
                $wpuserid = wp_insert_user($userdata);
            } else {
                $wpuserid = $tmp[0]->ID;
            }
            $post_statuses = array('1' => 'publish', '2' => 'pending', '3' => 'draft');
            $post = array('comment_status' => 'open', 'ping_status' => 'open', 'post_author' => $wpuserid, 'post_content' => $questions['body'], 'post_status' => $post_statuses[$campaign->post_status], 'post_title' => $questions['title'], 'post_type' => 'question');
            global $up_options;
            if (empty($up_options)) {
                $up_options = new stdClass();
            }
            if (!empty($_POST['points_per_question'])) {
                $up_options->points_per_question = $_POST['points_per_question'];
            }
            $id = wp_insert_post($post);
            // set question category
            wp_set_object_terms($id, array((int) $campaign->category_id), 'question_category');
            // handle comments
            // Stack Exchange retrieving Answers for Questions.
            $sitea = API::Site($campaign->site);
            $answers = array();
            $answer = $sitea->Questions($questions['question_id'])->Answers()->Exec();
            while ($answers = $answer->Fetch(TRUE)) {
                $data = array('comment_post_ID' => $id, 'comment_author' => $answers['owner']['display_name'], 'comment_author_email' => $answers['owner']['display_name'] . '@email.null', 'comment_content' => $answers['body'], 'comment_type' => '', 'comment_parent' => 0, 'comment_author_IP' => '127.0.0.1', 'comment_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)', 'comment_date' => $answers['creation_date'], 'comment_approved' => 1);
                $commentID = wp_insert_comment($data);
                if ($questions['owner']['user_id'] == $answers['owner']['user_id']) {
                    // set as best answer
Beispiel #14
0
      <input type="submit" value="Go" />
    </div>
    <?php 
if (isset($_GET['site'])) {
    ?>
        <div>
            <b>User:</b>
            <?php 
    echo OutputHelper::DisplayUserSelector('user_id', $_GET['site'], isset($_GET['user_id']) ? $_GET['user_id'] : '');
    ?>
            | <input type="submit" value="Go" />
        </div><br />
        <?php 
    if (isset($_GET['user_id']) && $_GET['user_id'] != '') {
        // Retrieve the user's account
        $user = API::Site($_GET['site'])->Users($_GET['user_id']);
        $user_data = $user->Exec()->Fetch();
        if ($user_data === FALSE) {
            echo '<pre>Error: the supplied user_id parameter is invalid.</pre>';
        } else {
            ?>
                <hr /><br />
                <div class='user-profile'>
                  <div class='gravatar'>
                    <img src='<?php 
            echo $user_data['profile_image'];
            ?>
&s=64' />
                  </div>
                  <b>Username:</b> <?php 
            echo $user_data['display_name'];