2014-09-14 20:32:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Includes the Template Engine to load and handle all Template files of the Plugin.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @author Stefan Herndler
|
|
|
|
* @since 1.5.0 14.09.14 10:58
|
2020-12-19 06:18:56 +00:00
|
|
|
*
|
|
|
|
* Edited:
|
|
|
|
* @since 2.0.3 further minify template content
|
|
|
|
* @since 2.0.4 regex to delete multiple spaces
|
2021-01-01 21:24:43 +00:00
|
|
|
*
|
|
|
|
* @since 2.2.6 support for custom templates in fixed location, while failing to add filter thanks to @misfist 2020-12-19T0606+0100
|
|
|
|
* @see <https://wordpress.org/support/topic/template-override-filter/>
|
|
|
|
* @since 2.3.1 templates may be in active theme, thanks to @misfist
|
|
|
|
* @see <https://wordpress.org/support/topic/template-override-filter/#post-13846598>
|
|
|
|
*
|
|
|
|
* Last modified: 2021-01-01T2214+0100
|
2014-09-14 20:32:59 +00:00
|
|
|
*/
|
2020-11-01 05:41:22 +00:00
|
|
|
|
2014-09-14 20:32: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.
|
|
|
|
*
|
|
|
|
* @author Stefan Herndler
|
|
|
|
* @since 1.5.0
|
|
|
|
*/
|
|
|
|
class MCI_Footnotes_Template {
|
|
|
|
|
2020-11-01 05:41:22 +00:00
|
|
|
/**
|
|
|
|
* Directory name for dashboard templates.
|
|
|
|
*
|
|
|
|
* @author Stefan Herndler
|
|
|
|
* @since 1.5.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const C_STR_DASHBOARD = "dashboard";
|
2014-09-14 20:32:59 +00:00
|
|
|
|
2020-11-01 05:41:22 +00:00
|
|
|
/**
|
|
|
|
* Directory name for public templates.
|
|
|
|
*
|
|
|
|
* @author Stefan Herndler
|
|
|
|
* @since 1.5.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const C_STR_PUBLIC = "public";
|
2014-09-14 20:32:59 +00:00
|
|
|
|
2020-11-01 05:41:22 +00:00
|
|
|
/**
|
|
|
|
* Contains the content of the template after initialize.
|
|
|
|
*
|
|
|
|
* @author Stefan Herndler
|
|
|
|
* @since 1.5.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $a_str_OriginalContent = "";
|
2014-09-14 20:32:59 +00:00
|
|
|
|
2020-11-01 05:41:22 +00:00
|
|
|
/**
|
|
|
|
* Contains the content of the template after initialize with replaced place holders.
|
|
|
|
*
|
|
|
|
* @author Stefan Herndler
|
|
|
|
* @since 1.5.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $a_str_ReplacedContent = "";
|
2014-09-14 20:32:59 +00:00
|
|
|
|
2020-11-01 05:41:22 +00:00
|
|
|
/**
|
|
|
|
* Class Constructor. Reads and loads the template file without replace any placeholder.
|
|
|
|
*
|
|
|
|
* @author Stefan Herndler
|
|
|
|
* @since 1.5.0
|
|
|
|
* @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)
|
2020-12-19 06:18:56 +00:00
|
|
|
*
|
|
|
|
* Edited:
|
|
|
|
* @since 2.0.3 further minify template content
|
|
|
|
* @since 2.0.4 regex to delete multiple spaces
|
2021-01-01 21:24:43 +00:00
|
|
|
*
|
|
|
|
* @since 2.2.6 support for custom templates 2020-12-19T0606+0100
|
|
|
|
* @see <https://wordpress.org/support/topic/template-override-filter/>
|
|
|
|
*
|
|
|
|
* @since 2.2.6 regex to delete a space before a closing pointy bracket
|
2020-12-19 06:18:56 +00:00
|
|
|
*
|
2021-01-01 21:24:43 +00:00
|
|
|
* @since 2.3.1 look for custom template in the active theme
|
|
|
|
* @see <https://wordpress.stackexchange.com/questions/220942/how-to-get-the-active-themes-slug>
|
2020-11-01 05:41:22 +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)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-19 06:18:56 +00:00
|
|
|
|
2021-01-01 21:24:43 +00:00
|
|
|
// First look for a custom template in a 'footnotes-custom' sibling folder:
|
2020-12-19 06:18:56 +00:00
|
|
|
$l_str_TemplateFile = dirname(__FILE__) . "/../../footnotes-custom/templates/" . $p_str_FileType . "/" . $p_str_FileName . "." . $p_str_Extension;
|
2021-01-01 21:24:43 +00:00
|
|
|
|
|
|
|
// else load template from the active theme:
|
2020-11-01 05:41:22 +00:00
|
|
|
if (!file_exists($l_str_TemplateFile)) {
|
2020-12-19 06:18:56 +00:00
|
|
|
|
2021-01-01 21:24:43 +00:00
|
|
|
$l_str_TemplateFile = dirname(__FILE__) . "/../../../themes/";
|
|
|
|
// get active theme dir name (parent theme unlikely to contain custom templates):
|
|
|
|
// see <https://wordpress.stackexchange.com/questions/220942/how-to-get-the-active-themes-slug>
|
|
|
|
$l_str_TemplateFile .= get_stylesheet();
|
|
|
|
$l_str_TemplateFile .= "/templates/footnotes/" . $p_str_FileName . "." . $p_str_Extension;
|
|
|
|
|
2020-12-23 03:34:51 +00:00
|
|
|
// else load internal template:
|
2020-12-19 06:18:56 +00:00
|
|
|
if (!file_exists($l_str_TemplateFile)) {
|
2021-01-01 21:24:43 +00:00
|
|
|
|
|
|
|
// get absolute path to the specified template file
|
|
|
|
$l_str_TemplateFile = dirname(__FILE__) . "/../templates/" . $p_str_FileType . "/" . $p_str_FileName . "." . $p_str_Extension;
|
|
|
|
|
|
|
|
// do nothing if template file does not exist:
|
|
|
|
if (!file_exists($l_str_TemplateFile)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-19 06:18:56 +00:00
|
|
|
}
|
2020-11-01 05:41:22 +00:00
|
|
|
}
|
2020-12-19 06:18:56 +00:00
|
|
|
|
2020-12-23 03:34:51 +00:00
|
|
|
// minify template to some extent:
|
2020-11-01 05:41:22 +00:00
|
|
|
// get Template file content
|
|
|
|
$this->a_str_OriginalContent = str_replace("\n", "", file_get_contents($l_str_TemplateFile));
|
|
|
|
$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);
|
2020-12-23 03:34:51 +00:00
|
|
|
$this->a_str_OriginalContent = str_replace(" >", ">", $this->a_str_OriginalContent);
|
2020-11-01 05:41:22 +00:00
|
|
|
$this->reload();
|
|
|
|
}
|
2014-09-14 20:32:59 +00:00
|
|
|
|
2020-11-01 05:41:22 +00:00
|
|
|
/**
|
|
|
|
* Replace all placeholders specified in array.
|
|
|
|
*
|
|
|
|
* @author Stefan Herndler
|
|
|
|
* @since 1.5.0
|
|
|
|
* @param array $p_arr_Placeholders Placeholders (key = placeholder, value = value).
|
|
|
|
* @return bool True on Success, False if Placeholders invalid.
|
|
|
|
*/
|
|
|
|
public function replace($p_arr_Placeholders) {
|
|
|
|
// no placeholders set
|
|
|
|
if (empty($p_arr_Placeholders)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// template content is empty
|
|
|
|
if (empty($this->a_str_ReplacedContent)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
// success
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-14 20:32:59 +00:00
|
|
|
|
2020-11-01 05:41:22 +00:00
|
|
|
/**
|
|
|
|
* Reloads the original content of the template file.
|
|
|
|
*
|
|
|
|
* @author Stefan Herndler
|
|
|
|
* @since 1.5.0
|
|
|
|
*/
|
|
|
|
public function reload() {
|
|
|
|
$this->a_str_ReplacedContent = $this->a_str_OriginalContent;
|
|
|
|
}
|
2014-09-14 20:32:59 +00:00
|
|
|
|
2020-11-01 05:41:22 +00:00
|
|
|
/**
|
|
|
|
* Returns the content of the template file with replaced placeholders.
|
|
|
|
*
|
|
|
|
* @author Stefan Herndler
|
|
|
|
* @since 1.5.0
|
|
|
|
* @return string Template content with replaced placeholders.
|
|
|
|
*/
|
|
|
|
public function getContent() {
|
|
|
|
return $this->a_str_ReplacedContent;
|
|
|
|
}
|
2014-09-14 20:32:59 +00:00
|
|
|
|
2020-10-29 13:17:36 +00:00
|
|
|
} // end of class
|