| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Manages links related functions |
| 8 | 5 | * |
| @@ -8,28 +5,11 @@ | ||
| 8 | 5 | * |
| 9 | 6 | * @since 1.2 |
| 10 | 7 | */ |
| 11 | 8 | class PLL_Links { |
| 12 | - /** | |
| 13 | - * Stores the plugin options. | |
| 14 | - * | |
| 15 | - * @var array | |
| 16 | - */ | |
| 17 | - public $options; | |
| 9 | + public $links_model, $model, $options; | |
| 18 | 10 | |
| 19 | 11 | /** |
| 20 | - * @var PLL_Model | |
| 21 | - */ | |
| 22 | - public $model; | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * Instance of a child class of PLL_Links_Model. | |
| 26 | - * | |
| 27 | - * @var PLL_Links_Model | |
| 28 | - */ | |
| 29 | - public $links_model; | |
| 30 | - | |
| 31 | - /** | |
| 32 | 12 | * Constructor |
| 33 | 13 | * |
| 34 | 14 | * @since 1.2 |
| 35 | 15 | * |
| @@ -41,17 +21,45 @@ | ||
| 41 | 21 | $this->options = &$polylang->options; |
| 42 | 22 | } |
| 43 | 23 | |
| 44 | 24 | /** |
| 45 | - * Returns the home url in the requested language. | |
| 25 | + * Returns the home url in the requested language | |
| 46 | 26 | * |
| 47 | 27 | * @since 1.3 |
| 48 | 28 | * |
| 49 | - * @param PLL_Language|string $language The language. | |
| 50 | - * @param bool $is_search Optional, whether we need the home url for a search form, defaults to false. | |
| 51 | - * @return string | |
| 29 | + * @param object|string $language | |
| 30 | + * @param bool $is_search optional whether we need the home url for a search form, defaults to false | |
| 52 | 31 | */ |
| 53 | 32 | public function get_home_url( $language, $is_search = false ) { |
| 54 | 33 | $language = is_object( $language ) ? $language : $this->model->get_language( $language ); |
| 55 | 34 | return $is_search ? $language->search_url : $language->home_url; |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * Checks if the current user can read the post | |
| 39 | + * | |
| 40 | + * @since 1.5 | |
| 41 | + * | |
| 42 | + * @param int $post_id | |
| 43 | + * @return bool | |
| 44 | + */ | |
| 45 | + public function current_user_can_read( $post_id ) { | |
| 46 | + $post = get_post( $post_id ); | |
| 47 | + | |
| 48 | + if ( 'inherit' === $post->post_status && $post->post_parent ) { | |
| 49 | + $post = get_post( $post->post_parent ); | |
| 50 | + } | |
| 51 | + | |
| 52 | + if ( 'inherit' === $post->post_status || in_array( $post->post_status, get_post_stati( array( 'public' => true ) ) ) ) { | |
| 53 | + return true; | |
| 54 | + } | |
| 55 | + | |
| 56 | + // Follow WP practices, which shows links to private posts ( when readable ), but not for draft posts ( ex: get_adjacent_post_link() ) | |
| 57 | + if ( in_array( $post->post_status, get_post_stati( array( 'private' => true ) ) ) ) { | |
| 58 | + $post_type_object = get_post_type_object( $post->post_type ); | |
| 59 | + $user = wp_get_current_user(); | |
| 60 | + return is_user_logged_in() && ( current_user_can( $post_type_object->cap->read_private_posts ) || $user->ID == $post->post_author ); // Comparison must not be strict! | |
| 61 | + } | |
| 62 | + | |
| 63 | + return false; | |
| 56 | 64 | } |
| 57 | 65 | } |