wp_is_rest_endpoint() │ WP 6.5.0

Click here to view original web page at wp-kama.ru

Пример из кода плагина Pods – Custom Content Types and Fields из файла pods/src/Pods/WP/Bindings.php.

/**
 * Get the bound value for a bound block.
 *
 * @since 3.2.0
 *
 * @param array    $source_args    List of source arguments from the block.
 * @param WP_Block $block_instance The block instance.
 * @param string   $attribute_name The name of the block attribute.
 *
 * @return string The bound value.
 */
public function get_value( $source_args, $block_instance, $attribute_name ) {
	if ( empty( $source_args['field'] ) ) {
			if ( is_admin() || wp_is_rest_endpoint() || pods_is_admin() ) {
					return __( 'You must provide the "field" of the field to bind.', 'pods' );
			}
			return '';
	}

	/** @var Field $field_block */
	$field_block = pods_container( 'pods.blocks.field' );

	if ( ! $field_block ) {
			if ( is_admin() || wp_is_rest_endpoint() || pods_is_admin() ) {
					return __( 'Pods blocks are not enabled.', 'pods' );
			}
			return '';
	}

	$value = $field_block->render( $source_args, '', $block_instance );

	// Only support full HTML for the content attribute.
	if ( 'content' !== $attribute_name ) {
			$value = wp_strip_all_tags( $value );
	}

	return $value;
}

Пример из кода плагина Markup Markdown из файла MarkupMarkdown/Core/Support.php.

/**
 * Output the rendering of markdown
 *
 * @since 3.3.4
 * @access public
 *
 * @return Void
 */
public function whitelist_wp_api() {
		if ( ! wp_is_rest_endpoint() ) :
				return false;
		endif;
		$this->prepare_markdown_editor();
		# Allow markdown on REST API with terms description
		add_filter( 'rest_prepare_category', array( $this, 'prepare_desc_field' ), 10, 3 );
		add_filter( 'rest_prepare_post_tag', array( $this, 'prepare_desc_field' ), 10, 3 );
		if ( function_exists( 'get_taxonomies' ) ) :
				$my_taxonomies = get_taxonomies( array( 'show_in_rest' => true, '_builtin' => false ) );
				foreach( $my_taxonomies as $tax ) :
						add_filter( 'rest_prepare_' . $tax, array( $this, 'prepare_desc_field' ), 10, 3 );
				endforeach;
		endif;
		$this->set_content_filters();
}

/**
 * Tiny switch to apply or not the markdown filters
 * Since 3.0: Checking the post type inside the loop
 * https://developer.wordpress.org/reference/hooks/the_content/
 *
 * @access public
 * @since 2.0
 *
 * @param String $field_content the HTML content
 * @param Integer $cache_allowed 1 if cache is allowed with the field
 *
 * @return String $content The modified HTML content
 */
private function content_data( $field_content, $cache_allowed ) {
		if ( wp_is_rest_endpoint() || ( ( is_home() || is_front_page() || is_singular() || is_archive() ) && in_the_loop() && is_main_query() ) ) :
				if ( post_type_supports( get_post_type(), 'markup_markdown' ) ) :
						return apply_filters( 'post_markdown2html', $field_content, $cache_allowed );
				else :
						return $field_content;
				endif;
		else :
				return $field_content;
		endif;
}