@@ -6,31 +6,31 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Sync_User_Adapter extends Abstract_Sync_Object_Adapter { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * Sync_User_Adapter constructor. |
|
| 11 | - * |
|
| 12 | - * @param int $user_id |
|
| 13 | - * |
|
| 14 | - * @throws \Exception |
|
| 15 | - */ |
|
| 16 | - function __construct( $user_id ) { |
|
| 17 | - parent::__construct( Object_Type_Enum::USER, $user_id ); |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - function is_published() { |
|
| 21 | - return true; |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - function is_public() { |
|
| 25 | - return true; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - function set_values( $arr ) { |
|
| 29 | - // @@todo |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - function get_value( $key ) { |
|
| 33 | - // @@todo |
|
| 34 | - } |
|
| 9 | + /** |
|
| 10 | + * Sync_User_Adapter constructor. |
|
| 11 | + * |
|
| 12 | + * @param int $user_id |
|
| 13 | + * |
|
| 14 | + * @throws \Exception |
|
| 15 | + */ |
|
| 16 | + function __construct( $user_id ) { |
|
| 17 | + parent::__construct( Object_Type_Enum::USER, $user_id ); |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + function is_published() { |
|
| 21 | + return true; |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + function is_public() { |
|
| 25 | + return true; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + function set_values( $arr ) { |
|
| 29 | + // @@todo |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + function get_value( $key ) { |
|
| 33 | + // @@todo |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | } |
@@ -13,8 +13,8 @@ discard block |
||
| 13 | 13 | * |
| 14 | 14 | * @throws \Exception |
| 15 | 15 | */ |
| 16 | - function __construct( $user_id ) { |
|
| 17 | - parent::__construct( Object_Type_Enum::USER, $user_id ); |
|
| 16 | + function __construct($user_id) { |
|
| 17 | + parent::__construct(Object_Type_Enum::USER, $user_id); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | function is_published() { |
@@ -25,11 +25,11 @@ discard block |
||
| 25 | 25 | return true; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - function set_values( $arr ) { |
|
| 28 | + function set_values($arr) { |
|
| 29 | 29 | // @@todo |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - function get_value( $key ) { |
|
| 32 | + function get_value($key) { |
|
| 33 | 33 | // @@todo |
| 34 | 34 | } |
| 35 | 35 | |
@@ -4,27 +4,27 @@ |
||
| 4 | 4 | |
| 5 | 5 | abstract class Abstract_Sync_Hooks { |
| 6 | 6 | |
| 7 | - private $queue = array(); |
|
| 7 | + private $queue = array(); |
|
| 8 | 8 | |
| 9 | - function __construct() { |
|
| 9 | + function __construct() { |
|
| 10 | 10 | |
| 11 | - // To sync at the end of the usual WordPress lifecycle |
|
| 12 | - add_action( 'shutdown', array( $this, 'shutdown' ) ); |
|
| 11 | + // To sync at the end of the usual WordPress lifecycle |
|
| 12 | + add_action( 'shutdown', array( $this, 'shutdown' ) ); |
|
| 13 | 13 | |
| 14 | - } |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - protected function enqueue( $item ) { |
|
| 16 | + protected function enqueue( $item ) { |
|
| 17 | 17 | |
| 18 | - if ( empty( $this->queue ) || $item !== $this->queue[ count( $this->queue ) - 1 ] ) { |
|
| 19 | - $this->queue[] = $item; |
|
| 20 | - } |
|
| 18 | + if ( empty( $this->queue ) || $item !== $this->queue[ count( $this->queue ) - 1 ] ) { |
|
| 19 | + $this->queue[] = $item; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - } |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - public function shutdown() { |
|
| 25 | - foreach ( $this->queue as $callback ) { |
|
| 26 | - call_user_func( array( $this, $callback[0] ), $callback[1] ); |
|
| 27 | - } |
|
| 28 | - } |
|
| 24 | + public function shutdown() { |
|
| 25 | + foreach ( $this->queue as $callback ) { |
|
| 26 | + call_user_func( array( $this, $callback[0] ), $callback[1] ); |
|
| 27 | + } |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | 30 | } |
@@ -9,21 +9,21 @@ |
||
| 9 | 9 | function __construct() { |
| 10 | 10 | |
| 11 | 11 | // To sync at the end of the usual WordPress lifecycle |
| 12 | - add_action( 'shutdown', array( $this, 'shutdown' ) ); |
|
| 12 | + add_action('shutdown', array($this, 'shutdown')); |
|
| 13 | 13 | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | - protected function enqueue( $item ) { |
|
| 16 | + protected function enqueue($item) { |
|
| 17 | 17 | |
| 18 | - if ( empty( $this->queue ) || $item !== $this->queue[ count( $this->queue ) - 1 ] ) { |
|
| 18 | + if (empty($this->queue) || $item !== $this->queue[count($this->queue) - 1]) { |
|
| 19 | 19 | $this->queue[] = $item; |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | public function shutdown() { |
| 25 | - foreach ( $this->queue as $callback ) { |
|
| 26 | - call_user_func( array( $this, $callback[0] ), $callback[1] ); |
|
| 25 | + foreach ($this->queue as $callback) { |
|
| 26 | + call_user_func(array($this, $callback[0]), $callback[1]); |
|
| 27 | 27 | } |
| 28 | 28 | } |
| 29 | 29 | |
@@ -7,45 +7,45 @@ |
||
| 7 | 7 | |
| 8 | 8 | abstract class Abstract_Sync_Object_Adapter implements Sync_Object_Adapter { |
| 9 | 9 | |
| 10 | - private $object_id; |
|
| 11 | - |
|
| 12 | - private $type; |
|
| 13 | - |
|
| 14 | - private static $meta_name = array( |
|
| 15 | - Object_Type_Enum::POST => 'post', |
|
| 16 | - Object_Type_Enum::TERM => 'term', |
|
| 17 | - Object_Type_Enum::USER => 'user', |
|
| 18 | - ); |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Sync_Object_Adapter constructor. |
|
| 22 | - * |
|
| 23 | - * @param int $type One of Object_Type_Enum. |
|
| 24 | - * @param int $object_id A post or term id. |
|
| 25 | - * @param Jsonld_Service |
|
| 26 | - * |
|
| 27 | - * @throws \Exception |
|
| 28 | - */ |
|
| 29 | - function __construct( $type, $object_id ) { |
|
| 30 | - |
|
| 31 | - $this->type = filter_var( $type, FILTER_VALIDATE_INT ); |
|
| 32 | - $this->object_id = filter_var( $object_id, FILTER_VALIDATE_INT ); |
|
| 33 | - |
|
| 34 | - if ( null === $this->type || ! isset( self::$meta_name[ $this->type ] ) ) { |
|
| 35 | - throw new \Exception( 'Invalid $type.' ); |
|
| 36 | - } |
|
| 37 | - if ( null === $this->object_id ) { |
|
| 38 | - throw new \Exception( 'Invalid $object.' ); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - function get_type() { |
|
| 44 | - return $this->type; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - function get_object_id() { |
|
| 48 | - return $this->object_id; |
|
| 49 | - } |
|
| 10 | + private $object_id; |
|
| 11 | + |
|
| 12 | + private $type; |
|
| 13 | + |
|
| 14 | + private static $meta_name = array( |
|
| 15 | + Object_Type_Enum::POST => 'post', |
|
| 16 | + Object_Type_Enum::TERM => 'term', |
|
| 17 | + Object_Type_Enum::USER => 'user', |
|
| 18 | + ); |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Sync_Object_Adapter constructor. |
|
| 22 | + * |
|
| 23 | + * @param int $type One of Object_Type_Enum. |
|
| 24 | + * @param int $object_id A post or term id. |
|
| 25 | + * @param Jsonld_Service |
|
| 26 | + * |
|
| 27 | + * @throws \Exception |
|
| 28 | + */ |
|
| 29 | + function __construct( $type, $object_id ) { |
|
| 30 | + |
|
| 31 | + $this->type = filter_var( $type, FILTER_VALIDATE_INT ); |
|
| 32 | + $this->object_id = filter_var( $object_id, FILTER_VALIDATE_INT ); |
|
| 33 | + |
|
| 34 | + if ( null === $this->type || ! isset( self::$meta_name[ $this->type ] ) ) { |
|
| 35 | + throw new \Exception( 'Invalid $type.' ); |
|
| 36 | + } |
|
| 37 | + if ( null === $this->object_id ) { |
|
| 38 | + throw new \Exception( 'Invalid $object.' ); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + function get_type() { |
|
| 44 | + return $this->type; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + function get_object_id() { |
|
| 48 | + return $this->object_id; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | } |
@@ -26,16 +26,16 @@ |
||
| 26 | 26 | * |
| 27 | 27 | * @throws \Exception |
| 28 | 28 | */ |
| 29 | - function __construct( $type, $object_id ) { |
|
| 29 | + function __construct($type, $object_id) { |
|
| 30 | 30 | |
| 31 | - $this->type = filter_var( $type, FILTER_VALIDATE_INT ); |
|
| 32 | - $this->object_id = filter_var( $object_id, FILTER_VALIDATE_INT ); |
|
| 31 | + $this->type = filter_var($type, FILTER_VALIDATE_INT); |
|
| 32 | + $this->object_id = filter_var($object_id, FILTER_VALIDATE_INT); |
|
| 33 | 33 | |
| 34 | - if ( null === $this->type || ! isset( self::$meta_name[ $this->type ] ) ) { |
|
| 35 | - throw new \Exception( 'Invalid $type.' ); |
|
| 34 | + if (null === $this->type || ! isset(self::$meta_name[$this->type])) { |
|
| 35 | + throw new \Exception('Invalid $type.'); |
|
| 36 | 36 | } |
| 37 | - if ( null === $this->object_id ) { |
|
| 38 | - throw new \Exception( 'Invalid $object.' ); |
|
| 37 | + if (null === $this->object_id) { |
|
| 38 | + throw new \Exception('Invalid $object.'); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | } |
@@ -7,100 +7,100 @@ |
||
| 7 | 7 | use Wordlift\Object_Type_Enum; |
| 8 | 8 | |
| 9 | 9 | class Sync_User_Hooks extends Abstract_Sync_Hooks { |
| 10 | - /** |
|
| 11 | - * @var \Wordlift_Log_Service |
|
| 12 | - */ |
|
| 13 | - private $log; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * @var Sync_Service |
|
| 17 | - */ |
|
| 18 | - private $sync_service; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Sync_User_Hooks constructor. |
|
| 22 | - * |
|
| 23 | - * @param Sync_Service $sync_service |
|
| 24 | - */ |
|
| 25 | - function __construct( Sync_Service $sync_service ) { |
|
| 26 | - parent::__construct(); |
|
| 27 | - |
|
| 28 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 29 | - |
|
| 30 | - $this->sync_service = $sync_service; |
|
| 31 | - |
|
| 32 | - $this->register_hooks(); |
|
| 33 | - |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - private function register_hooks() { |
|
| 37 | - /** |
|
| 38 | - * Register hooks for user and meta. |
|
| 39 | - */ |
|
| 40 | - add_action( 'user_register', array( $this, 'changed_user' ) ); |
|
| 41 | - add_action( 'profile_update', array( $this, 'changed_user' ) ); |
|
| 42 | - add_action( 'added_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 43 | - add_action( 'updated_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 44 | - add_action( 'deleted_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 45 | - add_action( 'delete_user', array( $this, 'delete_user' ) ); |
|
| 46 | - |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - public function changed_user( $user_id ) { |
|
| 50 | - |
|
| 51 | - $this->sync( $user_id ); |
|
| 52 | - |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function changed_user_meta( $meta_id, $user_id, $meta_key ) { |
|
| 56 | - |
|
| 57 | - if ( in_array( $meta_key, |
|
| 58 | - apply_filters( 'wl_dataset__sync_user_hooks__ignored_meta_keys', |
|
| 59 | - apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 60 | - array( |
|
| 61 | - 'rich_editing', |
|
| 62 | - 'comment_shortcuts', |
|
| 63 | - 'admin_color', |
|
| 64 | - 'use_ssl', |
|
| 65 | - 'show_admin_bar_front', |
|
| 66 | - 'wptests_capabilities', |
|
| 67 | - 'wptests_user_level', |
|
| 68 | - 'dismissed_wp_pointers', |
|
| 69 | - 'entity_url', |
|
| 70 | - ) ) ) ) ) { |
|
| 71 | - return; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - $this->sync( $user_id ); |
|
| 75 | - |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - private function sync( $user_id ) { |
|
| 79 | - $this->enqueue( array( 'do_sync', $user_id ) ); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function do_sync( $user_id ) { |
|
| 83 | - |
|
| 84 | - try { |
|
| 85 | - $this->sync_service->sync_one( Object_Type_Enum::USER, (int) $user_id ); |
|
| 86 | - } catch ( \Exception $e ) { |
|
| 87 | - $this->log->error( "An error occurred while trying to sync user $user_id: " . $e->getMessage(), $e ); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function delete_user( $user_id ) { |
|
| 93 | - $this->enqueue( array( 'do_delete', $user_id ) ); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - public function do_delete( $user_id ) { |
|
| 97 | - try { |
|
| 98 | - $this->sync_service->delete_one( Object_Type_Enum::USER, $user_id, |
|
| 99 | - Wordpress_User_Content_Legacy_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_user( $user_id ) ) ); |
|
| 100 | - } catch ( \Exception $e ) { |
|
| 101 | - $this->log->error( "An error occurred while trying to delete user $user_id: " . $e->getMessage(), $e ); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - } |
|
| 10 | + /** |
|
| 11 | + * @var \Wordlift_Log_Service |
|
| 12 | + */ |
|
| 13 | + private $log; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * @var Sync_Service |
|
| 17 | + */ |
|
| 18 | + private $sync_service; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Sync_User_Hooks constructor. |
|
| 22 | + * |
|
| 23 | + * @param Sync_Service $sync_service |
|
| 24 | + */ |
|
| 25 | + function __construct( Sync_Service $sync_service ) { |
|
| 26 | + parent::__construct(); |
|
| 27 | + |
|
| 28 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 29 | + |
|
| 30 | + $this->sync_service = $sync_service; |
|
| 31 | + |
|
| 32 | + $this->register_hooks(); |
|
| 33 | + |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + private function register_hooks() { |
|
| 37 | + /** |
|
| 38 | + * Register hooks for user and meta. |
|
| 39 | + */ |
|
| 40 | + add_action( 'user_register', array( $this, 'changed_user' ) ); |
|
| 41 | + add_action( 'profile_update', array( $this, 'changed_user' ) ); |
|
| 42 | + add_action( 'added_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 43 | + add_action( 'updated_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 44 | + add_action( 'deleted_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 45 | + add_action( 'delete_user', array( $this, 'delete_user' ) ); |
|
| 46 | + |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + public function changed_user( $user_id ) { |
|
| 50 | + |
|
| 51 | + $this->sync( $user_id ); |
|
| 52 | + |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function changed_user_meta( $meta_id, $user_id, $meta_key ) { |
|
| 56 | + |
|
| 57 | + if ( in_array( $meta_key, |
|
| 58 | + apply_filters( 'wl_dataset__sync_user_hooks__ignored_meta_keys', |
|
| 59 | + apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 60 | + array( |
|
| 61 | + 'rich_editing', |
|
| 62 | + 'comment_shortcuts', |
|
| 63 | + 'admin_color', |
|
| 64 | + 'use_ssl', |
|
| 65 | + 'show_admin_bar_front', |
|
| 66 | + 'wptests_capabilities', |
|
| 67 | + 'wptests_user_level', |
|
| 68 | + 'dismissed_wp_pointers', |
|
| 69 | + 'entity_url', |
|
| 70 | + ) ) ) ) ) { |
|
| 71 | + return; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + $this->sync( $user_id ); |
|
| 75 | + |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + private function sync( $user_id ) { |
|
| 79 | + $this->enqueue( array( 'do_sync', $user_id ) ); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function do_sync( $user_id ) { |
|
| 83 | + |
|
| 84 | + try { |
|
| 85 | + $this->sync_service->sync_one( Object_Type_Enum::USER, (int) $user_id ); |
|
| 86 | + } catch ( \Exception $e ) { |
|
| 87 | + $this->log->error( "An error occurred while trying to sync user $user_id: " . $e->getMessage(), $e ); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function delete_user( $user_id ) { |
|
| 93 | + $this->enqueue( array( 'do_delete', $user_id ) ); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + public function do_delete( $user_id ) { |
|
| 97 | + try { |
|
| 98 | + $this->sync_service->delete_one( Object_Type_Enum::USER, $user_id, |
|
| 99 | + Wordpress_User_Content_Legacy_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_user( $user_id ) ) ); |
|
| 100 | + } catch ( \Exception $e ) { |
|
| 101 | + $this->log->error( "An error occurred while trying to delete user $user_id: " . $e->getMessage(), $e ); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | 106 | } |
| 107 | 107 | \ No newline at end of file |
@@ -22,10 +22,10 @@ discard block |
||
| 22 | 22 | * |
| 23 | 23 | * @param Sync_Service $sync_service |
| 24 | 24 | */ |
| 25 | - function __construct( Sync_Service $sync_service ) { |
|
| 25 | + function __construct(Sync_Service $sync_service) { |
|
| 26 | 26 | parent::__construct(); |
| 27 | 27 | |
| 28 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 28 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 29 | 29 | |
| 30 | 30 | $this->sync_service = $sync_service; |
| 31 | 31 | |
@@ -37,26 +37,26 @@ discard block |
||
| 37 | 37 | /** |
| 38 | 38 | * Register hooks for user and meta. |
| 39 | 39 | */ |
| 40 | - add_action( 'user_register', array( $this, 'changed_user' ) ); |
|
| 41 | - add_action( 'profile_update', array( $this, 'changed_user' ) ); |
|
| 42 | - add_action( 'added_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 43 | - add_action( 'updated_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 44 | - add_action( 'deleted_user_meta', array( $this, 'changed_user_meta' ), 10, 3 ); |
|
| 45 | - add_action( 'delete_user', array( $this, 'delete_user' ) ); |
|
| 40 | + add_action('user_register', array($this, 'changed_user')); |
|
| 41 | + add_action('profile_update', array($this, 'changed_user')); |
|
| 42 | + add_action('added_user_meta', array($this, 'changed_user_meta'), 10, 3); |
|
| 43 | + add_action('updated_user_meta', array($this, 'changed_user_meta'), 10, 3); |
|
| 44 | + add_action('deleted_user_meta', array($this, 'changed_user_meta'), 10, 3); |
|
| 45 | + add_action('delete_user', array($this, 'delete_user')); |
|
| 46 | 46 | |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - public function changed_user( $user_id ) { |
|
| 49 | + public function changed_user($user_id) { |
|
| 50 | 50 | |
| 51 | - $this->sync( $user_id ); |
|
| 51 | + $this->sync($user_id); |
|
| 52 | 52 | |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - public function changed_user_meta( $meta_id, $user_id, $meta_key ) { |
|
| 55 | + public function changed_user_meta($meta_id, $user_id, $meta_key) { |
|
| 56 | 56 | |
| 57 | - if ( in_array( $meta_key, |
|
| 58 | - apply_filters( 'wl_dataset__sync_user_hooks__ignored_meta_keys', |
|
| 59 | - apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 57 | + if (in_array($meta_key, |
|
| 58 | + apply_filters('wl_dataset__sync_user_hooks__ignored_meta_keys', |
|
| 59 | + apply_filters('wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 60 | 60 | array( |
| 61 | 61 | 'rich_editing', |
| 62 | 62 | 'comment_shortcuts', |
@@ -67,38 +67,38 @@ discard block |
||
| 67 | 67 | 'wptests_user_level', |
| 68 | 68 | 'dismissed_wp_pointers', |
| 69 | 69 | 'entity_url', |
| 70 | - ) ) ) ) ) { |
|
| 70 | + ))))) { |
|
| 71 | 71 | return; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - $this->sync( $user_id ); |
|
| 74 | + $this->sync($user_id); |
|
| 75 | 75 | |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - private function sync( $user_id ) { |
|
| 79 | - $this->enqueue( array( 'do_sync', $user_id ) ); |
|
| 78 | + private function sync($user_id) { |
|
| 79 | + $this->enqueue(array('do_sync', $user_id)); |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - public function do_sync( $user_id ) { |
|
| 82 | + public function do_sync($user_id) { |
|
| 83 | 83 | |
| 84 | 84 | try { |
| 85 | - $this->sync_service->sync_one( Object_Type_Enum::USER, (int) $user_id ); |
|
| 86 | - } catch ( \Exception $e ) { |
|
| 87 | - $this->log->error( "An error occurred while trying to sync user $user_id: " . $e->getMessage(), $e ); |
|
| 85 | + $this->sync_service->sync_one(Object_Type_Enum::USER, (int) $user_id); |
|
| 86 | + } catch (\Exception $e) { |
|
| 87 | + $this->log->error("An error occurred while trying to sync user $user_id: ".$e->getMessage(), $e); |
|
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - public function delete_user( $user_id ) { |
|
| 93 | - $this->enqueue( array( 'do_delete', $user_id ) ); |
|
| 92 | + public function delete_user($user_id) { |
|
| 93 | + $this->enqueue(array('do_delete', $user_id)); |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - public function do_delete( $user_id ) { |
|
| 96 | + public function do_delete($user_id) { |
|
| 97 | 97 | try { |
| 98 | - $this->sync_service->delete_one( Object_Type_Enum::USER, $user_id, |
|
| 99 | - Wordpress_User_Content_Legacy_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_user( $user_id ) ) ); |
|
| 100 | - } catch ( \Exception $e ) { |
|
| 101 | - $this->log->error( "An error occurred while trying to delete user $user_id: " . $e->getMessage(), $e ); |
|
| 98 | + $this->sync_service->delete_one(Object_Type_Enum::USER, $user_id, |
|
| 99 | + Wordpress_User_Content_Legacy_Service::get_instance()->get_entity_id(Wordpress_Content_Id::create_user($user_id))); |
|
| 100 | + } catch (\Exception $e) { |
|
| 101 | + $this->log->error("An error occurred while trying to delete user $user_id: ".$e->getMessage(), $e); |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | } |
@@ -8,247 +8,247 @@ |
||
| 8 | 8 | use Wordlift\Object_Type_Enum; |
| 9 | 9 | |
| 10 | 10 | class Sync_Service { |
| 11 | - const JSONLD_HASH = 'jsonld_hash'; |
|
| 12 | - const SYNCED_GMT = 'synced_gmt'; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * @var \Wordlift_Log_Service |
|
| 16 | - */ |
|
| 17 | - private $log; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @var Api_Service |
|
| 21 | - */ |
|
| 22 | - private $api_service; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var Jsonld_Service |
|
| 26 | - */ |
|
| 27 | - private $jsonld_service; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var Sync_Background_Process |
|
| 31 | - */ |
|
| 32 | - private $sync_background_process; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The number of posts processed in one call. |
|
| 36 | - * |
|
| 37 | - * @var int The batch size. |
|
| 38 | - */ |
|
| 39 | - private $batch_size; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var Sync_Object_Adapter_Factory |
|
| 43 | - */ |
|
| 44 | - private $sync_object_adapter_factory; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var Sync_Service |
|
| 48 | - */ |
|
| 49 | - private static $instance; |
|
| 50 | - private $entity_service; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Constructor. |
|
| 54 | - * |
|
| 55 | - * @param Api_Service $api_service The {@link Api_Service} used to communicate with the remote APIs. |
|
| 56 | - * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 57 | - * @param Jsonld_Service $jsonld_service |
|
| 58 | - * @param \Wordlift_Entity_Service $entity_service |
|
| 59 | - */ |
|
| 60 | - public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) { |
|
| 61 | - |
|
| 62 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 63 | - |
|
| 64 | - $this->api_service = $api_service; |
|
| 65 | - $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 66 | - $this->jsonld_service = $jsonld_service; |
|
| 67 | - $this->entity_service = $entity_service; |
|
| 68 | - $this->batch_size = 10; |
|
| 69 | - |
|
| 70 | - // You need to initialize this early, otherwise the Background Process isn't registered in AJAX calls. |
|
| 11 | + const JSONLD_HASH = 'jsonld_hash'; |
|
| 12 | + const SYNCED_GMT = 'synced_gmt'; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * @var \Wordlift_Log_Service |
|
| 16 | + */ |
|
| 17 | + private $log; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @var Api_Service |
|
| 21 | + */ |
|
| 22 | + private $api_service; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var Jsonld_Service |
|
| 26 | + */ |
|
| 27 | + private $jsonld_service; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var Sync_Background_Process |
|
| 31 | + */ |
|
| 32 | + private $sync_background_process; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The number of posts processed in one call. |
|
| 36 | + * |
|
| 37 | + * @var int The batch size. |
|
| 38 | + */ |
|
| 39 | + private $batch_size; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var Sync_Object_Adapter_Factory |
|
| 43 | + */ |
|
| 44 | + private $sync_object_adapter_factory; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var Sync_Service |
|
| 48 | + */ |
|
| 49 | + private static $instance; |
|
| 50 | + private $entity_service; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Constructor. |
|
| 54 | + * |
|
| 55 | + * @param Api_Service $api_service The {@link Api_Service} used to communicate with the remote APIs. |
|
| 56 | + * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 57 | + * @param Jsonld_Service $jsonld_service |
|
| 58 | + * @param \Wordlift_Entity_Service $entity_service |
|
| 59 | + */ |
|
| 60 | + public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) { |
|
| 61 | + |
|
| 62 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 63 | + |
|
| 64 | + $this->api_service = $api_service; |
|
| 65 | + $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 66 | + $this->jsonld_service = $jsonld_service; |
|
| 67 | + $this->entity_service = $entity_service; |
|
| 68 | + $this->batch_size = 10; |
|
| 69 | + |
|
| 70 | + // You need to initialize this early, otherwise the Background Process isn't registered in AJAX calls. |
|
| 71 | 71 | // $this->sync_background_process = new Sync_Background_Process( $this );; |
| 72 | 72 | |
| 73 | - // Exclude the JSONLD_HASH meta key from those that require a resync. |
|
| 74 | - add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) { |
|
| 75 | - $args[] = Sync_Service::JSONLD_HASH; |
|
| 76 | - $args[] = Sync_Service::SYNCED_GMT; |
|
| 77 | - |
|
| 78 | - return $args; |
|
| 79 | - } ); |
|
| 80 | - |
|
| 81 | - self::$instance = $this; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public static function get_instance() { |
|
| 85 | - return self::$instance; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param int $type |
|
| 90 | - * @param int $object_id |
|
| 91 | - * |
|
| 92 | - * @return array|false |
|
| 93 | - * @throws Exception |
|
| 94 | - */ |
|
| 95 | - public function sync_one( $type, $object_id ) { |
|
| 96 | - |
|
| 97 | - $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 98 | - |
|
| 99 | - return $this->sync_many( array( $object ) ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param $type string Post or User. |
|
| 104 | - * @param $object_id int Post or User id |
|
| 105 | - * @param $uri string Entity uri , This needs to be supplied before deletion, if we |
|
| 106 | - * get it from meta it might not be available. |
|
| 107 | - * |
|
| 108 | - * @return bool |
|
| 109 | - */ |
|
| 110 | - public function delete_one( $type, $object_id, $uri ) { |
|
| 111 | - // Entity URL isn't set, bail out. |
|
| 112 | - if ( empty( $uri ) ) { |
|
| 113 | - return false; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $response = $this->api_service->request( |
|
| 117 | - 'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) ); |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - // Update the sync date in case of success, otherwise log an error. |
|
| 121 | - if ( ! $response->is_success() ) { |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Allow 3rd parties to run additional sync work. |
|
| 127 | - */ |
|
| 128 | - do_action( 'wl_sync__delete_one', $type, $object_id, $uri ); |
|
| 129 | - |
|
| 130 | - return true; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @param Sync_Object_Adapter[] $objects |
|
| 135 | - * @param bool $force Force synchronization even if the json-ld hash hasn't changed. |
|
| 136 | - * |
|
| 137 | - * @return bool |
|
| 138 | - * @throws Exception |
|
| 139 | - */ |
|
| 140 | - public function sync_many( $objects, $force = false ) { |
|
| 141 | - |
|
| 142 | - $hashes = array(); |
|
| 143 | - $payloads = array(); |
|
| 144 | - /** @var Sync_Object_Adapter $object */ |
|
| 145 | - foreach ( $objects as $object ) { |
|
| 146 | - // Bail out if no payload. |
|
| 147 | - $payload_as_string = $this->get_payload_as_string( $object ); |
|
| 148 | - if ( empty( $payload_as_string ) ) { |
|
| 149 | - continue; |
|
| 150 | - } |
|
| 151 | - $new_hash = sha1( $payload_as_string ); |
|
| 152 | - $old_hash = $object->get_value( self::JSONLD_HASH ); |
|
| 153 | - |
|
| 154 | - // JSON-LD hasn't changed, bail out. |
|
| 155 | - $should_sync = $force || $new_hash !== $old_hash; |
|
| 156 | - if ( ! apply_filters( 'wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string ) ) { |
|
| 157 | - continue; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - // Collect the hashes and the payloads. |
|
| 161 | - $hashes[] = array( $object, $new_hash, $payload_as_string ); |
|
| 162 | - $payloads[] = $payload_as_string; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - // Bail out if payloads are empty. |
|
| 166 | - if ( empty( $payloads ) ) { |
|
| 167 | - return false; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $response = $this->api_service->request( |
|
| 171 | - 'POST', '/middleware/dataset/batch', |
|
| 172 | - array( 'Content-Type' => 'application/json', ), |
|
| 173 | - // Put the payload in a JSON array w/o decoding/encoding again. |
|
| 174 | - '[ ' . implode( ', ', $payloads ) . ' ]' ); |
|
| 175 | - |
|
| 176 | - // Update the sync date in case of success, otherwise log an error. |
|
| 177 | - if ( ! $response->is_success() ) { |
|
| 178 | - return false; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // If successful update the hashes and sync'ed datetime. |
|
| 182 | - foreach ( $hashes as $hash ) { |
|
| 183 | - $object = $hash[0]; |
|
| 184 | - $new_hash = $hash[1]; |
|
| 185 | - |
|
| 186 | - $object->set_values( array( |
|
| 187 | - self::JSONLD_HASH => $new_hash, |
|
| 188 | - self::SYNCED_GMT => current_time( 'mysql', true ), |
|
| 189 | - ) ); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Allow 3rd parties to run additional sync work. |
|
| 195 | - */ |
|
| 196 | - do_action( 'wl_sync__sync_many', $hashes ); |
|
| 197 | - |
|
| 198 | - return true; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * @param Sync_Object_Adapter $object |
|
| 203 | - * |
|
| 204 | - * @return false|string |
|
| 205 | - * @throws Exception |
|
| 206 | - */ |
|
| 207 | - private function get_payload_as_string( $object ) { |
|
| 208 | - $type = $object->get_type(); |
|
| 209 | - $object_id = $object->get_object_id(); |
|
| 210 | - $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 211 | - $this->jsonld_service->get( $type, $object_id ), $type, $object_id ) ); |
|
| 212 | - $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 213 | - |
|
| 214 | - // Entity URL isn't set, bail out. |
|
| 215 | - if ( empty( $uri ) ) { |
|
| 216 | - return false; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - return wp_json_encode( array( |
|
| 220 | - 'uri' => $uri, |
|
| 221 | - 'model' => $jsonld_as_string, |
|
| 222 | - 'private' => ! $object->is_public(), |
|
| 223 | - ) ); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @param $post_id |
|
| 228 | - * |
|
| 229 | - * @todo Complete the delete item. |
|
| 230 | - */ |
|
| 231 | - public function delete_item( $post_id ) { |
|
| 232 | - $uri = $this->entity_service->get_uri( $post_id, Object_Type_Enum::POST ); |
|
| 233 | - |
|
| 234 | - if ( ! isset( $uri ) ) { |
|
| 235 | - return; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - // Make a request to the remote endpoint. |
|
| 239 | - $response = $this->api_service->request( |
|
| 240 | - 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 241 | - array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 242 | - |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - public function get_batch_size() { |
|
| 246 | - |
|
| 247 | - return $this->batch_size; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - public function delete_all() { |
|
| 251 | - $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 252 | - } |
|
| 73 | + // Exclude the JSONLD_HASH meta key from those that require a resync. |
|
| 74 | + add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) { |
|
| 75 | + $args[] = Sync_Service::JSONLD_HASH; |
|
| 76 | + $args[] = Sync_Service::SYNCED_GMT; |
|
| 77 | + |
|
| 78 | + return $args; |
|
| 79 | + } ); |
|
| 80 | + |
|
| 81 | + self::$instance = $this; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public static function get_instance() { |
|
| 85 | + return self::$instance; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param int $type |
|
| 90 | + * @param int $object_id |
|
| 91 | + * |
|
| 92 | + * @return array|false |
|
| 93 | + * @throws Exception |
|
| 94 | + */ |
|
| 95 | + public function sync_one( $type, $object_id ) { |
|
| 96 | + |
|
| 97 | + $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 98 | + |
|
| 99 | + return $this->sync_many( array( $object ) ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param $type string Post or User. |
|
| 104 | + * @param $object_id int Post or User id |
|
| 105 | + * @param $uri string Entity uri , This needs to be supplied before deletion, if we |
|
| 106 | + * get it from meta it might not be available. |
|
| 107 | + * |
|
| 108 | + * @return bool |
|
| 109 | + */ |
|
| 110 | + public function delete_one( $type, $object_id, $uri ) { |
|
| 111 | + // Entity URL isn't set, bail out. |
|
| 112 | + if ( empty( $uri ) ) { |
|
| 113 | + return false; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $response = $this->api_service->request( |
|
| 117 | + 'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) ); |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + // Update the sync date in case of success, otherwise log an error. |
|
| 121 | + if ( ! $response->is_success() ) { |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Allow 3rd parties to run additional sync work. |
|
| 127 | + */ |
|
| 128 | + do_action( 'wl_sync__delete_one', $type, $object_id, $uri ); |
|
| 129 | + |
|
| 130 | + return true; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @param Sync_Object_Adapter[] $objects |
|
| 135 | + * @param bool $force Force synchronization even if the json-ld hash hasn't changed. |
|
| 136 | + * |
|
| 137 | + * @return bool |
|
| 138 | + * @throws Exception |
|
| 139 | + */ |
|
| 140 | + public function sync_many( $objects, $force = false ) { |
|
| 141 | + |
|
| 142 | + $hashes = array(); |
|
| 143 | + $payloads = array(); |
|
| 144 | + /** @var Sync_Object_Adapter $object */ |
|
| 145 | + foreach ( $objects as $object ) { |
|
| 146 | + // Bail out if no payload. |
|
| 147 | + $payload_as_string = $this->get_payload_as_string( $object ); |
|
| 148 | + if ( empty( $payload_as_string ) ) { |
|
| 149 | + continue; |
|
| 150 | + } |
|
| 151 | + $new_hash = sha1( $payload_as_string ); |
|
| 152 | + $old_hash = $object->get_value( self::JSONLD_HASH ); |
|
| 153 | + |
|
| 154 | + // JSON-LD hasn't changed, bail out. |
|
| 155 | + $should_sync = $force || $new_hash !== $old_hash; |
|
| 156 | + if ( ! apply_filters( 'wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string ) ) { |
|
| 157 | + continue; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + // Collect the hashes and the payloads. |
|
| 161 | + $hashes[] = array( $object, $new_hash, $payload_as_string ); |
|
| 162 | + $payloads[] = $payload_as_string; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + // Bail out if payloads are empty. |
|
| 166 | + if ( empty( $payloads ) ) { |
|
| 167 | + return false; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $response = $this->api_service->request( |
|
| 171 | + 'POST', '/middleware/dataset/batch', |
|
| 172 | + array( 'Content-Type' => 'application/json', ), |
|
| 173 | + // Put the payload in a JSON array w/o decoding/encoding again. |
|
| 174 | + '[ ' . implode( ', ', $payloads ) . ' ]' ); |
|
| 175 | + |
|
| 176 | + // Update the sync date in case of success, otherwise log an error. |
|
| 177 | + if ( ! $response->is_success() ) { |
|
| 178 | + return false; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // If successful update the hashes and sync'ed datetime. |
|
| 182 | + foreach ( $hashes as $hash ) { |
|
| 183 | + $object = $hash[0]; |
|
| 184 | + $new_hash = $hash[1]; |
|
| 185 | + |
|
| 186 | + $object->set_values( array( |
|
| 187 | + self::JSONLD_HASH => $new_hash, |
|
| 188 | + self::SYNCED_GMT => current_time( 'mysql', true ), |
|
| 189 | + ) ); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Allow 3rd parties to run additional sync work. |
|
| 195 | + */ |
|
| 196 | + do_action( 'wl_sync__sync_many', $hashes ); |
|
| 197 | + |
|
| 198 | + return true; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * @param Sync_Object_Adapter $object |
|
| 203 | + * |
|
| 204 | + * @return false|string |
|
| 205 | + * @throws Exception |
|
| 206 | + */ |
|
| 207 | + private function get_payload_as_string( $object ) { |
|
| 208 | + $type = $object->get_type(); |
|
| 209 | + $object_id = $object->get_object_id(); |
|
| 210 | + $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 211 | + $this->jsonld_service->get( $type, $object_id ), $type, $object_id ) ); |
|
| 212 | + $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 213 | + |
|
| 214 | + // Entity URL isn't set, bail out. |
|
| 215 | + if ( empty( $uri ) ) { |
|
| 216 | + return false; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + return wp_json_encode( array( |
|
| 220 | + 'uri' => $uri, |
|
| 221 | + 'model' => $jsonld_as_string, |
|
| 222 | + 'private' => ! $object->is_public(), |
|
| 223 | + ) ); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @param $post_id |
|
| 228 | + * |
|
| 229 | + * @todo Complete the delete item. |
|
| 230 | + */ |
|
| 231 | + public function delete_item( $post_id ) { |
|
| 232 | + $uri = $this->entity_service->get_uri( $post_id, Object_Type_Enum::POST ); |
|
| 233 | + |
|
| 234 | + if ( ! isset( $uri ) ) { |
|
| 235 | + return; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + // Make a request to the remote endpoint. |
|
| 239 | + $response = $this->api_service->request( |
|
| 240 | + 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 241 | + array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 242 | + |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + public function get_batch_size() { |
|
| 246 | + |
|
| 247 | + return $this->batch_size; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + public function delete_all() { |
|
| 251 | + $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | 254 | } |
@@ -57,9 +57,9 @@ discard block |
||
| 57 | 57 | * @param Jsonld_Service $jsonld_service |
| 58 | 58 | * @param \Wordlift_Entity_Service $entity_service |
| 59 | 59 | */ |
| 60 | - public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) { |
|
| 60 | + public function __construct($api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service) { |
|
| 61 | 61 | |
| 62 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 62 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 63 | 63 | |
| 64 | 64 | $this->api_service = $api_service; |
| 65 | 65 | $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | // $this->sync_background_process = new Sync_Background_Process( $this );; |
| 72 | 72 | |
| 73 | 73 | // Exclude the JSONLD_HASH meta key from those that require a resync. |
| 74 | - add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) { |
|
| 74 | + add_filter('wl_dataset__sync_hooks__ignored_meta_keys', function($args) { |
|
| 75 | 75 | $args[] = Sync_Service::JSONLD_HASH; |
| 76 | 76 | $args[] = Sync_Service::SYNCED_GMT; |
| 77 | 77 | |
@@ -92,11 +92,11 @@ discard block |
||
| 92 | 92 | * @return array|false |
| 93 | 93 | * @throws Exception |
| 94 | 94 | */ |
| 95 | - public function sync_one( $type, $object_id ) { |
|
| 95 | + public function sync_one($type, $object_id) { |
|
| 96 | 96 | |
| 97 | - $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 97 | + $object = $this->sync_object_adapter_factory->create($type, $object_id); |
|
| 98 | 98 | |
| 99 | - return $this->sync_many( array( $object ) ); |
|
| 99 | + return $this->sync_many(array($object)); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
@@ -107,25 +107,25 @@ discard block |
||
| 107 | 107 | * |
| 108 | 108 | * @return bool |
| 109 | 109 | */ |
| 110 | - public function delete_one( $type, $object_id, $uri ) { |
|
| 110 | + public function delete_one($type, $object_id, $uri) { |
|
| 111 | 111 | // Entity URL isn't set, bail out. |
| 112 | - if ( empty( $uri ) ) { |
|
| 112 | + if (empty($uri)) { |
|
| 113 | 113 | return false; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | $response = $this->api_service->request( |
| 117 | - 'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) ); |
|
| 117 | + 'DELETE', sprintf('/middleware/dataset?uri=%s', rawurlencode($uri)) ); |
|
| 118 | 118 | |
| 119 | 119 | |
| 120 | 120 | // Update the sync date in case of success, otherwise log an error. |
| 121 | - if ( ! $response->is_success() ) { |
|
| 121 | + if ( ! $response->is_success()) { |
|
| 122 | 122 | return false; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
| 126 | 126 | * Allow 3rd parties to run additional sync work. |
| 127 | 127 | */ |
| 128 | - do_action( 'wl_sync__delete_one', $type, $object_id, $uri ); |
|
| 128 | + do_action('wl_sync__delete_one', $type, $object_id, $uri); |
|
| 129 | 129 | |
| 130 | 130 | return true; |
| 131 | 131 | } |
@@ -137,63 +137,63 @@ discard block |
||
| 137 | 137 | * @return bool |
| 138 | 138 | * @throws Exception |
| 139 | 139 | */ |
| 140 | - public function sync_many( $objects, $force = false ) { |
|
| 140 | + public function sync_many($objects, $force = false) { |
|
| 141 | 141 | |
| 142 | 142 | $hashes = array(); |
| 143 | 143 | $payloads = array(); |
| 144 | 144 | /** @var Sync_Object_Adapter $object */ |
| 145 | - foreach ( $objects as $object ) { |
|
| 145 | + foreach ($objects as $object) { |
|
| 146 | 146 | // Bail out if no payload. |
| 147 | - $payload_as_string = $this->get_payload_as_string( $object ); |
|
| 148 | - if ( empty( $payload_as_string ) ) { |
|
| 147 | + $payload_as_string = $this->get_payload_as_string($object); |
|
| 148 | + if (empty($payload_as_string)) { |
|
| 149 | 149 | continue; |
| 150 | 150 | } |
| 151 | - $new_hash = sha1( $payload_as_string ); |
|
| 152 | - $old_hash = $object->get_value( self::JSONLD_HASH ); |
|
| 151 | + $new_hash = sha1($payload_as_string); |
|
| 152 | + $old_hash = $object->get_value(self::JSONLD_HASH); |
|
| 153 | 153 | |
| 154 | 154 | // JSON-LD hasn't changed, bail out. |
| 155 | 155 | $should_sync = $force || $new_hash !== $old_hash; |
| 156 | - if ( ! apply_filters( 'wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string ) ) { |
|
| 156 | + if ( ! apply_filters('wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string)) { |
|
| 157 | 157 | continue; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // Collect the hashes and the payloads. |
| 161 | - $hashes[] = array( $object, $new_hash, $payload_as_string ); |
|
| 161 | + $hashes[] = array($object, $new_hash, $payload_as_string); |
|
| 162 | 162 | $payloads[] = $payload_as_string; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | // Bail out if payloads are empty. |
| 166 | - if ( empty( $payloads ) ) { |
|
| 166 | + if (empty($payloads)) { |
|
| 167 | 167 | return false; |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | $response = $this->api_service->request( |
| 171 | 171 | 'POST', '/middleware/dataset/batch', |
| 172 | - array( 'Content-Type' => 'application/json', ), |
|
| 172 | + array('Content-Type' => 'application/json',), |
|
| 173 | 173 | // Put the payload in a JSON array w/o decoding/encoding again. |
| 174 | - '[ ' . implode( ', ', $payloads ) . ' ]' ); |
|
| 174 | + '[ '.implode(', ', $payloads).' ]' ); |
|
| 175 | 175 | |
| 176 | 176 | // Update the sync date in case of success, otherwise log an error. |
| 177 | - if ( ! $response->is_success() ) { |
|
| 177 | + if ( ! $response->is_success()) { |
|
| 178 | 178 | return false; |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | // If successful update the hashes and sync'ed datetime. |
| 182 | - foreach ( $hashes as $hash ) { |
|
| 182 | + foreach ($hashes as $hash) { |
|
| 183 | 183 | $object = $hash[0]; |
| 184 | 184 | $new_hash = $hash[1]; |
| 185 | 185 | |
| 186 | - $object->set_values( array( |
|
| 186 | + $object->set_values(array( |
|
| 187 | 187 | self::JSONLD_HASH => $new_hash, |
| 188 | - self::SYNCED_GMT => current_time( 'mysql', true ), |
|
| 189 | - ) ); |
|
| 188 | + self::SYNCED_GMT => current_time('mysql', true), |
|
| 189 | + )); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | |
| 193 | 193 | /** |
| 194 | 194 | * Allow 3rd parties to run additional sync work. |
| 195 | 195 | */ |
| 196 | - do_action( 'wl_sync__sync_many', $hashes ); |
|
| 196 | + do_action('wl_sync__sync_many', $hashes); |
|
| 197 | 197 | |
| 198 | 198 | return true; |
| 199 | 199 | } |
@@ -204,23 +204,23 @@ discard block |
||
| 204 | 204 | * @return false|string |
| 205 | 205 | * @throws Exception |
| 206 | 206 | */ |
| 207 | - private function get_payload_as_string( $object ) { |
|
| 207 | + private function get_payload_as_string($object) { |
|
| 208 | 208 | $type = $object->get_type(); |
| 209 | 209 | $object_id = $object->get_object_id(); |
| 210 | - $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 211 | - $this->jsonld_service->get( $type, $object_id ), $type, $object_id ) ); |
|
| 212 | - $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 210 | + $jsonld_as_string = wp_json_encode(apply_filters('wl_dataset__sync_service__sync_item__jsonld', |
|
| 211 | + $this->jsonld_service->get($type, $object_id), $type, $object_id)); |
|
| 212 | + $uri = $this->entity_service->get_uri($object_id, $type); |
|
| 213 | 213 | |
| 214 | 214 | // Entity URL isn't set, bail out. |
| 215 | - if ( empty( $uri ) ) { |
|
| 215 | + if (empty($uri)) { |
|
| 216 | 216 | return false; |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | - return wp_json_encode( array( |
|
| 219 | + return wp_json_encode(array( |
|
| 220 | 220 | 'uri' => $uri, |
| 221 | 221 | 'model' => $jsonld_as_string, |
| 222 | 222 | 'private' => ! $object->is_public(), |
| 223 | - ) ); |
|
| 223 | + )); |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | /** |
@@ -228,17 +228,17 @@ discard block |
||
| 228 | 228 | * |
| 229 | 229 | * @todo Complete the delete item. |
| 230 | 230 | */ |
| 231 | - public function delete_item( $post_id ) { |
|
| 232 | - $uri = $this->entity_service->get_uri( $post_id, Object_Type_Enum::POST ); |
|
| 231 | + public function delete_item($post_id) { |
|
| 232 | + $uri = $this->entity_service->get_uri($post_id, Object_Type_Enum::POST); |
|
| 233 | 233 | |
| 234 | - if ( ! isset( $uri ) ) { |
|
| 234 | + if ( ! isset($uri)) { |
|
| 235 | 235 | return; |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | // Make a request to the remote endpoint. |
| 239 | 239 | $response = $this->api_service->request( |
| 240 | - 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 241 | - array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 240 | + 'DELETE', '/middleware/dataset?uri='.rawurlencode($uri), |
|
| 241 | + array('Content-Type' => 'application/ld+json',) ); |
|
| 242 | 242 | |
| 243 | 243 | } |
| 244 | 244 | |
@@ -248,7 +248,7 @@ discard block |
||
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | public function delete_all() { |
| 251 | - $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 251 | + $this->api_service->request('DELETE', '/middleware/dataset/all'); |
|
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | } |
@@ -7,119 +7,119 @@ |
||
| 7 | 7 | use Wordlift\Object_Type_Enum; |
| 8 | 8 | |
| 9 | 9 | class Sync_Term_Hooks extends Abstract_Sync_Hooks { |
| 10 | - /** |
|
| 11 | - * @var \Wordlift_Log_Service |
|
| 12 | - */ |
|
| 13 | - private $log; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * @var Sync_Service |
|
| 17 | - */ |
|
| 18 | - private $sync_service; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @var Sync_Object_Adapter_Factory |
|
| 22 | - */ |
|
| 23 | - private $sync_object_factory; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Sync_Term_Hooks constructor. |
|
| 27 | - * |
|
| 28 | - * @param Sync_Service $sync_service |
|
| 29 | - * @param Sync_Object_Adapter_Factory $sync_object_factory |
|
| 30 | - */ |
|
| 31 | - function __construct( $sync_service, $sync_object_factory ) { |
|
| 32 | - parent::__construct(); |
|
| 33 | - |
|
| 34 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 35 | - |
|
| 36 | - $this->sync_service = $sync_service; |
|
| 37 | - $this->sync_object_factory = $sync_object_factory; |
|
| 38 | - |
|
| 39 | - $this->register_hooks(); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - private function register_hooks() { |
|
| 43 | - /** |
|
| 44 | - * Register hooks for post and meta. |
|
| 45 | - */ |
|
| 46 | - add_action( 'create_term', array( $this, 'do_sync' ) ); |
|
| 47 | - add_action( 'edit_term', array( $this, 'do_sync' ) ); |
|
| 48 | - add_action( 'added_term_meta', array( $this, 'changed_term_meta' ), 10, 4 ); |
|
| 49 | - add_action( 'updated_term_meta', array( $this, 'changed_term_meta' ), 10, 4 ); |
|
| 50 | - add_action( 'deleted_term_meta', array( $this, 'changed_term_meta' ), 10, 4 ); |
|
| 51 | - add_action( 'pre_delete_term', array( $this, 'delete_term' ) ); |
|
| 52 | - |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function saved_term( $term_id ) { |
|
| 56 | - |
|
| 57 | - // Sync all the terms without filtering. |
|
| 58 | - $this->sync( $term_id ); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - public function changed_term_meta( $meta_id, $term_id, $meta_key, $_meta_value ) { |
|
| 62 | - |
|
| 63 | - if ( in_array( $meta_key, |
|
| 64 | - apply_filters( 'wl_dataset__sync_post_hooks__ignored_meta_keys', |
|
| 65 | - apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 66 | - array( |
|
| 67 | - '_pingme', |
|
| 68 | - '_encloseme', |
|
| 69 | - 'entity_url', |
|
| 70 | - ) ) ) ) |
|
| 71 | - ) { |
|
| 72 | - return; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - $this->sync( $term_id ); |
|
| 76 | - |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - private function sync( $term_id ) { |
|
| 80 | - |
|
| 81 | - $this->enqueue( array( 'do_sync', $term_id ) ); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function do_sync( $term_id ) { |
|
| 85 | - |
|
| 86 | - try { |
|
| 87 | - $term = get_term( $term_id ); |
|
| 88 | - if ( ! isset( $term ) ) { |
|
| 89 | - return; |
|
| 90 | - } |
|
| 91 | - $this->sync_service->sync_many( array( |
|
| 92 | - $this->sync_object_factory->create( Object_Type_Enum::TERM, $term_id ), |
|
| 93 | - ) ); |
|
| 94 | - } catch ( \Exception $e ) { |
|
| 95 | - $this->log->error( "An error occurred while trying to sync post $term_id: " . $e->getMessage(), $e ); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @param $term \WP_Term |
|
| 101 | - * |
|
| 102 | - * @throws \Exception |
|
| 103 | - */ |
|
| 104 | - public function delete_term( $term_id ) { |
|
| 105 | - $args = array( |
|
| 106 | - $term_id, |
|
| 107 | - Wordpress_Term_Content_Legacy_Service |
|
| 108 | - ::get_instance() |
|
| 109 | - ->get_entity_id( Wordpress_Content_Id::create_term( $term_id ) ) |
|
| 110 | - ); |
|
| 111 | - // We can't postpone the execution for a `delete` because we would miss the actual data. |
|
| 112 | - $this->do_delete( $args ); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - public function do_delete( $args ) { |
|
| 116 | - $term_id = $args[0]; |
|
| 117 | - $term_entity_uri = $args[1]; |
|
| 118 | - |
|
| 119 | - try { |
|
| 120 | - $this->sync_service->delete_one( Object_Type_Enum::TERM, $term_id, $term_entity_uri ); |
|
| 121 | - } catch ( \Exception $e ) { |
|
| 122 | - $this->log->error( "An error occurred while trying to delete term $term_id: " . $e->getMessage(), $e ); |
|
| 123 | - } |
|
| 124 | - } |
|
| 10 | + /** |
|
| 11 | + * @var \Wordlift_Log_Service |
|
| 12 | + */ |
|
| 13 | + private $log; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * @var Sync_Service |
|
| 17 | + */ |
|
| 18 | + private $sync_service; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @var Sync_Object_Adapter_Factory |
|
| 22 | + */ |
|
| 23 | + private $sync_object_factory; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Sync_Term_Hooks constructor. |
|
| 27 | + * |
|
| 28 | + * @param Sync_Service $sync_service |
|
| 29 | + * @param Sync_Object_Adapter_Factory $sync_object_factory |
|
| 30 | + */ |
|
| 31 | + function __construct( $sync_service, $sync_object_factory ) { |
|
| 32 | + parent::__construct(); |
|
| 33 | + |
|
| 34 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 35 | + |
|
| 36 | + $this->sync_service = $sync_service; |
|
| 37 | + $this->sync_object_factory = $sync_object_factory; |
|
| 38 | + |
|
| 39 | + $this->register_hooks(); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + private function register_hooks() { |
|
| 43 | + /** |
|
| 44 | + * Register hooks for post and meta. |
|
| 45 | + */ |
|
| 46 | + add_action( 'create_term', array( $this, 'do_sync' ) ); |
|
| 47 | + add_action( 'edit_term', array( $this, 'do_sync' ) ); |
|
| 48 | + add_action( 'added_term_meta', array( $this, 'changed_term_meta' ), 10, 4 ); |
|
| 49 | + add_action( 'updated_term_meta', array( $this, 'changed_term_meta' ), 10, 4 ); |
|
| 50 | + add_action( 'deleted_term_meta', array( $this, 'changed_term_meta' ), 10, 4 ); |
|
| 51 | + add_action( 'pre_delete_term', array( $this, 'delete_term' ) ); |
|
| 52 | + |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function saved_term( $term_id ) { |
|
| 56 | + |
|
| 57 | + // Sync all the terms without filtering. |
|
| 58 | + $this->sync( $term_id ); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + public function changed_term_meta( $meta_id, $term_id, $meta_key, $_meta_value ) { |
|
| 62 | + |
|
| 63 | + if ( in_array( $meta_key, |
|
| 64 | + apply_filters( 'wl_dataset__sync_post_hooks__ignored_meta_keys', |
|
| 65 | + apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 66 | + array( |
|
| 67 | + '_pingme', |
|
| 68 | + '_encloseme', |
|
| 69 | + 'entity_url', |
|
| 70 | + ) ) ) ) |
|
| 71 | + ) { |
|
| 72 | + return; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + $this->sync( $term_id ); |
|
| 76 | + |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + private function sync( $term_id ) { |
|
| 80 | + |
|
| 81 | + $this->enqueue( array( 'do_sync', $term_id ) ); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function do_sync( $term_id ) { |
|
| 85 | + |
|
| 86 | + try { |
|
| 87 | + $term = get_term( $term_id ); |
|
| 88 | + if ( ! isset( $term ) ) { |
|
| 89 | + return; |
|
| 90 | + } |
|
| 91 | + $this->sync_service->sync_many( array( |
|
| 92 | + $this->sync_object_factory->create( Object_Type_Enum::TERM, $term_id ), |
|
| 93 | + ) ); |
|
| 94 | + } catch ( \Exception $e ) { |
|
| 95 | + $this->log->error( "An error occurred while trying to sync post $term_id: " . $e->getMessage(), $e ); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @param $term \WP_Term |
|
| 101 | + * |
|
| 102 | + * @throws \Exception |
|
| 103 | + */ |
|
| 104 | + public function delete_term( $term_id ) { |
|
| 105 | + $args = array( |
|
| 106 | + $term_id, |
|
| 107 | + Wordpress_Term_Content_Legacy_Service |
|
| 108 | + ::get_instance() |
|
| 109 | + ->get_entity_id( Wordpress_Content_Id::create_term( $term_id ) ) |
|
| 110 | + ); |
|
| 111 | + // We can't postpone the execution for a `delete` because we would miss the actual data. |
|
| 112 | + $this->do_delete( $args ); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + public function do_delete( $args ) { |
|
| 116 | + $term_id = $args[0]; |
|
| 117 | + $term_entity_uri = $args[1]; |
|
| 118 | + |
|
| 119 | + try { |
|
| 120 | + $this->sync_service->delete_one( Object_Type_Enum::TERM, $term_id, $term_entity_uri ); |
|
| 121 | + } catch ( \Exception $e ) { |
|
| 122 | + $this->log->error( "An error occurred while trying to delete term $term_id: " . $e->getMessage(), $e ); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -28,10 +28,10 @@ discard block |
||
| 28 | 28 | * @param Sync_Service $sync_service |
| 29 | 29 | * @param Sync_Object_Adapter_Factory $sync_object_factory |
| 30 | 30 | */ |
| 31 | - function __construct( $sync_service, $sync_object_factory ) { |
|
| 31 | + function __construct($sync_service, $sync_object_factory) { |
|
| 32 | 32 | parent::__construct(); |
| 33 | 33 | |
| 34 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 34 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 35 | 35 | |
| 36 | 36 | $this->sync_service = $sync_service; |
| 37 | 37 | $this->sync_object_factory = $sync_object_factory; |
@@ -43,56 +43,56 @@ discard block |
||
| 43 | 43 | /** |
| 44 | 44 | * Register hooks for post and meta. |
| 45 | 45 | */ |
| 46 | - add_action( 'create_term', array( $this, 'do_sync' ) ); |
|
| 47 | - add_action( 'edit_term', array( $this, 'do_sync' ) ); |
|
| 48 | - add_action( 'added_term_meta', array( $this, 'changed_term_meta' ), 10, 4 ); |
|
| 49 | - add_action( 'updated_term_meta', array( $this, 'changed_term_meta' ), 10, 4 ); |
|
| 50 | - add_action( 'deleted_term_meta', array( $this, 'changed_term_meta' ), 10, 4 ); |
|
| 51 | - add_action( 'pre_delete_term', array( $this, 'delete_term' ) ); |
|
| 46 | + add_action('create_term', array($this, 'do_sync')); |
|
| 47 | + add_action('edit_term', array($this, 'do_sync')); |
|
| 48 | + add_action('added_term_meta', array($this, 'changed_term_meta'), 10, 4); |
|
| 49 | + add_action('updated_term_meta', array($this, 'changed_term_meta'), 10, 4); |
|
| 50 | + add_action('deleted_term_meta', array($this, 'changed_term_meta'), 10, 4); |
|
| 51 | + add_action('pre_delete_term', array($this, 'delete_term')); |
|
| 52 | 52 | |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - public function saved_term( $term_id ) { |
|
| 55 | + public function saved_term($term_id) { |
|
| 56 | 56 | |
| 57 | 57 | // Sync all the terms without filtering. |
| 58 | - $this->sync( $term_id ); |
|
| 58 | + $this->sync($term_id); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - public function changed_term_meta( $meta_id, $term_id, $meta_key, $_meta_value ) { |
|
| 61 | + public function changed_term_meta($meta_id, $term_id, $meta_key, $_meta_value) { |
|
| 62 | 62 | |
| 63 | - if ( in_array( $meta_key, |
|
| 64 | - apply_filters( 'wl_dataset__sync_post_hooks__ignored_meta_keys', |
|
| 65 | - apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 63 | + if (in_array($meta_key, |
|
| 64 | + apply_filters('wl_dataset__sync_post_hooks__ignored_meta_keys', |
|
| 65 | + apply_filters('wl_dataset__sync_hooks__ignored_meta_keys', |
|
| 66 | 66 | array( |
| 67 | 67 | '_pingme', |
| 68 | 68 | '_encloseme', |
| 69 | 69 | 'entity_url', |
| 70 | - ) ) ) ) |
|
| 70 | + )))) |
|
| 71 | 71 | ) { |
| 72 | 72 | return; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - $this->sync( $term_id ); |
|
| 75 | + $this->sync($term_id); |
|
| 76 | 76 | |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - private function sync( $term_id ) { |
|
| 79 | + private function sync($term_id) { |
|
| 80 | 80 | |
| 81 | - $this->enqueue( array( 'do_sync', $term_id ) ); |
|
| 81 | + $this->enqueue(array('do_sync', $term_id)); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - public function do_sync( $term_id ) { |
|
| 84 | + public function do_sync($term_id) { |
|
| 85 | 85 | |
| 86 | 86 | try { |
| 87 | - $term = get_term( $term_id ); |
|
| 88 | - if ( ! isset( $term ) ) { |
|
| 87 | + $term = get_term($term_id); |
|
| 88 | + if ( ! isset($term)) { |
|
| 89 | 89 | return; |
| 90 | 90 | } |
| 91 | - $this->sync_service->sync_many( array( |
|
| 92 | - $this->sync_object_factory->create( Object_Type_Enum::TERM, $term_id ), |
|
| 93 | - ) ); |
|
| 94 | - } catch ( \Exception $e ) { |
|
| 95 | - $this->log->error( "An error occurred while trying to sync post $term_id: " . $e->getMessage(), $e ); |
|
| 91 | + $this->sync_service->sync_many(array( |
|
| 92 | + $this->sync_object_factory->create(Object_Type_Enum::TERM, $term_id), |
|
| 93 | + )); |
|
| 94 | + } catch (\Exception $e) { |
|
| 95 | + $this->log->error("An error occurred while trying to sync post $term_id: ".$e->getMessage(), $e); |
|
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | |
@@ -101,25 +101,25 @@ discard block |
||
| 101 | 101 | * |
| 102 | 102 | * @throws \Exception |
| 103 | 103 | */ |
| 104 | - public function delete_term( $term_id ) { |
|
| 104 | + public function delete_term($term_id) { |
|
| 105 | 105 | $args = array( |
| 106 | 106 | $term_id, |
| 107 | 107 | Wordpress_Term_Content_Legacy_Service |
| 108 | 108 | ::get_instance() |
| 109 | - ->get_entity_id( Wordpress_Content_Id::create_term( $term_id ) ) |
|
| 109 | + ->get_entity_id(Wordpress_Content_Id::create_term($term_id)) |
|
| 110 | 110 | ); |
| 111 | 111 | // We can't postpone the execution for a `delete` because we would miss the actual data. |
| 112 | - $this->do_delete( $args ); |
|
| 112 | + $this->do_delete($args); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - public function do_delete( $args ) { |
|
| 115 | + public function do_delete($args) { |
|
| 116 | 116 | $term_id = $args[0]; |
| 117 | 117 | $term_entity_uri = $args[1]; |
| 118 | 118 | |
| 119 | 119 | try { |
| 120 | - $this->sync_service->delete_one( Object_Type_Enum::TERM, $term_id, $term_entity_uri ); |
|
| 121 | - } catch ( \Exception $e ) { |
|
| 122 | - $this->log->error( "An error occurred while trying to delete term $term_id: " . $e->getMessage(), $e ); |
|
| 120 | + $this->sync_service->delete_one(Object_Type_Enum::TERM, $term_id, $term_entity_uri); |
|
| 121 | + } catch (\Exception $e) { |
|
| 122 | + $this->log->error("An error occurred while trying to delete term $term_id: ".$e->getMessage(), $e); |
|
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | 125 | } |
@@ -5,41 +5,41 @@ |
||
| 5 | 5 | use Wordlift\Object_Type_Enum; |
| 6 | 6 | |
| 7 | 7 | class Sync_Term_Adapter extends Abstract_Sync_Object_Adapter { |
| 8 | - /** |
|
| 9 | - * @var int |
|
| 10 | - */ |
|
| 11 | - private $term_id; |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * Sync_Term_Adapter constructor. |
|
| 15 | - * |
|
| 16 | - * @param int $term_id |
|
| 17 | - * |
|
| 18 | - * @throws \Exception |
|
| 19 | - */ |
|
| 20 | - function __construct( $term_id ) { |
|
| 21 | - parent::__construct( Object_Type_Enum::TERM, $term_id ); |
|
| 22 | - |
|
| 23 | - $this->term_id = $term_id; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - function is_published() { |
|
| 27 | - return $this->is_public(); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - function is_public() { |
|
| 31 | - $term = get_term( $this->term_id ); |
|
| 32 | - |
|
| 33 | - return get_taxonomy( $term->taxonomy )->public; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - function set_values( $arr ) { |
|
| 37 | - // @@todo |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - function get_value( $key ) { |
|
| 41 | - // @@todo |
|
| 42 | - } |
|
| 8 | + /** |
|
| 9 | + * @var int |
|
| 10 | + */ |
|
| 11 | + private $term_id; |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * Sync_Term_Adapter constructor. |
|
| 15 | + * |
|
| 16 | + * @param int $term_id |
|
| 17 | + * |
|
| 18 | + * @throws \Exception |
|
| 19 | + */ |
|
| 20 | + function __construct( $term_id ) { |
|
| 21 | + parent::__construct( Object_Type_Enum::TERM, $term_id ); |
|
| 22 | + |
|
| 23 | + $this->term_id = $term_id; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + function is_published() { |
|
| 27 | + return $this->is_public(); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + function is_public() { |
|
| 31 | + $term = get_term( $this->term_id ); |
|
| 32 | + |
|
| 33 | + return get_taxonomy( $term->taxonomy )->public; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + function set_values( $arr ) { |
|
| 37 | + // @@todo |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + function get_value( $key ) { |
|
| 41 | + // @@todo |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | 44 | |
| 45 | 45 | } |
@@ -17,8 +17,8 @@ discard block |
||
| 17 | 17 | * |
| 18 | 18 | * @throws \Exception |
| 19 | 19 | */ |
| 20 | - function __construct( $term_id ) { |
|
| 21 | - parent::__construct( Object_Type_Enum::TERM, $term_id ); |
|
| 20 | + function __construct($term_id) { |
|
| 21 | + parent::__construct(Object_Type_Enum::TERM, $term_id); |
|
| 22 | 22 | |
| 23 | 23 | $this->term_id = $term_id; |
| 24 | 24 | } |
@@ -28,16 +28,16 @@ discard block |
||
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | function is_public() { |
| 31 | - $term = get_term( $this->term_id ); |
|
| 31 | + $term = get_term($this->term_id); |
|
| 32 | 32 | |
| 33 | - return get_taxonomy( $term->taxonomy )->public; |
|
| 33 | + return get_taxonomy($term->taxonomy)->public; |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - function set_values( $arr ) { |
|
| 36 | + function set_values($arr) { |
|
| 37 | 37 | // @@todo |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - function get_value( $key ) { |
|
| 40 | + function get_value($key) { |
|
| 41 | 41 | // @@todo |
| 42 | 42 | } |
| 43 | 43 | |
@@ -6,19 +6,19 @@ |
||
| 6 | 6 | |
| 7 | 7 | interface Sync_Object_Adapter { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @return int see {@link Object_Type_Enum} |
|
| 11 | - */ |
|
| 12 | - function get_type(); |
|
| 9 | + /** |
|
| 10 | + * @return int see {@link Object_Type_Enum} |
|
| 11 | + */ |
|
| 12 | + function get_type(); |
|
| 13 | 13 | |
| 14 | - function get_object_id(); |
|
| 14 | + function get_object_id(); |
|
| 15 | 15 | |
| 16 | - function is_published(); |
|
| 16 | + function is_published(); |
|
| 17 | 17 | |
| 18 | - function is_public(); |
|
| 18 | + function is_public(); |
|
| 19 | 19 | |
| 20 | - function set_values( $arr ); |
|
| 20 | + function set_values( $arr ); |
|
| 21 | 21 | |
| 22 | - function get_value( $key ); |
|
| 22 | + function get_value( $key ); |
|
| 23 | 23 | |
| 24 | 24 | } |
| 25 | 25 | \ No newline at end of file |
@@ -17,8 +17,8 @@ |
||
| 17 | 17 | |
| 18 | 18 | function is_public(); |
| 19 | 19 | |
| 20 | - function set_values( $arr ); |
|
| 20 | + function set_values($arr); |
|
| 21 | 21 | |
| 22 | - function get_value( $key ); |
|
| 22 | + function get_value($key); |
|
| 23 | 23 | |
| 24 | 24 | } |
| 25 | 25 | \ No newline at end of file |
@@ -14,35 +14,35 @@ |
||
| 14 | 14 | use Wordlift\Jsonld\Jsonld_Service; |
| 15 | 15 | |
| 16 | 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | - exit; |
|
| 17 | + exit; |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | // Register the Dataset JSON Endpoint. |
| 21 | 21 | if ( apply_filters( 'wl_feature__enable__dataset-ng', false ) ) { |
| 22 | 22 | |
| 23 | - $sync_object_adapter_factory = new Sync_Object_Adapter_Factory(); |
|
| 24 | - $sync_service = new Sync_Service( Default_Api_Service::get_instance(), $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() ); |
|
| 25 | - new Sync_Post_Hooks( $sync_service, $sync_object_adapter_factory ); |
|
| 26 | - new Sync_User_Hooks( $sync_service ); |
|
| 27 | - |
|
| 28 | - if ( apply_filters( 'wl_feature__enable__no-vocabulary-terms', false ) ) { |
|
| 29 | - new Sync_Term_Hooks( $sync_service, $sync_object_adapter_factory ); |
|
| 30 | - } |
|
| 31 | - /** |
|
| 32 | - * @since 3.28.0 |
|
| 33 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1186 |
|
| 34 | - */ |
|
| 35 | - new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() ); |
|
| 36 | - |
|
| 37 | - if ( apply_filters( 'wl_feature__enable__wordpress-ontology', false ) ) { |
|
| 38 | - new Sync_Hooks_Wordpress_Ontology(); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - if ( apply_filters( 'wl_feature__enable__sync-background', false ) ) { |
|
| 42 | - // Set up the sync background process. |
|
| 43 | - $sync_background_process = new Sync_Background_Process( $sync_service, $sync_object_adapter_factory ); |
|
| 44 | - new Sync_Background_Process_Wpjson_Endpoint( $sync_background_process ); |
|
| 45 | - new Sync_Page(); |
|
| 46 | - } |
|
| 23 | + $sync_object_adapter_factory = new Sync_Object_Adapter_Factory(); |
|
| 24 | + $sync_service = new Sync_Service( Default_Api_Service::get_instance(), $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() ); |
|
| 25 | + new Sync_Post_Hooks( $sync_service, $sync_object_adapter_factory ); |
|
| 26 | + new Sync_User_Hooks( $sync_service ); |
|
| 27 | + |
|
| 28 | + if ( apply_filters( 'wl_feature__enable__no-vocabulary-terms', false ) ) { |
|
| 29 | + new Sync_Term_Hooks( $sync_service, $sync_object_adapter_factory ); |
|
| 30 | + } |
|
| 31 | + /** |
|
| 32 | + * @since 3.28.0 |
|
| 33 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1186 |
|
| 34 | + */ |
|
| 35 | + new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() ); |
|
| 36 | + |
|
| 37 | + if ( apply_filters( 'wl_feature__enable__wordpress-ontology', false ) ) { |
|
| 38 | + new Sync_Hooks_Wordpress_Ontology(); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + if ( apply_filters( 'wl_feature__enable__sync-background', false ) ) { |
|
| 42 | + // Set up the sync background process. |
|
| 43 | + $sync_background_process = new Sync_Background_Process( $sync_service, $sync_object_adapter_factory ); |
|
| 44 | + new Sync_Background_Process_Wpjson_Endpoint( $sync_background_process ); |
|
| 45 | + new Sync_Page(); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | 48 | } |
@@ -13,35 +13,35 @@ |
||
| 13 | 13 | use Wordlift\Dataset\Sync_User_Hooks; |
| 14 | 14 | use Wordlift\Jsonld\Jsonld_Service; |
| 15 | 15 | |
| 16 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 16 | +if ( ! defined('ABSPATH')) { |
|
| 17 | 17 | exit; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | // Register the Dataset JSON Endpoint. |
| 21 | -if ( apply_filters( 'wl_feature__enable__dataset-ng', false ) ) { |
|
| 21 | +if (apply_filters('wl_feature__enable__dataset-ng', false)) { |
|
| 22 | 22 | |
| 23 | 23 | $sync_object_adapter_factory = new Sync_Object_Adapter_Factory(); |
| 24 | - $sync_service = new Sync_Service( Default_Api_Service::get_instance(), $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance() ); |
|
| 25 | - new Sync_Post_Hooks( $sync_service, $sync_object_adapter_factory ); |
|
| 26 | - new Sync_User_Hooks( $sync_service ); |
|
| 24 | + $sync_service = new Sync_Service(Default_Api_Service::get_instance(), $sync_object_adapter_factory, Jsonld_Service::get_instance(), Wordlift_Entity_Service::get_instance()); |
|
| 25 | + new Sync_Post_Hooks($sync_service, $sync_object_adapter_factory); |
|
| 26 | + new Sync_User_Hooks($sync_service); |
|
| 27 | 27 | |
| 28 | - if ( apply_filters( 'wl_feature__enable__no-vocabulary-terms', false ) ) { |
|
| 29 | - new Sync_Term_Hooks( $sync_service, $sync_object_adapter_factory ); |
|
| 28 | + if (apply_filters('wl_feature__enable__no-vocabulary-terms', false)) { |
|
| 29 | + new Sync_Term_Hooks($sync_service, $sync_object_adapter_factory); |
|
| 30 | 30 | } |
| 31 | 31 | /** |
| 32 | 32 | * @since 3.28.0 |
| 33 | 33 | * @see https://github.com/insideout10/wordlift-plugin/issues/1186 |
| 34 | 34 | */ |
| 35 | - new Sync_Hooks_Entity_Relation( Wordlift_Entity_Service::get_instance() ); |
|
| 35 | + new Sync_Hooks_Entity_Relation(Wordlift_Entity_Service::get_instance()); |
|
| 36 | 36 | |
| 37 | - if ( apply_filters( 'wl_feature__enable__wordpress-ontology', false ) ) { |
|
| 37 | + if (apply_filters('wl_feature__enable__wordpress-ontology', false)) { |
|
| 38 | 38 | new Sync_Hooks_Wordpress_Ontology(); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - if ( apply_filters( 'wl_feature__enable__sync-background', false ) ) { |
|
| 41 | + if (apply_filters('wl_feature__enable__sync-background', false)) { |
|
| 42 | 42 | // Set up the sync background process. |
| 43 | - $sync_background_process = new Sync_Background_Process( $sync_service, $sync_object_adapter_factory ); |
|
| 44 | - new Sync_Background_Process_Wpjson_Endpoint( $sync_background_process ); |
|
| 43 | + $sync_background_process = new Sync_Background_Process($sync_service, $sync_object_adapter_factory); |
|
| 44 | + new Sync_Background_Process_Wpjson_Endpoint($sync_background_process); |
|
| 45 | 45 | new Sync_Page(); |
| 46 | 46 | } |
| 47 | 47 | |