AnsPress_Rewrite::rewrites()

Description #

Rewrite rules.

Source #

File: includes/rewrite.php

	public static function rewrites() {
		global $wp_rewrite;

		$rule         = anspress()->question_rule['rule'];
		$rewrite      = anspress()->question_rule['rewrite'];
		$all_rules    = array();
		$base_page_id = ap_opt( 'base_page' );
		$slug_main    = ap_base_page_slug();
		$lang_rule    = '';
		$lang_rewrite = '';

		// Support polylang permalink.
		if ( function_exists( 'pll_languages_list' ) ) {
			if ( ! empty( pll_languages_list() ) ) {
				$lang_rule    = '(' . implode( '|', pll_languages_list() ) . ')/';
				$lang_rewrite = '&lang=$matches[#]';
			}
		}

		$slug         = $lang_rule . $slug_main . '/';
		$base_page_id = $base_page_id . $lang_rewrite;

		$answer_rewrite = str_replace( 'post_type=question', 'post_type=answer', $rewrite );
		$answer_rewrite = str_replace( '&question=', '&question_slug=', $answer_rewrite );
		$answer_rewrite = str_replace( '&p=', '&question_id=', $answer_rewrite );

		$all_rules = array(
			$slug . 'search/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?s=$matches[#]&paged=$matches[#]&post_type=question',
			$slug . 'search/([^/]+)/?$'                   => 'index.php?s=$matches[#]&post_type=question',
			$slug . 'edit/?$'                             => 'index.php?pagename=' . $slug_main . '&ap_page=edit',
			$lang_rule . $rule . '/answer/([0-9]+)/(feed|rdf|rss|rss2|atom)/?$' => $lang_rewrite . $answer_rewrite . '&answer_id=$matches[#]&feed=$matches[#]',
			$lang_rule . $rule . '/answer/([0-9]+)/embed/?$' => $lang_rewrite . $answer_rewrite . '&answer_id=$matches[#]&embed=true',
			$lang_rule . $rule . '/answer/([0-9]+)/?$'    => $lang_rewrite . $rewrite . '&answer_id=$matches[#]',
			$lang_rule . $rule . '/page/?([0-9]{1,})/?$'  => $lang_rewrite . $rewrite . '&ap_paged=$matches[#]',
			$lang_rule . $rule . '/(feed|rdf|rss|rss2|atom)/?$' => $lang_rewrite . $rewrite . '&feed=$matches[#]',
			$lang_rule . $rule . '/embed/?$'              => $lang_rewrite . $rewrite . '&embed=true',
			$lang_rule . $rule . '/?$'                    => $lang_rewrite . $rewrite,
		);

		/**
		 * Allows filtering AnsPress rewrite rules.
		 *
		 * @param array $all_rules Rewrite rules.
		 * @since 4.1.0
		 */
		$all_rules = apply_filters( 'ap_rewrites', $all_rules, $slug, $base_page_id );
		$ap_rules  = array();

		foreach ( $all_rules as $r => $re ) {
			$re             = preg_replace( '/\\$([1-9]+)/', '$matches[#]', $re );
			$re             = preg_replace_callback( '/\#/', array( __CLASS__, 'incr_hash' ), $re );
			$ap_rules[ $r ] = $re;
			self::$counter  = 1;
		}
		$front             = ltrim( $wp_rewrite->front, '/' );
		$wp_rewrite->rules = ap_array_insert_after( $wp_rewrite->rules, $front . 'type/([^/]+)/?$', $ap_rules );
		return $wp_rewrite->rules;
	}

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