@@ -104,6 +104,9 @@ discard block |
||
| 104 | 104 | |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | + /** |
|
| 108 | + * @param string $entity_uri |
|
| 109 | + */ |
|
| 107 | 110 | private function context_data_by_entity_uri( $entity_uri ) { |
| 108 | 111 | |
| 109 | 112 | $entity_id = $this->url_to_postid( $entity_uri ); |
@@ -113,6 +116,9 @@ discard block |
||
| 113 | 116 | |
| 114 | 117 | } |
| 115 | 118 | |
| 119 | + /** |
|
| 120 | + * @param string $entity_sameas |
|
| 121 | + */ |
|
| 116 | 122 | private function context_data_by_sameas( $entity_sameas ) { |
| 117 | 123 | |
| 118 | 124 | // Look for an entity. |
@@ -129,6 +135,9 @@ discard block |
||
| 129 | 135 | |
| 130 | 136 | } |
| 131 | 137 | |
| 138 | + /** |
|
| 139 | + * @param string $entity_uri |
|
| 140 | + */ |
|
| 132 | 141 | private function context_data_by_remote($entity_uri){ |
| 133 | 142 | |
| 134 | 143 | $entity_id = $this->url_to_postid( $entity_uri ); |
@@ -160,6 +169,9 @@ discard block |
||
| 160 | 169 | } |
| 161 | 170 | } |
| 162 | 171 | |
| 172 | + /** |
|
| 173 | + * @return integer|null |
|
| 174 | + */ |
|
| 163 | 175 | private function url_to_postid( $url ) { |
| 164 | 176 | // Try with url_to_postid |
| 165 | 177 | $post_id = url_to_postid( $url ); |
@@ -9,169 +9,169 @@ |
||
| 9 | 9 | |
| 10 | 10 | class Wordlift_Context_Cards_Service { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - private $endpoint; |
|
| 16 | - private $base_url; |
|
| 17 | - private $filtered_base_url; |
|
| 18 | - |
|
| 19 | - function __construct() { |
|
| 20 | - |
|
| 21 | - $this->endpoint = '/context-card'; |
|
| 22 | - $this->base_url = get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint; |
|
| 23 | - $this->filtered_base_url = apply_filters( 'wl_context_cards_base_url', $this->base_url ); |
|
| 24 | - |
|
| 25 | - // PHP 5.3 compatibility as `$this` cannot be used in closures. |
|
| 26 | - $that = $this; |
|
| 27 | - |
|
| 28 | - add_action( 'rest_api_init', function () use ( $that ) { |
|
| 29 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array( |
|
| 30 | - 'methods' => 'GET', |
|
| 31 | - 'callback' => array( $that, 'context_data' ), |
|
| 32 | - ) ); |
|
| 33 | - } ); |
|
| 34 | - |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - function format_response( $jsonld, $publisher = true ) { |
|
| 38 | - |
|
| 39 | - $response = array(); |
|
| 40 | - |
|
| 41 | - if ( ! isset( $jsonld ) || empty( $jsonld ) || empty( $jsonld[0] ) ) { |
|
| 42 | - return null; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - if ( isset( $jsonld[0]['description'] ) && ! empty( $jsonld[0]['description'] ) ) { |
|
| 46 | - if ( isset( $jsonld[0]['name'] ) && ! empty( $jsonld[0]['name'] ) ) { |
|
| 47 | - $title = $jsonld[0]['name']; |
|
| 48 | - $pos = strpos( $jsonld[0]['description'], $title ); |
|
| 49 | - $response['description'] = $jsonld[0]['description']; |
|
| 50 | - if ( $pos !== false ) { |
|
| 51 | - $response['description'] = substr_replace( $response['description'], "<strong>$title</strong>", $pos, strlen( $title ) ); |
|
| 52 | - } |
|
| 53 | - } else { |
|
| 54 | - $response['description'] = $jsonld[0]['description']; |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - if ( isset( $jsonld[0]['name'] ) && ! empty( $jsonld[0]['name'] ) ) { |
|
| 59 | - $response['title'] = $jsonld[0]['name']; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - if ( isset( $jsonld[0]['url'] ) && ! empty( $jsonld[0]['url'] ) ) { |
|
| 63 | - $response['url'] = $jsonld[0]['url']; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if ( isset( $jsonld[0]['image'] ) && |
|
| 67 | - isset( $jsonld[0]['image'][0]['url'] ) && |
|
| 68 | - isset( $jsonld[0]['image'][0]['width'] ) && |
|
| 69 | - isset( $jsonld[0]['image'][0]['height'] ) |
|
| 70 | - ) { |
|
| 71 | - $response['image'] = array( |
|
| 72 | - 'url' => $jsonld[0]['image'][0]['url'], |
|
| 73 | - 'width' => $jsonld[0]['image'][0]['width'], |
|
| 74 | - 'height' => $jsonld[0]['image'][0]['height'], |
|
| 75 | - ); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - if ( $publisher ) { |
|
| 79 | - $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id(); |
|
| 80 | - $publisher_jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $publisher_id ); |
|
| 81 | - $response['publisher'] = $this->format_response( $publisher_jsonld, false ); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - return $response; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - public function context_data( $request ) { |
|
| 88 | - |
|
| 89 | - $entity_uri = urldecode( $request->get_param( 'entity_url' ) ); |
|
| 90 | - $entity_sameas = urldecode( $request->get_param( 'id' ) ); |
|
| 91 | - |
|
| 92 | - if ( isset( $entity_uri ) ) { |
|
| 93 | - if ( $this->base_url !== $this->filtered_base_url ) { |
|
| 94 | - $remote_response = $this->context_data_by_remote( $entity_uri ); |
|
| 95 | - if( isset($remote_response) && !empty($remote_response) ){ |
|
| 96 | - return $remote_response; |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - return $this->context_data_by_entity_uri( $entity_uri ); |
|
| 100 | - } |
|
| 101 | - if ( isset( $entity_sameas ) ) { |
|
| 102 | - return $this->context_data_by_sameas( $entity_sameas ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - private function context_data_by_entity_uri( $entity_uri ) { |
|
| 108 | - |
|
| 109 | - $entity_id = $this->url_to_postid( $entity_uri ); |
|
| 110 | - $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $entity_id ); |
|
| 111 | - |
|
| 112 | - return $this->format_response( $jsonld ); |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - private function context_data_by_sameas( $entity_sameas ) { |
|
| 117 | - |
|
| 118 | - // Look for an entity. |
|
| 119 | - foreach ( $_REQUEST['id'] as $id ) { |
|
| 120 | - $post = Wordlift_Entity_Service::get_instance() |
|
| 121 | - ->get_entity_post_by_uri( $id ); |
|
| 122 | - |
|
| 123 | - if ( null !== $post ) { |
|
| 124 | - $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $post->ID ); |
|
| 125 | - |
|
| 126 | - return $this->format_response( $jsonld ); |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - private function context_data_by_remote($entity_uri){ |
|
| 133 | - |
|
| 134 | - $entity_id = $this->url_to_postid( $entity_uri ); |
|
| 135 | - |
|
| 136 | - $entity_sameas = get_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
| 137 | - $url = $this->filtered_base_url; |
|
| 138 | - $target_url = array_reduce( $entity_sameas , function ( $initial, $this_uri ) { |
|
| 139 | - return $initial . '&id[]=' . urlencode( $this_uri ); |
|
| 140 | - }, $url ); |
|
| 141 | - |
|
| 142 | - $response = wp_remote_get($target_url); |
|
| 143 | - if ( is_array( $response ) && ! is_wp_error( $response ) ) { |
|
| 144 | - return $response['body']; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - return; |
|
| 148 | - |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - public function enqueue_scripts() { |
|
| 152 | - $show_context_cards = true; |
|
| 153 | - $show_context_cards = apply_filters( 'wl_show_context_cards', $show_context_cards ); |
|
| 154 | - if ( $show_context_cards ) { |
|
| 155 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 156 | - wp_localize_script( 'wordlift-cloud', 'wlCloudContextCards', array( |
|
| 157 | - 'selector' => 'a.wl-entity-page-link', |
|
| 158 | - 'baseUrl' => $this->base_url |
|
| 159 | - ) ); |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - private function url_to_postid( $url ) { |
|
| 164 | - // Try with url_to_postid |
|
| 165 | - $post_id = url_to_postid( $url ); |
|
| 166 | - if ( $post_id == 0 ) { |
|
| 167 | - // Try with get_page_by_path |
|
| 168 | - $post = get_page_by_path( basename( untrailingslashit( $url ) ), OBJECT, 'entity' ); |
|
| 169 | - if ( $post ) { |
|
| 170 | - $post_id = $post->ID; |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - return $post_id; |
|
| 175 | - } |
|
| 12 | + /** |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + private $endpoint; |
|
| 16 | + private $base_url; |
|
| 17 | + private $filtered_base_url; |
|
| 18 | + |
|
| 19 | + function __construct() { |
|
| 20 | + |
|
| 21 | + $this->endpoint = '/context-card'; |
|
| 22 | + $this->base_url = get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint; |
|
| 23 | + $this->filtered_base_url = apply_filters( 'wl_context_cards_base_url', $this->base_url ); |
|
| 24 | + |
|
| 25 | + // PHP 5.3 compatibility as `$this` cannot be used in closures. |
|
| 26 | + $that = $this; |
|
| 27 | + |
|
| 28 | + add_action( 'rest_api_init', function () use ( $that ) { |
|
| 29 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array( |
|
| 30 | + 'methods' => 'GET', |
|
| 31 | + 'callback' => array( $that, 'context_data' ), |
|
| 32 | + ) ); |
|
| 33 | + } ); |
|
| 34 | + |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + function format_response( $jsonld, $publisher = true ) { |
|
| 38 | + |
|
| 39 | + $response = array(); |
|
| 40 | + |
|
| 41 | + if ( ! isset( $jsonld ) || empty( $jsonld ) || empty( $jsonld[0] ) ) { |
|
| 42 | + return null; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + if ( isset( $jsonld[0]['description'] ) && ! empty( $jsonld[0]['description'] ) ) { |
|
| 46 | + if ( isset( $jsonld[0]['name'] ) && ! empty( $jsonld[0]['name'] ) ) { |
|
| 47 | + $title = $jsonld[0]['name']; |
|
| 48 | + $pos = strpos( $jsonld[0]['description'], $title ); |
|
| 49 | + $response['description'] = $jsonld[0]['description']; |
|
| 50 | + if ( $pos !== false ) { |
|
| 51 | + $response['description'] = substr_replace( $response['description'], "<strong>$title</strong>", $pos, strlen( $title ) ); |
|
| 52 | + } |
|
| 53 | + } else { |
|
| 54 | + $response['description'] = $jsonld[0]['description']; |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + if ( isset( $jsonld[0]['name'] ) && ! empty( $jsonld[0]['name'] ) ) { |
|
| 59 | + $response['title'] = $jsonld[0]['name']; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + if ( isset( $jsonld[0]['url'] ) && ! empty( $jsonld[0]['url'] ) ) { |
|
| 63 | + $response['url'] = $jsonld[0]['url']; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if ( isset( $jsonld[0]['image'] ) && |
|
| 67 | + isset( $jsonld[0]['image'][0]['url'] ) && |
|
| 68 | + isset( $jsonld[0]['image'][0]['width'] ) && |
|
| 69 | + isset( $jsonld[0]['image'][0]['height'] ) |
|
| 70 | + ) { |
|
| 71 | + $response['image'] = array( |
|
| 72 | + 'url' => $jsonld[0]['image'][0]['url'], |
|
| 73 | + 'width' => $jsonld[0]['image'][0]['width'], |
|
| 74 | + 'height' => $jsonld[0]['image'][0]['height'], |
|
| 75 | + ); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + if ( $publisher ) { |
|
| 79 | + $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id(); |
|
| 80 | + $publisher_jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $publisher_id ); |
|
| 81 | + $response['publisher'] = $this->format_response( $publisher_jsonld, false ); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + return $response; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + public function context_data( $request ) { |
|
| 88 | + |
|
| 89 | + $entity_uri = urldecode( $request->get_param( 'entity_url' ) ); |
|
| 90 | + $entity_sameas = urldecode( $request->get_param( 'id' ) ); |
|
| 91 | + |
|
| 92 | + if ( isset( $entity_uri ) ) { |
|
| 93 | + if ( $this->base_url !== $this->filtered_base_url ) { |
|
| 94 | + $remote_response = $this->context_data_by_remote( $entity_uri ); |
|
| 95 | + if( isset($remote_response) && !empty($remote_response) ){ |
|
| 96 | + return $remote_response; |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + return $this->context_data_by_entity_uri( $entity_uri ); |
|
| 100 | + } |
|
| 101 | + if ( isset( $entity_sameas ) ) { |
|
| 102 | + return $this->context_data_by_sameas( $entity_sameas ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + private function context_data_by_entity_uri( $entity_uri ) { |
|
| 108 | + |
|
| 109 | + $entity_id = $this->url_to_postid( $entity_uri ); |
|
| 110 | + $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $entity_id ); |
|
| 111 | + |
|
| 112 | + return $this->format_response( $jsonld ); |
|
| 113 | + |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + private function context_data_by_sameas( $entity_sameas ) { |
|
| 117 | + |
|
| 118 | + // Look for an entity. |
|
| 119 | + foreach ( $_REQUEST['id'] as $id ) { |
|
| 120 | + $post = Wordlift_Entity_Service::get_instance() |
|
| 121 | + ->get_entity_post_by_uri( $id ); |
|
| 122 | + |
|
| 123 | + if ( null !== $post ) { |
|
| 124 | + $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $post->ID ); |
|
| 125 | + |
|
| 126 | + return $this->format_response( $jsonld ); |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + private function context_data_by_remote($entity_uri){ |
|
| 133 | + |
|
| 134 | + $entity_id = $this->url_to_postid( $entity_uri ); |
|
| 135 | + |
|
| 136 | + $entity_sameas = get_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
| 137 | + $url = $this->filtered_base_url; |
|
| 138 | + $target_url = array_reduce( $entity_sameas , function ( $initial, $this_uri ) { |
|
| 139 | + return $initial . '&id[]=' . urlencode( $this_uri ); |
|
| 140 | + }, $url ); |
|
| 141 | + |
|
| 142 | + $response = wp_remote_get($target_url); |
|
| 143 | + if ( is_array( $response ) && ! is_wp_error( $response ) ) { |
|
| 144 | + return $response['body']; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + return; |
|
| 148 | + |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + public function enqueue_scripts() { |
|
| 152 | + $show_context_cards = true; |
|
| 153 | + $show_context_cards = apply_filters( 'wl_show_context_cards', $show_context_cards ); |
|
| 154 | + if ( $show_context_cards ) { |
|
| 155 | + wp_enqueue_script( 'wordlift-cloud' ); |
|
| 156 | + wp_localize_script( 'wordlift-cloud', 'wlCloudContextCards', array( |
|
| 157 | + 'selector' => 'a.wl-entity-page-link', |
|
| 158 | + 'baseUrl' => $this->base_url |
|
| 159 | + ) ); |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + private function url_to_postid( $url ) { |
|
| 164 | + // Try with url_to_postid |
|
| 165 | + $post_id = url_to_postid( $url ); |
|
| 166 | + if ( $post_id == 0 ) { |
|
| 167 | + // Try with get_page_by_path |
|
| 168 | + $post = get_page_by_path( basename( untrailingslashit( $url ) ), OBJECT, 'entity' ); |
|
| 169 | + if ( $post ) { |
|
| 170 | + $post_id = $post->ID; |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + return $post_id; |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | 177 | } |
@@ -19,54 +19,54 @@ discard block |
||
| 19 | 19 | function __construct() { |
| 20 | 20 | |
| 21 | 21 | $this->endpoint = '/context-card'; |
| 22 | - $this->base_url = get_rest_url() . WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint; |
|
| 23 | - $this->filtered_base_url = apply_filters( 'wl_context_cards_base_url', $this->base_url ); |
|
| 22 | + $this->base_url = get_rest_url().WL_REST_ROUTE_DEFAULT_NAMESPACE.$this->endpoint; |
|
| 23 | + $this->filtered_base_url = apply_filters('wl_context_cards_base_url', $this->base_url); |
|
| 24 | 24 | |
| 25 | 25 | // PHP 5.3 compatibility as `$this` cannot be used in closures. |
| 26 | 26 | $that = $this; |
| 27 | 27 | |
| 28 | - add_action( 'rest_api_init', function () use ( $that ) { |
|
| 29 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array( |
|
| 28 | + add_action('rest_api_init', function() use ($that) { |
|
| 29 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, $that->endpoint, array( |
|
| 30 | 30 | 'methods' => 'GET', |
| 31 | - 'callback' => array( $that, 'context_data' ), |
|
| 32 | - ) ); |
|
| 31 | + 'callback' => array($that, 'context_data'), |
|
| 32 | + )); |
|
| 33 | 33 | } ); |
| 34 | 34 | |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - function format_response( $jsonld, $publisher = true ) { |
|
| 37 | + function format_response($jsonld, $publisher = true) { |
|
| 38 | 38 | |
| 39 | 39 | $response = array(); |
| 40 | 40 | |
| 41 | - if ( ! isset( $jsonld ) || empty( $jsonld ) || empty( $jsonld[0] ) ) { |
|
| 41 | + if ( ! isset($jsonld) || empty($jsonld) || empty($jsonld[0])) { |
|
| 42 | 42 | return null; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - if ( isset( $jsonld[0]['description'] ) && ! empty( $jsonld[0]['description'] ) ) { |
|
| 46 | - if ( isset( $jsonld[0]['name'] ) && ! empty( $jsonld[0]['name'] ) ) { |
|
| 45 | + if (isset($jsonld[0]['description']) && ! empty($jsonld[0]['description'])) { |
|
| 46 | + if (isset($jsonld[0]['name']) && ! empty($jsonld[0]['name'])) { |
|
| 47 | 47 | $title = $jsonld[0]['name']; |
| 48 | - $pos = strpos( $jsonld[0]['description'], $title ); |
|
| 48 | + $pos = strpos($jsonld[0]['description'], $title); |
|
| 49 | 49 | $response['description'] = $jsonld[0]['description']; |
| 50 | - if ( $pos !== false ) { |
|
| 51 | - $response['description'] = substr_replace( $response['description'], "<strong>$title</strong>", $pos, strlen( $title ) ); |
|
| 50 | + if ($pos !== false) { |
|
| 51 | + $response['description'] = substr_replace($response['description'], "<strong>$title</strong>", $pos, strlen($title)); |
|
| 52 | 52 | } |
| 53 | 53 | } else { |
| 54 | 54 | $response['description'] = $jsonld[0]['description']; |
| 55 | 55 | } |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - if ( isset( $jsonld[0]['name'] ) && ! empty( $jsonld[0]['name'] ) ) { |
|
| 58 | + if (isset($jsonld[0]['name']) && ! empty($jsonld[0]['name'])) { |
|
| 59 | 59 | $response['title'] = $jsonld[0]['name']; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - if ( isset( $jsonld[0]['url'] ) && ! empty( $jsonld[0]['url'] ) ) { |
|
| 62 | + if (isset($jsonld[0]['url']) && ! empty($jsonld[0]['url'])) { |
|
| 63 | 63 | $response['url'] = $jsonld[0]['url']; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - if ( isset( $jsonld[0]['image'] ) && |
|
| 67 | - isset( $jsonld[0]['image'][0]['url'] ) && |
|
| 68 | - isset( $jsonld[0]['image'][0]['width'] ) && |
|
| 69 | - isset( $jsonld[0]['image'][0]['height'] ) |
|
| 66 | + if (isset($jsonld[0]['image']) && |
|
| 67 | + isset($jsonld[0]['image'][0]['url']) && |
|
| 68 | + isset($jsonld[0]['image'][0]['width']) && |
|
| 69 | + isset($jsonld[0]['image'][0]['height']) |
|
| 70 | 70 | ) { |
| 71 | 71 | $response['image'] = array( |
| 72 | 72 | 'url' => $jsonld[0]['image'][0]['url'], |
@@ -75,72 +75,72 @@ discard block |
||
| 75 | 75 | ); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - if ( $publisher ) { |
|
| 78 | + if ($publisher) { |
|
| 79 | 79 | $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id(); |
| 80 | - $publisher_jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $publisher_id ); |
|
| 81 | - $response['publisher'] = $this->format_response( $publisher_jsonld, false ); |
|
| 80 | + $publisher_jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld(false, $publisher_id); |
|
| 81 | + $response['publisher'] = $this->format_response($publisher_jsonld, false); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | return $response; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - public function context_data( $request ) { |
|
| 87 | + public function context_data($request) { |
|
| 88 | 88 | |
| 89 | - $entity_uri = urldecode( $request->get_param( 'entity_url' ) ); |
|
| 90 | - $entity_sameas = urldecode( $request->get_param( 'id' ) ); |
|
| 89 | + $entity_uri = urldecode($request->get_param('entity_url')); |
|
| 90 | + $entity_sameas = urldecode($request->get_param('id')); |
|
| 91 | 91 | |
| 92 | - if ( isset( $entity_uri ) ) { |
|
| 93 | - if ( $this->base_url !== $this->filtered_base_url ) { |
|
| 94 | - $remote_response = $this->context_data_by_remote( $entity_uri ); |
|
| 95 | - if( isset($remote_response) && !empty($remote_response) ){ |
|
| 92 | + if (isset($entity_uri)) { |
|
| 93 | + if ($this->base_url !== $this->filtered_base_url) { |
|
| 94 | + $remote_response = $this->context_data_by_remote($entity_uri); |
|
| 95 | + if (isset($remote_response) && ! empty($remote_response)) { |
|
| 96 | 96 | return $remote_response; |
| 97 | 97 | } |
| 98 | 98 | } |
| 99 | - return $this->context_data_by_entity_uri( $entity_uri ); |
|
| 99 | + return $this->context_data_by_entity_uri($entity_uri); |
|
| 100 | 100 | } |
| 101 | - if ( isset( $entity_sameas ) ) { |
|
| 102 | - return $this->context_data_by_sameas( $entity_sameas ); |
|
| 101 | + if (isset($entity_sameas)) { |
|
| 102 | + return $this->context_data_by_sameas($entity_sameas); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - private function context_data_by_entity_uri( $entity_uri ) { |
|
| 107 | + private function context_data_by_entity_uri($entity_uri) { |
|
| 108 | 108 | |
| 109 | - $entity_id = $this->url_to_postid( $entity_uri ); |
|
| 110 | - $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $entity_id ); |
|
| 109 | + $entity_id = $this->url_to_postid($entity_uri); |
|
| 110 | + $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld(false, $entity_id); |
|
| 111 | 111 | |
| 112 | - return $this->format_response( $jsonld ); |
|
| 112 | + return $this->format_response($jsonld); |
|
| 113 | 113 | |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - private function context_data_by_sameas( $entity_sameas ) { |
|
| 116 | + private function context_data_by_sameas($entity_sameas) { |
|
| 117 | 117 | |
| 118 | 118 | // Look for an entity. |
| 119 | - foreach ( $_REQUEST['id'] as $id ) { |
|
| 119 | + foreach ($_REQUEST['id'] as $id) { |
|
| 120 | 120 | $post = Wordlift_Entity_Service::get_instance() |
| 121 | - ->get_entity_post_by_uri( $id ); |
|
| 121 | + ->get_entity_post_by_uri($id); |
|
| 122 | 122 | |
| 123 | - if ( null !== $post ) { |
|
| 124 | - $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld( false, $post->ID ); |
|
| 123 | + if (null !== $post) { |
|
| 124 | + $jsonld = Wordlift_Jsonld_Service::get_instance()->get_jsonld(false, $post->ID); |
|
| 125 | 125 | |
| 126 | - return $this->format_response( $jsonld ); |
|
| 126 | + return $this->format_response($jsonld); |
|
| 127 | 127 | } |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - private function context_data_by_remote($entity_uri){ |
|
| 132 | + private function context_data_by_remote($entity_uri) { |
|
| 133 | 133 | |
| 134 | - $entity_id = $this->url_to_postid( $entity_uri ); |
|
| 134 | + $entity_id = $this->url_to_postid($entity_uri); |
|
| 135 | 135 | |
| 136 | - $entity_sameas = get_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
| 136 | + $entity_sameas = get_post_meta($entity_id, Wordlift_Schema_Service::FIELD_SAME_AS); |
|
| 137 | 137 | $url = $this->filtered_base_url; |
| 138 | - $target_url = array_reduce( $entity_sameas , function ( $initial, $this_uri ) { |
|
| 139 | - return $initial . '&id[]=' . urlencode( $this_uri ); |
|
| 140 | - }, $url ); |
|
| 138 | + $target_url = array_reduce($entity_sameas, function($initial, $this_uri) { |
|
| 139 | + return $initial.'&id[]='.urlencode($this_uri); |
|
| 140 | + }, $url); |
|
| 141 | 141 | |
| 142 | 142 | $response = wp_remote_get($target_url); |
| 143 | - if ( is_array( $response ) && ! is_wp_error( $response ) ) { |
|
| 143 | + if (is_array($response) && ! is_wp_error($response)) { |
|
| 144 | 144 | return $response['body']; |
| 145 | 145 | } |
| 146 | 146 | |
@@ -150,23 +150,23 @@ discard block |
||
| 150 | 150 | |
| 151 | 151 | public function enqueue_scripts() { |
| 152 | 152 | $show_context_cards = true; |
| 153 | - $show_context_cards = apply_filters( 'wl_show_context_cards', $show_context_cards ); |
|
| 154 | - if ( $show_context_cards ) { |
|
| 155 | - wp_enqueue_script( 'wordlift-cloud' ); |
|
| 156 | - wp_localize_script( 'wordlift-cloud', 'wlCloudContextCards', array( |
|
| 153 | + $show_context_cards = apply_filters('wl_show_context_cards', $show_context_cards); |
|
| 154 | + if ($show_context_cards) { |
|
| 155 | + wp_enqueue_script('wordlift-cloud'); |
|
| 156 | + wp_localize_script('wordlift-cloud', 'wlCloudContextCards', array( |
|
| 157 | 157 | 'selector' => 'a.wl-entity-page-link', |
| 158 | 158 | 'baseUrl' => $this->base_url |
| 159 | - ) ); |
|
| 159 | + )); |
|
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - private function url_to_postid( $url ) { |
|
| 163 | + private function url_to_postid($url) { |
|
| 164 | 164 | // Try with url_to_postid |
| 165 | - $post_id = url_to_postid( $url ); |
|
| 166 | - if ( $post_id == 0 ) { |
|
| 165 | + $post_id = url_to_postid($url); |
|
| 166 | + if ($post_id == 0) { |
|
| 167 | 167 | // Try with get_page_by_path |
| 168 | - $post = get_page_by_path( basename( untrailingslashit( $url ) ), OBJECT, 'entity' ); |
|
| 169 | - if ( $post ) { |
|
| 168 | + $post = get_page_by_path(basename(untrailingslashit($url)), OBJECT, 'entity'); |
|
| 169 | + if ($post) { |
|
| 170 | 170 | $post_id = $post->ID; |
| 171 | 171 | } |
| 172 | 172 | } |