@@ -4,29 +4,29 @@ |
||
| 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 | - } |
|
| 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 | 31 | |
| 32 | 32 | } |
@@ -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 | } |
@@ -4,24 +4,24 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface Sync_Background_Process_State { |
| 6 | 6 | |
| 7 | - function enter(); |
|
| 7 | + function enter(); |
|
| 8 | 8 | |
| 9 | - function leave(); |
|
| 9 | + function leave(); |
|
| 10 | 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 ); |
|
| 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 | 24 | |
| 25 | - function get_info(); |
|
| 25 | + function get_info(); |
|
| 26 | 26 | |
| 27 | 27 | } |
| 28 | 28 | \ No newline at end of file |
@@ -20,7 +20,7 @@ |
||
| 20 | 20 | * |
| 21 | 21 | * @return mixed |
| 22 | 22 | */ |
| 23 | - function task( $item ); |
|
| 23 | + function task($item); |
|
| 24 | 24 | |
| 25 | 25 | function get_info(); |
| 26 | 26 | |
@@ -4,27 +4,27 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Sync_Background_Process_Info { |
| 6 | 6 | |
| 7 | - public $started; |
|
| 8 | - public $index; |
|
| 9 | - public $count; |
|
| 10 | - public $last_update; |
|
| 11 | - public $state; |
|
| 7 | + public $started; |
|
| 8 | + public $index; |
|
| 9 | + public $count; |
|
| 10 | + public $last_update; |
|
| 11 | + public $state; |
|
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * Sync_Model constructor. |
|
| 15 | - * |
|
| 16 | - * @param $started |
|
| 17 | - * @param $index |
|
| 18 | - * @param $count |
|
| 19 | - * @param $last_update |
|
| 20 | - * @param $state |
|
| 21 | - */ |
|
| 22 | - public function __construct( $state, $started = null, $index = null, $count = null, $last_update = null ) { |
|
| 23 | - $this->started = $started; |
|
| 24 | - $this->index = $index; |
|
| 25 | - $this->count = (int) $count; |
|
| 26 | - $this->last_update = $last_update; |
|
| 27 | - $this->state = $state; |
|
| 28 | - } |
|
| 13 | + /** |
|
| 14 | + * Sync_Model constructor. |
|
| 15 | + * |
|
| 16 | + * @param $started |
|
| 17 | + * @param $index |
|
| 18 | + * @param $count |
|
| 19 | + * @param $last_update |
|
| 20 | + * @param $state |
|
| 21 | + */ |
|
| 22 | + public function __construct( $state, $started = null, $index = null, $count = null, $last_update = null ) { |
|
| 23 | + $this->started = $started; |
|
| 24 | + $this->index = $index; |
|
| 25 | + $this->count = (int) $count; |
|
| 26 | + $this->last_update = $last_update; |
|
| 27 | + $this->state = $state; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | 30 | } |
| 31 | 31 | \ No newline at end of file |
@@ -19,7 +19,7 @@ |
||
| 19 | 19 | * @param $last_update |
| 20 | 20 | * @param $state |
| 21 | 21 | */ |
| 22 | - public function __construct( $state, $started = null, $index = null, $count = null, $last_update = null ) { |
|
| 22 | + public function __construct($state, $started = null, $index = null, $count = null, $last_update = null) { |
|
| 23 | 23 | $this->started = $started; |
| 24 | 24 | $this->index = $index; |
| 25 | 25 | $this->count = (int) $count; |
@@ -4,8 +4,8 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface Sync_Background_Process_Stage { |
| 6 | 6 | |
| 7 | - function count(); |
|
| 7 | + function count(); |
|
| 8 | 8 | |
| 9 | - function get_sync_object_adapters( $offset, $batch_size ); |
|
| 9 | + function get_sync_object_adapters( $offset, $batch_size ); |
|
| 10 | 10 | |
| 11 | 11 | } |
@@ -6,6 +6,6 @@ |
||
| 6 | 6 | |
| 7 | 7 | function count(); |
| 8 | 8 | |
| 9 | - function get_sync_object_adapters( $offset, $batch_size ); |
|
| 9 | + function get_sync_object_adapters($offset, $batch_size); |
|
| 10 | 10 | |
| 11 | 11 | } |
@@ -7,42 +7,42 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | class Sync_Background_Process_Terms_Stage { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * @var Sync_Object_Adapter_Factory |
|
| 12 | - */ |
|
| 13 | - private $sync_object_adapter_factory; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * Sync_Background_Process_Posts_Stage constructor. |
|
| 17 | - * |
|
| 18 | - * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 19 | - */ |
|
| 20 | - function __construct( $sync_object_adapter_factory ) { |
|
| 21 | - $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - function count() { |
|
| 25 | - |
|
| 26 | - $taxonomies = get_taxonomies( array( 'public' => true ) ); |
|
| 27 | - $in_taxonomies = implode( "','", array_map( 'esc_sql', $taxonomies ) ); |
|
| 28 | - |
|
| 29 | - global $wpdb; |
|
| 30 | - $sql = " |
|
| 10 | + /** |
|
| 11 | + * @var Sync_Object_Adapter_Factory |
|
| 12 | + */ |
|
| 13 | + private $sync_object_adapter_factory; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * Sync_Background_Process_Posts_Stage constructor. |
|
| 17 | + * |
|
| 18 | + * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 19 | + */ |
|
| 20 | + function __construct( $sync_object_adapter_factory ) { |
|
| 21 | + $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + function count() { |
|
| 25 | + |
|
| 26 | + $taxonomies = get_taxonomies( array( 'public' => true ) ); |
|
| 27 | + $in_taxonomies = implode( "','", array_map( 'esc_sql', $taxonomies ) ); |
|
| 28 | + |
|
| 29 | + global $wpdb; |
|
| 30 | + $sql = " |
|
| 31 | 31 | SELECT COUNT( 1 ) |
| 32 | 32 | FROM $wpdb->term_taxonomy |
| 33 | 33 | WHERE taxonomy IN ('$in_taxonomies') |
| 34 | 34 | "; |
| 35 | 35 | |
| 36 | - return $wpdb->get_var( $sql ); |
|
| 37 | - } |
|
| 36 | + return $wpdb->get_var( $sql ); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - function get_sync_object_adapters( $offset, $limit ) { |
|
| 39 | + function get_sync_object_adapters( $offset, $limit ) { |
|
| 40 | 40 | |
| 41 | - $taxonomies = get_taxonomies( array( 'public' => true ) ); |
|
| 42 | - $in_taxonomies = implode( "','", array_map( 'esc_sql', $taxonomies ) ); |
|
| 41 | + $taxonomies = get_taxonomies( array( 'public' => true ) ); |
|
| 42 | + $in_taxonomies = implode( "','", array_map( 'esc_sql', $taxonomies ) ); |
|
| 43 | 43 | |
| 44 | - global $wpdb; |
|
| 45 | - $sql = " |
|
| 44 | + global $wpdb; |
|
| 45 | + $sql = " |
|
| 46 | 46 | SELECT term_id |
| 47 | 47 | FROM $wpdb->term_taxonomy |
| 48 | 48 | WHERE taxonomy IN ('$in_taxonomies') |
@@ -50,9 +50,9 @@ discard block |
||
| 50 | 50 | LIMIT %d, %d |
| 51 | 51 | "; |
| 52 | 52 | |
| 53 | - $ids = $wpdb->get_col( $wpdb->prepare( $sql, $offset, $limit ) ); |
|
| 53 | + $ids = $wpdb->get_col( $wpdb->prepare( $sql, $offset, $limit ) ); |
|
| 54 | 54 | |
| 55 | - return $this->sync_object_adapter_factory->create_many( Object_Type_Enum::TERM, $ids ); |
|
| 56 | - } |
|
| 55 | + return $this->sync_object_adapter_factory->create_many( Object_Type_Enum::TERM, $ids ); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | } |
@@ -17,14 +17,14 @@ discard block |
||
| 17 | 17 | * |
| 18 | 18 | * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
| 19 | 19 | */ |
| 20 | - function __construct( $sync_object_adapter_factory ) { |
|
| 20 | + function __construct($sync_object_adapter_factory) { |
|
| 21 | 21 | $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | function count() { |
| 25 | 25 | |
| 26 | - $taxonomies = get_taxonomies( array( 'public' => true ) ); |
|
| 27 | - $in_taxonomies = implode( "','", array_map( 'esc_sql', $taxonomies ) ); |
|
| 26 | + $taxonomies = get_taxonomies(array('public' => true)); |
|
| 27 | + $in_taxonomies = implode("','", array_map('esc_sql', $taxonomies)); |
|
| 28 | 28 | |
| 29 | 29 | global $wpdb; |
| 30 | 30 | $sql = " |
@@ -33,13 +33,13 @@ discard block |
||
| 33 | 33 | WHERE taxonomy IN ('$in_taxonomies') |
| 34 | 34 | "; |
| 35 | 35 | |
| 36 | - return $wpdb->get_var( $sql ); |
|
| 36 | + return $wpdb->get_var($sql); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - function get_sync_object_adapters( $offset, $limit ) { |
|
| 39 | + function get_sync_object_adapters($offset, $limit) { |
|
| 40 | 40 | |
| 41 | - $taxonomies = get_taxonomies( array( 'public' => true ) ); |
|
| 42 | - $in_taxonomies = implode( "','", array_map( 'esc_sql', $taxonomies ) ); |
|
| 41 | + $taxonomies = get_taxonomies(array('public' => true)); |
|
| 42 | + $in_taxonomies = implode("','", array_map('esc_sql', $taxonomies)); |
|
| 43 | 43 | |
| 44 | 44 | global $wpdb; |
| 45 | 45 | $sql = " |
@@ -50,9 +50,9 @@ discard block |
||
| 50 | 50 | LIMIT %d, %d |
| 51 | 51 | "; |
| 52 | 52 | |
| 53 | - $ids = $wpdb->get_col( $wpdb->prepare( $sql, $offset, $limit ) ); |
|
| 53 | + $ids = $wpdb->get_col($wpdb->prepare($sql, $offset, $limit)); |
|
| 54 | 54 | |
| 55 | - return $this->sync_object_adapter_factory->create_many( Object_Type_Enum::TERM, $ids ); |
|
| 55 | + return $this->sync_object_adapter_factory->create_many(Object_Type_Enum::TERM, $ids); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | } |
@@ -8,49 +8,49 @@ discard block |
||
| 8 | 8 | |
| 9 | 9 | class Sync_Background_Process_Posts_Stage { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * @var Sync_Object_Adapter_Factory |
|
| 13 | - */ |
|
| 14 | - private $sync_object_adapter_factory; |
|
| 11 | + /** |
|
| 12 | + * @var Sync_Object_Adapter_Factory |
|
| 13 | + */ |
|
| 14 | + private $sync_object_adapter_factory; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Sync_Background_Process_Posts_Stage constructor. |
|
| 18 | - * |
|
| 19 | - * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 20 | - */ |
|
| 21 | - function __construct( $sync_object_adapter_factory ) { |
|
| 22 | - $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 23 | - } |
|
| 16 | + /** |
|
| 17 | + * Sync_Background_Process_Posts_Stage constructor. |
|
| 18 | + * |
|
| 19 | + * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 20 | + */ |
|
| 21 | + function __construct( $sync_object_adapter_factory ) { |
|
| 22 | + $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - function count() { |
|
| 25 | + function count() { |
|
| 26 | 26 | |
| 27 | - $post_types = get_post_types( array( 'public' => true ) ); |
|
| 27 | + $post_types = get_post_types( array( 'public' => true ) ); |
|
| 28 | 28 | |
| 29 | - global $wpdb; |
|
| 30 | - $in_post_type = implode( "','", array_map( 'esc_sql', $post_types ) ); |
|
| 31 | - $sql = " |
|
| 29 | + global $wpdb; |
|
| 30 | + $in_post_type = implode( "','", array_map( 'esc_sql', $post_types ) ); |
|
| 31 | + $sql = " |
|
| 32 | 32 | SELECT COUNT( 1 ) |
| 33 | 33 | FROM $wpdb->posts |
| 34 | 34 | WHERE post_type IN ('$in_post_type') |
| 35 | 35 | AND post_status IN ( 'publish', 'future', 'draft', 'pending', 'private' ) |
| 36 | 36 | "; |
| 37 | 37 | |
| 38 | - return $wpdb->get_var( $sql ); |
|
| 39 | - } |
|
| 38 | + return $wpdb->get_var( $sql ); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @param int $offset |
|
| 43 | - * @param int $limit |
|
| 44 | - * |
|
| 45 | - * @return Sync_Object_Adapter[] |
|
| 46 | - */ |
|
| 47 | - function get_sync_object_adapters( $offset, $limit ) { |
|
| 41 | + /** |
|
| 42 | + * @param int $offset |
|
| 43 | + * @param int $limit |
|
| 44 | + * |
|
| 45 | + * @return Sync_Object_Adapter[] |
|
| 46 | + */ |
|
| 47 | + function get_sync_object_adapters( $offset, $limit ) { |
|
| 48 | 48 | |
| 49 | - $post_types = get_post_types( array( 'public' => true ) ); |
|
| 49 | + $post_types = get_post_types( array( 'public' => true ) ); |
|
| 50 | 50 | |
| 51 | - global $wpdb; |
|
| 52 | - $in_post_type = implode( "','", array_map( 'esc_sql', $post_types ) ); |
|
| 53 | - $sql = " |
|
| 51 | + global $wpdb; |
|
| 52 | + $in_post_type = implode( "','", array_map( 'esc_sql', $post_types ) ); |
|
| 53 | + $sql = " |
|
| 54 | 54 | SELECT ID |
| 55 | 55 | FROM $wpdb->posts |
| 56 | 56 | WHERE post_type IN ('$in_post_type') |
@@ -59,10 +59,10 @@ discard block |
||
| 59 | 59 | LIMIT %d, %d |
| 60 | 60 | "; |
| 61 | 61 | |
| 62 | - $ids = $wpdb->get_col( $wpdb->prepare( $sql, $offset, $limit ) ); |
|
| 62 | + $ids = $wpdb->get_col( $wpdb->prepare( $sql, $offset, $limit ) ); |
|
| 63 | 63 | |
| 64 | - return $this->sync_object_adapter_factory |
|
| 65 | - ->create_many( Object_Type_Enum::POST, array_map( 'intval', $ids ) ); |
|
| 66 | - } |
|
| 64 | + return $this->sync_object_adapter_factory |
|
| 65 | + ->create_many( Object_Type_Enum::POST, array_map( 'intval', $ids ) ); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | 68 | } |
@@ -18,16 +18,16 @@ discard block |
||
| 18 | 18 | * |
| 19 | 19 | * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
| 20 | 20 | */ |
| 21 | - function __construct( $sync_object_adapter_factory ) { |
|
| 21 | + function __construct($sync_object_adapter_factory) { |
|
| 22 | 22 | $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | function count() { |
| 26 | 26 | |
| 27 | - $post_types = get_post_types( array( 'public' => true ) ); |
|
| 27 | + $post_types = get_post_types(array('public' => true)); |
|
| 28 | 28 | |
| 29 | 29 | global $wpdb; |
| 30 | - $in_post_type = implode( "','", array_map( 'esc_sql', $post_types ) ); |
|
| 30 | + $in_post_type = implode("','", array_map('esc_sql', $post_types)); |
|
| 31 | 31 | $sql = " |
| 32 | 32 | SELECT COUNT( 1 ) |
| 33 | 33 | FROM $wpdb->posts |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | AND post_status IN ( 'publish', 'future', 'draft', 'pending', 'private' ) |
| 36 | 36 | "; |
| 37 | 37 | |
| 38 | - return $wpdb->get_var( $sql ); |
|
| 38 | + return $wpdb->get_var($sql); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /** |
@@ -44,12 +44,12 @@ discard block |
||
| 44 | 44 | * |
| 45 | 45 | * @return Sync_Object_Adapter[] |
| 46 | 46 | */ |
| 47 | - function get_sync_object_adapters( $offset, $limit ) { |
|
| 47 | + function get_sync_object_adapters($offset, $limit) { |
|
| 48 | 48 | |
| 49 | - $post_types = get_post_types( array( 'public' => true ) ); |
|
| 49 | + $post_types = get_post_types(array('public' => true)); |
|
| 50 | 50 | |
| 51 | 51 | global $wpdb; |
| 52 | - $in_post_type = implode( "','", array_map( 'esc_sql', $post_types ) ); |
|
| 52 | + $in_post_type = implode("','", array_map('esc_sql', $post_types)); |
|
| 53 | 53 | $sql = " |
| 54 | 54 | SELECT ID |
| 55 | 55 | FROM $wpdb->posts |
@@ -59,10 +59,10 @@ discard block |
||
| 59 | 59 | LIMIT %d, %d |
| 60 | 60 | "; |
| 61 | 61 | |
| 62 | - $ids = $wpdb->get_col( $wpdb->prepare( $sql, $offset, $limit ) ); |
|
| 62 | + $ids = $wpdb->get_col($wpdb->prepare($sql, $offset, $limit)); |
|
| 63 | 63 | |
| 64 | 64 | return $this->sync_object_adapter_factory |
| 65 | - ->create_many( Object_Type_Enum::POST, array_map( 'intval', $ids ) ); |
|
| 65 | + ->create_many(Object_Type_Enum::POST, array_map('intval', $ids)); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | } |
@@ -7,43 +7,43 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Sync_Background_Process_Users_Stage { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * @var Sync_Object_Adapter_Factory |
|
| 12 | - */ |
|
| 13 | - private $sync_object_adapter_factory; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * Sync_Background_Process_Posts_Stage constructor. |
|
| 17 | - * |
|
| 18 | - * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 19 | - */ |
|
| 20 | - function __construct( $sync_object_adapter_factory ) { |
|
| 21 | - $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - function count() { |
|
| 25 | - |
|
| 26 | - global $wpdb; |
|
| 27 | - $sql = " |
|
| 10 | + /** |
|
| 11 | + * @var Sync_Object_Adapter_Factory |
|
| 12 | + */ |
|
| 13 | + private $sync_object_adapter_factory; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * Sync_Background_Process_Posts_Stage constructor. |
|
| 17 | + * |
|
| 18 | + * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 19 | + */ |
|
| 20 | + function __construct( $sync_object_adapter_factory ) { |
|
| 21 | + $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + function count() { |
|
| 25 | + |
|
| 26 | + global $wpdb; |
|
| 27 | + $sql = " |
|
| 28 | 28 | SELECT COUNT( 1 ) |
| 29 | 29 | FROM $wpdb->users |
| 30 | 30 | "; |
| 31 | 31 | |
| 32 | - return $wpdb->get_var( $sql ); |
|
| 33 | - } |
|
| 32 | + return $wpdb->get_var( $sql ); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - function get_sync_object_adapters( $offset, $limit ) { |
|
| 35 | + function get_sync_object_adapters( $offset, $limit ) { |
|
| 36 | 36 | |
| 37 | - global $wpdb; |
|
| 38 | - $sql = " |
|
| 37 | + global $wpdb; |
|
| 38 | + $sql = " |
|
| 39 | 39 | SELECT ID |
| 40 | 40 | FROM $wpdb->users |
| 41 | 41 | LIMIT %d, %d |
| 42 | 42 | "; |
| 43 | 43 | |
| 44 | - $ids = $wpdb->get_col( $wpdb->prepare( $sql, $offset, $limit ) ); |
|
| 44 | + $ids = $wpdb->get_col( $wpdb->prepare( $sql, $offset, $limit ) ); |
|
| 45 | 45 | |
| 46 | - return $this->sync_object_adapter_factory->create_many( Object_Type_Enum::USER, $ids ); |
|
| 47 | - } |
|
| 46 | + return $this->sync_object_adapter_factory->create_many( Object_Type_Enum::USER, $ids ); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | } |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | * |
| 18 | 18 | * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
| 19 | 19 | */ |
| 20 | - function __construct( $sync_object_adapter_factory ) { |
|
| 20 | + function __construct($sync_object_adapter_factory) { |
|
| 21 | 21 | $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
| 22 | 22 | } |
| 23 | 23 | |
@@ -29,10 +29,10 @@ discard block |
||
| 29 | 29 | FROM $wpdb->users |
| 30 | 30 | "; |
| 31 | 31 | |
| 32 | - return $wpdb->get_var( $sql ); |
|
| 32 | + return $wpdb->get_var($sql); |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - function get_sync_object_adapters( $offset, $limit ) { |
|
| 35 | + function get_sync_object_adapters($offset, $limit) { |
|
| 36 | 36 | |
| 37 | 37 | global $wpdb; |
| 38 | 38 | $sql = " |
@@ -41,9 +41,9 @@ discard block |
||
| 41 | 41 | LIMIT %d, %d |
| 42 | 42 | "; |
| 43 | 43 | |
| 44 | - $ids = $wpdb->get_col( $wpdb->prepare( $sql, $offset, $limit ) ); |
|
| 44 | + $ids = $wpdb->get_col($wpdb->prepare($sql, $offset, $limit)); |
|
| 45 | 45 | |
| 46 | - return $this->sync_object_adapter_factory->create_many( Object_Type_Enum::USER, $ids ); |
|
| 46 | + return $this->sync_object_adapter_factory->create_many(Object_Type_Enum::USER, $ids); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | } |
@@ -18,112 +18,112 @@ |
||
| 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 | - /** |
|
| 104 | - * Get the current state. |
|
| 105 | - * |
|
| 106 | - * @return string Either self::STARTED_STATE or self::STOPPED_STATE (default). |
|
| 107 | - */ |
|
| 108 | - public function get_state() { |
|
| 109 | - return get_option( '_wl_sync_background_process_state', self::STATE_STOPPED ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Persist the current state. |
|
| 114 | - * |
|
| 115 | - * @param string $value |
|
| 116 | - * |
|
| 117 | - * @return bool |
|
| 118 | - */ |
|
| 119 | - public function set_state( $value ) { |
|
| 120 | - return null === $value |
|
| 121 | - ? delete_option( '_wl_sync_background_process_state' ) |
|
| 122 | - : update_option( '_wl_sync_background_process_state', $value, true ); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - public function get_info() { |
|
| 126 | - return $this->state->get_info(); |
|
| 127 | - } |
|
| 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 | + /** |
|
| 104 | + * Get the current state. |
|
| 105 | + * |
|
| 106 | + * @return string Either self::STARTED_STATE or self::STOPPED_STATE (default). |
|
| 107 | + */ |
|
| 108 | + public function get_state() { |
|
| 109 | + return get_option( '_wl_sync_background_process_state', self::STATE_STOPPED ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Persist the current state. |
|
| 114 | + * |
|
| 115 | + * @param string $value |
|
| 116 | + * |
|
| 117 | + * @return bool |
|
| 118 | + */ |
|
| 119 | + public function set_state( $value ) { |
|
| 120 | + return null === $value |
|
| 121 | + ? delete_option( '_wl_sync_background_process_state' ) |
|
| 122 | + : update_option( '_wl_sync_background_process_state', $value, true ); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + public function get_info() { |
|
| 126 | + return $this->state->get_info(); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | 129 | } |
@@ -49,19 +49,19 @@ discard block |
||
| 49 | 49 | * @param Sync_Service $sync_service A {@link Sync_Service} instance providing the supporting functions to this background process. |
| 50 | 50 | * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
| 51 | 51 | */ |
| 52 | - public function __construct( $sync_service, $sync_object_adapter_factory ) { |
|
| 52 | + public function __construct($sync_service, $sync_object_adapter_factory) { |
|
| 53 | 53 | parent::__construct(); |
| 54 | 54 | |
| 55 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 55 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 56 | 56 | |
| 57 | 57 | $this->sync_service = $sync_service; |
| 58 | 58 | $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
| 59 | 59 | |
| 60 | 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 ); |
|
| 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 | 63 | } else { |
| 64 | - $this->state = new Sync_Background_Process_Stopped_State( $this ); |
|
| 64 | + $this->state = new Sync_Background_Process_Stopped_State($this); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | } |
@@ -77,9 +77,9 @@ discard block |
||
| 77 | 77 | * |
| 78 | 78 | * @return int[]|false The next post IDs or false if there are no more. |
| 79 | 79 | */ |
| 80 | - protected function task( $item ) { |
|
| 80 | + protected function task($item) { |
|
| 81 | 81 | |
| 82 | - return $this->state->task( $item ); |
|
| 82 | + return $this->state->task($item); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | public function start() { |
| 89 | 89 | $this->state->leave(); |
| 90 | - $this->state = new Sync_Background_Process_Started_State( $this, $this->sync_service, $this->sync_object_adapter_factory ); |
|
| 90 | + $this->state = new Sync_Background_Process_Started_State($this, $this->sync_service, $this->sync_object_adapter_factory); |
|
| 91 | 91 | $this->state->enter(); |
| 92 | 92 | } |
| 93 | 93 | |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | */ |
| 97 | 97 | public function stop() { |
| 98 | 98 | $this->state->leave(); |
| 99 | - $this->state = new Sync_Background_Process_Stopped_State( $this ); |
|
| 99 | + $this->state = new Sync_Background_Process_Stopped_State($this); |
|
| 100 | 100 | $this->state->enter(); |
| 101 | 101 | } |
| 102 | 102 | |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | * @return string Either self::STARTED_STATE or self::STOPPED_STATE (default). |
| 107 | 107 | */ |
| 108 | 108 | public function get_state() { |
| 109 | - return get_option( '_wl_sync_background_process_state', self::STATE_STOPPED ); |
|
| 109 | + return get_option('_wl_sync_background_process_state', self::STATE_STOPPED); |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
@@ -116,10 +116,10 @@ discard block |
||
| 116 | 116 | * |
| 117 | 117 | * @return bool |
| 118 | 118 | */ |
| 119 | - public function set_state( $value ) { |
|
| 119 | + public function set_state($value) { |
|
| 120 | 120 | return null === $value |
| 121 | - ? delete_option( '_wl_sync_background_process_state' ) |
|
| 122 | - : update_option( '_wl_sync_background_process_state', $value, true ); |
|
| 121 | + ? delete_option('_wl_sync_background_process_state') |
|
| 122 | + : update_option('_wl_sync_background_process_state', $value, true); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | public function get_info() { |
@@ -14,288 +14,288 @@ discard block |
||
| 14 | 14 | |
| 15 | 15 | class Analysis_Response_Ops { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The analysis response json. |
|
| 19 | - * |
|
| 20 | - * @since 3.21.5 |
|
| 21 | - * @access private |
|
| 22 | - * @var mixed $json Holds the analysis response json. |
|
| 23 | - */ |
|
| 24 | - private $json; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Holds the {@link Wordlift_Entity_Uri_Service}. |
|
| 28 | - * |
|
| 29 | - * @since 3.21.5 |
|
| 30 | - * @access private |
|
| 31 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 32 | - */ |
|
| 33 | - private $entity_uri_service; |
|
| 34 | - |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var \Wordlift_Entity_Type_Service |
|
| 39 | - */ |
|
| 40 | - private $entity_type_service; |
|
| 41 | - /** |
|
| 42 | - * @var \Wordlift_Post_Image_Storage |
|
| 43 | - */ |
|
| 44 | - private $post_image_storage; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var Entity_Helper |
|
| 48 | - */ |
|
| 49 | - private $entity_helper; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Analysis_Response_Ops constructor. |
|
| 53 | - * |
|
| 54 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service}. |
|
| 55 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service}. |
|
| 56 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service}. |
|
| 57 | - * @param \Wordlift_Post_Image_Storage $post_image_storage A {@link Wordlift_Post_Image_Storage} instance. |
|
| 58 | - * @param Entity_Helper $entity_helper The {@link Entity_Helper}. |
|
| 59 | - * @param mixed $json The analysis response json. |
|
| 60 | - * |
|
| 61 | - * @since 3.21.5 |
|
| 62 | - */ |
|
| 63 | - public function __construct( $entity_uri_service, $entity_service, $entity_type_service, $post_image_storage, $entity_helper, $json ) { |
|
| 64 | - |
|
| 65 | - $this->json = $json; |
|
| 66 | - $this->entity_uri_service = $entity_uri_service; |
|
| 67 | - $this->entity_service = $entity_service; |
|
| 68 | - $this->entity_type_service = $entity_type_service; |
|
| 69 | - $this->post_image_storage = $post_image_storage; |
|
| 70 | - $this->entity_helper = $entity_helper; |
|
| 71 | - |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Switches remote entities, i.e. entities with id outside the local dataset, to local entities. |
|
| 76 | - * |
|
| 77 | - * The function takes all the entities that have an id which is not local. For each remote entity, a list of URIs |
|
| 78 | - * is built comprising the entity id and the sameAs. Then a query is issued in the local database to find potential |
|
| 79 | - * matches from the local vocabulary. |
|
| 80 | - * |
|
| 81 | - * If found, the entity id is swapped with the local id and the remote id is added to the sameAs. |
|
| 82 | - * |
|
| 83 | - * @return Analysis_Response_Ops The current Analysis_Response_Ops instance. |
|
| 84 | - */ |
|
| 85 | - public function make_entities_local() { |
|
| 86 | - |
|
| 87 | - if ( ! isset( $this->json->entities ) ) { |
|
| 88 | - return $this; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - // Get the URIs. |
|
| 92 | - $uris = array_keys( get_object_vars( $this->json->entities ) ); |
|
| 93 | - $mappings = $this->entity_helper->map_many_to_local( $uris ); |
|
| 94 | - |
|
| 95 | - foreach ( $mappings as $external_uri => $internal_uri ) { |
|
| 96 | - |
|
| 97 | - // Move the data from the external URI to the internal URI. |
|
| 98 | - if ( ! isset( $this->json->entities->{$internal_uri} ) ) { |
|
| 99 | - $this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri}; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // Ensure sameAs is an array. |
|
| 103 | - if ( ! isset( $this->json->entities->{$internal_uri}->sameAs ) |
|
| 104 | - || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) { |
|
| 105 | - $this->json->entities->{$internal_uri}->sameAs = array(); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Add the external URI as sameAs. |
|
| 109 | - $this->json->entities->{$internal_uri}->sameAs[] = $external_uri; |
|
| 110 | - |
|
| 111 | - // Finally remove the external URI. |
|
| 112 | - unset( $this->json->entities->{$external_uri} ); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - if ( isset( $this->json->annotations ) ) { |
|
| 116 | - foreach ( $this->json->annotations as $key => $annotation ) { |
|
| 117 | - if ( isset( $annotation->entityMatches ) ) { |
|
| 118 | - foreach ( $annotation->entityMatches as $match ) { |
|
| 119 | - if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) { |
|
| 120 | - $match->entityId = $mappings[ $match->entityId ]; |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - return $this; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Add occurrences by parsing the provided html content. |
|
| 132 | - * |
|
| 133 | - * @param string $content The html content with annotations. |
|
| 134 | - * |
|
| 135 | - * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
|
| 136 | - * |
|
| 137 | - * @since 3.23.7 refactor the regex pattern to take into account that there might be css classes between textannotation |
|
| 138 | - * and disambiguated. |
|
| 139 | - * |
|
| 140 | - * @link https://github.com/insideout10/wordlift-plugin/issues/1001 |
|
| 141 | - */ |
|
| 142 | - public function add_occurrences( $content ) { |
|
| 143 | - |
|
| 144 | - // Try to get all the disambiguated annotations and bail out if an error occurs. |
|
| 145 | - if ( false === preg_match_all( |
|
| 146 | - '|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|', |
|
| 147 | - $content, |
|
| 148 | - $matches, |
|
| 149 | - PREG_OFFSET_CAPTURE |
|
| 150 | - ) ) { |
|
| 151 | - return $this; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - if ( empty( $matches ) ) { |
|
| 155 | - return $this; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - $parse_data = array_reduce( range( 0, count( $matches[1] ) - 1 ), function ( $carry, $i ) use ( $matches ) { |
|
| 159 | - if ( empty( $matches[0] ) ) { |
|
| 160 | - return $carry; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $start = $matches[0][ $i ][1]; |
|
| 164 | - $end = $start + strlen( $matches[0][ $i ][0] ); |
|
| 165 | - $annotation_id = $matches[1][ $i ][0]; |
|
| 166 | - $item_id = $matches[2][ $i ][0]; |
|
| 167 | - $text = $matches[3][ $i ][0]; |
|
| 168 | - |
|
| 169 | - $annotation = new StdClass; |
|
| 170 | - $annotation->annotationId = $annotation_id; |
|
| 171 | - $annotation->start = $start; |
|
| 172 | - $annotation->end = $end; |
|
| 173 | - $annotation->text = $text; |
|
| 174 | - |
|
| 175 | - $entity_match = new StdClass; |
|
| 176 | - $entity_match->confidence = 100; |
|
| 177 | - $entity_match->entityId = $item_id; |
|
| 178 | - $annotation->entityMatches[] = $entity_match; |
|
| 179 | - |
|
| 180 | - $carry['annotations'][ $annotation_id ] = $annotation; |
|
| 181 | - $carry['occurrences'][ $item_id ][] = $annotation_id; |
|
| 182 | - |
|
| 183 | - return $carry; |
|
| 184 | - }, array( 'annotations' => array(), 'occurrences' => array(), ) ); |
|
| 185 | - |
|
| 186 | - $annotations = $parse_data['annotations']; |
|
| 187 | - $occurrences = $parse_data['occurrences']; |
|
| 188 | - |
|
| 189 | - foreach ( array_keys( $occurrences ) as $item_id ) { |
|
| 190 | - |
|
| 191 | - // If the entity isn't there, add it. |
|
| 192 | - if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) { |
|
| 193 | - $entity = $this->get_local_entity( $item_id ); |
|
| 194 | - |
|
| 195 | - // Entity not found in the local vocabulary, continue to the next one. |
|
| 196 | - if ( false === $entity ) { |
|
| 197 | - continue; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $this->json->entities->{$item_id} = $entity; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - // Here we're adding back some data structures required by the client-side code. |
|
| 205 | - // |
|
| 206 | - // We're adding: |
|
| 207 | - // 1. the .entities[entity_id].occurrences array with the annotations' ids. |
|
| 208 | - // 2. the .entities[entity_id].annotations[annotation_id] = { id: annotation_id } map. |
|
| 209 | - // |
|
| 210 | - // Before 3.23.0 this was done by the client-side code located in src/coffee/editpost-widget/app.services.AnalysisService.coffee |
|
| 211 | - // function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in |
|
| 212 | - // `embedAnalysis`. |
|
| 213 | - |
|
| 214 | - if ( ! is_bool( $this->json ) ) { |
|
| 215 | - foreach ( $this->json->entities as $id => $entity ) { |
|
| 216 | - $this->json->entities->{$id}->occurrences = isset( $occurrences[ $id ] ) ? $occurrences[ $id ] : array();; |
|
| 217 | - |
|
| 218 | - foreach ( $this->json->entities->{$id}->occurrences as $annotation_id ) { |
|
| 219 | - $this->json->entities->{$id}->annotations[ $annotation_id ] = array( |
|
| 220 | - 'id' => $annotation_id, |
|
| 221 | - ); |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // Add the missing annotations. This allows the analysis response to work also if we didn't receive results |
|
| 227 | - // from the analysis API. |
|
| 228 | - foreach ( $annotations as $annotation_id => $annotation ) { |
|
| 229 | - |
|
| 230 | - if ( ! is_bool( $this->json ) && ! isset( $this->json->annotations->{$annotation_id} ) ) { |
|
| 231 | - $this->json->annotations->{$annotation_id} = $annotation; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - return $this; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Add local entities |
|
| 241 | - * |
|
| 242 | - * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
|
| 243 | - * |
|
| 244 | - * @since 3.27.6 |
|
| 245 | - * |
|
| 246 | - * @link https://github.com/insideout10/wordlift-plugin/issues/1178 |
|
| 247 | - */ |
|
| 248 | - public function add_local_entities() { |
|
| 249 | - |
|
| 250 | - // Populating the local entities object |
|
| 251 | - $entities = array(); |
|
| 252 | - foreach ( $this->json->annotations as $annotation ) { |
|
| 253 | - foreach ( $annotation->entityMatches as $entity_matches ) { |
|
| 254 | - |
|
| 255 | - $entity_id = url_to_postid( htmlspecialchars_decode( $entity_matches->entityId ) ); |
|
| 256 | - $serialized_entity = wl_serialize_entity( $entity_id ); |
|
| 257 | - |
|
| 258 | - if ( $serialized_entity ) { |
|
| 259 | - $serialized_entity['entityId'] = $serialized_entity['id']; |
|
| 260 | - unset( $serialized_entity['id'] ); |
|
| 261 | - |
|
| 262 | - $entities[ $entity_matches->entityId ] = $serialized_entity; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - // Adding occurrences and annotations data structures required by the client-side code. |
|
| 269 | - foreach ( $entities as $entity_id => $entity ) { |
|
| 270 | - foreach ( $this->json->annotations as $annotation ) { |
|
| 271 | - if ( $annotation->entityMatches[0]->entityId === $entity_id ) { |
|
| 272 | - $entities[ $entity_id ]['occurrences'][] = $annotation->annotationId; |
|
| 273 | - $entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId; |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - $this->json->entities = $entities; |
|
| 279 | - |
|
| 280 | - return $this; |
|
| 281 | - |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - private function get_local_entity( $uri ) { |
|
| 285 | - |
|
| 286 | - $entity = $this->entity_uri_service->get_entity( $uri ); |
|
| 287 | - |
|
| 288 | - if ( null === $entity ) { |
|
| 289 | - return false; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - $type = $this->entity_type_service->get( $entity->ID ); |
|
| 293 | - $images = $this->post_image_storage->get( $entity->ID ); |
|
| 294 | - |
|
| 295 | - return (object) array( |
|
| 296 | - 'id' => $uri, |
|
| 297 | - 'label' => $entity->post_title, |
|
| 298 | - /* |
|
| 17 | + /** |
|
| 18 | + * The analysis response json. |
|
| 19 | + * |
|
| 20 | + * @since 3.21.5 |
|
| 21 | + * @access private |
|
| 22 | + * @var mixed $json Holds the analysis response json. |
|
| 23 | + */ |
|
| 24 | + private $json; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Holds the {@link Wordlift_Entity_Uri_Service}. |
|
| 28 | + * |
|
| 29 | + * @since 3.21.5 |
|
| 30 | + * @access private |
|
| 31 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 32 | + */ |
|
| 33 | + private $entity_uri_service; |
|
| 34 | + |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var \Wordlift_Entity_Type_Service |
|
| 39 | + */ |
|
| 40 | + private $entity_type_service; |
|
| 41 | + /** |
|
| 42 | + * @var \Wordlift_Post_Image_Storage |
|
| 43 | + */ |
|
| 44 | + private $post_image_storage; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var Entity_Helper |
|
| 48 | + */ |
|
| 49 | + private $entity_helper; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Analysis_Response_Ops constructor. |
|
| 53 | + * |
|
| 54 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service}. |
|
| 55 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service}. |
|
| 56 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service}. |
|
| 57 | + * @param \Wordlift_Post_Image_Storage $post_image_storage A {@link Wordlift_Post_Image_Storage} instance. |
|
| 58 | + * @param Entity_Helper $entity_helper The {@link Entity_Helper}. |
|
| 59 | + * @param mixed $json The analysis response json. |
|
| 60 | + * |
|
| 61 | + * @since 3.21.5 |
|
| 62 | + */ |
|
| 63 | + public function __construct( $entity_uri_service, $entity_service, $entity_type_service, $post_image_storage, $entity_helper, $json ) { |
|
| 64 | + |
|
| 65 | + $this->json = $json; |
|
| 66 | + $this->entity_uri_service = $entity_uri_service; |
|
| 67 | + $this->entity_service = $entity_service; |
|
| 68 | + $this->entity_type_service = $entity_type_service; |
|
| 69 | + $this->post_image_storage = $post_image_storage; |
|
| 70 | + $this->entity_helper = $entity_helper; |
|
| 71 | + |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Switches remote entities, i.e. entities with id outside the local dataset, to local entities. |
|
| 76 | + * |
|
| 77 | + * The function takes all the entities that have an id which is not local. For each remote entity, a list of URIs |
|
| 78 | + * is built comprising the entity id and the sameAs. Then a query is issued in the local database to find potential |
|
| 79 | + * matches from the local vocabulary. |
|
| 80 | + * |
|
| 81 | + * If found, the entity id is swapped with the local id and the remote id is added to the sameAs. |
|
| 82 | + * |
|
| 83 | + * @return Analysis_Response_Ops The current Analysis_Response_Ops instance. |
|
| 84 | + */ |
|
| 85 | + public function make_entities_local() { |
|
| 86 | + |
|
| 87 | + if ( ! isset( $this->json->entities ) ) { |
|
| 88 | + return $this; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + // Get the URIs. |
|
| 92 | + $uris = array_keys( get_object_vars( $this->json->entities ) ); |
|
| 93 | + $mappings = $this->entity_helper->map_many_to_local( $uris ); |
|
| 94 | + |
|
| 95 | + foreach ( $mappings as $external_uri => $internal_uri ) { |
|
| 96 | + |
|
| 97 | + // Move the data from the external URI to the internal URI. |
|
| 98 | + if ( ! isset( $this->json->entities->{$internal_uri} ) ) { |
|
| 99 | + $this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri}; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // Ensure sameAs is an array. |
|
| 103 | + if ( ! isset( $this->json->entities->{$internal_uri}->sameAs ) |
|
| 104 | + || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) { |
|
| 105 | + $this->json->entities->{$internal_uri}->sameAs = array(); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Add the external URI as sameAs. |
|
| 109 | + $this->json->entities->{$internal_uri}->sameAs[] = $external_uri; |
|
| 110 | + |
|
| 111 | + // Finally remove the external URI. |
|
| 112 | + unset( $this->json->entities->{$external_uri} ); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + if ( isset( $this->json->annotations ) ) { |
|
| 116 | + foreach ( $this->json->annotations as $key => $annotation ) { |
|
| 117 | + if ( isset( $annotation->entityMatches ) ) { |
|
| 118 | + foreach ( $annotation->entityMatches as $match ) { |
|
| 119 | + if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) { |
|
| 120 | + $match->entityId = $mappings[ $match->entityId ]; |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + return $this; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Add occurrences by parsing the provided html content. |
|
| 132 | + * |
|
| 133 | + * @param string $content The html content with annotations. |
|
| 134 | + * |
|
| 135 | + * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
|
| 136 | + * |
|
| 137 | + * @since 3.23.7 refactor the regex pattern to take into account that there might be css classes between textannotation |
|
| 138 | + * and disambiguated. |
|
| 139 | + * |
|
| 140 | + * @link https://github.com/insideout10/wordlift-plugin/issues/1001 |
|
| 141 | + */ |
|
| 142 | + public function add_occurrences( $content ) { |
|
| 143 | + |
|
| 144 | + // Try to get all the disambiguated annotations and bail out if an error occurs. |
|
| 145 | + if ( false === preg_match_all( |
|
| 146 | + '|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|', |
|
| 147 | + $content, |
|
| 148 | + $matches, |
|
| 149 | + PREG_OFFSET_CAPTURE |
|
| 150 | + ) ) { |
|
| 151 | + return $this; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + if ( empty( $matches ) ) { |
|
| 155 | + return $this; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + $parse_data = array_reduce( range( 0, count( $matches[1] ) - 1 ), function ( $carry, $i ) use ( $matches ) { |
|
| 159 | + if ( empty( $matches[0] ) ) { |
|
| 160 | + return $carry; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $start = $matches[0][ $i ][1]; |
|
| 164 | + $end = $start + strlen( $matches[0][ $i ][0] ); |
|
| 165 | + $annotation_id = $matches[1][ $i ][0]; |
|
| 166 | + $item_id = $matches[2][ $i ][0]; |
|
| 167 | + $text = $matches[3][ $i ][0]; |
|
| 168 | + |
|
| 169 | + $annotation = new StdClass; |
|
| 170 | + $annotation->annotationId = $annotation_id; |
|
| 171 | + $annotation->start = $start; |
|
| 172 | + $annotation->end = $end; |
|
| 173 | + $annotation->text = $text; |
|
| 174 | + |
|
| 175 | + $entity_match = new StdClass; |
|
| 176 | + $entity_match->confidence = 100; |
|
| 177 | + $entity_match->entityId = $item_id; |
|
| 178 | + $annotation->entityMatches[] = $entity_match; |
|
| 179 | + |
|
| 180 | + $carry['annotations'][ $annotation_id ] = $annotation; |
|
| 181 | + $carry['occurrences'][ $item_id ][] = $annotation_id; |
|
| 182 | + |
|
| 183 | + return $carry; |
|
| 184 | + }, array( 'annotations' => array(), 'occurrences' => array(), ) ); |
|
| 185 | + |
|
| 186 | + $annotations = $parse_data['annotations']; |
|
| 187 | + $occurrences = $parse_data['occurrences']; |
|
| 188 | + |
|
| 189 | + foreach ( array_keys( $occurrences ) as $item_id ) { |
|
| 190 | + |
|
| 191 | + // If the entity isn't there, add it. |
|
| 192 | + if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) { |
|
| 193 | + $entity = $this->get_local_entity( $item_id ); |
|
| 194 | + |
|
| 195 | + // Entity not found in the local vocabulary, continue to the next one. |
|
| 196 | + if ( false === $entity ) { |
|
| 197 | + continue; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $this->json->entities->{$item_id} = $entity; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + // Here we're adding back some data structures required by the client-side code. |
|
| 205 | + // |
|
| 206 | + // We're adding: |
|
| 207 | + // 1. the .entities[entity_id].occurrences array with the annotations' ids. |
|
| 208 | + // 2. the .entities[entity_id].annotations[annotation_id] = { id: annotation_id } map. |
|
| 209 | + // |
|
| 210 | + // Before 3.23.0 this was done by the client-side code located in src/coffee/editpost-widget/app.services.AnalysisService.coffee |
|
| 211 | + // function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in |
|
| 212 | + // `embedAnalysis`. |
|
| 213 | + |
|
| 214 | + if ( ! is_bool( $this->json ) ) { |
|
| 215 | + foreach ( $this->json->entities as $id => $entity ) { |
|
| 216 | + $this->json->entities->{$id}->occurrences = isset( $occurrences[ $id ] ) ? $occurrences[ $id ] : array();; |
|
| 217 | + |
|
| 218 | + foreach ( $this->json->entities->{$id}->occurrences as $annotation_id ) { |
|
| 219 | + $this->json->entities->{$id}->annotations[ $annotation_id ] = array( |
|
| 220 | + 'id' => $annotation_id, |
|
| 221 | + ); |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // Add the missing annotations. This allows the analysis response to work also if we didn't receive results |
|
| 227 | + // from the analysis API. |
|
| 228 | + foreach ( $annotations as $annotation_id => $annotation ) { |
|
| 229 | + |
|
| 230 | + if ( ! is_bool( $this->json ) && ! isset( $this->json->annotations->{$annotation_id} ) ) { |
|
| 231 | + $this->json->annotations->{$annotation_id} = $annotation; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + return $this; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Add local entities |
|
| 241 | + * |
|
| 242 | + * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
|
| 243 | + * |
|
| 244 | + * @since 3.27.6 |
|
| 245 | + * |
|
| 246 | + * @link https://github.com/insideout10/wordlift-plugin/issues/1178 |
|
| 247 | + */ |
|
| 248 | + public function add_local_entities() { |
|
| 249 | + |
|
| 250 | + // Populating the local entities object |
|
| 251 | + $entities = array(); |
|
| 252 | + foreach ( $this->json->annotations as $annotation ) { |
|
| 253 | + foreach ( $annotation->entityMatches as $entity_matches ) { |
|
| 254 | + |
|
| 255 | + $entity_id = url_to_postid( htmlspecialchars_decode( $entity_matches->entityId ) ); |
|
| 256 | + $serialized_entity = wl_serialize_entity( $entity_id ); |
|
| 257 | + |
|
| 258 | + if ( $serialized_entity ) { |
|
| 259 | + $serialized_entity['entityId'] = $serialized_entity['id']; |
|
| 260 | + unset( $serialized_entity['id'] ); |
|
| 261 | + |
|
| 262 | + $entities[ $entity_matches->entityId ] = $serialized_entity; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + // Adding occurrences and annotations data structures required by the client-side code. |
|
| 269 | + foreach ( $entities as $entity_id => $entity ) { |
|
| 270 | + foreach ( $this->json->annotations as $annotation ) { |
|
| 271 | + if ( $annotation->entityMatches[0]->entityId === $entity_id ) { |
|
| 272 | + $entities[ $entity_id ]['occurrences'][] = $annotation->annotationId; |
|
| 273 | + $entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId; |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + $this->json->entities = $entities; |
|
| 279 | + |
|
| 280 | + return $this; |
|
| 281 | + |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + private function get_local_entity( $uri ) { |
|
| 285 | + |
|
| 286 | + $entity = $this->entity_uri_service->get_entity( $uri ); |
|
| 287 | + |
|
| 288 | + if ( null === $entity ) { |
|
| 289 | + return false; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + $type = $this->entity_type_service->get( $entity->ID ); |
|
| 293 | + $images = $this->post_image_storage->get( $entity->ID ); |
|
| 294 | + |
|
| 295 | + return (object) array( |
|
| 296 | + 'id' => $uri, |
|
| 297 | + 'label' => $entity->post_title, |
|
| 298 | + /* |
|
| 299 | 299 | * As of 2020.06.29 we're comment out the `post_content` because Gutenberg posts will return here |
| 300 | 300 | * the whole Gutenberg source including potentially our own wordlift/classification block, which means |
| 301 | 301 | * that data may grow quickly to more than a 100 KBytes and could break web servers. |
@@ -307,38 +307,38 @@ discard block |
||
| 307 | 307 | * |
| 308 | 308 | * PS: We didn't test using the WordLift Post Excerpt Helper. |
| 309 | 309 | */ |
| 310 | - // 'description' => $entity->post_content, |
|
| 311 | - 'description' => '', |
|
| 312 | - 'sameAs' => wl_schema_get_value( $entity->ID, 'sameAs' ), |
|
| 313 | - 'mainType' => str_replace( 'wl-', '', $type['css_class'] ), |
|
| 314 | - 'types' => wl_get_entity_rdf_types( $entity->ID ), |
|
| 315 | - 'images' => $images, |
|
| 316 | - ); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Return the JSON response. |
|
| 321 | - * |
|
| 322 | - * @return mixed The JSON response. |
|
| 323 | - * @since 3.24.2 |
|
| 324 | - */ |
|
| 325 | - public function get_json() { |
|
| 326 | - |
|
| 327 | - return $this->json; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Get the string representation of the JSON. |
|
| 332 | - * |
|
| 333 | - * @return false|string The string representation or false in case of error. |
|
| 334 | - */ |
|
| 335 | - public function to_string() { |
|
| 336 | - |
|
| 337 | - // Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+. |
|
| 338 | - $options = ( version_compare( PHP_VERSION, '5.4', '>=' ) |
|
| 339 | - ? 256 : 0 ); |
|
| 340 | - |
|
| 341 | - return wp_json_encode( $this->json, $options ); |
|
| 342 | - } |
|
| 310 | + // 'description' => $entity->post_content, |
|
| 311 | + 'description' => '', |
|
| 312 | + 'sameAs' => wl_schema_get_value( $entity->ID, 'sameAs' ), |
|
| 313 | + 'mainType' => str_replace( 'wl-', '', $type['css_class'] ), |
|
| 314 | + 'types' => wl_get_entity_rdf_types( $entity->ID ), |
|
| 315 | + 'images' => $images, |
|
| 316 | + ); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Return the JSON response. |
|
| 321 | + * |
|
| 322 | + * @return mixed The JSON response. |
|
| 323 | + * @since 3.24.2 |
|
| 324 | + */ |
|
| 325 | + public function get_json() { |
|
| 326 | + |
|
| 327 | + return $this->json; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Get the string representation of the JSON. |
|
| 332 | + * |
|
| 333 | + * @return false|string The string representation or false in case of error. |
|
| 334 | + */ |
|
| 335 | + public function to_string() { |
|
| 336 | + |
|
| 337 | + // Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+. |
|
| 338 | + $options = ( version_compare( PHP_VERSION, '5.4', '>=' ) |
|
| 339 | + ? 256 : 0 ); |
|
| 340 | + |
|
| 341 | + return wp_json_encode( $this->json, $options ); |
|
| 342 | + } |
|
| 343 | 343 | |
| 344 | 344 | } |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | * |
| 61 | 61 | * @since 3.21.5 |
| 62 | 62 | */ |
| 63 | - public function __construct( $entity_uri_service, $entity_service, $entity_type_service, $post_image_storage, $entity_helper, $json ) { |
|
| 63 | + public function __construct($entity_uri_service, $entity_service, $entity_type_service, $post_image_storage, $entity_helper, $json) { |
|
| 64 | 64 | |
| 65 | 65 | $this->json = $json; |
| 66 | 66 | $this->entity_uri_service = $entity_uri_service; |
@@ -84,24 +84,24 @@ discard block |
||
| 84 | 84 | */ |
| 85 | 85 | public function make_entities_local() { |
| 86 | 86 | |
| 87 | - if ( ! isset( $this->json->entities ) ) { |
|
| 87 | + if ( ! isset($this->json->entities)) { |
|
| 88 | 88 | return $this; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | // Get the URIs. |
| 92 | - $uris = array_keys( get_object_vars( $this->json->entities ) ); |
|
| 93 | - $mappings = $this->entity_helper->map_many_to_local( $uris ); |
|
| 92 | + $uris = array_keys(get_object_vars($this->json->entities)); |
|
| 93 | + $mappings = $this->entity_helper->map_many_to_local($uris); |
|
| 94 | 94 | |
| 95 | - foreach ( $mappings as $external_uri => $internal_uri ) { |
|
| 95 | + foreach ($mappings as $external_uri => $internal_uri) { |
|
| 96 | 96 | |
| 97 | 97 | // Move the data from the external URI to the internal URI. |
| 98 | - if ( ! isset( $this->json->entities->{$internal_uri} ) ) { |
|
| 98 | + if ( ! isset($this->json->entities->{$internal_uri} )) { |
|
| 99 | 99 | $this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri}; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | // Ensure sameAs is an array. |
| 103 | - if ( ! isset( $this->json->entities->{$internal_uri}->sameAs ) |
|
| 104 | - || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) { |
|
| 103 | + if ( ! isset($this->json->entities->{$internal_uri}->sameAs) |
|
| 104 | + || ! is_array($this->json->entities->{$internal_uri}->sameAs)) { |
|
| 105 | 105 | $this->json->entities->{$internal_uri}->sameAs = array(); |
| 106 | 106 | } |
| 107 | 107 | |
@@ -109,15 +109,15 @@ discard block |
||
| 109 | 109 | $this->json->entities->{$internal_uri}->sameAs[] = $external_uri; |
| 110 | 110 | |
| 111 | 111 | // Finally remove the external URI. |
| 112 | - unset( $this->json->entities->{$external_uri} ); |
|
| 112 | + unset($this->json->entities->{$external_uri} ); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - if ( isset( $this->json->annotations ) ) { |
|
| 116 | - foreach ( $this->json->annotations as $key => $annotation ) { |
|
| 117 | - if ( isset( $annotation->entityMatches ) ) { |
|
| 118 | - foreach ( $annotation->entityMatches as $match ) { |
|
| 119 | - if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) { |
|
| 120 | - $match->entityId = $mappings[ $match->entityId ]; |
|
| 115 | + if (isset($this->json->annotations)) { |
|
| 116 | + foreach ($this->json->annotations as $key => $annotation) { |
|
| 117 | + if (isset($annotation->entityMatches)) { |
|
| 118 | + foreach ($annotation->entityMatches as $match) { |
|
| 119 | + if (isset($match->entityId) && isset($mappings[$match->entityId])) { |
|
| 120 | + $match->entityId = $mappings[$match->entityId]; |
|
| 121 | 121 | } |
| 122 | 122 | } |
| 123 | 123 | } |
@@ -139,32 +139,32 @@ discard block |
||
| 139 | 139 | * |
| 140 | 140 | * @link https://github.com/insideout10/wordlift-plugin/issues/1001 |
| 141 | 141 | */ |
| 142 | - public function add_occurrences( $content ) { |
|
| 142 | + public function add_occurrences($content) { |
|
| 143 | 143 | |
| 144 | 144 | // Try to get all the disambiguated annotations and bail out if an error occurs. |
| 145 | - if ( false === preg_match_all( |
|
| 145 | + if (false === preg_match_all( |
|
| 146 | 146 | '|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|', |
| 147 | 147 | $content, |
| 148 | 148 | $matches, |
| 149 | 149 | PREG_OFFSET_CAPTURE |
| 150 | - ) ) { |
|
| 150 | + )) { |
|
| 151 | 151 | return $this; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | - if ( empty( $matches ) ) { |
|
| 154 | + if (empty($matches)) { |
|
| 155 | 155 | return $this; |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - $parse_data = array_reduce( range( 0, count( $matches[1] ) - 1 ), function ( $carry, $i ) use ( $matches ) { |
|
| 159 | - if ( empty( $matches[0] ) ) { |
|
| 158 | + $parse_data = array_reduce(range(0, count($matches[1]) - 1), function($carry, $i) use ($matches) { |
|
| 159 | + if (empty($matches[0])) { |
|
| 160 | 160 | return $carry; |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - $start = $matches[0][ $i ][1]; |
|
| 164 | - $end = $start + strlen( $matches[0][ $i ][0] ); |
|
| 165 | - $annotation_id = $matches[1][ $i ][0]; |
|
| 166 | - $item_id = $matches[2][ $i ][0]; |
|
| 167 | - $text = $matches[3][ $i ][0]; |
|
| 163 | + $start = $matches[0][$i][1]; |
|
| 164 | + $end = $start + strlen($matches[0][$i][0]); |
|
| 165 | + $annotation_id = $matches[1][$i][0]; |
|
| 166 | + $item_id = $matches[2][$i][0]; |
|
| 167 | + $text = $matches[3][$i][0]; |
|
| 168 | 168 | |
| 169 | 169 | $annotation = new StdClass; |
| 170 | 170 | $annotation->annotationId = $annotation_id; |
@@ -177,23 +177,23 @@ discard block |
||
| 177 | 177 | $entity_match->entityId = $item_id; |
| 178 | 178 | $annotation->entityMatches[] = $entity_match; |
| 179 | 179 | |
| 180 | - $carry['annotations'][ $annotation_id ] = $annotation; |
|
| 181 | - $carry['occurrences'][ $item_id ][] = $annotation_id; |
|
| 180 | + $carry['annotations'][$annotation_id] = $annotation; |
|
| 181 | + $carry['occurrences'][$item_id][] = $annotation_id; |
|
| 182 | 182 | |
| 183 | 183 | return $carry; |
| 184 | - }, array( 'annotations' => array(), 'occurrences' => array(), ) ); |
|
| 184 | + }, array('annotations' => array(), 'occurrences' => array(),)); |
|
| 185 | 185 | |
| 186 | 186 | $annotations = $parse_data['annotations']; |
| 187 | 187 | $occurrences = $parse_data['occurrences']; |
| 188 | 188 | |
| 189 | - foreach ( array_keys( $occurrences ) as $item_id ) { |
|
| 189 | + foreach (array_keys($occurrences) as $item_id) { |
|
| 190 | 190 | |
| 191 | 191 | // If the entity isn't there, add it. |
| 192 | - if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) { |
|
| 193 | - $entity = $this->get_local_entity( $item_id ); |
|
| 192 | + if ( ! is_bool($this->json) && ! isset($this->json->entities->{$item_id} )) { |
|
| 193 | + $entity = $this->get_local_entity($item_id); |
|
| 194 | 194 | |
| 195 | 195 | // Entity not found in the local vocabulary, continue to the next one. |
| 196 | - if ( false === $entity ) { |
|
| 196 | + if (false === $entity) { |
|
| 197 | 197 | continue; |
| 198 | 198 | } |
| 199 | 199 | |
@@ -211,12 +211,12 @@ discard block |
||
| 211 | 211 | // function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in |
| 212 | 212 | // `embedAnalysis`. |
| 213 | 213 | |
| 214 | - if ( ! is_bool( $this->json ) ) { |
|
| 215 | - foreach ( $this->json->entities as $id => $entity ) { |
|
| 216 | - $this->json->entities->{$id}->occurrences = isset( $occurrences[ $id ] ) ? $occurrences[ $id ] : array();; |
|
| 214 | + if ( ! is_bool($this->json)) { |
|
| 215 | + foreach ($this->json->entities as $id => $entity) { |
|
| 216 | + $this->json->entities->{$id}->occurrences = isset($occurrences[$id]) ? $occurrences[$id] : array(); ; |
|
| 217 | 217 | |
| 218 | - foreach ( $this->json->entities->{$id}->occurrences as $annotation_id ) { |
|
| 219 | - $this->json->entities->{$id}->annotations[ $annotation_id ] = array( |
|
| 218 | + foreach ($this->json->entities->{$id}->occurrences as $annotation_id) { |
|
| 219 | + $this->json->entities->{$id}->annotations[$annotation_id] = array( |
|
| 220 | 220 | 'id' => $annotation_id, |
| 221 | 221 | ); |
| 222 | 222 | } |
@@ -225,9 +225,9 @@ discard block |
||
| 225 | 225 | |
| 226 | 226 | // Add the missing annotations. This allows the analysis response to work also if we didn't receive results |
| 227 | 227 | // from the analysis API. |
| 228 | - foreach ( $annotations as $annotation_id => $annotation ) { |
|
| 228 | + foreach ($annotations as $annotation_id => $annotation) { |
|
| 229 | 229 | |
| 230 | - if ( ! is_bool( $this->json ) && ! isset( $this->json->annotations->{$annotation_id} ) ) { |
|
| 230 | + if ( ! is_bool($this->json) && ! isset($this->json->annotations->{$annotation_id} )) { |
|
| 231 | 231 | $this->json->annotations->{$annotation_id} = $annotation; |
| 232 | 232 | } |
| 233 | 233 | |
@@ -249,28 +249,28 @@ discard block |
||
| 249 | 249 | |
| 250 | 250 | // Populating the local entities object |
| 251 | 251 | $entities = array(); |
| 252 | - foreach ( $this->json->annotations as $annotation ) { |
|
| 253 | - foreach ( $annotation->entityMatches as $entity_matches ) { |
|
| 252 | + foreach ($this->json->annotations as $annotation) { |
|
| 253 | + foreach ($annotation->entityMatches as $entity_matches) { |
|
| 254 | 254 | |
| 255 | - $entity_id = url_to_postid( htmlspecialchars_decode( $entity_matches->entityId ) ); |
|
| 256 | - $serialized_entity = wl_serialize_entity( $entity_id ); |
|
| 255 | + $entity_id = url_to_postid(htmlspecialchars_decode($entity_matches->entityId)); |
|
| 256 | + $serialized_entity = wl_serialize_entity($entity_id); |
|
| 257 | 257 | |
| 258 | - if ( $serialized_entity ) { |
|
| 258 | + if ($serialized_entity) { |
|
| 259 | 259 | $serialized_entity['entityId'] = $serialized_entity['id']; |
| 260 | - unset( $serialized_entity['id'] ); |
|
| 260 | + unset($serialized_entity['id']); |
|
| 261 | 261 | |
| 262 | - $entities[ $entity_matches->entityId ] = $serialized_entity; |
|
| 262 | + $entities[$entity_matches->entityId] = $serialized_entity; |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | } |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | // Adding occurrences and annotations data structures required by the client-side code. |
| 269 | - foreach ( $entities as $entity_id => $entity ) { |
|
| 270 | - foreach ( $this->json->annotations as $annotation ) { |
|
| 271 | - if ( $annotation->entityMatches[0]->entityId === $entity_id ) { |
|
| 272 | - $entities[ $entity_id ]['occurrences'][] = $annotation->annotationId; |
|
| 273 | - $entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId; |
|
| 269 | + foreach ($entities as $entity_id => $entity) { |
|
| 270 | + foreach ($this->json->annotations as $annotation) { |
|
| 271 | + if ($annotation->entityMatches[0]->entityId === $entity_id) { |
|
| 272 | + $entities[$entity_id]['occurrences'][] = $annotation->annotationId; |
|
| 273 | + $entities[$entity_id]['annotations'][$annotation->annotationId]['id'] = $annotation->annotationId; |
|
| 274 | 274 | } |
| 275 | 275 | } |
| 276 | 276 | } |
@@ -281,16 +281,16 @@ discard block |
||
| 281 | 281 | |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | - private function get_local_entity( $uri ) { |
|
| 284 | + private function get_local_entity($uri) { |
|
| 285 | 285 | |
| 286 | - $entity = $this->entity_uri_service->get_entity( $uri ); |
|
| 286 | + $entity = $this->entity_uri_service->get_entity($uri); |
|
| 287 | 287 | |
| 288 | - if ( null === $entity ) { |
|
| 288 | + if (null === $entity) { |
|
| 289 | 289 | return false; |
| 290 | 290 | } |
| 291 | 291 | |
| 292 | - $type = $this->entity_type_service->get( $entity->ID ); |
|
| 293 | - $images = $this->post_image_storage->get( $entity->ID ); |
|
| 292 | + $type = $this->entity_type_service->get($entity->ID); |
|
| 293 | + $images = $this->post_image_storage->get($entity->ID); |
|
| 294 | 294 | |
| 295 | 295 | return (object) array( |
| 296 | 296 | 'id' => $uri, |
@@ -309,9 +309,9 @@ discard block |
||
| 309 | 309 | */ |
| 310 | 310 | // 'description' => $entity->post_content, |
| 311 | 311 | 'description' => '', |
| 312 | - 'sameAs' => wl_schema_get_value( $entity->ID, 'sameAs' ), |
|
| 313 | - 'mainType' => str_replace( 'wl-', '', $type['css_class'] ), |
|
| 314 | - 'types' => wl_get_entity_rdf_types( $entity->ID ), |
|
| 312 | + 'sameAs' => wl_schema_get_value($entity->ID, 'sameAs'), |
|
| 313 | + 'mainType' => str_replace('wl-', '', $type['css_class']), |
|
| 314 | + 'types' => wl_get_entity_rdf_types($entity->ID), |
|
| 315 | 315 | 'images' => $images, |
| 316 | 316 | ); |
| 317 | 317 | } |
@@ -335,10 +335,10 @@ discard block |
||
| 335 | 335 | public function to_string() { |
| 336 | 336 | |
| 337 | 337 | // Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+. |
| 338 | - $options = ( version_compare( PHP_VERSION, '5.4', '>=' ) |
|
| 339 | - ? 256 : 0 ); |
|
| 338 | + $options = (version_compare(PHP_VERSION, '5.4', '>=') |
|
| 339 | + ? 256 : 0); |
|
| 340 | 340 | |
| 341 | - return wp_json_encode( $this->json, $options ); |
|
| 341 | + return wp_json_encode($this->json, $options); |
|
| 342 | 342 | } |
| 343 | 343 | |
| 344 | 344 | } |