* @license GPL-3.0+ * @link https://anspress.io * @copyright 2014 Rahul Aryan * @package AnsPress * @subpackage Theme Functions */ // If this file is called directly, abort. if ( ! defined( 'WPINC' ) ) { die; } /** * Return current page title. * * @return string current title * @since unknown * @since 4.1.0 Removed `question_name` query var check. */ function ap_page_title() { $new_title = ''; $new_title = apply_filters( 'ap_page_title', $new_title ); return $new_title; } /** * Check if current page is search page * * @return boolean */ function is_ap_search() { if ( is_anspress() && get_query_var( 'ap_s' ) ) { return true; } return false; } /** * Get post status. * * @param boolean|integer $post_id question or answer ID. * @return string * @since 2.0.0 */ function ap_post_status( $post_id = false ) { if ( false === $post_id ) { $post_id = get_the_ID(); } return get_post_status( $post_id ); } /** * Check if current post is private. * * @param boolean|integer $post_id question or answer ID. * @return boolean */ function is_private_post( $post_id = false ) { if ( ap_post_status( $post_id ) === 'private_post' ) { return true; } return false; } /** * Check if post is waiting moderation. * * @param boolean|integer $post_id question or answer ID. * @return bool */ function is_post_waiting_moderation( $post_id = false ) { if ( get_post_status( $post_id ) === 'moderate' ) { return true; } return false; } /** * Check if question is closed. * * @param boolean|integer $post_id question or answer ID. * @return boolean * @since 2.0.0 */ function is_post_closed( $post_id = null ) { if ( '1' === ap_get_post_field( 'closed', $post_id ) ) { return true; } return false; } /** * Check if question have a parent post. * * @param boolean|integer $post_id question or answer ID. * @return boolean * @since 2.0.0 */ function ap_have_parent_post( $post_id = false ) { if ( ! $post_id ) { $post_id = get_the_ID(); } // Get post. $post_o = ap_get_post( $post_id ); if ( $post_o->post_parent > 0 && 'question' === $post_o->post_type ) { return true; } return false; } /** * Anspress pagination * Uses paginate_links. * * @param float $current Current paged, if not set then get_query_var('paged') is used. * @param int $total Total number of pages, if not set then global $questions is used. * @param string $format pagination format. * @param string $page_num_link Base link. * @return string */ function ap_pagination( $current = false, $total = false, $format = '?paged=%#%', $page_num_link = false ) { global $ap_max_num_pages, $ap_current; if ( is_front_page() ) { $format = ''; } $big = 999999999; // Need an unlikely integer. if ( false === $current ) { $paged = ap_sanitize_unslash( 'ap_paged', 'r', 1 ); $current = is_front_page() ? max( 1, $paged ) : max( 1, get_query_var( 'paged' ) ); } elseif ( ! empty( $ap_current ) ) { $current = $ap_current; } if ( ! empty( $ap_max_num_pages ) ) { $total = $ap_max_num_pages; } elseif ( false === $total && isset( anspress()->questions->max_num_pages ) ) { $total = anspress()->questions->max_num_pages; } if ( false === $page_num_link ) { $page_num_link = str_replace( array( '&', '&' ), '&', get_pagenum_link( $big ) ); } $base = str_replace( $big, '%#%', $page_num_link ); if ( '1' == $total ) { // WPCS: loose comparison ok. return; } echo '
'; $links = paginate_links( array( // WPCS: xss okay. 'base' => $base, 'format' => $format, 'current' => $current, 'total' => $total, 'end_size' => 2, 'mid_size' => 2, ) ); $links = str_replace(''; } /** * Answer meta to display. * * @param false|integer $answer_id Answer id. * @return string * @since 2.0.1 */ function ap_display_answer_metas( $answer_id = false ) { if ( false === $answer_id ) { $answer_id = get_the_ID(); } $metas = array(); if ( ap_is_selected( $answer_id ) ) { $metas['best_answer'] = '' . __( 'Best answer', 'anspress-question-answer' ) . ''; } $metas['history'] = ap_last_active_time( $answer_id ); /* * Used to filter answer display meta. * * @since 2.0.1 */ $metas = apply_filters( 'ap_display_answer_metas', $metas, $answer_id ); $output = ''; if ( ! empty( $metas ) && is_array( $metas ) ) { foreach ( $metas as $meta => $display ) { $output .= "{$display}"; } } return $output; } /** * Echo ask button. * * @since 2.1 */ function ap_ask_btn() { echo ap_get_ask_btn(); // WPCS: xss okay. } /** * Return the ask button. * * @return string Ask button HTML * @since 2.1 */ function ap_get_ask_btn() { $link = ap_get_link_to( 'ask' ); /** * Filter ask button link. * * @param string $link */ $link = apply_filters( 'ap_ask_btn_link', $link ); return '' . __( 'Ask question', 'anspress-question-answer' ) . ''; } /** * Include template php files. * * @param string $file File name without extension. * @since 2.1 */ function ap_get_template_part( $file, $args = false ) { if ( false !== $args ) { extract( $args ); } include ap_get_theme_location( $file . '.php' ); } /** * Return current AnsPress page * * @return string * @since unknown * @since 4.1.0 Check if ask question page. * @since 4.1.1 Do not return `base` by default. * @since 4.1.2 If 404 do not return anything. */ function ap_current_page() { $cache = wp_cache_get( 'current_page', 'anspress' ); if ( false !== $cache ) { return $cache; } $query_var = get_query_var( 'ap_page', '' ); $main_pages = array_keys( ap_main_pages() ); $page_ids = []; foreach ( $main_pages as $page_slug ) { $page_ids[ ap_opt( $page_slug ) ] = $page_slug; } if ( is_question() || is_singular( 'question' ) ) { $query_var = 'question'; } elseif ( 'edit' === $query_var ) { $query_var = 'edit'; } elseif ( in_array( $query_var . '_page', $main_pages, true ) ) { $query_var = $query_var; } elseif ( in_array( get_the_ID(), array_keys( $page_ids ) ) ) { $query_var = str_replace( '_page', '', $page_ids[ get_the_ID() ] ); } elseif ( 'base' === $query_var ) { $query_var = 'base'; } elseif ( is_404() ) { $query_var = ''; } /** * Filter AnsPress current page. * * @param string $query_var Current page slug. */ $query_var = apply_filters( 'ap_current_page', esc_attr( $query_var ) ); wp_cache_set( 'current_page', $query_var, 'anspress' ); return $query_var; } /** * AnsPress CSS and JS. * * @return array */ function ap_assets() { wp_register_script( 'selectize', ANSPRESS_URL . 'assets/js/min/selectize.min.js', [ 'jquery' ] ); $assets = array( 'js' => array( 'form' => [ 'dep' => [ 'jquery' ], 'footer' => true, ], 'main' => [ 'dep' => [ 'jquery', 'anspress-form', 'underscore', 'backbone', 'selectize' ], 'footer' => true, ], 'theme' => [ 'theme' => true, 'dep' => [ 'anspress-main' ], 'footer' => true, ], ), 'css' => array( 'main' => array( 'theme' => true, 'dep' => [ 'anspress-fonts' ], ), 'fonts' => array( 'theme' => true ), 'rtl' => array( 'theme' => true ), ), ); if ( ap_current_page() !== '' ) { $assets['js']['main']['active'] = true; } if ( is_rtl() ) { $assets['css']['rtl']['active'] = true; } $assets['js'] = apply_filters( 'ap_assets_js', $assets['js'] ); $assets['css'] = apply_filters( 'ap_assets_css', $assets['css'] ); return $assets; } /** * Enqueue AnsPress assets. * * @since 2.4.6 */ function ap_enqueue_scripts() { $assets = ap_assets(); foreach ( (array) $assets['js'] as $k => $js ) { $src = '/min/' . $k . '.min.js'; $src = ! empty( $js['theme'] ) ? ap_get_theme_url( 'js' . $src, false, false ) : ANSPRESS_URL . 'assets/js' . $src; $dep = isset( $js['dep'] ) ? $js['dep'] : array(); $footer = isset( $js['footer'] ) ? $js['footer'] : false; wp_register_script( 'anspress-' . $k, $src, $dep, AP_VERSION, $footer ); if ( isset( $js['active'] ) && $js['active'] ) { wp_enqueue_script( 'anspress-' . $k ); } } foreach ( (array) $assets['css'] as $k => $css ) { $src = ! empty( $css['theme'] ) ? ap_get_theme_url( 'css/' . $k . '.css', false, false ) : ANSPRESS_URL . 'assets/css' . $k . '.css'; $dep = isset( $css['dep'] ) ? $css['dep'] : array(); wp_register_style( 'anspress-' . $k, $src, $dep, AP_VERSION ); if ( isset( $css['active'] ) && $css['active'] ) { wp_enqueue_style( 'anspress-' . $k ); } } } /** * Get all list filters. * * @param string $current_url Current URL. */ function ap_get_list_filters() { $param = array(); $search_q = get_query_var( 'ap_s' ); if ( ! empty( $search_q ) ) { $param['ap_s'] = $search_q; } $filters = array( 'order_by' => array( 'title' => __( 'Order By', 'anspress-question-answer' ), 'items' => [], 'multiple' => false, ), ); /* * Filter question sorting. * @param array Question sortings. * @since 2.3 */ return apply_filters( 'ap_list_filters', $filters ); } /** * Output list filters form. * * @param string $current_url Current Url. */ function ap_list_filters( $current_url = '' ) { $filters = ap_get_list_filters(); echo '
'; foreach ( (array) $filters as $key => $filter ) { $active = ''; $current_order_by = ap_get_current_list_filters( 'order_by' ); if ( ! empty( $current_order_by ) ) { $active_arr = ap_search_array( ap_get_questions_orderby(), 'value', $current_order_by ); if ( ! empty( $active_arr ) ) { $active = ': ' . $active_arr[0]['label'] . ''; } } $active = apply_filters( 'ap_list_filter_active_' . $key, $active, $filter ); $args = wp_json_encode( [ '__nonce' => wp_create_nonce( 'filter_' . $key ), 'filter' => $key, ] ); echo '
'; echo '' . esc_attr( $filter['title'] ) . $active . ''; // xss okay. echo '
'; } echo ''; foreach ( (array) ap_get_current_list_filters() as $key => $value ) { if ( ! is_array( $value ) ) { echo ''; } else { foreach ( (array) $value as $v ) { echo ''; } } } echo '
'; } /** * Print select anser HTML button. * * @param mixed $_post Post. * @return string */ function ap_select_answer_btn_html( $_post = null ) { if ( ! ap_user_can_select_answer( $_post ) ) { return; } $_post = ap_get_post( $_post ); $nonce = wp_create_nonce( 'select-answer-' . $_post->ID ); $q = esc_js( wp_json_encode( [ 'answer_id' => $_post->ID, '__nonce' => $nonce, ] ) ); $active = false; $title = __( 'Select this answer as best', 'anspress-question-answer' ); $label = __( 'Select', 'anspress-question-answer' ); $have_best = ap_have_answer_selected( $_post->post_parent ); $selected = ap_is_selected( $_post ); $hide = false; if ( $have_best && $selected ) { $title = __( 'Unselect this answer', 'anspress-question-answer' ); $label = __( 'Unselect', 'anspress-question-answer' ); $active = true; } if ( $have_best && ! $selected ) { $hide = true; } return '' . $label . ''; } /** * Output chnage post status button. * * @param mixed $_post Post. * @return null|string * @since 4.0.0 */ function ap_post_status_btn_args( $_post = null ) { $_post = ap_get_post( $_post ); $args = []; if ( ap_user_can_change_status( $_post->ID ) ) { global $wp_post_statuses; $allowed_status = [ 'publish', 'private_post', 'moderate' ]; $status_labels = []; foreach ( (array) $allowed_status as $s ) { if ( isset( $wp_post_statuses[ $s ] ) ) { $status_labels[ $s ] = esc_attr( $wp_post_statuses[ $s ]->label ); } } foreach ( (array) $status_labels as $slug => $label ) { $can = true; if ( 'moderate' === $slug && ! ap_user_can_change_status_to_moderate() ) { $can = false; } if ( $can ) { $args[] = array( 'cb' => 'status', 'active' => ( $slug === $_post->post_status ), 'query' => [ 'status' => $slug, '__nonce' => wp_create_nonce( 'change-status-' . $slug . '-' . $_post->ID ), 'post_id' => $_post->ID, ], 'label' => esc_attr( $label ), ); } } return $args; } } /** * Return set featured question action args. * * @param boolean|integer $post_id Post ID. * @return array */ function ap_featured_post_args( $post_id = false ) { if ( ! is_user_logged_in() || ! ap_user_can_toggle_featured() ) { return []; } if ( false === $post_id ) { $post_id = get_question_id(); } $is_featured = ap_is_featured_question( $post_id ); if ( $is_featured ) { $title = __( 'Unmark this question as featured', 'anspress-question-answer' ); $label = __( 'Unfeature', 'anspress-question-answer' ); } else { $title = __( 'Mark this question as featured', 'anspress-question-answer' ); $label = __( 'Feature', 'anspress-question-answer' ); } return array( 'cb' => 'toggle_featured', 'active' => $is_featured, 'query' => [ '__nonce' => wp_create_nonce( 'set_featured_' . $post_id ), 'post_id' => $post_id, ], 'title' => esc_attr( $title ), 'label' => esc_attr( $label ), ); } /** * Output question subscribe button. * * @param object|integer|false $_post Post object or ID. * @param boolean $echo Echo or return. * @return string|null * @since 4.0.0 */ function ap_subscribe_btn( $_post = false, $echo = true ) { $_post = ap_get_post( $_post ); $args = wp_json_encode( [ '__nonce' => wp_create_nonce( 'subscribe_' . $_post->ID ), 'id' => $_post->ID, ] ); $subscribers = (int) ap_get_post_field( 'subscribers', $_post ); $subscribed = ap_is_user_subscriber( 'question', $_post->ID ); $label = $subscribed ? __( 'Unsubscribe', 'anspress-question-answer' ) : __( 'Subscribe', 'anspress-question-answer' ); $html = '' . esc_attr( $label ) . '' . esc_attr( $subscribers ) . ''; if ( ! $echo ) { return $html; } echo $html; // WPCS: xss okay. } /** * Create array of object containing AnsPress pages. To be used in admin menu metabox. * * @return array * @since unknown * @since 4.1.0 Improved ask page object. */ function ap_menu_obejct() { $menu_items = []; foreach ( (array) anspress()->pages as $k => $args ) { if ( $args['show_in_menu'] ) { $object_id = 1; $object = $k; $title = $args['title']; $url = home_url( '/' ); $type = 'anspress-links'; $main_pages = array_keys( ap_main_pages() ); if ( in_array( $k . '_page', $main_pages, true ) ) { $post = get_post( ap_opt( $k . '_page' ) ); $object_id = ap_opt( $k . '_page' ); $object = 'page'; $url = get_permalink( $post ); $title = $post->post_title; $type = 'post_type'; } $menu_items[] = (object) array( 'ID' => 1, 'db_id' => 0, 'menu_item_parent' => 0, 'object_id' => $object_id, 'post_parent' => 0, 'type' => $type, 'object' => $object, 'type_label' => __( 'AnsPress links', 'anspress-question-answer' ), 'title' => $title, 'url' => $url, 'target' => '', 'attr_title' => '', 'description' => '', 'classes' => [ 'anspress-menu-' . $k ], 'xfn' => '', ); } // End if(). } // End foreach(). /** * Hook for filtering default AnsPress menu objects. * * @param array $menu_items Array of menu objects. * @since 4.1.2 */ return apply_filters( 'ap_menu_object', $menu_items ); } /** * Return AnsPress page slug. * * @param string $slug Default page slug. * @return string */ function ap_get_page_slug( $slug ) { $option = ap_opt( $slug . '_page_slug' ); if ( ! empty( $option ) ) { $slug = $option; } return apply_filters( 'ap_page_slug_' . $slug, $slug ); }