Skip to content

Commit

Permalink
change logic for determining base template with SI (#762)
Browse files Browse the repository at this point in the history
* change logic for determining base template with SI

* add return false;

* move filters to 3rd party Sprout Invoices class
  • Loading branch information
jamesros161 authored Nov 1, 2022
1 parent 8283c22 commit b86ae7a
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/includes/class-boldgrid-framework-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Boldgrid_Framework_Wrapper {
* @since 1.1
*/
public function __construct( $template = 'base.php' ) {
$this->slug = sanitize_title( basename( $template, '.php' ) );
$this->slug = sanitize_title( basename( $template, '.php' ) );
$this->templates = array( $template );

if ( self::$base ) {
Expand All @@ -79,14 +79,16 @@ public function __construct( $template = 'base.php' ) {
/**
* Set the string value for object.
*
* This applies a filter to the final $templates array, before returning
* This applies filters to the final $templates array, before returning
* the full path to the most specific existing base template via
* locate_template()
*
* @since 1.1
*/
public function __toString() {
$this->templates = apply_filters( 'boldgrid/wrap_' . $this->slug, $this->templates );
$this->templates = apply_filters( 'bgtfw_wrapper_templates', $this->templates, self::$base, self::$main_template );

return locate_template( $this->templates );
}

Expand All @@ -107,7 +109,7 @@ public static function wrap( $main ) {
}

self::$main_template = $main;
self::$base = basename( self::$main_template, '.php' );
self::$base = basename( self::$main_template, '.php' );

// Check if index.php is the base.
if ( 'index' === self::$base ) {
Expand Down
20 changes: 20 additions & 0 deletions src/includes/class-boldgrid-framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ private function load_dependencies() {
// Load Customizer Files.
$this->load_customizer_files();

$this->load_third_party_files();

// Load Pro Feature Cards.
$this->load_pro_cards();

Expand All @@ -216,6 +218,24 @@ private function load_dependencies() {
$this->loader = new Boldgrid_Framework_Loader();
}

/**
* Include third party compatibility files.
*
* @since 2.17.2
* @access private
*/
private function load_third_party_files() {
$third_party_files = array(
'sprout-invoices',
);

foreach ( $third_party_files as $filename ) {
require_once trailingslashit( __DIR__ ) . 'third-party/class-boldgrid-framework-' . $filename . '.php';
}

Boldgrid_Framework_Sprout_Invoices::add_hooks();
}

/**
* Include additional configuration options to assign.
*
Expand Down
121 changes: 121 additions & 0 deletions src/includes/third-party/class-boldgrid-framework-sprout-invoices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* File: class-boldgrid-framework-sprout-invoices.php
*
* This class is responsible for adding Sprout Invoices support to the BoldGrid Framework.
*
* @package Boldgrid_Framework
*
* @since 2.17.2
*/

/**
* Class: Boldgrid_Framework_Sprout_Invoices
*
* This class is responsible for adding Sprout Invoices support to the BoldGrid Framework.
*
* @since 2.17.2
*/
class Boldgrid_Framework_Sprout_Invoices {
/**
* Add hooks.
*
* @since 2.17.2
*/
public static function add_hooks() {
add_filter( 'bgtfw_wrapper_templates', array( __CLASS__, 'bgtfw_wrapper_templates' ), 10, 3 );
}

/**
* BGTFW Wrapper Templates.
*
* Adjusts the templates used when various sprout invoices pages are loaded.
*
* @since 2.17.2
*/
public static function bgtfw_wrapper_templates( $templates, $base, $main_template ) {

if ( self::is_si_pdf( $base, $main_template ) ) {
$templates = array( 'si-pdf-base.php' );
} elseif ( self::is_si_invoice() ) {
$templates = array( 'si-invoice-base.php' );
} elseif ( self::is_si_estimate() ) {
$templates = array( 'si-estimate-base.php' );
}

return $templates;
}

/**
* Is this page an SI invoice?
*
* Determines if a page is a Sprout Invoices invoice
* based on the SI_Invoice::is_invoice_query() method.
*
* @since 2.17.2
*
* @return boolean
*/
public static function is_si_invoice() {
if ( class_exists( 'SI_Invoice' ) ) {
return SI_Invoice::is_invoice_query();
} else {
return false;
}
}

/**
* Is this page an SI estimate?
*
* @since 2.17.2
*
* @return boolean
*/
public static function is_si_estimate() {
global $post;

if ( ! empty( $post ) && class_exists( 'SI_Invoice' ) ) {
return 'sa_estimate' === $post->post_type;
} else {
return false;
}
}

/**
* Is this page an SI PDF?
*
* @since 2.17.2
*
* @return boolean
*/
public static function is_si_pdf( $base, $main_template ) {
global $post;

// If Sprout is not active, we won't be viewing a sprout pdf.
if ( ! class_exists( 'SI_Invoice' ) ) {
return false;
}

// When viewing the sprout invoice, these two variables are always set.
if ( ! $base || ! isset( $main_template ) ) {
return false;
}

// If template doesn't contain 'sprout-invoices' in it's path, it's not a sprout pdf.
if ( false === strpos( $main_template, 'sprout-invoices' ) ) {
return false;
}

// At this point, we know its a sprout page. If it's a pdf redirected page, we know it's a pdf.
if ( isset( $_SERVER['REDIRECT_QUERY_STRING'] ) && 'pdf=1' === $_SERVER['REDIRECT_QUERY_STRING'] ) {
return true;
}

// If the base name contians '-pdf', then it's a sprout pdf.
if ( false !== strpos( $base, '-pdf' ) ) {
return true;
}

return false;
}
}

0 comments on commit b86ae7a

Please sign in to comment.