Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor plugin dirname and basename #796

Merged
merged 3 commits into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 29 additions & 37 deletions admin/qtx_activation_hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ function qtranxf_search_config_files_theme( $theme = null, $found = null ) {
if ( file_exists( $fn ) ) {
$found[] = $fn;
} else {
$fn = WP_PLUGIN_DIR . '/' . qtranxf_plugin_dirname() . '/i18n-config/themes/' . $theme->stylesheet . '/i18n-config.json';
$fn = QTRANSLATE_DIR . '/i18n-config/themes/' . $theme->stylesheet . '/i18n-config.json';
if ( file_exists( $fn ) ) {
$found[] = $fn;
}
Expand All @@ -399,11 +399,10 @@ function qtranxf_search_config_files_theme( $theme = null, $found = null ) {
* @since 3.4
*/
function qtranxf_normalize_config_files( $found ) {
$nc = strlen( WP_CONTENT_DIR );
$plugin_dir = WP_PLUGIN_DIR . '/' . qtranxf_plugin_dirname();
$np = strlen( $plugin_dir );
$nc = strlen( WP_CONTENT_DIR );
$np = strlen( QTRANSLATE_DIR );
foreach ( $found as $k => $fn ) {
if ( substr( $fn, 0, $np ) === $plugin_dir ) {
if ( substr( $fn, 0, $np ) === QTRANSLATE_DIR ) {
$found[ $k ] = '.' . substr( $fn, $np );
} else if ( substr( $fn, 0, $nc ) === WP_CONTENT_DIR ) {
$found[ $k ] = substr( $fn, $nc + 1 );
Expand Down Expand Up @@ -441,24 +440,22 @@ function qtranxf_find_plugin_by_folder( $fld, $plugins ) {
* @since 3.4
*/
function qtranxf_search_config_files() {
$found = qtranxf_search_config_files_theme();
$plugins = wp_get_active_and_valid_plugins();
$plugin_bnm = qtranxf_plugin_dirname();
$plugin_dir = WP_PLUGIN_DIR . '/' . $plugin_bnm;
$found = qtranxf_search_config_files_theme();
$plugins = wp_get_active_and_valid_plugins();
// Caution: plugin files are given here in absolute paths, not relative - wrong WP PHPDoc!
foreach ( $plugins as $plugin ) {
$dir = dirname( $plugin );
$bnm = basename( $dir );
if ( $bnm === $plugin_bnm ) {
if ( $plugin === QTRANSLATE_FILE ) {
continue;
}
$fn = $dir . '/i18n-config.json';
if ( ! file_exists( $fn ) ) {
$fn = $plugin_dir . '/i18n-config/plugins/' . $bnm . '/i18n-config.json';
if ( ! file_exists( $fn ) ) {
$plugin_dir = dirname( $plugin );
$config_file = $plugin_dir . '/i18n-config.json';
if ( ! is_readable( $config_file ) ) {
$config_file = QTRANSLATE_DIR . '/i18n-config/plugins/' . basename( $plugin_dir ) . '/i18n-config.json';
if ( ! is_readable( $config_file ) ) {
continue;
}
}
$found[] = $fn;
$found[] = $config_file;
}

return qtranxf_normalize_config_files( $found );
Expand Down Expand Up @@ -587,24 +584,27 @@ function qtranxf_find_plugin_config_files( &$fn_bnm, &$fn_qtx, $bnm ) {
* Search for i18n-config.json files
* see https://github.com/qtranslate/qtranslate-xt/wiki/Integration-Guide/
*
* @param string $plugin_dir
* @param string $plugin name as relative dir/file.php
*
* @return string|bool
*/
function qtranxf_find_plugin_config_file( $plugin_dir ) {
function qtranxf_find_plugin_config_file( $plugin ) {
$plugin_rel = dirname( $plugin );

$config_path = qtranxf_find_plugin_file( $plugin_dir . '/i18n-config.json' );
$config_path = qtranxf_find_plugin_file( $plugin_rel . '/i18n-config.json' );
if ( $config_path ) {
return $config_path;
}

$config_sub_path = qtranxf_plugin_dirname() . '/i18n-config/plugins/' . $plugin_dir . '/i18n-config.json';
$qtx_rel = basename( QTRANSLATE_DIR );

$config_sub_path = $qtx_rel . '/i18n-config/plugins/' . $plugin_rel . '/i18n-config.json';
$config_path = qtranxf_find_plugin_file( $config_sub_path );
if ( $config_path ) {
return $config_path;
}

$config_sub_path = qtranxf_plugin_dirname() . '/i18n-config/themes/' . $plugin_dir . '/i18n-config.json';
$config_sub_path = $qtx_rel . '/i18n-config/themes/' . $plugin_rel . '/i18n-config.json';
$config_path = qtranxf_find_plugin_file( $config_sub_path );
if ( $config_path ) {
return $config_path;
Expand Down Expand Up @@ -636,13 +636,10 @@ function qtranxf_adjust_config_files( $file_to_add, $file_to_del ) {
}

function qtranxf_on_activate_plugin( $plugin, $network_wide = false ) {
//qtranxf_dbg_log('qtranxf_on_activate_plugin: $plugin: ',$plugin);
$plugin_dir = dirname( $plugin );
$qtx_dir = qtranxf_plugin_dirname();
if ( $plugin_dir == $qtx_dir ) {
if ( $plugin === plugin_basename( QTRANSLATE_FILE ) ) {
return;
}
$file_to_add = qtranxf_find_plugin_config_file( $plugin_dir );
$file_to_add = qtranxf_find_plugin_config_file( $plugin );
if ( $file_to_add ) {
qtranxf_adjust_config_files( $file_to_add, null );
}
Expand All @@ -651,13 +648,10 @@ function qtranxf_on_activate_plugin( $plugin, $network_wide = false ) {
add_action( 'activate_plugin', 'qtranxf_on_activate_plugin' );

function qtranxf_on_deactivate_plugin( $plugin, $network_deactivating = false ) {
//qtranxf_dbg_log('qtranxf_on_deactivate_plugin: $plugin: ',$plugin);
$plugin_dir = dirname( $plugin );
$qtx_dir = qtranxf_plugin_dirname();
if ( $plugin_dir == $qtx_dir ) {
if ( $plugin === plugin_basename( QTRANSLATE_FILE ) ) {
return;
}
$file_to_del = qtranxf_find_plugin_config_file( $plugin_dir );
$file_to_del = qtranxf_find_plugin_config_file( $plugin );
if ( $file_to_del ) {
qtranxf_adjust_config_files( null, $file_to_del );
}
Expand Down Expand Up @@ -685,11 +679,9 @@ function qtranxf_activation_hook() {
//qtranxf_dbg_log('qtranxf_activation_hook: ', __FILE__);
if ( version_compare( PHP_VERSION, '5.4' ) < 0 ) {
// Deactivate ourself
$plugin_dir = qtranxf_plugin_dirname();
$lang_dir = $plugin_dir . '/lang';
load_plugin_textdomain( 'qtranslate', false, $lang_dir );
load_plugin_textdomain( 'qtranslate', false, basename( QTRANSLATE_DIR ) . '/lang' );
$msg = sprintf( __( 'Plugin %s requires PHP version %s at least. This server instance runs PHP version %s. A PHP version %s or higher is recommended. The plugin has not been activated.', 'qtranslate' ), qtranxf_get_plugin_link(), '5.4', PHP_VERSION, '7.3' );
deactivate_plugins( $plugin_dir . '/qtranslate.php' );
deactivate_plugins( plugin_basename( QTRANSLATE_FILE ) );
wp_die( $msg );
}

Expand Down Expand Up @@ -952,7 +944,7 @@ function qtranxf_admin_notice_dismiss_script() {

/** register activation/deactivation hooks */
function qtranxf_register_activation_hooks() {
$qtx_plugin_basename = qtranxf_plugin_basename();
$qtx_plugin_basename = plugin_basename( QTRANSLATE_FILE );
register_activation_hook( $qtx_plugin_basename, 'qtranxf_activation_hook' );
register_deactivation_hook( $qtx_plugin_basename, 'qtranxf_deactivation_hook' );
QTX_Admin_Modules::register_hooks();
Expand Down
4 changes: 2 additions & 2 deletions admin/qtx_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ function qtranxf_admin_footer_update( $text ) {
if ( qtranxf_admin_is_config_page() ) {
$text = sprintf( __( 'Plugin Version %s', 'qtranslate' ), QTX_VERSION );
$current = get_site_transient( 'update_plugins' );
$plugin_file = qtranxf_plugin_basename();
$plugin_file = plugin_basename( QTRANSLATE_FILE );
if ( isset( $current->response[ $plugin_file ] ) ) {
$data = $current->response[ $plugin_file ];
if ( is_plugin_active_for_network( $plugin_file ) ) {
Expand All @@ -880,7 +880,7 @@ function qtranxf_admin_footer_update( $text ) {
function qtranxf_admin_load() {
qtranxf_admin_loadConfig();

$basename = qtranxf_plugin_basename();
$basename = plugin_basename( QTRANSLATE_FILE );
add_filter( 'plugin_action_links_' . $basename, 'qtranxf_links', 10, 4 );
// should be executed after all plugins loaded their *-admin.php
add_action( 'qtranslate_init_language', 'qtranxf_load_admin_page_config', 20 );
Expand Down
2 changes: 1 addition & 1 deletion qtranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

if ( ! defined( 'QTRANSLATE_FILE' ) ) {
define( 'QTRANSLATE_FILE', __FILE__ );
define( 'QTRANSLATE_DIR', dirname( __FILE__ ) );
define( 'QTRANSLATE_DIR', __DIR__ );
}

require_once( QTRANSLATE_DIR . '/inc/qtx_class_translator.php' );
Expand Down
4 changes: 1 addition & 3 deletions qtranslate_core.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,7 @@ function qtranxf_load_option_qtrans_compatibility() {
}

function qtranxf_load_plugin_textdomain() {
$domain = 'qtranslate';
$lang_dir = qtranxf_plugin_dirname() . '/lang';
if ( load_plugin_textdomain( $domain, false, $lang_dir ) ) {
if ( load_plugin_textdomain( 'qtranslate', false, basename( QTRANSLATE_DIR ) . '/lang' ) ) {
return true;
}

Expand Down
15 changes: 4 additions & 11 deletions qtranslate_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,18 @@ function qtranxf_translate_wp( $string ) {
* @since 3.3.8.8
*/
function qtranxf_plugin_basename() {
static $basename;
if ( ! $basename ) {
$basename = plugin_basename( wp_normalize_path( QTRANSLATE_FILE ) );
}
_deprecated_function( __FUNCTION__, '3.7.3', 'plugin_basename( QTRANSLATE_FILE )' );

return $basename;
return plugin_basename( QTRANSLATE_FILE );
}

/**
* @since 3.3.2
*/
function qtranxf_plugin_dirname() {
static $dirname;
if ( ! $dirname ) {
$basename = qtranxf_plugin_basename();
$dirname = dirname( $basename );
}
_deprecated_function( __FUNCTION__, '3.7.3', 'dirname( QTRANSLATE_DIR )' );

return $dirname;
return dirname( QTRANSLATE_DIR );
}

/**
Expand Down