* @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_content(){ global $post; echo $post->post_content; } 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'); $user = get_userdata($id); return ''.$user->display_name.' ('.ap_get_points($id).' '.__('Points', 'ap').')'; } /* 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($question_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($question_id, $user_id)) return false; else return true; } return false; } //check if user answered on a question function ap_is_user_answered($question_id, $user_id){ global $wpdb; $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts where post_parent = $question_id AND ( post_author = $user_id AND post_type = 'answer')"); if($count) 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('mod_question') || 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('mod_question') || 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('mod_question') || is_super_admin()){ global $current_user; if($post_id ) $post = get_post($post_id); else global $post; if(is_super_admin() || current_user_can('mod_question') ) 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(){ if(is_super_admin() || current_user_can('mod_question')) return true; return false; } function ap_user_can_comment(){ if(is_super_admin() || current_user_can('add_comment')) return true; return false; } function ap_user_can_edit_comment($comment_id){ if(is_super_admin() || current_user_can('mod_comment')) return true; global $current_user; if( current_user_can('edit_comment') && ($current_user->ID == $comment_id)) return true; return false; } function ap_user_can_delete_comment($comment_id){ if(is_super_admin() || current_user_can('mod_comment')) return true; global $current_user; if( current_user_can('delete_comment') && ($current_user->ID == $comment_id)) return true; return false; } function ap_status_type(){ $types = array('open', 'solved', 'closed', 'duplicate'); return apply_filters('ap_status_types', $types); } function ap_set_question_status($post_id, $status = 'open'){ $status = in_array( $status, ap_status_type()) ? $status : 'open'; update_post_meta( $post_id, '_status', apply_filters('ap_set_status', $status)); } function ap_get_question_status($post_id = NULL){ if(!$post_id) $post_id = get_the_ID(); $status = get_post_meta( $post_id, '_status', true ); if(!strlen($status)) return 'open'; return $status; } // count numbers of answers function ap_count_ans($id = NULL){ if(!$id) $id = get_the_ID(); global $wpdb; $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts where post_parent = $id AND post_status = 'publish'"); return $count; } //check if current questions have answers function ap_have_ans($id = NULL){ if(!$id) $id = get_the_ID(); if(ap_count_ans($id) > 0) return true; return false; } // link to asnwers function ap_answers_link(){ return get_permalink().'#answers'; } function ap_comment_btn_html(){ $action = get_post_type(get_the_ID()).'-'.get_the_ID(); $nonce = wp_create_nonce( $action ); echo ''.__('Comment', 'ap').''; } 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() && get_post_type($post_id) == 'question'){ $action = get_post_type($post_id).'-'.$post_id; $nonce = wp_create_nonce( $action ); $status = ap_get_question_status($post_id); echo ''; } return; }