@@ -23,261 +23,261 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Wordlift_Content_Filter_Service { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * The pattern to find entities in text. |
|
| 28 | - * |
|
| 29 | - * @since 3.8.0 |
|
| 30 | - */ |
|
| 31 | - const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.8.0 |
|
| 37 | - * @access private |
|
| 38 | - * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 39 | - */ |
|
| 40 | - private $entity_service; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The `link by default` setting. |
|
| 44 | - * |
|
| 45 | - * @since 3.13.0 |
|
| 46 | - * @access private |
|
| 47 | - * @var bool True if link by default is enabled otherwise false. |
|
| 48 | - */ |
|
| 49 | - private $is_link_by_default; |
|
| 50 | - |
|
| 51 | - private $linked_entity_uris = array(); |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 55 | - * |
|
| 56 | - * @since 3.16.3 |
|
| 57 | - * @access private |
|
| 58 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 59 | - */ |
|
| 60 | - private $entity_uri_service; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * A {@link Wordlift_Log_Service} instance. |
|
| 64 | - * |
|
| 65 | - * @since 3.16.0 |
|
| 66 | - * |
|
| 67 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 68 | - */ |
|
| 69 | - private $log; |
|
| 70 | - /** |
|
| 71 | - * @var Object_Link_Provider |
|
| 72 | - */ |
|
| 73 | - private $object_link_provider; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 77 | - * |
|
| 78 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 79 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 80 | - * |
|
| 81 | - * @since 3.8.0 |
|
| 82 | - * |
|
| 83 | - */ |
|
| 84 | - protected function __construct( $entity_service, $entity_uri_service ) { |
|
| 85 | - |
|
| 86 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 87 | - |
|
| 88 | - $this->entity_service = $entity_service; |
|
| 89 | - $this->entity_uri_service = $entity_uri_service; |
|
| 90 | - $this->object_link_provider = Object_Link_Provider::get_instance(); |
|
| 91 | - |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - private static $instance = null; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 98 | - * |
|
| 99 | - * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 100 | - * @since 3.14.2 |
|
| 101 | - */ |
|
| 102 | - public static function get_instance() { |
|
| 103 | - |
|
| 104 | - if ( ! isset( self::$instance ) ) { |
|
| 105 | - self::$instance = new self( Wordlift_Entity_Service::get_instance(), Wordlift_Entity_Uri_Service::get_instance() ); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - return self::$instance; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Mangle the content by adding links to the entity pages. This function is |
|
| 113 | - * hooked to the 'the_content' WP's filter. |
|
| 114 | - * |
|
| 115 | - * @param string $content The content being filtered. |
|
| 116 | - * |
|
| 117 | - * @return string The filtered content. |
|
| 118 | - * @since 3.8.0 |
|
| 119 | - * |
|
| 120 | - */ |
|
| 121 | - public function the_content( $content ) { |
|
| 122 | - $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 123 | - |
|
| 124 | - // Links should be added only on the front end and not for RSS. |
|
| 125 | - if ( is_feed() || is_admin() ) { |
|
| 126 | - return $content; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - // Preload the `link by default` setting. |
|
| 130 | - $this->is_link_by_default = Wordlift_Configuration_Service::get_instance()->is_link_by_default(); |
|
| 131 | - |
|
| 132 | - // Reset the array of of entity post ids linked from the post content. |
|
| 133 | - // This is used to avoid linking more the once the same post. |
|
| 134 | - $this->linked_entity_uris = array(); |
|
| 135 | - |
|
| 136 | - // Preload URIs. |
|
| 137 | - $matches = array(); |
|
| 138 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
| 139 | - |
|
| 140 | - // Bail out if there are no URIs. |
|
| 141 | - if ( empty( $matches[3] ) ) { |
|
| 142 | - return $content; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // Replace each match of the entity tag with the entity link. If an error |
|
| 146 | - // occurs fail silently returning the original content. |
|
| 147 | - return preg_replace_callback( self::PATTERN, array( |
|
| 148 | - $this, |
|
| 149 | - 'link', |
|
| 150 | - ), $content ) ?: $content; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Get the entity match and replace it with a page link. |
|
| 155 | - * |
|
| 156 | - * @param array $matches An array of matches. |
|
| 157 | - * |
|
| 158 | - * @return string The replaced text with the link to the entity page. |
|
| 159 | - * @since 3.8.0 |
|
| 160 | - * |
|
| 161 | - */ |
|
| 162 | - private function link( $matches ) { |
|
| 163 | - |
|
| 164 | - // Get the entity itemid URI and label. |
|
| 165 | - $css_class = $matches[2]; |
|
| 166 | - $uri = $matches[3]; |
|
| 167 | - $label = $matches[4]; |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * If the entity is already linked, dont send query to the db. |
|
| 172 | - */ |
|
| 173 | - if ( $this->is_already_linked( $uri ) ) { |
|
| 174 | - return $label; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 179 | - |
|
| 180 | - // If the entity should not be linked and link by default is also disabled, |
|
| 181 | - // then don't lookup the entity on the table. |
|
| 182 | - if ( ! $this->is_link_by_default && ! $link ) { |
|
| 183 | - return $label; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
| 187 | - $content = $content_service->get_by_entity_id_or_same_as( $uri ); |
|
| 188 | - |
|
| 189 | - // If no content is found, return the label, that is _remove the annotation_. |
|
| 190 | - if ( ! isset( $content ) ) { |
|
| 191 | - return $label; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - $object_id = $content->get_id(); |
|
| 195 | - $object_type = $content->get_object_type_enum(); |
|
| 196 | - |
|
| 197 | - |
|
| 198 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 199 | - |
|
| 200 | - // Don't link if links are disabled and the entity is not link or the |
|
| 201 | - // entity is do not link. |
|
| 202 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 203 | - |
|
| 204 | - // Return the label if it's don't link. |
|
| 205 | - if ( $dont_link ) { |
|
| 206 | - return $label; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * @since 3.32.0 |
|
| 211 | - * Object_ids are prefixed with object_type to prevent conflicts. |
|
| 212 | - */ |
|
| 213 | - $this->linked_entity_uris[] = $uri; |
|
| 214 | - |
|
| 215 | - // Get the link. |
|
| 216 | - $href = Wordlift_Post_Adapter::get_production_permalink( $object_id, $object_type ); |
|
| 217 | - |
|
| 218 | - // Bail out if the `$href` has been reset. |
|
| 219 | - if ( empty( $href ) ) { |
|
| 220 | - return $label; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return Link_Builder::create( $uri, $object_id ) |
|
| 224 | - ->label( $label ) |
|
| 225 | - ->href( $href ) |
|
| 226 | - ->generate_link(); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Get a string to be used as a title attribute in links to a post |
|
| 232 | - * |
|
| 233 | - * @param int $post_id The post id of the post being linked. |
|
| 234 | - * @param string $ignore_label A label to ignore. |
|
| 235 | - * |
|
| 236 | - * @return string The title to be used in the link. An empty string when |
|
| 237 | - * there is no alternative that is not the $ignore_label. |
|
| 238 | - * @deprecated 3.32.0 Use object link provider to get the link title for getting link |
|
| 239 | - * title for different types. |
|
| 240 | - * @since 3.15.0 |
|
| 241 | - * |
|
| 242 | - * As of 3.32.0 this method is not used anywhere in the core, this should be removed |
|
| 243 | - * from tests and companion plugins. |
|
| 244 | - * |
|
| 245 | - */ |
|
| 246 | - function get_link_title( $post_id, $ignore_label, $object_type = Object_Type_Enum::POST ) { |
|
| 247 | - return $this->object_link_provider->get_link_title( $post_id, $ignore_label, $object_type ); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 252 | - * the provided content. |
|
| 253 | - * |
|
| 254 | - * @param string $content The content. |
|
| 255 | - * |
|
| 256 | - * @return array An array of URIs. |
|
| 257 | - * @since 3.14.2 |
|
| 258 | - * |
|
| 259 | - */ |
|
| 260 | - public function get_entity_uris( $content ) { |
|
| 261 | - |
|
| 262 | - $matches = array(); |
|
| 263 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 264 | - |
|
| 265 | - // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 266 | - // to turn it into an object (since if the 3rd match isn't found the index |
|
| 267 | - // is not sequential. |
|
| 268 | - // |
|
| 269 | - // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 270 | - return array_values( array_unique( $matches[3] ) ); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @param $entity_uri |
|
| 276 | - * |
|
| 277 | - * @return bool |
|
| 278 | - */ |
|
| 279 | - private function is_already_linked( $entity_uri ) { |
|
| 280 | - return in_array( $entity_uri, $this->linked_entity_uris ); |
|
| 281 | - } |
|
| 26 | + /** |
|
| 27 | + * The pattern to find entities in text. |
|
| 28 | + * |
|
| 29 | + * @since 3.8.0 |
|
| 30 | + */ |
|
| 31 | + const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.8.0 |
|
| 37 | + * @access private |
|
| 38 | + * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 39 | + */ |
|
| 40 | + private $entity_service; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The `link by default` setting. |
|
| 44 | + * |
|
| 45 | + * @since 3.13.0 |
|
| 46 | + * @access private |
|
| 47 | + * @var bool True if link by default is enabled otherwise false. |
|
| 48 | + */ |
|
| 49 | + private $is_link_by_default; |
|
| 50 | + |
|
| 51 | + private $linked_entity_uris = array(); |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 55 | + * |
|
| 56 | + * @since 3.16.3 |
|
| 57 | + * @access private |
|
| 58 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 59 | + */ |
|
| 60 | + private $entity_uri_service; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * A {@link Wordlift_Log_Service} instance. |
|
| 64 | + * |
|
| 65 | + * @since 3.16.0 |
|
| 66 | + * |
|
| 67 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 68 | + */ |
|
| 69 | + private $log; |
|
| 70 | + /** |
|
| 71 | + * @var Object_Link_Provider |
|
| 72 | + */ |
|
| 73 | + private $object_link_provider; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 77 | + * |
|
| 78 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 79 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 80 | + * |
|
| 81 | + * @since 3.8.0 |
|
| 82 | + * |
|
| 83 | + */ |
|
| 84 | + protected function __construct( $entity_service, $entity_uri_service ) { |
|
| 85 | + |
|
| 86 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 87 | + |
|
| 88 | + $this->entity_service = $entity_service; |
|
| 89 | + $this->entity_uri_service = $entity_uri_service; |
|
| 90 | + $this->object_link_provider = Object_Link_Provider::get_instance(); |
|
| 91 | + |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + private static $instance = null; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 98 | + * |
|
| 99 | + * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 100 | + * @since 3.14.2 |
|
| 101 | + */ |
|
| 102 | + public static function get_instance() { |
|
| 103 | + |
|
| 104 | + if ( ! isset( self::$instance ) ) { |
|
| 105 | + self::$instance = new self( Wordlift_Entity_Service::get_instance(), Wordlift_Entity_Uri_Service::get_instance() ); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + return self::$instance; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Mangle the content by adding links to the entity pages. This function is |
|
| 113 | + * hooked to the 'the_content' WP's filter. |
|
| 114 | + * |
|
| 115 | + * @param string $content The content being filtered. |
|
| 116 | + * |
|
| 117 | + * @return string The filtered content. |
|
| 118 | + * @since 3.8.0 |
|
| 119 | + * |
|
| 120 | + */ |
|
| 121 | + public function the_content( $content ) { |
|
| 122 | + $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 123 | + |
|
| 124 | + // Links should be added only on the front end and not for RSS. |
|
| 125 | + if ( is_feed() || is_admin() ) { |
|
| 126 | + return $content; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + // Preload the `link by default` setting. |
|
| 130 | + $this->is_link_by_default = Wordlift_Configuration_Service::get_instance()->is_link_by_default(); |
|
| 131 | + |
|
| 132 | + // Reset the array of of entity post ids linked from the post content. |
|
| 133 | + // This is used to avoid linking more the once the same post. |
|
| 134 | + $this->linked_entity_uris = array(); |
|
| 135 | + |
|
| 136 | + // Preload URIs. |
|
| 137 | + $matches = array(); |
|
| 138 | + preg_match_all( self::PATTERN, $content, $matches ); |
|
| 139 | + |
|
| 140 | + // Bail out if there are no URIs. |
|
| 141 | + if ( empty( $matches[3] ) ) { |
|
| 142 | + return $content; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // Replace each match of the entity tag with the entity link. If an error |
|
| 146 | + // occurs fail silently returning the original content. |
|
| 147 | + return preg_replace_callback( self::PATTERN, array( |
|
| 148 | + $this, |
|
| 149 | + 'link', |
|
| 150 | + ), $content ) ?: $content; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Get the entity match and replace it with a page link. |
|
| 155 | + * |
|
| 156 | + * @param array $matches An array of matches. |
|
| 157 | + * |
|
| 158 | + * @return string The replaced text with the link to the entity page. |
|
| 159 | + * @since 3.8.0 |
|
| 160 | + * |
|
| 161 | + */ |
|
| 162 | + private function link( $matches ) { |
|
| 163 | + |
|
| 164 | + // Get the entity itemid URI and label. |
|
| 165 | + $css_class = $matches[2]; |
|
| 166 | + $uri = $matches[3]; |
|
| 167 | + $label = $matches[4]; |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * If the entity is already linked, dont send query to the db. |
|
| 172 | + */ |
|
| 173 | + if ( $this->is_already_linked( $uri ) ) { |
|
| 174 | + return $label; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 179 | + |
|
| 180 | + // If the entity should not be linked and link by default is also disabled, |
|
| 181 | + // then don't lookup the entity on the table. |
|
| 182 | + if ( ! $this->is_link_by_default && ! $link ) { |
|
| 183 | + return $label; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
| 187 | + $content = $content_service->get_by_entity_id_or_same_as( $uri ); |
|
| 188 | + |
|
| 189 | + // If no content is found, return the label, that is _remove the annotation_. |
|
| 190 | + if ( ! isset( $content ) ) { |
|
| 191 | + return $label; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + $object_id = $content->get_id(); |
|
| 195 | + $object_type = $content->get_object_type_enum(); |
|
| 196 | + |
|
| 197 | + |
|
| 198 | + $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 199 | + |
|
| 200 | + // Don't link if links are disabled and the entity is not link or the |
|
| 201 | + // entity is do not link. |
|
| 202 | + $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 203 | + |
|
| 204 | + // Return the label if it's don't link. |
|
| 205 | + if ( $dont_link ) { |
|
| 206 | + return $label; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * @since 3.32.0 |
|
| 211 | + * Object_ids are prefixed with object_type to prevent conflicts. |
|
| 212 | + */ |
|
| 213 | + $this->linked_entity_uris[] = $uri; |
|
| 214 | + |
|
| 215 | + // Get the link. |
|
| 216 | + $href = Wordlift_Post_Adapter::get_production_permalink( $object_id, $object_type ); |
|
| 217 | + |
|
| 218 | + // Bail out if the `$href` has been reset. |
|
| 219 | + if ( empty( $href ) ) { |
|
| 220 | + return $label; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return Link_Builder::create( $uri, $object_id ) |
|
| 224 | + ->label( $label ) |
|
| 225 | + ->href( $href ) |
|
| 226 | + ->generate_link(); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Get a string to be used as a title attribute in links to a post |
|
| 232 | + * |
|
| 233 | + * @param int $post_id The post id of the post being linked. |
|
| 234 | + * @param string $ignore_label A label to ignore. |
|
| 235 | + * |
|
| 236 | + * @return string The title to be used in the link. An empty string when |
|
| 237 | + * there is no alternative that is not the $ignore_label. |
|
| 238 | + * @deprecated 3.32.0 Use object link provider to get the link title for getting link |
|
| 239 | + * title for different types. |
|
| 240 | + * @since 3.15.0 |
|
| 241 | + * |
|
| 242 | + * As of 3.32.0 this method is not used anywhere in the core, this should be removed |
|
| 243 | + * from tests and companion plugins. |
|
| 244 | + * |
|
| 245 | + */ |
|
| 246 | + function get_link_title( $post_id, $ignore_label, $object_type = Object_Type_Enum::POST ) { |
|
| 247 | + return $this->object_link_provider->get_link_title( $post_id, $ignore_label, $object_type ); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 252 | + * the provided content. |
|
| 253 | + * |
|
| 254 | + * @param string $content The content. |
|
| 255 | + * |
|
| 256 | + * @return array An array of URIs. |
|
| 257 | + * @since 3.14.2 |
|
| 258 | + * |
|
| 259 | + */ |
|
| 260 | + public function get_entity_uris( $content ) { |
|
| 261 | + |
|
| 262 | + $matches = array(); |
|
| 263 | + preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 264 | + |
|
| 265 | + // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 266 | + // to turn it into an object (since if the 3rd match isn't found the index |
|
| 267 | + // is not sequential. |
|
| 268 | + // |
|
| 269 | + // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 270 | + return array_values( array_unique( $matches[3] ) ); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @param $entity_uri |
|
| 276 | + * |
|
| 277 | + * @return bool |
|
| 278 | + */ |
|
| 279 | + private function is_already_linked( $entity_uri ) { |
|
| 280 | + return in_array( $entity_uri, $this->linked_entity_uris ); |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | 283 | } |
@@ -81,9 +81,9 @@ discard block |
||
| 81 | 81 | * @since 3.8.0 |
| 82 | 82 | * |
| 83 | 83 | */ |
| 84 | - protected function __construct( $entity_service, $entity_uri_service ) { |
|
| 84 | + protected function __construct($entity_service, $entity_uri_service) { |
|
| 85 | 85 | |
| 86 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 86 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 87 | 87 | |
| 88 | 88 | $this->entity_service = $entity_service; |
| 89 | 89 | $this->entity_uri_service = $entity_uri_service; |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | */ |
| 102 | 102 | public static function get_instance() { |
| 103 | 103 | |
| 104 | - if ( ! isset( self::$instance ) ) { |
|
| 105 | - self::$instance = new self( Wordlift_Entity_Service::get_instance(), Wordlift_Entity_Uri_Service::get_instance() ); |
|
| 104 | + if ( ! isset(self::$instance)) { |
|
| 105 | + self::$instance = new self(Wordlift_Entity_Service::get_instance(), Wordlift_Entity_Uri_Service::get_instance()); |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | return self::$instance; |
@@ -118,11 +118,11 @@ discard block |
||
| 118 | 118 | * @since 3.8.0 |
| 119 | 119 | * |
| 120 | 120 | */ |
| 121 | - public function the_content( $content ) { |
|
| 122 | - $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 121 | + public function the_content($content) { |
|
| 122 | + $this->log->trace("Filtering content [ ".(is_singular() ? 'yes' : 'no')." ]..."); |
|
| 123 | 123 | |
| 124 | 124 | // Links should be added only on the front end and not for RSS. |
| 125 | - if ( is_feed() || is_admin() ) { |
|
| 125 | + if (is_feed() || is_admin()) { |
|
| 126 | 126 | return $content; |
| 127 | 127 | } |
| 128 | 128 | |
@@ -135,19 +135,19 @@ discard block |
||
| 135 | 135 | |
| 136 | 136 | // Preload URIs. |
| 137 | 137 | $matches = array(); |
| 138 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
| 138 | + preg_match_all(self::PATTERN, $content, $matches); |
|
| 139 | 139 | |
| 140 | 140 | // Bail out if there are no URIs. |
| 141 | - if ( empty( $matches[3] ) ) { |
|
| 141 | + if (empty($matches[3])) { |
|
| 142 | 142 | return $content; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | // Replace each match of the entity tag with the entity link. If an error |
| 146 | 146 | // occurs fail silently returning the original content. |
| 147 | - return preg_replace_callback( self::PATTERN, array( |
|
| 147 | + return preg_replace_callback(self::PATTERN, array( |
|
| 148 | 148 | $this, |
| 149 | 149 | 'link', |
| 150 | - ), $content ) ?: $content; |
|
| 150 | + ), $content) ?: $content; |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | * @since 3.8.0 |
| 160 | 160 | * |
| 161 | 161 | */ |
| 162 | - private function link( $matches ) { |
|
| 162 | + private function link($matches) { |
|
| 163 | 163 | |
| 164 | 164 | // Get the entity itemid URI and label. |
| 165 | 165 | $css_class = $matches[2]; |
@@ -170,24 +170,24 @@ discard block |
||
| 170 | 170 | /** |
| 171 | 171 | * If the entity is already linked, dont send query to the db. |
| 172 | 172 | */ |
| 173 | - if ( $this->is_already_linked( $uri ) ) { |
|
| 173 | + if ($this->is_already_linked($uri)) { |
|
| 174 | 174 | return $label; |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | |
| 178 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 178 | + $link = - 1 < strpos($css_class, 'wl-link'); |
|
| 179 | 179 | |
| 180 | 180 | // If the entity should not be linked and link by default is also disabled, |
| 181 | 181 | // then don't lookup the entity on the table. |
| 182 | - if ( ! $this->is_link_by_default && ! $link ) { |
|
| 182 | + if ( ! $this->is_link_by_default && ! $link) { |
|
| 183 | 183 | return $label; |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | $content_service = Wordpress_Content_Service::get_instance(); |
| 187 | - $content = $content_service->get_by_entity_id_or_same_as( $uri ); |
|
| 187 | + $content = $content_service->get_by_entity_id_or_same_as($uri); |
|
| 188 | 188 | |
| 189 | 189 | // If no content is found, return the label, that is _remove the annotation_. |
| 190 | - if ( ! isset( $content ) ) { |
|
| 190 | + if ( ! isset($content)) { |
|
| 191 | 191 | return $label; |
| 192 | 192 | } |
| 193 | 193 | |
@@ -195,14 +195,14 @@ discard block |
||
| 195 | 195 | $object_type = $content->get_object_type_enum(); |
| 196 | 196 | |
| 197 | 197 | |
| 198 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 198 | + $no_link = - 1 < strpos($css_class, 'wl-no-link'); |
|
| 199 | 199 | |
| 200 | 200 | // Don't link if links are disabled and the entity is not link or the |
| 201 | 201 | // entity is do not link. |
| 202 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 202 | + $dont_link = ( ! $this->is_link_by_default && ! $link) || $no_link; |
|
| 203 | 203 | |
| 204 | 204 | // Return the label if it's don't link. |
| 205 | - if ( $dont_link ) { |
|
| 205 | + if ($dont_link) { |
|
| 206 | 206 | return $label; |
| 207 | 207 | } |
| 208 | 208 | |
@@ -213,16 +213,16 @@ discard block |
||
| 213 | 213 | $this->linked_entity_uris[] = $uri; |
| 214 | 214 | |
| 215 | 215 | // Get the link. |
| 216 | - $href = Wordlift_Post_Adapter::get_production_permalink( $object_id, $object_type ); |
|
| 216 | + $href = Wordlift_Post_Adapter::get_production_permalink($object_id, $object_type); |
|
| 217 | 217 | |
| 218 | 218 | // Bail out if the `$href` has been reset. |
| 219 | - if ( empty( $href ) ) { |
|
| 219 | + if (empty($href)) { |
|
| 220 | 220 | return $label; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - return Link_Builder::create( $uri, $object_id ) |
|
| 224 | - ->label( $label ) |
|
| 225 | - ->href( $href ) |
|
| 223 | + return Link_Builder::create($uri, $object_id) |
|
| 224 | + ->label($label) |
|
| 225 | + ->href($href) |
|
| 226 | 226 | ->generate_link(); |
| 227 | 227 | } |
| 228 | 228 | |
@@ -243,8 +243,8 @@ discard block |
||
| 243 | 243 | * from tests and companion plugins. |
| 244 | 244 | * |
| 245 | 245 | */ |
| 246 | - function get_link_title( $post_id, $ignore_label, $object_type = Object_Type_Enum::POST ) { |
|
| 247 | - return $this->object_link_provider->get_link_title( $post_id, $ignore_label, $object_type ); |
|
| 246 | + function get_link_title($post_id, $ignore_label, $object_type = Object_Type_Enum::POST) { |
|
| 247 | + return $this->object_link_provider->get_link_title($post_id, $ignore_label, $object_type); |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | /** |
@@ -257,17 +257,17 @@ discard block |
||
| 257 | 257 | * @since 3.14.2 |
| 258 | 258 | * |
| 259 | 259 | */ |
| 260 | - public function get_entity_uris( $content ) { |
|
| 260 | + public function get_entity_uris($content) { |
|
| 261 | 261 | |
| 262 | 262 | $matches = array(); |
| 263 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 263 | + preg_match_all(Wordlift_Content_Filter_Service::PATTERN, $content, $matches); |
|
| 264 | 264 | |
| 265 | 265 | // We need to use `array_values` here in order to avoid further `json_encode` |
| 266 | 266 | // to turn it into an object (since if the 3rd match isn't found the index |
| 267 | 267 | // is not sequential. |
| 268 | 268 | // |
| 269 | 269 | // See https://github.com/insideout10/wordlift-plugin/issues/646. |
| 270 | - return array_values( array_unique( $matches[3] ) ); |
|
| 270 | + return array_values(array_unique($matches[3])); |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | |
@@ -276,8 +276,8 @@ discard block |
||
| 276 | 276 | * |
| 277 | 277 | * @return bool |
| 278 | 278 | */ |
| 279 | - private function is_already_linked( $entity_uri ) { |
|
| 280 | - return in_array( $entity_uri, $this->linked_entity_uris ); |
|
| 279 | + private function is_already_linked($entity_uri) { |
|
| 280 | + return in_array($entity_uri, $this->linked_entity_uris); |
|
| 281 | 281 | } |
| 282 | 282 | |
| 283 | 283 | } |
@@ -8,57 +8,57 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Entity_Uri_Generator { |
| 10 | 10 | |
| 11 | - public static function create_uri( $object_type_enum, $id ) { |
|
| 11 | + public static function create_uri( $object_type_enum, $id ) { |
|
| 12 | 12 | |
| 13 | - switch ( $object_type_enum ) { |
|
| 14 | - case Object_Type_Enum::POST: |
|
| 15 | - $post = get_post( $id ); |
|
| 16 | - if ( ! isset( $post ) || in_array( $post->post_status, array( 'auto-draft', 'inherit' ) ) ) { |
|
| 17 | - return null; |
|
| 18 | - } |
|
| 13 | + switch ( $object_type_enum ) { |
|
| 14 | + case Object_Type_Enum::POST: |
|
| 15 | + $post = get_post( $id ); |
|
| 16 | + if ( ! isset( $post ) || in_array( $post->post_status, array( 'auto-draft', 'inherit' ) ) ) { |
|
| 17 | + return null; |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - $slug = $post->post_name ?: sanitize_title( $post->post_title ) . '-' . $post->ID; |
|
| 20 | + $slug = $post->post_name ?: sanitize_title( $post->post_title ) . '-' . $post->ID; |
|
| 21 | 21 | |
| 22 | - return self::ensure_unique( $post->post_type . '/' . $slug ); |
|
| 22 | + return self::ensure_unique( $post->post_type . '/' . $slug ); |
|
| 23 | 23 | |
| 24 | - case Object_Type_Enum::TERM: |
|
| 25 | - $term = get_term( $id ); |
|
| 26 | - if ( ! is_a( $term, 'WP_Term' ) ) { |
|
| 27 | - return null; |
|
| 28 | - } |
|
| 24 | + case Object_Type_Enum::TERM: |
|
| 25 | + $term = get_term( $id ); |
|
| 26 | + if ( ! is_a( $term, 'WP_Term' ) ) { |
|
| 27 | + return null; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - return self::ensure_unique( $term->taxonomy . '/' . $term->slug ); |
|
| 30 | + return self::ensure_unique( $term->taxonomy . '/' . $term->slug ); |
|
| 31 | 31 | |
| 32 | - case Object_Type_Enum::USER: |
|
| 33 | - $user = get_user_by( 'id', $id ); |
|
| 32 | + case Object_Type_Enum::USER: |
|
| 33 | + $user = get_user_by( 'id', $id ); |
|
| 34 | 34 | |
| 35 | - if ( ! is_a( $user, 'WP_User' ) ) { |
|
| 36 | - return null; |
|
| 37 | - } |
|
| 35 | + if ( ! is_a( $user, 'WP_User' ) ) { |
|
| 36 | + return null; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - return self::ensure_unique( 'user/' . $user->user_nicename ); |
|
| 39 | + return self::ensure_unique( 'user/' . $user->user_nicename ); |
|
| 40 | 40 | |
| 41 | - default: |
|
| 42 | - } |
|
| 41 | + default: |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - return null; |
|
| 45 | - } |
|
| 44 | + return null; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @throws Exception |
|
| 49 | - */ |
|
| 50 | - private static function ensure_unique( $rel_uri ) { |
|
| 51 | - for ( $try_rel_uri = $rel_uri, $i = 2; $i < 100; $i ++ ) { |
|
| 52 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id( $try_rel_uri ); |
|
| 53 | - if ( ! isset( $content ) ) { |
|
| 54 | - return $try_rel_uri; |
|
| 55 | - } |
|
| 47 | + /** |
|
| 48 | + * @throws Exception |
|
| 49 | + */ |
|
| 50 | + private static function ensure_unique( $rel_uri ) { |
|
| 51 | + for ( $try_rel_uri = $rel_uri, $i = 2; $i < 100; $i ++ ) { |
|
| 52 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id( $try_rel_uri ); |
|
| 53 | + if ( ! isset( $content ) ) { |
|
| 54 | + return $try_rel_uri; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - $try_rel_uri = $rel_uri . '-' . $i; |
|
| 58 | - } |
|
| 57 | + $try_rel_uri = $rel_uri . '-' . $i; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - // Giving up. |
|
| 61 | - return $rel_uri . '-' . uniqid(); |
|
| 62 | - } |
|
| 60 | + // Giving up. |
|
| 61 | + return $rel_uri . '-' . uniqid(); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | 64 | } |
@@ -8,35 +8,35 @@ discard block |
||
| 8 | 8 | |
| 9 | 9 | class Entity_Uri_Generator { |
| 10 | 10 | |
| 11 | - public static function create_uri( $object_type_enum, $id ) { |
|
| 11 | + public static function create_uri($object_type_enum, $id) { |
|
| 12 | 12 | |
| 13 | - switch ( $object_type_enum ) { |
|
| 13 | + switch ($object_type_enum) { |
|
| 14 | 14 | case Object_Type_Enum::POST: |
| 15 | - $post = get_post( $id ); |
|
| 16 | - if ( ! isset( $post ) || in_array( $post->post_status, array( 'auto-draft', 'inherit' ) ) ) { |
|
| 15 | + $post = get_post($id); |
|
| 16 | + if ( ! isset($post) || in_array($post->post_status, array('auto-draft', 'inherit'))) { |
|
| 17 | 17 | return null; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - $slug = $post->post_name ?: sanitize_title( $post->post_title ) . '-' . $post->ID; |
|
| 20 | + $slug = $post->post_name ?: sanitize_title($post->post_title).'-'.$post->ID; |
|
| 21 | 21 | |
| 22 | - return self::ensure_unique( $post->post_type . '/' . $slug ); |
|
| 22 | + return self::ensure_unique($post->post_type.'/'.$slug); |
|
| 23 | 23 | |
| 24 | 24 | case Object_Type_Enum::TERM: |
| 25 | - $term = get_term( $id ); |
|
| 26 | - if ( ! is_a( $term, 'WP_Term' ) ) { |
|
| 25 | + $term = get_term($id); |
|
| 26 | + if ( ! is_a($term, 'WP_Term')) { |
|
| 27 | 27 | return null; |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - return self::ensure_unique( $term->taxonomy . '/' . $term->slug ); |
|
| 30 | + return self::ensure_unique($term->taxonomy.'/'.$term->slug); |
|
| 31 | 31 | |
| 32 | 32 | case Object_Type_Enum::USER: |
| 33 | - $user = get_user_by( 'id', $id ); |
|
| 33 | + $user = get_user_by('id', $id); |
|
| 34 | 34 | |
| 35 | - if ( ! is_a( $user, 'WP_User' ) ) { |
|
| 35 | + if ( ! is_a($user, 'WP_User')) { |
|
| 36 | 36 | return null; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - return self::ensure_unique( 'user/' . $user->user_nicename ); |
|
| 39 | + return self::ensure_unique('user/'.$user->user_nicename); |
|
| 40 | 40 | |
| 41 | 41 | default: |
| 42 | 42 | } |
@@ -47,18 +47,18 @@ discard block |
||
| 47 | 47 | /** |
| 48 | 48 | * @throws Exception |
| 49 | 49 | */ |
| 50 | - private static function ensure_unique( $rel_uri ) { |
|
| 51 | - for ( $try_rel_uri = $rel_uri, $i = 2; $i < 100; $i ++ ) { |
|
| 52 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id( $try_rel_uri ); |
|
| 53 | - if ( ! isset( $content ) ) { |
|
| 50 | + private static function ensure_unique($rel_uri) { |
|
| 51 | + for ($try_rel_uri = $rel_uri, $i = 2; $i < 100; $i++) { |
|
| 52 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id($try_rel_uri); |
|
| 53 | + if ( ! isset($content)) { |
|
| 54 | 54 | return $try_rel_uri; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - $try_rel_uri = $rel_uri . '-' . $i; |
|
| 57 | + $try_rel_uri = $rel_uri.'-'.$i; |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | // Giving up. |
| 61 | - return $rel_uri . '-' . uniqid(); |
|
| 61 | + return $rel_uri.'-'.uniqid(); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | } |
@@ -17,177 +17,177 @@ |
||
| 17 | 17 | // @@todo: add a hook to clear the cached files now and then. |
| 18 | 18 | class Ttl_Cache { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * The cache name. |
|
| 22 | - * |
|
| 23 | - * @var string $name The cache name. |
|
| 24 | - * @access private |
|
| 25 | - * @since 3.21.2 |
|
| 26 | - */ |
|
| 27 | - private $name; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The TTL of cached responses in seconds. |
|
| 31 | - * |
|
| 32 | - * @var int $ttl The TTL in seconds. |
|
| 33 | - * @access private |
|
| 34 | - * @since 3.21.2 |
|
| 35 | - */ |
|
| 36 | - private $ttl; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The cache dir where the cached data is written. |
|
| 40 | - * |
|
| 41 | - * @since 3.21.2 |
|
| 42 | - * @access private |
|
| 43 | - * @var string $cache_dir The cache dir where the cached responses are written. |
|
| 44 | - */ |
|
| 45 | - private $cache_dir; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * A {@link Wordlift_Log_Service} instance. |
|
| 49 | - * |
|
| 50 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 51 | - * @access private |
|
| 52 | - * @since 3.21.2 |
|
| 53 | - */ |
|
| 54 | - private $log; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var array |
|
| 58 | - */ |
|
| 59 | - private static $caches = array(); |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Create a {@link Ttl_Cache} with the specified TTL, default 900 secs. |
|
| 63 | - * |
|
| 64 | - * @param string $name The cache name. |
|
| 65 | - * @param int $ttl The cache TTL, default 900 secs. |
|
| 66 | - * |
|
| 67 | - * @since 3.21.2 |
|
| 68 | - */ |
|
| 69 | - public function __construct( $name, $ttl = 900 ) { |
|
| 70 | - |
|
| 71 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 72 | - |
|
| 73 | - $this->name = $name; |
|
| 74 | - $this->ttl = $ttl; |
|
| 75 | - |
|
| 76 | - $this->cache_dir = self::get_cache_folder() . DIRECTORY_SEPARATOR . md5( $name ); |
|
| 77 | - |
|
| 78 | - $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
| 79 | - wp_mkdir_p( $this->cache_dir ); |
|
| 80 | - |
|
| 81 | - self::$caches[ $name ] = $this; |
|
| 82 | - |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Get the root cache folder. |
|
| 87 | - * |
|
| 88 | - * This is useful to introduce a cache cleaning procedure which will scan and delete older stale cache files. |
|
| 89 | - * |
|
| 90 | - * @return string The root cache folder. |
|
| 91 | - * @since 3.22.5 |
|
| 92 | - */ |
|
| 93 | - public static function get_cache_folder() { |
|
| 94 | - |
|
| 95 | - // Get the temp dir and add the directory separator if missing. |
|
| 96 | - $temp_dir = get_temp_dir(); |
|
| 97 | - if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
| 98 | - $temp_dir .= DIRECTORY_SEPARATOR; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( home_url() ); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Get the cached data for the specified key. |
|
| 106 | - * |
|
| 107 | - * @param mixed $key A serializable key. |
|
| 108 | - * |
|
| 109 | - * @return mixed|null |
|
| 110 | - * @since 3.21.2 |
|
| 111 | - */ |
|
| 112 | - public function get( $key ) { |
|
| 113 | - |
|
| 114 | - $filename = $this->get_filename( $key ); |
|
| 115 | - |
|
| 116 | - // If the cache file exists and it's not too old, then return it. |
|
| 117 | - if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) { |
|
| 118 | - $this->log->trace( "Cache HIT.\n" ); |
|
| 119 | - |
|
| 120 | - return json_decode( file_get_contents( $filename ), true ); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $this->log->trace( "Cache MISS, filename $filename." ); |
|
| 124 | - |
|
| 125 | - return null; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - public function put( $key, $data ) { |
|
| 129 | - |
|
| 130 | - $filename = $this->get_filename( $key ); |
|
| 131 | - |
|
| 132 | - // Cache. |
|
| 133 | - if ( file_exists( $filename ) ) { |
|
| 134 | - @unlink( $filename ); |
|
| 135 | - } |
|
| 136 | - @file_put_contents( $filename, wp_json_encode( $data ) ); |
|
| 20 | + /** |
|
| 21 | + * The cache name. |
|
| 22 | + * |
|
| 23 | + * @var string $name The cache name. |
|
| 24 | + * @access private |
|
| 25 | + * @since 3.21.2 |
|
| 26 | + */ |
|
| 27 | + private $name; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The TTL of cached responses in seconds. |
|
| 31 | + * |
|
| 32 | + * @var int $ttl The TTL in seconds. |
|
| 33 | + * @access private |
|
| 34 | + * @since 3.21.2 |
|
| 35 | + */ |
|
| 36 | + private $ttl; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The cache dir where the cached data is written. |
|
| 40 | + * |
|
| 41 | + * @since 3.21.2 |
|
| 42 | + * @access private |
|
| 43 | + * @var string $cache_dir The cache dir where the cached responses are written. |
|
| 44 | + */ |
|
| 45 | + private $cache_dir; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * A {@link Wordlift_Log_Service} instance. |
|
| 49 | + * |
|
| 50 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 51 | + * @access private |
|
| 52 | + * @since 3.21.2 |
|
| 53 | + */ |
|
| 54 | + private $log; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var array |
|
| 58 | + */ |
|
| 59 | + private static $caches = array(); |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Create a {@link Ttl_Cache} with the specified TTL, default 900 secs. |
|
| 63 | + * |
|
| 64 | + * @param string $name The cache name. |
|
| 65 | + * @param int $ttl The cache TTL, default 900 secs. |
|
| 66 | + * |
|
| 67 | + * @since 3.21.2 |
|
| 68 | + */ |
|
| 69 | + public function __construct( $name, $ttl = 900 ) { |
|
| 70 | + |
|
| 71 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 72 | + |
|
| 73 | + $this->name = $name; |
|
| 74 | + $this->ttl = $ttl; |
|
| 75 | + |
|
| 76 | + $this->cache_dir = self::get_cache_folder() . DIRECTORY_SEPARATOR . md5( $name ); |
|
| 77 | + |
|
| 78 | + $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
| 79 | + wp_mkdir_p( $this->cache_dir ); |
|
| 80 | + |
|
| 81 | + self::$caches[ $name ] = $this; |
|
| 82 | + |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Get the root cache folder. |
|
| 87 | + * |
|
| 88 | + * This is useful to introduce a cache cleaning procedure which will scan and delete older stale cache files. |
|
| 89 | + * |
|
| 90 | + * @return string The root cache folder. |
|
| 91 | + * @since 3.22.5 |
|
| 92 | + */ |
|
| 93 | + public static function get_cache_folder() { |
|
| 94 | + |
|
| 95 | + // Get the temp dir and add the directory separator if missing. |
|
| 96 | + $temp_dir = get_temp_dir(); |
|
| 97 | + if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
| 98 | + $temp_dir .= DIRECTORY_SEPARATOR; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( home_url() ); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Get the cached data for the specified key. |
|
| 106 | + * |
|
| 107 | + * @param mixed $key A serializable key. |
|
| 108 | + * |
|
| 109 | + * @return mixed|null |
|
| 110 | + * @since 3.21.2 |
|
| 111 | + */ |
|
| 112 | + public function get( $key ) { |
|
| 113 | + |
|
| 114 | + $filename = $this->get_filename( $key ); |
|
| 115 | + |
|
| 116 | + // If the cache file exists and it's not too old, then return it. |
|
| 117 | + if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) { |
|
| 118 | + $this->log->trace( "Cache HIT.\n" ); |
|
| 119 | + |
|
| 120 | + return json_decode( file_get_contents( $filename ), true ); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $this->log->trace( "Cache MISS, filename $filename." ); |
|
| 124 | + |
|
| 125 | + return null; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + public function put( $key, $data ) { |
|
| 129 | + |
|
| 130 | + $filename = $this->get_filename( $key ); |
|
| 131 | + |
|
| 132 | + // Cache. |
|
| 133 | + if ( file_exists( $filename ) ) { |
|
| 134 | + @unlink( $filename ); |
|
| 135 | + } |
|
| 136 | + @file_put_contents( $filename, wp_json_encode( $data ) ); |
|
| 137 | 137 | |
| 138 | - } |
|
| 139 | - |
|
| 140 | - public function delete( $key ) { |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + public function delete( $key ) { |
|
| 141 | 141 | |
| 142 | - $filename = $this->get_filename( $key ); |
|
| 142 | + $filename = $this->get_filename( $key ); |
|
| 143 | 143 | |
| 144 | - // Delete. |
|
| 145 | - if ( file_exists( $filename ) ) { |
|
| 146 | - @unlink( $filename ); |
|
| 147 | - } |
|
| 144 | + // Delete. |
|
| 145 | + if ( file_exists( $filename ) ) { |
|
| 146 | + @unlink( $filename ); |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - } |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - public function flush() { |
|
| 151 | + public function flush() { |
|
| 152 | 152 | |
| 153 | - $files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' ); |
|
| 154 | - foreach ( $files as $file ) { // iterate files |
|
| 155 | - if ( is_file( $file ) ) { |
|
| 156 | - @unlink( $file ); |
|
| 157 | - } |
|
| 158 | - } |
|
| 153 | + $files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' ); |
|
| 154 | + foreach ( $files as $file ) { // iterate files |
|
| 155 | + if ( is_file( $file ) ) { |
|
| 156 | + @unlink( $file ); |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - } |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - public static function flush_all() { |
|
| 162 | + public static function flush_all() { |
|
| 163 | 163 | |
| 164 | - /** @var Ttl_Cache $cache */ |
|
| 165 | - foreach ( self::$caches as $cache ) { |
|
| 166 | - $cache->flush(); |
|
| 167 | - } |
|
| 164 | + /** @var Ttl_Cache $cache */ |
|
| 165 | + foreach ( self::$caches as $cache ) { |
|
| 166 | + $cache->flush(); |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - } |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * Get the full path for the given `$hash`. The file is not checked for its existence. |
|
| 173 | - * |
|
| 174 | - * @param string $hash A file hash. |
|
| 175 | - * |
|
| 176 | - * @return string The full path to the file. |
|
| 177 | - * @since 3.21.2 |
|
| 178 | - */ |
|
| 179 | - private function get_path( $hash ) { |
|
| 171 | + /** |
|
| 172 | + * Get the full path for the given `$hash`. The file is not checked for its existence. |
|
| 173 | + * |
|
| 174 | + * @param string $hash A file hash. |
|
| 175 | + * |
|
| 176 | + * @return string The full path to the file. |
|
| 177 | + * @since 3.21.2 |
|
| 178 | + */ |
|
| 179 | + private function get_path( $hash ) { |
|
| 180 | 180 | |
| 181 | - return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
| 182 | - } |
|
| 181 | + return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - private function get_filename( $key ) { |
|
| 184 | + private function get_filename( $key ) { |
|
| 185 | 185 | |
| 186 | - // Create a hash and a path to the cache file. |
|
| 187 | - $hash = md5( json_encode( $key ) ); |
|
| 188 | - $filename = $this->get_path( $hash ); |
|
| 186 | + // Create a hash and a path to the cache file. |
|
| 187 | + $hash = md5( json_encode( $key ) ); |
|
| 188 | + $filename = $this->get_path( $hash ); |
|
| 189 | 189 | |
| 190 | - return $filename; |
|
| 191 | - } |
|
| 190 | + return $filename; |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | 193 | } |
@@ -66,19 +66,19 @@ discard block |
||
| 66 | 66 | * |
| 67 | 67 | * @since 3.21.2 |
| 68 | 68 | */ |
| 69 | - public function __construct( $name, $ttl = 900 ) { |
|
| 69 | + public function __construct($name, $ttl = 900) { |
|
| 70 | 70 | |
| 71 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 71 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 72 | 72 | |
| 73 | 73 | $this->name = $name; |
| 74 | 74 | $this->ttl = $ttl; |
| 75 | 75 | |
| 76 | - $this->cache_dir = self::get_cache_folder() . DIRECTORY_SEPARATOR . md5( $name ); |
|
| 76 | + $this->cache_dir = self::get_cache_folder().DIRECTORY_SEPARATOR.md5($name); |
|
| 77 | 77 | |
| 78 | - $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
| 79 | - wp_mkdir_p( $this->cache_dir ); |
|
| 78 | + $this->log->trace("Creating the cache folder {$this->cache_dir}..."); |
|
| 79 | + wp_mkdir_p($this->cache_dir); |
|
| 80 | 80 | |
| 81 | - self::$caches[ $name ] = $this; |
|
| 81 | + self::$caches[$name] = $this; |
|
| 82 | 82 | |
| 83 | 83 | } |
| 84 | 84 | |
@@ -94,11 +94,11 @@ discard block |
||
| 94 | 94 | |
| 95 | 95 | // Get the temp dir and add the directory separator if missing. |
| 96 | 96 | $temp_dir = get_temp_dir(); |
| 97 | - if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
| 97 | + if (DIRECTORY_SEPARATOR !== substr($temp_dir, - strlen(DIRECTORY_SEPARATOR))) { |
|
| 98 | 98 | $temp_dir .= DIRECTORY_SEPARATOR; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - return $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( home_url() ); |
|
| 101 | + return $temp_dir.'wl.cache'.DIRECTORY_SEPARATOR.md5(home_url()); |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | /** |
@@ -109,51 +109,51 @@ discard block |
||
| 109 | 109 | * @return mixed|null |
| 110 | 110 | * @since 3.21.2 |
| 111 | 111 | */ |
| 112 | - public function get( $key ) { |
|
| 112 | + public function get($key) { |
|
| 113 | 113 | |
| 114 | - $filename = $this->get_filename( $key ); |
|
| 114 | + $filename = $this->get_filename($key); |
|
| 115 | 115 | |
| 116 | 116 | // If the cache file exists and it's not too old, then return it. |
| 117 | - if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) { |
|
| 118 | - $this->log->trace( "Cache HIT.\n" ); |
|
| 117 | + if (file_exists($filename) && $this->ttl >= time() - filemtime($filename)) { |
|
| 118 | + $this->log->trace("Cache HIT.\n"); |
|
| 119 | 119 | |
| 120 | - return json_decode( file_get_contents( $filename ), true ); |
|
| 120 | + return json_decode(file_get_contents($filename), true); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - $this->log->trace( "Cache MISS, filename $filename." ); |
|
| 123 | + $this->log->trace("Cache MISS, filename $filename."); |
|
| 124 | 124 | |
| 125 | 125 | return null; |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - public function put( $key, $data ) { |
|
| 128 | + public function put($key, $data) { |
|
| 129 | 129 | |
| 130 | - $filename = $this->get_filename( $key ); |
|
| 130 | + $filename = $this->get_filename($key); |
|
| 131 | 131 | |
| 132 | 132 | // Cache. |
| 133 | - if ( file_exists( $filename ) ) { |
|
| 134 | - @unlink( $filename ); |
|
| 133 | + if (file_exists($filename)) { |
|
| 134 | + @unlink($filename); |
|
| 135 | 135 | } |
| 136 | - @file_put_contents( $filename, wp_json_encode( $data ) ); |
|
| 136 | + @file_put_contents($filename, wp_json_encode($data)); |
|
| 137 | 137 | |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | - public function delete( $key ) { |
|
| 140 | + public function delete($key) { |
|
| 141 | 141 | |
| 142 | - $filename = $this->get_filename( $key ); |
|
| 142 | + $filename = $this->get_filename($key); |
|
| 143 | 143 | |
| 144 | 144 | // Delete. |
| 145 | - if ( file_exists( $filename ) ) { |
|
| 146 | - @unlink( $filename ); |
|
| 145 | + if (file_exists($filename)) { |
|
| 146 | + @unlink($filename); |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | public function flush() { |
| 152 | 152 | |
| 153 | - $files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' ); |
|
| 154 | - foreach ( $files as $file ) { // iterate files |
|
| 155 | - if ( is_file( $file ) ) { |
|
| 156 | - @unlink( $file ); |
|
| 153 | + $files = glob($this->cache_dir.DIRECTORY_SEPARATOR.'*'); |
|
| 154 | + foreach ($files as $file) { // iterate files |
|
| 155 | + if (is_file($file)) { |
|
| 156 | + @unlink($file); |
|
| 157 | 157 | } |
| 158 | 158 | } |
| 159 | 159 | |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | public static function flush_all() { |
| 163 | 163 | |
| 164 | 164 | /** @var Ttl_Cache $cache */ |
| 165 | - foreach ( self::$caches as $cache ) { |
|
| 165 | + foreach (self::$caches as $cache) { |
|
| 166 | 166 | $cache->flush(); |
| 167 | 167 | } |
| 168 | 168 | |
@@ -176,16 +176,16 @@ discard block |
||
| 176 | 176 | * @return string The full path to the file. |
| 177 | 177 | * @since 3.21.2 |
| 178 | 178 | */ |
| 179 | - private function get_path( $hash ) { |
|
| 179 | + private function get_path($hash) { |
|
| 180 | 180 | |
| 181 | - return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
| 181 | + return $this->cache_dir.DIRECTORY_SEPARATOR.$hash; |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | - private function get_filename( $key ) { |
|
| 184 | + private function get_filename($key) { |
|
| 185 | 185 | |
| 186 | 186 | // Create a hash and a path to the cache file. |
| 187 | - $hash = md5( json_encode( $key ) ); |
|
| 188 | - $filename = $this->get_path( $hash ); |
|
| 187 | + $hash = md5(json_encode($key)); |
|
| 188 | + $filename = $this->get_path($hash); |
|
| 189 | 189 | |
| 190 | 190 | return $filename; |
| 191 | 191 | } |
@@ -8,251 +8,251 @@ |
||
| 8 | 8 | use Wordlift\Object_Type_Enum; |
| 9 | 9 | |
| 10 | 10 | class Sync_Service { |
| 11 | - const JSONLD_HASH = 'jsonld_hash'; |
|
| 12 | - const SYNCED_GMT = 'synced_gmt'; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * @var \Wordlift_Log_Service |
|
| 16 | - */ |
|
| 17 | - private $log; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @var Api_Service |
|
| 21 | - */ |
|
| 22 | - private $api_service; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var Jsonld_Service |
|
| 26 | - */ |
|
| 27 | - private $jsonld_service; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var Sync_Background_Process |
|
| 31 | - */ |
|
| 32 | - private $sync_background_process; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The number of posts processed in one call. |
|
| 36 | - * |
|
| 37 | - * @var int The batch size. |
|
| 38 | - */ |
|
| 39 | - private $batch_size; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var Sync_Object_Adapter_Factory |
|
| 43 | - */ |
|
| 44 | - private $sync_object_adapter_factory; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var Sync_Service |
|
| 48 | - */ |
|
| 49 | - private static $instance; |
|
| 50 | - private $entity_service; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Constructor. |
|
| 54 | - * |
|
| 55 | - * @param Api_Service $api_service The {@link Api_Service} used to communicate with the remote APIs. |
|
| 56 | - * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 57 | - * @param Jsonld_Service $jsonld_service |
|
| 58 | - * @param \Wordlift_Entity_Service $entity_service |
|
| 59 | - */ |
|
| 60 | - public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) { |
|
| 61 | - |
|
| 62 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 63 | - |
|
| 64 | - $this->api_service = $api_service; |
|
| 65 | - $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 66 | - $this->jsonld_service = $jsonld_service; |
|
| 67 | - $this->entity_service = $entity_service; |
|
| 68 | - $this->batch_size = 10; |
|
| 69 | - |
|
| 70 | - // You need to initialize this early, otherwise the Background Process isn't registered in AJAX calls. |
|
| 11 | + const JSONLD_HASH = 'jsonld_hash'; |
|
| 12 | + const SYNCED_GMT = 'synced_gmt'; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * @var \Wordlift_Log_Service |
|
| 16 | + */ |
|
| 17 | + private $log; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @var Api_Service |
|
| 21 | + */ |
|
| 22 | + private $api_service; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var Jsonld_Service |
|
| 26 | + */ |
|
| 27 | + private $jsonld_service; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var Sync_Background_Process |
|
| 31 | + */ |
|
| 32 | + private $sync_background_process; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The number of posts processed in one call. |
|
| 36 | + * |
|
| 37 | + * @var int The batch size. |
|
| 38 | + */ |
|
| 39 | + private $batch_size; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var Sync_Object_Adapter_Factory |
|
| 43 | + */ |
|
| 44 | + private $sync_object_adapter_factory; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var Sync_Service |
|
| 48 | + */ |
|
| 49 | + private static $instance; |
|
| 50 | + private $entity_service; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Constructor. |
|
| 54 | + * |
|
| 55 | + * @param Api_Service $api_service The {@link Api_Service} used to communicate with the remote APIs. |
|
| 56 | + * @param Sync_Object_Adapter_Factory $sync_object_adapter_factory |
|
| 57 | + * @param Jsonld_Service $jsonld_service |
|
| 58 | + * @param \Wordlift_Entity_Service $entity_service |
|
| 59 | + */ |
|
| 60 | + public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) { |
|
| 61 | + |
|
| 62 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 63 | + |
|
| 64 | + $this->api_service = $api_service; |
|
| 65 | + $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
|
| 66 | + $this->jsonld_service = $jsonld_service; |
|
| 67 | + $this->entity_service = $entity_service; |
|
| 68 | + $this->batch_size = 10; |
|
| 69 | + |
|
| 70 | + // You need to initialize this early, otherwise the Background Process isn't registered in AJAX calls. |
|
| 71 | 71 | // $this->sync_background_process = new Sync_Background_Process( $this );; |
| 72 | 72 | |
| 73 | - // Exclude the JSONLD_HASH meta key from those that require a resync. |
|
| 74 | - add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) { |
|
| 75 | - $args[] = Sync_Service::JSONLD_HASH; |
|
| 76 | - $args[] = Sync_Service::SYNCED_GMT; |
|
| 77 | - |
|
| 78 | - return $args; |
|
| 79 | - } ); |
|
| 80 | - |
|
| 81 | - self::$instance = $this; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public static function get_instance() { |
|
| 85 | - return self::$instance; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param int $type |
|
| 90 | - * @param int $object_id |
|
| 91 | - * |
|
| 92 | - * @return array|false |
|
| 93 | - * @throws Exception |
|
| 94 | - */ |
|
| 95 | - public function sync_one( $type, $object_id ) { |
|
| 96 | - |
|
| 97 | - $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 98 | - |
|
| 99 | - return $this->sync_many( array( $object ) ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param $type string Post or User. |
|
| 104 | - * @param $object_id int Post or User id |
|
| 105 | - * @param $uri string Entity uri , This needs to be supplied before deletion, if we |
|
| 106 | - * get it from meta it might not be available. |
|
| 107 | - * |
|
| 108 | - * @return bool |
|
| 109 | - */ |
|
| 110 | - public function delete_one( $type, $object_id, $uri ) { |
|
| 111 | - // Entity URL isn't set, bail out. |
|
| 112 | - if ( empty( $uri ) ) { |
|
| 113 | - return false; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $response = $this->api_service->request( |
|
| 117 | - 'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) ); |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - // Update the sync date in case of success, otherwise log an error. |
|
| 121 | - if ( ! $response->is_success() ) { |
|
| 122 | - return false; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Allow 3rd parties to run additional sync work. |
|
| 127 | - */ |
|
| 128 | - do_action( 'wl_sync__delete_one', $type, $object_id, $uri ); |
|
| 129 | - |
|
| 130 | - return true; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @param Sync_Object_Adapter[] $objects |
|
| 135 | - * @param bool $force Force synchronization even if the json-ld hash hasn't changed. |
|
| 136 | - * |
|
| 137 | - * @return bool |
|
| 138 | - * @throws Exception |
|
| 139 | - */ |
|
| 140 | - public function sync_many( $objects, $force = false ) { |
|
| 141 | - |
|
| 142 | - $hashes = array(); |
|
| 143 | - $payloads = array(); |
|
| 144 | - /** @var Sync_Object_Adapter $object */ |
|
| 145 | - foreach ( $objects as $object ) { |
|
| 146 | - // Bail out if no payload. |
|
| 147 | - $payload_as_string = $this->get_payload_as_string( $object ); |
|
| 148 | - if ( empty( $payload_as_string ) ) { |
|
| 149 | - continue; |
|
| 150 | - } |
|
| 151 | - $new_hash = sha1( $payload_as_string ); |
|
| 152 | - $old_hash = $object->get_value( self::JSONLD_HASH ); |
|
| 153 | - |
|
| 154 | - // JSON-LD hasn't changed, bail out. |
|
| 155 | - $should_sync = $force || $new_hash !== $old_hash; |
|
| 156 | - if ( ! apply_filters( 'wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string ) ) { |
|
| 157 | - continue; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - // Collect the hashes and the payloads. |
|
| 161 | - $hashes[] = array( $object, $new_hash, $payload_as_string ); |
|
| 162 | - $payloads[] = $payload_as_string; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - // Bail out if payloads are empty. |
|
| 166 | - if ( empty( $payloads ) ) { |
|
| 167 | - return false; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $blocking = apply_filters( 'wl_feature__enable__sync-blocking', false ); |
|
| 171 | - $response = $this->api_service->request( |
|
| 172 | - 'POST', '/middleware/dataset/batch', |
|
| 173 | - array( 'Content-Type' => 'application/json', ), |
|
| 174 | - // Put the payload in a JSON array w/o decoding/encoding again. |
|
| 175 | - '[ ' . implode( ', ', $payloads ) . ' ]', |
|
| 176 | - $blocking ? 60 : 0.001, |
|
| 177 | - null, |
|
| 178 | - array( 'blocking' => $blocking ) ); |
|
| 179 | - |
|
| 180 | - // Update the sync date in case of success, otherwise log an error. |
|
| 181 | - if ( $blocking && ! $response->is_success() ) { |
|
| 182 | - return false; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // If successful update the hashes and sync datetime. |
|
| 186 | - foreach ( $hashes as $hash ) { |
|
| 187 | - $object = $hash[0]; |
|
| 188 | - $new_hash = $hash[1]; |
|
| 189 | - |
|
| 190 | - $object->set_values( array( |
|
| 191 | - self::JSONLD_HASH => $new_hash, |
|
| 192 | - self::SYNCED_GMT => current_time( 'mysql', true ), |
|
| 193 | - ) ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Allow 3rd parties to run additional sync work. |
|
| 199 | - */ |
|
| 200 | - do_action( 'wl_sync__sync_many', $hashes ); |
|
| 201 | - |
|
| 202 | - return true; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @param Sync_Object_Adapter $object |
|
| 207 | - * |
|
| 208 | - * @return false|string |
|
| 209 | - * @throws Exception |
|
| 210 | - */ |
|
| 211 | - private function get_payload_as_string( $object ) { |
|
| 212 | - $type = $object->get_type(); |
|
| 213 | - $object_id = $object->get_object_id(); |
|
| 214 | - $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 215 | - $this->jsonld_service->get( $type, $object_id ), $type, $object_id ), 64 ); // JSON_UNESCAPED_SLASHES |
|
| 216 | - $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 217 | - |
|
| 218 | - // Entity URL isn't set, bail out. |
|
| 219 | - if ( empty( $uri ) ) { |
|
| 220 | - return false; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return wp_json_encode( array( |
|
| 224 | - 'uri' => $uri, |
|
| 225 | - 'model' => $jsonld_as_string, |
|
| 226 | - 'private' => ! $object->is_public(), |
|
| 227 | - ), 64 ); // JSON_UNESCAPED_SLASHES |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * @param $post_id |
|
| 232 | - * |
|
| 233 | - * @todo Complete the delete item. |
|
| 234 | - */ |
|
| 235 | - public function delete_item( $post_id ) { |
|
| 236 | - $uri = $this->entity_service->get_uri( $post_id, Object_Type_Enum::POST ); |
|
| 237 | - |
|
| 238 | - if ( ! isset( $uri ) ) { |
|
| 239 | - return; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - // Make a request to the remote endpoint. |
|
| 243 | - $response = $this->api_service->request( |
|
| 244 | - 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 245 | - array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 246 | - |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - public function get_batch_size() { |
|
| 250 | - |
|
| 251 | - return $this->batch_size; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - public function delete_all() { |
|
| 255 | - $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 256 | - } |
|
| 73 | + // Exclude the JSONLD_HASH meta key from those that require a resync. |
|
| 74 | + add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) { |
|
| 75 | + $args[] = Sync_Service::JSONLD_HASH; |
|
| 76 | + $args[] = Sync_Service::SYNCED_GMT; |
|
| 77 | + |
|
| 78 | + return $args; |
|
| 79 | + } ); |
|
| 80 | + |
|
| 81 | + self::$instance = $this; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public static function get_instance() { |
|
| 85 | + return self::$instance; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param int $type |
|
| 90 | + * @param int $object_id |
|
| 91 | + * |
|
| 92 | + * @return array|false |
|
| 93 | + * @throws Exception |
|
| 94 | + */ |
|
| 95 | + public function sync_one( $type, $object_id ) { |
|
| 96 | + |
|
| 97 | + $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 98 | + |
|
| 99 | + return $this->sync_many( array( $object ) ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param $type string Post or User. |
|
| 104 | + * @param $object_id int Post or User id |
|
| 105 | + * @param $uri string Entity uri , This needs to be supplied before deletion, if we |
|
| 106 | + * get it from meta it might not be available. |
|
| 107 | + * |
|
| 108 | + * @return bool |
|
| 109 | + */ |
|
| 110 | + public function delete_one( $type, $object_id, $uri ) { |
|
| 111 | + // Entity URL isn't set, bail out. |
|
| 112 | + if ( empty( $uri ) ) { |
|
| 113 | + return false; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $response = $this->api_service->request( |
|
| 117 | + 'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) ); |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + // Update the sync date in case of success, otherwise log an error. |
|
| 121 | + if ( ! $response->is_success() ) { |
|
| 122 | + return false; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Allow 3rd parties to run additional sync work. |
|
| 127 | + */ |
|
| 128 | + do_action( 'wl_sync__delete_one', $type, $object_id, $uri ); |
|
| 129 | + |
|
| 130 | + return true; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @param Sync_Object_Adapter[] $objects |
|
| 135 | + * @param bool $force Force synchronization even if the json-ld hash hasn't changed. |
|
| 136 | + * |
|
| 137 | + * @return bool |
|
| 138 | + * @throws Exception |
|
| 139 | + */ |
|
| 140 | + public function sync_many( $objects, $force = false ) { |
|
| 141 | + |
|
| 142 | + $hashes = array(); |
|
| 143 | + $payloads = array(); |
|
| 144 | + /** @var Sync_Object_Adapter $object */ |
|
| 145 | + foreach ( $objects as $object ) { |
|
| 146 | + // Bail out if no payload. |
|
| 147 | + $payload_as_string = $this->get_payload_as_string( $object ); |
|
| 148 | + if ( empty( $payload_as_string ) ) { |
|
| 149 | + continue; |
|
| 150 | + } |
|
| 151 | + $new_hash = sha1( $payload_as_string ); |
|
| 152 | + $old_hash = $object->get_value( self::JSONLD_HASH ); |
|
| 153 | + |
|
| 154 | + // JSON-LD hasn't changed, bail out. |
|
| 155 | + $should_sync = $force || $new_hash !== $old_hash; |
|
| 156 | + if ( ! apply_filters( 'wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string ) ) { |
|
| 157 | + continue; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + // Collect the hashes and the payloads. |
|
| 161 | + $hashes[] = array( $object, $new_hash, $payload_as_string ); |
|
| 162 | + $payloads[] = $payload_as_string; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + // Bail out if payloads are empty. |
|
| 166 | + if ( empty( $payloads ) ) { |
|
| 167 | + return false; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $blocking = apply_filters( 'wl_feature__enable__sync-blocking', false ); |
|
| 171 | + $response = $this->api_service->request( |
|
| 172 | + 'POST', '/middleware/dataset/batch', |
|
| 173 | + array( 'Content-Type' => 'application/json', ), |
|
| 174 | + // Put the payload in a JSON array w/o decoding/encoding again. |
|
| 175 | + '[ ' . implode( ', ', $payloads ) . ' ]', |
|
| 176 | + $blocking ? 60 : 0.001, |
|
| 177 | + null, |
|
| 178 | + array( 'blocking' => $blocking ) ); |
|
| 179 | + |
|
| 180 | + // Update the sync date in case of success, otherwise log an error. |
|
| 181 | + if ( $blocking && ! $response->is_success() ) { |
|
| 182 | + return false; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // If successful update the hashes and sync datetime. |
|
| 186 | + foreach ( $hashes as $hash ) { |
|
| 187 | + $object = $hash[0]; |
|
| 188 | + $new_hash = $hash[1]; |
|
| 189 | + |
|
| 190 | + $object->set_values( array( |
|
| 191 | + self::JSONLD_HASH => $new_hash, |
|
| 192 | + self::SYNCED_GMT => current_time( 'mysql', true ), |
|
| 193 | + ) ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Allow 3rd parties to run additional sync work. |
|
| 199 | + */ |
|
| 200 | + do_action( 'wl_sync__sync_many', $hashes ); |
|
| 201 | + |
|
| 202 | + return true; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @param Sync_Object_Adapter $object |
|
| 207 | + * |
|
| 208 | + * @return false|string |
|
| 209 | + * @throws Exception |
|
| 210 | + */ |
|
| 211 | + private function get_payload_as_string( $object ) { |
|
| 212 | + $type = $object->get_type(); |
|
| 213 | + $object_id = $object->get_object_id(); |
|
| 214 | + $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 215 | + $this->jsonld_service->get( $type, $object_id ), $type, $object_id ), 64 ); // JSON_UNESCAPED_SLASHES |
|
| 216 | + $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 217 | + |
|
| 218 | + // Entity URL isn't set, bail out. |
|
| 219 | + if ( empty( $uri ) ) { |
|
| 220 | + return false; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return wp_json_encode( array( |
|
| 224 | + 'uri' => $uri, |
|
| 225 | + 'model' => $jsonld_as_string, |
|
| 226 | + 'private' => ! $object->is_public(), |
|
| 227 | + ), 64 ); // JSON_UNESCAPED_SLASHES |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * @param $post_id |
|
| 232 | + * |
|
| 233 | + * @todo Complete the delete item. |
|
| 234 | + */ |
|
| 235 | + public function delete_item( $post_id ) { |
|
| 236 | + $uri = $this->entity_service->get_uri( $post_id, Object_Type_Enum::POST ); |
|
| 237 | + |
|
| 238 | + if ( ! isset( $uri ) ) { |
|
| 239 | + return; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + // Make a request to the remote endpoint. |
|
| 243 | + $response = $this->api_service->request( |
|
| 244 | + 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 245 | + array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 246 | + |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + public function get_batch_size() { |
|
| 250 | + |
|
| 251 | + return $this->batch_size; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + public function delete_all() { |
|
| 255 | + $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | 258 | } |
@@ -57,9 +57,9 @@ discard block |
||
| 57 | 57 | * @param Jsonld_Service $jsonld_service |
| 58 | 58 | * @param \Wordlift_Entity_Service $entity_service |
| 59 | 59 | */ |
| 60 | - public function __construct( $api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service ) { |
|
| 60 | + public function __construct($api_service, $sync_object_adapter_factory, $jsonld_service, $entity_service) { |
|
| 61 | 61 | |
| 62 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 62 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 63 | 63 | |
| 64 | 64 | $this->api_service = $api_service; |
| 65 | 65 | $this->sync_object_adapter_factory = $sync_object_adapter_factory; |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | // $this->sync_background_process = new Sync_Background_Process( $this );; |
| 72 | 72 | |
| 73 | 73 | // Exclude the JSONLD_HASH meta key from those that require a resync. |
| 74 | - add_filter( 'wl_dataset__sync_hooks__ignored_meta_keys', function ( $args ) { |
|
| 74 | + add_filter('wl_dataset__sync_hooks__ignored_meta_keys', function($args) { |
|
| 75 | 75 | $args[] = Sync_Service::JSONLD_HASH; |
| 76 | 76 | $args[] = Sync_Service::SYNCED_GMT; |
| 77 | 77 | |
@@ -92,11 +92,11 @@ discard block |
||
| 92 | 92 | * @return array|false |
| 93 | 93 | * @throws Exception |
| 94 | 94 | */ |
| 95 | - public function sync_one( $type, $object_id ) { |
|
| 95 | + public function sync_one($type, $object_id) { |
|
| 96 | 96 | |
| 97 | - $object = $this->sync_object_adapter_factory->create( $type, $object_id ); |
|
| 97 | + $object = $this->sync_object_adapter_factory->create($type, $object_id); |
|
| 98 | 98 | |
| 99 | - return $this->sync_many( array( $object ) ); |
|
| 99 | + return $this->sync_many(array($object)); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
@@ -107,25 +107,25 @@ discard block |
||
| 107 | 107 | * |
| 108 | 108 | * @return bool |
| 109 | 109 | */ |
| 110 | - public function delete_one( $type, $object_id, $uri ) { |
|
| 110 | + public function delete_one($type, $object_id, $uri) { |
|
| 111 | 111 | // Entity URL isn't set, bail out. |
| 112 | - if ( empty( $uri ) ) { |
|
| 112 | + if (empty($uri)) { |
|
| 113 | 113 | return false; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | $response = $this->api_service->request( |
| 117 | - 'DELETE', sprintf( '/middleware/dataset?uri=%s', rawurlencode( $uri ) ) ); |
|
| 117 | + 'DELETE', sprintf('/middleware/dataset?uri=%s', rawurlencode($uri)) ); |
|
| 118 | 118 | |
| 119 | 119 | |
| 120 | 120 | // Update the sync date in case of success, otherwise log an error. |
| 121 | - if ( ! $response->is_success() ) { |
|
| 121 | + if ( ! $response->is_success()) { |
|
| 122 | 122 | return false; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
| 126 | 126 | * Allow 3rd parties to run additional sync work. |
| 127 | 127 | */ |
| 128 | - do_action( 'wl_sync__delete_one', $type, $object_id, $uri ); |
|
| 128 | + do_action('wl_sync__delete_one', $type, $object_id, $uri); |
|
| 129 | 129 | |
| 130 | 130 | return true; |
| 131 | 131 | } |
@@ -137,67 +137,67 @@ discard block |
||
| 137 | 137 | * @return bool |
| 138 | 138 | * @throws Exception |
| 139 | 139 | */ |
| 140 | - public function sync_many( $objects, $force = false ) { |
|
| 140 | + public function sync_many($objects, $force = false) { |
|
| 141 | 141 | |
| 142 | 142 | $hashes = array(); |
| 143 | 143 | $payloads = array(); |
| 144 | 144 | /** @var Sync_Object_Adapter $object */ |
| 145 | - foreach ( $objects as $object ) { |
|
| 145 | + foreach ($objects as $object) { |
|
| 146 | 146 | // Bail out if no payload. |
| 147 | - $payload_as_string = $this->get_payload_as_string( $object ); |
|
| 148 | - if ( empty( $payload_as_string ) ) { |
|
| 147 | + $payload_as_string = $this->get_payload_as_string($object); |
|
| 148 | + if (empty($payload_as_string)) { |
|
| 149 | 149 | continue; |
| 150 | 150 | } |
| 151 | - $new_hash = sha1( $payload_as_string ); |
|
| 152 | - $old_hash = $object->get_value( self::JSONLD_HASH ); |
|
| 151 | + $new_hash = sha1($payload_as_string); |
|
| 152 | + $old_hash = $object->get_value(self::JSONLD_HASH); |
|
| 153 | 153 | |
| 154 | 154 | // JSON-LD hasn't changed, bail out. |
| 155 | 155 | $should_sync = $force || $new_hash !== $old_hash; |
| 156 | - if ( ! apply_filters( 'wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string ) ) { |
|
| 156 | + if ( ! apply_filters('wl_dataset__sync_service__sync_item', $should_sync, $object, $payload_as_string)) { |
|
| 157 | 157 | continue; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // Collect the hashes and the payloads. |
| 161 | - $hashes[] = array( $object, $new_hash, $payload_as_string ); |
|
| 161 | + $hashes[] = array($object, $new_hash, $payload_as_string); |
|
| 162 | 162 | $payloads[] = $payload_as_string; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | // Bail out if payloads are empty. |
| 166 | - if ( empty( $payloads ) ) { |
|
| 166 | + if (empty($payloads)) { |
|
| 167 | 167 | return false; |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | - $blocking = apply_filters( 'wl_feature__enable__sync-blocking', false ); |
|
| 170 | + $blocking = apply_filters('wl_feature__enable__sync-blocking', false); |
|
| 171 | 171 | $response = $this->api_service->request( |
| 172 | 172 | 'POST', '/middleware/dataset/batch', |
| 173 | - array( 'Content-Type' => 'application/json', ), |
|
| 173 | + array('Content-Type' => 'application/json',), |
|
| 174 | 174 | // Put the payload in a JSON array w/o decoding/encoding again. |
| 175 | - '[ ' . implode( ', ', $payloads ) . ' ]', |
|
| 175 | + '[ '.implode(', ', $payloads).' ]', |
|
| 176 | 176 | $blocking ? 60 : 0.001, |
| 177 | 177 | null, |
| 178 | - array( 'blocking' => $blocking ) ); |
|
| 178 | + array('blocking' => $blocking) ); |
|
| 179 | 179 | |
| 180 | 180 | // Update the sync date in case of success, otherwise log an error. |
| 181 | - if ( $blocking && ! $response->is_success() ) { |
|
| 181 | + if ($blocking && ! $response->is_success()) { |
|
| 182 | 182 | return false; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | // If successful update the hashes and sync datetime. |
| 186 | - foreach ( $hashes as $hash ) { |
|
| 186 | + foreach ($hashes as $hash) { |
|
| 187 | 187 | $object = $hash[0]; |
| 188 | 188 | $new_hash = $hash[1]; |
| 189 | 189 | |
| 190 | - $object->set_values( array( |
|
| 190 | + $object->set_values(array( |
|
| 191 | 191 | self::JSONLD_HASH => $new_hash, |
| 192 | - self::SYNCED_GMT => current_time( 'mysql', true ), |
|
| 193 | - ) ); |
|
| 192 | + self::SYNCED_GMT => current_time('mysql', true), |
|
| 193 | + )); |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | |
| 197 | 197 | /** |
| 198 | 198 | * Allow 3rd parties to run additional sync work. |
| 199 | 199 | */ |
| 200 | - do_action( 'wl_sync__sync_many', $hashes ); |
|
| 200 | + do_action('wl_sync__sync_many', $hashes); |
|
| 201 | 201 | |
| 202 | 202 | return true; |
| 203 | 203 | } |
@@ -208,23 +208,23 @@ discard block |
||
| 208 | 208 | * @return false|string |
| 209 | 209 | * @throws Exception |
| 210 | 210 | */ |
| 211 | - private function get_payload_as_string( $object ) { |
|
| 211 | + private function get_payload_as_string($object) { |
|
| 212 | 212 | $type = $object->get_type(); |
| 213 | 213 | $object_id = $object->get_object_id(); |
| 214 | - $jsonld_as_string = wp_json_encode( apply_filters( 'wl_dataset__sync_service__sync_item__jsonld', |
|
| 215 | - $this->jsonld_service->get( $type, $object_id ), $type, $object_id ), 64 ); // JSON_UNESCAPED_SLASHES |
|
| 216 | - $uri = $this->entity_service->get_uri( $object_id, $type ); |
|
| 214 | + $jsonld_as_string = wp_json_encode(apply_filters('wl_dataset__sync_service__sync_item__jsonld', |
|
| 215 | + $this->jsonld_service->get($type, $object_id), $type, $object_id), 64); // JSON_UNESCAPED_SLASHES |
|
| 216 | + $uri = $this->entity_service->get_uri($object_id, $type); |
|
| 217 | 217 | |
| 218 | 218 | // Entity URL isn't set, bail out. |
| 219 | - if ( empty( $uri ) ) { |
|
| 219 | + if (empty($uri)) { |
|
| 220 | 220 | return false; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - return wp_json_encode( array( |
|
| 223 | + return wp_json_encode(array( |
|
| 224 | 224 | 'uri' => $uri, |
| 225 | 225 | 'model' => $jsonld_as_string, |
| 226 | 226 | 'private' => ! $object->is_public(), |
| 227 | - ), 64 ); // JSON_UNESCAPED_SLASHES |
|
| 227 | + ), 64); // JSON_UNESCAPED_SLASHES |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | /** |
@@ -232,17 +232,17 @@ discard block |
||
| 232 | 232 | * |
| 233 | 233 | * @todo Complete the delete item. |
| 234 | 234 | */ |
| 235 | - public function delete_item( $post_id ) { |
|
| 236 | - $uri = $this->entity_service->get_uri( $post_id, Object_Type_Enum::POST ); |
|
| 235 | + public function delete_item($post_id) { |
|
| 236 | + $uri = $this->entity_service->get_uri($post_id, Object_Type_Enum::POST); |
|
| 237 | 237 | |
| 238 | - if ( ! isset( $uri ) ) { |
|
| 238 | + if ( ! isset($uri)) { |
|
| 239 | 239 | return; |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | // Make a request to the remote endpoint. |
| 243 | 243 | $response = $this->api_service->request( |
| 244 | - 'DELETE', '/middleware/dataset?uri=' . rawurlencode( $uri ), |
|
| 245 | - array( 'Content-Type' => 'application/ld+json', ) ); |
|
| 244 | + 'DELETE', '/middleware/dataset?uri='.rawurlencode($uri), |
|
| 245 | + array('Content-Type' => 'application/ld+json',) ); |
|
| 246 | 246 | |
| 247 | 247 | } |
| 248 | 248 | |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | public function delete_all() { |
| 255 | - $this->api_service->request( 'DELETE', '/middleware/dataset/all' ); |
|
| 255 | + $this->api_service->request('DELETE', '/middleware/dataset/all'); |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | } |