* @license GPL-2.0+ * @link http://rahularyan.com * @copyright 2014 Rahul Aryan */ // AnsPress options function ap_opt($key = false, $value = false){ $settings = get_option( 'anspress_opt'); if($value){ $settings[$key] = $value; update_option( 'anspress_opt', $settings); return; } 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, ''. $category->description .'
'; $child = get_terms( array('taxonomy' => 'question_category'), array( 'parent' => $category->term_id, 'hierarchical' => false, 'hide_empty' => false )); if($child) : 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(){ $queried_object = get_queried_object(); if(!isset($queried_object->ID)) return false; if( $queried_object->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') || get_query_var('question_tags'))) return true; return false; } function get_question_tag_id(){ if(is_question_tag() && get_option('permalink_structure')){ $term = get_term_by('slug', get_query_var('question_tags'), 'question_tags'); return $term->term_id; }else return get_query_var('qtag_id'); return false; } function is_question_cat(){ if(is_anspress() && (get_query_var('qcat_id') || get_query_var('question_category'))) return true; return false; } function get_question_cat_id(){ if(is_question_cat() && get_option('permalink_structure')){ $term = get_term_by('slug', get_query_var('question_category'), 'question_category'); return $term->term_id; }else 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){ global $post; wp_editor( apply_filters('the_content', $content), 'post_content', array('media_buttons' => false, 'quicktags' => false, 'textarea_rows' => 5, 'teeny' => true, 'statusbar' => false)); 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) ; } function ap_answers_list($question_id, $order = 'voted'){ $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; if(is_question()){ $order = get_query_var('sort'); if(empty($order )) $order = ap_opt('answers_sort'); } if($order == 'voted'){ $ans_args=array( 'post_type' => 'answer', 'post_status' => 'publish', 'post_parent' => get_the_ID(), 'showposts' => ap_opt('answers_per_page'), 'paged' => $paged, 'orderby' => 'meta_value_num', 'meta_key' => ANSPRESS_VOTE_META, ); }elseif($order == 'oldest'){ $ans_args=array( 'post_type' => 'answer', 'post_status' => 'publish', 'post_parent' => get_the_ID(), 'showposts' => ap_opt('answers_per_page'), 'paged' => $paged, 'orderby' => 'date', 'order' => 'ASC' ); }else{ $ans_args=array( 'post_type' => 'answer', 'post_status' => 'publish', 'post_parent' => get_the_ID(), 'showposts' => ap_opt('answers_per_page'), 'paged' => $paged, 'orderby' => 'date', 'order' => 'DESC' ); } $ans_args = apply_filters('ap_answers_query_args', $ans_args); $ans = new WP_Query($ans_args); // get answer sorting tab ap_ans_tab(); while ( $ans->have_posts() ) : $ans->the_post(); include(ap_get_theme_location('answer.php')); endwhile ; ap_pagination('', 2, $paged, $ans); wp_reset_query(); } function ap_ans_tab(){ $order = get_query_var('sort'); if(empty($order )) $order = ap_opt('answers_sort'); $link = get_permalink( get_the_ID() ).'?sort='; ?>