first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
<?php
class WPML_Media_Post_Media_Usage_Factory implements IWPML_Backend_Action_Loader, IWPML_Frontend_Action_Loader {
public function create(){
global $sitepress;
return new WPML_Media_Post_Media_Usage(
$sitepress,
new WPML_Media_Post_With_Media_Files_Factory(),
new WPML_Media_Usage_Factory()
);
}
}

View File

@@ -0,0 +1,61 @@
<?php
class WPML_Media_Post_Media_Usage implements IWPML_Action {
/** @see WPML_Post_Translation::save_post_actions() */
const PRIORITY_AFTER_CORE_SAVE_POST_ACTIONS = 200;
/**
* @var SitePress
*/
private $sitepress;
/**
* @var WPML_Media_Post_With_Media_Files_Factory
*/
private $post_with_media_files_factory;
/**
* @var WPML_Media_Usage_Factory
*/
private $media_usage_factory;
public function __construct(
SitePress $sitepress,
WPML_Media_Post_With_Media_Files_Factory $post_with_media_files_factory,
WPML_Media_Usage_Factory $media_usage_factory
) {
$this->sitepress = $sitepress;
$this->post_with_media_files_factory = $post_with_media_files_factory;
$this->media_usage_factory = $media_usage_factory;
}
public function add_hooks() {
add_action( 'save_post', array( $this, 'update_media_usage' ), self::PRIORITY_AFTER_CORE_SAVE_POST_ACTIONS, 2 );
}
/**
* @param int $post_id
* @param WP_Post|null $post
*/
public function update_media_usage( $post_id, $post = null ) {
if ( null === $post ) {
$post = get_post( $post_id );
}
if ( $this->sitepress->get_wp_api()->constant( 'DOING_AUTOSAVE' )
|| ! $this->sitepress->is_translated_post_type( $post->post_type )
|| $post_id !== (int) $this->sitepress->get_original_element_id( $post_id, 'post_' . $post->post_type )
) {
return;
}
$media_ids = $this->post_with_media_files_factory->create( $post_id )->get_media_ids();
foreach ( $media_ids as $media_id ) {
$this->media_usage_factory->create( $media_id )->add_post( $post->ID );
}
}
}

View File

@@ -0,0 +1,9 @@
<?php
class WPML_Media_Usage_Factory {
public function create( $attachment_id ) {
return new WPML_Media_Usage( $attachment_id );
}
}

View File

@@ -0,0 +1,55 @@
<?php
class WPML_Media_Usage {
const FIELD_NAME = '_wpml_media_usage';
/**
* @var int
*/
private $attachment_id;
/**
* @var array
*/
private $usage;
/**
* @param int $attachment_id
*/
public function __construct( $attachment_id ) {
$this->attachment_id = $attachment_id;
$usage = get_post_meta( $this->attachment_id, self::FIELD_NAME, true );
$this->usage = empty( $usage ) ? array() : $usage;
}
/**
* @return array
*/
public function get_posts() {
return empty( $this->usage['posts'] ) ? array() : $this->usage['posts'];
}
/**
* @param int $post_id
*/
public function add_post( $post_id ) {
$posts = $this->get_posts();
$posts[] = $post_id;
$this->usage['posts'] = array_unique( $posts );
$this->update_usage();
}
/**
* @param int $post_id
*/
public function remove_post( $post_id ) {
$this->usage['posts'] = array_values( array_diff( (array) $this->usage['posts'], array( $post_id ) ) );
$this->update_usage();
}
private function update_usage() {
update_post_meta( $this->attachment_id, self::FIELD_NAME, $this->usage );
}
}