plugin_path = $path;
$this->plugin_url = $url;
}
public function get_post_meta()
{
global $post;
return array(
'se_site' => get_post_meta( $post->ID, 'se_site', true ),
'user_id' => get_post_meta( $post->ID, 'se_user_id', true ),
'disable_cache' => get_post_meta( $post->ID, 'se_cached', true ),
'per_page' => get_post_meta( $post->ID, 'se_per_page', true ),
'q_or_a' => get_post_meta( $post->ID, 'se_post_type', true ),
'sort_order' => get_post_meta( $post->ID, 'se_sort_order', true ),
'referrer' => get_post_meta( $post->ID, 'se_referrer_id', true )
);
}
public function get_profile( $user, $showing_type, $site, $total )
{
$reputation = $this->count_format($user['reputation']);
$total_posts = number_format($total, 0, ',', '.');
$badges = $this->get_badges( $user );
$rep_text = __( 'reputation', 'aysp' );
$html = <<
{$user['display_name']}
$badges$reputation $rep_text
HTML;
return $html;
}
public function get_badges( $user )
{
$badges = $gold = $silver = $bronze = '';
if( $user['badge_counts']['gold'] > 0 ) {
$val = $user['badge_counts']['gold'];
$gold = "
$val
";
}
if( $user['badge_counts']['silver'] > 0 ) {
$val = $user['badge_counts']['silver'];
$silver = "
$val
";
}
if( $user['badge_counts']['bronze'] > 0 ) {
$val = $user['badge_counts']['bronze'];
$bronze = "
$val
";
}
if( !empty( $gold ) || !empty( $silver ) || !empty( $bronze ) )
$badges = '' . $gold . $silver . $bronze . '
';
return $badges;
}
public function get_my_question( $question, $print_count, $site_link, $referrer )
{
$not_retrieve = sprintf(
'%s',
__( 'could not retrieve question body', 'aysp' )
);
# Set Question properties
$tags = !empty($question['tags'])
? ''.implode('', $question['tags'] ).'' : '';
$author = $question['owner']['display_name'];
$authorlink = isset( $question['owner']['link'] ) ? $question['owner']['link'] : '#';
$body= isset( $question['body'] )
? $question['body'] : $not_retrieve;
$date = date( 'd/m/Y', $question['creation_date'] );
$link = $question['link'];
$short_link = $site_link. '/q/'.$question['question_id'];
if( !empty($referrer) )
$short_link .= '/'.$referrer;
$tit = $question['title'];
$score = $this->get_score( $question['score'], '| ' );
$answers_count = ( !empty( $question['answers'] ) ) ? ' | '.count($question['answers']).' answers' : '';
$count_plus = $print_count + 1;
$count_minus = $print_count - 1;
$prev_text = __( 'prev', 'aysp' );
$next_text = __( 'next', 'aysp' );
#Output
echo <<
$tit$author | $date $score $answers_count
$body
$tags
HTML;
}
public function get_their_answers( $answer )
{
$accept_text = sprintf(
'%s',
__( 'Accepted', 'aysp' )
);
# Set Answer properties
$body = $answer['body'];
$score = $this->get_score( $answer['score'], '', ' - ' );
$accepted = ( isset( $answer['is_accepted']) && $answer['is_accepted'] )
? $accept_text : '';
$accepted_bg = ( isset( $answer['is_accepted']) && $answer['is_accepted'] )
? 'accepted-bg' : '';
$accepted_arrow = ( isset( $answer['is_accepted']) && $answer['is_accepted'] )
? '' : '';
$author = $answer['owner']['display_name'];
$authorlink = isset( $answer['owner']['link'] )
? $answer['owner']['link'] : '#';
$date = isset( $answer['creation_date'] ) ? ' | '.date('d/m/Y', $answer['creation_date'] ) : '';
$avatar = $this->get_avatar( $answer );
# Output
echo <<
$avatar $score
$author$date $accepted_arrow
$body
HTML;
}
public function get_their_question( $question, $site_link, $referrer )
{
$not_retrieve = sprintf(
'%s',
__( 'could not retrieve question body', 'aysp' )
);
# Set Question properties
$short_link = $site_link. '/q/'.$question['question_id'];
if( !empty($referrer) )
$short_link .= '/'.$referrer;
$tags = !empty($question['tags'])
? ''.implode('', $question['tags'] ).'' : '';
$date = date('d/m/Y', $question['creation_date'] );
$score = $this->get_score( $question['score'], '| ', '' );
$body = isset( $question['body'] )
? $question['body'] : $not_retrieve;
$avatar = $this->get_avatar( $question );
# Output
$html = <<
$avatar {$question['owner']['display_name']} | $date $score
{$question['title']}
$body
$tags
HTML;
return $html;
}
public function get_my_answer( $answer, $site_link, $referrer )
{
# Set Answer properties
$short_link = $site_link. '/a/'.$answer['answer_id'];
if( !empty($referrer) )
$short_link .= '/'.$referrer;
$score = $this->get_score( $answer['score'], '', '' );
$score_accepted_bg = ( isset( $answer['is_accepted']) && $answer['is_accepted'] )
? ' accepted-bg' : '';
$score_box = ''.$answer['score'].'';
$accepted = ( isset( $answer['is_accepted']) && $answer['is_accepted'] )
? '' : '';
$accepted = ''; // DEBUG
$date = date('d/m/Y', $answer['creation_date'] );
# Output
$html = <<
$accepted $score_box
$date
{$answer['body']}
HTML;
return $html;
}
public function get_avatar( $post )
{
if( isset( $post['owner']['profile_image'] ) )
{
$avatar_image = $post['owner']['profile_image'] ;
$src = str_replace( 's=128', 's=32', $avatar_image );
return "
";
}
else
return '';
}
/**
* Zero, one or more votes
* @param string $score
* @return string
*/
public function get_score( $score, $prefix='', $suffix='' )
{
$vote_one = __( '1 vote', 'aysp' );
$vote_many = __( 'votes', 'aysp' );
switch( $score )
{
case '0':
null:
$score = '';
break;
case '1':
$score = $prefix . $vote_one . $suffix;
break;
default:
$score = $prefix . $score . $vote_many . $suffix;
break;
}
return $score;
}
private function count_format($n, $point=',', $sep='.') {
if ($n < 0)
return 0;
if ($n < 10000)
return number_format($n, 0, $point, $sep);
$d = $n < 1000000 ? 1000 : 1000000;
$f = round($n / $d, 1);
return number_format($f, $f - intval($f) ? 1 : 0, $sep, $point) . ($d == 1000 ? 'k' : 'M');
}
}