No Description

class-pum-wpml.php 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. // Exit if accessed directly
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit;
  5. }
  6. /**
  7. * Class PUM_WPML_Integration
  8. */
  9. class PUM_WPML_Integration {
  10. /**
  11. *
  12. */
  13. public static function init() {
  14. add_action( 'icl_make_duplicate', array( __CLASS__, 'duplicate_post' ), 10, 4 );
  15. /*
  16. add_filter( 'pum_popup', array( __CLASS__, 'pum_popup' ), 10, 2 );
  17. TODO Further testing of this filter may prove 80+% of the following unneeded.
  18. */
  19. add_filter( 'pum_popup_get_display', array( __CLASS__, 'popup_get_display' ), 10, 2 );
  20. add_filter( 'pum_popup_get_close', array( __CLASS__, 'popup_get_close' ), 10, 2 );
  21. add_filter( 'pum_popup_get_triggers', array( __CLASS__, 'popup_get_triggers' ), 10, 2 );
  22. add_filter( 'pum_popup_get_cookies', array( __CLASS__, 'popup_get_cookies' ), 10, 2 );
  23. add_filter( 'pum_popup_get_conditions', array( __CLASS__, 'popup_get_conditions' ), 10, 2 );
  24. add_filter( 'pum_popup_get_theme_id', array( __CLASS__, 'popup_get_theme_id' ), 10, 2 );
  25. add_filter( 'pum_popup_mobile_disabled', array( __CLASS__, 'popup_mobile_disabled' ), 10, 2 );
  26. add_filter( 'pum_popup_tablet_disabled', array( __CLASS__, 'popup_tablet_disabled' ), 10, 2 );
  27. }
  28. /**
  29. * @param $popup
  30. * @param null $popup_id
  31. *
  32. * @return \PUM_Model_Popup
  33. */
  34. public static function pum_popup( $popup, $popup_id = null ) {
  35. if ( self::is_new_popup_translation( $popup_id ) ) {
  36. remove_filter( 'pum_popup', array( __CLASS__, 'pum_popup' ), 10 );
  37. $popup = pum_get_popup( self::source_id( $popup_id ) );
  38. add_filter( 'pum_popup', array( __CLASS__, 'pum_popup' ), 10, 2 );
  39. }
  40. return $popup;
  41. }
  42. /**
  43. * @param int $post_id
  44. *
  45. * @return bool
  46. */
  47. public static function is_new_popup_translation( $post_id = 0 ) {
  48. global $pagenow, $sitepress;
  49. return is_admin() && $pagenow == 'post-new.php' && ! empty( $_GET['post_type'] ) && $_GET['post_type'] == 'popup' && self::source_id( $post_id ) > 0;
  50. }
  51. /**
  52. * @param int $post_id
  53. *
  54. * @return int
  55. */
  56. public static function source_id( $post_id = 0 ) {
  57. global $sitepress;
  58. static $source_id;
  59. if ( ! isset( $source_id ) && ! empty( $sitepress ) && method_exists( $sitepress, 'get_new_post_source_id' ) ) {
  60. $source_id = absint( $sitepress->get_new_post_source_id( $post_id ) );
  61. }
  62. return $source_id;
  63. }
  64. /**
  65. * @param int $post_id
  66. */
  67. public static function source_lang( $post_id = 0 ) {
  68. }
  69. /**
  70. * @param int $post_id
  71. *
  72. * @return int
  73. */
  74. public static function trid( $post_id = 0 ) {
  75. global $sitepress;
  76. static $trid;
  77. if ( ! isset( $trid ) && ! empty( $sitepress ) && method_exists( $sitepress, 'get_element_trid' )) {
  78. $trid = absint( $sitepress->get_element_trid( $post_id, 'post_popup' ) );
  79. }
  80. return $trid;
  81. }
  82. /**
  83. * @param $disabled
  84. * @param $post_id
  85. *
  86. * @return bool
  87. */
  88. public static function popup_mobile_disabled( $disabled, $post_id ) {
  89. if ( self::is_new_popup_translation( $post_id ) ) {
  90. remove_filter( 'pum_popup_mobile_disabled', array( __CLASS__, 'popup_mobile_disabled' ), 10 );
  91. $disabled = pum_get_popup( self::source_id( $post_id ) )->mobile_disabled();
  92. add_filter( 'pum_popup_mobile_disabled', array( __CLASS__, 'popup_mobile_disabled' ), 10, 2 );
  93. }
  94. return $disabled;
  95. }
  96. /**
  97. * @param $disabled
  98. * @param $post_id
  99. *
  100. * @return bool
  101. */
  102. public static function popup_tablet_disabled( $disabled, $post_id ) {
  103. if ( self::is_new_popup_translation( $post_id ) ) {
  104. remove_filter( 'pum_popup_tablet_disabled', array( __CLASS__, 'popup_tablet_disabled' ), 10 );
  105. $disabled = pum_get_popup( self::source_id( $post_id ) )->tablet_disabled();
  106. add_filter( 'pum_popup_tablet_disabled', array( __CLASS__, 'popup_tablet_disabled' ), 10, 2 );
  107. }
  108. return $disabled;
  109. }
  110. /**
  111. * @param $triggers
  112. * @param $post_id
  113. *
  114. * @return array
  115. */
  116. public static function popup_get_triggers( $triggers, $post_id ) {
  117. if ( self::is_new_popup_translation( $post_id ) ) {
  118. remove_filter( 'pum_popup_get_triggers', array( __CLASS__, 'popup_get_triggers' ), 10 );
  119. $triggers = pum_get_popup( self::source_id( $post_id ) )->get_triggers();
  120. add_filter( 'pum_popup_get_triggers', array( __CLASS__, 'popup_get_triggers' ), 10, 2 );
  121. }
  122. return $triggers;
  123. }
  124. /**
  125. * @param $display
  126. * @param $post_id
  127. *
  128. * @return mixed
  129. */
  130. public static function popup_get_display( $display, $post_id ) {
  131. if ( self::is_new_popup_translation( $post_id ) ) {
  132. remove_filter( 'pum_popup_get_display', array( __CLASS__, 'popup_get_display' ), 10 );
  133. $display = pum_get_popup( self::source_id( $post_id ) )->get_display();
  134. add_filter( 'pum_popup_get_display', array( __CLASS__, 'popup_get_display' ), 10, 2 );
  135. }
  136. return $display;
  137. }
  138. /**
  139. * @param $close
  140. * @param $post_id
  141. *
  142. * @return mixed
  143. */
  144. public static function popup_get_close( $close, $post_id ) {
  145. if ( self::is_new_popup_translation( $post_id ) ) {
  146. remove_filter( 'pum_popup_get_close', array( __CLASS__, 'popup_get_close' ), 10 );
  147. $close = pum_get_popup( self::source_id( $post_id ) )->get_close();
  148. add_filter( 'pum_popup_get_close', array( __CLASS__, 'popup_get_close' ), 10, 2 );
  149. }
  150. return $close;
  151. }
  152. /**
  153. * @param $cookies
  154. * @param $post_id
  155. *
  156. * @return array
  157. */
  158. public static function popup_get_cookies( $cookies, $post_id ) {
  159. if ( self::is_new_popup_translation( $post_id ) ) {
  160. remove_filter( 'pum_popup_get_cookies', array( __CLASS__, 'popup_get_cookies' ), 10 );
  161. $cookies = pum_get_popup( self::source_id( $post_id ) )->get_cookies();
  162. add_filter( 'pum_popup_get_cookies', array( __CLASS__, 'popup_get_cookies' ), 10, 2 );
  163. }
  164. return $cookies;
  165. }
  166. /**
  167. * @param $theme_id
  168. * @param $post_id
  169. *
  170. * @return int
  171. */
  172. public static function popup_get_theme_id( $theme_id, $post_id ) {
  173. if ( self::is_new_popup_translation( $post_id ) ) {
  174. remove_filter( 'pum_popup_get_theme_id', array( __CLASS__, 'popup_get_theme_id' ), 10 );
  175. $theme_id = pum_get_popup( self::source_id( $post_id ) )->get_theme_id();
  176. add_filter( 'pum_popup_get_theme_id', array( __CLASS__, 'popup_get_theme_id' ), 10, 2 );
  177. }
  178. return $theme_id;
  179. }
  180. /**
  181. * @param $conditions
  182. * @param null $new_lang
  183. *
  184. * @return mixed
  185. */
  186. public static function remap_conditions( $conditions, $new_lang = null ) {
  187. if ( ! isset( $new_lang ) && empty( $_GET['lang'] ) ) {
  188. return $conditions;
  189. }
  190. if ( ! isset( $new_lang ) ) {
  191. $new_lang = $_GET['lang'];
  192. }
  193. foreach ( $conditions as $group_key => $group ) {
  194. foreach ( $group as $key => $condition ) {
  195. $target = $condition['target'];
  196. $tests = array(
  197. strpos( $target, '_selected' ) !== false,
  198. strpos( $target, '_ID' ) !== false,
  199. strpos( $target, '_children' ) !== false,
  200. strpos( $target, '_ancestors' ) !== false,
  201. strpos( $target, '_w_' ) !== false,
  202. );
  203. if ( ! in_array( true, $tests ) ) {
  204. continue;
  205. }
  206. // Taxonomy
  207. if ( strpos( $target, 'tax_' ) === 0 ) {
  208. $t = explode( '_', $target );
  209. // Remove the tax_ prefix.
  210. array_shift( $t );
  211. // Assign the last key as the modifier _all, _selected
  212. $modifier = array_pop( $t );
  213. // Whatever is left is the taxonomy.
  214. $type = implode( '_', $t );
  215. } // Post by Tax
  216. elseif ( strpos( $target, '_w_' ) !== false ) {
  217. $t = explode( '_w_', $target );
  218. // First key is the post type.
  219. $post_type = array_shift( $t );
  220. // Last Key is the taxonomy
  221. $type = array_pop( $t );
  222. } // Post Type
  223. else {
  224. $t = explode( '_', $target );
  225. // Modifier should be the last key.
  226. $modifier = array_pop( $t );
  227. // Post type is the remaining keys combined.
  228. $type = implode( '_', $t );
  229. }
  230. // To hold the newly remapped selection.
  231. $selected = array();
  232. foreach ( wp_parse_id_list( $condition['selected'] ) as $object_id ) {
  233. // Insert the translated post_id or the original if no translation exists.
  234. $selected[] = wpml_object_id_filter( $object_id, $type, true, $new_lang );
  235. }
  236. // Replace the original conditions with the new remapped ones.
  237. $conditions[ $group_key ][ $key ]['selected'] = $selected;
  238. }
  239. }
  240. return $conditions;
  241. }
  242. /**
  243. * @param $conditions
  244. * @param $post_id
  245. *
  246. * @return array|mixed
  247. */
  248. public static function popup_get_conditions( $conditions, $post_id ) {
  249. if ( self::is_new_popup_translation( $post_id ) ) {
  250. remove_filter( 'pum_popup_get_conditions', array( __CLASS__, 'popup_get_conditions' ), 10 );
  251. $popup = pum_get_popup( self::source_id( $post_id ) );
  252. $conditions = $popup->get_conditions();
  253. $conditions = self::remap_conditions( $conditions, $post_id );
  254. add_filter( 'pum_popup_get_conditions', array( __CLASS__, 'popup_get_conditions' ), 10, 2 );
  255. }
  256. return $conditions;
  257. }
  258. /**
  259. * @return mixed|void
  260. */
  261. public static function untranslatable_meta_keys() {
  262. return apply_filters( 'pum_wpml_untranslatable_meta_keys', array(
  263. 'popup_display',
  264. 'popup_theme',
  265. 'popup_triggers',
  266. 'popup_cookies',
  267. 'popup_conditions',
  268. 'popup_mobile_disabled',
  269. 'popup_tablet_disabled',
  270. ) );
  271. }
  272. /**
  273. * @return mixed|void
  274. */
  275. public static function translatable_meta_keys() {
  276. return apply_filters( 'pum_wpml_translatable_meta_keys', array(
  277. 'popup_close',
  278. 'popup_title',
  279. ) );
  280. }
  281. /**
  282. * Copies post_meta for popups on duplication.
  283. *
  284. * Only copies untranslatable data.
  285. *
  286. * @param $master_post_id int Original post_ID.
  287. * @param $lang string The new language.
  288. * @param $post_array array The $post array for the new/duplicate post.
  289. * @param $id int The post_ID for the new/duplicate post.
  290. */
  291. public static function duplicate_post( $master_post_id, $lang, $post_array, $id ) {
  292. // Only do this for popups.
  293. if ( get_post_type( $master_post_id ) != 'popup' ) {
  294. return;
  295. }
  296. foreach ( self::untranslatable_meta_keys() as $key ) {
  297. $value = get_post_meta( $master_post_id, $key, true );
  298. if ( ! $value ) {
  299. continue;
  300. }
  301. if ( $key == 'popup_conditions' ) {
  302. $value = self::remap_conditions( $value, $lang );
  303. }
  304. update_post_meta( $id, $key, $value );
  305. }
  306. }
  307. }