Inventory_Presser_Template_Provider::maybe_provide_template( string $template )

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.


Parameters

$template

(string) (Required) The template file to load.


Return

(string) The same template file that was passed in


Source

File: includes/class-template-provider.php

		public 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.
				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;
			}

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

			// Lie to themes using has_post_thumbnail() statically.
			add_filter( 'has_post_thumbnail', array( __CLASS__, 'lie_about_post_thumbnails' ), 10, 3 );
			// Except when posts are being output by the Divi Blog Module.
			add_filter( 'et_builder_blog_query', array( $this, 'dont_lie_to_divi_blog_module' ) );

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