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.../classes/media
File: class-wpml-model-attachments.php
<?php
[0] Fix | Delete
[1] Fix | Delete
class WPML_Model_Attachments {
[2] Fix | Delete
const ATTACHMENT_TYPE = 'post_attachment';
[3] Fix | Delete
[4] Fix | Delete
/** @var SitePress */
[5] Fix | Delete
private $sitepress;
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* @var WPML_Post_Status
[9] Fix | Delete
*/
[10] Fix | Delete
private $status_helper;
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* @param SitePress $sitepress
[14] Fix | Delete
* @param WPML_Post_Status $status_helper
[15] Fix | Delete
*/
[16] Fix | Delete
public function __construct( SitePress $sitepress, WPML_Post_Status $status_helper ) {
[17] Fix | Delete
$this->sitepress = $sitepress;
[18] Fix | Delete
$this->status_helper = $status_helper;
[19] Fix | Delete
}
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* @param int $attachment_id
[23] Fix | Delete
* @param int $duplicated_attachment_id
[24] Fix | Delete
*/
[25] Fix | Delete
public function duplicate_post_meta_data( $attachment_id, $duplicated_attachment_id ) {
[26] Fix | Delete
foreach ( array( '_wp_attachment_metadata', '_wp_attached_file' ) as $meta_key ) {
[27] Fix | Delete
$duplicated_meta_value = get_post_meta( $duplicated_attachment_id, $meta_key, true );
[28] Fix | Delete
[29] Fix | Delete
if ( ! $duplicated_meta_value ) {
[30] Fix | Delete
$source_meta_value = get_post_meta( $attachment_id, $meta_key, true );
[31] Fix | Delete
update_post_meta( $duplicated_attachment_id, $meta_key, $source_meta_value );
[32] Fix | Delete
}
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
update_post_meta( $duplicated_attachment_id, 'wpml_media_processed', 1 );
[36] Fix | Delete
[37] Fix | Delete
do_action( 'wpml_media_create_duplicate_attachment', $attachment_id, $duplicated_attachment_id );
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* @param int $trid
[42] Fix | Delete
* @param string $target_language
[43] Fix | Delete
*
[44] Fix | Delete
* @return null|WP_Post
[45] Fix | Delete
*/
[46] Fix | Delete
public function find_duplicated_attachment( $trid, $target_language ) {
[47] Fix | Delete
$attachment_translations = $this->sitepress->get_element_translations( $trid, self::ATTACHMENT_TYPE, true, true );
[48] Fix | Delete
if ( is_array( $attachment_translations ) ) {
[49] Fix | Delete
foreach ( $attachment_translations as $attachment_translation ) {
[50] Fix | Delete
if ( $attachment_translation->language_code === $target_language ) {
[51] Fix | Delete
return get_post( $attachment_translation->element_id );
[52] Fix | Delete
}
[53] Fix | Delete
}
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
return null;
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* @param WP_Post|null $attachment
[61] Fix | Delete
* @param int $parent_id_of_attachement
[62] Fix | Delete
* @param string $target_language
[63] Fix | Delete
*
[64] Fix | Delete
* @return int|null
[65] Fix | Delete
*/
[66] Fix | Delete
public function fetch_translated_parent_id( $attachment, $parent_id_of_attachement, $target_language ) {
[67] Fix | Delete
$translated_parent_id = null;
[68] Fix | Delete
[69] Fix | Delete
if ( null !== $attachment && $attachment->post_parent ) {
[70] Fix | Delete
$translated_parent_id = $attachment->post_parent;
[71] Fix | Delete
}
[72] Fix | Delete
[73] Fix | Delete
if ( $parent_id_of_attachement ) {
[74] Fix | Delete
$parent_post = get_post( $parent_id_of_attachement );
[75] Fix | Delete
if ( $parent_post ) {
[76] Fix | Delete
$translated_parent_id = $parent_id_of_attachement;
[77] Fix | Delete
$parent_id_language_code = $this->sitepress->get_language_for_element( $parent_post->ID, 'post_' . $parent_post->post_type );
[78] Fix | Delete
if ( $parent_id_language_code !== $target_language ) {
[79] Fix | Delete
$translated_parent_id = $this->sitepress->get_object_id( $parent_post->ID, $parent_post->post_type, false, $target_language );
[80] Fix | Delete
}
[81] Fix | Delete
}
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
return $translated_parent_id;
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
/**
[88] Fix | Delete
* @param int $new_parent_id
[89] Fix | Delete
* @param WP_Post $attachment
[90] Fix | Delete
*/
[91] Fix | Delete
public function update_parent_id_in_existing_attachment( $new_parent_id, $attachment ) {
[92] Fix | Delete
if ( $this->is_valid_post_type( $attachment->post_type ) ) {
[93] Fix | Delete
wp_update_post( array( 'ID' => $attachment->ID, 'post_parent' => $new_parent_id ) );
[94] Fix | Delete
}
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* @param string $post_type
[99] Fix | Delete
*
[100] Fix | Delete
* @return bool
[101] Fix | Delete
*/
[102] Fix | Delete
private function is_valid_post_type( $post_type ) {
[103] Fix | Delete
$post_types = array_keys( get_post_types( ) );
[104] Fix | Delete
[105] Fix | Delete
return in_array( $post_type, $post_types, true );
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
/**
[109] Fix | Delete
* @param int $attachment_id
[110] Fix | Delete
* @param string $target_language
[111] Fix | Delete
* @param int $parent_id_in_target_language
[112] Fix | Delete
* @param int $trid
[113] Fix | Delete
*
[114] Fix | Delete
* @return int
[115] Fix | Delete
* @throws WPML_Media_Exception
[116] Fix | Delete
*/
[117] Fix | Delete
public function duplicate_attachment( $attachment_id, $target_language, $parent_id_in_target_language, $trid ) {
[118] Fix | Delete
$post = get_post( $attachment_id );
[119] Fix | Delete
$post->post_parent = $parent_id_in_target_language;
[120] Fix | Delete
$post->ID = null;
[121] Fix | Delete
[122] Fix | Delete
update_post_meta( $parent_id_in_target_language, '_wpml_media_duplicate', true ); // add the post meta if missing
[123] Fix | Delete
[124] Fix | Delete
$duplicated_attachment_id = $this->insert_attachment( $post );
[125] Fix | Delete
if ( ! $duplicated_attachment_id ) {
[126] Fix | Delete
throw new WPML_Media_Exception( 'Error occured during inserting duplicated attachment to db' );
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
$this->add_language_information_to_attachment( $attachment_id, $duplicated_attachment_id, $target_language, $trid );
[130] Fix | Delete
[131] Fix | Delete
return $duplicated_attachment_id;
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
[135] Fix | Delete
/**
[136] Fix | Delete
* @param WP_Post $post
[137] Fix | Delete
*
[138] Fix | Delete
* @return int
[139] Fix | Delete
*/
[140] Fix | Delete
private function insert_attachment( $post ) {
[141] Fix | Delete
$add_attachment_filters_temp = null;
[142] Fix | Delete
if ( array_key_exists( 'add_attachment', $GLOBALS['wp_filter'] ) ) {
[143] Fix | Delete
$add_attachment_filters_temp = $GLOBALS['wp_filter']['add_attachment'];
[144] Fix | Delete
unset( $GLOBALS['wp_filter']['add_attachment'] );
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
$duplicated_attachment_id = wp_insert_post( $post );
[148] Fix | Delete
if ( ! is_int( $duplicated_attachment_id ) ) {
[149] Fix | Delete
$duplicated_attachment_id = 0;
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
if ( null !== $add_attachment_filters_temp ) {
[153] Fix | Delete
$GLOBALS['wp_filter']['add_attachment'] = $add_attachment_filters_temp;
[154] Fix | Delete
unset( $add_attachment_filters_temp );
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
return $duplicated_attachment_id;
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
/**
[161] Fix | Delete
* @param int $attachment_id
[162] Fix | Delete
* @param int $duplicated_attachment_id
[163] Fix | Delete
* @param string $target_language
[164] Fix | Delete
* @param int $trid
[165] Fix | Delete
*/
[166] Fix | Delete
private function add_language_information_to_attachment( $attachment_id, $duplicated_attachment_id, $target_language, $trid ) {
[167] Fix | Delete
$source_language = $this->sitepress->get_language_for_element( $attachment_id, self::ATTACHMENT_TYPE );
[168] Fix | Delete
$this->sitepress->set_element_language_details( $duplicated_attachment_id, self::ATTACHMENT_TYPE, $trid, $target_language, $source_language );
[169] Fix | Delete
$this->status_helper->set_status( $duplicated_attachment_id, ICL_TM_DUPLICATE );
[170] Fix | Delete
$this->status_helper->set_update_status( $duplicated_attachment_id, false );
[171] Fix | Delete
}
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function