2021-02-26 08:07:03 +00:00
< ? php
2021-02-23 16:00:59 +00:00
/**
* Includes the Template Engine to load and handle all Template files of the Plugin .
*
* @ filesource
2021-02-26 08:07:03 +00:00
* @ author Stefan Herndler
* @ since 1.5 . 0 14.09 . 14 10 : 58
*
*
* @ lastmodified 2021 - 02 - 18 T2024 + 0100
*
* @ since 2.0 . 3 prettify reference container template
* @ since 2.0 . 3 replace tab with a space
* @ since 2.0 . 3 replace 2 spaces with 1
* @ since 2.0 . 4 collapse multiple spaces
* @ since 2.0 . 6 prettify other templates ( footnote , tooltip script , ref container row )
* @ since 2.2 . 6 delete a space before a closing pointy bracket
*
* @ since 2.2 . 6 support for custom templates in fixed location , while failing to add filter thanks to @ misfist 2020 - 12 - 19 T0606 + 0100
* @ link https :// wordpress . org / support / topic / template - override - filter /
*
* @ since 2.4 . 0 templates may be in active theme , thanks to @ misfist
* @ link https :// wordpress . org / support / topic / template - override - filter / #post-13846598
2021-02-23 16:00:59 +00:00
*
2021-02-26 08:07:03 +00:00
* @ since 2.5 . 0 Enable template location stack , contributed by @ misfist
* @ link https :// wordpress . org / support / topic / template - override - filter / #post-13864301
*
* @ since 2.5 . 4 collapse HTML comments and PHP / JS docblocks ( only )
2021-02-23 16:00:59 +00:00
*/
2021-02-26 08:07:03 +00:00
2021-02-23 16:00:59 +00:00
/**
* Handles each Template file for the Plugin Frontend ( e . g . Settings Dashboard , Public pages , ... ) .
* Loads a template file , replaces all Placeholders and returns the replaced file content .
*
2021-02-26 08:07:03 +00:00
* @ author Stefan Herndler
2021-02-23 16:00:59 +00:00
* @ since 1.5 . 0
*/
class MCI_Footnotes_Template {
/**
* Directory name for dashboard templates .
*
2021-02-26 08:07:03 +00:00
* @ author Stefan Herndler
2021-02-23 16:00:59 +00:00
* @ since 1.5 . 0
* @ var string
*/
2021-02-26 08:07:03 +00:00
const C_STR_DASHBOARD = " dashboard " ;
2021-02-23 16:00:59 +00:00
/**
* Directory name for public templates .
*
2021-02-26 08:07:03 +00:00
* @ author Stefan Herndler
2021-02-23 16:00:59 +00:00
* @ since 1.5 . 0
* @ var string
*/
2021-02-26 08:07:03 +00:00
const C_STR_PUBLIC = " public " ;
2021-02-23 16:00:59 +00:00
/**
* Contains the content of the template after initialize .
*
2021-02-26 08:07:03 +00:00
* @ author Stefan Herndler
2021-02-23 16:00:59 +00:00
* @ since 1.5 . 0
* @ var string
*/
2021-02-26 08:07:03 +00:00
private $a_str_OriginalContent = " " ;
2021-02-23 16:00:59 +00:00
/**
* Contains the content of the template after initialize with replaced place holders .
*
2021-02-26 08:07:03 +00:00
* @ author Stefan Herndler
2021-02-23 16:00:59 +00:00
* @ since 1.5 . 0
* @ var string
*/
2021-02-26 08:07:03 +00:00
private $a_str_ReplacedContent = " " ;
2021-02-23 16:00:59 +00:00
/**
* Plugin Directory
*
2021-02-26 08:07:03 +00:00
* @ author Patrizia Lutz @ misfist
2021-02-23 16:00:59 +00:00
* @ since 2.4 . 0 d3
*
* @ var string
*/
public $plugin_directory ;
/**
* Class Constructor . Reads and loads the template file without replace any placeholder .
*
2021-02-26 08:07:03 +00:00
* @ author Stefan Herndler
2021-02-23 16:00:59 +00:00
* @ since 1.5 . 0
2021-02-26 08:07:03 +00:00
* @ param string $p_str_FileType Template file type ( take a look on the Class constants ) .
* @ param string $p_str_FileName Template file name inside the Template directory without the file extension .
* @ param string $p_str_Extension Optional Template file extension ( default : html )
2021-02-23 16:00:59 +00:00
*
*
2021-02-26 08:07:03 +00:00
* @ since 2.2 . 6 support for custom templates 2020 - 12 - 19 T0606 + 0100
2021-02-25 19:17:00 +00:00
* @ link https :// wordpress . org / support / topic / template - override - filter /
2021-02-26 08:07:03 +00:00
*
* @ since 2.4 . 0 look for custom template in the active theme first , thanks to @ misfist
* @ link https :// wordpress . org / support / topic / template - override - filter / #post-13846598
2021-02-23 16:00:59 +00:00
*/
2021-02-26 08:07:03 +00:00
public function __construct ( $p_str_FileType , $p_str_FileName , $p_str_Extension = " html " ) {
// no template file type and/or file name set
if ( empty ( $p_str_FileType ) || empty ( $p_str_FileName )) {
2021-02-23 16:00:59 +00:00
return ;
}
/**
2021-02-26 08:07:03 +00:00
* Define plugin root path
2021-02-23 16:00:59 +00:00
*
* @ since 2.4 . 0 d3
2021-02-26 08:07:03 +00:00
*
* @ author Patrizia Lutz @ misfist
2021-02-23 16:00:59 +00:00
*/
$this -> plugin_directory = plugin_dir_path ( dirname ( __FILE__ ) );
/**
2021-02-26 08:07:03 +00:00
* Modularize functions
2021-02-23 16:00:59 +00:00
*
* @ since 2.4 . 0 d3
2021-02-26 08:07:03 +00:00
*
* @ author Patrizia Lutz @ misfist
2021-02-23 16:00:59 +00:00
*/
2021-02-26 08:07:03 +00:00
if ( $template = $this -> get_template ( $p_str_FileType , $p_str_FileName , $p_str_Extension ) ) {
2021-02-23 16:00:59 +00:00
$this -> process_template ( $template );
} else {
return ;
}
}
/**
* Replace all placeholders specified in array .
*
2021-02-26 08:07:03 +00:00
* @ author Stefan Herndler
2021-02-23 16:00:59 +00:00
* @ since 1.5 . 0
2021-02-26 08:07:03 +00:00
* @ param array $p_arr_Placeholders Placeholders ( key = placeholder , value = value ) .
2021-02-23 16:00:59 +00:00
* @ return bool True on Success , False if Placeholders invalid .
*/
2021-02-26 08:07:03 +00:00
public function replace ( $p_arr_Placeholders ) {
// no placeholders set
if ( empty ( $p_arr_Placeholders )) {
2021-02-23 16:00:59 +00:00
return false ;
}
2021-02-26 08:07:03 +00:00
// template content is empty
if ( empty ( $this -> a_str_ReplacedContent )) {
2021-02-23 16:00:59 +00:00
return false ;
}
2021-02-26 08:07:03 +00:00
// iterate through each placeholder and replace it with its value
foreach ( $p_arr_Placeholders as $l_str_Placeholder => $l_str_Value ) {
$this -> a_str_ReplacedContent = str_replace ( " [[ " . $l_str_Placeholder . " ]] " , $l_str_Value , $this -> a_str_ReplacedContent );
2021-02-23 16:00:59 +00:00
}
2021-02-26 08:07:03 +00:00
// success
2021-02-23 16:00:59 +00:00
return true ;
}
/**
* Reloads the original content of the template file .
*
2021-02-26 08:07:03 +00:00
* @ author Stefan Herndler
2021-02-23 16:00:59 +00:00
* @ since 1.5 . 0
*/
public function reload () {
2021-02-26 08:07:03 +00:00
$this -> a_str_ReplacedContent = $this -> a_str_OriginalContent ;
2021-02-23 16:00:59 +00:00
}
/**
* Returns the content of the template file with replaced placeholders .
*
2021-02-26 08:07:03 +00:00
* @ author Stefan Herndler
2021-02-23 16:00:59 +00:00
* @ since 1.5 . 0
* @ return string Template content with replaced placeholders .
*/
2021-02-26 08:07:03 +00:00
public function getContent () {
return $this -> a_str_ReplacedContent ;
2021-02-23 16:00:59 +00:00
}
/**
2021-02-26 08:07:03 +00:00
* Process template file
*
* @ author Patrizia Lutz @ misfist
2021-02-23 16:00:59 +00:00
*
* @ since 2.4 . 0 d3
*
2021-02-26 08:07:03 +00:00
* @ param string $template
2021-02-23 16:00:59 +00:00
* @ return void
*
2021-02-26 08:07:03 +00:00
*
* @ since 2.0 . 3 replace tab with a space
* @ since 2.0 . 3 replace 2 spaces with 1
* @ since 2.0 . 4 collapse multiple spaces
* @ since 2.2 . 6 delete a space before a closing pointy bracket
* @ since 2.5 . 4 collapse HTML comments and PHP / JS docblocks ( only )
2021-02-23 16:00:59 +00:00
*/
public function process_template ( $template ) {
2021-02-26 08:07:03 +00:00
$this -> a_str_OriginalContent = preg_replace ( '#<!--.+?-->#s' , " " , file_get_contents ( $template ) );
$this -> a_str_OriginalContent = preg_replace ( '#/\*\*.+?\*/#s' , " " , $this -> a_str_OriginalContent );
$this -> a_str_OriginalContent = str_replace ( " \n " , " " , $this -> a_str_OriginalContent );
$this -> a_str_OriginalContent = str_replace ( " \r " , " " , $this -> a_str_OriginalContent );
$this -> a_str_OriginalContent = str_replace ( " \t " , " " , $this -> a_str_OriginalContent );
$this -> a_str_OriginalContent = preg_replace ( '# +#' , " " , $this -> a_str_OriginalContent );
$this -> a_str_OriginalContent = str_replace ( " > " , " > " , $this -> a_str_OriginalContent );
2021-02-23 16:00:59 +00:00
$this -> reload ();
}
/**
2021-02-26 08:07:03 +00:00
* Get the template
2021-02-23 16:00:59 +00:00
*
2021-02-26 08:07:03 +00:00
* @ author Patrizia Lutz @ misfist
2021-02-23 16:00:59 +00:00
*
2021-02-26 08:07:03 +00:00
* @ since 2.4 . 0 d3
2021-02-25 19:17:00 +00:00
*
2021-02-26 08:07:03 +00:00
* @ param string $p_str_FileType
* @ param string $p_str_FileName
* @ param string $p_str_Extension
2021-02-23 16:00:59 +00:00
* @ return mixed false | template path
*/
2021-02-26 08:07:03 +00:00
public function get_template ( $p_str_FileType , $p_str_FileName , $p_str_Extension = " html " ) {
2021-02-23 16:00:59 +00:00
$located = false ;
/**
2021-02-26 08:07:03 +00:00
* The directory change be modified
* @ usage to change location of templates to ` template_parts / footnotes / ' :
2021-02-23 16:00:59 +00:00
* add_filter ( 'mci_footnotes_template_directory' , function ( $directory ) {
2021-02-26 08:07:03 +00:00
* return ' template_parts / footnotes / ;
2021-02-23 16:00:59 +00:00
* } );
*/
$template_directory = apply_filters ( 'mci_footnotes_template_directory' , 'footnotes/templates/' );
2021-02-26 08:07:03 +00:00
$custom_directory = apply_filters ( 'mci_footnotes_custom_template_directory' , 'footnotes-custom/' );
$template_name = $p_str_FileType . '/' . $p_str_FileName . '.' . $p_str_Extension ;
2021-02-23 16:00:59 +00:00
/**
2021-02-26 08:07:03 +00:00
* Look in active ( child ) theme
2021-02-23 16:00:59 +00:00
*/
if ( file_exists ( trailingslashit ( get_stylesheet_directory () ) . $template_directory . $template_name ) ) {
$located = trailingslashit ( get_stylesheet_directory () ) . $template_directory . $template_name ;
2021-02-26 08:07:03 +00:00
/**
* Look in parent theme
*/
2021-02-23 16:00:59 +00:00
} elseif ( file_exists ( trailingslashit ( get_template_directory () ) . $template_directory . $template_name ) ) {
$located = trailingslashit ( get_template_directory () ) . $template_directory . $template_name ;
2021-02-26 08:07:03 +00:00
/**
* Look in custom directory
*/
2021-02-23 16:00:59 +00:00
} elseif ( file_exists ( trailingslashit ( WP_PLUGIN_DIR ) . $custom_directory . 'templates/' . $template_name ) ) {
$located = trailingslashit ( WP_PLUGIN_DIR ) . $custom_directory . 'templates/' . $template_name ;
2021-02-26 08:07:03 +00:00
/**
* Look in plugin
*/
2021-02-23 16:00:59 +00:00
} elseif ( file_exists ( $this -> plugin_directory . 'templates/' . $template_name ) ) {
$located = $this -> plugin_directory . 'templates/' . $template_name ;
}
return $located ;
}
2021-02-26 08:07:03 +00:00
} // end of class