deleted testfile
This commit is contained in:
commit
e09dbe2c5c
7
constants.php
Normal file
7
constants.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
define ('REGEX_START', "/(_[_a-z])\( *(['\"][\w\d\s%$&;.,!?:\-0-9]*[\"'],? *)(['\"][\w\d\s%$&;.,!?:\-0-9]*[\"'],? *)?, *[\"']");
|
||||
define ('REGEX_END', "[\"'] *\)/");
|
||||
define ('THEME_FOLDER_PATH', 'wp-content/themes');
|
||||
81
sntg-woocommerce-language-fix.php
Normal file
81
sntg-woocommerce-language-fix.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* The plugin bootstrap file
|
||||
*
|
||||
* This file is read by WordPress to generate the plugin information in the plugin
|
||||
* admin area. This file also includes all of the dependencies used by the plugin,
|
||||
* registers the activation and deactivation functions, and defines a function
|
||||
* that starts the plugin.
|
||||
*
|
||||
* @link https://sonith.de
|
||||
*
|
||||
* @wordpress-plugin
|
||||
* Plugin Name: Language Fix for Woocommerce Template Files
|
||||
* Plugin URI: https://sonith.de
|
||||
* Description: Some themes use their own textdomain in their woocommerce template files, resulting in partially missing translations. This plugin fixes the textdomains to woocommerce on every update.
|
||||
* Version: 1.0.0
|
||||
* Author: Sonith
|
||||
* Author URI: https://sonith.de
|
||||
* License: GPL-2.0+
|
||||
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
|
||||
* Text Domain: sntg-wc-template-fix
|
||||
* Domain Path: /languages
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
include ('src/helpers.php');
|
||||
include ('constants.php');
|
||||
include ('src/Template_Fixer.php');
|
||||
include ('src/Template_Finder.php');
|
||||
|
||||
add_action ('init', 'sntg_wclf_init');
|
||||
add_action( 'upgrader_process_complete', 'sntg_wc_template_fixer_update_trigger',10, 2);
|
||||
|
||||
function sntg_wc_template_fixer_update_trigger( $upgrader_object, $options ) {
|
||||
|
||||
//this is a bit of an overhead as on every theme update all themes templates get updated
|
||||
if ($options['action'] == 'update' && $options['type'] == 'theme' ){
|
||||
fix_woocommerce_template_textdomains();
|
||||
}
|
||||
}
|
||||
|
||||
function sntg_wclf_init(){
|
||||
//register shortcode for testing
|
||||
add_shortcode('sntg_wclf_tester', 'fix_woocommerce_template_textdomains');
|
||||
}
|
||||
|
||||
|
||||
register_activation_hook( __FILE__, 'sntg_woocommerce_language_fix_activation' );
|
||||
register_deactivation_hook( __FILE__, 'sntg_woocommerce_language_fix_deactivation' );
|
||||
|
||||
add_action('admin_init', 'sntg_woocommerce_language_fix_activate');
|
||||
|
||||
function sntg_woocommerce_language_fix_activation(){
|
||||
add_option( 'sntg_activated_plugin', 'sntg-woocommerce-language-fix' );
|
||||
}
|
||||
|
||||
function sntg_woocommerce_language_fix_activate(){
|
||||
|
||||
if (is_admin() && get_option ('sntg_activated_plugin') == 'sntg-woocommerce-language-fix'){
|
||||
fix_woocommerce_template_textdomains();
|
||||
delete_option( 'sntg_activated_plugin' );
|
||||
}
|
||||
}
|
||||
|
||||
function sntg_automatic_user_deletion_deactivation(){
|
||||
//i don't think there is actions necessarry...
|
||||
}
|
||||
|
||||
function fix_woocommerce_template_textdomains(){
|
||||
//find all templates
|
||||
$tfinder = new Template_Finder(ABSPATH . THEME_FOLDER_PATH);
|
||||
$templates=$tfinder->get_templates();
|
||||
|
||||
foreach ($templates as $template_path => $template_textdomain){
|
||||
$tfixer = new Template_Fixer($template_path, $template_textdomain);
|
||||
$tfixer->fix_template();
|
||||
}
|
||||
|
||||
}
|
||||
62
src/Template_Finder.php
Normal file
62
src/Template_Finder.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Template Finder searches for all woocommerce templates in theme folder and passes them to Template Fixer
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class Template_Finder{
|
||||
|
||||
private $theme_folder;
|
||||
|
||||
public function __construct($theme_folder){
|
||||
$this->theme_folder = $theme_folder;
|
||||
}
|
||||
|
||||
/** iterates over all themes in search for woocommerce template folder
|
||||
* iterates over woocommerce template folder in search of templates and subdirectories
|
||||
* iterates over subdirectories in search of templates
|
||||
* @return array (template path => template text domain )
|
||||
*/
|
||||
public function get_templates(){
|
||||
//get themes
|
||||
$themes = wp_get_themes();
|
||||
$template_files = array ();
|
||||
|
||||
foreach ($themes as $theme) {
|
||||
$theme_dir = $theme->get_stylesheet_directory();
|
||||
$textdomain = $theme->get('TextDomain');
|
||||
if (is_dir($theme_dir . '/woocommerce')){
|
||||
foreach ($this->extract_files_from_subfolders($theme_dir . '/woocommerce', $textdomain) as $subentry){
|
||||
$template_files = array_merge($template_files, array($subentry=>$textdomain));
|
||||
}
|
||||
}
|
||||
}
|
||||
return $template_files;
|
||||
}
|
||||
|
||||
private function extract_files_from_subfolders($path){
|
||||
$files = array();
|
||||
if ($handle = opendir($path)){
|
||||
}
|
||||
else {
|
||||
echo "could not open directory $path";
|
||||
}
|
||||
|
||||
while (false != ($entry = readdir($handle))){
|
||||
if ($entry === '.' or $entry === '..') continue;
|
||||
$full_path = "$path/$entry";
|
||||
if (is_dir($full_path)){
|
||||
foreach ($this->extract_files_from_subfolders("$full_path") as $subentry){
|
||||
array_push($files, $subentry);
|
||||
}
|
||||
}
|
||||
else {
|
||||
array_push($files, "$full_path");
|
||||
}
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
}
|
||||
68
src/Template_Fixer.php
Normal file
68
src/Template_Fixer.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* class Template Fixer gets one woocommerce template of foreign theme.
|
||||
* Example:
|
||||
* esc_html_e( 'Product', 'themename' ) becomes
|
||||
* esc_html_e( 'Product', 'woocommerce')
|
||||
*/
|
||||
class Template_Fixer{
|
||||
|
||||
private $template_path, //template file to be fixed
|
||||
$textdomain, //text domain of theme (has to be changed to woocommerce)
|
||||
$file_content, //the content of the template
|
||||
$search_string; //regex string, used for replacement
|
||||
|
||||
public function __construct($template_path, $textdomain){
|
||||
$this->template_path = $template_path;
|
||||
$this->textdomain = $textdomain;
|
||||
$this->file_content = file_get_contents($this->template_path);
|
||||
$this->search_string = REGEX_START.$textdomain.REGEX_END;
|
||||
}
|
||||
|
||||
public function fix_template(){
|
||||
$current_offset = 0;
|
||||
$next_match = array();
|
||||
do {
|
||||
preg_match($this->search_string, $this->file_content, $next_match, PREG_OFFSET_CAPTURE, $current_offset);
|
||||
if (empty($next_match)){
|
||||
break;
|
||||
}
|
||||
echo "<br>";
|
||||
echo var_dump ($next_match);
|
||||
//example: echo esc_attr_x( 'Search', 'submit button', 'understrap' );
|
||||
//matches[0] -> full match
|
||||
//matches[1] -> function prefix (_x)
|
||||
//matches[2] -> word to translate (unchanged, 'Search')
|
||||
//matches[3] -> context, if present (here 'submit button')
|
||||
$current_offset = $next_match[0][1]; //beginning of string to exchange
|
||||
$function_suffix = $next_match[1][0]; //eg _e or __ (zB esc_html_e)
|
||||
$word_to_translate = $next_match[2][0];
|
||||
$cutout_length = strlen ($next_match[0][0]);
|
||||
//3 function arguments (matches[3]) remains unchanged
|
||||
if(array_key_exists(3, $next_match)){
|
||||
$context = $next_match[3][0];
|
||||
$insert_string = "$function_suffix($word_to_translate $context , 'woocommerce')";
|
||||
}
|
||||
else{
|
||||
$insert_string = "$function_suffix( $word_to_translate, 'woocommerce')";
|
||||
}
|
||||
echo ("matches[0][0] " .$next_match[0][0]);
|
||||
echo ("part to exchange: " . substr($this->file_content, $current_offset, $cutout_length));
|
||||
|
||||
$pre = substr ($this->file_content, 0, $current_offset);
|
||||
$post = substr ($this->file_content, $current_offset + $cutout_length);
|
||||
$this->file_content = $pre . $insert_string . $post;
|
||||
}while (!empty($next_match));
|
||||
if (!file_put_contents($this->template_path, $this->file_content)){
|
||||
write_log ("trying to write template: " . $this->template_path);
|
||||
}
|
||||
}
|
||||
|
||||
/** only for echoing to frontend for developement */
|
||||
public function get_template(){
|
||||
return esc_html($this->file_content);
|
||||
}
|
||||
}
|
||||
16
src/helpers.php
Normal file
16
src/helpers.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
/**
|
||||
* this file contains helper functions that are useful for any plugin
|
||||
*
|
||||
*/
|
||||
|
||||
if ( ! function_exists('write_log')) {
|
||||
/** function for easily writing data to the log */
|
||||
function write_log ( $log ) {
|
||||
if ( is_array( $log ) || is_object( $log ) ) {
|
||||
error_log( print_r( $log, true ) );
|
||||
} else {
|
||||
error_log( $log );
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user