| 1 |
<?php |
| 2 |
namespace FileBird\Controller; |
| 3 |
|
| 4 |
use FileBird\Model\Folder as FolderModel; |
| 5 |
|
| 6 |
defined('ABSPATH') || exit; |
| 7 |
|
| 8 |
class CompatibleWpml extends Controller { |
| 9 |
protected static $instance = null; |
| 10 |
|
| 11 |
protected $post_translations; |
| 12 |
private $sitepress; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
global $sitepress; |
| 16 |
if ( $sitepress === null || get_class($sitepress) !== "SitePress" ) { |
| 17 |
return; |
| 18 |
} |
| 19 |
$this->sitepress = $sitepress; |
| 20 |
$this->post_translations = $sitepress->post_translations(); |
| 21 |
|
| 22 |
add_action('fbv_after_set_folder', array($this, 'fbvAfterSetFolder'), 10, 2); |
| 23 |
add_filter('fbv_in_not_in', array($this, 'filterInNotIn')); |
| 24 |
add_filter('wpml_pre_parse_query', array($this, 'preParseQuery')); |
| 25 |
add_filter('wpml_post_parse_query', array($this, 'postParseQuery')); |
| 26 |
} |
| 27 |
|
| 28 |
public static function getInstance() { |
| 29 |
if (null == self::$instance) { |
| 30 |
self::$instance = new self; |
| 31 |
} |
| 32 |
return self::$instance; |
| 33 |
} |
| 34 |
|
| 35 |
public function fbvAfterSetFolder($id, $folder) { |
| 36 |
global $wpdb; |
| 37 |
$cpt_sync_options = $this->sitepress->get_setting( 'custom_posts_sync_option', array() ); |
| 38 |
if(isset($cpt_sync_options['attachment']) && $cpt_sync_options['attachment'] == '0') { |
| 39 |
return; |
| 40 |
} |
| 41 |
$post = get_post( $id ); |
| 42 |
$post_type = $post->post_type; |
| 43 |
$post_trid = $this->sitepress->get_element_trid( $id, 'post_' . $post_type ); |
| 44 |
$post_translations = $this->sitepress->get_element_translations( $post_trid, 'post_' . $post_type ); |
| 45 |
|
| 46 |
foreach ( $post_translations as $post_language => $translated_post ) { |
| 47 |
$translated_post_id = $translated_post->element_id; |
| 48 |
if ( ! $translated_post_id ) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
FolderModel::setFoldersForPosts($translated_post_id, (int)$folder, false); |
| 52 |
} |
| 53 |
} |
| 54 |
public function filterInNotIn($query) { |
| 55 |
$query = $this->adjust_q_var_pids($query, 'post__not_in'); |
| 56 |
$query = $this->adjust_q_var_pids($query, 'post__in'); |
| 57 |
return $query; |
| 58 |
} |
| 59 |
public function preParseQuery($q) { |
| 60 |
if ( ! empty( $q->query_vars['post_type'] ) && $q->query_vars['post_type'] == 'attachment' ) { |
| 61 |
$cpt_sync_options = $this->sitepress->get_setting( 'custom_posts_sync_option', array() ); |
| 62 |
if(isset($cpt_sync_options['attachment']) && $cpt_sync_options['attachment'] == '0') { |
| 63 |
$q->query_vars['fbv_backup_post__in'] = $q->query_vars['post__in']; |
| 64 |
$q->query_vars['fbv_backup_post__not_in'] = $q->query_vars['post__not_in']; |
| 65 |
$q->query_vars['post__in'] = array(); |
| 66 |
$q->query_vars['post__not_in'] = array(); |
| 67 |
} |
| 68 |
} |
| 69 |
return $q; |
| 70 |
} |
| 71 |
public function postParseQuery($q) { |
| 72 |
if ( ! empty( $q->query_vars['post_type'] ) && $q->query_vars['post_type'] == 'attachment' ) { |
| 73 |
$cpt_sync_options = $this->sitepress->get_setting( 'custom_posts_sync_option', array() ); |
| 74 |
if(isset($cpt_sync_options['attachment']) && $cpt_sync_options['attachment'] == '0') { |
| 75 |
$q->query_vars['post__in'] = $q->query_vars['fbv_backup_post__in']; |
| 76 |
$q->query_vars['post__not_in'] = $q->query_vars['fbv_backup_post__not_in']; |
| 77 |
unset($q->query_vars['fbv_backup_post__in']); |
| 78 |
unset($q->query_vars['fbv_backup_post__not_in']); |
| 79 |
} |
| 80 |
} |
| 81 |
return $q; |
| 82 |
} |
| 83 |
private function adjust_q_var_pids( $q, $index ) { |
| 84 |
if ( ! empty( $q[ $index ] ) ) { |
| 85 |
|
| 86 |
$untranslated = $q[ $index ]; |
| 87 |
$this->post_translations->prefetch_ids( $untranslated ); |
| 88 |
$current_lang = $this->sitepress->get_current_language(); |
| 89 |
$pid = array(); |
| 90 |
foreach ( $q[ $index ] as $p ) { |
| 91 |
$pid[] = $this->post_translations->element_id_in( $p, $current_lang, true ); |
| 92 |
} |
| 93 |
$q[ $index ] = $pid; |
| 94 |
} |
| 95 |
|
| 96 |
return $q; |
| 97 |
} |
| 98 |
} |