@@ -6,48 +6,48 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Sync_Hooks_Wordpress_Ontology { |
| 8 | 8 | |
| 9 | - const HTTP_PURL_ORG_WORDPRESS_1_0 = 'http://purl.org/wordpress/1.0/'; |
|
| 9 | + const HTTP_PURL_ORG_WORDPRESS_1_0 = 'http://purl.org/wordpress/1.0/'; |
|
| 10 | 10 | |
| 11 | - public function __construct() { |
|
| 12 | - add_filter( 'wl_dataset__sync_service__sync_item__jsonld', array( $this, 'jsonld' ), 10, 3 ); |
|
| 13 | - } |
|
| 11 | + public function __construct() { |
|
| 12 | + add_filter( 'wl_dataset__sync_service__sync_item__jsonld', array( $this, 'jsonld' ), 10, 3 ); |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - public function jsonld( $jsonld, $type, $object_id ) { |
|
| 15 | + public function jsonld( $jsonld, $type, $object_id ) { |
|
| 16 | 16 | |
| 17 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'id' ] = $object_id; |
|
| 17 | + $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'id' ] = $object_id; |
|
| 18 | 18 | |
| 19 | - switch ( $type ) { |
|
| 19 | + switch ( $type ) { |
|
| 20 | 20 | |
| 21 | - case Object_Type_Enum::TERM: |
|
| 22 | - $term = get_term( $object_id ); |
|
| 21 | + case Object_Type_Enum::TERM: |
|
| 22 | + $term = get_term( $object_id ); |
|
| 23 | 23 | |
| 24 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ] = 'term'; |
|
| 25 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'name' ] = $term->name; |
|
| 26 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'description' ] = $term->description; |
|
| 27 | - break; |
|
| 24 | + $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ] = 'term'; |
|
| 25 | + $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'name' ] = $term->name; |
|
| 26 | + $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'description' ] = $term->description; |
|
| 27 | + break; |
|
| 28 | 28 | |
| 29 | - case Object_Type_Enum::USER: |
|
| 30 | - $user = get_userdata( $object_id ); |
|
| 29 | + case Object_Type_Enum::USER: |
|
| 30 | + $user = get_userdata( $object_id ); |
|
| 31 | 31 | |
| 32 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ] = 'user'; |
|
| 33 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'displayName' ] = $user->display_name; |
|
| 34 | - break; |
|
| 32 | + $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ] = 'user'; |
|
| 33 | + $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'displayName' ] = $user->display_name; |
|
| 34 | + break; |
|
| 35 | 35 | |
| 36 | - case Object_Type_Enum::POST: |
|
| 37 | - $post = get_post( $object_id ); |
|
| 36 | + case Object_Type_Enum::POST: |
|
| 37 | + $post = get_post( $object_id ); |
|
| 38 | 38 | |
| 39 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ] = 'post'; |
|
| 40 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'customType' ] = $post->post_type; |
|
| 41 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'title' ] = $post->post_title; |
|
| 42 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'status' ] = $post->post_status; |
|
| 43 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'content' ] = do_shortcode( $post->post_content ); |
|
| 44 | - break; |
|
| 39 | + $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ] = 'post'; |
|
| 40 | + $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'customType' ] = $post->post_type; |
|
| 41 | + $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'title' ] = $post->post_title; |
|
| 42 | + $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'status' ] = $post->post_status; |
|
| 43 | + $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'content' ] = do_shortcode( $post->post_content ); |
|
| 44 | + break; |
|
| 45 | 45 | |
| 46 | - default: |
|
| 46 | + default: |
|
| 47 | 47 | |
| 48 | - } |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - return $jsonld; |
|
| 51 | - } |
|
| 50 | + return $jsonld; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | 53 | } |
@@ -9,38 +9,38 @@ |
||
| 9 | 9 | const HTTP_PURL_ORG_WORDPRESS_1_0 = 'http://purl.org/wordpress/1.0/'; |
| 10 | 10 | |
| 11 | 11 | public function __construct() { |
| 12 | - add_filter( 'wl_dataset__sync_service__sync_item__jsonld', array( $this, 'jsonld' ), 10, 3 ); |
|
| 12 | + add_filter('wl_dataset__sync_service__sync_item__jsonld', array($this, 'jsonld'), 10, 3); |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | - public function jsonld( $jsonld, $type, $object_id ) { |
|
| 15 | + public function jsonld($jsonld, $type, $object_id) { |
|
| 16 | 16 | |
| 17 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'id' ] = $object_id; |
|
| 17 | + $jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'id'] = $object_id; |
|
| 18 | 18 | |
| 19 | - switch ( $type ) { |
|
| 19 | + switch ($type) { |
|
| 20 | 20 | |
| 21 | 21 | case Object_Type_Enum::TERM: |
| 22 | - $term = get_term( $object_id ); |
|
| 22 | + $term = get_term($object_id); |
|
| 23 | 23 | |
| 24 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ] = 'term'; |
|
| 25 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'name' ] = $term->name; |
|
| 26 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'description' ] = $term->description; |
|
| 24 | + $jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'type'] = 'term'; |
|
| 25 | + $jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'name'] = $term->name; |
|
| 26 | + $jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'description'] = $term->description; |
|
| 27 | 27 | break; |
| 28 | 28 | |
| 29 | 29 | case Object_Type_Enum::USER: |
| 30 | - $user = get_userdata( $object_id ); |
|
| 30 | + $user = get_userdata($object_id); |
|
| 31 | 31 | |
| 32 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ] = 'user'; |
|
| 33 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'displayName' ] = $user->display_name; |
|
| 32 | + $jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'type'] = 'user'; |
|
| 33 | + $jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'displayName'] = $user->display_name; |
|
| 34 | 34 | break; |
| 35 | 35 | |
| 36 | 36 | case Object_Type_Enum::POST: |
| 37 | - $post = get_post( $object_id ); |
|
| 37 | + $post = get_post($object_id); |
|
| 38 | 38 | |
| 39 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'type' ] = 'post'; |
|
| 40 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'customType' ] = $post->post_type; |
|
| 41 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'title' ] = $post->post_title; |
|
| 42 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'status' ] = $post->post_status; |
|
| 43 | - $jsonld[0][ self::HTTP_PURL_ORG_WORDPRESS_1_0 . 'content' ] = do_shortcode( $post->post_content ); |
|
| 39 | + $jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'type'] = 'post'; |
|
| 40 | + $jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'customType'] = $post->post_type; |
|
| 41 | + $jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'title'] = $post->post_title; |
|
| 42 | + $jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'status'] = $post->post_status; |
|
| 43 | + $jsonld[0][self::HTTP_PURL_ORG_WORDPRESS_1_0.'content'] = do_shortcode($post->post_content); |
|
| 44 | 44 | break; |
| 45 | 45 | |
| 46 | 46 | default: |
@@ -18,116 +18,116 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Sync_Background_Process extends \Wordlift_Plugin_WP_Background_Process { |
| 20 | 20 | |
| 21 | - const STATE_STARTED = 'started'; |
|
| 22 | - const STATE_STOPPED = 'stopped'; |
|
| 23 | - |
|
| 24 | - protected $action = 'wl_dataset__sync'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var Sync_Service |
|
| 28 | - */ |
|
| 29 | - private $sync_service; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var Sync_Object_Adapter_Factory |
|
| 33 | - */ |
|
| 34 | - private $sync_object_adapter_factory; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var \Wordlift_Log_Service |
|
| 38 | - */ |
|
| 39 | - private $log; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var Sync_Background_Process_State |
|
| 43 | - */ |
|
| 44 | - private $state; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Sync_Background_Process constructor. |
|
| 48 | - * |
|
| 49 | - * @param Sync_Service $sync_service A {@link Sync_Service} instance providing the supporting functions to this background process. |
|
| 50 | - * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 51 | - */ |
|
| 52 | - public function __construct( $sync_service, $sync_object_adapter_factory ) { |
|
| 53 | - parent::__construct(); |
|
| 54 | - |
|
| 55 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 56 | - |
|
| 57 | - $this->sync_service = $sync_service; |
|
| 58 | - $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 59 | - |
|
| 60 | - // Set the current state. |
|
| 61 | - if ( self::STATE_STARTED === $this->get_state() ) { |
|
| 62 | - $this->state = new Sync_Background_Process_Started_State( $this, $this->sync_service, $this->sync_object_adapter_factory ); |
|
| 63 | - } else { |
|
| 64 | - $this->state = new Sync_Background_Process_Stopped_State( $this ); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * This function is called: |
|
| 71 | - * - To start a new Synchronization, by passing a {@link Sync_Start_Message} instance. |
|
| 72 | - * - To synchronize a post, by passing a numeric ID. |
|
| 73 | - * |
|
| 74 | - * This function returns the parameter for the next call or NULL if there are no more posts to process. |
|
| 75 | - * |
|
| 76 | - * @param mixed $item Queue item to iterate over. |
|
| 77 | - * |
|
| 78 | - * @return int[]|false The next post IDs or false if there are no more. |
|
| 79 | - */ |
|
| 80 | - protected function task( $item ) { |
|
| 81 | - |
|
| 82 | - return $this->state->task( $item ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Transition to the started state. |
|
| 87 | - */ |
|
| 88 | - public function start() { |
|
| 89 | - $this->state->leave(); |
|
| 90 | - $this->state = new Sync_Background_Process_Started_State( $this, $this->sync_service, $this->sync_object_adapter_factory ); |
|
| 91 | - $this->state->enter(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Transition to the stopped state. |
|
| 96 | - */ |
|
| 97 | - public function stop() { |
|
| 98 | - $this->state->leave(); |
|
| 99 | - $this->state = new Sync_Background_Process_Stopped_State( $this ); |
|
| 100 | - $this->state->enter(); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - public function resume() { |
|
| 104 | - $this->state->resume(); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Get the current state. |
|
| 109 | - * |
|
| 110 | - * @return string Either self::STARTED_STATE or self::STOPPED_STATE (default). |
|
| 111 | - */ |
|
| 112 | - public function get_state() { |
|
| 113 | - return get_option( '_wl_sync_background_process_state', self::STATE_STOPPED ); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Persist the current state. |
|
| 118 | - * |
|
| 119 | - * @param string $value |
|
| 120 | - * |
|
| 121 | - * @return bool |
|
| 122 | - */ |
|
| 123 | - public function set_state( $value ) { |
|
| 124 | - return null === $value |
|
| 125 | - ? delete_option( '_wl_sync_background_process_state' ) |
|
| 126 | - : update_option( '_wl_sync_background_process_state', $value, true ); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - public function get_info() { |
|
| 130 | - return $this->state->get_info(); |
|
| 131 | - } |
|
| 21 | + const STATE_STARTED = 'started'; |
|
| 22 | + const STATE_STOPPED = 'stopped'; |
|
| 23 | + |
|
| 24 | + protected $action = 'wl_dataset__sync'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var Sync_Service |
|
| 28 | + */ |
|
| 29 | + private $sync_service; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var Sync_Object_Adapter_Factory |
|
| 33 | + */ |
|
| 34 | + private $sync_object_adapter_factory; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var \Wordlift_Log_Service |
|
| 38 | + */ |
|
| 39 | + private $log; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var Sync_Background_Process_State |
|
| 43 | + */ |
|
| 44 | + private $state; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Sync_Background_Process constructor. |
|
| 48 | + * |
|
| 49 | + * @param Sync_Service $sync_service A {@link Sync_Service} instance providing the supporting functions to this background process. |
|
| 50 | + * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 51 | + */ |
|
| 52 | + public function __construct( $sync_service, $sync_object_adapter_factory ) { |
|
| 53 | + parent::__construct(); |
|
| 54 | + |
|
| 55 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 56 | + |
|
| 57 | + $this->sync_service = $sync_service; |
|
| 58 | + $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 59 | + |
|
| 60 | + // Set the current state. |
|
| 61 | + if ( self::STATE_STARTED === $this->get_state() ) { |
|
| 62 | + $this->state = new Sync_Background_Process_Started_State( $this, $this->sync_service, $this->sync_object_adapter_factory ); |
|
| 63 | + } else { |
|
| 64 | + $this->state = new Sync_Background_Process_Stopped_State( $this ); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * This function is called: |
|
| 71 | + * - To start a new Synchronization, by passing a {@link Sync_Start_Message} instance. |
|
| 72 | + * - To synchronize a post, by passing a numeric ID. |
|
| 73 | + * |
|
| 74 | + * This function returns the parameter for the next call or NULL if there are no more posts to process. |
|
| 75 | + * |
|
| 76 | + * @param mixed $item Queue item to iterate over. |
|
| 77 | + * |
|
| 78 | + * @return int[]|false The next post IDs or false if there are no more. |
|
| 79 | + */ |
|
| 80 | + protected function task( $item ) { |
|
| 81 | + |
|
| 82 | + return $this->state->task( $item ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Transition to the started state. |
|
| 87 | + */ |
|
| 88 | + public function start() { |
|
| 89 | + $this->state->leave(); |
|
| 90 | + $this->state = new Sync_Background_Process_Started_State( $this, $this->sync_service, $this->sync_object_adapter_factory ); |
|
| 91 | + $this->state->enter(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Transition to the stopped state. |
|
| 96 | + */ |
|
| 97 | + public function stop() { |
|
| 98 | + $this->state->leave(); |
|
| 99 | + $this->state = new Sync_Background_Process_Stopped_State( $this ); |
|
| 100 | + $this->state->enter(); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + public function resume() { |
|
| 104 | + $this->state->resume(); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Get the current state. |
|
| 109 | + * |
|
| 110 | + * @return string Either self::STARTED_STATE or self::STOPPED_STATE (default). |
|
| 111 | + */ |
|
| 112 | + public function get_state() { |
|
| 113 | + return get_option( '_wl_sync_background_process_state', self::STATE_STOPPED ); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Persist the current state. |
|
| 118 | + * |
|
| 119 | + * @param string $value |
|
| 120 | + * |
|
| 121 | + * @return bool |
|
| 122 | + */ |
|
| 123 | + public function set_state( $value ) { |
|
| 124 | + return null === $value |
|
| 125 | + ? delete_option( '_wl_sync_background_process_state' ) |
|
| 126 | + : update_option( '_wl_sync_background_process_state', $value, true ); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + public function get_info() { |
|
| 130 | + return $this->state->get_info(); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | 133 | } |
@@ -4,33 +4,33 @@ |
||
| 4 | 4 | |
| 5 | 5 | abstract class Abstract_Sync_Background_Process_State implements Sync_Background_Process_State { |
| 6 | 6 | |
| 7 | - private $state; |
|
| 8 | - |
|
| 9 | - function __construct( $state ) { |
|
| 10 | - $this->state = $state; |
|
| 11 | - } |
|
| 12 | - |
|
| 13 | - function get_info() { |
|
| 14 | - $started = get_option( '_wl_sync_background_process_started' ); |
|
| 15 | - $offset = get_option( '_wl_sync_background_process_offset' ); |
|
| 16 | - $stage = get_option( '_wl_sync_background_process_stage' ); |
|
| 17 | - $counts = get_option( '_wl_sync_background_process_count', array( 0 ) ); |
|
| 18 | - $last_update = get_option( '_wl_sync_background_process_updated' ); |
|
| 19 | - |
|
| 20 | - // Calculate the overall index by adding the count of completed stages. |
|
| 21 | - $index = $offset + 1; |
|
| 22 | - for ( $i = 0; $i < $stage; $i ++ ) { |
|
| 23 | - $index += $counts[ $i ]; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - // Get the total count. |
|
| 27 | - $total_count = array_sum( $counts ); |
|
| 28 | - |
|
| 29 | - return new Sync_Background_Process_Info( $this->state, $started, $index, $total_count, $last_update ); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - function resume() { |
|
| 33 | - // do nothing. |
|
| 34 | - } |
|
| 7 | + private $state; |
|
| 8 | + |
|
| 9 | + function __construct( $state ) { |
|
| 10 | + $this->state = $state; |
|
| 11 | + } |
|
| 12 | + |
|
| 13 | + function get_info() { |
|
| 14 | + $started = get_option( '_wl_sync_background_process_started' ); |
|
| 15 | + $offset = get_option( '_wl_sync_background_process_offset' ); |
|
| 16 | + $stage = get_option( '_wl_sync_background_process_stage' ); |
|
| 17 | + $counts = get_option( '_wl_sync_background_process_count', array( 0 ) ); |
|
| 18 | + $last_update = get_option( '_wl_sync_background_process_updated' ); |
|
| 19 | + |
|
| 20 | + // Calculate the overall index by adding the count of completed stages. |
|
| 21 | + $index = $offset + 1; |
|
| 22 | + for ( $i = 0; $i < $stage; $i ++ ) { |
|
| 23 | + $index += $counts[ $i ]; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + // Get the total count. |
|
| 27 | + $total_count = array_sum( $counts ); |
|
| 28 | + |
|
| 29 | + return new Sync_Background_Process_Info( $this->state, $started, $index, $total_count, $last_update ); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + function resume() { |
|
| 33 | + // do nothing. |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | } |
@@ -6,27 +6,27 @@ |
||
| 6 | 6 | |
| 7 | 7 | private $state; |
| 8 | 8 | |
| 9 | - function __construct( $state ) { |
|
| 9 | + function __construct($state) { |
|
| 10 | 10 | $this->state = $state; |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | function get_info() { |
| 14 | - $started = get_option( '_wl_sync_background_process_started' ); |
|
| 15 | - $offset = get_option( '_wl_sync_background_process_offset' ); |
|
| 16 | - $stage = get_option( '_wl_sync_background_process_stage' ); |
|
| 17 | - $counts = get_option( '_wl_sync_background_process_count', array( 0 ) ); |
|
| 18 | - $last_update = get_option( '_wl_sync_background_process_updated' ); |
|
| 14 | + $started = get_option('_wl_sync_background_process_started'); |
|
| 15 | + $offset = get_option('_wl_sync_background_process_offset'); |
|
| 16 | + $stage = get_option('_wl_sync_background_process_stage'); |
|
| 17 | + $counts = get_option('_wl_sync_background_process_count', array(0)); |
|
| 18 | + $last_update = get_option('_wl_sync_background_process_updated'); |
|
| 19 | 19 | |
| 20 | 20 | // Calculate the overall index by adding the count of completed stages. |
| 21 | 21 | $index = $offset + 1; |
| 22 | - for ( $i = 0; $i < $stage; $i ++ ) { |
|
| 23 | - $index += $counts[ $i ]; |
|
| 22 | + for ($i = 0; $i < $stage; $i++) { |
|
| 23 | + $index += $counts[$i]; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | // Get the total count. |
| 27 | - $total_count = array_sum( $counts ); |
|
| 27 | + $total_count = array_sum($counts); |
|
| 28 | 28 | |
| 29 | - return new Sync_Background_Process_Info( $this->state, $started, $index, $total_count, $last_update ); |
|
| 29 | + return new Sync_Background_Process_Info($this->state, $started, $index, $total_count, $last_update); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | function resume() { |
@@ -6,66 +6,66 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Sync_Background_Process_Wpjson_Endpoint { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @var Sync_Background_Process |
|
| 11 | - */ |
|
| 12 | - private $sync_background_process; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * Sync_Background_Process_Wpjson_Endpoint constructor. |
|
| 16 | - * |
|
| 17 | - * @param Sync_Background_Process $sync_background_process |
|
| 18 | - */ |
|
| 19 | - function __construct( $sync_background_process ) { |
|
| 20 | - |
|
| 21 | - add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
|
| 22 | - |
|
| 23 | - $this->sync_background_process = $sync_background_process; |
|
| 24 | - |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - function rest_api_init() { |
|
| 28 | - |
|
| 29 | - register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
| 30 | - 'methods' => WP_REST_Server::CREATABLE, |
|
| 31 | - 'callback' => array( $this->sync_background_process, 'start' ), |
|
| 32 | - 'permission_callback' => function () { |
|
| 33 | - $user = wp_get_current_user(); |
|
| 34 | - |
|
| 35 | - return is_super_admin( $user->ID ) || in_array( 'administrator', (array) $user->roles ); |
|
| 36 | - } |
|
| 37 | - ) ); |
|
| 38 | - |
|
| 39 | - register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
| 40 | - 'methods' => WP_REST_Server::READABLE, |
|
| 41 | - 'callback' => array( $this->sync_background_process, 'get_info' ), |
|
| 42 | - 'permission_callback' => function () { |
|
| 43 | - $user = wp_get_current_user(); |
|
| 44 | - |
|
| 45 | - return is_super_admin( $user->ID ) || in_array( 'administrator', (array) $user->roles ); |
|
| 46 | - } |
|
| 47 | - ) ); |
|
| 48 | - |
|
| 49 | - register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
| 50 | - 'methods' => WP_REST_Server::DELETABLE, |
|
| 51 | - 'callback' => array( $this->sync_background_process, 'stop' ), |
|
| 52 | - 'permission_callback' => function () { |
|
| 53 | - $user = wp_get_current_user(); |
|
| 54 | - |
|
| 55 | - return is_super_admin( $user->ID ) || in_array( 'administrator', (array) $user->roles ); |
|
| 56 | - } |
|
| 57 | - ) ); |
|
| 58 | - |
|
| 59 | - register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
| 60 | - 'methods' => 'PUT', |
|
| 61 | - 'callback' => array( $this->sync_background_process, 'resume' ), |
|
| 62 | - 'permission_callback' => function () { |
|
| 63 | - $user = wp_get_current_user(); |
|
| 64 | - |
|
| 65 | - return is_super_admin( $user->ID ) || in_array( 'administrator', (array) $user->roles ); |
|
| 66 | - } |
|
| 67 | - ) ); |
|
| 68 | - |
|
| 69 | - } |
|
| 9 | + /** |
|
| 10 | + * @var Sync_Background_Process |
|
| 11 | + */ |
|
| 12 | + private $sync_background_process; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * Sync_Background_Process_Wpjson_Endpoint constructor. |
|
| 16 | + * |
|
| 17 | + * @param Sync_Background_Process $sync_background_process |
|
| 18 | + */ |
|
| 19 | + function __construct( $sync_background_process ) { |
|
| 20 | + |
|
| 21 | + add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
|
| 22 | + |
|
| 23 | + $this->sync_background_process = $sync_background_process; |
|
| 24 | + |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + function rest_api_init() { |
|
| 28 | + |
|
| 29 | + register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
| 30 | + 'methods' => WP_REST_Server::CREATABLE, |
|
| 31 | + 'callback' => array( $this->sync_background_process, 'start' ), |
|
| 32 | + 'permission_callback' => function () { |
|
| 33 | + $user = wp_get_current_user(); |
|
| 34 | + |
|
| 35 | + return is_super_admin( $user->ID ) || in_array( 'administrator', (array) $user->roles ); |
|
| 36 | + } |
|
| 37 | + ) ); |
|
| 38 | + |
|
| 39 | + register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
| 40 | + 'methods' => WP_REST_Server::READABLE, |
|
| 41 | + 'callback' => array( $this->sync_background_process, 'get_info' ), |
|
| 42 | + 'permission_callback' => function () { |
|
| 43 | + $user = wp_get_current_user(); |
|
| 44 | + |
|
| 45 | + return is_super_admin( $user->ID ) || in_array( 'administrator', (array) $user->roles ); |
|
| 46 | + } |
|
| 47 | + ) ); |
|
| 48 | + |
|
| 49 | + register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
| 50 | + 'methods' => WP_REST_Server::DELETABLE, |
|
| 51 | + 'callback' => array( $this->sync_background_process, 'stop' ), |
|
| 52 | + 'permission_callback' => function () { |
|
| 53 | + $user = wp_get_current_user(); |
|
| 54 | + |
|
| 55 | + return is_super_admin( $user->ID ) || in_array( 'administrator', (array) $user->roles ); |
|
| 56 | + } |
|
| 57 | + ) ); |
|
| 58 | + |
|
| 59 | + register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
| 60 | + 'methods' => 'PUT', |
|
| 61 | + 'callback' => array( $this->sync_background_process, 'resume' ), |
|
| 62 | + 'permission_callback' => function () { |
|
| 63 | + $user = wp_get_current_user(); |
|
| 64 | + |
|
| 65 | + return is_super_admin( $user->ID ) || in_array( 'administrator', (array) $user->roles ); |
|
| 66 | + } |
|
| 67 | + ) ); |
|
| 68 | + |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | 71 | } |
@@ -16,9 +16,9 @@ discard block |
||
| 16 | 16 | * |
| 17 | 17 | * @param Sync_Background_Process $sync_background_process |
| 18 | 18 | */ |
| 19 | - function __construct( $sync_background_process ) { |
|
| 19 | + function __construct($sync_background_process) { |
|
| 20 | 20 | |
| 21 | - add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
|
| 21 | + add_action('rest_api_init', array($this, 'rest_api_init')); |
|
| 22 | 22 | |
| 23 | 23 | $this->sync_background_process = $sync_background_process; |
| 24 | 24 | |
@@ -26,45 +26,45 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | function rest_api_init() { |
| 28 | 28 | |
| 29 | - register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
| 29 | + register_rest_route('wordlift/v1', '/dataset/background/sync', array( |
|
| 30 | 30 | 'methods' => WP_REST_Server::CREATABLE, |
| 31 | - 'callback' => array( $this->sync_background_process, 'start' ), |
|
| 32 | - 'permission_callback' => function () { |
|
| 31 | + 'callback' => array($this->sync_background_process, 'start'), |
|
| 32 | + 'permission_callback' => function() { |
|
| 33 | 33 | $user = wp_get_current_user(); |
| 34 | 34 | |
| 35 | - return is_super_admin( $user->ID ) || in_array( 'administrator', (array) $user->roles ); |
|
| 35 | + return is_super_admin($user->ID) || in_array('administrator', (array) $user->roles); |
|
| 36 | 36 | } |
| 37 | - ) ); |
|
| 37 | + )); |
|
| 38 | 38 | |
| 39 | - register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
| 39 | + register_rest_route('wordlift/v1', '/dataset/background/sync', array( |
|
| 40 | 40 | 'methods' => WP_REST_Server::READABLE, |
| 41 | - 'callback' => array( $this->sync_background_process, 'get_info' ), |
|
| 42 | - 'permission_callback' => function () { |
|
| 41 | + 'callback' => array($this->sync_background_process, 'get_info'), |
|
| 42 | + 'permission_callback' => function() { |
|
| 43 | 43 | $user = wp_get_current_user(); |
| 44 | 44 | |
| 45 | - return is_super_admin( $user->ID ) || in_array( 'administrator', (array) $user->roles ); |
|
| 45 | + return is_super_admin($user->ID) || in_array('administrator', (array) $user->roles); |
|
| 46 | 46 | } |
| 47 | - ) ); |
|
| 47 | + )); |
|
| 48 | 48 | |
| 49 | - register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
| 49 | + register_rest_route('wordlift/v1', '/dataset/background/sync', array( |
|
| 50 | 50 | 'methods' => WP_REST_Server::DELETABLE, |
| 51 | - 'callback' => array( $this->sync_background_process, 'stop' ), |
|
| 52 | - 'permission_callback' => function () { |
|
| 51 | + 'callback' => array($this->sync_background_process, 'stop'), |
|
| 52 | + 'permission_callback' => function() { |
|
| 53 | 53 | $user = wp_get_current_user(); |
| 54 | 54 | |
| 55 | - return is_super_admin( $user->ID ) || in_array( 'administrator', (array) $user->roles ); |
|
| 55 | + return is_super_admin($user->ID) || in_array('administrator', (array) $user->roles); |
|
| 56 | 56 | } |
| 57 | - ) ); |
|
| 57 | + )); |
|
| 58 | 58 | |
| 59 | - register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
| 59 | + register_rest_route('wordlift/v1', '/dataset/background/sync', array( |
|
| 60 | 60 | 'methods' => 'PUT', |
| 61 | - 'callback' => array( $this->sync_background_process, 'resume' ), |
|
| 62 | - 'permission_callback' => function () { |
|
| 61 | + 'callback' => array($this->sync_background_process, 'resume'), |
|
| 62 | + 'permission_callback' => function() { |
|
| 63 | 63 | $user = wp_get_current_user(); |
| 64 | 64 | |
| 65 | - return is_super_admin( $user->ID ) || in_array( 'administrator', (array) $user->roles ); |
|
| 65 | + return is_super_admin($user->ID) || in_array('administrator', (array) $user->roles); |
|
| 66 | 66 | } |
| 67 | - ) ); |
|
| 67 | + )); |
|
| 68 | 68 | |
| 69 | 69 | } |
| 70 | 70 | |
@@ -11,132 +11,132 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Sync_Background_Process_Started_State extends Abstract_Sync_Background_Process_State { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @var Sync_Background_Process |
|
| 16 | - */ |
|
| 17 | - private $context; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @var Sync_Service |
|
| 21 | - */ |
|
| 22 | - private $sync_service; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var Sync_Background_Process_Stage[] |
|
| 26 | - */ |
|
| 27 | - private $stages; |
|
| 28 | - |
|
| 29 | - private $batch_size = 5; |
|
| 30 | - /** |
|
| 31 | - * @var bool |
|
| 32 | - */ |
|
| 33 | - private $reset; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Sync_Background_Process_Started_State constructor. |
|
| 37 | - * |
|
| 38 | - * @param Sync_Background_Process $context |
|
| 39 | - * @param Sync_Service $sync_service |
|
| 40 | - * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 41 | - * @param bool $reset Whether to reset the counters |
|
| 42 | - */ |
|
| 43 | - function __construct( $context, $sync_service, $sync_object_adapter_factory, $reset = true ) { |
|
| 44 | - parent::__construct( Sync_Background_Process::STATE_STARTED ); |
|
| 45 | - |
|
| 46 | - $this->context = $context; |
|
| 47 | - $this->sync_service = $sync_service; |
|
| 48 | - |
|
| 49 | - $this->stages = array( |
|
| 50 | - new Sync_Background_Process_Posts_Stage( $sync_object_adapter_factory ), |
|
| 51 | - new Sync_Background_Process_Terms_Stage( $sync_object_adapter_factory ), |
|
| 52 | - new Sync_Background_Process_Users_Stage( $sync_object_adapter_factory ), |
|
| 53 | - ); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - function enter() { |
|
| 57 | - // Delete the KG contents. |
|
| 58 | - $this->sync_service->delete_all(); |
|
| 59 | - |
|
| 60 | - // Clear caches. |
|
| 61 | - do_action( 'wl_ttl_cache_cleaner__flush' ); |
|
| 62 | - |
|
| 63 | - $counts = array_map( function ( $item ) { |
|
| 64 | - return $item->count(); |
|
| 65 | - }, $this->stages ); |
|
| 66 | - |
|
| 67 | - update_option( '_wl_sync_background_process_count', $counts, true ); |
|
| 68 | - update_option( '_wl_sync_background_process_stage', 0, true ); |
|
| 69 | - update_option( '_wl_sync_background_process_offset', 0, true ); |
|
| 70 | - update_option( '_wl_sync_background_process_started', time(), true ); |
|
| 71 | - update_option( '_wl_sync_background_process_updated', time(), true ); |
|
| 72 | - |
|
| 73 | - $this->context->set_state( Sync_Background_Process::STATE_STARTED ); |
|
| 74 | - |
|
| 75 | - $this->resume(); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - function resume() { |
|
| 79 | - $this->context->push_to_queue( true ); |
|
| 80 | - $this->context->save()->dispatch(); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - function leave() { |
|
| 84 | - $this->context->set_state( null ); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - function task( $args ) { |
|
| 88 | - |
|
| 89 | - $offset = get_option( '_wl_sync_background_process_offset' ); |
|
| 90 | - $stage = get_option( '_wl_sync_background_process_stage' ); |
|
| 91 | - $counts = get_option( '_wl_sync_background_process_count' ); |
|
| 92 | - $batch_size = min( $counts[ $stage ] - $offset, $this->batch_size ); |
|
| 93 | - |
|
| 94 | - add_filter( 'wl_api_service__request', array( $this, 'api_service__request' ) ); |
|
| 95 | - try { |
|
| 96 | - $object_adapters = $this->stages[ $stage ]->get_sync_object_adapters( $offset, $batch_size ); |
|
| 97 | - $this->sync_service->sync_many( $object_adapters, true ); |
|
| 98 | - } catch ( \Exception $e ) { |
|
| 99 | - // ignored. |
|
| 100 | - } |
|
| 101 | - remove_filter( 'wl_api_service__request', array( $this, 'api_service__request' ) ); |
|
| 102 | - |
|
| 103 | - update_option( '_wl_sync_background_process_updated', time(), true ); |
|
| 104 | - |
|
| 105 | - // Increase the offset. |
|
| 106 | - if ( ( $offset + $batch_size ) < $counts[ $stage ] ) { |
|
| 107 | - update_option( '_wl_sync_background_process_offset', $offset + $batch_size, true ); |
|
| 108 | - |
|
| 109 | - return true; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // Increase the stage. |
|
| 113 | - if ( ( $stage + 1 ) < count( $this->stages ) ) { |
|
| 114 | - update_option( '_wl_sync_background_process_stage', $stage + 1, true ); |
|
| 115 | - update_option( '_wl_sync_background_process_offset', 0, true ); |
|
| 116 | - |
|
| 117 | - return true; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - // Stop processing. |
|
| 121 | - $this->context->stop(); |
|
| 122 | - |
|
| 123 | - return false; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Hook to provide a request to update the status on the server. |
|
| 128 | - * |
|
| 129 | - * @param array $args |
|
| 130 | - * |
|
| 131 | - * @return mixed |
|
| 132 | - */ |
|
| 133 | - public function api_service__request( $args ) { |
|
| 134 | - |
|
| 135 | - $state_header_value = str_replace( "\n", '', wp_json_encode( $this->context->get_info() ) ); |
|
| 136 | - |
|
| 137 | - $args['headers']['X-Wordlift-Dataset-Sync-State-V1'] = $state_header_value; |
|
| 138 | - |
|
| 139 | - return $args; |
|
| 140 | - } |
|
| 14 | + /** |
|
| 15 | + * @var Sync_Background_Process |
|
| 16 | + */ |
|
| 17 | + private $context; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @var Sync_Service |
|
| 21 | + */ |
|
| 22 | + private $sync_service; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var Sync_Background_Process_Stage[] |
|
| 26 | + */ |
|
| 27 | + private $stages; |
|
| 28 | + |
|
| 29 | + private $batch_size = 5; |
|
| 30 | + /** |
|
| 31 | + * @var bool |
|
| 32 | + */ |
|
| 33 | + private $reset; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Sync_Background_Process_Started_State constructor. |
|
| 37 | + * |
|
| 38 | + * @param Sync_Background_Process $context |
|
| 39 | + * @param Sync_Service $sync_service |
|
| 40 | + * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 41 | + * @param bool $reset Whether to reset the counters |
|
| 42 | + */ |
|
| 43 | + function __construct( $context, $sync_service, $sync_object_adapter_factory, $reset = true ) { |
|
| 44 | + parent::__construct( Sync_Background_Process::STATE_STARTED ); |
|
| 45 | + |
|
| 46 | + $this->context = $context; |
|
| 47 | + $this->sync_service = $sync_service; |
|
| 48 | + |
|
| 49 | + $this->stages = array( |
|
| 50 | + new Sync_Background_Process_Posts_Stage( $sync_object_adapter_factory ), |
|
| 51 | + new Sync_Background_Process_Terms_Stage( $sync_object_adapter_factory ), |
|
| 52 | + new Sync_Background_Process_Users_Stage( $sync_object_adapter_factory ), |
|
| 53 | + ); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + function enter() { |
|
| 57 | + // Delete the KG contents. |
|
| 58 | + $this->sync_service->delete_all(); |
|
| 59 | + |
|
| 60 | + // Clear caches. |
|
| 61 | + do_action( 'wl_ttl_cache_cleaner__flush' ); |
|
| 62 | + |
|
| 63 | + $counts = array_map( function ( $item ) { |
|
| 64 | + return $item->count(); |
|
| 65 | + }, $this->stages ); |
|
| 66 | + |
|
| 67 | + update_option( '_wl_sync_background_process_count', $counts, true ); |
|
| 68 | + update_option( '_wl_sync_background_process_stage', 0, true ); |
|
| 69 | + update_option( '_wl_sync_background_process_offset', 0, true ); |
|
| 70 | + update_option( '_wl_sync_background_process_started', time(), true ); |
|
| 71 | + update_option( '_wl_sync_background_process_updated', time(), true ); |
|
| 72 | + |
|
| 73 | + $this->context->set_state( Sync_Background_Process::STATE_STARTED ); |
|
| 74 | + |
|
| 75 | + $this->resume(); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + function resume() { |
|
| 79 | + $this->context->push_to_queue( true ); |
|
| 80 | + $this->context->save()->dispatch(); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + function leave() { |
|
| 84 | + $this->context->set_state( null ); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + function task( $args ) { |
|
| 88 | + |
|
| 89 | + $offset = get_option( '_wl_sync_background_process_offset' ); |
|
| 90 | + $stage = get_option( '_wl_sync_background_process_stage' ); |
|
| 91 | + $counts = get_option( '_wl_sync_background_process_count' ); |
|
| 92 | + $batch_size = min( $counts[ $stage ] - $offset, $this->batch_size ); |
|
| 93 | + |
|
| 94 | + add_filter( 'wl_api_service__request', array( $this, 'api_service__request' ) ); |
|
| 95 | + try { |
|
| 96 | + $object_adapters = $this->stages[ $stage ]->get_sync_object_adapters( $offset, $batch_size ); |
|
| 97 | + $this->sync_service->sync_many( $object_adapters, true ); |
|
| 98 | + } catch ( \Exception $e ) { |
|
| 99 | + // ignored. |
|
| 100 | + } |
|
| 101 | + remove_filter( 'wl_api_service__request', array( $this, 'api_service__request' ) ); |
|
| 102 | + |
|
| 103 | + update_option( '_wl_sync_background_process_updated', time(), true ); |
|
| 104 | + |
|
| 105 | + // Increase the offset. |
|
| 106 | + if ( ( $offset + $batch_size ) < $counts[ $stage ] ) { |
|
| 107 | + update_option( '_wl_sync_background_process_offset', $offset + $batch_size, true ); |
|
| 108 | + |
|
| 109 | + return true; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // Increase the stage. |
|
| 113 | + if ( ( $stage + 1 ) < count( $this->stages ) ) { |
|
| 114 | + update_option( '_wl_sync_background_process_stage', $stage + 1, true ); |
|
| 115 | + update_option( '_wl_sync_background_process_offset', 0, true ); |
|
| 116 | + |
|
| 117 | + return true; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + // Stop processing. |
|
| 121 | + $this->context->stop(); |
|
| 122 | + |
|
| 123 | + return false; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Hook to provide a request to update the status on the server. |
|
| 128 | + * |
|
| 129 | + * @param array $args |
|
| 130 | + * |
|
| 131 | + * @return mixed |
|
| 132 | + */ |
|
| 133 | + public function api_service__request( $args ) { |
|
| 134 | + |
|
| 135 | + $state_header_value = str_replace( "\n", '', wp_json_encode( $this->context->get_info() ) ); |
|
| 136 | + |
|
| 137 | + $args['headers']['X-Wordlift-Dataset-Sync-State-V1'] = $state_header_value; |
|
| 138 | + |
|
| 139 | + return $args; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | 142 | } |
@@ -40,16 +40,16 @@ discard block |
||
| 40 | 40 | * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
| 41 | 41 | * @param bool $reset Whether to reset the counters |
| 42 | 42 | */ |
| 43 | - function __construct( $context, $sync_service, $sync_object_adapter_factory, $reset = true ) { |
|
| 44 | - parent::__construct( Sync_Background_Process::STATE_STARTED ); |
|
| 43 | + function __construct($context, $sync_service, $sync_object_adapter_factory, $reset = true) { |
|
| 44 | + parent::__construct(Sync_Background_Process::STATE_STARTED); |
|
| 45 | 45 | |
| 46 | 46 | $this->context = $context; |
| 47 | 47 | $this->sync_service = $sync_service; |
| 48 | 48 | |
| 49 | 49 | $this->stages = array( |
| 50 | - new Sync_Background_Process_Posts_Stage( $sync_object_adapter_factory ), |
|
| 51 | - new Sync_Background_Process_Terms_Stage( $sync_object_adapter_factory ), |
|
| 52 | - new Sync_Background_Process_Users_Stage( $sync_object_adapter_factory ), |
|
| 50 | + new Sync_Background_Process_Posts_Stage($sync_object_adapter_factory), |
|
| 51 | + new Sync_Background_Process_Terms_Stage($sync_object_adapter_factory), |
|
| 52 | + new Sync_Background_Process_Users_Stage($sync_object_adapter_factory), |
|
| 53 | 53 | ); |
| 54 | 54 | } |
| 55 | 55 | |
@@ -58,61 +58,61 @@ discard block |
||
| 58 | 58 | $this->sync_service->delete_all(); |
| 59 | 59 | |
| 60 | 60 | // Clear caches. |
| 61 | - do_action( 'wl_ttl_cache_cleaner__flush' ); |
|
| 61 | + do_action('wl_ttl_cache_cleaner__flush'); |
|
| 62 | 62 | |
| 63 | - $counts = array_map( function ( $item ) { |
|
| 63 | + $counts = array_map(function($item) { |
|
| 64 | 64 | return $item->count(); |
| 65 | - }, $this->stages ); |
|
| 65 | + }, $this->stages); |
|
| 66 | 66 | |
| 67 | - update_option( '_wl_sync_background_process_count', $counts, true ); |
|
| 68 | - update_option( '_wl_sync_background_process_stage', 0, true ); |
|
| 69 | - update_option( '_wl_sync_background_process_offset', 0, true ); |
|
| 70 | - update_option( '_wl_sync_background_process_started', time(), true ); |
|
| 71 | - update_option( '_wl_sync_background_process_updated', time(), true ); |
|
| 67 | + update_option('_wl_sync_background_process_count', $counts, true); |
|
| 68 | + update_option('_wl_sync_background_process_stage', 0, true); |
|
| 69 | + update_option('_wl_sync_background_process_offset', 0, true); |
|
| 70 | + update_option('_wl_sync_background_process_started', time(), true); |
|
| 71 | + update_option('_wl_sync_background_process_updated', time(), true); |
|
| 72 | 72 | |
| 73 | - $this->context->set_state( Sync_Background_Process::STATE_STARTED ); |
|
| 73 | + $this->context->set_state(Sync_Background_Process::STATE_STARTED); |
|
| 74 | 74 | |
| 75 | 75 | $this->resume(); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | function resume() { |
| 79 | - $this->context->push_to_queue( true ); |
|
| 79 | + $this->context->push_to_queue(true); |
|
| 80 | 80 | $this->context->save()->dispatch(); |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | function leave() { |
| 84 | - $this->context->set_state( null ); |
|
| 84 | + $this->context->set_state(null); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - function task( $args ) { |
|
| 87 | + function task($args) { |
|
| 88 | 88 | |
| 89 | - $offset = get_option( '_wl_sync_background_process_offset' ); |
|
| 90 | - $stage = get_option( '_wl_sync_background_process_stage' ); |
|
| 91 | - $counts = get_option( '_wl_sync_background_process_count' ); |
|
| 92 | - $batch_size = min( $counts[ $stage ] - $offset, $this->batch_size ); |
|
| 89 | + $offset = get_option('_wl_sync_background_process_offset'); |
|
| 90 | + $stage = get_option('_wl_sync_background_process_stage'); |
|
| 91 | + $counts = get_option('_wl_sync_background_process_count'); |
|
| 92 | + $batch_size = min($counts[$stage] - $offset, $this->batch_size); |
|
| 93 | 93 | |
| 94 | - add_filter( 'wl_api_service__request', array( $this, 'api_service__request' ) ); |
|
| 94 | + add_filter('wl_api_service__request', array($this, 'api_service__request')); |
|
| 95 | 95 | try { |
| 96 | - $object_adapters = $this->stages[ $stage ]->get_sync_object_adapters( $offset, $batch_size ); |
|
| 97 | - $this->sync_service->sync_many( $object_adapters, true ); |
|
| 98 | - } catch ( \Exception $e ) { |
|
| 96 | + $object_adapters = $this->stages[$stage]->get_sync_object_adapters($offset, $batch_size); |
|
| 97 | + $this->sync_service->sync_many($object_adapters, true); |
|
| 98 | + } catch (\Exception $e) { |
|
| 99 | 99 | // ignored. |
| 100 | 100 | } |
| 101 | - remove_filter( 'wl_api_service__request', array( $this, 'api_service__request' ) ); |
|
| 101 | + remove_filter('wl_api_service__request', array($this, 'api_service__request')); |
|
| 102 | 102 | |
| 103 | - update_option( '_wl_sync_background_process_updated', time(), true ); |
|
| 103 | + update_option('_wl_sync_background_process_updated', time(), true); |
|
| 104 | 104 | |
| 105 | 105 | // Increase the offset. |
| 106 | - if ( ( $offset + $batch_size ) < $counts[ $stage ] ) { |
|
| 107 | - update_option( '_wl_sync_background_process_offset', $offset + $batch_size, true ); |
|
| 106 | + if (($offset + $batch_size) < $counts[$stage]) { |
|
| 107 | + update_option('_wl_sync_background_process_offset', $offset + $batch_size, true); |
|
| 108 | 108 | |
| 109 | 109 | return true; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | // Increase the stage. |
| 113 | - if ( ( $stage + 1 ) < count( $this->stages ) ) { |
|
| 114 | - update_option( '_wl_sync_background_process_stage', $stage + 1, true ); |
|
| 115 | - update_option( '_wl_sync_background_process_offset', 0, true ); |
|
| 113 | + if (($stage + 1) < count($this->stages)) { |
|
| 114 | + update_option('_wl_sync_background_process_stage', $stage + 1, true); |
|
| 115 | + update_option('_wl_sync_background_process_offset', 0, true); |
|
| 116 | 116 | |
| 117 | 117 | return true; |
| 118 | 118 | } |
@@ -130,9 +130,9 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | * @return mixed |
| 132 | 132 | */ |
| 133 | - public function api_service__request( $args ) { |
|
| 133 | + public function api_service__request($args) { |
|
| 134 | 134 | |
| 135 | - $state_header_value = str_replace( "\n", '', wp_json_encode( $this->context->get_info() ) ); |
|
| 135 | + $state_header_value = str_replace("\n", '', wp_json_encode($this->context->get_info())); |
|
| 136 | 136 | |
| 137 | 137 | $args['headers']['X-Wordlift-Dataset-Sync-State-V1'] = $state_header_value; |
| 138 | 138 | |
@@ -4,29 +4,29 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface Sync_Background_Process_State { |
| 6 | 6 | |
| 7 | - function enter(); |
|
| 8 | - |
|
| 9 | - function leave(); |
|
| 10 | - |
|
| 11 | - /** |
|
| 12 | - * Task |
|
| 13 | - * |
|
| 14 | - * Override this method to perform any actions required on each |
|
| 15 | - * queue item. Return the modified item for further processing |
|
| 16 | - * in the next pass through. Or, return false to remove the |
|
| 17 | - * item from the queue. |
|
| 18 | - * |
|
| 19 | - * @param mixed $item Queue item to iterate over. |
|
| 20 | - * |
|
| 21 | - * @return mixed |
|
| 22 | - */ |
|
| 23 | - function task( $item ); |
|
| 24 | - |
|
| 25 | - function get_info(); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Try to resume an interrupted task. |
|
| 29 | - */ |
|
| 30 | - function resume(); |
|
| 7 | + function enter(); |
|
| 8 | + |
|
| 9 | + function leave(); |
|
| 10 | + |
|
| 11 | + /** |
|
| 12 | + * Task |
|
| 13 | + * |
|
| 14 | + * Override this method to perform any actions required on each |
|
| 15 | + * queue item. Return the modified item for further processing |
|
| 16 | + * in the next pass through. Or, return false to remove the |
|
| 17 | + * item from the queue. |
|
| 18 | + * |
|
| 19 | + * @param mixed $item Queue item to iterate over. |
|
| 20 | + * |
|
| 21 | + * @return mixed |
|
| 22 | + */ |
|
| 23 | + function task( $item ); |
|
| 24 | + |
|
| 25 | + function get_info(); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Try to resume an interrupted task. |
|
| 29 | + */ |
|
| 30 | + function resume(); |
|
| 31 | 31 | |
| 32 | 32 | } |
| 33 | 33 | \ No newline at end of file |