Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Minor improvements. #114
Browse files Browse the repository at this point in the history
  • Loading branch information
tfrommen committed Jun 26, 2015
1 parent 6966b82 commit f0b6e57
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
62 changes: 31 additions & 31 deletions inc/hreflang-header/Mlp_Hreflang_Header_Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,39 @@
class Mlp_Hreflang_Header_Output {

/**
* @type Mlp_Language_Api_Interface
* @var Mlp_Language_Api_Interface
*/
private $language_api;

/**
* @type array
* @var array
*/
private $translations = array();

/**
* @param Mlp_Language_Api_Interface $language_api
* Constructor. Set up properties.
*
* @param Mlp_Language_Api_Interface $language_api Language API object.
*/
public function __construct( Mlp_Language_Api_Interface $language_api ) {

$this->language_api = $language_api;
}

/**
* Prints language attributes into the HTML head
* Print language attributes into the HTML head.
*
* @wp-hook wp_head
*
* @return void
*/
public function wp_head() {

$translations = $this->get_translations();

if ( empty ( $translations ) ) {
if ( empty( $translations ) ) {
return;
}

/** @var Mlp_Translation_Interface $translation */
foreach ( $translations as $lang => $url ) {
$html = sprintf(
'<link rel="alternate" hreflang="%1$s" href="%2$s" />',
Expand All @@ -52,29 +54,28 @@ public function wp_head() {
/**
* Filter the output of the hreflang links in the HTML head.
*
* @param string $html Markup generated by MultilingualPress
* @param string $lang Language code like 'en-US'
* @param string $url Target URL
* @param string $html Markup generated by MultilingualPress.
* @param string $lang Language code (e.g., 'en-US').
* @param string $url Target URL.
*/
$html = apply_filters( 'mlp_hreflang_html', $html, $lang, $url );
print $html;
echo apply_filters( 'mlp_hreflang_html', $html, $lang, $url );
}
}

/**
* Adds language attributes to the HTTP header
* Add language attributes to the HTTP header.
*
* @wp-hook template_redirect
*
* @return void
*/
public function http_header() {

$translations = $this->get_translations();

if ( empty ( $translations ) ) {
if ( empty( $translations ) ) {
return;
}

/** @var Mlp_Translation_Interface $translation */
foreach ( $translations as $lang => $url ) {
$header = sprintf(
'Link: <%1$s>; rel="alternate"; hreflang="%2$s"',
Expand All @@ -85,13 +86,12 @@ public function http_header() {
/**
* Filter the output of the hreflang links in the HTTP header.
*
* @param string $header Header generated by MultilingualPress
* @param string $lang Language code like 'en-US'
* @param string $url Target URL
* @param string $header Header generated by MultilingualPress.
* @param string $lang Language code (e.g., 'en-US').
* @param string $url Target URL.
*/
$header = apply_filters( 'mlp_hreflang_http_header', $header, $lang, $url );

if ( ! empty( $header ) ) {
if ( $header ) {
header( $header, FALSE );
}
}
Expand All @@ -108,29 +108,28 @@ private function get_translations() {
return array();
}

/** @var Mlp_Translation_Interface[] $translations */
$translations = $this->language_api->get_translations( array( 'include_base' => TRUE ) );

if ( empty ( $translations ) ) {
if ( empty( $translations ) ) {
$this->translations = array( 'failed' );

return array();
}

$prepared = array();

/** @var Mlp_Translation_Interface $translation */
foreach ( $translations as $translation ) {
$language = $translation->get_language();

$lang = $translation->get_language()
->get_name( 'http' );
$url = $translation->get_remote_url();
$language_name = $language->get_name( 'http' );

if ( ! empty( $url ) ) {
$prepared[ $lang ] = (string) $url;
$url = $translation->get_remote_url();
if ( $url ) {
$prepared[ $language_name ] = $url;
}
}

if ( empty ( $prepared ) ) {
if ( empty( $prepared ) ) {
$this->translations = array( 'failed' );

return array();
Expand All @@ -140,4 +139,5 @@ private function get_translations() {

return $this->translations;
}
}

}
2 changes: 1 addition & 1 deletion inc/types/Mlp_Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function get_remote_url() {
* @param int $target_content_id ID of the target post.
* @param Mlp_Translation_Interface|NULL $translation Translation object. NULL, if there is no translation.
*/
return apply_filters(
return (string) apply_filters(
'mlp_linked_element_link',
(string) $this->remote_url,
$this->get_target_site_id(),
Expand Down

0 comments on commit f0b6e57

Please sign in to comment.