class-tiny-wpml.php
63 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Tiny Compress Images - WordPress plugin. |
| 4 | * Copyright (C) 2015-2018 Tinify B.V. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | class Tiny_WPML { |
| 22 | |
| 23 | public function __construct() { |
| 24 | $this->add_hooks(); |
| 25 | } |
| 26 | |
| 27 | private function add_hooks() { |
| 28 | // When WPML duplicates an attachment in other languages. |
| 29 | add_action( |
| 30 | 'wpml_after_duplicate_attachment', |
| 31 | array( $this, 'copy_tiny_postmeta' ), |
| 32 | 10, |
| 33 | 2 |
| 34 | ); |
| 35 | |
| 36 | // When you add a missing translation text or restore an image |
| 37 | // on the WPML media tranlation popup. |
| 38 | add_action( |
| 39 | 'wpml_after_copy_attached_file_postmeta', |
| 40 | array( $this, 'after_copy_attached_file' ), |
| 41 | 10, |
| 42 | 2 |
| 43 | ); |
| 44 | |
| 45 | // When adding an alternative image on the WPML media translation popup. |
| 46 | add_action( 'wpml_updated_attached_file', array( $this, 'updated_attached_file' ) ); |
| 47 | } |
| 48 | |
| 49 | public function copy_tiny_postmeta( $post_id, $duplicate_post_id ) { |
| 50 | $original_tiny_postmeta = get_post_meta( $post_id, Tiny_Config::META_KEY, true ); |
| 51 | update_post_meta( $duplicate_post_id, Tiny_Config::META_KEY, $original_tiny_postmeta ); |
| 52 | } |
| 53 | |
| 54 | public function after_copy_attached_file( $original_post_id, $post_id ) { |
| 55 | $original_tiny_postmeta = get_post_meta( $original_post_id, Tiny_Config::META_KEY, true ); |
| 56 | update_post_meta( $post_id, Tiny_Config::META_KEY, $original_tiny_postmeta ); |
| 57 | } |
| 58 | |
| 59 | public function updated_attached_file( $post_id ) { |
| 60 | delete_post_meta( $post_id, Tiny_Config::META_KEY ); |
| 61 | } |
| 62 | } |
| 63 |