Advertisement
sarahn

wpml_media.class.php

Feb 11th, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. //delete file filter
  58. add_filter('wp_delete_file',array($this,'delete_file'));
  59.  
  60.  
  61. if($pagenow == 'media-upload.php'){
  62. add_action('media_upload_library', array($this,'language_filter'), 99);
  63. }
  64.  
  65. if($pagenow == 'media.php') {
  66. add_action('admin_footer', array($this,'media_language_options'));
  67. }
  68. if($pagenow == 'upload.php') {
  69. //add language filter
  70. add_action('admin_footer', array($this,'language_filter_upload_page'));
  71. }
  72.  
  73. }
  74.  
  75. $this->ajax_responses();
  76.  
  77. }
  78.  
  79. //add_filter('get_post_metadata', array($this, 'get_post_metadata'), 10, 4);
  80. add_filter('WPML_filter_link', array($this, 'filter_link'), 10, 2);
  81. add_filter('icl_ls_languages', array($this, 'icl_ls_languages'), 10, 1);
  82. add_action('icl_pro_translation_saved', array($this, 'icl_pro_translation_saved'), 10, 1);
  83.  
  84. }
  85.  
  86. function media_language_options() {
  87. global $sitepress;
  88.  
  89. $att_id = $_GET['attachment_id'];
  90. $translations = $this->_get_translations($att_id);
  91. $current_lang = '';
  92. foreach($translations as $lang => $id) {
  93. if ($id == $att_id) {
  94. $current_lang = $lang;
  95. unset($translations[$lang]);
  96. break;
  97. }
  98. }
  99.  
  100. $active_languages = icl_get_languages('orderby=id&order=asc&skip_missing=0');
  101. $lang_links = '';
  102.  
  103. if ($current_lang) {
  104.  
  105. $lang_links = '<strong>' . $active_languages[$current_lang]['native_name'] . '</strong>';
  106.  
  107. }
  108.  
  109. foreach ($translations as $lang => $id) {
  110. $lang_links .= ' | <a href="' . admin_url('media.php?attachment_id=' . $id . '&action=edit') . '">' . $active_languages[$lang]['native_name'] . '</a>';
  111. }
  112.  
  113.  
  114.  
  115. echo '<div id="icl_lang_options" style="display:none">' . $lang_links . '</div>';
  116. }
  117.  
  118. function icl_pro_translation_saved($new_post_id) {
  119. global $wpdb;
  120.  
  121. $post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID = " . $new_post_id);
  122. $trid = $_POST['trid'];
  123. $lang = $_POST['lang'];
  124.  
  125. $source_lang = $wpdb->get_var("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE trid={$trid} AND source_language_code IS NULL");
  126.  
  127. $this->duplicate_post_attachments($new_post_id, $trid, $source_lang, $lang);
  128. }
  129.  
  130. function save_post_actions($pidd, $post){
  131. global $wpdb, $sitepress;
  132.  
  133. list($post_type, $post_status) = $wpdb->get_row("SELECT post_type, post_status FROM {$wpdb->posts} WHERE ID = " . $pidd, ARRAY_N);
  134.  
  135. //checking - if translation and not saved before
  136. if (isset($_GET['trid']) && !empty($_GET['trid']) && $post_status == 'auto-draft') {
  137.  
  138. //get source language
  139. if (isset($_GET['source_lang']) && !empty($_GET['source_lang'])) {
  140. $src_lang = $_GET['source_lang'];
  141. } else {
  142. $src_lang = $sitepress->get_default_language();
  143. }
  144.  
  145. //get source id
  146. $src_id = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid={$_GET['trid']} AND language_code='{$src_lang}'");
  147.  
  148.  
  149. //delete exist auto-draft post media
  150. $results = $wpdb->get_results("SELECT p.id FROM {$wpdb->posts} AS p LEFT JOIN {$wpdb->posts} AS p1 ON p.post_parent = p1.id WHERE p1.post_status = 'auto-draft'", ARRAY_A);
  151. $attachments = array();
  152. if (!empty($results)) {
  153. foreach ($results as $result) {
  154. $attachments[] = $result["id"];
  155. }
  156. if (!empty($attachments)) {
  157. $wpdb->query("DELETE FROM {$wpdb->posts} WHERE id IN (" . join(',', $attachments) . ")");
  158. $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id IN (" . join(',', $attachments) . ")");
  159. }
  160. }
  161.  
  162. //checking - if set dublicate media
  163. if (get_post_meta($src_id, '_wpml_media_duplicate', true)){
  164. //dublicate media before first save
  165. $this->duplicate_post_attachments($pidd, $_GET['trid'], $src_lang, $sitepress->get_language_for_element($pidd, 'post_' . $post_type));
  166. }
  167. }
  168.  
  169. // exceptions
  170. if(
  171. !$sitepress->is_translated_post_type($post_type)
  172. || isset($_POST['autosave'])
  173. || (isset($_POST['post_ID']) && $_POST['post_ID']!=$pidd) || (isset($_POST['post_type']) && $_POST['post_type']=='revision')
  174. || $post_type == 'revision'
  175. || get_post_meta($pidd, '_wp_trash_meta_status', true)
  176. || ( isset($_GET['action']) && $_GET['action']=='restore')
  177. || $post_status == 'auto-draft'
  178. ){
  179. return;
  180. }
  181.  
  182. if (isset($_POST['icl_trid'])) {
  183. // save the post from the edit screen.
  184. if (isset($_POST['icl_duplicate_attachments'])) {
  185. update_post_meta($pidd, '_wpml_media_duplicate', intval($_POST['icl_duplicate_attachments']));
  186. } else {
  187. update_post_meta($pidd, '_wpml_media_duplicate', "0");
  188. }
  189.  
  190. if (isset($_POST['icl_duplicate_featured_image'])) {
  191. update_post_meta($pidd, '_wpml_media_featured', intval($_POST['icl_duplicate_featured_image']));
  192. } else {
  193. update_post_meta($pidd, '_wpml_media_featured', "0");
  194. }
  195.  
  196. $icl_trid = $_POST['icl_trid'];
  197. } else {
  198. // get trid from database.
  199. $icl_trid = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id={$pidd} AND element_type = 'post_$post_type'");
  200. }
  201.  
  202. if ($icl_trid) {
  203. $this->duplicate_post_attachments($pidd, $icl_trid);
  204. }
  205. }
  206.  
  207. function save_attachment_actions($post_id){
  208. global $wpdb, $sitepress;
  209.  
  210. $wpml_media_lang = get_post_meta($post_id, 'wpml_media_lang', true);
  211.  
  212. if(empty($wpml_media_lang)){
  213. $parent_post = $wpdb->get_row($wpdb->prepare(
  214. "SELECT p2.ID, p2.post_type FROM $wpdb->posts p1 JOIN $wpdb->posts p2 ON p1.post_parent =p2.ID WHERE p1.ID=%d"
  215. , $post_id));
  216.  
  217. if($parent_post){
  218. $wpml_media_lang = $sitepress->get_language_for_element($parent_post->ID, 'post_' . $parent_post->post_type);
  219. }
  220.  
  221. if(empty($wpml_media_lang)){
  222. $wpml_media_lang = $sitepress->get_admin_language_cookie();
  223. }
  224. if(empty($wpml_media_lang)){
  225. $wpml_media_lang = $sitepress->get_default_language();
  226. }
  227.  
  228. }
  229.  
  230. if(!empty($wpml_media_lang)){
  231. update_post_meta($post_id, 'wpml_media_lang', $wpml_media_lang);
  232. }
  233.  
  234. }
  235.  
  236. function duplicate_post_attachments($pidd, $icl_trid, $source_lang = null, $lang = null) {
  237. global $wpdb, $sitepress;
  238. if ($icl_trid == "") {
  239. return;
  240. }
  241.  
  242. if (!$source_lang) {
  243. $source_lang = $wpdb->get_var("SELECT source_language_code FROM {$wpdb->prefix}icl_translations WHERE element_id = $pidd AND trid = $icl_trid");
  244. }
  245.  
  246. if ($source_lang == null || $source_lang == "") {
  247. // This is the original see if we should copy to translations
  248.  
  249. $duplicate = get_post_meta($pidd, '_wpml_media_duplicate', true);
  250. $featured = get_post_meta($pidd, '_wpml_media_featured', true);
  251. if ($duplicate || $featured) {
  252. $translations = $wpdb->get_col("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid = $icl_trid");
  253.  
  254. foreach ($translations as $translation_id) {
  255. if ($translation_id && $translation_id != $pidd) {
  256. $duplicate_t = $duplicate;
  257. if ($duplicate_t) {
  258. // See if the translation is marked for duplication
  259. $duplicate_t = get_post_meta($translation_id, '_wpml_media_duplicate', true);
  260. }
  261.  
  262. $lang = $wpdb->get_var("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id = $translation_id AND trid = $icl_trid");
  263. if ($duplicate_t || $duplicate_t == '') {
  264. $source_attachments = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_parent = $pidd AND post_type = 'attachment'");
  265. $attachments = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_parent = $translation_id AND post_type = 'attachment'");
  266.  
  267. foreach ($source_attachments as $source_att_id) {
  268. $found = false;
  269. foreach($attachments as $att_id) {
  270. $duplicate_of = get_post_meta($att_id, 'wpml_media_duplicate_of', true);
  271. if ($duplicate_of == $source_att_id) {
  272. $found = true;
  273. }
  274. }
  275.  
  276. if (!$found) {
  277. $this->create_duplicate_attachment($source_att_id, $translation_id, $lang);
  278. }
  279. }
  280. }
  281.  
  282. $featured_t = $featured;
  283. if ($featured_t) {
  284. // See if the translation is marked for duplication
  285. $featured_t = get_post_meta($translation_id, '_wpml_media_featured', true);
  286. }
  287. if ($featured_t || $featured_t == '') {
  288. $thumbnail_id = get_post_meta($pidd, '_thumbnail_id', true);
  289. if ($thumbnail_id) {
  290. $t_thumbnail_id = $wpdb->get_var( $wpdb->prepare(
  291. "
  292. SELECT pm.post_id
  293. FROM $wpdb->postmeta AS pm
  294. INNER JOIN $wpdb->posts AS p
  295. ON pm.post_id = p.ID
  296. WHERE
  297. pm.meta_key = 'wpml_media_duplicate_of'
  298. AND pm.meta_value = %s
  299. AND p.post_parent = %s
  300. AND p.post_type = 'attachment'
  301. ",
  302. $thumbnail_id,
  303. $translation_id
  304. ) );
  305. update_post_meta($translation_id, '_thumbnail_id', $t_thumbnail_id);
  306. }
  307. }
  308. }
  309. }
  310. }
  311.  
  312. } else {
  313. // This is a translation.
  314.  
  315. $source_id = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE language_code = '$source_lang' AND trid = $icl_trid");
  316.  
  317. if (!$lang) {
  318. $lang = $wpdb->get_var("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id = $pidd AND trid = $icl_trid");
  319. }
  320.  
  321. $duplicate = get_post_meta($pidd, '_wpml_media_duplicate', true);
  322. if ($duplicate === "") {
  323. // check the original state
  324. $duplicate = get_post_meta($source_id, '_wpml_media_duplicate', true);
  325. }
  326.  
  327. if ($duplicate) {
  328. $attachments = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_parent = $pidd AND post_type = 'attachment'");
  329. $source_attachments = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_parent = $source_id AND post_type = 'attachment'");
  330.  
  331. foreach ($source_attachments as $source_att_id) {
  332. $found = false;
  333. foreach($attachments as $att_id) {
  334. $duplicate_of = get_post_meta($att_id, 'wpml_media_duplicate_of', true);
  335. if ($duplicate_of == $source_att_id) {
  336. $found = true;
  337. }
  338. }
  339.  
  340. if (!$found) {
  341. $this->create_duplicate_attachment($source_att_id, $pidd, $lang);
  342. }
  343.  
  344. }
  345. }
  346.  
  347. $featured = get_post_meta($pidd, '_wpml_media_featured', true);
  348. if ($featured === "") {
  349. // check the original state
  350. $featured = get_post_meta($source_id, '_wpml_media_featured', true);
  351. }
  352.  
  353. if ($featured) {
  354. $thumbnail_id = get_post_meta($source_id, '_thumbnail_id', true);
  355. if ($thumbnail_id) {
  356. $t_thumbnail_id = $wpdb->get_var( $wpdb->prepare(
  357. "
  358. SELECT pm.post_id
  359. FROM $wpdb->postmeta AS pm
  360. INNER JOIN $wpdb->posts AS p
  361. ON pm.post_id = p.ID
  362. WHERE
  363. pm.meta_key = 'wpml_media_duplicate_of'
  364. AND pm.meta_value = %s
  365. AND p.post_parent = %s
  366. AND p.post_type = 'attachment'
  367. ",
  368. $thumbnail_id,
  369. $pidd
  370. ) );
  371. update_post_meta($pidd, '_thumbnail_id', $t_thumbnail_id);
  372. }
  373.  
  374. }
  375.  
  376. }
  377.  
  378. }
  379.  
  380. function language_options() {
  381. global $icl_meta_box_globals, $wpdb;
  382.  
  383. $translation = false;
  384. $source_id = null;
  385. $translated_id = null;
  386. if (sizeof($icl_meta_box_globals['translations']) > 0) {
  387. if (!isset($icl_meta_box_globals['translations'][$icl_meta_box_globals['selected_language']])) {
  388. // We are creating a new translation
  389. $translation = true;
  390. // find the original
  391. foreach ($icl_meta_box_globals['translations'] as $trans_data) {
  392. if ($trans_data->original == '1') {
  393. $source_id = $trans_data->element_id;
  394. break;
  395. }
  396. }
  397. } else {
  398. $trans_data = $icl_meta_box_globals['translations'][$icl_meta_box_globals['selected_language']];
  399. // see if this is an original or a translation.
  400. if ($trans_data->original == '0') {
  401. // double check that it's not the original
  402. // This is because the source_language_code field in icl_translations is not always being set to null.
  403.  
  404. $source_language_code = $wpdb->get_var("SELECT source_language_code FROM {$wpdb->prefix}icl_translations WHERE translation_id = $trans_data->translation_id");
  405. $translation = !($source_language_code == "" || $source_language_code == null);
  406. if ($translation) {
  407. $source_id = $icl_meta_box_globals['translations'][$source_language_code]->element_id;
  408. $translated_id = $trans_data->element_id;
  409. } else {
  410. $source_id = $trans_data->element_id;
  411. }
  412. } else {
  413. $source_id = $trans_data->element_id;
  414. }
  415. }
  416. }
  417.  
  418. echo '<br /><br /><strong>' . __('Media attachments', 'wpml-media') . '</strong>';
  419.  
  420. $checked = '';
  421. if ($translation) {
  422. if ($translated_id) {
  423. $duplicate = get_post_meta($translated_id, '_wpml_media_duplicate', true);
  424. if ($duplicate === "") {
  425. // check the original state
  426. $duplicate = get_post_meta($source_id, '_wpml_media_duplicate', true);
  427. }
  428. $featured = get_post_meta($translated_id, '_wpml_media_featured', true);
  429. if ($featured === "") {
  430. // check the original state
  431. $featured = get_post_meta($source_id, '_wpml_media_featured', true);
  432. }
  433.  
  434. } else {
  435. // This is a new translation.
  436. $duplicate = get_post_meta($source_id, '_wpml_media_duplicate', true);
  437. $featured = get_post_meta($source_id, '_wpml_media_featured', true);
  438. }
  439.  
  440. if ($duplicate) {
  441. $checked = ' checked="checked"';
  442. }
  443. echo '<br /><label><input name="icl_duplicate_attachments" type="checkbox" value="1" '.$checked . '/>&nbsp;' . __('Duplicate uploaded media from original', 'wpml-media') . '</label>';
  444.  
  445. if ($featured) {
  446. $checked = ' checked="checked"';
  447. } else {
  448. $checked = '';
  449. }
  450. echo '<br /><label><input name="icl_duplicate_featured_image" type="checkbox" value="1" '.$checked . '/>&nbsp;' . __('Duplicate featured image from original', 'wpml-media') . '</label>';
  451. } else {
  452.  
  453. $duplicate = get_post_meta($source_id, '_wpml_media_duplicate', true);
  454. if ($duplicate) {
  455. $checked = ' checked="checked"';
  456. }
  457. echo '<br /><label><input name="icl_duplicate_attachments" type="checkbox" value="1" '.$checked . '/>&nbsp;' . __('Duplicate uploaded media to translations', 'wpml-media') . '</label>';
  458.  
  459. $featured = get_post_meta($source_id, '_wpml_media_featured', true);
  460. if ($featured) {
  461. $checked = ' checked="checked"';
  462. } else {
  463. $checked = '';
  464. }
  465. echo '<br /><label><input name="icl_duplicate_featured_image" type="checkbox" value="1" '.$checked . '/>&nbsp;' . __('Duplicate featured image to translations', 'wpml-media') . '</label>';
  466. }
  467. }
  468.  
  469. function manage_media_columns($posts_columns) {
  470. $posts_columns['language'] = __('Language', 'wpml-media');
  471. return $posts_columns;
  472. }
  473.  
  474. function manage_media_custom_column($column_name, $id) {
  475. if ($column_name == 'language') {
  476. global $wpdb, $sitepress;
  477. if(!empty($this->languages[$id])){
  478. echo $sitepress->get_display_language_name($this->languages[$id], $sitepress->get_admin_language());
  479. }else{
  480. echo __('None', 'wpml-media');
  481. }
  482. }
  483. }
  484.  
  485. //function manage_upload_sortable_columns($sortable_columns) {
  486. // $sortable_columns['language'] = 'language';
  487. // return $sortable_columns;
  488. //}
  489.  
  490. function parse_query($q){
  491. global $wp_query, $wpdb, $pagenow, $sitepress;
  492. if($pagenow == 'upload.php' || $pagenow == 'media-upload.php' || (isset($_POST['action']) && $_POST['action']=='query-attachments')) {
  493.  
  494. $this->_get_lang_info();
  495.  
  496. }
  497. }
  498.  
  499. function _get_lang_info() {
  500. global $wpdb, $sitepress;
  501.  
  502. // get the attachment languages.
  503. //if query-attachments need display all attachments
  504. if ((isset($_POST['action']) && $_POST['action'] == 'query-attachments')) {
  505. $results = $wpdb->get_results("SELECT ID, post_parent FROM {$wpdb->posts} WHERE post_type='attachment'");
  506. } else {
  507. //don't display attachments auto-draft posts
  508. $results = $wpdb->get_results("SELECT p.ID, p.post_parent FROM {$wpdb->posts} AS p LEFT JOIN {$wpdb->posts} AS p1 ON p.post_parent = p1.id WHERE p1.post_status <> 'auto-draft' AND p.post_type='attachment'");
  509. }
  510. $this->parents = array();
  511. $this->unattached = array();
  512. foreach ($results as $result) {
  513. $this->parents[$result->ID] = $result->post_parent;
  514. if (!$result->post_parent) {
  515. $this->unattached[] = $result->ID;
  516. }
  517. }
  518. if ((isset($_POST['action']) && $_POST['action'] == 'query-attachments')) {
  519. //don't display attachments auto-draft posts
  520. $results = $wpdb->get_results("SELECT post_id, meta_value FROM {$wpdb->postmeta} AS pm LEFT JOIN {$wpdb->posts} AS p ON pm.post_id = p.id LEFT JOIN {$wpdb->posts} AS p1 ON p.post_parent = p1.id WHERE p1.post_status <> 'auto-draft' AND pm.meta_key='wpml_media_lang'");
  521. } else {
  522. $results = $wpdb->get_results("SELECT post_id, meta_value FROM {$wpdb->postmeta} AS pm LEFT JOIN {$wpdb->posts} AS p ON pm.post_id = p.id WHERE pm.meta_key='wpml_media_lang'");
  523. }
  524.  
  525.  
  526.  
  527. $this->languages = array();
  528. foreach ($results as $result) {
  529. $this->languages[$result->post_id] = $result->meta_value;
  530. }
  531.  
  532. // determine list of att without language set (with their parents)
  533. foreach($this->parents as $att_id => $parent_id) {
  534. if (!isset($this->languages[$att_id])) {
  535. $missing_langs[$att_id] = $parent_id;
  536. }
  537. }
  538. // get language of their parents
  539. if(!empty($missing_langs)){
  540. $results = $wpdb->get_results("
  541. SELECT p.ID, t.language_code
  542. 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)
  543. WHERE p.ID IN(".join(',', $missing_langs).")
  544. ");
  545. foreach($results as $row){
  546. $parent_langs[$row->ID] = $row->language_code;
  547. }
  548. }
  549.  
  550. // set language of their parents
  551. if(isset($parent_langs))
  552. foreach($this->parents as $att_id => $parent_id) {
  553. if (!isset($this->languages[$att_id])) {
  554. $this->languages[$att_id] = $parent_langs[$parent_id];
  555. }
  556. }
  557.  
  558. }
  559.  
  560. /**
  561. *Add a filter to fix the links for attachments in the language switcher so
  562. *they point to the corresponding pages in different languages.
  563. */
  564. function filter_link($url, $lang_info) {
  565. global $wp_query, $sitepress;
  566.  
  567. $current_lang = $sitepress->get_current_language();
  568. if ($wp_query->is_attachment && $lang_info['language_code'] != $current_lang) {
  569. $att_id = $wp_query->queried_object_id;
  570. // is this a duplicate of another attachment
  571. $translations = $this->_get_translations($att_id);
  572.  
  573. if (isset($translations[$lang_info['language_code']])) {
  574. $att_id = $translations[$lang_info['language_code']];
  575. }
  576. $link = get_attachment_link($att_id);
  577. $link = str_replace('?lang='.$current_lang, '', $link);
  578. $link = str_replace('&lang='.$current_lang, '', $link);
  579. $link = str_replace('&amp;lang='.$current_lang, '', $link);
  580. $link = str_replace('/'.$current_lang.'/', '/', $link);
  581. $url = $sitepress->convert_url($link, $lang_info['language_code']);
  582.  
  583. }
  584.  
  585. return $url;
  586. }
  587.  
  588. function _get_translations($att_id) {
  589. global $wpdb;
  590.  
  591. if ($this->languages == null) {
  592. $this->_get_lang_info();
  593. }
  594.  
  595. $duplicates = array();
  596. $duplicate_of = $wpdb->get_var("SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id={$att_id} AND meta_key='wpml_media_duplicate_of'");
  597. if ($duplicate_of) {
  598. $duplicates = $wpdb->get_col("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value={$duplicate_of} AND meta_key='wpml_media_duplicate_of'");
  599. $duplicates[] = $duplicate_of;
  600.  
  601. } else {
  602. // this might be an original
  603. $duplicates = $wpdb->get_col("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value={$att_id} AND meta_key='wpml_media_duplicate_of'");
  604. $duplicates[] = $att_id;
  605. }
  606.  
  607. $translations = array();
  608. foreach ($duplicates as $duplicate) {
  609. if (isset($this->languages[$duplicate])) {
  610. $translations[$this->languages[$duplicate]] = $duplicate;
  611. }
  612. }
  613.  
  614. return $translations;
  615. }
  616.  
  617. function icl_ls_languages($w_active_languages) {
  618. static $doing_it = false;
  619.  
  620. if(is_attachment() && !$doing_it){
  621. $doing_it = true;
  622. // Always include missing languages.
  623. $w_active_languages = icl_get_languages('skip_missing=0');
  624. $doing_it = false;
  625. }
  626.  
  627. return $w_active_languages;
  628. }
  629.  
  630. function posts_where_filter($where){
  631. global $wpdb, $pagenow, $sitepress;
  632.  
  633. if ($pagenow == 'upload.php' || $pagenow == 'media-upload.php' || (isset($_POST['action']) && $_POST['action']=='query-attachments')) {
  634. $lang_code = '';
  635. if (isset($_GET['lang'])) {
  636. $lang_code = $_GET['lang'];
  637. } else {
  638. if (method_exists($sitepress, 'get_admin_language_cookie')) {
  639. $lang_code = $sitepress->get_admin_language_cookie();
  640.  
  641. //get lang_code from post
  642. if (isset($_POST['action']) && $_POST['action'] == 'query-attachments') {
  643. $lang_code = $sitepress->get_language_for_element($_POST['post_id'], 'post_' . get_post_type($_POST['post_id']));
  644. }
  645.  
  646.  
  647. if(empty($lang_code)){
  648. $lang_code = $sitepress->get_current_language();
  649. }
  650. }
  651. }
  652.  
  653.  
  654. //if choose "display to this post" not need add "_posts.id in (ids)" in query where
  655. if ($lang_code != "" && $lang_code != "all" && !isset($_POST['query']['post_parent'])) {
  656.  
  657. $att_ids = array();
  658. foreach ($this->languages as $att_id => $lang) {
  659. if ($lang == $lang_code) {
  660. $att_ids[] = $att_id;
  661. }
  662. }
  663.  
  664. $att_ids = array_merge($att_ids, $this->unattached);
  665.  
  666. if (sizeof($att_ids) > 0) {
  667. $att_ids = '(' . implode(',', $att_ids) . ')';
  668.  
  669. $where .= " AND {$wpdb->posts}.ID in {$att_ids}";
  670. } else {
  671. // Add a where clause that wont return any matches.
  672. $where .= " AND {$wpdb->posts}.ID = -1";
  673. }
  674. }
  675.  
  676. }
  677. return $where;
  678. }
  679.  
  680.  
  681. function get_post_metadata($value, $object_id, $meta_key, $single) {
  682. if ($meta_key == '_thumbnail_id') {
  683.  
  684. global $wpdb;
  685.  
  686. $thumbnail = $wpdb->get_var("SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = {$object_id} AND meta_key = '{$meta_key}'");
  687.  
  688. if ($thumbnail == null) {
  689. // see if it's available in the original language.
  690.  
  691. $post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID = $object_id");
  692. $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'");
  693. if ($trid) {
  694.  
  695. global $sitepress;
  696.  
  697. $translations = $sitepress->get_element_translations($trid->trid, 'post_' . $post_type);
  698. if (isset($translations[$trid->source_language_code])) {
  699. $translation = $translations[$trid->source_language_code];
  700. // see if the original has a thumbnail.
  701. $thumbnail = $wpdb->get_var("SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = {$translation->element_id} AND meta_key = '{$meta_key}'");
  702. if ($thumbnail) {
  703. $value = $thumbnail;
  704. }
  705. }
  706.  
  707. }
  708.  
  709. } else {
  710. $value = $thumbnail;
  711. }
  712.  
  713. }
  714. return $value;
  715. }
  716.  
  717. function menu(){
  718. $top_page = apply_filters('icl_menu_main_page', basename(ICL_PLUGIN_PATH).'/menu/languages.php');
  719. $this->settings['initial_message_shown'] = true;
  720. update_option('wpml_media_settings', $this->settings);
  721.  
  722. add_submenu_page($top_page,
  723. __('Media translation','wpml-media'),
  724. __('Media translation','wpml-media'), 'manage_options',
  725. 'wpml-media', array($this,'menu_content'));
  726. }
  727.  
  728. function menu_content(){
  729. global $wpdb;
  730.  
  731. $total_attachments = $wpdb->get_var("
  732. SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND ID NOT IN
  733. (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'wpml_media_processed')");
  734.  
  735.  
  736. include WPML_MEDIA_PATH . '/menu/management.php';
  737. }
  738.  
  739. function ajax_responses(){
  740. if(!isset($_POST['wpml_media_ajx_action'])){
  741. return;
  742. }
  743. global $wpdb;
  744.  
  745. $limit = 10;
  746.  
  747. switch($_POST['wpml_media_ajx_action']){
  748. case 'rescan_all':
  749. $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key='wpml_media_processed'");
  750. // Drop throught and do the rescan.
  751.  
  752. case 'rescan':
  753. $attachments = $wpdb->get_results("
  754. SELECT SQL_CALC_FOUND_ROWS p1.ID, p1.post_parent FROM {$wpdb->posts} p1 WHERE post_type = 'attachment' AND ID NOT IN
  755. (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'wpml_media_processed')
  756. ORDER BY p1.ID ASC LIMIT $limit
  757. ");
  758. if($attachments){
  759. $found = $wpdb->get_var("SELECT FOUND_ROWS()");
  760. foreach($attachments as $attachment){
  761. $this->create_duplicate_media($attachment);
  762. }
  763. echo $found >= $limit ? $found - $limit : 0;
  764. }else{
  765. echo -1;
  766. }
  767. break;
  768.  
  769. case 'featured_image_scan':
  770. global $sitepress;
  771.  
  772. $count = 0;
  773.  
  774. $featured_images = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE meta_key = '_thumbnail_id'");
  775. $thumbnails = array();
  776. foreach ($featured_images as $featured) {
  777. $thumbnails[$featured->post_id] = $featured->meta_value;
  778. }
  779.  
  780. if (sizeof($thumbnails)) {
  781. $ids = implode(', ', array_keys($thumbnails));
  782. $posts = $wpdb->get_results("SELECT ID, post_type FROM {$wpdb->posts} WHERE ID in ({$ids})");
  783. foreach ($posts as $post) {
  784. $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'");
  785. if ($row && $row->trid && ($row->source_language_code == null || $row->source_language_code == "")) {
  786.  
  787. $translations = $sitepress->get_element_translations($row->trid, 'post_' . $post->post_type);
  788. foreach ($translations as $translation) {
  789. if ($translation->element_id != $post->ID) {
  790. if (!in_array($translation->element_id, array_keys($thumbnails))) {
  791. // translation doesn't have a feature image
  792. $t_thumbnail_id = $wpdb->get_var( $wpdb->prepare(
  793. "
  794. SELECT pm.post_id
  795. FROM $wpdb->postmeta AS pm
  796. INNER JOIN $wpdb->posts AS p
  797. ON pm.post_id = p.ID
  798. WHERE
  799. pm.meta_key = 'wpml_media_duplicate_of'
  800. AND pm.meta_value = %s
  801. AND p.post_parent = %s
  802. AND p.post_type = 'attachment'
  803. ",
  804. $thumbnails[$post->ID],
  805. $translation->element_id
  806. ) );
  807. update_post_meta($translation->element_id, '_thumbnail_id', $t_thumbnail_id);
  808. $count += 1;
  809.  
  810. }
  811. }
  812. }
  813. }
  814.  
  815. }
  816. }
  817. echo $count . __(' Featured images duplicate to translated content', 'wpml-media');
  818. break;
  819. }
  820. exit;
  821. }
  822.  
  823. function create_duplicate_media($attachment) {
  824. static $parents_processed = array();
  825.  
  826. if ($attachment->post_parent && !in_array($attachment->post_parent, $parents_processed)) {
  827. global $wpdb, $sitepress;
  828.  
  829. // see if we have translations.
  830.  
  831. $post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID = $attachment->post_parent");
  832. $trid = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id={$attachment->post_parent} AND element_type = 'post_$post_type'");
  833. if ($trid) {
  834.  
  835. $attachments = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_parent = $attachment->post_parent");
  836.  
  837. $translations = $sitepress->get_element_translations($trid, 'post_' . $post_type);
  838. foreach ($translations as $translation) {
  839. if ($translation->element_id && $translation->element_id != $attachment->post_parent) {
  840.  
  841. $attachments_in_translation = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_parent = $translation->element_id");
  842. if (sizeof($attachments_in_translation) == 0) {
  843. // only duplicate attachments if there a none already.
  844. foreach ($attachments as $att_id) {
  845. // duplicate the attachement
  846.  
  847. $this->create_duplicate_attachment($att_id, $translation->element_id, $translation->language_code);
  848.  
  849. }
  850. }
  851. }
  852. }
  853. }
  854.  
  855. $parents_processed[] = $attachment->post_parent;
  856.  
  857. }
  858. update_post_meta($attachment->ID, 'wpml_media_processed', 1);
  859. }
  860.  
  861. function create_duplicate_attachment($att_id, $parent_id, $lang) {
  862. $post = get_post($att_id);
  863. $post->post_parent = $parent_id;
  864. $post->ID = NULL;
  865. $dup_att_id = wp_insert_post($post);
  866. // duplicate the post meta data.
  867. $meta = get_post_meta($att_id, '_wp_attachment_metadata', true);
  868. add_post_meta($dup_att_id, '_wp_attachment_metadata', $meta);
  869. update_post_meta($dup_att_id, 'wpml_media_processed', 1);
  870. update_post_meta($dup_att_id, 'wpml_media_lang', $lang);
  871. update_post_meta($dup_att_id, 'wpml_media_duplicate_of', $att_id);
  872. $attached_file = get_post_meta($att_id, '_wp_attached_file', true);
  873. update_post_meta($dup_att_id, '_wp_attached_file', $attached_file);
  874.  
  875. do_action('wpml_media_create_duplicate_attachment', $att_id, $dup_att_id);
  876. }
  877.  
  878. function js_scripts(){
  879. global $pagenow;
  880. if ($pagenow == 'media.php') {
  881. ?>
  882. <script type="text/javascript">
  883. addLoadEvent(function(){
  884. jQuery('#icl_lang_options').insertBefore(jQuery('#post_id'));
  885. jQuery('#icl_lang_options').fadeIn();
  886. });
  887. </script>
  888.  
  889. <?php
  890. }
  891. if (isset($_GET['page']) && $_GET['page'] == 'wpml-media') {
  892. ?>
  893. <script type="text/javascript">
  894. addLoadEvent(function(){
  895. jQuery('#wpml_media_re_scan_but').click(wpml_media_re_scan);
  896. jQuery('#wpml_media_re_scan_all_but').click(wpml_media_re_scan_all);
  897. jQuery('#wpml_media_feature_image_but').click(wpml_media_feature_image_scan)
  898.  
  899. });
  900. var wpml_media_scan_started = false;
  901. var req_timer = 0;
  902. function wpml_media_toogle_scan(action){
  903. action = typeof(action) != 'undefined' ? action : 'rescan';
  904.  
  905. if(!wpml_media_scan_started){
  906. wpml_media_send_request(action);
  907. jQuery('#wpml_media_ajx_ldr_1').fadeIn();
  908. jQuery('#wpml_media_re_scan_but').attr('value','<?php echo icl_js_escape(__('Running', 'wpml-media')) ?>');
  909. jQuery('#wpml_media_re_scan_all_but').attr('value','<?php echo icl_js_escape(__('Running', 'wpml-media')) ?>');
  910. }else{
  911. jQuery('#wpml_media_re_scan_but').attr('value','<?php echo icl_js_escape(__('Scan and duplicate attachments', 'wpml-media')); ?>');
  912. jQuery('#wpml_media_re_scan_all_but').attr('value','<?php echo icl_js_escape(__('Scan All', 'wpml-media')); ?>');
  913. window.clearTimeout(req_timer);
  914. jQuery('#wpml_media_ajx_ldr_1').fadeOut();
  915. location.reload();
  916. }
  917. wpml_media_scan_started = !wpml_media_scan_started;
  918. return false;
  919. }
  920.  
  921. function wpml_media_send_request(action){
  922. jQuery.ajax({
  923. type: "POST",
  924. url: "<?php echo htmlentities($_SERVER['REQUEST_URI']) ?>",
  925. data: "wpml_media_ajx_action=" + action,
  926. success: function(msg){
  927. if(-1==msg || msg==0){
  928. left = '0';
  929. wpml_media_toogle_scan();
  930. }else{
  931. left=msg;
  932. }
  933.  
  934.  
  935. jQuery('#wpml_media_re_scan_toscan').html(left);
  936. if(wpml_media_scan_started){
  937. req_timer = window.setTimeout('wpml_media_send_request("rescan")', 1000);
  938. }
  939. }
  940. });
  941.  
  942. }
  943. function wpml_media_re_scan(){
  944. wpml_media_toogle_scan("rescan");
  945. }
  946. function wpml_media_re_scan_all(){
  947. wpml_media_toogle_scan("rescan_all");
  948. }
  949.  
  950. function wpml_media_feature_image_scan(){
  951. jQuery('#wpml_media_result').fadeOut();
  952. jQuery('#wpml_media_ajx_ldr_2').fadeIn();
  953. jQuery('#wpml_media_feature_image_but').attr('value','<?php echo icl_js_escape(__('Running', 'wpml-media')) ?>');
  954. jQuery.ajax({
  955. type: "POST",
  956. url: "<?php echo htmlentities($_SERVER['REQUEST_URI']) ?>",
  957. data: "wpml_media_ajx_action=featured_image_scan",
  958. success: function(msg){
  959. jQuery('#wpml_media_ajx_ldr_2').fadeOut();
  960. jQuery('#wpml_media_feature_image_but').attr('value','<?php echo icl_js_escape(__('Scan and duplicate featured images', 'wpml-media')) ?>');
  961. jQuery('#wpml_media_result').html(msg);
  962. jQuery('#wpml_media_result').fadeIn();
  963. }
  964. });
  965. }
  966.  
  967. </script>
  968. <?php
  969. }
  970. }
  971.  
  972.  
  973.  
  974. function _initialize_message(){
  975. $url = rtrim(get_option('siteurl'),'/') . '/wp-admin/admin.php?page=wpml-media';
  976. ?>
  977. <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'),
  978. $url); ?></p></div>
  979. <?php
  980. }
  981.  
  982. function _no_wpml_warning(){
  983. ?>
  984. <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'),
  985. 'http://wpml.org/'); ?></p></div>
  986. <?php
  987. }
  988.  
  989. function _old_wpml_warning(){
  990. ?>
  991. <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'),
  992. 'http://wpml.org/'); ?></p></div>
  993. <?php
  994. }
  995. // Localization
  996. function plugin_localization(){
  997. load_plugin_textdomain( 'wpml-media', false, WPML_MEDIA_FOLDER . '/locale');
  998. }
  999.  
  1000. function language_filter(){
  1001. global $sitepress;
  1002.  
  1003. if (isset($_GET['lang'])) {
  1004. $lang_code = $_GET['lang'];
  1005. } else {
  1006. if (method_exists($sitepress, 'get_admin_language_cookie')) {
  1007. $lang_code = $sitepress->get_admin_language_cookie();
  1008. }
  1009. }
  1010.  
  1011. $active_languages = $sitepress->get_active_languages();
  1012.  
  1013.  
  1014. $active_languages[] = array('code'=>'all','display_name'=>__('All languages','sitepress'));
  1015. foreach($active_languages as $lang){
  1016. if($lang['code'] == $lang_code){
  1017. $px = '<strong>';
  1018. $sx = ' <span class="count">('. $lang['code'] .')<\/span><\/strong>';
  1019. } else {
  1020. $px = '<a href="' . $_SERVER['REQUEST_URI'] . '&lang=' . $lang['code']. '">';
  1021. $sx = '<\/a> <span class="count">('. $lang['code'] .')<\/span>';
  1022. }
  1023. $as[] = $px . $lang['display_name'] . $sx;
  1024. }
  1025.  
  1026. $allas = join(' | ', $as);
  1027.  
  1028. $prot_link = '';
  1029. ?>
  1030. <script type="text/javascript">
  1031. jQuery(".subsubsub").append('<br /><span id="icl_subsubsub"><?php echo $allas ?><\/span><br /><?php echo $prot_link ?>');
  1032. </script>
  1033. <?php
  1034. }
  1035.  
  1036. function views_upload($views) {
  1037. global $sitepress, $wpdb,$pagenow;
  1038.  
  1039. if ($pagenow == 'upload.php') {
  1040. //get current language
  1041. $lang = $sitepress->get_current_language();
  1042.  
  1043. foreach ($views as $key => $view) {
  1044. if ($lang != 'all') {
  1045. if ($key == 'all') {
  1046. //select all attachments
  1047. $sql = "SELECT COUNT(p.id) FROM {$wpdb->posts} AS p LEFT JOIN {$wpdb->postmeta} AS pm ON pm.post_id = p.id WHERE p.post_type = 'attachment' AND pm.meta_key='wpml_media_lang' AND pm.meta_value='" . $lang . "'";
  1048. } elseif ($key == 'detached') {
  1049. //select detached attachments
  1050. $sql .= " AND p.post_parent = 0 ";
  1051. } else {
  1052. //select mime type(image,etc) attachments
  1053. $sql .= " AND p.post_mime_type LIKE '" . $key . "%'";
  1054. }
  1055. $res = $wpdb->get_col($sql);
  1056. //replace count
  1057. $view = preg_replace('/\((\d+)\)/', '(' . $res[0] . ')', $view);
  1058. }
  1059. //replace href link
  1060. if ($key == 'all') {
  1061. $views[$key] = preg_replace('/(href=["\'])([\s\S]+?)(["\'])/', '$1$2?lang=' . $lang . '$3', $view);
  1062. } else {
  1063. $views[$key] = preg_replace('/(href=["\'])([\s\S]+?)(["\'])/', '$1$2&lang=' . $lang . '$3', $view);
  1064. }
  1065. }
  1066. }
  1067. return $views;
  1068. }
  1069.  
  1070. function language_filter_upload_page() {
  1071. global $sitepress, $wpdb;
  1072.  
  1073. //get language code
  1074. if (isset($_GET['lang'])) {
  1075. $lang_code = $_GET['lang'];
  1076. } else {
  1077. if (method_exists($sitepress, 'get_admin_language_cookie')) {
  1078. $lang_code = $sitepress->get_admin_language_cookie();
  1079. }
  1080. if (empty($lang_code)) {
  1081. $lang_code = $sitepress->get_default_language();
  1082. }
  1083. }
  1084.  
  1085. $active_languages = $sitepress->get_active_languages();
  1086. $active_languages[] = array('code' => 'all', 'display_name' => __('All languages', 'sitepress'));
  1087.  
  1088.  
  1089.  
  1090.  
  1091. $sql = '';
  1092. $langc['all'] = 0;
  1093. foreach ($active_languages as $lang) {
  1094. //select all attachments
  1095. $sql = "SELECT COUNT(p.id) FROM {$wpdb->posts} AS p LEFT JOIN {$wpdb->postmeta} AS pm ON pm.post_id = p.id WHERE p.post_type = 'attachment' AND pm.meta_key='wpml_media_lang' AND pm.meta_value='" . $lang['code'] . "'";
  1096. //select detached attachments
  1097. if (isset($_GET['detached']))
  1098. $sql .= " AND p.post_parent = 0 ";
  1099. //select mime type(image,etc) attachments
  1100. if (isset($_GET['post_mime_type']))
  1101. $sql .= " AND p.post_mime_type LIKE '" . $_GET['post_mime_type'] . "%'";
  1102. $res = $wpdb->get_col($sql);
  1103.  
  1104. //count attachments
  1105. if ($lang['code'] != 'all')
  1106. $langc[$lang['code']] = $res[0];
  1107. $langc['all'] += $res[0];
  1108.  
  1109. //generation language block
  1110. if ($lang['code'] == $lang_code) {
  1111. $px = '<strong>';
  1112. $sx = ' <span class="count">(' . $langc[$lang['code']] . ')<\/span><\/strong>';
  1113. } else {
  1114. if (isset($_GET['post_mime_type'])) {
  1115. $px = '<a href="?post_mime_type=' . $_GET['post_mime_type'] . '&lang=' . $lang['code'] . '">';
  1116. } elseif (isset($_GET['detached'])) {
  1117. $px = '<a href="?detached=' . $_GET['detached'] . '&lang=' . $lang['code'] . '">';
  1118. } else {
  1119. $px = '<a href="?lang=' . $lang['code'] . '">';
  1120. }
  1121. $sx = '<\/a> <span class="count">(' . $langc[$lang['code']] . ')<\/span>';
  1122. }
  1123. $as[] = $px . $lang['display_name'] . $sx;
  1124. }
  1125.  
  1126. $allas = join(' | ', $as);
  1127.  
  1128. $prot_link = '';
  1129. //append language block
  1130. ?>
  1131. <script type="text/javascript">
  1132. jQuery(".subsubsub").append('<br /><span id="icl_subsubsub"><?php echo $allas ?><\/span><br /><?php echo $prot_link ?>');
  1133. </script>
  1134. <?php
  1135. }
  1136.  
  1137. //check if the image is not duplicated to another post before deleting it physically
  1138. function delete_file($file) {
  1139. global $wpdb;
  1140. //get file name from full name
  1141. $file_name = preg_replace('/^(.+)\-\d+x\d+(\.\w+)$/', '$1$2', $file);
  1142. $file_name = preg_replace('/^[\s\S]+(\/.+)$/', '$1', $file_name);
  1143. //check file name in DB
  1144. $attachment = $wpdb->get_row("SELECT pm.meta_id FROM {$wpdb->postmeta} AS pm WHERE pm.meta_value LIKE '%" . $file_name . "'");
  1145. //if exist return NULL(do not delete physically)
  1146. if (!empty($attachment))
  1147. $file = null;
  1148. return $file;
  1149. }
  1150.  
  1151.  
  1152.  
  1153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement