Inventory_Presser_Template_Provider

Inventory_Presser_Template_Provider

Description Description

This class provides single and archive templates for the post type created by this plugin if the active theme does not contain any.


Top ↑

Source Source

File: includes/class-template-provider.php

	class Inventory_Presser_Template_Provider {

		/**
		 * Adds hooks to catch requests for vehicle singles and archives.
		 *
		 * @return void
		 */
		public function add_hooks() {
			add_filter( 'single_template', array( $this, 'maybe_provide_template' ) );
			add_filter( 'archive_template', array( $this, 'maybe_provide_template' ) );
		}

		/**
		 * This method lies about whether vehicles have thumbnails so that all
		 * template content can be handled by the shortcode.
		 *
		 * @param  bool             $has_thumbnail true if the post has a post thumbnail, otherwise false.
		 * @param  int|WP_Post|null $post Post ID or WP_Post object. Default is global $post.
		 * @param  int|false        $thumbnail_id Post thumbnail ID or false if the post does not exist.
		 * @return bool
		 */
		public static function lie_about_post_thumbnails( $has_thumbnail, $post, $thumbnail_id ) {
			if ( ! empty( $post ) && INVP::POST_TYPE !== get_post_type( $post ) ) {
				return $has_thumbnail;
			}

			$is_vehicle_photo = 'attachment' === get_post_type( $thumbnail_id )
				&& ! empty( INVP::get_meta( 'photo_number', $thumbnail_id ) );

			if ( ! $is_vehicle_photo ) {
				return $has_thumbnail;
			}

			// if it's a vehicle with a vehicle photo, lie and say no.
			return false;
		}

		/**
		 * 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.
		 *
		 * @param  string $template The template file to load.
		 * @return string The same template file that was passed in
		 */
		public function maybe_provide_template( $template ) {

			// Is this setting enabled?
			$settings = INVP::settings();
			if ( ! isset( $settings['provide_templates'] ) || ! $settings['provide_templates'] ) {
				// No.
				return $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 );

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

		/**
		 * Returns the output of the 
		
		 shortcode.
		 *
		 * @param  string $content The post content as provided by the the_content filter.
		 * @return string The output of the 
		
		 shortcode
		 */
		public function replace_content_with_shortcode_archive( $content ) {
			return do_shortcode( '
		
		' );
		}

		/**
		 * Filter callback. Returns the output of the 
		 * shortcode regardless of what is passed in.
		 *
		 * @param  string $content The post content as provided by the the_content filter.
		 * @return string The output of the  shortcode
		 */
		public function replace_content_with_shortcode_single( $content ) {
			/**
			 * Avoid running the shortcode more than necessary by checking if
			 * this stylesheet is already enqueued. Some themes, like
			 * GeneratePress, apply filters to the_content a few times.
			 */
			if ( wp_style_is( 'invp_single_vehicle' ) ) {
				// Remove the filter we're in to avoid nested calls.
				remove_filter( 'the_content', array( $this, 'replace_content_with_shortcode_single' ) );
			}
			return do_shortcode( '' );
		}
	}

Top ↑

Methods Methods

  • add_hooks — Adds hooks to catch requests for vehicle singles and archives.
  • lie_about_post_thumbnails — This method lies about whether vehicles have thumbnails so that all template content can be handled by the shortcode.
  • maybe_provide_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.
  • replace_content_with_shortcode_archive — Returns the output of the shortcode.
  • replace_content_with_shortcode_single — Filter callback. Returns the output of the shortcode regardless of what is passed in.

Top ↑

Changelog Changelog

Changelog
Version Description
10.7.0 Introduced.