ap_get_link_to( string|array $sub )

Description #

Return link to AnsPress pages.

Parameters #

  • $sub
    string | array (Required) Sub pages/s.

Source #

File: includes/functions.php

function ap_get_link_to( $sub ) {
	$url = false;

	if ( 'ask' === $sub ) {
		$url = get_permalink( ap_opt( 'ask_page' ) );
	}

	if ( false === $url ) {
		/**
		 * Define default AnsPress page slugs.
		 *
		 * @var array
		 */
		$default_pages = array(
			'question'   => ap_opt( 'question_page_slug' ),
			'users'      => str_replace( ap_base_page_link(), '', get_permalink( ap_opt( 'users_page' ) ) ),
			'user'       => str_replace( ap_base_page_link(), '', get_permalink( ap_opt( 'user_page' ) ) ),
			'activities' => str_replace( ap_base_page_link(), '', get_permalink( ap_opt( 'activities_page' ) ) ),
			'categories' => str_replace( ap_base_page_link(), '', get_permalink( ap_opt( 'categories_page' ) ) ),
			'tags'       => str_replace( ap_base_page_link(), '', get_permalink( ap_opt( 'tags_page' ) ) ),
		);

		$default_pages = apply_filters( 'ap_default_page_slugs', $default_pages );

		if ( is_array( $sub ) && isset( $sub['ap_page'] ) && isset( $default_pages[ $sub['ap_page'] ] ) ) {
			$sub['ap_page'] = $default_pages[ $sub['ap_page'] ];
		} elseif ( ! is_array( $sub ) && ! empty( $sub ) && isset( $default_pages[ $sub ] ) ) {
			$sub = $default_pages[ $sub ];
		}

		$base = rtrim( ap_base_page_link(), '/' );
		$args = '';

		if ( get_option( 'permalink_structure' ) !== '' ) {
			if ( ! is_array( $sub ) && 'base' !== $sub ) {
				if ( strpos( $sub, 'http://' ) === 0 || strpos( $sub, 'https://' ) === 0 ) {
					$args = $sub;
				} else {
					$args = $sub ? '/' . $sub : '';
				}
			} elseif ( is_array( $sub ) ) {
				$args = '/';

				if ( ! empty( $sub ) ) {
					foreach ( (array) $sub as $s ) {
						$args .= $s . '/';
					}
				}
			}

			$args = user_trailingslashit( rtrim( $args, '/' ) );
		} elseif ( ! is_array( $sub ) ) {
			$args = $sub ? '&ap_page=' . $sub : '';
		} elseif ( is_array( $sub ) ) {
			$args = '';

			if ( ! empty( $sub ) ) {
				foreach ( $sub as $k => $s ) {
					$args .= '&' . $k . '=' . $s;
				}
			}
		}

		$url = get_option( 'permalink_structure' ) ? $base . $args : $base;
	}

	/**
	 * Allows filtering anspress links.
	 *
	 * @param string       $url Generated url.
	 * @param string|array $sub AnsPress sub pages.
	 *
	 * @since unknown
	 */
	return apply_filters( 'ap_link_to', $url, $sub );
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment