| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Manages links related functions |
| 5 |
* |
| 6 |
* @since 1.8 |
| 7 |
*/ |
| 8 |
class PLL_Admin_Links extends PLL_Links { |
| 9 |
|
| 10 |
/** |
| 11 |
* Get the link to create a new post translation |
| 12 |
* |
| 13 |
* @since 1.5 |
| 14 |
* |
| 15 |
* @param int $post_id the source post id |
| 16 |
* @param object $language the language of the new translation |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
public function get_new_post_translation_link( $post_id, $language ) { |
| 20 |
$post_type = get_post_type( $post_id ); |
| 21 |
$post_type_object = get_post_type_object( get_post_type( $post_id ) ); |
| 22 |
if ( ! current_user_can( $post_type_object->cap->create_posts ) ) { |
| 23 |
return ''; |
| 24 |
} |
| 25 |
|
| 26 |
// Special case for the privacy policy page which is associated to a specific capability |
| 27 |
if ( 'page' === $post_type_object->name && ! current_user_can( 'manage_privacy_options' ) ) { |
| 28 |
$privacy_page = get_option( 'wp_page_for_privacy_policy' ); |
| 29 |
if ( $privacy_page && in_array( $post_id, $this->model->post->get_translations( $privacy_page ) ) ) { |
| 30 |
return ''; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
if ( 'attachment' == $post_type ) { |
| 35 |
$args = array( |
| 36 |
'action' => 'translate_media', |
| 37 |
'from_media' => $post_id, |
| 38 |
'new_lang' => $language->slug, |
| 39 |
); |
| 40 |
|
| 41 |
// Add nonce for media as we will directly publish a new attachment from a click on this link |
| 42 |
$link = wp_nonce_url( add_query_arg( $args, admin_url( 'admin.php' ) ), 'translate_media' ); |
| 43 |
} else { |
| 44 |
$args = array( |
| 45 |
'post_type' => $post_type, |
| 46 |
'from_post' => $post_id, |
| 47 |
'new_lang' => $language->slug, |
| 48 |
); |
| 49 |
|
| 50 |
$link = add_query_arg( $args, admin_url( 'post-new.php' ) ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Filter the new post translation link |
| 55 |
* |
| 56 |
* @since 1.8 |
| 57 |
* |
| 58 |
* @param string $link the new post translation link |
| 59 |
* @param object $language the language of the new translation |
| 60 |
* @param int $post_id the source post id |
| 61 |
*/ |
| 62 |
return apply_filters( 'pll_get_new_post_translation_link', $link, $language, $post_id ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns html markup for a new post translation link |
| 67 |
* |
| 68 |
* @since 1.8 |
| 69 |
* |
| 70 |
* @param int $post_id |
| 71 |
* @param object $language |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
public function new_post_translation_link( $post_id, $language ) { |
| 75 |
$link = $this->get_new_post_translation_link( $post_id, $language ); |
| 76 |
return $link ? sprintf( |
| 77 |
'<a href="%1$s" class="pll_icon_add"><span class="screen-reader-text">%2$s</span></a>', |
| 78 |
esc_url( $link ), |
| 79 |
/* translators: accessibility text, %s is a native language name */ |
| 80 |
esc_html( sprintf( __( 'Add a translation in %s', 'polylang' ), $language->name ) ) |
| 81 |
) : ''; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns html markup for a translation link |
| 86 |
* |
| 87 |
* @since 1.4 |
| 88 |
* |
| 89 |
* @param int $post_id translation post id |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
public function edit_post_translation_link( $post_id ) { |
| 93 |
$link = get_edit_post_link( $post_id ); |
| 94 |
$language = $this->model->post->get_language( $post_id ); |
| 95 |
return $link ? sprintf( |
| 96 |
'<a href="%1$s" class="pll_icon_edit"><span class="screen-reader-text">%2$s</span></a>', |
| 97 |
esc_url( $link ), |
| 98 |
/* translators: accessibility text, %s is a native language name */ |
| 99 |
esc_html( sprintf( __( 'Edit the translation in %s', 'polylang' ), $language->name ) ) |
| 100 |
) : ''; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get the link to create a new term translation |
| 105 |
* |
| 106 |
* @since 1.5 |
| 107 |
* |
| 108 |
* @param int $term_id |
| 109 |
* @param string $taxonomy |
| 110 |
* @param string $post_type |
| 111 |
* @param object $language |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
public function get_new_term_translation_link( $term_id, $taxonomy, $post_type, $language ) { |
| 115 |
$tax = get_taxonomy( $taxonomy ); |
| 116 |
if ( ! $tax || ! current_user_can( $tax->cap->edit_terms ) ) { |
| 117 |
return ''; |
| 118 |
} |
| 119 |
|
| 120 |
$args = array( |
| 121 |
'taxonomy' => $taxonomy, |
| 122 |
'post_type' => $post_type, |
| 123 |
'from_tag' => $term_id, |
| 124 |
'new_lang' => $language->slug, |
| 125 |
); |
| 126 |
|
| 127 |
$link = add_query_arg( $args, admin_url( 'edit-tags.php' ) ); |
| 128 |
|
| 129 |
/** |
| 130 |
* Filter the new term translation link |
| 131 |
* |
| 132 |
* @since 1.8 |
| 133 |
* |
| 134 |
* @param string $link the new term translation link |
| 135 |
* @param object $language the language of the new translation |
| 136 |
* @param int $term_id the source term id |
| 137 |
* @param string $taxonomy |
| 138 |
* @param string $post_type |
| 139 |
*/ |
| 140 |
return apply_filters( 'pll_get_new_term_translation_link', $link, $language, $term_id, $taxonomy, $post_type ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Returns html markup for a new term translation |
| 145 |
* |
| 146 |
* @since 1.8 |
| 147 |
* |
| 148 |
* @param int $term_id |
| 149 |
* @param string $taxonomy |
| 150 |
* @param string $post_type |
| 151 |
* @param object $language |
| 152 |
* @return string |
| 153 |
*/ |
| 154 |
public function new_term_translation_link( $term_id, $taxonomy, $post_type, $language ) { |
| 155 |
$link = $this->get_new_term_translation_link( $term_id, $taxonomy, $post_type, $language ); |
| 156 |
return $link ? sprintf( |
| 157 |
'<a href="%1$s" class="pll_icon_add"><span class="screen-reader-text">%2$s</span></a>', |
| 158 |
esc_url( $link ), |
| 159 |
/* translators: accessibility text, %s is a native language name */ |
| 160 |
esc_html( sprintf( __( 'Add a translation in %s', 'polylang' ), $language->name ) ) |
| 161 |
) : ''; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Returns html markup for a term translation link |
| 166 |
* |
| 167 |
* @since 1.4 |
| 168 |
* |
| 169 |
* @param object $term_id translation term id |
| 170 |
* @param string $taxonomy |
| 171 |
* @param string $post_type |
| 172 |
* @return string |
| 173 |
*/ |
| 174 |
public function edit_term_translation_link( $term_id, $taxonomy, $post_type ) { |
| 175 |
$link = get_edit_term_link( $term_id, $taxonomy, $post_type ); |
| 176 |
$language = $this->model->term->get_language( $term_id ); |
| 177 |
return $link ? sprintf( |
| 178 |
'<a href="%1$s" class="pll_icon_edit"><span class="screen-reader-text">%2$s</span></a>', |
| 179 |
esc_url( $link ), |
| 180 |
/* translators: accessibility text, %s is a native language name */ |
| 181 |
esc_html( sprintf( __( 'Edit the translation in %s', 'polylang' ), $language->name ) ) |
| 182 |
) : ''; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|