Skip to content

Commit

Permalink
Merge branch 'devel' of github.com:transifex/transifex-live-wordpress
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Jackowski committed Apr 20, 2016
2 parents d1a2082 + 10d1145 commit 7fcb2fd
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 63 deletions.
27 changes: 24 additions & 3 deletions includes/admin/transifex-live-integration-admin-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,37 @@
<td>
<p><input type="checkbox" id="transifex_live_settings_enable_prerender" name="transifex_live_settings[enable_prerender]" value="1" <?php echo $checked_enable_prerender ?> /><?php _e('Enable Prerender', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>
</p>
<p class="prerender-enable-options hide-if-js">
<div class="prerender-enable-options hide-if-js">
<label for="transifex_live_settings[prerender_url]"><?php _e( 'Prerender URL', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?></label>
<input name="transifex_live_settings[prerender_url]" type="text" id="transifex_live_settings_prerender_url" value="<?php echo $settings['prerender_url']; ?>" class="regular-text" placeholder="<?php _e( 'Put your prerender url here.', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>">
<div class="prerender-advanced-options hide-if-js">
<p>
<input type="checkbox" id="transifex_live_settings_enable_prerender_check" name="transifex_live_settings[enable_prerender_check]" value="1" <?php echo $checked_enable_prerender_check ?> /><?php _e( 'Enable Prerender Check', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>
<input name="transifex_live_settings[prerender_header_check_key]" type="text" id="transifex_live_settings_prerender_header_check_key" value="<?php echo $settings['prerender_header_check_key']; ?>" class="regular-text" placeholder="<?php _e( 'Prerender Check Key', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>">
<input name="transifex_live_settings[prerender_header_check_value]" type="text" id="transifex_live_settings_prerender_header_check_value" value="<?php echo $settings['prerender_header_check_value']; ?>" class="regular-text" placeholder="<?php _e( 'Prerender Check Header Value', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>">
</p>
<p>
<input type="checkbox" id="transifex_live_settings_prerender_enable_vary_header" name="transifex_live_settings[prerender_enable_vary_header]" value="1" <?php echo $checked_prerender_enable_vary_header ?> /><?php _e( 'Enable Vary Header', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>
<input name="transifex_live_settings[prerender_vary_header_value]" type="text" id="transifex_live_settings_prerender_vary_header_value" value="<?php echo $settings['prerender_vary_header_value']; ?>" class="regular-text" placeholder="<?php _e( 'Value for Vary Header', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>">
</p>
<p>
<input type="checkbox" id="transifex_live_settings_prerender_enable_response_header" name="transifex_live_settings[prerender_enable_response_header]" value="1" <?php echo $checked_prerender_enable_response_header ?> /><?php _e( 'Enable Custom Prerender Headers', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>
<input name="transifex_live_settings[prerender_response_headers]" type="text" id="transifex_live_settings_prerender_response_headers" value="<?php echo htmlentities( stripslashes($prerender_response_headers)); ?>" class="regular-text" placeholder="<?php _e( 'Custom Headers for Prerender Responses', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>">
</p>
<p>
<input type="checkbox" id="transifex_live_settings_prerender_enable_cookie" name="transifex_live_settings[prerender_enable_cookie]" value="1" <?php echo $checked_prerender_enable_cookie ?> /><?php _e( 'Enable Custom Cookie Prerender Responses', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>
<input name="transifex_live_settings[prerender_cookie]" type="text" id="transifex_live_settings_prerender_cookie" value="<?php echo htmlentities( stripslashes($prerender_cookie)); ?>" class="regular-text" placeholder="<?php _e( 'Custom Cookie for Prerender Responses', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>">
</p>
<p>
<input name="transifex_live_settings[whitelist_crawlers]" type="text" id="transifex_live_settings_whitelist_crawlers" value="<?php echo $settings['whitelist_crawlers']; ?>" class="regular-text" placeholder="<?php _e( 'Regex whitelist of allowed crawlers.', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>">
<input name="transifex_live_settings[generic_bot_types]" type="text" id="transifex_live_settings_generic_bot_types" value="<?php echo $settings['generic_bot_types']; ?>" class="regular-text" placeholder="<?php _e( 'Regex list of crawler type keywords.', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?>">
</p>
</div>
<p class="description"><?php _e( 'Important so crawler and bots can see your translated content <a target="_blank" href="#">Check out our docs for details.</a>', TRANSIFEX_LIVE_INTEGRATION_TEXT_DOMAIN ); ?></p>

</div>
</td></tr>
</tbody>
</table>
<input type="hidden" id="transifex_live_settings_enable_prerender_check" name="transifex_live_settings[enable_prerender_check]" value="<?php echo $enable_prerender_check ?>" />
<input name="transifex_live_settings[enable_custom_urls]" id="transifex_live_settings_custom_urls" type="hidden" value="<?php echo $checked_custom_urls ?>" >
<input type="hidden" value="<?php echo htmlentities( stripslashes( $language_map ) ) ?>" name="transifex_live_settings[language_map]" id="transifex_live_settings_language_map" />
<input type="hidden" value="<?php echo htmlentities( stripslashes( $transifex_settings_settings ) ) ?>" name="transifex_live_transifex_settings[settings]" id="transifex_live_transifex_settings_settings" />
Expand Down
30 changes: 28 additions & 2 deletions includes/admin/transifex-live-integration-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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'];
Expand All @@ -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 );
Expand Down Expand Up @@ -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'] ) ) : '';
Expand Down
40 changes: 39 additions & 1 deletion includes/lib/transifex-live-integration-hreflang.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*
Expand Down Expand Up @@ -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 );
Expand Down
111 changes: 91 additions & 20 deletions includes/lib/transifex-live-integration-prerender.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,45 @@ 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
* @param string $prerender_url Url to prerender service
* @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 );
}
}

/*
Expand All @@ -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
Expand All @@ -87,24 +158,24 @@ function call_curl($url) {
function callback( $buffer ) {
global $wp;
$output = $buffer;
$debug_html = '<!--'."\n";
$debug_html = '<!--' . "\n";
$page_url = home_url( $wp->request );
$page_url = rtrim( $page_url, '/' ) . '/';
if (function_exists('curl_version')) {
$curl_response = $this->call_curl($this->prerender_url . $page_url);
if ( function_exists( 'curl_version' ) ) {
$curl_response = $this->call_curl( $this->prerender_url . $page_url );
$header = substr( $curl_response['response'], 0, $curl_response['header_size'] );
$body = substr( $curl_response['response'], $curl_response['header_size'] );
$header_lowercase = strtolower($header);
$header_prerender_check = (strpos( $header_lowercase, 'x-prerender-req' ))?true:false;
$debug_html .= 'X-Prerender-Req Header check:'. $header_prerender_check . "\n";
$debug_html .= 'Check enabled:'.$this->enable_prerender_check."\n";
$header_lowercase = strtolower( $header );
$header_prerender_check = (strpos( $header_lowercase, strtolower( $this->prerender_header_check_key ) )) ? true : false;
$debug_html .= 'X-Prerender-Req Header check:' . $header_prerender_check . "\n";
$debug_html .= 'Check enabled:' . $this->enable_prerender_check . "\n";
if ( $header_prerender_check && $this->enable_prerender_check ) {
$output = ($curl_response['response'])?$body:$output;
$debug_html .= 'Buffer swapped with prerender response.'."\n";
$output = ($curl_response['response']) ? $body : $output;
$debug_html .= 'Buffer swapped with prerender response.' . "\n";
}
$debug_html .= $curl_response['url'] . "\n";
$debug_html .= $header."\n";
$debug_html .= $curl_response['error']."\n";
$debug_html .= $header . "\n";
$debug_html .= $curl_response['error'] . "\n";
} else {
$debug_html .= 'Curl functions missing, skipping prerender call';
}
Expand Down
2 changes: 1 addition & 1 deletion includes/lib/transifex-live-integration-rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct( $settings, $rewrite_options ) {
if ( isset( $rewrite_options['add_rewrites_feed'] ) ) {
$this->rewrite_options[] = ($rewrite_options['add_rewrites_feed']) ? 'feed' : '';
}
if ( isset( $rewrite_options['add_rewrites_feed'] ) ) {
if ( isset( $rewrite_options['add_rewrites_permalink_tag'] ) ) {
$this->rewrite_options[] = ($rewrite_options['add_rewrites_permalink_tag']) ? 'permalink_tag' : '';
}
if ( !empty( $settings['languages'] ) ) {
Expand Down
13 changes: 11 additions & 2 deletions includes/transifex-live-integration-defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,19 @@ static function settings()
'static_frontpage_support' => false,
'enable_prerender' => 0,
'prerender_url' => '',
'enable_prerender_check' => 1,
'whitelist_crawlers' =>
'googlebot|yahoo|bingbot|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest/0.|developers.google.com/+/web/snippet|slackbot|vkshare|w3c_validator|redditbot|applebot|whatsapp|flipboard',
'generic_bot_types' => 'bot|crawl|slurp|spider'
'generic_bot_types' => 'bot|crawl|slurp|spider',
'enable_prerender_check' => 1,
'prerender_header_check_key' => 'X-Prerender-Req',
'prerender_header_check_value' => 'TRUE',
'prerender_enable_response_header' => 0,

'prerender_response_headers' => '{"Expires" : "Tue, 03 Jul 2001 06:00:00 GMT", "Last-Modified" : "{now} GMT","Cache-Control":"max-age=0, no-cache, must-revalidate, proxy-revalidate"}',
'prerender_enable_vary_header' => 1,
'prerender_vary_header_value' => 'User-Agent,X-Prerender-Req',
'prerender_enable_cookie' => 0,
'prerender_cookie' => '{"wordpress_test_cookie" : "WP+Cookie+check"}'
);
}

Expand Down
Loading

0 comments on commit 7fcb2fd

Please sign in to comment.