2021-02-23 16:44:18 +00:00
|
|
|
<?php // phpcs:disable WordPress.Files.FileName.InvalidClassFileName
|
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-23 16:44:18 +00:00
|
|
|
* @package footnotes
|
2021-02-23 16:00:59 +00:00
|
|
|
* @since 1.5.0 14.09.14 10:58
|
|
|
|
*
|
2021-02-23 16:44:18 +00:00
|
|
|
* @lastmodified 2021-02-22T1416+0100
|
2021-02-23 17:59:52 +00:00
|
|
|
*
|
2021-02-23 16:44:18 +00:00
|
|
|
* @since 2.2.6 Adding: Templates: support for custom templates in sibling folder, thanks to @misfist issue report.
|
|
|
|
* @since 2.5.0 Adding: Templates: Enable template location stack, thanks to @misfist code contribution.
|
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.
|
|
|
|
*
|
|
|
|
* @since 1.5.0
|
|
|
|
*/
|
|
|
|
class MCI_Footnotes_Template {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Directory name for dashboard templates.
|
|
|
|
*
|
|
|
|
* @since 1.5.0
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-02-23 16:44:18 +00:00
|
|
|
const C_STR_DASHBOARD = 'dashboard';
|
2021-02-23 16:00:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Directory name for public templates.
|
|
|
|
*
|
|
|
|
* @since 1.5.0
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-02-23 16:44:18 +00:00
|
|
|
const C_STR_PUBLIC = 'public';
|
2021-02-23 16:00:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains the content of the template after initialize.
|
|
|
|
*
|
|
|
|
* @since 1.5.0
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-02-23 16:44:18 +00:00
|
|
|
private $a_str_original_content = '';
|
2021-02-23 16:00:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains the content of the template after initialize with replaced place holders.
|
|
|
|
*
|
|
|
|
* @since 1.5.0
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-02-23 16:44:18 +00:00
|
|
|
private $a_str_replaced_content = '';
|
2021-02-23 16:00:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Plugin Directory
|
|
|
|
*
|
|
|
|
* @since 2.4.0d3
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $plugin_directory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Constructor. Reads and loads the template file without replace any placeholder.
|
|
|
|
*
|
|
|
|
* @since 1.5.0
|
2021-02-23 16:44:18 +00:00
|
|
|
* @param string $p_str_file_type Template file type (take a look on the Class constants).
|
|
|
|
* @param string $p_str_file_name 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 17:59:52 +00:00
|
|
|
*
|
2021-02-23 16:44:18 +00:00
|
|
|
* - Adding: Templates: support for custom templates in sibling folder, thanks to @misfist issue report.
|
2021-02-23 17:59:52 +00:00
|
|
|
*
|
2021-02-23 16:44:18 +00:00
|
|
|
* @since 2.2.6
|
|
|
|
* @date 2020-12-19T0606+0100
|
2021-02-23 17:59:52 +00:00
|
|
|
*
|
2021-02-23 16:44:18 +00:00
|
|
|
* @reporter @misfist
|
2021-02-23 16:00:59 +00:00
|
|
|
* @link https://wordpress.org/support/topic/template-override-filter/
|
|
|
|
*/
|
2021-02-23 16:44:18 +00:00
|
|
|
public function __construct( $p_str_file_type, $p_str_file_name, $p_str_extension = 'html' ) {
|
|
|
|
// No template file type and/or file name set.
|
|
|
|
if ( empty( $p_str_file_type ) || empty( $p_str_file_name ) ) {
|
2021-02-23 16:00:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-23 16:44:18 +00:00
|
|
|
* Define plugin root path.
|
2021-02-23 16:00:59 +00:00
|
|
|
*
|
|
|
|
* @since 2.4.0d3
|
|
|
|
*/
|
|
|
|
$this->plugin_directory = plugin_dir_path( dirname( __FILE__ ) );
|
|
|
|
|
|
|
|
/**
|
2021-02-23 16:44:18 +00:00
|
|
|
* Modularize functions.
|
2021-02-23 16:00:59 +00:00
|
|
|
*
|
|
|
|
* @since 2.4.0d3
|
|
|
|
*/
|
2021-02-23 16:44:18 +00:00
|
|
|
$template = $this->get_template( $p_str_file_type, $p_str_file_name, $p_str_extension );
|
|
|
|
if ( $template ) {
|
2021-02-23 16:00:59 +00:00
|
|
|
$this->process_template( $template );
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace all placeholders specified in array.
|
|
|
|
*
|
|
|
|
* @since 1.5.0
|
2021-02-23 16:44:18 +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-23 16:44:18 +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-23 16:44:18 +00:00
|
|
|
// Template content is empty.
|
|
|
|
if ( empty( $this->a_str_replaced_content ) ) {
|
2021-02-23 16:00:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-23 16:44:18 +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_replaced_content = str_replace( '[[' . $l_str_placeholder . ']]', $l_str_value, $this->a_str_replaced_content );
|
2021-02-23 16:00:59 +00:00
|
|
|
}
|
2021-02-23 16:44:18 +00:00
|
|
|
// Success.
|
2021-02-23 16:00:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reloads the original content of the template file.
|
|
|
|
*
|
|
|
|
* @since 1.5.0
|
|
|
|
*/
|
|
|
|
public function reload() {
|
2021-02-23 16:44:18 +00:00
|
|
|
$this->a_str_replaced_content = $this->a_str_original_content;
|
2021-02-23 16:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the content of the template file with replaced placeholders.
|
|
|
|
*
|
|
|
|
* @since 1.5.0
|
|
|
|
* @return string Template content with replaced placeholders.
|
|
|
|
*/
|
2021-02-23 16:44:18 +00:00
|
|
|
public function get_content() {
|
|
|
|
return $this->a_str_replaced_content;
|
2021-02-23 16:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-23 16:44:18 +00:00
|
|
|
* Process template file.
|
2021-02-23 16:00:59 +00:00
|
|
|
*
|
|
|
|
* @since 2.4.0d3
|
|
|
|
*
|
2021-02-23 16:44:18 +00:00
|
|
|
* @param string $template The template to be processed.
|
2021-02-23 16:00:59 +00:00
|
|
|
* @return void
|
|
|
|
*
|
2021-02-23 16:44:18 +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-23 17:17:14 +00:00
|
|
|
// phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
|
|
|
|
$this->a_str_original_content = preg_replace( '#<!--.+?-->#s', '', file_get_contents( $template ) );
|
|
|
|
// phpcs:enable
|
2021-02-23 16:44:18 +00:00
|
|
|
$this->a_str_original_content = preg_replace( '#/\*\*.+?\*/#s', '', $this->a_str_original_content );
|
|
|
|
$this->a_str_original_content = str_replace( "\n", '', $this->a_str_original_content );
|
|
|
|
$this->a_str_original_content = str_replace( "\r", '', $this->a_str_original_content );
|
|
|
|
$this->a_str_original_content = str_replace( "\t", ' ', $this->a_str_original_content );
|
|
|
|
$this->a_str_original_content = preg_replace( '# +#', ' ', $this->a_str_original_content );
|
|
|
|
$this->a_str_original_content = str_replace( ' >', '>', $this->a_str_original_content );
|
2021-02-23 16:00:59 +00:00
|
|
|
$this->reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-23 16:44:18 +00:00
|
|
|
* Get the template.
|
2021-02-23 17:59:52 +00:00
|
|
|
*
|
2021-02-23 16:44:18 +00:00
|
|
|
* - Adding: Templates: Enable template location stack, thanks to @misfist code contribution.
|
|
|
|
*
|
|
|
|
* @since 2.4.0d3 Contribution.
|
|
|
|
* @since 2.5.0 Release.
|
2021-02-23 17:59:52 +00:00
|
|
|
*
|
2021-02-23 16:44:18 +00:00
|
|
|
* @contributor @misfist
|
|
|
|
* @link https://wordpress.org/support/topic/template-override-filter/#post-13864301
|
|
|
|
*
|
|
|
|
* @param string $p_str_file_type The file type of the template.
|
|
|
|
* @param string $p_str_file_name The file name of the template.
|
|
|
|
* @param string $p_str_extension The file extension of the template.
|
2021-02-23 16:00:59 +00:00
|
|
|
* @return mixed false | template path
|
|
|
|
*/
|
2021-02-23 16:44:18 +00:00
|
|
|
public function get_template( $p_str_file_type, $p_str_file_name, $p_str_extension = 'html' ) {
|
2021-02-23 16:00:59 +00:00
|
|
|
$located = false;
|
|
|
|
|
|
|
|
/**
|
2021-02-23 16:44:18 +00:00
|
|
|
* The directory can be changed.
|
|
|
|
*
|
|
|
|
* @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-23 16:44:18 +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-23 16:44:18 +00:00
|
|
|
$custom_directory = apply_filters( 'mci_footnotes_custom_template_directory', 'footnotes-custom/' );
|
|
|
|
$template_name = $p_str_file_type . '/' . $p_str_file_name . '.' . $p_str_extension;
|
2021-02-23 16:00:59 +00:00
|
|
|
|
|
|
|
/**
|
2021-02-23 16:44:18 +00:00
|
|
|
* Look in active 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-23 16:44:18 +00:00
|
|
|
/**
|
|
|
|
* Look in parent theme in case active is child.
|
|
|
|
*/
|
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-23 16:44:18 +00:00
|
|
|
/**
|
|
|
|
* Look in custom plugin 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-23 16:44:18 +00:00
|
|
|
/**
|
|
|
|
* Fall back to the templates shipped with the 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-23 16:44:18 +00:00
|
|
|
} // End of class.
|