Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change logic for determining base template with SI #762

Merged
merged 3 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}