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

https://gitlab.com/woxiprogrammers/infinia-wordpress · PHP · 169 lines · 113 code · 22 blank · 34 comment · 26 complexity · 4ff7b7a2610a622842e40d48f9536c8d MD5 · raw file

  1. <?php
  2. /**
  3. * Class WPML_Admin_Post_Actions
  4. *
  5. * @package wpml-core
  6. * @subpackage post-translation
  7. */
  8. class WPML_Admin_Post_Actions extends WPML_Post_Translation{
  9. public function init() {
  10. parent::init ();
  11. if ( $this->is_setup_complete() ) {
  12. add_action ( 'delete_post', array( $this, 'delete_post_actions' ) );
  13. add_action ( 'wp_trash_post', array( $this, 'trashed_post_actions' ) );
  14. add_action ( 'untrashed_post', array( $this, 'untrashed_post_actions' ) );
  15. }
  16. }
  17. /**
  18. * @param Integer $post_id
  19. * @param String $post_status
  20. * @return null|int
  21. */
  22. function get_save_post_trid( $post_id, $post_status ) {
  23. $trid = $this->get_element_trid( $post_id );
  24. $trid = ! $trid && isset( $post_vars['icl_trid'] ) ? $post_vars['icl_trid'] : $trid;
  25. $trid = $trid ? $trid : filter_var( isset( $_GET['trid'] ) ? $_GET['trid'] : '', FILTER_SANITIZE_NUMBER_INT );
  26. $trid = $trid ? $trid : $this->get_trid_from_referer();
  27. $trid = apply_filters( 'wpml_save_post_trid_value', $trid, $post_status );
  28. return $trid;
  29. }
  30. public function save_post_actions( $pidd, $post ) {
  31. global $sitepress;
  32. wp_defer_term_counting( true );
  33. $post = isset( $post ) ? $post : get_post( $pidd );
  34. // exceptions
  35. if ( ! $this->has_save_post_action( $post ) ) {
  36. wp_defer_term_counting( false );
  37. return;
  38. }
  39. if ( WPML_WordPress_Actions::is_bulk_trash( $pidd ) || WPML_WordPress_Actions::is_bulk_untrash( $pidd ) ) {
  40. return;
  41. }
  42. $default_language = $sitepress->get_default_language();
  43. $post_vars = (array) $_POST;
  44. foreach ( (array) $post as $k => $v ) {
  45. $post_vars[ $k ] = $v;
  46. }
  47. $post_vars['post_type'] = isset( $post_vars['post_type'] ) ? $post_vars['post_type'] : $post->post_type;
  48. $post_id = $pidd;
  49. if ( isset( $post_vars['action'] ) && $post_vars['action'] === 'post-quickpress-publish' ) {
  50. $language_code = $default_language;
  51. } else {
  52. $post_id = isset( $post_vars['post_ID'] ) ? $post_vars['post_ID']
  53. : $pidd; //latter case for XML-RPC publishing
  54. $language_code = $this->get_save_post_lang( $post_id, $sitepress );
  55. }
  56. if ( $this->is_inline_action( $post_vars ) && ! ( $language_code = $this->get_element_lang_code(
  57. $post_id
  58. ) )
  59. ) {
  60. return;
  61. }
  62. if ( isset( $post_vars['icl_translation_of'] ) && is_numeric( $post_vars['icl_translation_of'] ) ) {
  63. $translation_of_data_prepared = $this->wpdb->prepare(
  64. "SELECT trid, language_code
  65. FROM {$this->wpdb->prefix}icl_translations
  66. WHERE element_id=%d
  67. AND element_type=%s
  68. LIMIT 1",
  69. $post_vars['icl_translation_of'],
  70. 'post_' . $post->post_type
  71. );
  72. list( $trid, $source_language ) = $this->wpdb->get_row( $translation_of_data_prepared, 'ARRAY_N' );
  73. }
  74. if ( isset( $post_vars['icl_translation_of'] ) && $post_vars['icl_translation_of'] == 'none' ) {
  75. $trid = null;
  76. $source_language = $language_code;
  77. } else {
  78. $trid = isset( $trid ) && $trid ? $trid : $this->get_save_post_trid( $post_id, $post->post_status );
  79. // after getting the right trid set the source language from it by referring to the root translation
  80. // of this trid, in case no proper source language has been set yet
  81. $source_language = isset( $source_language )
  82. ? $source_language : $this->get_save_post_source_lang( $trid, $language_code, $default_language );
  83. }
  84. if ( isset( $post_vars['icl_tn_note'] ) ) {
  85. update_post_meta( $post_id, '_icl_translator_note', $post_vars['icl_tn_note'] );
  86. }
  87. $this->after_save_post( $trid, $post_vars, $language_code, $source_language );
  88. }
  89. /**
  90. * @param integer $post_id
  91. * @param SitePress $sitepress
  92. *
  93. * @return null|string
  94. */
  95. protected function get_save_post_lang( $post_id, $sitepress ) {
  96. $language_code = filter_var(
  97. ( isset( $_POST['icl_post_language'] ) ? $_POST['icl_post_language'] : '' ),
  98. FILTER_SANITIZE_FULL_SPECIAL_CHARS );
  99. $language_code = $language_code
  100. ? $language_code
  101. : filter_input(
  102. INPUT_GET,
  103. 'lang',
  104. FILTER_SANITIZE_FULL_SPECIAL_CHARS
  105. );
  106. return $language_code ? $language_code : parent::get_save_post_lang( $post_id, $sitepress );
  107. }
  108. /**
  109. * @param array $post_vars
  110. * @return bool
  111. */
  112. private function is_inline_action( $post_vars ) {
  113. return isset( $post_vars[ 'action' ] )
  114. && $post_vars[ 'action' ] == 'inline-save'
  115. || isset( $_GET[ 'bulk_edit' ] )
  116. || isset( $_GET[ 'doing_wp_cron' ] )
  117. || ( isset( $_GET[ 'action' ] )
  118. && $_GET[ 'action' ] == 'untrash' );
  119. }
  120. /**
  121. * @param int $trid
  122. * @param string $language_code
  123. * @param string $default_language
  124. *
  125. * @uses \WPML_Backend_Request::get_source_language_from_referer to retrieve the source_language when saving via ajax
  126. *
  127. * @return null|string
  128. */
  129. protected function get_save_post_source_lang( $trid, $language_code, $default_language ) {
  130. /** @var WPML_Backend_Request $wpml_request_handler */
  131. global $wpml_request_handler;
  132. $source_language = filter_input ( INPUT_GET, 'source_lang', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
  133. $source_language = $source_language ? $source_language
  134. : $wpml_request_handler->get_source_language_from_referer ();
  135. $source_language = $source_language ? $source_language : SitePress::get_source_language_by_trid ( $trid );
  136. $source_language = $source_language === 'all' ? $default_language : $source_language;
  137. $source_language = $source_language !== $language_code ? $source_language : null;
  138. return $source_language;
  139. }
  140. private function get_trid_from_referer() {
  141. if ( isset( $_SERVER[ 'HTTP_REFERER' ] ) ) {
  142. $query = parse_url ( $_SERVER[ 'HTTP_REFERER' ], PHP_URL_QUERY );
  143. parse_str ( $query, $vars );
  144. }
  145. return isset( $vars[ 'trid' ] ) ? filter_var ( $vars[ 'trid' ], FILTER_SANITIZE_NUMBER_INT ) : false;
  146. }
  147. }