@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract class for a file.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Theme.File
|
||||
*/
|
||||
abstract class Ai1ec_File_Abstract extends Ai1ec_Base {
|
||||
|
||||
/**
|
||||
* @var array The paths where to look for the file.
|
||||
*/
|
||||
protected $_paths;
|
||||
|
||||
/**
|
||||
* @var string The name of the file.
|
||||
*/
|
||||
protected $_name;
|
||||
|
||||
/**
|
||||
* @var mixed The content of the file.
|
||||
* Usually it's a string but for some edge cases it might be a PHP type like an array
|
||||
* The only case now is user_variables.php for Less
|
||||
*/
|
||||
protected $_content;
|
||||
|
||||
/**
|
||||
* Locates the file and parses its content. Populates $this->_content.
|
||||
*
|
||||
* @return boolean Returns true if the file is found, false otheriwse.
|
||||
*/
|
||||
abstract public function process_file();
|
||||
|
||||
/**
|
||||
* Standard constructor for basic files.
|
||||
*
|
||||
* @param Ai1ec_Registry_Object $registry
|
||||
* @param string $name
|
||||
* @param array $paths
|
||||
*/
|
||||
public function __construct(
|
||||
Ai1ec_Registry_Object $registry,
|
||||
$name,
|
||||
array $paths
|
||||
) {
|
||||
parent::__construct( $registry );
|
||||
$this->_paths = $paths;
|
||||
$this->_name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the content of the file to the screen.
|
||||
*/
|
||||
public function render() {
|
||||
echo $this->_content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param bool $mute_output used for compatibility reason with old code.
|
||||
*
|
||||
* @return mixed the parsed content of the file.
|
||||
*/
|
||||
public function get_content( $mute_output = false ) {
|
||||
if ( true === $mute_output ) {
|
||||
return '';
|
||||
}
|
||||
return $this->_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Just in case you want to echo the object.
|
||||
*/
|
||||
public function __toString() {
|
||||
return $this->_content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
/**
|
||||
* Exception thrown when a file/extension is not found.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Theme.File
|
||||
*/
|
||||
class Ai1ec_File_Exception extends Ai1ec_Exception {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Handle finding and parsing an image file.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
* @instantiator new
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Theme.File
|
||||
*/
|
||||
class Ai1ec_File_Image extends Ai1ec_File_Abstract {
|
||||
|
||||
/**
|
||||
* @var string The url of the image file.
|
||||
*/
|
||||
protected $_url;
|
||||
|
||||
/**
|
||||
* Get the URL to the image file.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_url() {
|
||||
return $this->_url;
|
||||
}
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Ai1ec_File_Abstract::process_file()
|
||||
*/
|
||||
public function process_file() {
|
||||
$files_to_check = array();
|
||||
foreach ( array_keys( $this->_paths ) as $path ) {
|
||||
$files_to_check[$path] =
|
||||
$path . 'img' . DIRECTORY_SEPARATOR . $this->_name;
|
||||
}
|
||||
foreach ( $files_to_check as $path => $file ) {
|
||||
if ( file_exists( $file ) ) {
|
||||
// Construct URL based on base URL available in $this->_paths array.
|
||||
$this->_url = $this->_paths[$path] . '/img/' . $this->_name;
|
||||
$this->_content = $file;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Handle finding CSS/LESS files.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
* @instantiator new
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Theme.File
|
||||
*/
|
||||
class Ai1ec_File_Less extends Ai1ec_File_Abstract {
|
||||
|
||||
/**
|
||||
* @var string The default CSS folder.
|
||||
*/
|
||||
const THEME_CSS_FOLDER = 'css';
|
||||
|
||||
/**
|
||||
* @var string The default less folder.
|
||||
*/
|
||||
const THEME_LESS_FOLDER = 'less';
|
||||
|
||||
/**
|
||||
* Returns the name of the file.
|
||||
* @return string
|
||||
*/
|
||||
public function get_name() {
|
||||
return $this->_name;
|
||||
}
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Ai1ec_File_Abstract::process_file()
|
||||
*/
|
||||
public function process_file() {
|
||||
// 1. Look for a CSS file in the directory of the current theme.
|
||||
// 2. Look for a LESS version in the directory of the current theme.
|
||||
// 3. Look for a LESS file into the default theme folder.
|
||||
$name = $this->_name;
|
||||
$css_file = $name . '.css';
|
||||
$less_file = $name . '.less';
|
||||
$files_to_check = array();
|
||||
foreach ( $this->_paths as $path ) {
|
||||
$files_to_check[] =
|
||||
$path . self::THEME_LESS_FOLDER . DIRECTORY_SEPARATOR . $less_file;
|
||||
$files_to_check[] =
|
||||
$path . self::THEME_CSS_FOLDER . DIRECTORY_SEPARATOR . $css_file;
|
||||
if ( '..' . DIRECTORY_SEPARATOR . 'style' === $name ) {
|
||||
$files_to_check[] =
|
||||
$path . self::THEME_LESS_FOLDER . DIRECTORY_SEPARATOR . $css_file;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $files_to_check as $file_to_check ) {
|
||||
if ( file_exists( $file_to_check ) ) {
|
||||
$this->_content = file_get_contents( $file_to_check );
|
||||
$this->_name = $file_to_check;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* Handle finding and parsing a PHP file.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
* @instantiator new
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Theme.File
|
||||
*/
|
||||
class Ai1ec_File_Php extends Ai1ec_File_Abstract {
|
||||
|
||||
/**
|
||||
* @var string filename with the variables
|
||||
*/
|
||||
const USER_VARIABLES_FILE = 'user_variables';
|
||||
|
||||
/**
|
||||
* @var array the arguments used by the PHP template.
|
||||
*/
|
||||
private $_args;
|
||||
|
||||
/**
|
||||
* Initialize class specific variables.
|
||||
*
|
||||
* @parma Ai1ec_Registry_Object $registry
|
||||
* @param string $name
|
||||
* @param array $paths
|
||||
* @param array $args
|
||||
*/
|
||||
public function __construct(
|
||||
Ai1ec_Registry_Object $registry,
|
||||
$name,
|
||||
array $paths,
|
||||
array $args
|
||||
) {
|
||||
parent::__construct( $registry, $name, $paths );
|
||||
$this->_args = $args;
|
||||
}
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Ai1ec_File_Abstract::locate_file()
|
||||
*/
|
||||
public function process_file() {
|
||||
// if the file was already processed just return.
|
||||
if ( isset( $this->_content ) ) {
|
||||
return true;
|
||||
}
|
||||
$files_to_check = array();
|
||||
foreach ( array_values( $this->_paths ) as $path ) {
|
||||
$files_to_check[] = $path . $this->_name;
|
||||
}
|
||||
foreach ( $files_to_check as $file ) {
|
||||
if ( is_file( $file ) ) {
|
||||
// Check if file is custom LESS variable definitions.
|
||||
$user_variables_pattern = Ai1ec_File_Less::THEME_LESS_FOLDER .
|
||||
'/' . self::USER_VARIABLES_FILE;
|
||||
|
||||
if ( strpos( $this->_name, $user_variables_pattern ) === 0 ) {
|
||||
// It's a user variables file. We must handle the fact that it might
|
||||
// be legacy.
|
||||
if ( true === $this->_args['is_legacy_theme'] ) {
|
||||
$content = ( require $file );
|
||||
if ( isset( $less_user_variables ) ) {
|
||||
$content = $less_user_variables;
|
||||
}
|
||||
$this->_content = $content;
|
||||
} else {
|
||||
$this->_content = require $file;
|
||||
}
|
||||
} else {
|
||||
$this->_registry->get( 'compatibility.ob' )->start();
|
||||
extract( $this->_args );
|
||||
require $file;
|
||||
$this->_content = $this->_registry
|
||||
->get( 'compatibility.ob' )->get_clean();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy function to keep conpatibility with 1.x themes.
|
||||
*
|
||||
* @param string $file
|
||||
*/
|
||||
public function get_theme_img_url( $file ) {
|
||||
return $this->_registry->get( 'theme.loader' )
|
||||
->get_file( $file, array(), false )->get_url();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* Handle finding and parsing a twig template.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.0
|
||||
* @instantiator new
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Theme.File
|
||||
*/
|
||||
class Ai1ec_File_Twig extends Ai1ec_File_Abstract {
|
||||
|
||||
/**
|
||||
* @var Twig_Environment Twig environment for this helper.
|
||||
*/
|
||||
protected $_twig;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $_args;
|
||||
|
||||
/**
|
||||
* @param string $name The name of the template.
|
||||
* @param array $args The arguments needed to render the template.
|
||||
* @param Twig_Environment $twig An instance of the Twig environment
|
||||
*/
|
||||
public function __construct( $name, array $args, $twig ) {
|
||||
$this->_args = $args;
|
||||
$this->_name = $name;
|
||||
$this->_twig = $twig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given search path to the end of the list (low priority).
|
||||
*
|
||||
* @param string $search_path Path to add to end of list
|
||||
*/
|
||||
public function appendPath( $search_path ) {
|
||||
$loader = $this->_twig->getLoader();
|
||||
$loader->addPath( $search_path );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given search path to the front of the list (high priority).
|
||||
*
|
||||
* @param string $search_path Path to add to front of list
|
||||
*/
|
||||
public function prepend_path( $search_path ) {
|
||||
$loader = $this->_twig->getLoader();
|
||||
$loader->prependPath( $search_path );
|
||||
}
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Ai1ec_File::locate_file()
|
||||
*/
|
||||
public function process_file() {
|
||||
$loader = $this->_twig->getLoader();
|
||||
if ( $loader->exists( $this->_name ) ) {
|
||||
|
||||
$this->_content = $this->_twig->render( $this->_name, $this->_args );
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user