Last active
April 2, 2020 15:39
-
-
Save oxyc/f18c7a417f48772cbe25ac3426c58f04 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Roots\Acorn\ServiceProvider; | |
use Illuminate\Support\Str; | |
use function Roots\view; | |
class WooCommerceServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->viewFinder = $this->app['view.finder']; | |
$this->sageFinder = $this->app['sage.finder']; | |
$this->sage = $this->app['sage']; | |
} | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
$this->applyThemeModifications(); | |
// Unhook Sage's filter and run our filter after WooCommerce. | |
remove_filter('comments_template', [$this->sage, 'filterCommentsTemplate']); | |
add_filter('comments_template', [$this, 'reviewsTemplate'], 11); | |
add_action('after_setup_theme', [$this, 'declareThemeSupport']); | |
add_filter('template_include', [$this, 'templateInclude'], 11); | |
add_filter('woocommerce_locate_template', [$this, 'template']); | |
add_filter('wc_get_template_part', [$this, 'template']); | |
} | |
public function applyThemeModifications(): void | |
{ | |
// Load the template hook overrides if available. | |
locate_template('app/wc-template-hooks.php', true, true); | |
/** | |
* Disable all default styles. | |
*/ | |
// add_filter('woocommerce_enqueue_styles', '__return_empty_array'); | |
/** | |
* Remove Wistia. | |
*/ | |
add_filter('woocommerce_enable_admin_help_tab', '__return_false'); | |
} | |
/** | |
* Declare theme support. | |
*/ | |
public function declareThemeSupport(): void | |
{ | |
add_theme_support('woocommerce'); | |
// add_theme_support('wc-product-gallery-zoom'); | |
add_theme_support('wc-product-gallery-lightbox'); | |
// add_theme_support('wc-product-gallery-slider'); | |
} | |
/** | |
* Support blade templates for the main template include. | |
*/ | |
public function templateInclude(string $template): string | |
{ | |
if (strpos($template, \WC_ABSPATH) === -1) { | |
return $template; | |
} | |
return $this->locateThemeTemplate($template) ?: $template; | |
} | |
/** | |
* Support blade templates for the woocommerce comments/reviews. | |
*/ | |
public function reviewsTemplate(string $template): string | |
{ | |
// Unless it's a WC template, keep using the Sage's default filter. | |
if (strpos($template, \WC_ABSPATH) === -1) { | |
return $this->sage->filterCommentsTemplate($template); | |
} | |
return $this->template($template); | |
} | |
/** | |
* Filter a template path, taking into account theme templates and creating | |
* blade loaders as needed. | |
*/ | |
public function template(string $template): string | |
{ | |
// Locate any matching template within the theme. | |
$themeTemplate = $this->locateThemeTemplate($template); | |
if (!$themeTemplate) { | |
return $template; | |
} | |
// Include directly unless it's a blade file. | |
if (!Str::endsWith($themeTemplate, '.blade.php')) { | |
return $themeTemplate; | |
} | |
// We have a template, create a loader file and return it's path. | |
return view( | |
$this->viewFinder->getPossibleViewNameFromPath($themeTemplate) | |
)->makeLoader(); | |
} | |
/** | |
* Locate the theme's woocommerce blade template when available. | |
*/ | |
protected function locateThemeTemplate(string $template): string | |
{ | |
$themeTemplate = WC()->template_path() . str_replace(\WC_ABSPATH . 'templates/', '', $template); | |
return locate_template($this->sageFinder->locate($themeTemplate)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment