| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* some common code for PLL_Admin_Filters_Post and PLL_Admin_Filters_Media |
| 5 |
* |
| 6 |
* @since 1.5 |
| 7 |
*/ |
| 8 |
abstract class PLL_Admin_Filters_Post_Base { |
| 9 |
public $links, $model, $pref_lang; |
| 10 |
|
| 11 |
/* |
| 12 |
* constructor: setups filters and actions |
| 13 |
* |
| 14 |
* @since 1.2 |
| 15 |
* |
| 16 |
* @param object $polylang |
| 17 |
*/ |
| 18 |
public function __construct(&$polylang) { |
| 19 |
$this->links = &$polylang->links; |
| 20 |
$this->model = &$polylang->model; |
| 21 |
$this->pref_lang = &$polylang->pref_lang; |
| 22 |
} |
| 23 |
|
| 24 |
/* |
| 25 |
* allows to set a language by default for posts if it has no language yet |
| 26 |
* |
| 27 |
* @since 1.5 |
| 28 |
* |
| 29 |
* @param int $post_id |
| 30 |
*/ |
| 31 |
public function set_default_language($post_id) { |
| 32 |
if (!$this->model->get_post_language($post_id)) { |
| 33 |
if (isset($_GET['new_lang'])) |
| 34 |
$this->model->set_post_language($post_id, $_GET['new_lang']); |
| 35 |
|
| 36 |
elseif (($parent_id = wp_get_post_parent_id($post_id)) && $parent_lang = $this->model->get_post_language($parent_id)) |
| 37 |
$this->model->set_post_language($post_id, $parent_lang); |
| 38 |
|
| 39 |
else |
| 40 |
$this->model->set_post_language($post_id, $this->pref_lang); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/* |
| 45 |
* save translations from language metabox |
| 46 |
* |
| 47 |
* @since 1.5 |
| 48 |
* |
| 49 |
* @param int $post_id |
| 50 |
* @param array $arr |
| 51 |
* @return array |
| 52 |
*/ |
| 53 |
protected function save_translations($post_id, $arr) { |
| 54 |
// security check |
| 55 |
// as 'wp_insert_post' can be called from outside WP admin |
| 56 |
check_admin_referer('pll_language', '_pll_nonce'); |
| 57 |
|
| 58 |
// save translations after checking the translated post is in the right language |
| 59 |
foreach ($arr as $lang => $tr_id) |
| 60 |
$translations[$lang] = ($tr_id && $this->model->get_post_language((int) $tr_id)->slug == $lang) ? (int) $tr_id : 0; |
| 61 |
|
| 62 |
$this->model->save_translations('post', $post_id, $translations); |
| 63 |
|
| 64 |
// refresh language cache when a static front page has been translated |
| 65 |
if (($pof = get_option('page_on_front')) && in_array($pof, $translations)) |
| 66 |
$this->model->clean_languages_cache(); |
| 67 |
|
| 68 |
return $translations; |
| 69 |
} |
| 70 |
} |
| 71 |
|