Skip to content

Commit

Permalink
Hreflang generator should respect http protocol
Browse files Browse the repository at this point in the history
* The links that are generated should adhere to the requested http
protocol (http or https).
* We update the travis.yml to run tests on all branches.
* Update codeception to version 4.
* Change all tests to use the newly introduced BaseTestCase class.
* Fix ReverseHardLinkTest crashing test.
* Remove unused `_boostrap.php` of codeception.yml.
* Bump plugin version.
  • Loading branch information
ThemisB committed Jun 1, 2020
1 parent a3f3227 commit b6359eb
Show file tree
Hide file tree
Showing 19 changed files with 808 additions and 90 deletions.
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
branches:
only:
- master
- devel

sudo: required
dist: trusty
language: php
Expand All @@ -27,7 +22,8 @@ addons:
before_script:
- sudo whoami
- pwd
- COMPOSER_MEMORY_LIMIT=-1 composer global require "codeception/codeception:2.5.5"
- COMPOSER_MEMORY_LIMIT=-1 composer global require "codeception/module-asserts:1.2"
- COMPOSER_MEMORY_LIMIT=-1 composer global require "codeception/codeception:4.1.5"
- sudo ln -s /home/travis/.config/composer/vendor/bin/codecept /usr/local/bin/codecept
- sudo apt-add-repository multiverse && sudo apt-get update
- sudo add-apt-repository -y ppa:chris-lea/node.js
Expand Down
1 change: 0 additions & 1 deletion codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ paths:
support: tests/_support
envs: tests/_envs
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
extensions:
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"require": {
"psy/psysh": "@stable",
"codeception/codeception": "^2.1"
"codeception/codeception": "^4.0"
},
"require-dev": {
"codeception/module-asserts": "^1.2",
"phpunit/phpunit": "^7.0"
}
}
22 changes: 15 additions & 7 deletions includes/lib/transifex-live-integration-hreflang.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ class Transifex_Live_Integration_Hreflang {

/*
* A key/value array that maps Transifex locale->plugin code
* @var language_map array
* @var language_map array
*/
private $language_map;

/*
* A list of Transifex locales, for enabled languages
* @var languages array
* @var languages array
*/
private $languages;

/*
* The site_url with a placeholder for language
* @var tokenized_url string
* @var tokenized_url string
*/
private $tokenized_url;
private $rewrite_options;
Expand Down Expand Up @@ -99,8 +99,16 @@ private function generate_languages_hreflang( $raw_url, $languages,
$ret = [ ];
foreach ($languages as $language) {
$arr = [ ];

$arr['href'] = $url_map[$language];
$site_url = site_url();
$href_link = $url_map[$language];
$href_link_parts = explode(':', $href_link);
if (count($href_link_parts) && ($href_link_parts[0] === 'http' || $href_link_parts[0] === 'https')) {
$protocol = Transifex_Live_Integration_Util::get_http_requested_protocol();
$href_link_parts[0] = $protocol;
$arr['href'] = implode($href_link_parts, ':');
} else {
$arr['href'] = $url_map[$language];
}
$arr['hreflang'] = $hreflang_map[$language];
array_push( $ret, $arr );
}
Expand All @@ -126,7 +134,7 @@ public function render_hreflang() {
$source_url = rtrim( $unslashed_source_url, '/' ) . '/';
$hreflang_out = '';
$hreflang_out .= <<<SOURCE
<link rel="alternate" href="$source_url" hreflang="$source"/>\n
<link rel="alternate" href="$source_url" hreflang="$source"/>\n
SOURCE;
$hreflangs = $this->generate_languages_hreflang( $source_url_path, $this->languages, $this->language_map, $this->hreflang_map );
foreach ($hreflangs as $hreflang) {
Expand All @@ -142,4 +150,4 @@ public function render_hreflang() {

}

?>
?>
21 changes: 14 additions & 7 deletions includes/lib/transifex-live-integration-rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Transifex_Live_Integration_Rewrite {
private $source_language;

/**
* List of languages used by rewrite
* List of languages used by rewrite
* @var array
*/
private $language_codes;
Expand All @@ -38,8 +38,14 @@ class Transifex_Live_Integration_Rewrite {
*/
public function __construct( $settings, $rewrite_options ) {
Plugin_Debug::logTrace();
if ( !defined( 'TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE' ) ) {
define( 'TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE', dirname( __FILE__, 3));
}

include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/common/transifex-live-integration-validators.php';
include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/override/transifex-live-integration-generate-rewrite-rules.php';
include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE .'/includes/lib/transifex-live-integration-wp-services.php';

$this->rewrite_options = [ ];
$this->languages_regex = $settings['languages_regex'];
$this->source_language = $settings['source_language'];
Expand Down Expand Up @@ -97,6 +103,7 @@ public function __construct( $settings, $rewrite_options ) {
$this->rewrite_pattern = $pattern;
}
}
$this->wp_services = new Transifex_Live_Integration_WP_Services();
}

public function get_language_url( $atts ) {
Expand Down Expand Up @@ -138,15 +145,15 @@ function wp_hook() {

/*
* This function takes any WP link and associated language configuration and returns a localized url
*
*
* @param string $lang Current language
* @param string $link The url to localize
* @param array $languages_map A key/value array that maps Transifex locale->plugin code
* @param string $source_lang The current source language
* @return string Returns modified link
*/

static function reverse_hard_link( $lang, $link, $languages_map, $source_lang,
function reverse_hard_link( $lang, $link, $languages_map, $source_lang,
$pattern ) {
Plugin_Debug::logTrace();
if ( !(isset( $pattern )) ) {
Expand All @@ -170,7 +177,7 @@ static function reverse_hard_link( $lang, $link, $languages_map, $source_lang,
if ( count( $m ) > 1 ) {
$link = str_replace( $m[1], $lang, $m[0] );
} else {
$site_host = parse_url(site_url())['host'];
$site_host = parse_url($this->wp_services->get_site_url())['host'];
$parsed_url = parse_url($link);
$link_host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
// change only wordpress links - not links reffering to other domains
Expand All @@ -188,11 +195,11 @@ static function reverse_hard_link( $lang, $link, $languages_map, $source_lang,
}

/*
* WP pre_post_link filter, adds lang to permalink
* WP pre_post_link filter, adds lang to permalink
* @param string $permalink The permalink to filter
* @param object $post The post object
* @param ??? $leavename what this is I dont even know
* @return string filtered permalink
* @return string filtered permalink
*/

function pre_post_link_hook( $permalink, $post, $leavename ) {
Expand Down Expand Up @@ -331,7 +338,7 @@ function home_url_hook( $url ) {
$retlink = $this->reverse_hard_link( $this->lang, $url, $this->languages_map, $this->source_language, $this->rewrite_pattern );
return $retlink;
}

/*
* WP the_content_hook hook, filters links using the the_content function
* @param string $string The string to filter
Expand Down
18 changes: 18 additions & 0 deletions includes/lib/transifex-live-integration-wp-services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* @package TransifexLiveIntegration
*/

/**
* Provides WP services to other classes.
*/
class Transifex_Live_Integration_WP_Services {

/*
* Wraps WP site_url().
*/
function get_site_url() {
return site_url();
}
}
13 changes: 9 additions & 4 deletions includes/transifex-live-integration-util.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

/**
* Common util functions
* Common util functions
* @package TransifexLiveIntegration
*/
/*
* Common util
* Common util
*/
class Transifex_Live_Integration_Util {

Expand All @@ -20,6 +20,11 @@ static function query_vars_hook( $vars ) {
return $vars;
}

static function get_http_requested_protocol() {
Plugin_Debug::logTrace();
return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https" : "http";
}

/*
* Checks if an agent is on the whitelist
* @param string $agent User agent to search against
Expand Down Expand Up @@ -68,12 +73,12 @@ static function is_prerender_req( $agent ) {

/*
* This function is the core backend check to determine if user agent should be prerender'd
*
*
* @param string $req_user_agent User agent string, generally from browser
* @param string $req_escaped_fragment Escaped fragment string, generally from browser
* @param string $bot_types A regex string list of bot keywords for quick matching
* @param string $whitelist_names A regex string list of whitelisted bots
* @return bool Checks a given user agent for bot-ability
* @return bool Checks a given user agent for bot-ability
*/

static function prerender_check( $req_user_agent, $req_escaped_fragment,
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Contributors: txmatthew, ThemeBoy, brooksx
Tags: transifex, localize, localization, multilingual, international, SEO
Requires at least: 3.5.2
Tested up to: 5.4
Stable tag: 1.3.21
Stable tag: 1.3.22
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -79,6 +79,10 @@ It is also recommended to use [widgets](https://codex.wordpress.org/Widgets_API

== Changelog ==

= 1.3.22 =
Fix a bug related with the generation of the HTTP version of the links
that have the hreflang attribute.

= 1.3.21 =
Update the latest tested WordPress version (5.4)

Expand Down
4 changes: 1 addition & 3 deletions tests/_support/_generated/AcceptanceTesterActions.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php //[STAMP] b59d79cf1aa77768d094061b69a3a05b
<?php //[STAMP] 7340e56da20a5658034ce78a24cfe5fc
namespace _generated;

// This class was automatically generated by build task
// You should not change it manually as it will be overwritten on next build
// @codingStandardsIgnoreFile

use Helper\Acceptance;

trait AcceptanceTesterActions
{
/**
Expand Down
4 changes: 1 addition & 3 deletions tests/_support/_generated/FunctionalTesterActions.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php //[STAMP] a6e9b191d980221f283cba44a4ae1415
<?php //[STAMP] 324af9ab641893a90326e4ddf90e85be
namespace _generated;

// This class was automatically generated by build task
// You should not change it manually as it will be overwritten on next build
// @codingStandardsIgnoreFile

use Helper\Functional;

trait FunctionalTesterActions
{
/**
Expand Down
Loading

0 comments on commit b6359eb

Please sign in to comment.