PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/sitepress-multilingual-cms/inc/post-translation/wpml-post-translation.class.php

https://gitlab.com/woxiprogrammers/infinia-wordpress
PHP | 367 lines | 209 code | 51 blank | 107 comment | 26 complexity | 388dcc83fa79030c3a6cb21a5c730097 MD5 | raw file
  1. <?php
  2. require ICL_PLUGIN_PATH . '/inc/post-translation/wpml-post-duplication.class.php';
  3. require 'wpml-post-synchronization.class.php';
  4. require_once 'wpml-wordpress-actions.class.php';
  5. /**
  6. * Class WPML_Post_Translation
  7. *
  8. * @package wpml-core
  9. * @subpackage post-translation
  10. */
  11. abstract class WPML_Post_Translation extends WPML_Element_Translation {
  12. protected $settings;
  13. protected $post_translation_sync;
  14. /**
  15. * @var int[] $filtered_sticky_posts
  16. * @used-by \WPML_Post_Translation::pre_option_sticky_posts_filter to store sticky post_ids that were filtered
  17. * for display but might have to be added back
  18. * to an update of the option
  19. */
  20. private $filtered_sticky_posts = array();
  21. /**
  22. * @param array $settings
  23. * @param wpdb $wpdb
  24. */
  25. public function __construct( &$settings, &$wpdb ) {
  26. parent::__construct( $wpdb );
  27. $this->settings = $settings;
  28. }
  29. protected function is_setup_complete( ) {
  30. return isset( $this->settings[ 'setup_complete' ]) && $this->settings[ 'setup_complete' ];
  31. }
  32. public function init() {
  33. if ( $this->is_setup_complete() ) {
  34. add_action ( 'save_post', array( $this, 'save_post_actions' ), 100, 2 );
  35. add_filter( 'pre_update_option_sticky_posts', array( $this, 'pre_update_option_sticky_posts' ), 10, 1 );
  36. }
  37. }
  38. /**
  39. * Filters the sticky posts option. If synchronization of sticky posts is activated it will translated
  40. * wrong language values, if deactivated filter out wrong language values.
  41. *
  42. * @param int[] $posts
  43. * @param SitePress $sitepress
  44. *
  45. * @used-by \SitePress::option_sticky_posts which uses this function to filter the sticky posts array after
  46. * having removed WPML per-language filtering on the sticky posts option.
  47. *
  48. * @return int[]
  49. */
  50. public function pre_option_sticky_posts_filter( $posts, &$sitepress ) {
  51. $posts = $posts ? $posts : get_option( 'sticky_posts' );
  52. $this->filtered_sticky_posts = array();
  53. if ( $posts ) {
  54. $current_lang = $sitepress->get_current_language();
  55. $this->prefetch_ids( $posts );
  56. if ( $sitepress->get_setting( 'sync_sticky_flag' ) ) {
  57. $unfiltered = $posts;
  58. foreach ( $posts as $index => $sticky_post_id ) {
  59. $posts[ $index ] = $this->element_id_in(
  60. $sticky_post_id,
  61. $current_lang
  62. );
  63. }
  64. if ( $unfiltered != $posts ) {
  65. update_option( 'sticky_posts',
  66. array_values( array_unique( array_filter( array_merge( $posts, $unfiltered ) ) ) ) );
  67. }
  68. } else {
  69. foreach ( $posts as $index => $sticky_post_id ) {
  70. if ( $this->get_element_lang_code( $sticky_post_id ) !== $current_lang ) {
  71. $this->filtered_sticky_posts[] = $sticky_post_id;
  72. unset( $posts[ $index ] );
  73. }
  74. }
  75. }
  76. $posts = array_values( array_unique( array_filter( $posts ) ) );
  77. }
  78. return $posts;
  79. }
  80. /**
  81. * @param int[] $posts updated sticky posts option
  82. *
  83. * @uses \WPML_Post_Translation::$filtered_sticky_posts to retrieve those sticky post ids that might have been
  84. * filtered at option retrieval by \WPML_Post_Translation::pre_option_sticky_posts_filter but need to be
  85. * in the updated option, as it stores sticky posts for all languages
  86. *
  87. * @return array sticky posts option with previously filtered values added
  88. *
  89. * @hook pre_update_option_sticky_posts
  90. */
  91. public function pre_update_option_sticky_posts( $posts ) {
  92. $updated_sticky_list = array_values( array_unique( array_filter( array_merge( $posts,
  93. $this->filtered_sticky_posts ) ) ) );
  94. $this->filtered_sticky_posts = array();
  95. return $updated_sticky_list;
  96. }
  97. public function get_original_post_status( $trid, $source_lang_code = null ) {
  98. return $this->get_original_post_attr ( $trid, 'post_status', $source_lang_code );
  99. }
  100. public function get_original_post_ID( $trid, $source_lang_code = null ) {
  101. return $this->get_original_post_attr ( $trid, 'ID', $source_lang_code );
  102. }
  103. public function get_original_menu_order( $trid, $source_lang_code = null ) {
  104. return $this->get_original_post_attr ( $trid, 'menu_order', $source_lang_code );
  105. }
  106. public function get_original_comment_status( $trid, $source_lang_code = null ) {
  107. return $this->get_original_post_attr ( $trid, 'comment_status', $source_lang_code );
  108. }
  109. public function get_original_ping_status( $trid, $source_lang_code = null ) {
  110. return $this->get_original_post_attr ( $trid, 'ping_status', $source_lang_code );
  111. }
  112. public function get_original_post_format( $trid, $source_lang_code = null ) {
  113. return get_post_format ( $this->get_original_post_ID ( $trid, $source_lang_code ) );
  114. }
  115. /**
  116. * @param int $pidd
  117. * @param WP_Post $post
  118. *
  119. * @return void
  120. */
  121. public abstract function save_post_actions( $pidd, $post );
  122. public function trash_translation ( $trans_id ) {
  123. if ( !WPML_WordPress_Actions::is_bulk_trash( $trans_id ) ) {
  124. wp_trash_post( $trans_id );
  125. }
  126. }
  127. public function untrash_translation( $trans_id ) {
  128. if ( ! WPML_WordPress_Actions::is_bulk_untrash( $trans_id ) ) {
  129. wp_untrash_post( $trans_id );
  130. }
  131. }
  132. function untrashed_post_actions( $post_id ) {
  133. $translation_sync = $this->get_sync_helper ();
  134. $translation_sync->untrashed_post_actions ( $post_id );
  135. }
  136. public function delete_post_translation_entry( $post_id ) {
  137. $sql = $this->wpdb->prepare( "DELETE FROM {$this->wpdb->prefix}icl_translations
  138. WHERE element_id = %d
  139. AND element_type LIKE 'post%%'
  140. LIMIT 1",
  141. $post_id );
  142. $res = $this->wpdb->query( $sql );
  143. return $res;
  144. }
  145. public function trashed_post_actions( $post_id ) {
  146. $this->delete_post_actions( $post_id, true );
  147. }
  148. /**
  149. * This function holds all actions to be run after deleting a post.
  150. * 1. Delete the posts entry in icl_translations.
  151. * 2. Set one of the posts translations or delete all translations of the post, depending on sitepress settings.
  152. *
  153. * @param Integer $post_id
  154. * @param bool $keep_db_entries Sets whether icl_translations entries are to be deleted or kept, when hooking this to
  155. * post trashing we want them to be kept.
  156. */
  157. public function delete_post_actions( $post_id, $keep_db_entries = false ) {
  158. $translation_sync = $this->get_sync_helper ();
  159. $translation_sync->delete_post_actions ( $post_id, $keep_db_entries );
  160. }
  161. /**
  162. * @param int $post_id
  163. * @param string $post_status
  164. *
  165. * @return int|null
  166. */
  167. abstract function get_save_post_trid( $post_id, $post_status );
  168. /**
  169. * @param integer $post_id
  170. * @param SitePress $sitepress
  171. * @return bool|mixed|null|string|void
  172. */
  173. protected function get_save_post_lang( $post_id, $sitepress ) {
  174. $language_code = $this->get_element_lang_code ( $post_id );
  175. $language_code = $language_code ? $language_code : $sitepress->get_current_language ();
  176. $language_code = $sitepress->is_active_language ( $language_code ) ? $language_code
  177. : $sitepress->get_default_language ();
  178. return apply_filters ( 'wpml_save_post_lang', $language_code );
  179. }
  180. /**
  181. * @param int $trid
  182. * @param string $language_code
  183. * @param string $default_language
  184. *
  185. * @return string|null
  186. */
  187. protected abstract function get_save_post_source_lang( $trid, $language_code, $default_language );
  188. /**
  189. * Sets a posts language details, invalidates caches relating to the post and triggers
  190. * synchronisation actions across translations of the just saved post.
  191. *
  192. * @param int $trid
  193. * @param array $post_vars
  194. * @param string $language_code
  195. * @param string $source_language
  196. *
  197. * @used-by \WPML_Post_Translation::save_post_actions as final step of the WPML Core save_post actions
  198. */
  199. protected function after_save_post( $trid, $post_vars, $language_code, $source_language ) {
  200. $this->maybe_set_elid( $trid, $post_vars['post_type'], $language_code, $post_vars['ID'], $source_language );
  201. $translation_sync = $this->get_sync_helper();
  202. $original_id = $this->get_original_element( $post_vars['ID'] );
  203. if ( $original_id ) {
  204. $translation_sync->sync_with_translations( $original_id, $post_vars );
  205. }
  206. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  207. icl_cache_clear( $post_vars['post_type'] . 's_per_language', true );
  208. wp_defer_term_counting( false );
  209. if ( $post_vars['post_type'] !== 'nav_menu_item' ) {
  210. do_action( 'wpml_tm_save_post', $post_vars['ID'], get_post( $post_vars['ID'] ), false );
  211. }
  212. }
  213. private function get_original_post_attr( $trid, $attribute, $source_lang_code ) {
  214. $legal_attributes = array(
  215. 'post_status',
  216. 'post_date',
  217. 'menu_order',
  218. 'comment_status',
  219. 'ping_status',
  220. 'ID'
  221. );
  222. $res = false;
  223. if ( in_array ( $attribute, $legal_attributes, true ) ) {
  224. $attribute = 'p.' . $attribute;
  225. $source_snippet = $source_lang_code === null
  226. ? " AND t.source_language_code IS NULL "
  227. : $this->wpdb->prepare ( " AND t.language_code = %s ", $source_lang_code );
  228. $res = $this->wpdb->get_var (
  229. $this->wpdb->prepare (
  230. "SELECT {$attribute}
  231. {$this->element_join}
  232. WHERE t.trid=%d
  233. {$source_snippet}
  234. LIMIT 1",
  235. $trid
  236. )
  237. );
  238. }
  239. return $res;
  240. }
  241. protected function has_save_post_action( $post ) {
  242. return !( !$this->is_translated_type ( $post->post_type )
  243. || ( isset( $post->post_status ) && $post->post_status === "auto-draft" )
  244. || isset( $_POST[ 'autosave' ] )
  245. || isset( $_POST[ 'skip_sitepress_actions' ] )
  246. || ( isset( $_POST[ 'post_ID' ] )
  247. && $_POST[ 'post_ID' ] != $post->ID )
  248. || ( isset( $_POST[ 'post_type' ] )
  249. && $_POST[ 'post_type' ] === 'revision' )
  250. || $post->post_type === 'revision'
  251. || get_post_meta ( $post->ID, '_wp_trash_meta_status', true )
  252. || ( isset( $_GET[ 'action' ] )
  253. && $_GET[ 'action' ] === 'untrash' ) );
  254. }
  255. protected function get_element_join() {
  256. return "FROM {$this->wpdb->prefix}icl_translations t
  257. JOIN {$this->wpdb->posts} p
  258. ON t.element_id = p.ID
  259. AND t.element_type = CONCAT('post_', p.post_type)";
  260. }
  261. public function is_translated_type( $post_type ) {
  262. global $sitepress;
  263. return $sitepress->is_translated_post_type ( $post_type );
  264. }
  265. /**
  266. * @param WP_Post $post
  267. *
  268. * @return string[] all language codes the post can be translated into
  269. */
  270. public function get_allowed_target_langs( $post ) {
  271. global $sitepress;
  272. $active_languages = $sitepress->get_active_languages ();
  273. $can_translate = array_keys ( $active_languages );
  274. $can_translate = array_diff (
  275. $can_translate,
  276. array( $this->get_element_lang_code ( $post->ID ) )
  277. );
  278. return apply_filters ( 'wpml_allowed_target_langs', $can_translate, $post->ID, 'post' );
  279. }
  280. /** Before setting the language of the post to be saved, check if a translation in this language already exists
  281. * This check is necessary, so that synchronization actions like thrashing or un-trashing of posts, do not lead to
  282. * database corruption, due to erroneously changing a posts language into a state,
  283. * where it collides with an existing translation. While the UI prevents this sort of action for the most part,
  284. * this is not necessarily the case for other plugins like TM.
  285. * The logic here first of all checks if an existing translation id is present in the desired language_code.
  286. * If so but this translation is actually not the currently to be saved post,
  287. * then this post will be saved to its current language. If the translation already exists,
  288. * the existing translation id will be used. In all other cases a new entry in icl_translations will be created.
  289. *
  290. * @param Integer $trid
  291. * @param String $post_type
  292. * @param String $language_code
  293. * @param Integer $post_id
  294. * @param String $source_language
  295. */
  296. protected function maybe_set_elid( $trid, $post_type, $language_code, $post_id, $source_language ) {
  297. global $sitepress;
  298. $element_type = 'post_' . $post_type;
  299. $sitepress->set_element_language_details (
  300. $post_id,
  301. $element_type,
  302. $trid,
  303. $language_code,
  304. $source_language
  305. );
  306. }
  307. /**
  308. * @return WPML_Post_Synchronization
  309. */
  310. private function get_sync_helper() {
  311. global $sitepress;
  312. $this->post_translation_sync = $this->post_translation_sync
  313. ? $this->post_translation_sync : new WPML_Post_Synchronization( $this->settings, $this, $sitepress );
  314. return $this->post_translation_sync;
  315. }
  316. }