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

https://gitlab.com/woxiprogrammers/infinia-wordpress · PHP · 212 lines · 144 code · 34 blank · 34 comment · 29 complexity · 66b3aadfb62076d619f2e5c12a07ee72 MD5 · raw file

  1. <?php
  2. class WPML_Post_Edit_Ajax {
  3. /**
  4. * Ajax handler for adding a term via Ajax.
  5. */
  6. public static function wpml_save_term() {
  7. if ( !wpml_is_action_authenticated ( 'wpml_save_term' ) ) {
  8. wp_send_json_error ( 'Wrong Nonce' );
  9. }
  10. global $sitepress;
  11. $lang = filter_input ( INPUT_POST, 'term_language_code', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
  12. $taxonomy = filter_input ( INPUT_POST, 'taxonomy' );
  13. $slug = filter_input ( INPUT_POST, 'slug' );
  14. $name = filter_input ( INPUT_POST, 'name' );
  15. $trid = filter_input ( INPUT_POST, 'trid', FILTER_SANITIZE_NUMBER_INT );
  16. $description = filter_input ( INPUT_POST, 'description' );
  17. $new_term_object = false;
  18. if ( $name !== "" && $taxonomy && $trid && $lang ) {
  19. $args = array(
  20. 'taxonomy' => $taxonomy,
  21. 'lang_code' => $lang,
  22. 'term' => $name,
  23. 'trid' => $trid,
  24. 'overwrite' => true
  25. );
  26. if ( $slug ) {
  27. $args[ 'slug' ] = $slug;
  28. }
  29. if ( $description ) {
  30. $args[ 'description' ] = $description;
  31. }
  32. $res = WPML_Terms_Translations::create_new_term( $args );
  33. if ( $res && isset( $res[ 'term_taxonomy_id' ] ) ) {
  34. /* res holds the term taxonomy id, we return the whole term objects to the ajax call */
  35. $new_term_object = get_term_by( 'term_taxonomy_id', (int) $res[ 'term_taxonomy_id' ], $taxonomy );
  36. $lang_details = $sitepress->get_element_language_details( $new_term_object->term_taxonomy_id, 'tax_' . $new_term_object->taxonomy );
  37. $new_term_object->trid = $lang_details->trid;
  38. $new_term_object->language_code = $lang_details->language_code;
  39. WPML_Terms_Translations::icl_save_term_translation_action( $taxonomy, $res );
  40. }
  41. }
  42. wp_send_json_success( $new_term_object );
  43. }
  44. /**
  45. * Gets the content of a post, its excerpt as well as its title and returns it as an array
  46. *
  47. * @param string $content_type
  48. * @param string $excerpt_type
  49. * @param int $trid
  50. * @param string $lang
  51. *
  52. * @return array containing all the fields information
  53. */
  54. public static function copy_from_original_fields( $content_type, $excerpt_type, $trid, $lang ) {
  55. global $wpdb;
  56. $post_id = $wpdb->get_var(
  57. $wpdb->prepare( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND language_code=%s",
  58. $trid,
  59. $lang ) );
  60. $post = get_post( $post_id );
  61. $fields_to_copy = array( 'content' => 'post_content',
  62. 'title' => 'post_title',
  63. 'excerpt' => 'post_excerpt' );
  64. $fields_contents = array();
  65. if ( ! empty( $post ) ) {
  66. foreach ( $fields_to_copy as $editor_key => $editor_field ) { //loops over the three fields to be inserted into the array
  67. if ( $editor_key === 'content' || $editor_key === 'excerpt' ) { //
  68. $editor_var = 'rich';
  69. if ( $editor_key === 'content' ) {
  70. $editor_var = $content_type; //these variables are supplied by a javascript call in scripts.js icl_copy_from_original(lang, trid)
  71. } elseif ( $editor_key === 'excerpt' ) {
  72. $editor_var = $excerpt_type;
  73. }
  74. if ( function_exists( 'format_for_editor' ) ) {
  75. // WordPress 4.3 uses format_for_editor
  76. $html_pre = $post->$editor_field;
  77. if($editor_var == 'rich') {
  78. $html_pre = convert_chars( $html_pre );
  79. $html_pre = wpautop( $html_pre );
  80. }
  81. $html_pre = format_for_editor( $html_pre, $editor_var );
  82. } else {
  83. // Backwards compatible for WordPress < 4.3
  84. if ( $editor_var === 'rich' ) {
  85. $html_pre = wp_richedit_pre( $post->$editor_field );
  86. } else {
  87. $html_pre = wp_htmledit_pre( $post->$editor_field );
  88. }
  89. }
  90. $fields_contents[$editor_key] = htmlspecialchars_decode( $html_pre );
  91. } elseif ( $editor_key === 'title' ) {
  92. $fields_contents[ $editor_key ] = strip_tags( $post->$editor_field );
  93. }
  94. }
  95. $fields_contents[ 'customfields' ] = apply_filters( 'wpml_copy_from_original_custom_fields',
  96. self::copy_from_original_custom_fields( $post ) );
  97. } else {
  98. $fields_contents[ 'error' ] = __( 'Post not found', 'sitepress' );
  99. }
  100. do_action( 'icl_copy_from_original', $post_id );
  101. return $fields_contents;
  102. }
  103. /**
  104. * Gets the content of a custom posts custom field , its excerpt as well as its title and returns it as an array
  105. *
  106. * @param WP_post $post
  107. *
  108. * @return array
  109. */
  110. public static function copy_from_original_custom_fields( $post ) {
  111. $elements = array();
  112. $elements [ 'post_type' ] = $post->post_type;
  113. $elements[ 'excerpt' ] = array(
  114. 'editor_name' => 'excerpt',
  115. 'editor_type' => 'text',
  116. 'value' => $post->post_excerpt
  117. );
  118. return $elements;
  119. }
  120. /**
  121. * Ajax handler for switching the language of a post.
  122. */
  123. public static function wpml_switch_post_language() {
  124. global $sitepress, $wpdb;
  125. $to = false;
  126. $post_id = false;
  127. if ( isset( $_POST[ 'wpml_to' ] ) ) {
  128. $to = $_POST[ 'wpml_to' ];
  129. }
  130. if ( isset( $_POST[ 'wpml_post_id' ] ) ) {
  131. $post_id = $_POST[ 'wpml_post_id' ];
  132. }
  133. $result = false;
  134. set_transient( md5( $sitepress->get_current_user()->ID . 'current_user_post_edit_lang' ), $to );
  135. if ( $post_id && $to ) {
  136. $post_type = get_post_type( $post_id );
  137. $wpml_post_type = 'post_' . $post_type;
  138. $trid = $sitepress->get_element_trid( $post_id, $wpml_post_type );
  139. /* Check if a translation in that language already exists with a different post id.
  140. * If so, then don't perform this action.
  141. */
  142. $query_for_existing_translation = $wpdb->prepare( " SELECT translation_id, element_id
  143. FROM {$wpdb->prefix}icl_translations
  144. WHERE element_type = %s
  145. AND trid = %d
  146. AND language_code = %s",
  147. $wpml_post_type, $trid, $to );
  148. $existing_translation = $wpdb->get_row( $query_for_existing_translation );
  149. if ( $existing_translation && $existing_translation->element_id != $post_id ) {
  150. $result = false;
  151. } else {
  152. $sitepress->set_element_language_details( $post_id, $wpml_post_type, $trid, $to );
  153. // Synchronize the posts terms languages. Do not create automatic translations though.
  154. WPML_Terms_Translations::sync_post_terms_language( $post_id );
  155. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  156. icl_cache_clear( $post_type . 's_per_language', true );
  157. $result = $to;
  158. }
  159. }
  160. wp_send_json_success( $result );
  161. }
  162. /**
  163. * Saves the language from which a user is editing the currently edited post as a transient.
  164. * This is done so that filtering the language from which terms for the flat terms preview dropdown can be performed.
  165. */
  166. public static function wpml_set_post_edit_lang() {
  167. global $sitepress;
  168. $lang_code = false;
  169. if ( isset( $_POST[ 'wpml_post_lang' ] ) ) {
  170. $lang_code = $_POST[ 'wpml_post_lang' ];
  171. }
  172. set_transient( md5( $sitepress->get_current_user()->ID . 'current_user_post_edit_lang' ), $lang_code );
  173. }
  174. public static function wpml_get_default_lang() {
  175. global $sitepress;
  176. wp_send_json_success( $sitepress->get_default_language() );
  177. }
  178. }