* @license GPL-2.0+
* @link http://rahularyan.com
* @copyright 2014 Rahul Aryan
*/
// AnsPress options
function ap_opt($key = false){
$settings = get_option( 'anspress_opt');
if(!$key)
return $settings;
if(isset($settings[$key]))
return $settings[$key];
return false;
}
function ap_theme_list(){
$themes = array();
$dirs = array_filter(glob(ANSPRESS_THEME_DIR.'/*'), 'is_dir');
foreach($dirs as $dir)
$themes[basename($dir)] = basename($dir);
return $themes;
}
function ap_get_theme(){
$option = ap_opt('theme');
if(!$option)
return 'default';
return ap_opt('theme');
}
// get the location of the theme
function ap_get_theme_location($file){
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array( 'anspress/'.$file ) ) ) {
$template_path = $theme_file;
} else {
$template_path = ANSPRESS_THEME_DIR .'/'.ap_get_theme().'/'.$file;
}
return $template_path;
}
// get the url theme
function ap_get_theme_url($file){
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array( 'anspress/'.$file ) ) ) {
$template_url = get_template_directory().'/anspress/'.$file;
} else {
$template_url = ANSPRESS_THEME_URL .'/'.ap_get_theme().'/'.$file;
}
return $template_url;
}
//get current user id
function ap_current_user_id() {
require_once(ABSPATH . WPINC . '/pluggable.php');
global $current_user;
get_currentuserinfo();
return $current_user->ID;
}
function ap_question_tags($list = true){
global $post;
if($list)
$o = get_the_term_list( $post->ID, ANSPRESS_TAG_TAX, '
' );
else
$o = get_the_term_list( $post->ID, ANSPRESS_TAG_TAX, '', '', '' );
echo $o;
}
function ap_question_categories($list = true){
global $post;
if($list)
$o = get_the_term_list( $post->ID, ANSPRESS_CAT_TAX, '' );
else
$o = get_the_term_list( $post->ID, ANSPRESS_CAT_TAX, '', ', ', '' );
echo $o;
}
function ap_human_time($time){
return human_time_diff( $time, current_time('timestamp') );
}
function ap_please_login(){
$o = '';
$o .= '';
$o .= __('Please login or register to continue this action.', 'ap');
$o .= '
';
echo apply_filters('ap_please_login', $o);
}
function ap_user_display_name_point($id = false){
if(!$id)
$id = get_the_author_meta('ID');
printf('
%s (%s)',
get_the_author_meta('display_name'),
ap_get_points($id)
);
}
/* Check if a user can ask a question */
function ap_user_can_ask(){
if(current_user_can('add_question') || is_super_admin())
return true;
return false;
}
/* Check if a user can answer on a question */
function ap_user_can_answer($post_id){
if(current_user_can('add_answer') || is_super_admin()){
global $current_user;
$user_id = $current_user->ID;
if(!ap_opt('multiple_answers') && ap_is_user_answered($post_id, $user_id))
return false;
else
return true;
}
return false;
}
/* Check if a user can edit answer on a question */
function ap_user_can_edit_ans($post_id){
if(current_user_can('edit_own_answer') || current_user_can('moderate_qa') || is_super_admin()){
$post = get_post($post_id);
global $current_user;
$user_id = $current_user->ID;
if(($post->author_id == $user_id) || current_user_can('moderate_qa') || is_super_admin())
return true;
else
return false;
}
return false;
}
function ap_user_can_edit_question($post_id = false){
if(current_user_can('edit_own_answer') || current_user_can('moderate_qa') || is_super_admin()){
global $current_user;
if($post_id )
$post = get_post($post_id);
else
global $post;
if(is_super_admin() || current_user_can('moderate_qa') )
return true;
if(($current_user->ID == $post->post_author) && current_user_can('edit_own_question'))
return true;
}
return false;
}
function ap_user_can_change_status($post_id = false){
if(is_super_admin() || current_user_can('moderate_qa'))
return true;
return false;
}
function ap_comment_btn_html(){
$action = get_post_type(get_the_ID()).'-'.get_the_ID();
$nonce = wp_create_nonce( $action );
echo '';
}
function ap_edit_question_btn_html(){
$post_id = get_the_ID();
if(ap_user_can_edit_question($post_id)){
$action = get_post_type($post_id).'-'.$post_id;
$nonce = wp_create_nonce( $action );
$edit_link = add_query_arg( array('edit_q' => $post_id, 'ap_nonce' => $nonce), get_permalink( ap_opt('edit_page')) );
echo ''.__('Edit', 'ap').'';
}
return;
}
function ap_edit_a_btn_html(){
$post_id = get_the_ID();
if(ap_user_can_edit_ans($post_id)){
$action = get_post_type($post_id).'-'.$post_id;
$nonce = wp_create_nonce( $action );
$edit_link = add_query_arg( array('edit_a' => $post_id, 'ap_nonce' => $nonce), get_permalink( ap_opt('a_edit_page')) );
echo ''.__('Edit', 'ap').'';
}
return;
}
function ap_change_status_btn_html($post_id = null){
if(!$post_id)
$post_id = get_the_ID();
if(ap_user_can_change_status($post_id) && get_post_type($post_id) == 'question'){
$action = get_post_type($post_id).'-'.$post_id;
$nonce = wp_create_nonce( $action );
$status = get_post_status($post_id);
echo '';
}
return;
}