refactor: namespace everything
This commit is contained in:
parent
6683c0b169
commit
ce41cd8353
21 changed files with 939 additions and 839 deletions
228
src/includes/class-template.php
Normal file
228
src/includes/class-template.php
Normal file
|
@ -0,0 +1,228 @@
|
|||
<?php
|
||||
/**
|
||||
* File providing the `Template` class.
|
||||
*
|
||||
* @package footnotes
|
||||
* @subpackage includes
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @since 2.2.6 Add support for custom templates in sibling folder.
|
||||
* @since 2.8.0 Rename file from `templates.php` to `class-footnotes-templates.php`,
|
||||
* rename `class/` sub-directory to `includes/`.
|
||||
*/
|
||||
|
||||
namespace footnotes\includes;
|
||||
|
||||
/**
|
||||
* Class defining template rendering.
|
||||
*
|
||||
* Loads a template file, replaces all Placeholders and returns the replaced
|
||||
* file content.
|
||||
|
||||
* @package footnotes
|
||||
* @subpackage includes
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @todo Refactor templating.
|
||||
*/
|
||||
class Template {
|
||||
|
||||
/**
|
||||
* Directory name for dashboard partials.
|
||||
*
|
||||
* @var string
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
const C_STR_DASHBOARD = 'admin/partials';
|
||||
|
||||
/**
|
||||
* Directory name for public partials.
|
||||
*
|
||||
* @var string
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
const C_STR_PUBLIC = 'public/partials';
|
||||
|
||||
/**
|
||||
* Contains the content of the template after initialize.
|
||||
*
|
||||
* @var string
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
private $a_str_original_content = '';
|
||||
|
||||
/**
|
||||
* Contains the content of the template after initialize with replaced place holders.
|
||||
*
|
||||
* @var string
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
private $a_str_replaced_content = '';
|
||||
|
||||
/**
|
||||
* Plugin Directory
|
||||
*
|
||||
* @var string
|
||||
*
|
||||
* @since 2.4.0d3
|
||||
*/
|
||||
public $plugin_directory;
|
||||
|
||||
/**
|
||||
* Class Constructor. Reads and loads the template file without replace any placeholder.
|
||||
*
|
||||
* @param string $p_str_file_type Template file type.
|
||||
* @param string $p_str_file_name Template file name inside the `partials/` directory, without the file extension.
|
||||
* @param string $p_str_extension (optional) Template file extension (default: 'html').
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @todo Refactor templating.
|
||||
*/
|
||||
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 ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->plugin_directory = plugin_dir_path( dirname( __FILE__ ) );
|
||||
|
||||
$template = $this->get_template( $p_str_file_type, $p_str_file_name, $p_str_extension );
|
||||
if ( $template ) {
|
||||
$this->process_template( $template );
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace all placeholders specified in array.
|
||||
*
|
||||
* @param array $p_arr_placeholders Placeholders (key = placeholder, value = value).
|
||||
* @return bool `true` on Success, `false` if placeholders invalid.
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @todo Refactor templating.
|
||||
*/
|
||||
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_replaced_content ) ) {
|
||||
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_replaced_content = str_replace( '[[' . $l_str_placeholder . ']]', $l_str_value, $this->a_str_replaced_content );
|
||||
}
|
||||
// Success.
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads the original content of the template file.
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @todo Refactor templating.
|
||||
*/
|
||||
public function reload() {
|
||||
$this->a_str_replaced_content = $this->a_str_original_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the template file with replaced placeholders.
|
||||
*
|
||||
* @return string Template content with replaced placeholders.
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @todo Refactor templating.
|
||||
*/
|
||||
public function get_content() {
|
||||
return $this->a_str_replaced_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process template file.
|
||||
*
|
||||
* @param string $template The template to be processed.
|
||||
* @return void
|
||||
*
|
||||
* @since 2.4.0d3
|
||||
* @todo Refactor templating.
|
||||
*/
|
||||
public function process_template( $template ) {
|
||||
// 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
|
||||
$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 );
|
||||
$this->reload();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the template.
|
||||
*
|
||||
* @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.
|
||||
* @return mixed `false` or the template path
|
||||
*
|
||||
* @since 2.5.0
|
||||
* @todo Refactor templating.
|
||||
*/
|
||||
public function get_template( $p_str_file_type, $p_str_file_name, $p_str_extension = 'html' ) {
|
||||
$located = false;
|
||||
|
||||
/**
|
||||
* The directory can be changed.
|
||||
*
|
||||
* @usage to change location of templates to 'template_parts/footnotes/':
|
||||
* add_filter( 'template_directory', function( $directory ) {
|
||||
* return 'template_parts/footnotes/';
|
||||
* } );
|
||||
*
|
||||
* @todo Review.
|
||||
*/
|
||||
$template_directory = apply_filters( '', 'footnotes/' );
|
||||
$custom_directory = apply_filters( 'custom_template_directory', 'footnotes-custom/' );
|
||||
$template_name = $p_str_file_type . '/' . $p_str_file_name . '.' . $p_str_extension;
|
||||
|
||||
/**
|
||||
* Look in active theme.
|
||||
*/
|
||||
if ( file_exists( trailingslashit( get_stylesheet_directory() ) . $template_directory . $template_name ) ) {
|
||||
$located = trailingslashit( get_stylesheet_directory() ) . $template_directory . $template_name;
|
||||
|
||||
/**
|
||||
* Look in parent theme in case active is child.
|
||||
*/
|
||||
} elseif ( file_exists( trailingslashit( get_template_directory() ) . $template_directory . $template_name ) ) {
|
||||
$located = trailingslashit( get_template_directory() ) . $template_directory . $template_name;
|
||||
|
||||
/**
|
||||
* Look in custom plugin directory.
|
||||
*/
|
||||
} elseif ( file_exists( trailingslashit( WP_PLUGIN_DIR ) . $custom_directory . 'templates/' . $template_name ) ) {
|
||||
$located = trailingslashit( WP_PLUGIN_DIR ) . $custom_directory . 'templates/' . $template_name;
|
||||
|
||||
/**
|
||||
* Fall back to the templates shipped with the plugin.
|
||||
*/
|
||||
} elseif ( file_exists( $this->plugin_directory . $template_name ) ) {
|
||||
$located = $this->plugin_directory . $template_name;
|
||||
}
|
||||
|
||||
return $located;
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue