* @license GPL-2.0+ * @link http://anspress.io * @copyright 2014 Rahul Aryan */ /** * Register user page * @param string $page_slug slug for links * @param string $page_title Page title * @param callable $func Hook to run when shortcode is found. * @return void * @since 2.0.1 */ function ap_register_user_page($page_slug, $page_title, $func, $show_in_menu = true, $public = true) { anspress()->user_pages[$page_slug] = array( 'title' => $page_title, 'func' => $func, 'show_in_menu' => $show_in_menu, 'public' => $public ); } /** * Count user posts by post type * @param int $userid * @param string $post_type * @return int * @since unknown */ function ap_count_user_posts_by_type($userid, $post_type = 'question') { global $wpdb; $where = get_posts_by_author_sql( $post_type, true, $userid ); $query = "SELECT COUNT(*) FROM $wpdb->posts $where"; $key = md5( $query ); $cache = wp_cache_get( $key, 'count' ); if ( $cache === false ) { $count = $wpdb->get_var( $query ); wp_cache_set( $key, $count, 'count' ); } else { $count = $cache; } return apply_filters( 'ap_count_user_posts_by_type', $count, $userid ); } /** * retrive user question counts * @param int $userid * @return int * @since unknown */ function ap_user_question_count($userid) { return ap_count_user_posts_by_type( $userid, $post_type = 'question' ); } /** * get user answer counts * @param int $userid * @return int * @since unknown */ function ap_user_answer_count($userid) { return ap_count_user_posts_by_type( $userid, $post_type = 'answer' ); } function ap_user_best_answer_count($user_id) { global $wpdb; $query = $wpdb->prepare( "SELECT count(DISTINCT pm.post_id) FROM $wpdb->postmeta pm JOIN $wpdb->posts p ON (p.ID = pm.post_id) WHERE pm.meta_key = '".ANSPRESS_BEST_META."' AND pm.meta_value = 1 AND p.post_type = 'answer' AND p.post_author = %d", $user_id ); $key = md5( $query ); $cache = wp_cache_get( $key, 'count' ); if ( $cache === false ) { $count = $wpdb->get_var( $query ); wp_cache_set( $key, $count, 'count' ); } else { $count = $cache; } return apply_filters( 'ap_user_best_answer_count', $count, $user_id ); } function ap_user_solved_answer_count($user_id) { global $wpdb; $query = $wpdb->prepare( "SELECT count(DISTINCT pm.post_id) FROM $wpdb->postmeta pm JOIN $wpdb->posts p ON (p.ID = pm.post_id) WHERE pm.meta_key = '".ANSPRESS_SELECTED_META."' AND pm.meta_value is not null AND pm.meta_value != 0 AND p.post_type = 'question' AND p.post_author = %d", $user_id ); $key = md5( $query ); $cache = wp_cache_get( $key, 'count' ); if ( $cache === false ) { $count = $wpdb->get_var( $query ); wp_cache_set( $key, $count, 'count' ); } else { $count = $cache; } return apply_filters( 'ap_user_best_answer_count', $count, $user_id ); } /** * For user display name * It can be filtered for adding cutom HTML * @param mixed $args * @return string * @since 0.1 */ function ap_user_display_name($args = array()) { global $post; $defaults = array( 'user_id' => get_the_author_meta( 'ID' ), 'html' => false, 'echo' => false, 'anonymous_label' => __( 'Anonymous', 'anspress-question-answer' ), ); if ( ! is_array( $args ) ) { $defaults['user_id'] = $args; $args = $defaults; } else { $args = wp_parse_args( $args, $defaults ); } extract( $args ); $user = get_userdata( $user_id ); if ( $user ) { if ( ! $html ) { $return = $user->display_name; } else { $return = ''.$user->display_name.''; } } elseif ( $post && $post->post_type == 'question' || $post->post_type == 'answer' ) { $name = get_post_meta( $post->ID, 'anonymous_name', true ); if ( ! $html ) { if ( $name != '' ) { $return = $name; } else { $return = $anonymous_label; } } else { if ( $name != '' ) { $return = ''.$name.__( ' (anonymous)', 'anspress-question-answer' ).''; } else { $return = ''.$anonymous_label.''; } } } else { if ( ! $html ) { $return = $anonymous_label; } else { $return = ''.$anonymous_label.''; } } /** * FILTER: ap_user_display_name * Filter can be used to alter display name * @var string * @since 2.0.1 */ $return = apply_filters( 'ap_user_display_name', $return ); if ( $echo !== false ) { echo $return; return; } return $return; } /** * Return Link to user pages * @param boolean|integer $user_id user id * @param string $sub page slug * @return string * @since unknown */ function ap_user_link($user_id = false, $sub = false) { if ( false === $user_id ) { $user_id = get_the_author_meta( 'ID' ); } if ( $user_id < 1 ) { return '#AnonymousUser'; } if ( ap_opt( 'user_profile' ) == '' ) { return apply_filters( 'ap_user_custom_profile_link', $user_id, $sub ); } elseif ( function_exists( 'bp_core_get_userlink' ) && ap_opt( 'user_profile' ) == 'buddypress' ) { return bp_core_get_userlink( $user_id, false, true ); } elseif ( ap_opt( 'user_profile' ) == 'userpro' ) { global $userpro; return $userpro->permalink( $user_id ); } if ( 0 == $user_id ) { return false; } $user = get_user_by( 'id', $user_id ); // If permalink is enabled. if ( get_option( 'permalink_structure' ) != '' ) { if ( ! ap_opt( 'base_before_user_perma' ) ) { $base = home_url( '/'.ap_get_user_page_slug().'/' ); } else { $base = ap_get_link_to( ap_get_user_page_slug() ); } if ( $sub === false ) { $link = $base. $user->user_login.'/'; } elseif ( is_array( $sub ) ) { $link = $base . $user->user_login.'/'; if ( ! empty( $sub ) ) { foreach ( $sub as $s ) { $link .= $s.'/'; } } } elseif ( ! is_array( $sub ) ) { $link = $base. $user->user_login.'/'.$sub.'/'; } } else { if ( false === $sub ) { $sub = array( 'ap_page' => 'user', 'ap_user' => $user->user_login ); } elseif ( is_array( $sub ) ) { $sub['ap_page'] = 'user'; $sub['ap_user'] = $user->user_login; } elseif ( ! is_array( $sub ) ) { $sub = array( 'ap_page' => 'user', 'ap_user' => $user->user_login, 'user_page' => $sub ); } $link = ap_get_link_to( $sub ); } return apply_filters( 'ap_user_link', $link, $user_id ); } /** * Get user menu array items * @param boolean|integer $user_id * @return array */ function ap_get_user_menu($user_id = false, $private = false) { if ( $user_id === false ) { $user_id = ap_get_displayed_user_id(); } $user_pages = anspress()->user_pages; $menus = array(); $i = 1; foreach ( $user_pages as $k => $args ) { $link = ap_user_link( $user_id, $k ); $title = $k == 'notification' ? $args['title'].ap_get_the_total_unread_notification( $user_id, false ): $args['title']; $menus[$k] = array( 'slug' => $k, 'title' => $title, 'link' => $link, 'order' => 5 + $i, 'show_in_menu' => $args['show_in_menu'], 'public' => $args['public'] ); $i++; } /** * FILTER: ap_user_menu * filter is applied before showing user menu * @var array * @since unknown */ $menus = apply_filters( 'ap_user_menu', $menus ); $menus = ap_sort_array_by_order( $menus ); return $menus; } /** * Output user menu * Extract menu from registered user pages * @return void * @since 2.0.1 */ function ap_user_menu($collapse = true, $user_id = false) { if ( false === $user_id ) { $user_id = ap_get_displayed_user_id(); } $menus = ap_get_user_menu( $user_id ); foreach ( $menus as $k => $m ) { if ( ( false === $m['public'] && ! ap_is_my_profile( )) ) { unset( $menus[$k] ); } } $active_user_page = get_query_var( 'user_page' ); $active_user_page = ap_active_user_page(); if ( ! empty( $menus ) && is_array( $menus ) ) { $o = '
';*/ echo $o; } } /** * @return string */ function ap_get_current_user_page_template() { $user_page = get_query_var( 'user_page' ); $user_page = $user_page ? $user_page : 'profile'; $template = 'user-'.$user_page.'.php'; return apply_filters( 'ap_get_current_user_page_template', $template ); } /** * Output user page * @return void * @since 2.0 */ function ap_user_page() { $user_pages = anspress()->user_pages; $user_id = ap_get_displayed_user_id(); $user_page = ap_active_user_page(); $callback = @$user_pages[$user_page]['func']; if ( $user_id > 0 && ((is_array( $callback ) && method_exists( $callback[0], $callback[1] )) || ( ! is_array( $callback ) && function_exists( $callback )) ) ) { call_user_func( $callback ); } else { echo '