Create New Item
Item Type
File
Folder
Item Name
Search file in folder and subfolders...
Are you sure want to rename?
File Manager
/
wp-content
/
plugins
/
prisna-wp-translate-automate
/
classes
:
admin.class.php
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
<?php /** * Prisna WP Translate Automate * http://www.prisna.net/ * * LICENSE * * @package Prisna WP Translate * @copyright Copyright (c) 2016, Prisna Ltd, www.prisna.net * @license http://www.prisna.net/license/ * @version 1.1 * @date 2017-04-14 */ class PrisnaWPTranslateAutomateAdmin { public static function initialize() { if (!is_admin()) return; add_action('admin_init', array('PrisnaWPTranslateAutomateAdmin', '_initialize')); add_action('admin_head', array('PrisnaWPTranslateAutomateAdmin', '_remove_messages')); add_action('plugins_loaded', array('PrisnaWPTranslateAutomateAdmin', 'initializeMenus')); add_filter('plugin_action_links', array('PrisnaWPTranslateAutomateAdmin', 'addSettingsLink'), 10, 2); add_action('wp_ajax_nopriv_prisna_wp_translate_validate_automated', array('PrisnaWPTranslateAutomateAdminTranslations', '_validate')); add_action('wp_ajax_prisna_wp_translate_validate_automated', array('PrisnaWPTranslateAutomateAdminTranslations', '_validate')); add_action('wp_ajax_nopriv_prisna_wp_translate_save_automated_translations', array('PrisnaWPTranslateAutomateAdminTranslations', '_save_automated_translations')); add_action('wp_ajax_prisna_wp_translate_save_automated_translations', array('PrisnaWPTranslateAutomateAdminTranslations', '_save_automated_translations')); add_action('wp_ajax_nopriv_prisna_wp_translate_run_automated', array('PrisnaWPTranslateAutomateAdminTranslations', '_run')); add_action('wp_ajax_prisna_wp_translate_run_automated', array('PrisnaWPTranslateAutomateAdminTranslations', '_run')); } public static function _initialize() { self::_watch_events(); self::_select_language(); self::_load_styles(); self::_load_scripts(); } public static function addSettingsLink($_links, $_file) { if (PrisnaWPTranslateCommon::endsWith($_file, 'prisna-wp-translate-automate.php')) { $link = '<a href="plugins.php?page=' . PrisnaWPTranslateAutomateConfig::getAdminHandle() . '">' . PrisnaWPTranslateAutomateMessage::get('settings_link') . '</a>'; $_links[] = $link; } return $_links; } protected static function _watch_events() { @header('X-XSS-Protection: 0'); if (PrisnaWPTranslateAutomateAdminEvents::isGeneratingPoFile()) self::_po(); } protected static function _po() { header('Content-Type: text/html; charset=utf-8'); PrisnaWPTranslateAutomateMessage::genPo(); die(); } protected static function _load_scripts() { if (PrisnaWPTranslateAutomateAdminEvents::isLoadingAdminPage()) { wp_enqueue_script('prisna-wp-translate-automate-admin-common', PRISNA_WP_TRANSLATE_JS .'/common.class.js', 'jquery-ui-core', PrisnaWPTranslateConfig::getVersion(), true); wp_enqueue_script('prisna-wp-translate-automate-admin', PRISNA_WP_TRANSLATE_AUTOMATE_JS .'/admin.class.js', 'jquery-ui-core', PrisnaWPTranslateAutomateConfig::getVersion()); wp_enqueue_script('jquery'); wp_enqueue_script('jquery-ui-core'); wp_enqueue_script('thickbox'); } } protected static function _load_styles() { if (PrisnaWPTranslateAutomateAdminEvents::isLoadingAdminPage() || strpos(PrisnaWPTranslateCommon::getAdminWidgetsUrl(), $_SERVER['REQUEST_URI']) !== false) { wp_enqueue_style('prisna-wp-translate-admin', PRISNA_WP_TRANSLATE_CSS .'/admin.css', false, PrisnaWPTranslateConfig::getVersion(), 'screen'); wp_enqueue_style('prisna-wp-translate-automate-admin', PRISNA_WP_TRANSLATE_AUTOMATE_CSS .'/admin.css', false, PrisnaWPTranslateAutomateConfig::getVersion(), 'screen'); wp_enqueue_style('thickbox'); } } public static function _remove_messages() { if (PrisnaWPTranslateAutomateAdminEvents::isLoadingAdminPage() || strpos(PrisnaWPTranslateCommon::getAdminWidgetsUrl(), $_SERVER['REQUEST_URI']) !== false) PrisnaWPTranslateCommon::renderCSS('.update-nag,div.updated,div.error{display:none}'); } public static function initializeMenus() { add_action('admin_menu', array('PrisnaWPTranslateAutomateAdmin', '_add_options_page')); } public static function _add_options_page() { if (current_user_can('activate_plugins')) add_submenu_page('plugins.php', PrisnaWPTranslateAutomateConfig::getName(false, true), PrisnaWPTranslateAutomateConfig::getName(false, true), 'manage_options', PrisnaWPTranslateAutomateConfig::getAdminHandle(), array('PrisnaWPTranslateAutomateAdmin', '_render_main_form')); } public static function _render_main_form() { $form = new PrisnaWPTranslateAutomateAdminForm(); echo $form->render(array( 'type' => 'file', 'base' => PRISNA_WP_TRANSLATE_AUTOMATE_TEMPLATES, 'content' => '/admin/main_form.tpl' )); } protected static function _select_language() { load_plugin_textdomain(PrisnaWPTranslateAutomateConfig::getAdminLanguageDomain(), false, dirname(plugin_basename(__FILE__)) . '/../languages'); } } class PrisnaWPTranslateTranslateAutomateConnector { protected $_text; protected $_translation; protected $_from; protected $_to; public function __construct($_array) { $this->_set_properties($_array); } public function getProperty($_name) { return property_exists($this, '_' . $_name) ? $this->{'_' . $_name} : false; } protected function _set_properties($_array) { foreach($_array as $property => $value) $this->{'_' . $property} = $value; if (!is_array($this->_text)) $this->_text = array($this->_text); if (!is_array($this->_translation)) $this->_translation = array($this->_translation); for ($i=0; $i<count($this->_text); $i++) $this->_text[$i] = PrisnaWPTranslateCommon::removeEtx($this->_text[$i]); for ($i=0; $i<count($this->_translation); $i++) $this->_translation[$i] = PrisnaWPTranslateCommon::removeEtx($this->_translation[$i]); } } class PrisnaWPTranslateAutomateAdminTranslations { public static function _save_automated_translations() { $text = PrisnaWPTranslateCommon::getVariable('prisna_tx', 'POST'); $translation = PrisnaWPTranslateCommon::getVariable('prisna_tr', 'POST'); $original_url = PrisnaWPTranslateCommon::getVariable('prisna_rs', 'POST'); $from = PrisnaWPTranslateCommon::getVariable('prisna_sl', 'POST'); $to = PrisnaWPTranslateCommon::getVariable('prisna_tl', 'POST'); $connector = new PrisnaWPTranslateTranslateAutomateConnector(array( 'text' => $text, 'translation' => $translation, 'from' => $from, 'to' => $to )); $url = PrisnaWPTranslateSeo::removeLanguage(PrisnaWPTranslateCommon::getPathUrl($original_url)); $translate = new PrisnaWPTranslateTranslateTransport($connector); $translate->setProperty('url', $url); $translate->setProperty('url_hash', empty($url) ? false : PrisnaWPTranslateCommon::hashUrl($url)); $translate->save(); echo json_encode(array( 'url' => $original_url )); exit; } protected static function _url_is_alive($_url) { $file_headers = @get_headers($_url); return $file_headers[0] != 'HTTP/1.1 404 Not Found'; } protected static function _is_url($_url) { $url = $_url; if (PrisnaWPTranslateCommon::startsWith($url, '/')) $url = PrisnaWPTranslateCommon::getCurrentUrlBase() . $url; if (!PrisnaWPTranslateCommon::startsWith($url, 'http')) $url = 'http://' . $url; return filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED) !== false; } protected static function _validate_source_value($_name, $_source) { $value = PrisnaWPTranslateCommon::getVariable($_name, 'POST'); if (!$_source['success']) return array( 'success' => false, 'value' => $value ); if ($value !== false) $value = trim($value); if (empty($value)) return array( 'success' => false, 'value' => 'Empty resource value' ); switch ($_source['value']) { case PrisnaWPTranslateAutomateMessage::get('automate_source_sitemap'): if (!self::_is_url($value)) return array( 'success' => false, 'value' => sprintf('%s (invalid)', $value) ); break; case PrisnaWPTranslateAutomateMessage::get('automate_source_url'): $urls = preg_split("/(\n\r|\r\n|\n|\r){1,}/", $value); $flag = false; foreach ($urls as $url) if (self::_is_url(trim($url))) $flag = true; if (!$flag) return array( 'success' => false, 'value' => sprintf('%s (invalid)', $value) ); break; case PrisnaWPTranslateAutomateMessage::get('automate_source_category'): $term = term_exists($value, 'category'); if (empty($term)) return array( 'success' => false, 'value' => sprintf('%s (doesn\'t exist)', $value) ); break; } return array( 'success' => true, 'value' => $value ); } protected static function _validate_action($_name) { $value = PrisnaWPTranslateCommon::getVariable($_name, 'POST'); if ($value !== false) $value = trim($value); if (empty($value)) return array( 'success' => false, 'value' => 'Empty action' ); $mx = array( 'retranslate' => PrisnaWPTranslateAutomateMessage::get('automate_retranslate_url_retranslate'), 'skip' => PrisnaWPTranslateAutomateMessage::get('automate_retranslate_url_skip') ); if (!array_key_exists($value, $mx)) return array( 'success' => false, 'value' => sprintf('Invalid action: %s', $value) ); return array( 'success' => true, 'value' => $mx[$value] ); } protected static function _validate_source($_name) { $value = PrisnaWPTranslateCommon::getVariable($_name, 'POST'); if ($value !== false) $value = trim($value); if (empty($value)) return array( 'success' => false, 'value' => 'Empty resource' ); $mx = array( 'sitemap' => PrisnaWPTranslateAutomateMessage::get('automate_source_sitemap'), 'url' => PrisnaWPTranslateAutomateMessage::get('automate_source_url'), 'category' => PrisnaWPTranslateAutomateMessage::get('automate_source_category') ); if (!array_key_exists($value, $mx)) return array( 'success' => false, 'value' => sprintf('%s (invalid)', $value) ); return array( 'success' => true, 'value' => $mx[$value] ); } protected static function _validate_timeout($_name) { $value = (int) PrisnaWPTranslateCommon::getVariable($_name, 'POST'); if (empty($value)) return array( 'success' => false, 'value' => 'Invalid time' ); return array( 'success' => true, 'value' => $value . ' ' . PrisnaWPTranslateAutomateMessage::get('automate_unit_ms') ); } protected static function _validate_language($_name) { $value = PrisnaWPTranslateCommon::getVariable($_name, 'POST'); if (empty($value) || !is_array($value)) return array( 'success' => false, 'value' => 'Empty language' ); $languages = PrisnaWPTranslateCommon::getLanguages(); $from = PrisnaWPTranslateConfig::getSettingValue('from'); $output = array(); foreach ($value as $language) { if (!array_key_exists($language, $languages) || $language == $from) return array( 'success' => false, 'value' => sprintf('%s (invalid)', $language) ); $output[] = $languages[$language]; } return array( 'success' => true, 'value' => implode(', ', $output) ); } public static function _validate() { $automate_to = self::_validate_language('prisna_automate_to'); $automate_source = self::_validate_source('prisna_automate_source'); $automate_source_value = self::_validate_source_value('prisna_automate_source_value', $automate_source); $automate_action = self::_validate_action('prisna_automate_action'); $automate_timeout = self::_validate_timeout('prisna_automate_timeout'); $succes = $automate_to['success'] && $automate_source['success'] && $automate_source_value['success'] && $automate_action['success'] && $automate_timeout['success']; $result = array( 'success' => $succes, 'from' => PrisnaWPTranslateConfig::getSettingValue('from'), 'validate' => array( 'automate_to' => $automate_to, 'automate_source' => $automate_source, 'automate_source_value' => $automate_source_value, 'automate_action' => $automate_action, 'automate_timeout' => $automate_timeout ) ); echo json_encode($result); exit; } protected static function _get_urls_from_list($_urls) { $urls = preg_split("/(\n\r|\r\n|\n|\r){1,}/", $_urls); $result = array(); foreach ($urls as $temp_url) { $url = self::_rel2abs($temp_url); if (self::_is_url(trim($url))) $result[] = $url; } if (count($result) == 0) return array( 'success' => false, 'message' => 'Invalid list' ); return array( 'success' => true, 'value' => $result ); } protected static function _get_urls_from_category($_name) { $items = query_posts( array( 'category_name' => $_name, 'posts_per_page' => -1 ) ); if (empty($items)) return array( 'success' => false, 'message' => 'No resources found' ); $result = array(); foreach ($items as $item) $result[] = get_permalink($item); return array( 'success' => true, 'value' => $result ); } protected static function _get_urls_from_sitemap($_url) { if (!PrisnaWPTranslateCommon::startsWith($_url, 'http')) $_url = 'http://' . $_url; if (($response_xml_data = @file_get_contents($_url)) === false) { return array( 'success' => false, 'message' => sprintf('Error fetching sitemap: %s', $_url) ); } else { libxml_use_internal_errors(true); $data = simplexml_load_string($response_xml_data); if (!$data) { $message = array(sprintf('Error fetching sitemap: %s', $_url)); foreach (libxml_get_errors() as $error) $message[] = $error->message; return array( 'success' => false, 'message' => implode('. ', $message) ); } } if (!property_exists($data, 'url')) return array( 'success' => false, 'message' => sprintf('Invalid sitemap: %s', $_url) ); $result = array(); foreach($data->url as $url) $result[] = self::_rel2abs((string) $url->loc); if (count($result) == 0) return array( 'success' => false, 'message' => sprintf('Empty sitemap: %s', $_url) ); return array( 'success' => true, 'value' => $result ); } protected static function _rel2abs($rel) { /* return if already absolute URL */ if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel; $base = PrisnaWPTranslateCommon::getCurrentUrlBase(); /* queries and anchors */ if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel; /* parse base URL and convert to local variables: $scheme, $host, $path */ extract(parse_url($base)); if (!isset($path)) $path = ''; /* remove non-directory element from path */ $path = preg_replace('#/[^/]*$#', '', $path); /* destroy path if relative url points to root */ if ($rel[0] == '/') $path = ''; /* dirty absolute URL */ $abs = "$host$path/$rel"; /* replace '//' or '/./' or '/foo/../' with '/' */ $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#'); for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {} /* absolute URL is ready! */ return $scheme.'://'.$abs; } protected static function _is_local_url($_url) { $local_host = $_SERVER['HTTP_HOST']; $host = parse_url($_url, PHP_URL_HOST); return $local_host == $host; } protected static function _filter_urls($_urls, $_action, $_languages) { $result = array( 'data' => array() ); $ix = 0; foreach ($_urls as $temp_url) { $url = self::_rel2abs($temp_url); $success = self::_is_local_url($url); $aux = array( 'success' => $success, 'value' => $url, 'ix' => $ix ); if (!$success) $aux['message'] = PrisnaWPTranslateAutomateMessage::get('automate_hostname_match_error', array($_SERVER['HTTP_HOST'], parse_url($url, PHP_URL_HOST))); $result['data'][] = $aux; $ix++; } $result['success'] = true; if ($_action == 'skip') $result['data'] = self::_filter_existent_urls($result['data'], $_languages); return $result; } protected static function _translated_url($_url, $_languages) { $from = PrisnaWPTranslateConfig::getSettingValue('from'); $path = PrisnaWPTranslateCommon::stripHost($_url['value']); $hash = PrisnaWPTranslateCommon::hashUrl($path); foreach ($_languages as $_language) { $base = $from . '_' . $_language . '_'; $file = PRISNA_WP_TRANSLATE_CACHE . '/' . $base . $hash . '.xml'; if (is_file($file)) return true; } return false; } protected static function _filter_existent_urls($_urls, $_languages) { $result = array(); foreach ($_urls as $url) { if (!$url['success']) { $result[] = $url; continue; } if (self::_translated_url($url, $_languages)) { $url['success'] = false; $url['message'] = PrisnaWPTranslateAutomateMessage::get('automate_skipped_url'); } $result[] = $url; } return $result; } protected static function _get_urls($_source, $_value, $_action, $_language, $_timeout) { switch ($_source) { case 'sitemap': $result = self::_get_urls_from_sitemap($_value); break; case 'url': $result = self::_get_urls_from_list($_value); break; case 'category': $result = self::_get_urls_from_category($_value); break; } if (!$result['success']) return $result; $result = self::_filter_urls($result['value'], $_action, $_language); $result['from'] = PrisnaWPTranslateConfig::getSettingValue('from'); $result['to'] = $_language; $result['exclude'] = PrisnaWPTranslateConfig::getSettingValue('exclude_selector'); $result['target'] = PrisnaWPTranslateConfig::getSettingValue('target_selector'); $result['home_url'] = PrisnaWPTranslateCommon::getHomeUrl('/'); $result['meta'] = PrisnaWPTranslateConfig::getSettingValue('translate_meta_tags_names'); $result['timeout'] = (int) $_timeout; return $result; } public static function _run() { $automate_to = PrisnaWPTranslateCommon::getVariable('prisna_automate_to', 'POST'); $automate_source = PrisnaWPTranslateCommon::getVariable('prisna_automate_source', 'POST'); $automate_source_value = PrisnaWPTranslateCommon::getVariable('prisna_automate_source_value', 'POST'); $automate_action = PrisnaWPTranslateCommon::getVariable('prisna_automate_action', 'POST'); $automate_timeout = PrisnaWPTranslateCommon::getVariable('prisna_automate_timeout'); $result = self::_get_urls($automate_source, $automate_source_value, $automate_action, $automate_to, $automate_timeout); echo json_encode($result); exit; } } class PrisnaWPTranslateAutomateAdminBaseForm extends PrisnaWPTranslateItem { public $title_message; protected $_fields; public function __construct() { $this->title_message = PrisnaWPTranslateAutomateMessage::get('plugin_title', PrisnaWPTranslateAutomateConfig::getName()); } public function render($_options, $_html_encode=false) { return parent::render($_options, $_html_encode); } protected function _prepare_settings() {} protected function _set_fields() {} } class PrisnaWPTranslateAutomateAdminForm extends PrisnaWPTranslateAutomateAdminBaseForm { public $group_0; public $nonce; public $tab; public $general_message; public $general_api_key_validate_message; public $ajax_url; public $images_url; public $next_message; public $back_message; public $run_message; public $stop_message; public $stopping_message; public $continue_message; public $queued_message; public $processing_message; public $translating_message; public $saving_message; public $done_message; public $timed_out_message; public $loading_message; public $saving_translations_from_message; public $summary_message; public $language_message; public $resource_message; public $url_behavior_message; public $timeout_message; public function __construct() { parent::__construct(); $this->general_message = PrisnaWPTranslateAutomateMessage::get('general_tab'); $this->general_api_key_validate_message = PrisnaWPTranslateAutomateMessage::get('plugin_api_key_validate'); $this->ajax_url = PrisnaWPTranslateCommon::getAjaxUrl(); $this->nonce = wp_nonce_field(PrisnaWPTranslateAutomateConfig::getAdminHandle(), '_prisna_wpnonce'); $this->summary_message = PrisnaWPTranslateAutomateMessage::get('automate_summary'); $this->next_message = PrisnaWPTranslateAutomateMessage::get('automate_next'); $this->back_message = PrisnaWPTranslateAutomateMessage::get('automate_back'); $this->run_message = PrisnaWPTranslateAutomateMessage::get('automate_run'); $this->stop_message = PrisnaWPTranslateAutomateMessage::get('automate_stop'); $this->stopping_message = PrisnaWPTranslateAutomateMessage::get('automate_stopping'); $this->continue_message = PrisnaWPTranslateAutomateMessage::get('automate_continue'); $this->queued_message = PrisnaWPTranslateAutomateMessage::get('automate_queued'); $this->processing_message = PrisnaWPTranslateAutomateMessage::get('automate_processing'); $this->translating_message = PrisnaWPTranslateAutomateMessage::get('automate_translating'); $this->saving_message = PrisnaWPTranslateAutomateMessage::get('automate_saving'); $this->done_message = PrisnaWPTranslateAutomateMessage::get('automate_done'); $this->timed_out_message = PrisnaWPTranslateAutomateMessage::get('automate_timed_out'); $this->loading_message = PrisnaWPTranslateAutomateMessage::get('automate_loading'); $this->saving_translations_from_message = PrisnaWPTranslateAutomateMessage::get('automate_saving_translations_from'); $this->language_message = PrisnaWPTranslateAutomateMessage::get('automate_language'); $this->resource_message = PrisnaWPTranslateAutomateMessage::get('automate_resource'); $this->url_behavior_message = PrisnaWPTranslateAutomateMessage::get('automate_url_behavior'); $this->timeout_message = PrisnaWPTranslateAutomateMessage::get('automate_timeout'); $this->_set_fields(); } protected function _api_key_validate($_length=56) { $value = PrisnaWPTranslateConfig::getSettingValue('api_key'); if (empty($value)) return true; if (strlen($value) != $_length) return false; if (strpos($value, 'Purchase') !== false) return false; return true; } protected function _cache_file_validate() { if (!$this->_cache_folder_validate()) return true; $files_validate = PrisnaWPTranslateCommon::areFolderFilesWritable(PRISNA_WP_TRANSLATE_CACHE); if ($files_validate !== true) { $this->general_cache_file_validate_message = PrisnaWPTranslateAutomateMessage::get('plugin_cache_file_validate', $files_validate); return false; } return true; } protected function _cache_folder_validate() { return PrisnaWPTranslateCommon::isFolderWritable(PRISNA_WP_TRANSLATE_CACHE); } protected function _api_key_google_empty_validate() { if ($this->_fields['translation_service']->value != 'gt') return true; return !empty($this->_fields['google_api_key']->value); } public function render($_options, $_html_encode=false) { $this->_prepare_settings(); if (!array_key_exists('meta_tag_rules', $_options)) $_options['meta_tag_rules'] = array(); $_options['meta_tag_rules'][] = array( 'expression' => $this->_api_key_validate(), 'tag' => 'api_key_validate' ); $_options['meta_tag_rules'][] = array( 'expression' => $this->_cache_folder_validate(), 'tag' => 'cache_folder_validate' ); $_options['meta_tag_rules'][] = array( 'expression' => $this->_cache_file_validate(), 'tag' => 'cache_file_validate' ); return parent::render($_options, $_html_encode); } protected function _set_fields() { if (is_array($this->_fields)) return; $this->_fields = array(); $settings = PrisnaWPTranslateAutomateConfig::getSettings(true); foreach ($settings as $key => $setting) { if (!array_key_exists('type', $setting)) continue; $field_class = 'PrisnaWPTranslate' . ucfirst($setting['type']) . 'Field'; if ($field_class == 'PrisnaWPTranslateField') continue; $this->_fields[$key] = new $field_class($setting); } } protected function _prepare_settings() { $settings = PrisnaWPTranslateAutomateConfig::getSettings(); $groups = 1; for ($i=0; $i<$groups+1; $i++) { $partial = array(); foreach ($this->_fields as $key => $field) { if ($field->group == $i) { $field->satisfyDependence($this->_fields); $partial[] = $field->output(); } } $group = 'group_' . $i; $this->{$group} = implode("\n", $partial); } } } class PrisnaWPTranslateAutomateAdminEvents { public static function isLoadingAdminPage() { return in_array(PrisnaWPTranslateCommon::getVariable('page', 'GET'), array(PrisnaWPTranslateAutomateConfig::getAdminHandle())); } public static function isGeneratingPoFile() { return self::isLoadingAdminPage() && PrisnaWPTranslateCommon::getVariable('prisna-po', 'GET') !== false; } } class PrisnaWPTranslateAutomateCommon { public static function getTimeoutValues() { $result = array(); for ($i=10; $i<501; $i=$i+10) $result[$i] = $i . ' ' . PrisnaWPTranslateAutomateMessage::get('automate_unit_ms'); return $result; } } PrisnaWPTranslateAutomateAdmin::initialize(); ?>