Advertisement
sarahn

wpml_media.class.php

Nov 14th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.11 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class WPML_media{
  5.  
  6.  
  7. function __construct($ext = false){
  8. add_action('init', array($this,'init'));
  9. }
  10.  
  11. function __destruct(){
  12. return;
  13. }
  14.  
  15. function init(){
  16. global $sitepress, $pagenow;
  17. $this->plugin_localization();
  18.  
  19. // Check if WPML is active. If not display warning message and don't load WPML-media
  20. if(!defined('ICL_SITEPRESS_VERSION') || empty($sitepress)){
  21. add_action('admin_notices', array($this, '_no_wpml_warning'));
  22. return false;
  23. }elseif(version_compare(ICL_SITEPRESS_VERSION, '2.0.5', '<')){
  24. add_action('admin_notices', array($this, '_old_wpml_warning'));
  25. return false;
  26. }
  27.  
  28. $this->languages = null;
  29.  
  30. if(is_admin()){
  31.  
  32. add_action('admin_head',array($this,'js_scripts'));
  33.  
  34. if (1 < count($sitepress->get_active_languages())) {
  35. $this->settings = get_option('wpml_media_settings', array('initial_message_shown' => false));
  36.  
  37. if (!isset($_GET['page']) || $_GET['page'] != 'wpml-media') {
  38. if (!$this->settings['initial_message_shown']) {
  39. add_action('admin_notices', array($this, '_initialize_message'));
  40. }
  41. }
  42.  
  43. add_action('admin_menu', array($this,'menu'));
  44. add_filter('manage_media_columns', array($this, 'manage_media_columns'), 10 , 1);
  45. add_action('manage_media_custom_column', array($this, 'manage_media_custom_column'), 10, 2);
  46. //add_filter('manage_upload_sortable_columns', array($this, 'manage_upload_sortable_columns'));
  47. add_action('parse_query', array($this, 'parse_query'));
  48. add_filter('posts_where', array($this,'posts_where_filter'));
  49. add_filter('views_upload', array($this, 'views_upload'));
  50. add_action('icl_post_languages_options_after', array($this, 'language_options'));
  51.  
  52. // Post/page save actions
  53. add_action('save_post', array($this,'save_post_actions'), 10, 2);
  54.  
  55. add_action('add_attachment', array($this,'save_attachment_actions'));
  56. add_action('edit_attachment', array($this,'save_attachment_actions'));
  57.  
  58.  
  59.  
  60. if($pagenow == 'media-upload.php'){
  61. add_action('media_upload_library', array($this,'language_filter'), 99);
  62. }
  63.  
  64. if($pagenow == 'media.php') {
  65. add_action('admin_footer', array($this,'media_language_options'));
  66. }
  67.  
  68. }
  69.  
  70. $this->ajax_responses();
  71.  
  72. }
  73.  
  74. //add_filter('get_post_metadata', array($this, 'get_post_metadata'), 10, 4);
  75. add_filter('WPML_filter_link', array($this, 'filter_link'), 10, 2);
  76. add_filter('icl_ls_languages', array($this, 'icl_ls_languages'), 10, 1);
  77. add_action('icl_pro_translation_saved', array($this, 'icl_pro_translation_saved'), 10, 1);
  78.  
  79. }
  80.  
  81. function media_language_options() {
  82. global $sitepress;
  83.  
  84. $att_id = $_GET['attachment_id'];
  85. $translations = $this->_get_translations($att_id);
  86. $current_lang = '';
  87. foreach($translations as $lang => $id) {
  88. if ($id == $att_id) {
  89. $current_lang = $lang;
  90. unset($translations[$lang]);
  91. break;
  92. }
  93. }
  94.  
  95. $active_languages = icl_get_languages('orderby=id&order=asc&skip_missing=0');
  96. $lang_links = '';
  97.  
  98. if ($current_lang) {
  99.  
  100. $lang_links = '<strong>' . $active_languages[$current_lang]['native_name'] . '</strong>';
  101.  
  102. }
  103.  
  104. foreach ($translations as $lang => $id) {
  105. $lang_links .= ' | <a href="' . admin_url('media.php?attachment_id=' . $id . '&action=edit') . '">' . $active_languages[$lang]['native_name'] . '</a>';
  106. }
  107.  
  108.  
  109.  
  110. echo '<div id="icl_lang_options" style="display:none">' . $lang_links . '</div>';
  111. }
  112.  
  113. function icl_pro_translation_saved($new_post_id) {
  114. global $wpdb;
  115.  
  116. $post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID = " . $new_post_id);
  117. $trid = $_POST['trid'];
  118. $lang = $_POST['lang'];
  119.  
  120. $source_lang = $wpdb->get_var("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE trid={$trid} AND source_language_code IS NULL");
  121.  
  122. $this->duplicate_post_attachments($new_post_id, $trid, $source_lang, $lang);
  123. }
  124.  
  125. function save_post_actions($pidd, $post){
  126. global $wpdb, $sitepress;
  127.  
  128. list($post_type, $post_status) = $wpdb->get_row("SELECT post_type, post_status FROM {$wpdb->posts} WHERE ID = " . $pidd, ARRAY_N);
  129. // exceptions
  130. if(
  131. !$sitepress->is_translated_post_type($post_type)
  132. || isset($_POST['autosave'])
  133. || (isset($_POST['post_ID']) && $_POST['post_ID']!=$pidd) || (isset($_POST['post_type']) && $_POST['post_type']=='revision')
  134. || $post_type == 'revision'
  135. || get_post_meta($pidd, '_wp_trash_meta_status', true)
  136. || ( isset($_GET['action']) && $_GET['action']=='restore')
  137. || $post_status == 'auto-draft'
  138. ){
  139. return;
  140. }
  141.  
  142. if (isset($_POST['icl_trid'])) {
  143. // save the post from the edit screen.
  144. if (isset($_POST['icl_duplicate_attachments'])) {
  145. update_post_meta($pidd, '_wpml_media_duplicate', intval($_POST['icl_duplicate_attachments']));
  146. } else {
  147. update_post_meta($pidd, '_wpml_media_duplicate', "0");
  148. }
  149.  
  150. if (isset($_POST['icl_duplicate_featured_image'])) {
  151. update_post_meta($pidd, '_wpml_media_featured', intval($_POST['icl_duplicate_featured_image']));
  152. } else {
  153. update_post_meta($pidd, '_wpml_media_featured', "0");
  154. }
  155.  
  156. $icl_trid = $_POST['icl_trid'];
  157. } else {
  158. // get trid from database.
  159. $icl_trid = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id={$pidd} AND element_type = 'post_$post_type'");
  160. }
  161.  
  162. if ($icl_trid) {
  163. $this->duplicate_post_attachments($pidd, $icl_trid);
  164. }
  165. }
  166.  
  167. function save_attachment_actions($post_id){
  168. global $wpdb, $sitepress;
  169.  
  170. $wpml_media_lang = get_post_meta($post_id, 'wpml_media_lang', true);
  171.  
  172. if(empty($wpml_media_lang)){
  173. $parent_post = $wpdb->get_row($wpdb->prepare(
  174. "SELECT p2.ID, p2.post_type FROM $wpdb->posts p1 JOIN $wpdb->posts p2 ON p1.post_parent =p2.ID WHERE p1.ID=%d"
  175. , $post_id));
  176.  
  177. if($parent_post){
  178. $wpml_media_lang = $sitepress->get_language_for_element($parent_post->ID, 'post_' . $parent_post->post_type);
  179. }
  180.  
  181. if(empty($wpml_media_lang)){
  182. $wpml_media_lang = $sitepress->get_admin_language_cookie();
  183. }
  184. if(empty($wpml_media_lang)){
  185. $wpml_media_lang = $sitepress->get_default_language();
  186. }
  187.  
  188. }
  189.  
  190. if(!empty($wpml_media_lang)){
  191. update_post_meta($post_id, 'wpml_media_lang', $wpml_media_lang);
  192. }
  193.  
  194. }
  195.  
  196. function duplicate_post_attachments($pidd, $icl_trid, $source_lang = null, $lang = null) {
  197. global $wpdb, $sitepress;
  198. if ($icl_trid == "") {
  199. return;
  200. }
  201.  
  202. if (!$source_lang) {
  203. $source_lang = $wpdb->get_var("SELECT source_language_code FROM {$wpdb->prefix}icl_translations WHERE element_id = $pidd AND trid = $icl_trid");
  204. }
  205.  
  206. if ($source_lang == null || $source_lang == "") {
  207. // This is the original see if we should copy to translations
  208.  
  209. $duplicate = get_post_meta($pidd, '_wpml_media_duplicate', true);
  210. $featured = get_post_meta($pidd, '_wpml_media_featured', true);
  211. if ($duplicate || $featured) {
  212. $translations = $wpdb->get_col("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid = $icl_trid");
  213.  
  214. foreach ($translations as $translation_id) {
  215. if ($translation_id && $translation_id != $pidd) {
  216. $duplicate_t = $duplicate;
  217. if ($duplicate_t) {
  218. // See if the translation is marked for duplication
  219. $duplicate_t = get_post_meta($translation_id, '_wpml_media_duplicate', true);
  220. }
  221.  
  222. $lang = $wpdb->get_var("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id = $translation_id AND trid = $icl_trid");
  223. if ($duplicate_t || $duplicate_t == '') {
  224. $source_attachments = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_parent = $pidd AND post_type = 'attachment'");
  225. $attachments = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_parent = $translation_id AND post_type = 'attachment'");
  226.  
  227. foreach ($source_attachments as $source_att_id) {
  228. $found = false;
  229. foreach($attachments as $att_id) {
  230. $duplicate_of = get_post_meta($att_id, 'wpml_media_duplicate_of', true);
  231. if ($duplicate_of == $source_att_id) {
  232. $found = true;
  233. }
  234. }
  235.  
  236. if (!$found) {
  237. $this->create_duplicate_attachment($source_att_id, $translation_id, $lang);
  238. }
  239. }
  240. }
  241.  
  242. $featured_t = $featured;
  243. if ($featured_t) {
  244. // See if the translation is marked for duplication
  245. $featured_t = get_post_meta($translation_id, '_wpml_media_featured', true);
  246. }
  247. if ($featured_t || $featured_t == '') {
  248. $thumbnail_id = get_post_meta($pidd, '_thumbnail_id', true);
  249. if ($thumbnail_id) {
  250. $t_thumbnail_id = $wpdb->get_var( $wpdb->prepare(
  251. "
  252. SELECT post_id
  253. FROM $wpdb->postmeta
  254. WHERE meta_key = 'wpml_media_duplicate_of' AND meta_value = %s
  255. ",
  256. $thumbnail_id
  257. ) );
  258. update_post_meta($translation_id, '_thumbnail_id', $thumbnail_id);
  259. }
  260. }
  261. }
  262. }
  263. }
  264.  
  265. } else {
  266. // This is a translation.
  267.  
  268. $source_id = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE language_code = '$source_lang' AND trid = $icl_trid");
  269.  
  270. if (!$lang) {
  271. $lang = $wpdb->get_var("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id = $pidd AND trid = $icl_trid");
  272. }
  273.  
  274. $duplicate = get_post_meta($pidd, '_wpml_media_duplicate', true);
  275. if ($duplicate === "") {
  276. // check the original state
  277. $duplicate = get_post_meta($source_id, '_wpml_media_duplicate', true);
  278. }
  279.  
  280. if ($duplicate) {
  281. $attachments = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_parent = $pidd AND post_type = 'attachment'");
  282. $source_attachments = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_parent = $source_id AND post_type = 'attachment'");
  283.  
  284. foreach ($source_attachments as $source_att_id) {
  285. $found = false;
  286. foreach($attachments as $att_id) {
  287. $duplicate_of = get_post_meta($att_id, 'wpml_media_duplicate_of', true);
  288. if ($duplicate_of == $source_att_id) {
  289. $found = true;
  290. }
  291. }
  292.  
  293. if (!$found) {
  294. $this->create_duplicate_attachment($source_att_id, $pidd, $lang);
  295. }
  296.  
  297. }
  298. }
  299.  
  300. $featured = get_post_meta($pidd, '_wpml_media_featured', true);
  301. if ($featured === "") {
  302. // check the original state
  303. $featured = get_post_meta($source_id, '_wpml_media_featured', true);
  304. }
  305.  
  306. if ($featured) {
  307. $thumbnail_id = get_post_meta($source_id, '_thumbnail_id', true);
  308. if ($thumbnail_id) {
  309. $t_thumbnail_id = $wpdb->get_var( $wpdb->prepare(
  310. "
  311. SELECT post_id
  312. FROM $wpdb->postmeta
  313. WHERE meta_key = 'wpml_media_duplicate_of' AND meta_value = %s
  314. ",
  315. $thumbnail_id
  316. ) );
  317. update_post_meta($pidd, '_thumbnail_id', $thumbnail_id);
  318. }
  319.  
  320. }
  321.  
  322. }
  323.  
  324. }
  325.  
  326. function language_options() {
  327. global $icl_meta_box_globals, $wpdb;
  328.  
  329. $translation = false;
  330. $source_id = null;
  331. $translated_id = null;
  332. if (sizeof($icl_meta_box_globals['translations']) > 0) {
  333. if (!isset($icl_meta_box_globals['translations'][$icl_meta_box_globals['selected_language']])) {
  334. // We are creating a new translation
  335. $translation = true;
  336. // find the original
  337. foreach ($icl_meta_box_globals['translations'] as $trans_data) {
  338. if ($trans_data->original == '1') {
  339. $source_id = $trans_data->element_id;
  340. break;
  341. }
  342. }
  343. } else {
  344. $trans_data = $icl_meta_box_globals['translations'][$icl_meta_box_globals['selected_language']];
  345. // see if this is an original or a translation.
  346. if ($trans_data->original == '0') {
  347. // double check that it's not the original
  348. // This is because the source_language_code field in icl_translations is not always being set to null.
  349.  
  350. $source_language_code = $wpdb->get_var("SELECT source_language_code FROM {$wpdb->prefix}icl_translations WHERE translation_id = $trans_data->translation_id");
  351. $translation = !($source_language_code == "" || $source_language_code == null);
  352. if ($translation) {
  353. $source_id = $icl_meta_box_globals['translations'][$source_language_code]->element_id;
  354. $translated_id = $trans_data->element_id;
  355. } else {
  356. $source_id = $trans_data->element_id;
  357. }
  358. } else {
  359. $source_id = $trans_data->element_id;
  360. }
  361. }
  362. }
  363.  
  364. echo '<br /><br /><strong>' . __('Media attachments', 'wpml-media') . '</strong>';
  365.  
  366. $checked = '';
  367. if ($translation) {
  368. if ($translated_id) {
  369. $duplicate = get_post_meta($translated_id, '_wpml_media_duplicate', true);
  370. if ($duplicate === "") {
  371. // check the original state
  372. $duplicate = get_post_meta($source_id, '_wpml_media_duplicate', true);
  373. }
  374. $featured = get_post_meta($translated_id, '_wpml_media_featured', true);
  375. if ($featured === "") {
  376. // check the original state
  377. $featured = get_post_meta($source_id, '_wpml_media_featured', true);
  378. }
  379.  
  380. } else {
  381. // This is a new translation.
  382. $duplicate = get_post_meta($source_id, '_wpml_media_duplicate', true);
  383. $featured = get_post_meta($source_id, '_wpml_media_featured', true);
  384. }
  385.  
  386. if ($duplicate) {
  387. $checked = ' checked="checked"';
  388. }
  389. echo '<br /><label><input name="icl_duplicate_attachments" type="checkbox" value="1" '.$checked . '/>&nbsp;' . __('Duplicate uploaded media from original', 'wpml-media') . '</label>';
  390.  
  391. if ($featured) {
  392. $checked = ' checked="checked"';
  393. } else {
  394. $checked = '';
  395. }
  396. echo '<br /><label><input name="icl_duplicate_featured_image" type="checkbox" value="1" '.$checked . '/>&nbsp;' . __('Duplicate featured image from original', 'wpml-media') . '</label>';
  397. } else {
  398.  
  399. $duplicate = get_post_meta($source_id, '_wpml_media_duplicate', true);
  400. if ($duplicate) {
  401. $checked = ' checked="checked"';
  402. }
  403. echo '<br /><label><input name="icl_duplicate_attachments" type="checkbox" value="1" '.$checked . '/>&nbsp;' . __('Duplicate uploaded media to translations', 'wpml-media') . '</label>';
  404.  
  405. $featured = get_post_meta($source_id, '_wpml_media_featured', true);
  406. if ($featured) {
  407. $checked = ' checked="checked"';
  408. } else {
  409. $checked = '';
  410. }
  411. echo '<br /><label><input name="icl_duplicate_featured_image" type="checkbox" value="1" '.$checked . '/>&nbsp;' . __('Duplicate featured image to translations', 'wpml-media') . '</label>';
  412. }
  413. }
  414.  
  415. function manage_media_columns($posts_columns) {
  416. $posts_columns['language'] = __('Language', 'wpml-media');
  417. return $posts_columns;
  418. }
  419.  
  420. function manage_media_custom_column($column_name, $id) {
  421. if ($column_name == 'language') {
  422. global $wpdb, $sitepress;
  423. if(!empty($this->languages[$id])){
  424. echo $sitepress->get_display_language_name($this->languages[$id], $sitepress->get_admin_language());
  425. }else{
  426. echo __('None', 'wpml-media');
  427. }
  428. }
  429. }
  430.  
  431. //function manage_upload_sortable_columns($sortable_columns) {
  432. // $sortable_columns['language'] = 'language';
  433. // return $sortable_columns;
  434. //}
  435.  
  436. function parse_query($q){
  437. global $wp_query, $wpdb, $pagenow, $sitepress;
  438. if($pagenow == 'upload.php' || $pagenow == 'media-upload.php') {
  439.  
  440. $this->_get_lang_info();
  441.  
  442. }
  443. }
  444.  
  445. function _get_lang_info() {
  446. global $wpdb, $sitepress;
  447.  
  448. // get the attachment languages.
  449. $results = $wpdb->get_results("SELECT ID, post_parent FROM {$wpdb->posts} WHERE post_type='attachment'");
  450. $this->parents = array();
  451. $this->unattached = array();
  452. foreach ($results as $result) {
  453. $this->parents[$result->ID] = $result->post_parent;
  454. if (!$result->post_parent) {
  455. $this->unattached[] = $result->ID;
  456. }
  457. }
  458.  
  459. $results = $wpdb->get_results("SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key='wpml_media_lang'");
  460. $this->languages = array();
  461. foreach($results as $result) {
  462. $this->languages[$result->post_id] = $result->meta_value;
  463. }
  464.  
  465. // determine list of att without language set (with their parents)
  466. foreach($this->parents as $att_id => $parent_id) {
  467. if (!isset($this->languages[$att_id])) {
  468. $missing_langs[$att_id] = $parent_id;
  469. }
  470. }
  471. // get language of their parents
  472. if(!empty($missing_langs)){
  473. $results = $wpdb->get_results($wpdb->prepare("
  474. SELECT p.ID, t.language_code
  475. FROM {$wpdb->posts} p JOIN {$wpdb->prefix}icl_translations t ON p.ID = t.element_id AND t.element_type = CONCAT('post_', p.post_type)
  476. WHERE p.ID IN(".join(',', $missing_langs).")
  477. "));
  478. foreach($results as $row){
  479. $parent_langs[$row->ID] = $row->language_code;
  480. }
  481. }
  482.  
  483. // set language of their parents
  484. if(isset($parent_langs))
  485. foreach($this->parents as $att_id => $parent_id) {
  486. if (!isset($this->languages[$att_id])) {
  487. $this->languages[$att_id] = $parent_langs[$parent_id];
  488. }
  489. }
  490.  
  491. }
  492.  
  493. /**
  494. *Add a filter to fix the links for attachments in the language switcher so
  495. *they point to the corresponding pages in different languages.
  496. */
  497. function filter_link($url, $lang_info) {
  498. global $wp_query, $sitepress;
  499.  
  500. $current_lang = $sitepress->get_current_language();
  501. if ($wp_query->is_attachment && $lang_info['language_code'] != $current_lang) {
  502. $att_id = $wp_query->queried_object_id;
  503. // is this a duplicate of another attachment
  504. $translations = $this->_get_translations($att_id);
  505.  
  506. if (isset($translations[$lang_info['language_code']])) {
  507. $att_id = $translations[$lang_info['language_code']];
  508. }
  509. $link = get_attachment_link($att_id);
  510. $link = str_replace('?lang='.$current_lang, '', $link);
  511. $link = str_replace('&lang='.$current_lang, '', $link);
  512. $link = str_replace('&amp;lang='.$current_lang, '', $link);
  513. $link = str_replace('/'.$current_lang.'/', '/', $link);
  514. $url = $sitepress->convert_url($link, $lang_info['language_code']);
  515.  
  516. }
  517.  
  518. return $url;
  519. }
  520.  
  521. function _get_translations($att_id) {
  522. global $wpdb;
  523.  
  524. if ($this->languages == null) {
  525. $this->_get_lang_info();
  526. }
  527.  
  528. $duplicates = array();
  529. $duplicate_of = $wpdb->get_var("SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id={$att_id} AND meta_key='wpml_media_duplicate_of'");
  530. if ($duplicate_of) {
  531. $duplicates = $wpdb->get_col("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value={$duplicate_of} AND meta_key='wpml_media_duplicate_of'");
  532. $duplicates[] = $duplicate_of;
  533.  
  534. } else {
  535. // this might be an original
  536. $duplicates = $wpdb->get_col("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value={$att_id} AND meta_key='wpml_media_duplicate_of'");
  537. $duplicates[] = $att_id;
  538. }
  539.  
  540. $translations = array();
  541. foreach ($duplicates as $duplicate) {
  542. if (isset($this->languages[$duplicate])) {
  543. $translations[$this->languages[$duplicate]] = $duplicate;
  544. }
  545. }
  546.  
  547. return $translations;
  548. }
  549.  
  550. function icl_ls_languages($w_active_languages) {
  551. static $doing_it = false;
  552.  
  553. if(is_attachment() && !$doing_it){
  554. $doing_it = true;
  555. // Always include missing languages.
  556. $w_active_languages = icl_get_languages('skip_missing=0');
  557. $doing_it = false;
  558. }
  559.  
  560. return $w_active_languages;
  561. }
  562.  
  563. function posts_where_filter($where){
  564. global $wpdb, $pagenow, $sitepress;
  565.  
  566. if ($pagenow == 'upload.php' || $pagenow == 'media-upload.php') {
  567. $lang_code = '';
  568. if (isset($_GET['lang'])) {
  569. $lang_code = $_GET['lang'];
  570. } else {
  571. if (method_exists($sitepress, 'get_admin_language_cookie')) {
  572. $lang_code = $sitepress->get_admin_language_cookie();
  573. }
  574. }
  575.  
  576. if ($lang_code != "" && $lang_code != "all") {
  577.  
  578. $att_ids = array();
  579. foreach ($this->languages as $att_id => $lang) {
  580. if ($lang == $lang_code) {
  581. $att_ids[] = $att_id;
  582. }
  583. }
  584.  
  585. $att_ids = array_merge($att_ids, $this->unattached);
  586.  
  587. if (sizeof($att_ids) > 0) {
  588. $att_ids = '(' . implode(',', $att_ids) . ')';
  589.  
  590. $where .= " AND {$wpdb->posts}.ID in {$att_ids}";
  591. } else {
  592. // Add a where clause that wont return any matches.
  593. $where .= " AND {$wpdb->posts}.ID = -1";
  594. }
  595. }
  596.  
  597. }
  598. return $where;
  599. }
  600. function views_upload($views) {
  601.  
  602. return $views;
  603.  
  604. }
  605.  
  606. function get_post_metadata($value, $object_id, $meta_key, $single) {
  607. if ($meta_key == '_thumbnail_id') {
  608.  
  609. global $wpdb;
  610.  
  611. $thumbnail = $wpdb->get_var("SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = {$object_id} AND meta_key = '{$meta_key}'");
  612.  
  613. if ($thumbnail == null) {
  614. // see if it's available in the original language.
  615.  
  616. $post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID = $object_id");
  617. $trid = $wpdb->get_row("SELECT trid, source_language_code FROM {$wpdb->prefix}icl_translations WHERE element_id={$object_id} AND element_type = 'post_$post_type'");
  618. if ($trid) {
  619.  
  620. global $sitepress;
  621.  
  622. $translations = $sitepress->get_element_translations($trid->trid, 'post_' . $post_type);
  623. if (isset($translations[$trid->source_language_code])) {
  624. $translation = $translations[$trid->source_language_code];
  625. // see if the original has a thumbnail.
  626. $thumbnail = $wpdb->get_var("SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = {$translation->element_id} AND meta_key = '{$meta_key}'");
  627. if ($thumbnail) {
  628. $value = $thumbnail;
  629. }
  630. }
  631.  
  632. }
  633.  
  634. } else {
  635. $value = $thumbnail;
  636. }
  637.  
  638. }
  639. return $value;
  640. }
  641.  
  642. function menu(){
  643. $top_page = apply_filters('icl_menu_main_page', basename(ICL_PLUGIN_PATH).'/menu/languages.php');
  644. $this->settings['initial_message_shown'] = true;
  645. update_option('wpml_media_settings', $this->settings);
  646.  
  647. add_submenu_page($top_page,
  648. __('Media translation','wpml-media'),
  649. __('Media translation','wpml-media'), 'manage_options',
  650. 'wpml-media', array($this,'menu_content'));
  651. }
  652.  
  653. function menu_content(){
  654. global $wpdb;
  655.  
  656. $total_attachments = $wpdb->get_var("
  657. SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND ID NOT IN
  658. (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'wpml_media_processed')");
  659.  
  660.  
  661. include WPML_MEDIA_PATH . '/menu/management.php';
  662. }
  663.  
  664. function ajax_responses(){
  665. if(!isset($_POST['wpml_media_ajx_action'])){
  666. return;
  667. }
  668. global $wpdb;
  669.  
  670. $limit = 10;
  671.  
  672. switch($_POST['wpml_media_ajx_action']){
  673. case 'rescan_all':
  674. $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key='wpml_media_processed'");
  675. // Drop throught and do the rescan.
  676.  
  677. case 'rescan':
  678. $attachments = $wpdb->get_results("
  679. SELECT SQL_CALC_FOUND_ROWS p1.ID, p1.post_parent FROM {$wpdb->posts} p1 WHERE post_type = 'attachment' AND ID NOT IN
  680. (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'wpml_media_processed')
  681. ORDER BY p1.ID ASC LIMIT $limit
  682. ");
  683. if($attachments){
  684. $found = $wpdb->get_var("SELECT FOUND_ROWS()");
  685. foreach($attachments as $attachment){
  686. $this->create_duplicate_media($attachment);
  687. }
  688. echo $found >= $limit ? $found - $limit : 0;
  689. }else{
  690. echo -1;
  691. }
  692. break;
  693.  
  694. case 'featured_image_scan':
  695. global $sitepress;
  696.  
  697. $count = 0;
  698.  
  699. $featured_images = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE meta_key = '_thumbnail_id'");
  700. $thumbnails = array();
  701. foreach ($featured_images as $featured) {
  702. $thumbnails[$featured->post_id] = $featured->meta_value;
  703. }
  704.  
  705. if (sizeof($thumbnails)) {
  706. $ids = implode(', ', array_keys($thumbnails));
  707. $posts = $wpdb->get_results("SELECT ID, post_type FROM {$wpdb->posts} WHERE ID in ({$ids})");
  708. foreach ($posts as $post) {
  709. $row = $wpdb->get_row("SELECT trid, source_language_code FROM {$wpdb->prefix}icl_translations WHERE element_id={$post->ID} AND element_type = 'post_$post->post_type'");
  710. if ($row && $row->trid && ($row->source_language_code == null || $row->source_language_code == "")) {
  711.  
  712. $translations = $sitepress->get_element_translations($row->trid, 'post_' . $post->post_type);
  713. foreach ($translations as $translation) {
  714. if ($translation->element_id != $post->ID) {
  715. if (!in_array($translation->element_id, array_keys($thumbnails))) {
  716. // translation doesn't have a feature image
  717. $t_thumbnail_id = $wpdb->get_var( $wpdb->prepare(
  718. "
  719. SELECT post_id
  720. FROM $wpdb->postmeta
  721. WHERE meta_key = 'wpml_media_duplicate_of' AND meta_value = %s
  722. ",
  723. $thumbnails[$post->ID]
  724. ) );
  725. update_post_meta($translation->element_id, '_thumbnail_id', $t_thumbnail_id);
  726. $count += 1;
  727.  
  728. }
  729. }
  730. }
  731. }
  732.  
  733. }
  734. }
  735. echo $count . __(' Featured images duplicate to translated content', 'wpml-media');
  736. break;
  737. }
  738. exit;
  739. }
  740.  
  741. function create_duplicate_media($attachment) {
  742. static $parents_processed = array();
  743.  
  744. if ($attachment->post_parent && !in_array($attachment->post_parent, $parents_processed)) {
  745. global $wpdb, $sitepress;
  746.  
  747. // see if we have translations.
  748.  
  749. $post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID = $attachment->post_parent");
  750. $trid = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id={$attachment->post_parent} AND element_type = 'post_$post_type'");
  751. if ($trid) {
  752.  
  753. $attachments = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_parent = $attachment->post_parent");
  754.  
  755. $translations = $sitepress->get_element_translations($trid, 'post_' . $post_type);
  756. foreach ($translations as $translation) {
  757. if ($translation->element_id && $translation->element_id != $attachment->post_parent) {
  758.  
  759. $attachments_in_translation = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_parent = $translation->element_id");
  760. if (sizeof($attachments_in_translation) == 0) {
  761. // only duplicate attachments if there a none already.
  762. foreach ($attachments as $att_id) {
  763. // duplicate the attachement
  764.  
  765. $this->create_duplicate_attachment($att_id, $translation->element_id, $translation->language_code);
  766.  
  767. }
  768. }
  769. }
  770. }
  771. }
  772.  
  773. $parents_processed[] = $attachment->post_parent;
  774.  
  775. }
  776. update_post_meta($attachment->ID, 'wpml_media_processed', 1);
  777. }
  778.  
  779. function create_duplicate_attachment($att_id, $parent_id, $lang) {
  780. $post = get_post($att_id);
  781. $post->post_parent = $parent_id;
  782. $post->ID = NULL;
  783. $dup_att_id = wp_insert_post($post);
  784. // duplicate the post meta data.
  785. $meta = get_post_meta($att_id, '_wp_attachment_metadata', true);
  786. add_post_meta($dup_att_id, '_wp_attachment_metadata', $meta);
  787. update_post_meta($dup_att_id, 'wpml_media_processed', 1);
  788. update_post_meta($dup_att_id, 'wpml_media_lang', $lang);
  789. update_post_meta($dup_att_id, 'wpml_media_duplicate_of', $att_id);
  790.  
  791. update_post_meta($dup_att_id, '_wp_attached_file', get_attached_file($att_id) );
  792. }
  793.  
  794. function js_scripts(){
  795. global $pagenow;
  796. if ($pagenow == 'media.php') {
  797. ?>
  798. <script type="text/javascript">
  799. addLoadEvent(function(){
  800. jQuery('#icl_lang_options').insertBefore(jQuery('#post_id'));
  801. jQuery('#icl_lang_options').fadeIn();
  802. });
  803. </script>
  804.  
  805. <?php
  806. }
  807. if (isset($_GET['page']) && $_GET['page'] == 'wpml-media') {
  808. ?>
  809. <script type="text/javascript">
  810. addLoadEvent(function(){
  811. jQuery('#wpml_media_re_scan_but').click(wpml_media_re_scan);
  812. jQuery('#wpml_media_re_scan_all_but').click(wpml_media_re_scan_all);
  813. jQuery('#wpml_media_feature_image_but').click(wpml_media_feature_image_scan)
  814.  
  815. });
  816. var wpml_media_scan_started = false;
  817. var req_timer = 0;
  818. function wpml_media_toogle_scan(action){
  819. action = typeof(action) != 'undefined' ? action : 'rescan';
  820.  
  821. if(!wpml_media_scan_started){
  822. wpml_media_send_request(action);
  823. jQuery('#wpml_media_ajx_ldr_1').fadeIn();
  824. jQuery('#wpml_media_re_scan_but').attr('value','<?php echo icl_js_escape(__('Running', 'wpml-media')) ?>');
  825. jQuery('#wpml_media_re_scan_all_but').attr('value','<?php echo icl_js_escape(__('Running', 'wpml-media')) ?>');
  826. }else{
  827. jQuery('#wpml_media_re_scan_but').attr('value','<?php echo icl_js_escape(__('Scan and duplicate attachments', 'wpml-media')); ?>');
  828. jQuery('#wpml_media_re_scan_all_but').attr('value','<?php echo icl_js_escape(__('Scan All', 'wpml-media')); ?>');
  829. window.clearTimeout(req_timer);
  830. jQuery('#wpml_media_ajx_ldr_1').fadeOut();
  831. location.reload();
  832. }
  833. wpml_media_scan_started = !wpml_media_scan_started;
  834. return false;
  835. }
  836.  
  837. function wpml_media_send_request(action){
  838. jQuery.ajax({
  839. type: "POST",
  840. url: "<?php echo htmlentities($_SERVER['REQUEST_URI']) ?>",
  841. data: "wpml_media_ajx_action=" + action,
  842. success: function(msg){
  843. if(-1==msg || msg==0){
  844. left = '0';
  845. wpml_media_toogle_scan();
  846. }else{
  847. left=msg;
  848. }
  849.  
  850.  
  851. jQuery('#wpml_media_re_scan_toscan').html(left);
  852. if(wpml_media_scan_started){
  853. req_timer = window.setTimeout('wpml_media_send_request("rescan")', 1000);
  854. }
  855. }
  856. });
  857.  
  858. }
  859. function wpml_media_re_scan(){
  860. wpml_media_toogle_scan("rescan");
  861. }
  862. function wpml_media_re_scan_all(){
  863. wpml_media_toogle_scan("rescan_all");
  864. }
  865.  
  866. function wpml_media_feature_image_scan(){
  867. jQuery('#wpml_media_result').fadeOut();
  868. jQuery('#wpml_media_ajx_ldr_2').fadeIn();
  869. jQuery('#wpml_media_feature_image_but').attr('value','<?php echo icl_js_escape(__('Running', 'wpml-media')) ?>');
  870. jQuery.ajax({
  871. type: "POST",
  872. url: "<?php echo htmlentities($_SERVER['REQUEST_URI']) ?>",
  873. data: "wpml_media_ajx_action=featured_image_scan",
  874. success: function(msg){
  875. jQuery('#wpml_media_ajx_ldr_2').fadeOut();
  876. jQuery('#wpml_media_feature_image_but').attr('value','<?php echo icl_js_escape(__('Scan and duplicate featured images', 'wpml-media')) ?>');
  877. jQuery('#wpml_media_result').html(msg);
  878. jQuery('#wpml_media_result').fadeIn();
  879. }
  880. });
  881. }
  882.  
  883. </script>
  884. <?php
  885. }
  886. }
  887.  
  888.  
  889.  
  890. function _initialize_message(){
  891. $url = rtrim(get_option('siteurl'),'/') . '/wp-admin/admin.php?page=wpml-media';
  892. ?>
  893. <div class="message updated"><p><?php printf(__('WPML Media needs to be set up. <a href="%s">Setup translation of media files</a>', 'wpml-media'),
  894. $url); ?></p></div>
  895. <?php
  896. }
  897.  
  898. function _no_wpml_warning(){
  899. ?>
  900. <div class="message error"><p><?php printf(__('WPML Media is enabled but not effective. It requires <a href="%s">WPML</a> in order to work.', 'wpml-translation-management'),
  901. 'http://wpml.org/'); ?></p></div>
  902. <?php
  903. }
  904.  
  905. function _old_wpml_warning(){
  906. ?>
  907. <div class="message error"><p><?php printf(__('WPML Media is enabled but not effective. It is not compatible with <a href="%s">WPML</a> versions prior 2.0.5.', 'wpml-translation-management'),
  908. 'http://wpml.org/'); ?></p></div>
  909. <?php
  910. }
  911. // Localization
  912. function plugin_localization(){
  913. load_plugin_textdomain( 'wpml-media', false, WPML_MEDIA_FOLDER . '/locale');
  914. }
  915.  
  916. function language_filter(){
  917. global $sitepress;
  918.  
  919. if (isset($_GET['lang'])) {
  920. $lang_code = $_GET['lang'];
  921. } else {
  922. if (method_exists($sitepress, 'get_admin_language_cookie')) {
  923. $lang_code = $sitepress->get_admin_language_cookie();
  924. }
  925. }
  926.  
  927. $active_languages = $sitepress->get_active_languages();
  928.  
  929.  
  930. $active_languages[] = array('code'=>'all','display_name'=>__('All languages','sitepress'));
  931. foreach($active_languages as $lang){
  932. if($lang['code'] == $lang_code){
  933. $px = '<strong>';
  934. $sx = ' <span class="count">('. $lang['code'] .')<\/span><\/strong>';
  935. } else {
  936. $px = '<a href="' . $_SERVER['REQUEST_URI'] . '&lang=' . $lang['code']. '">';
  937. $sx = '<\/a> <span class="count">('. $lang['code'] .')<\/span>';
  938. }
  939. $as[] = $px . $lang['display_name'] . $sx;
  940. }
  941.  
  942. $allas = join(' | ', $as);
  943.  
  944. $prot_link = '';
  945. ?>
  946. <script type="text/javascript">
  947. jQuery(".subsubsub").append('<br /><span id="icl_subsubsub"><?php echo $allas ?><\/span><br /><?php echo $prot_link ?>');
  948. </script>
  949. <?php
  950. }
  951.  
  952.  
  953. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement