* @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){ global $wpdb; $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts where post_parent = $id AND post_status = 'publish' AND post_type = 'answer'"); return $count; } //check if current questions have answers function ap_have_ans($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_question_edit_link($post_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('base_page')) ); return apply_filters( 'ap_question_edit_link', $edit_link ); } return; } function ap_edit_a_btn_html(){ $post_id = get_edit_answer_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_answer_edit_link(){ $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('base_page')) ); return apply_filters( 'ap_answer_edit_link', $edit_link ); } 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; } function ap_truncate_chars($text, $limit, $ellipsis = '...') { if( strlen($text) > $limit ) { $endpos = strpos(str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $text), ' ', $limit); if($endpos !== FALSE) $text = trim(substr($text, 0, $endpos)) . $ellipsis; } return $text; } function ap_child_cat_list($parent){ $categories = get_terms( array('taxonomy' => 'question_category'), array( 'parent' => $parent, 'hide_empty' => false )); if($categories){ echo ''; } } function ap_category_details(){ $var = get_query_var('question_category'); $category = get_term_by('slug', $var, 'question_category'); echo '
'; echo '

'. $category->name .'

'; echo '
'; echo ''. $category->count .' '.__('Questions', 'ap').''; echo ''; echo '
'; echo '
'; echo '

'. $category->description .'

'; $child = get_terms( array('taxonomy' => 'question_category'), array( 'parent' => $category->term_id, 'hierarchical' => false, 'hide_empty' => false )); if($child) : echo ''; endif; } function ap_tag_details(){ $var = get_query_var('question_tags'); $tag = get_term_by('slug', $var, 'question_tags'); echo '
'; echo '

'. $tag->name .'

'; echo '
'; echo ''. $tag->count .' '.__('Questions', 'ap').''; echo ''; echo '
'; echo '
'; echo '

'. $tag->description .'

'; } function ap_get_all_users(){ $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $per_page = ap_opt('tags_per_page'); $total_terms = wp_count_terms('question_tags'); $offset = $per_page * ( $paged - 1) ; $args = array( 'number' => $per_page, 'offset' => $offset ); $users = get_users( $args); echo ''; ap_pagination(ceil( $total_terms / $per_page ), $range = 1, $paged); } function is_anspress(){ if( get_the_ID() == ap_opt('base_page')) return true; return false; } function is_question(){ if(is_anspress() && get_query_var('question_id') || get_query_var('question')) return true; return false; } function get_question_id(){ if(is_question() && get_query_var('question_id')) $id = get_query_var('question_id'); elseif(is_question() && get_query_var('question')) $id = get_query_var('question'); return $id; return false; } function is_ask(){ if(is_anspress() && get_query_var('ap_page')=='ask') return true; return false; } function is_question_categories(){ if(is_anspress() && get_query_var('ap_page')=='categories') return true; return false; } function is_question_tags(){ if(is_anspress() && get_query_var('ap_page')=='tags') return true; return false; } function is_question_tag(){ if(is_anspress() && get_query_var('qtag_id')) return true; return false; } function get_question_tag_id(){ if(is_question_tag()) return get_query_var('qtag_id'); return false; } function is_question_cat(){ if(is_anspress() && get_query_var('qcat_id')) return true; return false; } function get_question_cat_id(){ if(is_question_cat()) return get_query_var('qcat_id'); return false; } function is_question_edit(){ if(is_anspress() && get_query_var('edit_q')) return true; return false; } function get_edit_question_id(){ if(is_anspress() && get_query_var('edit_q')) return get_query_var('edit_q'); return false; } function is_answer_edit(){ if(is_anspress() && get_query_var('edit_a')) return true; return false; } function get_edit_answer_id(){ if(is_anspress() && get_query_var('edit_a')) return get_query_var('edit_a'); return false; } function ap_get_current_page_template(){ if(is_anspress()){ if(is_question()) $template = 'question.php'; elseif(is_ask()) $template = 'ask.php'; elseif(is_question_categories()) $template = 'categories.php'; elseif(is_question_tags()) $template = 'tags.php'; elseif(is_question_tag()) $template = 'tag.php'; elseif(is_question_cat()) $template = 'category.php'; elseif(is_question_edit()) $template = 'edit-question.php'; elseif(is_answer_edit()) $template = 'edit-answer.php'; else $template = 'base.php'; return $template; } return 'content-none.php'; } function ap_editor_content($content){ wp_editor( apply_filters('the_content', $content), 'post_content', array('media_buttons' => false, 'quicktags' => false, 'textarea_rows' => 15, 'teeny' => true)); remove_filter('the_content', $post->post_content); } function ap_base_page_slug(){ $base_page_slug = ap_opt('base_page_slug'); // get the base slug, if base page was set to home page then dont use any slug $slug = ((ap_opt('base_page') !== get_option('page_on_front')) ? $base_page_slug.'/' : ''); return apply_filters('ap_base_page_slug', $slug) ; }