Inventory_Presser_Template_Provider::maybe_provide_template( string $template )

maybe_provide_template


Description Description

This method decides whether or not to add filters to the_content and has_post_thumbnail by examining the template file. If the theme does not have templates for vehicle singles and archives, the filters are added and shortcodes provide the templates instead. I stole this technique from WooCommerce, and it's kind of beautiful.


Top ↑

Parameters Parameters

$template

(string) (Required) The template file to load


Top ↑

Return Return

(string) The same template file that was passed in


Top ↑

Source Source

File: includes/class-template-provider.php

		function maybe_provide_template( $template )
		{
			//is this our vehicle post type?
			global $post;
			if( empty( $post ) )
			{
				return $template;
			}

			if ( INVP::POST_TYPE != $post->post_type )
			{
				//no, who cares what happens
				return $template;
			}

			$single_or_archive = str_replace( '_template', '', current_filter() );

			remove_filter( $single_or_archive . '_template', array( $this, 'maybe_provide_template' ) );

			if( ! empty( $template )
				&& (
					( 'archive' == $single_or_archive && 'archive-' . INVP::POST_TYPE . '.php' == basename( $template ) )
					|| ( 'single' == $single_or_archive && 'single-' . INVP::POST_TYPE . '.php' == basename( $template ) )
				) )
			{
				//the current theme already has a template
				return $template;
			}

			//lie to themes using has_post_thumbnail() statically
			add_filter( 'has_post_thumbnail', array( __CLASS__, 'lie_about_post_thumbnails' ), 10, 3 );

			//filter the post content to use a shortcode instead
			add_filter( 'the_content', array( $this, 'replace_content_with_shortcode_' . $single_or_archive ) );

			//Still return the empty template
			return $template;
		}