diff --git a/includes/admin/transifex-live-integration-admin-template.php b/includes/admin/transifex-live-integration-admin-template.php index 244e18d2..ed30942c 100644 --- a/includes/admin/transifex-live-integration-admin-template.php +++ b/includes/admin/transifex-live-integration-admin-template.php @@ -87,16 +87,37 @@

/>

-

+

+
+

+ /> + + +

+

+ /> + +

+

+ /> + +

+

+ /> + +

+

+ +

+

Check out our docs for details.', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>

- +
- diff --git a/includes/admin/transifex-live-integration-admin.php b/includes/admin/transifex-live-integration-admin.php index 7f005896..59cfce87 100644 --- a/includes/admin/transifex-live-integration-admin.php +++ b/includes/admin/transifex-live-integration-admin.php @@ -84,8 +84,22 @@ static function options_page() { ob_start(); checked( $settings['enable_prerender'], 1 ); $checked_enable_prerender = ob_get_clean(); + + ob_start(); + checked( $settings['enable_prerender_check'], 1 ); + $checked_enable_prerender_check = ob_get_clean(); + + ob_start(); + checked( $settings['prerender_enable_response_header'], 1 ); + $checked_prerender_enable_response_header = ob_get_clean(); + + ob_start(); + checked( $settings['prerender_enable_vary_header'], 1 ); + $checked_prerender_enable_vary_header = ob_get_clean(); - $enable_prerender_check = $settings['enable_prerender_check']; + ob_start(); + checked( $settings['prerender_enable_cookie'], 1 ); + $checked_prerender_enable_cookie = ob_get_clean(); ob_start(); checked( $settings['static_frontpage_support'], 1 ); @@ -116,6 +130,17 @@ static function options_page() { if ( $settings['language_map'] !== '' ) { $language_map = $settings['language_map']; } + + $prerender_response_headers = ''; + if ( $settings['prerender_response_headers'] !== '' ) { + $prerender_response_headers = $settings['prerender_response_headers']; + } + + $prerender_cookie = ''; + if ( $settings['prerender_cookie'] !== '' ) { + $prerender_cookie = $settings['prerender_cookie']; + } + $checked_custom_urls = ($settings['enable_custom_urls'] === "1") ? "1" : "0"; $url_options = $settings['url_options']; @@ -128,6 +153,8 @@ static function options_page() { ob_start(); checked( $settings['url_options'], '3' ); $url_options_subdirectory = ob_get_clean(); + + $site_url = site_url(); $site_url_subdirectory_example = $site_url . '/%lang%'; $site_url_array = explode( '/', $site_url ); @@ -267,7 +294,6 @@ static public function sanitize_settings( $settings ) { $settings['transifex_live_settings']['subdomain_pattern'] = ( isset( $settings['transifex_live_settings']['subdomain_pattern'] )) ? sanitize_text_field( $settings['transifex_live_settings']['subdomain_pattern'] ) : ''; $settings['transifex_live_settings']['languages_regex'] = ( isset( $settings['transifex_live_settings']['languages_regex'] )) ? sanitize_text_field( $settings['transifex_live_settings']['languages_regex'] ) : ''; $settings['transifex_live_settings']['transifex_languages'] = ( isset( $settings['transifex_live_settings']['transifex_languages'] )) ? sanitize_text_field( stripslashes( $settings['transifex_live_settings']['transifex_languages'] ) ) : ''; - $settings['transifex_live_settings']['language_map'] = ( isset( $settings['transifex_live_settings']['language_map'] )) ? sanitize_text_field( $settings['transifex_live_settings']['language_map'] ) : ''; $settings['transifex_live_transifex_settings']['settings'] = ( isset( $settings['transifex_live_transifex_settings']['settings'] )) ? sanitize_text_field( stripslashes( $settings['transifex_live_transifex_settings']['settings'] ) ) : ''; diff --git a/includes/lib/transifex-live-integration-hreflang.php b/includes/lib/transifex-live-integration-hreflang.php index 2bb414a7..df3434f9 100644 --- a/includes/lib/transifex-live-integration-hreflang.php +++ b/includes/lib/transifex-live-integration-hreflang.php @@ -33,17 +33,52 @@ class Transifex_Live_Integration_Hreflang { * @var tokenized_url string */ private $tokenized_url; + + private $rewrite_options; /** * Public constructor, sets the settings * @param array $settings Associative array used to store plugin settings. */ - public function __construct( $settings ) { + public function __construct( $settings, $rewrite_options ) { Plugin_Debug::logTrace(); $this->settings = $settings; $this->language_map = json_decode( $settings['language_map'], true )[0]; $this->languages = json_decode( $settings['transifex_languages'], true ); $this->tokenized_url = $settings['tokenized_url']; + $this->rewrite_options = $rewrite_options; + } + + public function check_rewrite_options() { + Plugin_Debug::logTrace(); + if ( isset( $this->rewrite_options['add_rewrites_post'] ) && is_single ()) { + return true; + } + if ( isset( $this->rewrite_options['add_rewrites_root'] ) && is_home()) { + return true; + } + if ( isset( $this->rewrite_options['add_rewrites_date'] ) && is_archive()) { + return true; + } + if ( isset( $this->rewrite_options['add_rewrites_page'] ) && is_page ()) { + return true; + } + if ( isset( $this->rewrite_options['add_rewrites_author'] ) && is_author()) { + return true; + } + if ( isset( $this->rewrite_options['add_rewrites_tag'] ) && is_tag()) { + return true; + } + if ( isset( $this->rewrite_options['add_rewrites_category'] ) && is_category()) { + return true; + } + if ( isset( $this->rewrite_options['add_rewrites_search'] ) && is_search()) { + return true; + } + if ( isset( $this->rewrite_options['add_rewrites_feed'] ) && is_feed()) { + return true; + } + return false; } /* @@ -74,6 +109,9 @@ private function generate_languages_hreflang( $raw_url, $languages, */ public function render_hreflang() { Plugin_Debug::logTrace(); + if ( !($this->check_rewrite_options())) { + return false; + } global $wp; $lang = get_query_var( 'lang' ); $url_path = add_query_arg( array(), $wp->request ); diff --git a/includes/lib/transifex-live-integration-prerender.php b/includes/lib/transifex-live-integration-prerender.php index 5d027e0d..bf9989ad 100644 --- a/includes/lib/transifex-live-integration-prerender.php +++ b/includes/lib/transifex-live-integration-prerender.php @@ -21,6 +21,16 @@ class Transifex_Live_Integration_Prerender { * @var bool */ private $enable_prerender_check; + private $prerender_enable_vary_header; + private $prerender_vary_header_value; + private $prerender_header_check_key; + private $prerender_header_check_value; + private $prerender_enable_cookie; + private $prerender_cookie; + private $prerender_enable_response_header; + private $prerender_response_headers; + private $generic_bot_types; + private $whitelist_crawlers; /* * Constructor @@ -28,10 +38,28 @@ class Transifex_Live_Integration_Prerender { * @param bool $override_prerender_check Overrides check for prerender header */ - public function __construct( $prerender_url, $enable_prerender_check ) { + public function __construct( $prerender_url, $enable_prerender_check, $settings ) { Plugin_Debug::logTrace(); $this->prerender_url = $prerender_url; $this->enable_prerender_check = ($enable_prerender_check) ? true : false; + $this->prerender_enable_vary_header = (isset( $settings['prerender_enable_vary_header'] )) ? true : false; + $this->prerender_vary_header_value = $settings['prerender_vary_header_value']; + $this->prerender_header_check_key = $settings['prerender_header_check_key']; + $this->prerender_header_check_value = $settings['prerender_header_check_value']; + $this->prerender_enable_response_header = (isset( $settings['prerender_enable_response_header'] )) ? true : false; + $this->generic_bot_types = $settings['generic_bot_types']; + $this->whitelist_crawlers = $settings['whitelist_crawlers']; + + $this->prerender_response_headers = [ ]; + if ( isset( $settings['prerender_response_headers'] ) ) { + $this->prerender_response_headers = json_decode( stripslashes($settings['prerender_response_headers']), true ); + } + + $this->prerender_enable_cookie = (isset( $settings['prerender_enable_cookie'] )) ? true : false; + $this->prerender_cookie = [ ]; + if ( isset( $settings['prerender_cookie'] ) ) { + $this->prerender_cookie = json_decode( stripslashes($settings['prerender_cookie']), true ); + } } /* @@ -49,34 +77,77 @@ function wp_head_hook() { echo $status; } + function wp_headers_response_hook( $headers ) { + Plugin_Debug::logTrace(); + $a = $this->prerender_response_headers; + foreach ($a as $k => $v) { + $headers[$k] = $v; + } + return $headers; + } + + function wp_headers_vary_hook( $headers ) { + Plugin_Debug::logTrace(); + $headers['Vary'] = $this->prerender_vary_header_value; + return $headers; + } + /* - * WP wp_headers filter, adds a prerender header + * WP wp_headers filter, adds headers for prerender request */ - function wp_headers_hook( $headers ) { + function wp_headers_prerender_hook( $headers ) { Plugin_Debug::logTrace(); - $headers['X-Prerender-Req'] = 'TRUE'; + $headers[$this->prerender_header_check_key] = $this->prerender_header_check_value; return $headers; } + function init_hook() { + $a = $this->prerender_cookie; + foreach ($a as $k => $v) { + setcookie( $k, $v, DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); + } + } + + function ok_call_prerender() { + include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/transifex-live-integration-util.php'; + $agent = Transifex_Live_Integration_Util::get_user_agent(); + $req_escaped_fragment = (isset( $_GET['_escaped_fragment_'] )) ? true : false; + include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/lib/transifex-live-integration-prerender.php'; + $check = Transifex_Live_Integration_Util::prerender_check( $agent, $req_escaped_fragment, $this->generic_bot_types, $this->whitelist_crawlers ); + return $check; + } + + function ok_add_vary_header() { + return ($this->prerender_enable_vary_header); + } - function call_curl($url) { - $arr = []; + function ok_add_response_header() { + return ($this->prerender_enable_response_header); + } + + function ok_add_cookie() { + return ($this->prerender_enable_cookie); + } + + function call_curl( $url ) { + $arr = [ ]; $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_VERBOSE, 1 ); curl_setopt( $ch, CURLOPT_HEADER, 1 ); - curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT ,10); - curl_setopt( $ch, CURLOPT_TIMEOUT, 20); + curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 ); + curl_setopt( $ch, CURLOPT_TIMEOUT, 20 ); $arr['url'] = $url; $arr['response'] = curl_exec( $ch ); - $arr['statuscode'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $arr['statuscode'] = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); $arr['header_size'] = curl_getinfo( $ch, CURLINFO_HEADER_SIZE ); $arr['error'] = curl_error( $ch ); curl_close( $ch ); return $arr; } + /* * This aptly named filter function is used to make the prerender call, * ideally it should be executed after the template render is finished but before sending to the browser @@ -87,24 +158,24 @@ function call_curl($url) { function callback( $buffer ) { global $wp; $output = $buffer; - $debug_html = '