From e09dbe2c5c245676b5ca30b4fb79fd50cc5187c6 Mon Sep 17 00:00:00 2001 From: Runa Date: Wed, 9 Sep 2020 12:33:30 +0200 Subject: [PATCH] deleted testfile --- constants.php | 7 +++ sntg-woocommerce-language-fix.php | 81 +++++++++++++++++++++++++++++++ src/Template_Finder.php | 62 +++++++++++++++++++++++ src/Template_Fixer.php | 68 ++++++++++++++++++++++++++ src/helpers.php | 16 ++++++ 5 files changed, 234 insertions(+) create mode 100644 constants.php create mode 100644 sntg-woocommerce-language-fix.php create mode 100644 src/Template_Finder.php create mode 100644 src/Template_Fixer.php create mode 100644 src/helpers.php diff --git a/constants.php b/constants.php new file mode 100644 index 0000000..7f63d8e --- /dev/null +++ b/constants.php @@ -0,0 +1,7 @@ +get_templates(); + + foreach ($templates as $template_path => $template_textdomain){ + $tfixer = new Template_Fixer($template_path, $template_textdomain); + $tfixer->fix_template(); + } + +} diff --git a/src/Template_Finder.php b/src/Template_Finder.php new file mode 100644 index 0000000..9647c4a --- /dev/null +++ b/src/Template_Finder.php @@ -0,0 +1,62 @@ +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; + } + + } \ No newline at end of file diff --git a/src/Template_Fixer.php b/src/Template_Fixer.php new file mode 100644 index 0000000..fca9358 --- /dev/null +++ b/src/Template_Fixer.php @@ -0,0 +1,68 @@ +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 "
"; + 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); + } +} \ No newline at end of file diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..cc08077 --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,16 @@ +