| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* manages links filters needed on both frontend and admin |
| 5 |
* |
| 6 |
* @since 1.8 |
| 7 |
*/ |
| 8 |
class PLL_Filters_Links { |
| 9 |
public $links, $links_model, $model, $options; |
| 10 |
|
| 11 |
/** |
| 12 |
* constructor |
| 13 |
* |
| 14 |
* @since 1.8 |
| 15 |
* |
| 16 |
* @param object $polylang |
| 17 |
*/ |
| 18 |
public function __construct( &$polylang ) { |
| 19 |
$this->links = &$polylang->links; |
| 20 |
$this->links_model = &$polylang->links_model; |
| 21 |
$this->model = &$polylang->model; |
| 22 |
$this->options = &$polylang->options; |
| 23 |
|
| 24 |
// low priority on links filters to come after any other modifications |
| 25 |
if ( $this->options['force_lang'] ) { |
| 26 |
add_filter( 'post_link', array( $this, 'post_type_link' ), 20, 2 ); |
| 27 |
add_filter( '_get_page_link', array( $this, '_get_page_link' ), 20, 2 ); |
| 28 |
} |
| 29 |
|
| 30 |
add_filter( 'post_type_link', array( $this, 'post_type_link' ), 20, 2 ); |
| 31 |
add_filter( 'term_link', array( $this, 'term_link' ), 20, 3 ); |
| 32 |
|
| 33 |
if ( $this->options['force_lang'] > 0 ) { |
| 34 |
add_filter( 'attachment_link', array( $this, 'attachment_link' ), 20, 2 ); |
| 35 |
} |
| 36 |
|
| 37 |
if ( 3 === $this->options['force_lang'] ) { |
| 38 |
add_filter( 'preview_post_link', array( $this, 'preview_post_link' ), 20 ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* modifies page links |
| 44 |
* |
| 45 |
* @since 1.7 |
| 46 |
* |
| 47 |
* @param string $link post link |
| 48 |
* @param int $post_id post ID |
| 49 |
* @return string modified post link |
| 50 |
*/ |
| 51 |
public function _get_page_link( $link, $post_id ) { |
| 52 |
// /!\ WP does not use pretty permalinks for preview |
| 53 |
return false !== strpos( $link, 'preview=true' ) && false !== strpos( $link, 'page_id=' ) ? $link : $this->links_model->add_language_to_link( $link, $this->model->post->get_language( $post_id ) ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* modifies attachment links |
| 58 |
* |
| 59 |
* @since 1.6.2 |
| 60 |
* |
| 61 |
* @param string $link attachment link |
| 62 |
* @param int $post_id attachment link |
| 63 |
* @return string modified attachment link |
| 64 |
*/ |
| 65 |
public function attachment_link( $link, $post_id ) { |
| 66 |
return wp_get_post_parent_id( $post_id ) ? $link : $this->links_model->add_language_to_link( $link, $this->model->post->get_language( $post_id ) ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* modifies custom posts links |
| 71 |
* |
| 72 |
* @since 1.6 |
| 73 |
* |
| 74 |
* @param string $link post link |
| 75 |
* @param object $post post object |
| 76 |
* @return string modified post link |
| 77 |
*/ |
| 78 |
public function post_type_link( $link, $post ) { |
| 79 |
// /!\ WP does not use pretty permalinks for preview |
| 80 |
if ( ( false === strpos( $link, 'preview=true' ) || false === strpos( $link, 'p=' ) ) && $this->model->is_translated_post_type( $post->post_type ) ) { |
| 81 |
$lang = $this->model->post->get_language( $post->ID ); |
| 82 |
$link = $this->options['force_lang'] ? $this->links_model->add_language_to_link( $link, $lang ) : $link; |
| 83 |
|
| 84 |
/** |
| 85 |
* Filter a post or custom post type link |
| 86 |
* |
| 87 |
* @since 1.6 |
| 88 |
* |
| 89 |
* @param string $link the post link |
| 90 |
* @param object $lang the current language |
| 91 |
* @param object $post the post object |
| 92 |
*/ |
| 93 |
$link = apply_filters( 'pll_post_type_link', $link, $lang, $post ); |
| 94 |
} |
| 95 |
|
| 96 |
return $link; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* modifies term link |
| 101 |
* |
| 102 |
* @since 0.7 |
| 103 |
* |
| 104 |
* @param string $link term link |
| 105 |
* @param object $term term object |
| 106 |
* @param string $tax taxonomy name |
| 107 |
* @return string modified term link |
| 108 |
*/ |
| 109 |
public function term_link( $link, $term, $tax ) { |
| 110 |
if ( $this->model->is_translated_taxonomy( $tax ) ) { |
| 111 |
$lang = $this->model->term->get_language( $term->term_id ); |
| 112 |
$link = $this->options['force_lang'] ? $this->links_model->add_language_to_link( $link, $lang ) : $link; |
| 113 |
|
| 114 |
/** |
| 115 |
* Filter a term link |
| 116 |
* |
| 117 |
* @since 1.6 |
| 118 |
* |
| 119 |
* @param string $link the term link |
| 120 |
* @param object $lang the current language |
| 121 |
* @param object $term the term object |
| 122 |
*/ |
| 123 |
return apply_filters( 'pll_term_link', $link, $lang, $term ); |
| 124 |
} |
| 125 |
|
| 126 |
// in case someone calls get_term_link for the 'language' taxonomy |
| 127 |
if ( 'language' === $tax ) { |
| 128 |
return $this->links_model->home_url( $term ); |
| 129 |
} |
| 130 |
|
| 131 |
return $link; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* FIXME: keeps the preview post link on default domain when using multiple domains |
| 136 |
* |
| 137 |
* @since 1.6.1 |
| 138 |
* |
| 139 |
* @param string $url |
| 140 |
* @return string modified url |
| 141 |
*/ |
| 142 |
public function preview_post_link( $url ) { |
| 143 |
return $this->links_model->remove_language_from_link( $url ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|