AnsPress_PostTypes::post_type_link( string $link, object $post, bool $leavename, bool $sample )

Description #

Alter question and answer CPT permalink.

Parameters #

  • $link
    string (Required) Link.
  • $post
    object (Required) Post object.
  • $leavename
    bool (Required) Whether to keep the post name.
  • $sample
    bool (Required) Is it a sample permalink.

Changelog #

VersionDescription
2.0.0Introduced.

Source #

File: includes/post-types.php

	public static function post_type_link( $link, $post, $leavename, $sample ) {
		if ( 'question' === $post->post_type ) {
			$question_slug = ap_opt( 'question_page_permalink' );

			if ( empty( $question_slug ) ) {
				$question_slug = 'question_perma_1';
			}

			$default_lang = '';

			// Support polylang permalink.
			if ( function_exists( 'pll_default_language' ) ) {
				$default_lang = pll_get_post_language( $post->ID ) ? pll_get_post_language( $post->ID ) : pll_default_language();
			}

			if ( get_option( 'permalink_structure' ) ) {
				$structure = self::question_perm_structure();
				$rule      = str_replace( '%question_id%', $post->ID, $structure->rule );
				$rule      = str_replace( '%question%', ( $leavename ? '%question%' : $post->post_name ), $rule );
				$link      = home_url( $default_lang . '/' . $rule . '/' );
			} else {
				$link = add_query_arg( array( 'question' => $post->ID ), ap_base_page_link() );
			}

			/**
			 * Allow overriding of question post type permalink
			 *
			 * @param string $link Question link.
			 * @param object $post Post object.
			 */
			$link = apply_filters_deprecated( 'ap_question_post_type_link', array( $link, $post ), '4.4.0', 'ap_question_post_type_link_structure' );

			/**
			 * Allow overriding of question post type permalink
			 *
			 * @param string $link      Question link.
			 * @param object $post      Post object.
			 * @param bool   $leavename Whether to keep the post name.
			 * @param bool   $sample    Is it a sample permalink.
			 */
			$link = apply_filters( 'ap_question_post_type_link_structure', $link, $post, $leavename, $sample );

			return $link;
		} elseif ( 'answer' === $post->post_type && 0 !== (int) $post->post_parent ) {
			$link = get_permalink( $post->post_parent ) . "answer/{$post->ID}/";

			/**
			 * Allow overriding of answer post type permalink.
			 *
			 * @param string $link Answer link.
			 * @param object $post Post object.
			 */
			$link = apply_filters_deprecated( 'ap_answer_post_type_link', array( $link, $post ), '4.4.0', 'ap_answer_post_type_link_structure' );

			/**
			 * Allow overriding of answer post type permalink
			 *
			 * @param string $link      Answer link.
			 * @param object $post      Post object.
			 * @param bool   $leavename Whether to keep the post name.
			 * @param bool   $sample    Is it a sample permalink.
			 */
			$link = apply_filters( 'ap_answer_post_type_link_structure', $link, $post, $leavename, $sample );

			return $link;
		}

		return $link;
	}

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