private function scan_plugin_files($dir_or_file = false, $recursion = 0) { require_once WPML_ST_PATH . '/inc/potx.php'; global $icl_st_p_scan_plugin_id; if ($dir_or_file === false) { $dir_or_file = $this->current_path; } if (wpml_st_file_path_is_valid($dir_or_file)) { if (!$recursion) { $icl_st_p_scan_plugin_id = str_replace(WP_PLUGIN_DIR . '/', '', $dir_or_file); $icl_st_p_scan_plugin_id = str_replace(WPMU_PLUGIN_DIR . '/', '', $icl_st_p_scan_plugin_id); } if (is_file($dir_or_file) && !$recursion) { // case of one-file plugins $this->add_stat(sprintf(__('Scanning file: %s', 'wpml-string-translation'), $dir_or_file)); _potx_process_file($dir_or_file, 0, array($this, 'store_results'), '_potx_save_version', $this->get_default_domain()); $this->add_scanned_file($dir_or_file); } else { $dh = opendir($dir_or_file); while ($dh && false !== ($file = readdir($dh))) { if (0 === strpos($file, '.')) { continue; } if (is_dir($dir_or_file . "/" . $file)) { $recursion++; $this->add_stat(str_repeat("\t", $recursion - 1) . sprintf(__('Opening folder: %s', 'wpml-string-translation'), "/" . $file)); $this->scan_plugin_files($dir_or_file . "/" . $file, $recursion); $recursion--; } elseif (preg_match('#(\\.php|\\.inc)$#i', $file)) { $this->add_stat(str_repeat("\t", $recursion) . sprintf(__('Scanning file: %s', 'wpml-string-translation'), "/" . $file)); $this->add_scanned_file("/" . $file); _potx_process_file($dir_or_file . "/" . $file, 0, array($this, 'store_results'), '_potx_save_version', $this->get_default_domain()); } else { $this->add_stat(str_repeat("\t", $recursion) . sprintf(__('Skipping file: %s', 'wpml-string-translation'), "/" . $file)); } } } } else { $this->add_stat(str_repeat("\t", $recursion) . sprintf(__('Invalid file: %s', 'wpml-string-translation'), "/" . $dir_or_file)); } if (!$recursion) { unset($icl_st_p_scan_plugin_id); } }
function plugin_po_file_download($file = false, $recursion = 0) { global $__wpml_st_po_file_content; if (empty($file) && !empty($_GET['file'])) { $file = WP_PLUGIN_DIR . '/' . $_GET['file']; } if (empty($file) && !wpml_st_file_path_is_valid($file)) { return; } if (is_null($__wpml_st_po_file_content)) { $__wpml_st_po_file_content = ''; } require_once WPML_ST_PATH . '/inc/potx.php'; if (is_file($file) && WP_PLUGIN_DIR == dirname($file)) { _potx_process_file($file, 0, '__pos_scan_store_results', '_potx_save_version', ''); } else { if (!$recursion) { $file = dirname($file); } if (is_dir($file)) { $dh = opendir($file); while ($dh && false !== ($f = readdir($dh))) { if (0 === strpos($f, '.')) { continue; } $this->plugin_po_file_download($file . '/' . $f, $recursion + 1); } } elseif (preg_match('#(\\.php|\\.inc)$#i', $file)) { _potx_process_file($file, 0, '__pos_scan_store_results', '_potx_save_version', ''); } } if (!$recursion) { $po = ""; $po .= '# This file was generated by WPML' . PHP_EOL; $po .= '# WPML is a WordPress plugin that can turn any WordPress site into a full featured multilingual content management system.' . PHP_EOL; $po .= '# https://wpml.org' . PHP_EOL; $po .= 'msgid ""' . PHP_EOL; $po .= 'msgstr ""' . PHP_EOL; $po .= '"Content-Type: text/plain; charset=utf-8\\n"' . PHP_EOL; $po .= '"Content-Transfer-Encoding: 8bit\\n"' . PHP_EOL; $po_title = 'WPML_EXPORT'; if (isset($_GET['context'])) { $po_title .= '_' . $_GET['context']; } $po .= '"Project-Id-Version:' . $po_title . '\\n"' . PHP_EOL; $po .= '"POT-Creation-Date: \\n"' . PHP_EOL; $po .= '"PO-Revision-Date: \\n"' . PHP_EOL; $po .= '"Last-Translator: \\n"' . PHP_EOL; $po .= '"Language-Team: \\n"' . PHP_EOL; $translation_language = 'en'; if (isset($_GET['translation_language'])) { $translation_language = $_GET['translation_language']; } $po .= '"Language:' . $translation_language . '\\n"' . PHP_EOL; $po .= '"MIME-Version: 1.0\\n"' . PHP_EOL; $po .= $__wpml_st_po_file_content; header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header('Content-Transfer-Encoding: binary'); header("Content-Disposition: attachment; filename=\"" . basename($file) . ".po\""); header("Content-Length: " . strlen($po)); echo $po; exit(0); } }
/** * Process a file and put extracted information to the given parameters. * * @param $file_path * Complete path to file to process. * @param $strip_prefix * An integer denoting the number of chars to strip from filepath for output. * @param $save_callback * Callback function to use to save the collected strings. * @param $version_callback * Callback function to use to save collected version numbers. * @param $default_domain * Default domain to be used if one can't be found. */ function _potx_process_file($file_path, $strip_prefix = 0, $save_callback = '_potx_save_string', $version_callback = '_potx_save_version', $default_domain = '') { global $_potx_tokens, $_potx_lookup; // Always grab the CVS version number from the code if (!wpml_st_file_path_is_valid($file_path)) { return; } $code = file_get_contents($file_path); $file_name = $strip_prefix > 0 ? substr($file_path, $strip_prefix) : $file_path; _potx_find_version_number($code, $file_name, $version_callback); // Extract raw PHP language tokens. $raw_tokens = token_get_all($code); unset($code); // Remove whitespace and possible HTML (the later in templates for example), // count line numbers so we can include them in the output. $_potx_tokens = array(); $_potx_lookup = array(); $token_number = 0; $line_number = 1; // Fill array for finding token offsets quickly. $src_tokens = array('__', 'esc_attr__', 'esc_html__', '_e', 'esc_attr_e', 'esc_html_e', '_x', 'esc_attr_x', 'esc_html_x', '_ex', '_n', '_nx'); foreach ($raw_tokens as $token) { if (!is_array($token) || $token[0] != T_WHITESPACE && $token[0] != T_INLINE_HTML) { if (is_array($token)) { $token[] = $line_number; if ($token[0] == T_STRING || $token[0] == T_VARIABLE && in_array($token[1], $src_tokens)) { if (!isset($_potx_lookup[$token[1]])) { $_potx_lookup[$token[1]] = array(); } $_potx_lookup[$token[1]][] = $token_number; } } $_potx_tokens[] = $token; $token_number++; } // Collect line numbers. if (is_array($token)) { $line_number += count(explode("\n", $token[1])) - 1; } else { $line_number += count(explode("\n", $token)) - 1; } } unset($raw_tokens); if (!empty($src_tokens)) { foreach ($src_tokens as $tk) { _potx_find_t_calls_with_context($file_name, $save_callback, $tk, $default_domain); } } }