PLL_Filters_Links

Manages links filters needed on both frontend and admin.

Defined (1)

The class is defined in the following location(s).

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