first commit
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\Media\Duplication;
|
||||
|
||||
abstract class AbstractFactory implements \IWPML_Backend_Action_Loader, \IWPML_Frontend_Action_Loader {
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected static function shouldActivateHooks() {
|
||||
return \WPML_Element_Sync_Settings_Factory::createPost()->is_sync( 'attachment' );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\Media\Duplication;
|
||||
|
||||
use WPML\Element\API\IfOriginalPost;
|
||||
use WPML\FP\Fns;
|
||||
use WPML\FP\Relation;
|
||||
use WPML\LIB\WP\Post;
|
||||
use WPML\LIB\WP\Hooks as WPHooks;
|
||||
use function WPML\FP\pipe;
|
||||
use function WPML\FP\spreadArgs;
|
||||
|
||||
class Hooks {
|
||||
|
||||
public static function add() {
|
||||
WPHooks::onAction( 'update_postmeta', 10, 4 )
|
||||
->then( spreadArgs( Fns::withoutRecursion( Fns::noop(), [ self::class, 'syncAttachedFile' ] ) ) );
|
||||
}
|
||||
|
||||
public static function syncAttachedFile( $meta_id, $object_id, $meta_key, $meta_value ) {
|
||||
if ( $meta_key === '_wp_attached_file' ) {
|
||||
|
||||
$prevValue = Post::getMetaSingle( $object_id, $meta_key );
|
||||
|
||||
// $isMetaSameAsPrevious :: id → bool
|
||||
$isMetaSameAsPrevious = pipe( Post::getMetaSingle( Fns::__, $meta_key ), Relation::equals( $prevValue ) );
|
||||
|
||||
IfOriginalPost::getTranslationIds( $object_id )
|
||||
->filter( Fns::unary( $isMetaSameAsPrevious ) )
|
||||
->each( Fns::unary( Post::updateMeta( Fns::__, $meta_key, $meta_value ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\Media\Duplication;
|
||||
|
||||
class HooksFactory extends AbstractFactory {
|
||||
public function create() {
|
||||
if ( self::shouldActivateHooks() ) {
|
||||
return [ Hooks::class, 'add' ];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
class WPML_Media_Attachments_Duplication_Factory extends \WPML\Media\Duplication\AbstractFactory {
|
||||
|
||||
public function create() {
|
||||
if ( self::shouldActivateHooks() ) {
|
||||
return \WPML\Container\make( WPML_Media_Attachments_Duplication::class );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user