Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/sitepres.../inc
File: wpml-post-edit-ajax.class.php
<?php
[0] Fix | Delete
[1] Fix | Delete
class WPML_Post_Edit_Ajax {
[2] Fix | Delete
const AJAX_ACTION_SWITCH_POST_LANGUAGE = 'wpml_switch_post_language';
[3] Fix | Delete
[4] Fix | Delete
/**
[5] Fix | Delete
* For test purposes
[6] Fix | Delete
*
[7] Fix | Delete
* @var WPML_Custom_Field_Setting_Factory
[8] Fix | Delete
*/
[9] Fix | Delete
public static $post_custom_field_settings;
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Ajax handler for adding a term via Ajax.
[13] Fix | Delete
*/
[14] Fix | Delete
public static function wpml_save_term_action() {
[15] Fix | Delete
global $sitepress;
[16] Fix | Delete
[17] Fix | Delete
if ( ! wpml_is_action_authenticated( 'wpml_save_term' ) ) {
[18] Fix | Delete
wp_send_json_error( 'Wrong Nonce' );
[19] Fix | Delete
}
[20] Fix | Delete
[21] Fix | Delete
$lang = filter_var( $_POST['term_language_code'], FILTER_SANITIZE_STRING );
[22] Fix | Delete
$taxonomy = filter_var( $_POST['taxonomy'], FILTER_SANITIZE_STRING );
[23] Fix | Delete
$slug = filter_var( $_POST['slug'], FILTER_SANITIZE_STRING );
[24] Fix | Delete
$name = filter_var( $_POST['name'], FILTER_SANITIZE_STRING );
[25] Fix | Delete
$trid = filter_var( $_POST['trid'], FILTER_SANITIZE_NUMBER_INT );
[26] Fix | Delete
$description = filter_var( $_POST['description'], FILTER_SANITIZE_STRING );
[27] Fix | Delete
$meta_data = isset( $_POST['meta_data'] ) ? $_POST['meta_data'] : array();
[28] Fix | Delete
[29] Fix | Delete
$new_term_object = self::save_term_ajax( $sitepress, $lang, $taxonomy, $slug, $name, $trid, $description, $meta_data );
[30] Fix | Delete
$sitepress->get_wp_api()->wp_send_json_success( $new_term_object );
[31] Fix | Delete
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
public static function save_term_ajax( $sitepress, $lang, $taxonomy, $slug, $name, $trid, $description, $meta_data ) {
[35] Fix | Delete
$new_term_object = false;
[36] Fix | Delete
[37] Fix | Delete
if ( $name !== "" && $taxonomy && $trid && $lang ) {
[38] Fix | Delete
[39] Fix | Delete
$args = array(
[40] Fix | Delete
'taxonomy' => $taxonomy,
[41] Fix | Delete
'lang_code' => $lang,
[42] Fix | Delete
'term' => $name,
[43] Fix | Delete
'trid' => $trid,
[44] Fix | Delete
'overwrite' => true
[45] Fix | Delete
);
[46] Fix | Delete
[47] Fix | Delete
if ( $slug ) {
[48] Fix | Delete
$args[ 'slug' ] = $slug;
[49] Fix | Delete
}
[50] Fix | Delete
if ( $description ) {
[51] Fix | Delete
$args[ 'description' ] = $description;
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
$switch_lang = new WPML_Temporary_Switch_Language( $sitepress, $lang );
[55] Fix | Delete
$is_new_term = ! term_exists( $name, $taxonomy );
[56] Fix | Delete
$res = WPML_Terms_Translations::create_new_term( $args );
[57] Fix | Delete
$switch_lang->restore_lang();
[58] Fix | Delete
[59] Fix | Delete
if ( $res && isset( $res[ 'term_taxonomy_id' ] ) ) {
[60] Fix | Delete
/* res holds the term taxonomy id, we return the whole term objects to the ajax call */
[61] Fix | Delete
$switch_lang = new WPML_Temporary_Switch_Language( $sitepress, $lang );
[62] Fix | Delete
$new_term_object = get_term_by( 'term_taxonomy_id', (int) $res[ 'term_taxonomy_id' ], $taxonomy );
[63] Fix | Delete
$switch_lang->restore_lang();
[64] Fix | Delete
$lang_details = $sitepress->get_element_language_details( $new_term_object->term_taxonomy_id, 'tax_' . $new_term_object->taxonomy );
[65] Fix | Delete
$new_term_object->trid = $lang_details->trid;
[66] Fix | Delete
$new_term_object->language_code = $lang_details->language_code;
[67] Fix | Delete
if ( self::add_term_metadata( $res, $meta_data, $is_new_term ) ) {
[68] Fix | Delete
$new_term_object->meta_data = get_term_meta( $res['term_id'] );
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
WPML_Terms_Translations::icl_save_term_translation_action( $taxonomy, $res );
[72] Fix | Delete
}
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
return $new_term_object;
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* Gets the content of a post, its excerpt as well as its title and returns it as an array
[80] Fix | Delete
*
[81] Fix | Delete
* @param string $content_type
[82] Fix | Delete
* @param string $excerpt_type
[83] Fix | Delete
* @param int $trid
[84] Fix | Delete
* @param string $lang
[85] Fix | Delete
*
[86] Fix | Delete
* @return array containing all the fields information
[87] Fix | Delete
*/
[88] Fix | Delete
public static function copy_from_original_fields( $content_type, $excerpt_type, $trid, $lang ) {
[89] Fix | Delete
global $wpdb;
[90] Fix | Delete
$post_id = $wpdb->get_var(
[91] Fix | Delete
$wpdb->prepare( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND language_code=%s",
[92] Fix | Delete
$trid,
[93] Fix | Delete
$lang ) );
[94] Fix | Delete
$post = get_post( $post_id );
[95] Fix | Delete
[96] Fix | Delete
$fields_to_copy = array( 'content' => 'post_content',
[97] Fix | Delete
'title' => 'post_title',
[98] Fix | Delete
'excerpt' => 'post_excerpt' );
[99] Fix | Delete
[100] Fix | Delete
$fields_contents = array();
[101] Fix | Delete
if ( ! empty( $post ) ) {
[102] Fix | Delete
foreach ( $fields_to_copy as $editor_key => $editor_field ) { //loops over the three fields to be inserted into the array
[103] Fix | Delete
if ( $editor_key === 'content' || $editor_key === 'excerpt' ) { //
[104] Fix | Delete
$editor_var = 'rich';
[105] Fix | Delete
if ( $editor_key === 'content' ) {
[106] Fix | Delete
$editor_var = $content_type; //these variables are supplied by a javascript call in scripts.js icl_copy_from_original(lang, trid)
[107] Fix | Delete
} elseif ( $editor_key === 'excerpt' ) {
[108] Fix | Delete
$editor_var = $excerpt_type;
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
if ( function_exists( 'format_for_editor' ) ) {
[112] Fix | Delete
// WordPress 4.3 uses format_for_editor
[113] Fix | Delete
$html_pre = $post->$editor_field;
[114] Fix | Delete
if($editor_var == 'rich') {
[115] Fix | Delete
$html_pre = convert_chars( $html_pre );
[116] Fix | Delete
$html_pre = wpautop( $html_pre );
[117] Fix | Delete
}
[118] Fix | Delete
$html_pre = format_for_editor( $html_pre, $editor_var );
[119] Fix | Delete
} else {
[120] Fix | Delete
// Backwards compatible for WordPress < 4.3
[121] Fix | Delete
if ( $editor_var === 'rich' ) {
[122] Fix | Delete
$html_pre = wp_richedit_pre( $post->$editor_field );
[123] Fix | Delete
} else {
[124] Fix | Delete
$html_pre = wp_htmledit_pre( $post->$editor_field );
[125] Fix | Delete
}
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
$fields_contents[$editor_key] = htmlspecialchars_decode( $html_pre );
[129] Fix | Delete
} elseif ( $editor_key === 'title' ) {
[130] Fix | Delete
$fields_contents[ $editor_key ] = strip_tags( $post->$editor_field );
[131] Fix | Delete
}
[132] Fix | Delete
}
[133] Fix | Delete
$fields_contents[ 'builtin_custom_fields' ] = apply_filters( 'wpml_copy_from_original_custom_fields',
[134] Fix | Delete
self::copy_from_original_custom_fields( $post ) );
[135] Fix | Delete
[136] Fix | Delete
$fields_contents['external_custom_fields'] = self::copy_meta_values_from_original( $post );
[137] Fix | Delete
} else {
[138] Fix | Delete
$fields_contents[ 'error' ] = __( 'Post not found', 'sitepress' );
[139] Fix | Delete
}
[140] Fix | Delete
do_action( 'icl_copy_from_original', $post_id );
[141] Fix | Delete
[142] Fix | Delete
return $fields_contents;
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
/**
[146] Fix | Delete
* Gets the content of a custom posts custom field , its excerpt as well as its title and returns it as an array
[147] Fix | Delete
*
[148] Fix | Delete
* @param WP_post $post
[149] Fix | Delete
*
[150] Fix | Delete
* @return array
[151] Fix | Delete
*/
[152] Fix | Delete
public static function copy_from_original_custom_fields( $post ) {
[153] Fix | Delete
[154] Fix | Delete
$elements = array();
[155] Fix | Delete
$elements [ 'post_type' ] = $post->post_type;
[156] Fix | Delete
$elements[ 'excerpt' ] = array(
[157] Fix | Delete
'editor_name' => 'excerpt',
[158] Fix | Delete
'editor_type' => 'text',
[159] Fix | Delete
'value' => $post->post_excerpt
[160] Fix | Delete
);
[161] Fix | Delete
[162] Fix | Delete
return $elements;
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
/**
[166] Fix | Delete
* @param WP_Post $post
[167] Fix | Delete
* @return array
[168] Fix | Delete
*/
[169] Fix | Delete
private static function copy_meta_values_from_original ($post) {
[170] Fix | Delete
global $wpdb;
[171] Fix | Delete
[172] Fix | Delete
if ( ! self::$post_custom_field_settings instanceof WPML_Custom_Field_Setting_Factory ) {
[173] Fix | Delete
$translation_management = wpml_load_core_tm();
[174] Fix | Delete
self::$post_custom_field_settings = new WPML_Custom_Field_Setting_Factory( $translation_management );
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
$post_custom_fields = self::$post_custom_field_settings->get_post_meta_keys();
[178] Fix | Delete
[179] Fix | Delete
if ( ! is_array( $post_custom_fields ) || empty( $post_custom_fields ) ) {
[180] Fix | Delete
return array();
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
$sql = "SELECT meta_key as name, meta_value as value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key IN ("
[184] Fix | Delete
. wpml_prepare_in( $post_custom_fields ) . ')';
[185] Fix | Delete
[186] Fix | Delete
return $wpdb->get_results( $wpdb->prepare( $sql, $post->ID ), ARRAY_A );
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/**
[190] Fix | Delete
* Ajax handler for switching the language of a post.
[191] Fix | Delete
*/
[192] Fix | Delete
public static function wpml_switch_post_language() {
[193] Fix | Delete
global $sitepress, $wpdb;
[194] Fix | Delete
[195] Fix | Delete
$nonce = $_POST['nonce'];
[196] Fix | Delete
if ( ! wp_verify_nonce( $nonce, self::AJAX_ACTION_SWITCH_POST_LANGUAGE ) ) {
[197] Fix | Delete
wp_send_json_error();
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
$to = false;
[201] Fix | Delete
$post_id = false;
[202] Fix | Delete
[203] Fix | Delete
if ( isset( $_POST[ 'wpml_to' ] ) ) {
[204] Fix | Delete
$to = $_POST[ 'wpml_to' ];
[205] Fix | Delete
}
[206] Fix | Delete
if ( isset( $_POST[ 'wpml_post_id' ] ) ) {
[207] Fix | Delete
$post_id = $_POST[ 'wpml_post_id' ];
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
$result = false;
[211] Fix | Delete
[212] Fix | Delete
if ( $post_id && $to ) {
[213] Fix | Delete
[214] Fix | Delete
$post_type = get_post_type( $post_id );
[215] Fix | Delete
$wpml_post_type = 'post_' . $post_type;
[216] Fix | Delete
$trid = $sitepress->get_element_trid( $post_id, $wpml_post_type );
[217] Fix | Delete
[218] Fix | Delete
/* Check if a translation in that language already exists with a different post id.
[219] Fix | Delete
* If so, then don't perform this action.
[220] Fix | Delete
*/
[221] Fix | Delete
[222] Fix | Delete
$query_for_existing_translation = $wpdb->prepare( " SELECT translation_id, element_id
[223] Fix | Delete
FROM {$wpdb->prefix}icl_translations
[224] Fix | Delete
WHERE element_type = %s
[225] Fix | Delete
AND trid = %d
[226] Fix | Delete
AND language_code = %s",
[227] Fix | Delete
$wpml_post_type, $trid, $to );
[228] Fix | Delete
$existing_translation = $wpdb->get_row( $query_for_existing_translation );
[229] Fix | Delete
[230] Fix | Delete
if ( $existing_translation && $existing_translation->element_id != $post_id ) {
[231] Fix | Delete
$result = false;
[232] Fix | Delete
} else {
[233] Fix | Delete
$sitepress->set_element_language_details( $post_id, $wpml_post_type, $trid, $to );
[234] Fix | Delete
// Synchronize the posts terms languages. Do not create automatic translations though.
[235] Fix | Delete
WPML_Terms_Translations::sync_post_terms_language( $post_id );
[236] Fix | Delete
require_once WPML_PLUGIN_PATH . '/inc/cache.php';
[237] Fix | Delete
icl_cache_clear( $post_type . 's_per_language', true );
[238] Fix | Delete
[239] Fix | Delete
$result = $to;
[240] Fix | Delete
}
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
wp_send_json_success( $result );
[244] Fix | Delete
}
[245] Fix | Delete
[246] Fix | Delete
public static function wpml_get_default_lang() {
[247] Fix | Delete
global $sitepress;
[248] Fix | Delete
wp_send_json_success( $sitepress->get_default_language() );
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
/**
[252] Fix | Delete
* @param array $term
[253] Fix | Delete
* @param array $meta_data
[254] Fix | Delete
* @param bool $is_new_term
[255] Fix | Delete
*
[256] Fix | Delete
* @return bool
[257] Fix | Delete
*/
[258] Fix | Delete
private static function add_term_metadata( $term, $meta_data, $is_new_term ) {
[259] Fix | Delete
global $sitepress;
[260] Fix | Delete
[261] Fix | Delete
foreach ( $meta_data as $meta_key => $meta_value ) {
[262] Fix | Delete
delete_term_meta( $term['term_id'], $meta_key );
[263] Fix | Delete
$data = maybe_unserialize( stripslashes( $meta_value ) );
[264] Fix | Delete
if ( ! add_term_meta( $term['term_id'], $meta_key, $data ) ) {
[265] Fix | Delete
throw new RuntimeException( sprintf( 'Unable to add term meta form term: %d', $term['term_id'] ) );
[266] Fix | Delete
}
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
$sync_meta_action = new WPML_Sync_Term_Meta_Action( $sitepress, $term[ 'term_taxonomy_id' ], $is_new_term );
[270] Fix | Delete
$sync_meta_action->run();
[271] Fix | Delete
[272] Fix | Delete
return true;
[273] Fix | Delete
}
[274] Fix | Delete
}
[275] Fix | Delete
[276] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function