Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/sitepres.../classes/templati...
File: class-wpml-templates-factory.php
<?php
[0] Fix | Delete
[1] Fix | Delete
use WPML\Core\Twig_Environment;
[2] Fix | Delete
use WPML\Core\Twig_Error_Syntax;
[3] Fix | Delete
use WPML\Core\Twig_Error_Runtime;
[4] Fix | Delete
use WPML\Core\Twig_Error_Loader;
[5] Fix | Delete
use WPML\Core\Twig_LoaderInterface;
[6] Fix | Delete
[7] Fix | Delete
abstract class WPML_Templates_Factory {
[8] Fix | Delete
const NOTICE_GROUP = 'template_factory';
[9] Fix | Delete
const OTGS_TWIG_CACHE_DISABLED_KEY = '_otgs_twig_cache_disabled';
[10] Fix | Delete
[11] Fix | Delete
/** @var array */
[12] Fix | Delete
protected $custom_filters;
[13] Fix | Delete
[14] Fix | Delete
/** @var array */
[15] Fix | Delete
protected $custom_functions;
[16] Fix | Delete
[17] Fix | Delete
/** @var string|array */
[18] Fix | Delete
protected $template_paths;
[19] Fix | Delete
[20] Fix | Delete
/** @var string|bool */
[21] Fix | Delete
protected $cache_directory;
[22] Fix | Delete
[23] Fix | Delete
protected $template_string;
[24] Fix | Delete
[25] Fix | Delete
/** @var WPML_WP_API $wp_api */
[26] Fix | Delete
private $wp_api;
[27] Fix | Delete
[28] Fix | Delete
/** @var Twig_Environment */
[29] Fix | Delete
protected $twig;
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* WPML_Templates_Factory constructor.
[33] Fix | Delete
*
[34] Fix | Delete
* @param array $custom_functions
[35] Fix | Delete
* @param array $custom_filters
[36] Fix | Delete
* @param WPML_WP_API $wp_api
[37] Fix | Delete
*/
[38] Fix | Delete
public function __construct( array $custom_functions = array(), array $custom_filters = array(), $wp_api = null ) {
[39] Fix | Delete
$this->init_template_base_dir();
[40] Fix | Delete
$this->custom_functions = $custom_functions;
[41] Fix | Delete
$this->custom_filters = $custom_filters;
[42] Fix | Delete
[43] Fix | Delete
if ( $wp_api ) {
[44] Fix | Delete
$this->wp_api = $wp_api;
[45] Fix | Delete
}
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
abstract protected function init_template_base_dir();
[49] Fix | Delete
[50] Fix | Delete
public function show( $template = null, $model = null ) {
[51] Fix | Delete
echo $this->get_view( $template, $model );
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* @param $template
[56] Fix | Delete
* @param $model
[57] Fix | Delete
*
[58] Fix | Delete
* @return string
[59] Fix | Delete
* @throws Twig_Error_Syntax
[60] Fix | Delete
* @throws Twig_Error_Runtime
[61] Fix | Delete
* @throws Twig_Error_Loader
[62] Fix | Delete
*/
[63] Fix | Delete
public function get_view( $template = null, $model = null ) {
[64] Fix | Delete
$output = '';
[65] Fix | Delete
$this->maybe_init_twig();
[66] Fix | Delete
[67] Fix | Delete
if ( null === $model ) {
[68] Fix | Delete
$model = $this->get_model();
[69] Fix | Delete
}
[70] Fix | Delete
if ( null === $template ) {
[71] Fix | Delete
$template = $this->get_template();
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
try {
[75] Fix | Delete
$output = $this->twig->render( $template, $model );
[76] Fix | Delete
} catch ( RuntimeException $e ) {
[77] Fix | Delete
if ( $this->is_caching_enabled() ) {
[78] Fix | Delete
$this->disable_twig_cache();
[79] Fix | Delete
$this->twig = null;
[80] Fix | Delete
$this->maybe_init_twig();
[81] Fix | Delete
$output = $this->get_view( $template, $model );
[82] Fix | Delete
} else {
[83] Fix | Delete
$this->add_exception_notice( $e );
[84] Fix | Delete
}
[85] Fix | Delete
} catch ( Twig_Error_Syntax $e ) {
[86] Fix | Delete
$message = 'Invalid Twig template string: ' . $e->getRawMessage() . "\n" . $template;
[87] Fix | Delete
$this->get_wp_api()->error_log( $message );
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
return $output;
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
protected function maybe_init_twig() {
[94] Fix | Delete
if ( ! $this->twig ) {
[95] Fix | Delete
$loader = $this->get_twig_loader();
[96] Fix | Delete
[97] Fix | Delete
$environment_args = array();
[98] Fix | Delete
[99] Fix | Delete
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
[100] Fix | Delete
$environment_args['debug'] = true;
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
if ( $this->is_caching_enabled() ) {
[104] Fix | Delete
$wpml_cache_directory = new WPML_Cache_Directory( $this->get_wp_api() );
[105] Fix | Delete
$this->cache_directory = $wpml_cache_directory->get( 'twig' );
[106] Fix | Delete
[107] Fix | Delete
if ( $this->cache_directory ) {
[108] Fix | Delete
$environment_args['cache'] = $this->cache_directory;
[109] Fix | Delete
$environment_args['auto_reload'] = true;
[110] Fix | Delete
} else {
[111] Fix | Delete
$this->disable_twig_cache();
[112] Fix | Delete
}
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
$this->twig = $this->get_wp_api()->get_twig_environment( $loader, $environment_args );
[116] Fix | Delete
if ( $this->custom_functions && count( $this->custom_functions ) > 0 ) {
[117] Fix | Delete
foreach ( $this->custom_functions as $custom_function ) {
[118] Fix | Delete
$this->twig->addFunction( $custom_function );
[119] Fix | Delete
}
[120] Fix | Delete
}
[121] Fix | Delete
if ( $this->custom_filters && count( $this->custom_filters ) > 0 ) {
[122] Fix | Delete
foreach ( $this->custom_filters as $custom_filter ) {
[123] Fix | Delete
$this->twig->addFilter( $custom_filter );
[124] Fix | Delete
}
[125] Fix | Delete
}
[126] Fix | Delete
}
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
abstract public function get_template();
[130] Fix | Delete
[131] Fix | Delete
abstract public function get_model();
[132] Fix | Delete
[133] Fix | Delete
/**
[134] Fix | Delete
* @return Twig_Environment
[135] Fix | Delete
*/
[136] Fix | Delete
protected function get_twig() {
[137] Fix | Delete
return $this->twig;
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
/**
[141] Fix | Delete
* @param RuntimeException $e
[142] Fix | Delete
*/
[143] Fix | Delete
protected function add_exception_notice( RuntimeException $e ) {
[144] Fix | Delete
if ( false !== strpos( $e->getMessage(), 'create' ) ) {
[145] Fix | Delete
/* translators: %s: Cache directory path */
[146] Fix | Delete
$text = sprintf( __( 'WPML could not create a cache directory in %s', 'sitepress' ), $this->cache_directory );
[147] Fix | Delete
} else {
[148] Fix | Delete
/* translators: %s: Cache directory path */
[149] Fix | Delete
$text = sprintf( __( 'WPML could not write in the cache directory: %s', 'sitepress' ), $this->cache_directory );
[150] Fix | Delete
}
[151] Fix | Delete
$notice = new WPML_Notice( 'exception', $text, self::NOTICE_GROUP );
[152] Fix | Delete
$notice->set_dismissible( true );
[153] Fix | Delete
$notice->set_css_class_types( 'notice-error' );
[154] Fix | Delete
$admin_notices = $this->get_wp_api()->get_admin_notices();
[155] Fix | Delete
$admin_notices->add_notice( $notice );
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
/**
[159] Fix | Delete
* @return WPML_WP_API
[160] Fix | Delete
*/
[161] Fix | Delete
protected function get_wp_api() {
[162] Fix | Delete
if ( ! $this->wp_api ) {
[163] Fix | Delete
$this->wp_api = new WPML_WP_API();
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
return $this->wp_api;
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
protected function disable_twig_cache() {
[170] Fix | Delete
update_option( self::OTGS_TWIG_CACHE_DISABLED_KEY, true, 'no' );
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
protected function is_caching_enabled() {
[174] Fix | Delete
return ! (bool) get_option( self::OTGS_TWIG_CACHE_DISABLED_KEY, false );
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
/**
[178] Fix | Delete
* @return bool
[179] Fix | Delete
*/
[180] Fix | Delete
protected function is_string_template() {
[181] Fix | Delete
return isset( $this->template_string );
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
/**
[185] Fix | Delete
* @return Twig_LoaderInterface
[186] Fix | Delete
*/
[187] Fix | Delete
protected function get_twig_loader() {
[188] Fix | Delete
if ( $this->is_string_template() ) {
[189] Fix | Delete
$loader = $this->get_wp_api()->get_twig_loader_string();
[190] Fix | Delete
} else {
[191] Fix | Delete
$loader = $this->get_wp_api()->get_twig_loader_filesystem( $this->template_paths );
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
return $loader;
[195] Fix | Delete
}
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function