first commit
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
class WPML_ST_Translations_File_Component_Details {
|
||||
/** @var WPML_ST_Translations_File_Components_Find[] */
|
||||
private $finders;
|
||||
|
||||
/** @var WPML_File $file */
|
||||
private $file;
|
||||
|
||||
/** @var string */
|
||||
private $plugin_dir;
|
||||
|
||||
/** @var string */
|
||||
private $theme_dir;
|
||||
|
||||
/** @var string */
|
||||
private $languages_plugin_dir;
|
||||
|
||||
/** @var string */
|
||||
private $languages_theme_dir;
|
||||
|
||||
/** @var array */
|
||||
private $cache = array();
|
||||
|
||||
/**
|
||||
* @param WPML_ST_Translations_File_Components_Find_Plugin $plugin_id_finder
|
||||
* @param WPML_ST_Translations_File_Components_Find_Theme $theme_id_finder
|
||||
* @param WPML_File $wpml_file
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_ST_Translations_File_Components_Find_Plugin $plugin_id_finder,
|
||||
WPML_ST_Translations_File_Components_Find_Theme $theme_id_finder,
|
||||
WPML_File $wpml_file
|
||||
) {
|
||||
$this->finders['plugin'] = $plugin_id_finder;
|
||||
$this->finders['theme'] = $theme_id_finder;
|
||||
$this->file = $wpml_file;
|
||||
|
||||
$this->theme_dir = $this->file->fix_dir_separator( get_theme_root() );
|
||||
$this->plugin_dir = $this->file->fix_dir_separator( realpath( WPML_PLUGINS_DIR ) );
|
||||
|
||||
$wp_content_dir = realpath( WP_CONTENT_DIR );
|
||||
|
||||
$this->languages_plugin_dir = $this->file->fix_dir_separator( $wp_content_dir . '/languages/plugins' );
|
||||
$this->languages_theme_dir = $this->file->fix_dir_separator( $wp_content_dir . '/languages/themes' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file_full_path
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function find_details( $file_full_path ) {
|
||||
$file_full_path = $this->file->fix_dir_separator( $file_full_path );
|
||||
|
||||
if ( ! isset( $this->cache[ $file_full_path ] ) ) {
|
||||
$type = $this->find_type( $file_full_path );
|
||||
if ( 'other' === $type ) {
|
||||
$this->cache[ $file_full_path ] = array( $type, null );
|
||||
} else {
|
||||
$this->cache[ $file_full_path ] = array( $type, $this->find_id( $type, $file_full_path ) );
|
||||
}
|
||||
}
|
||||
|
||||
return $this->cache[ $file_full_path ];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $component_type
|
||||
* @param string $file_full_path
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
public function find_id( $component_type, $file_full_path ) {
|
||||
if ( ! isset( $this->finders[ $component_type ] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->finders[ $component_type ]->find_id( $file_full_path );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file_full_path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function find_type( $file_full_path ) {
|
||||
if ( $this->theme_dir && ( $this->string_contains( $file_full_path, $this->theme_dir ) || $this->string_contains( $file_full_path, $this->languages_theme_dir ) ) ) {
|
||||
return 'theme';
|
||||
}
|
||||
|
||||
if ( $this->string_contains( $file_full_path, $this->plugin_dir ) || $this->string_contains( $file_full_path, $this->languages_plugin_dir ) ) {
|
||||
return 'plugin';
|
||||
}
|
||||
|
||||
return 'other';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file_full_path
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_component_active( $file_full_path ) {
|
||||
list( $type, $id ) = $this->find_details( $file_full_path );
|
||||
|
||||
if ( 'other' === $type ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( ! $id ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( 'plugin' === $type ) {
|
||||
return is_plugin_active( $id );
|
||||
} else {
|
||||
return get_stylesheet_directory() === get_theme_root() . '/' . $id;
|
||||
}
|
||||
}
|
||||
|
||||
private function string_contains( $haystack, $needle ) {
|
||||
return false !== strpos( $haystack, $needle );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
class WPML_ST_Translations_File_Components_Find_Plugin implements WPML_ST_Translations_File_Components_Find {
|
||||
/** @var WPML_Debug_BackTrace */
|
||||
private $debug_backtrace;
|
||||
|
||||
/** @var string */
|
||||
private $plugin_dir;
|
||||
|
||||
/** @var array */
|
||||
private $plugin_ids;
|
||||
|
||||
/**
|
||||
* @param WPML_Debug_BackTrace $debug_backtrace
|
||||
*/
|
||||
public function __construct( WPML_Debug_BackTrace $debug_backtrace ) {
|
||||
$this->debug_backtrace = $debug_backtrace;
|
||||
$this->plugin_dir = realpath( WPML_PLUGINS_DIR );
|
||||
}
|
||||
|
||||
public function find_id( $file ) {
|
||||
$directory = $this->find_plugin_directory( $file );
|
||||
if ( ! $directory ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->get_plugin_id_by_directory( $directory );
|
||||
}
|
||||
|
||||
private function find_plugin_directory( $file ) {
|
||||
if ( false !== strpos( $file, $this->plugin_dir ) ) {
|
||||
return $this->extract_plugin_directory( $file );
|
||||
}
|
||||
|
||||
return $this->find_plugin_directory_in_backtrace();
|
||||
}
|
||||
|
||||
private function find_plugin_directory_in_backtrace() {
|
||||
$file = $this->find_file_in_backtrace();
|
||||
if ( ! $file ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->extract_plugin_directory( $file );
|
||||
}
|
||||
|
||||
private function find_file_in_backtrace() {
|
||||
$stack = $this->debug_backtrace->get_backtrace();
|
||||
|
||||
foreach ( $stack as $call ) {
|
||||
if ( isset( $call['function'] ) && 'load_plugin_textdomain' === $call['function'] ) {
|
||||
return $call['file'];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file_path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function extract_plugin_directory( $file_path ) {
|
||||
$dir = ltrim( str_replace( $this->plugin_dir, '', $file_path ), DIRECTORY_SEPARATOR );
|
||||
$dir = explode( DIRECTORY_SEPARATOR, $dir );
|
||||
|
||||
return trim( $dir[0], DIRECTORY_SEPARATOR );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $directory
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function get_plugin_id_by_directory( $directory ) {
|
||||
foreach ( $this->get_plugin_ids() as $plugin_id ) {
|
||||
if ( 0 === strpos( $plugin_id, $directory . '/' ) ) {
|
||||
return $plugin_id;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function get_plugin_ids() {
|
||||
if ( null === $this->plugin_ids ) {
|
||||
$this->plugin_ids = array_keys( get_plugins() );
|
||||
}
|
||||
|
||||
return $this->plugin_ids;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
class WPML_ST_Translations_File_Components_Find_Theme implements WPML_ST_Translations_File_Components_Find {
|
||||
/** @var WPML_Debug_BackTrace */
|
||||
private $debug_backtrace;
|
||||
|
||||
/** @var WPML_File $file */
|
||||
private $file;
|
||||
|
||||
/** @var string */
|
||||
private $theme_dir;
|
||||
|
||||
/**
|
||||
* @param WPML_Debug_BackTrace $debug_backtrace
|
||||
* @param WPML_File $file
|
||||
*/
|
||||
public function __construct( WPML_Debug_BackTrace $debug_backtrace, WPML_File $file ) {
|
||||
$this->debug_backtrace = $debug_backtrace;
|
||||
$this->file = $file;
|
||||
|
||||
$this->theme_dir = $this->file->fix_dir_separator( get_theme_root() );
|
||||
}
|
||||
|
||||
public function find_id( $file ) {
|
||||
return $this->find_theme_directory( $file );
|
||||
}
|
||||
|
||||
private function find_theme_directory( $file ) {
|
||||
if ( false !== strpos( $file, $this->theme_dir ) ) {
|
||||
return $this->extract_theme_directory( $file );
|
||||
}
|
||||
|
||||
return $this->find_theme_directory_in_backtrace();
|
||||
}
|
||||
|
||||
private function find_theme_directory_in_backtrace() {
|
||||
$file = $this->find_file_in_backtrace();
|
||||
if ( ! $file ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->extract_theme_directory( $file );
|
||||
}
|
||||
|
||||
private function find_file_in_backtrace() {
|
||||
$stack = $this->debug_backtrace->get_backtrace();
|
||||
|
||||
foreach ( $stack as $call ) {
|
||||
if ( isset( $call['function'] ) && 'load_theme_textdomain' === $call['function'] ) {
|
||||
return $call['file'];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file_path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function extract_theme_directory( $file_path ) {
|
||||
$file_path = $this->file->fix_dir_separator( $file_path );
|
||||
$dir = ltrim( str_replace( $this->theme_dir, '', $file_path ), DIRECTORY_SEPARATOR );
|
||||
$dir = explode( DIRECTORY_SEPARATOR, $dir );
|
||||
|
||||
return trim( $dir[0], DIRECTORY_SEPARATOR );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
interface WPML_ST_Translations_File_Components_Find {
|
||||
/**
|
||||
* @param string $file
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function find_id( $file );
|
||||
}
|
||||
Reference in New Issue
Block a user