@@ -18,258 +18,258 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Content_Filter_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The pattern to find entities in text. |
|
| 23 | - * |
|
| 24 | - * @since 3.8.0 |
|
| 25 | - */ |
|
| 26 | - const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.8.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.13.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $configuration_service; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The `link by default` setting. |
|
| 48 | - * |
|
| 49 | - * @since 3.13.0 |
|
| 50 | - * @access private |
|
| 51 | - * @var bool True if link by default is enabled otherwise false. |
|
| 52 | - */ |
|
| 53 | - private $is_link_by_default; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * A {@link Wordlift_Log_Service} instance. |
|
| 57 | - * |
|
| 58 | - * @since 3.16.0 |
|
| 59 | - * |
|
| 60 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 61 | - */ |
|
| 62 | - private $log; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 66 | - * |
|
| 67 | - * @since 3.14.2 |
|
| 68 | - * @access private |
|
| 69 | - * @var \Wordlift_Content_Filter_Service $instance The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 70 | - */ |
|
| 71 | - private static $instance; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 75 | - * |
|
| 76 | - * @since 3.8.0 |
|
| 77 | - * |
|
| 78 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 79 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 80 | - */ |
|
| 81 | - public function __construct( $entity_service, $configuration_service ) { |
|
| 82 | - |
|
| 83 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 84 | - |
|
| 85 | - $this->entity_service = $entity_service; |
|
| 86 | - $this->configuration_service = $configuration_service; |
|
| 87 | - |
|
| 88 | - self::$instance = $this; |
|
| 89 | - |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 94 | - * |
|
| 95 | - * @since 3.14.2 |
|
| 96 | - * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 97 | - */ |
|
| 98 | - public static function get_instance() { |
|
| 99 | - |
|
| 100 | - return self::$instance; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Mangle the content by adding links to the entity pages. This function is |
|
| 105 | - * hooked to the 'the_content' WP's filter. |
|
| 106 | - * |
|
| 107 | - * @since 3.8.0 |
|
| 108 | - * |
|
| 109 | - * @param string $content The content being filtered. |
|
| 110 | - * |
|
| 111 | - * @return string The filtered content. |
|
| 112 | - */ |
|
| 113 | - public function the_content( $content ) { |
|
| 114 | - |
|
| 115 | - $this->log->trace( 'Filtering content...' ); |
|
| 116 | - |
|
| 117 | - // Links should be added only on the front end and not for RSS. |
|
| 118 | - if ( is_feed() ) { |
|
| 119 | - return $content; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // Preload the `link by default` setting. |
|
| 123 | - $this->is_link_by_default = $this->configuration_service->is_link_by_default(); |
|
| 124 | - |
|
| 125 | - // Preload URIs. |
|
| 126 | - $matches = array(); |
|
| 127 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
| 128 | - $this->entity_service->preload_uris( $matches[3] ); |
|
| 129 | - |
|
| 130 | - // Replace each match of the entity tag with the entity link. If an error |
|
| 131 | - // occurs fail silently returning the original content. |
|
| 132 | - $result = preg_replace_callback( self::PATTERN, array( |
|
| 133 | - $this, |
|
| 134 | - 'link', |
|
| 135 | - ), $content ) ?: $content; |
|
| 136 | - |
|
| 137 | - $this->entity_service->reset_uris(); |
|
| 138 | - |
|
| 139 | - return $result; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Get the entity match and replace it with a page link. |
|
| 144 | - * |
|
| 145 | - * @since 3.8.0 |
|
| 146 | - * |
|
| 147 | - * @param array $matches An array of matches. |
|
| 148 | - * |
|
| 149 | - * @return string The replaced text with the link to the entity page. |
|
| 150 | - */ |
|
| 151 | - private function link( $matches ) { |
|
| 152 | - |
|
| 153 | - // Get the entity itemid URI and label. |
|
| 154 | - $css_class = $matches[2]; |
|
| 155 | - $uri = $matches[3]; |
|
| 156 | - $label = $matches[4]; |
|
| 157 | - |
|
| 158 | - // Get the entity post by URI. |
|
| 159 | - $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 160 | - if ( null === $post ) { |
|
| 161 | - |
|
| 162 | - // If the entity post is not found return the label w/o the markup |
|
| 163 | - // around it. |
|
| 164 | - // |
|
| 165 | - // See https://github.com/insideout10/wordlift-plugin/issues/461. |
|
| 166 | - return $label; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 170 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 171 | - |
|
| 172 | - // Don't link if links are disabled and the entity is not link or the |
|
| 173 | - // entity is do not link. |
|
| 174 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 175 | - |
|
| 176 | - // Return the label if it's don't link. |
|
| 177 | - if ( $dont_link ) { |
|
| 178 | - return $label; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // Get the link. |
|
| 182 | - $href = get_permalink( $post ); |
|
| 183 | - |
|
| 184 | - // Get an alternative title attribute. |
|
| 185 | - $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 186 | - |
|
| 187 | - // Return the link. |
|
| 188 | - return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Get a `title` attribute with an alternative label for the link. |
|
| 193 | - * |
|
| 194 | - * If an alternative title isn't available an empty string is returned. |
|
| 195 | - * |
|
| 196 | - * @since 3.15.0 |
|
| 197 | - * |
|
| 198 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 199 | - * @param string $label The main link label. |
|
| 200 | - * |
|
| 201 | - * @return string A `title` attribute with an alternative label or an empty |
|
| 202 | - * string if none available. |
|
| 203 | - */ |
|
| 204 | - private function get_title_attribute( $post_id, $label ) { |
|
| 205 | - |
|
| 206 | - // Get an alternative title. |
|
| 207 | - $title = $this->get_link_title( $post_id, $label ); |
|
| 208 | - if ( ! empty( $title ) ) { |
|
| 209 | - return 'title="' . esc_attr( $title ) . '"'; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - return ''; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Get a string to be used as a title attribute in links to a post |
|
| 217 | - * |
|
| 218 | - * @since 3.15.0 |
|
| 219 | - * |
|
| 220 | - * @param int $post_id The post id of the post being linked. |
|
| 221 | - * @param string $ignore_label A label to ignore. |
|
| 222 | - * |
|
| 223 | - * @return string The title to be used in the link. An empty string when |
|
| 224 | - * there is no alternative that is not the $ignore_label. |
|
| 225 | - */ |
|
| 226 | - function get_link_title( $post_id, $ignore_label ) { |
|
| 227 | - |
|
| 228 | - // Get possible alternative labels we can select from. |
|
| 229 | - $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 230 | - |
|
| 231 | - /* |
|
| 21 | + /** |
|
| 22 | + * The pattern to find entities in text. |
|
| 23 | + * |
|
| 24 | + * @since 3.8.0 |
|
| 25 | + */ |
|
| 26 | + const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.8.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.13.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $configuration_service; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The `link by default` setting. |
|
| 48 | + * |
|
| 49 | + * @since 3.13.0 |
|
| 50 | + * @access private |
|
| 51 | + * @var bool True if link by default is enabled otherwise false. |
|
| 52 | + */ |
|
| 53 | + private $is_link_by_default; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * A {@link Wordlift_Log_Service} instance. |
|
| 57 | + * |
|
| 58 | + * @since 3.16.0 |
|
| 59 | + * |
|
| 60 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 61 | + */ |
|
| 62 | + private $log; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 66 | + * |
|
| 67 | + * @since 3.14.2 |
|
| 68 | + * @access private |
|
| 69 | + * @var \Wordlift_Content_Filter_Service $instance The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 70 | + */ |
|
| 71 | + private static $instance; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 75 | + * |
|
| 76 | + * @since 3.8.0 |
|
| 77 | + * |
|
| 78 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 79 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 80 | + */ |
|
| 81 | + public function __construct( $entity_service, $configuration_service ) { |
|
| 82 | + |
|
| 83 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 84 | + |
|
| 85 | + $this->entity_service = $entity_service; |
|
| 86 | + $this->configuration_service = $configuration_service; |
|
| 87 | + |
|
| 88 | + self::$instance = $this; |
|
| 89 | + |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 94 | + * |
|
| 95 | + * @since 3.14.2 |
|
| 96 | + * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 97 | + */ |
|
| 98 | + public static function get_instance() { |
|
| 99 | + |
|
| 100 | + return self::$instance; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Mangle the content by adding links to the entity pages. This function is |
|
| 105 | + * hooked to the 'the_content' WP's filter. |
|
| 106 | + * |
|
| 107 | + * @since 3.8.0 |
|
| 108 | + * |
|
| 109 | + * @param string $content The content being filtered. |
|
| 110 | + * |
|
| 111 | + * @return string The filtered content. |
|
| 112 | + */ |
|
| 113 | + public function the_content( $content ) { |
|
| 114 | + |
|
| 115 | + $this->log->trace( 'Filtering content...' ); |
|
| 116 | + |
|
| 117 | + // Links should be added only on the front end and not for RSS. |
|
| 118 | + if ( is_feed() ) { |
|
| 119 | + return $content; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + // Preload the `link by default` setting. |
|
| 123 | + $this->is_link_by_default = $this->configuration_service->is_link_by_default(); |
|
| 124 | + |
|
| 125 | + // Preload URIs. |
|
| 126 | + $matches = array(); |
|
| 127 | + preg_match_all( self::PATTERN, $content, $matches ); |
|
| 128 | + $this->entity_service->preload_uris( $matches[3] ); |
|
| 129 | + |
|
| 130 | + // Replace each match of the entity tag with the entity link. If an error |
|
| 131 | + // occurs fail silently returning the original content. |
|
| 132 | + $result = preg_replace_callback( self::PATTERN, array( |
|
| 133 | + $this, |
|
| 134 | + 'link', |
|
| 135 | + ), $content ) ?: $content; |
|
| 136 | + |
|
| 137 | + $this->entity_service->reset_uris(); |
|
| 138 | + |
|
| 139 | + return $result; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Get the entity match and replace it with a page link. |
|
| 144 | + * |
|
| 145 | + * @since 3.8.0 |
|
| 146 | + * |
|
| 147 | + * @param array $matches An array of matches. |
|
| 148 | + * |
|
| 149 | + * @return string The replaced text with the link to the entity page. |
|
| 150 | + */ |
|
| 151 | + private function link( $matches ) { |
|
| 152 | + |
|
| 153 | + // Get the entity itemid URI and label. |
|
| 154 | + $css_class = $matches[2]; |
|
| 155 | + $uri = $matches[3]; |
|
| 156 | + $label = $matches[4]; |
|
| 157 | + |
|
| 158 | + // Get the entity post by URI. |
|
| 159 | + $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 160 | + if ( null === $post ) { |
|
| 161 | + |
|
| 162 | + // If the entity post is not found return the label w/o the markup |
|
| 163 | + // around it. |
|
| 164 | + // |
|
| 165 | + // See https://github.com/insideout10/wordlift-plugin/issues/461. |
|
| 166 | + return $label; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 170 | + $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 171 | + |
|
| 172 | + // Don't link if links are disabled and the entity is not link or the |
|
| 173 | + // entity is do not link. |
|
| 174 | + $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 175 | + |
|
| 176 | + // Return the label if it's don't link. |
|
| 177 | + if ( $dont_link ) { |
|
| 178 | + return $label; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // Get the link. |
|
| 182 | + $href = get_permalink( $post ); |
|
| 183 | + |
|
| 184 | + // Get an alternative title attribute. |
|
| 185 | + $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 186 | + |
|
| 187 | + // Return the link. |
|
| 188 | + return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Get a `title` attribute with an alternative label for the link. |
|
| 193 | + * |
|
| 194 | + * If an alternative title isn't available an empty string is returned. |
|
| 195 | + * |
|
| 196 | + * @since 3.15.0 |
|
| 197 | + * |
|
| 198 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 199 | + * @param string $label The main link label. |
|
| 200 | + * |
|
| 201 | + * @return string A `title` attribute with an alternative label or an empty |
|
| 202 | + * string if none available. |
|
| 203 | + */ |
|
| 204 | + private function get_title_attribute( $post_id, $label ) { |
|
| 205 | + |
|
| 206 | + // Get an alternative title. |
|
| 207 | + $title = $this->get_link_title( $post_id, $label ); |
|
| 208 | + if ( ! empty( $title ) ) { |
|
| 209 | + return 'title="' . esc_attr( $title ) . '"'; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + return ''; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Get a string to be used as a title attribute in links to a post |
|
| 217 | + * |
|
| 218 | + * @since 3.15.0 |
|
| 219 | + * |
|
| 220 | + * @param int $post_id The post id of the post being linked. |
|
| 221 | + * @param string $ignore_label A label to ignore. |
|
| 222 | + * |
|
| 223 | + * @return string The title to be used in the link. An empty string when |
|
| 224 | + * there is no alternative that is not the $ignore_label. |
|
| 225 | + */ |
|
| 226 | + function get_link_title( $post_id, $ignore_label ) { |
|
| 227 | + |
|
| 228 | + // Get possible alternative labels we can select from. |
|
| 229 | + $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 230 | + |
|
| 231 | + /* |
|
| 232 | 232 | * Since the original text might use an alternative label than the |
| 233 | 233 | * Entity title, add the title itself which is not returned by the api. |
| 234 | 234 | */ |
| 235 | - $labels[] = get_the_title( $post_id ); |
|
| 236 | - |
|
| 237 | - // Add some randomness to the label selection. |
|
| 238 | - shuffle( $labels ); |
|
| 239 | - |
|
| 240 | - // Select the first label which is not to be ignored. |
|
| 241 | - $title = ''; |
|
| 242 | - foreach ( $labels as $label ) { |
|
| 243 | - if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 244 | - $title = $label; |
|
| 245 | - break; |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - return $title; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 254 | - * the provided content. |
|
| 255 | - * |
|
| 256 | - * @since 3.14.2 |
|
| 257 | - * |
|
| 258 | - * @param string $content The content. |
|
| 259 | - * |
|
| 260 | - * @return array An array of URIs. |
|
| 261 | - */ |
|
| 262 | - public function get_entity_uris( $content ) { |
|
| 263 | - |
|
| 264 | - $matches = array(); |
|
| 265 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 266 | - |
|
| 267 | - // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 268 | - // to turn it into an object (since if the 3rd match isn't found the index |
|
| 269 | - // is not sequential. |
|
| 270 | - // |
|
| 271 | - // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 272 | - return array_values( array_unique( $matches[3] ) ); |
|
| 273 | - } |
|
| 235 | + $labels[] = get_the_title( $post_id ); |
|
| 236 | + |
|
| 237 | + // Add some randomness to the label selection. |
|
| 238 | + shuffle( $labels ); |
|
| 239 | + |
|
| 240 | + // Select the first label which is not to be ignored. |
|
| 241 | + $title = ''; |
|
| 242 | + foreach ( $labels as $label ) { |
|
| 243 | + if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 244 | + $title = $label; |
|
| 245 | + break; |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + return $title; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 254 | + * the provided content. |
|
| 255 | + * |
|
| 256 | + * @since 3.14.2 |
|
| 257 | + * |
|
| 258 | + * @param string $content The content. |
|
| 259 | + * |
|
| 260 | + * @return array An array of URIs. |
|
| 261 | + */ |
|
| 262 | + public function get_entity_uris( $content ) { |
|
| 263 | + |
|
| 264 | + $matches = array(); |
|
| 265 | + preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 266 | + |
|
| 267 | + // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 268 | + // to turn it into an object (since if the 3rd match isn't found the index |
|
| 269 | + // is not sequential. |
|
| 270 | + // |
|
| 271 | + // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 272 | + return array_values( array_unique( $matches[3] ) ); |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | 275 | } |
@@ -78,9 +78,9 @@ discard block |
||
| 78 | 78 | * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
| 79 | 79 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
| 80 | 80 | */ |
| 81 | - public function __construct( $entity_service, $configuration_service ) { |
|
| 81 | + public function __construct($entity_service, $configuration_service) { |
|
| 82 | 82 | |
| 83 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 83 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 84 | 84 | |
| 85 | 85 | $this->entity_service = $entity_service; |
| 86 | 86 | $this->configuration_service = $configuration_service; |
@@ -110,12 +110,12 @@ discard block |
||
| 110 | 110 | * |
| 111 | 111 | * @return string The filtered content. |
| 112 | 112 | */ |
| 113 | - public function the_content( $content ) { |
|
| 113 | + public function the_content($content) { |
|
| 114 | 114 | |
| 115 | - $this->log->trace( 'Filtering content...' ); |
|
| 115 | + $this->log->trace('Filtering content...'); |
|
| 116 | 116 | |
| 117 | 117 | // Links should be added only on the front end and not for RSS. |
| 118 | - if ( is_feed() ) { |
|
| 118 | + if (is_feed()) { |
|
| 119 | 119 | return $content; |
| 120 | 120 | } |
| 121 | 121 | |
@@ -124,15 +124,15 @@ discard block |
||
| 124 | 124 | |
| 125 | 125 | // Preload URIs. |
| 126 | 126 | $matches = array(); |
| 127 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
| 128 | - $this->entity_service->preload_uris( $matches[3] ); |
|
| 127 | + preg_match_all(self::PATTERN, $content, $matches); |
|
| 128 | + $this->entity_service->preload_uris($matches[3]); |
|
| 129 | 129 | |
| 130 | 130 | // Replace each match of the entity tag with the entity link. If an error |
| 131 | 131 | // occurs fail silently returning the original content. |
| 132 | - $result = preg_replace_callback( self::PATTERN, array( |
|
| 132 | + $result = preg_replace_callback(self::PATTERN, array( |
|
| 133 | 133 | $this, |
| 134 | 134 | 'link', |
| 135 | - ), $content ) ?: $content; |
|
| 135 | + ), $content) ?: $content; |
|
| 136 | 136 | |
| 137 | 137 | $this->entity_service->reset_uris(); |
| 138 | 138 | |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @return string The replaced text with the link to the entity page. |
| 150 | 150 | */ |
| 151 | - private function link( $matches ) { |
|
| 151 | + private function link($matches) { |
|
| 152 | 152 | |
| 153 | 153 | // Get the entity itemid URI and label. |
| 154 | 154 | $css_class = $matches[2]; |
@@ -156,8 +156,8 @@ discard block |
||
| 156 | 156 | $label = $matches[4]; |
| 157 | 157 | |
| 158 | 158 | // Get the entity post by URI. |
| 159 | - $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 160 | - if ( null === $post ) { |
|
| 159 | + $post = $this->entity_service->get_entity_post_by_uri($uri); |
|
| 160 | + if (null === $post) { |
|
| 161 | 161 | |
| 162 | 162 | // If the entity post is not found return the label w/o the markup |
| 163 | 163 | // around it. |
@@ -166,23 +166,23 @@ discard block |
||
| 166 | 166 | return $label; |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 170 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 169 | + $no_link = - 1 < strpos($css_class, 'wl-no-link'); |
|
| 170 | + $link = - 1 < strpos($css_class, 'wl-link'); |
|
| 171 | 171 | |
| 172 | 172 | // Don't link if links are disabled and the entity is not link or the |
| 173 | 173 | // entity is do not link. |
| 174 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 174 | + $dont_link = ( ! $this->is_link_by_default && ! $link) || $no_link; |
|
| 175 | 175 | |
| 176 | 176 | // Return the label if it's don't link. |
| 177 | - if ( $dont_link ) { |
|
| 177 | + if ($dont_link) { |
|
| 178 | 178 | return $label; |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | // Get the link. |
| 182 | - $href = get_permalink( $post ); |
|
| 182 | + $href = get_permalink($post); |
|
| 183 | 183 | |
| 184 | 184 | // Get an alternative title attribute. |
| 185 | - $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 185 | + $title_attribute = $this->get_title_attribute($post->ID, $label); |
|
| 186 | 186 | |
| 187 | 187 | // Return the link. |
| 188 | 188 | return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
@@ -201,12 +201,12 @@ discard block |
||
| 201 | 201 | * @return string A `title` attribute with an alternative label or an empty |
| 202 | 202 | * string if none available. |
| 203 | 203 | */ |
| 204 | - private function get_title_attribute( $post_id, $label ) { |
|
| 204 | + private function get_title_attribute($post_id, $label) { |
|
| 205 | 205 | |
| 206 | 206 | // Get an alternative title. |
| 207 | - $title = $this->get_link_title( $post_id, $label ); |
|
| 208 | - if ( ! empty( $title ) ) { |
|
| 209 | - return 'title="' . esc_attr( $title ) . '"'; |
|
| 207 | + $title = $this->get_link_title($post_id, $label); |
|
| 208 | + if ( ! empty($title)) { |
|
| 209 | + return 'title="'.esc_attr($title).'"'; |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | return ''; |
@@ -223,24 +223,24 @@ discard block |
||
| 223 | 223 | * @return string The title to be used in the link. An empty string when |
| 224 | 224 | * there is no alternative that is not the $ignore_label. |
| 225 | 225 | */ |
| 226 | - function get_link_title( $post_id, $ignore_label ) { |
|
| 226 | + function get_link_title($post_id, $ignore_label) { |
|
| 227 | 227 | |
| 228 | 228 | // Get possible alternative labels we can select from. |
| 229 | - $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 229 | + $labels = $this->entity_service->get_alternative_labels($post_id); |
|
| 230 | 230 | |
| 231 | 231 | /* |
| 232 | 232 | * Since the original text might use an alternative label than the |
| 233 | 233 | * Entity title, add the title itself which is not returned by the api. |
| 234 | 234 | */ |
| 235 | - $labels[] = get_the_title( $post_id ); |
|
| 235 | + $labels[] = get_the_title($post_id); |
|
| 236 | 236 | |
| 237 | 237 | // Add some randomness to the label selection. |
| 238 | - shuffle( $labels ); |
|
| 238 | + shuffle($labels); |
|
| 239 | 239 | |
| 240 | 240 | // Select the first label which is not to be ignored. |
| 241 | 241 | $title = ''; |
| 242 | - foreach ( $labels as $label ) { |
|
| 243 | - if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 242 | + foreach ($labels as $label) { |
|
| 243 | + if (0 !== strcasecmp($label, $ignore_label)) { |
|
| 244 | 244 | $title = $label; |
| 245 | 245 | break; |
| 246 | 246 | } |
@@ -259,17 +259,17 @@ discard block |
||
| 259 | 259 | * |
| 260 | 260 | * @return array An array of URIs. |
| 261 | 261 | */ |
| 262 | - public function get_entity_uris( $content ) { |
|
| 262 | + public function get_entity_uris($content) { |
|
| 263 | 263 | |
| 264 | 264 | $matches = array(); |
| 265 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 265 | + preg_match_all(Wordlift_Content_Filter_Service::PATTERN, $content, $matches); |
|
| 266 | 266 | |
| 267 | 267 | // We need to use `array_values` here in order to avoid further `json_encode` |
| 268 | 268 | // to turn it into an object (since if the 3rd match isn't found the index |
| 269 | 269 | // is not sequential. |
| 270 | 270 | // |
| 271 | 271 | // See https://github.com/insideout10/wordlift-plugin/issues/646. |
| 272 | - return array_values( array_unique( $matches[3] ) ); |
|
| 272 | + return array_values(array_unique($matches[3])); |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | } |
@@ -19,295 +19,295 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Wordlift_Entity_Type_Service { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The {@link Wordlift_Schema_Service} instance. |
|
| 24 | - * |
|
| 25 | - * @since 3.7.0 |
|
| 26 | - * @access private |
|
| 27 | - * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 28 | - */ |
|
| 29 | - private $schema_service; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * A {@link Wordlift_Log_Service} instance. |
|
| 33 | - * |
|
| 34 | - * @since 3.8.0 |
|
| 35 | - * @access private |
|
| 36 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 37 | - */ |
|
| 38 | - private $log; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 42 | - * |
|
| 43 | - * @since 3.7.0 |
|
| 44 | - * @access private |
|
| 45 | - * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 46 | - */ |
|
| 47 | - private static $instance; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Wordlift_Entity_Type_Service constructor. |
|
| 51 | - * |
|
| 52 | - * @since 3.7.0 |
|
| 53 | - * |
|
| 54 | - * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 55 | - */ |
|
| 56 | - public function __construct( $schema_service ) { |
|
| 57 | - |
|
| 58 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' ); |
|
| 59 | - |
|
| 60 | - $this->schema_service = $schema_service; |
|
| 61 | - |
|
| 62 | - self::$instance = $this; |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Get the {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 68 | - * |
|
| 69 | - * @since 3.7.0 |
|
| 70 | - * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 71 | - */ |
|
| 72 | - public static function get_instance() { |
|
| 73 | - |
|
| 74 | - return self::$instance; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Get the types associated with the specified entity post id. |
|
| 79 | - * |
|
| 80 | - * @since 3.7.0 |
|
| 81 | - * |
|
| 82 | - * @param int $post_id The post id. |
|
| 83 | - * |
|
| 84 | - * @return array|null { |
|
| 85 | - * An array of type properties or null if no term is associated |
|
| 86 | - * |
|
| 87 | - * @type string css_class The css class, e.g. `wl-thing`. |
|
| 88 | - * @type string uri The schema.org class URI, e.g. `http://schema.org/Thing`. |
|
| 89 | - * @type array same_as An array of same as attributes. |
|
| 90 | - * @type array custom_fields An array of custom fields. |
|
| 91 | - * @type array linked_data An array of {@link Wordlift_Sparql_Tuple_Rendition}. |
|
| 92 | - * } |
|
| 93 | - */ |
|
| 94 | - public function get( $post_id ) { |
|
| 95 | - |
|
| 96 | - $this->log->trace( "Getting the post type for post $post_id..." ); |
|
| 97 | - |
|
| 98 | - $post_type = get_post_type( $post_id ); |
|
| 99 | - |
|
| 100 | - // If it's not an entity post type return `Thing` by default. |
|
| 101 | - if ( ! self::is_valid_entity_post_type( $post_type ) ) { |
|
| 102 | - $this->log->info( "Returning `Thing` for post $post_id." ); |
|
| 103 | - |
|
| 104 | - return array( |
|
| 105 | - 'uri' => 'http://schema.org/Thing', |
|
| 106 | - 'css_class' => 'wl-thing', |
|
| 107 | - ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // Get the type from the associated classification. |
|
| 111 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 112 | - |
|
| 113 | - if ( is_wp_error( $terms ) ) { |
|
| 114 | - $this->log->error( "An error occurred while getting the post type for post $post_id: " . $terms->get_error_message() ); |
|
| 115 | - |
|
| 116 | - // TODO: handle error |
|
| 117 | - return null; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - // If there are not terms associated, default to article. |
|
| 121 | - if ( 0 === count( $terms ) ) { |
|
| 122 | - $this->log->debug( "Post $post_id has no terms, returning `Article`." ); |
|
| 123 | - |
|
| 124 | - return array( |
|
| 125 | - 'uri' => 'http://schema.org/Article', |
|
| 126 | - 'css_class' => 'wl-post', |
|
| 127 | - ); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $this->log->debug( "Found {$terms[0]->slug} term for post $post_id." ); |
|
| 131 | - |
|
| 132 | - // Return the entity type with the specified id. |
|
| 133 | - return $this->schema_service->get_schema( $terms[0]->slug ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Set the main type for the specified entity post, given the type URI. |
|
| 138 | - * |
|
| 139 | - * @since 3.8.0 |
|
| 140 | - * |
|
| 141 | - * @param int $post_id The post id. |
|
| 142 | - * @param string $type_uri The type URI. |
|
| 143 | - */ |
|
| 144 | - public function set( $post_id, $type_uri ) { |
|
| 145 | - |
|
| 146 | - // If the type URI is empty we remove the type. |
|
| 147 | - if ( empty( $type_uri ) ) { |
|
| 148 | - $this->log->debug( "Removing entity type for post $post_id..." ); |
|
| 149 | - |
|
| 150 | - wp_set_object_terms( $post_id, null, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 151 | - |
|
| 152 | - return; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $this->log->debug( "Setting entity type for post $post_id..." ); |
|
| 156 | - |
|
| 157 | - // Get all the terms bound to the wl_entity_type taxonomy. |
|
| 158 | - $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 159 | - 'hide_empty' => false, |
|
| 160 | - // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 161 | - // An issue has been opened with the AAM plugin author as well. |
|
| 162 | - // |
|
| 163 | - // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 164 | - // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 165 | - 'fields' => 'all', |
|
| 166 | - ) ); |
|
| 167 | - |
|
| 168 | - // Check which term matches the specified URI. |
|
| 169 | - foreach ( $terms as $term ) { |
|
| 170 | - |
|
| 171 | - $term_id = $term->term_id; |
|
| 172 | - $term_slug = $term->slug; |
|
| 173 | - |
|
| 174 | - $this->log->trace( "Parsing term {$term->slug}..." ); |
|
| 175 | - |
|
| 176 | - // Load the type data. |
|
| 177 | - $type = $this->schema_service->get_schema( $term_slug ); |
|
| 178 | - |
|
| 179 | - // Set the related term ID. |
|
| 180 | - if ( $type_uri === $type['uri'] || $type_uri === $type['css_class'] ) { |
|
| 181 | - |
|
| 182 | - $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term_id ][ term slug :: $term_slug ][ type uri :: {$type['uri']} ][ type css class :: {$type['css_class']} ]" ); |
|
| 183 | - |
|
| 184 | - wp_set_object_terms( $post_id, (int) $term_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 185 | - |
|
| 186 | - return; |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - $this->log->error( "Type not found [ post id :: $post_id ][ type uri :: $type_uri ]" ); |
|
| 191 | - |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Check whether an entity type is set for the {@link WP_Post} with the |
|
| 196 | - * specified id. |
|
| 197 | - * |
|
| 198 | - * @since 3.15.0 |
|
| 199 | - * |
|
| 200 | - * @param int $post_id The {@link WP_Post}'s `id`. |
|
| 201 | - * @param string $uri The entity type URI. |
|
| 202 | - * |
|
| 203 | - * @return bool True if an entity type is set otherwise false. |
|
| 204 | - */ |
|
| 205 | - public function has_entity_type( $post_id, $uri = null ) { |
|
| 206 | - |
|
| 207 | - $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." ); |
|
| 208 | - |
|
| 209 | - // If an URI hasn't been specified just check whether we have at least |
|
| 210 | - // one entity type. |
|
| 211 | - if ( null === $uri ) { |
|
| 212 | - |
|
| 213 | - // Get the post terms for the specified post ID. |
|
| 214 | - $terms = $this->get_post_terms( $post_id ); |
|
| 215 | - |
|
| 216 | - $this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' ); |
|
| 217 | - |
|
| 218 | - // True if there's at least one term bound to the post. |
|
| 219 | - return ( 0 < count( $terms ) ); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - $has_entity_type = ( null !== $this->get_term_by_uri( $post_id, $uri ) ); |
|
| 223 | - |
|
| 224 | - $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) ); |
|
| 225 | - |
|
| 226 | - // Check whether the post has an entity type with that URI. |
|
| 227 | - return $has_entity_type; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Get the list of entity types' terms for the specified {@link WP_Post}. |
|
| 232 | - * |
|
| 233 | - * @since 3.15.0 |
|
| 234 | - * |
|
| 235 | - * @param int $post_id The {@link WP_Post} id. |
|
| 236 | - * |
|
| 237 | - * @return array|WP_Error An array of entity types' terms or {@link WP_Error}. |
|
| 238 | - */ |
|
| 239 | - private function get_post_terms( $post_id ) { |
|
| 240 | - |
|
| 241 | - return wp_get_post_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 242 | - 'hide_empty' => false, |
|
| 243 | - // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 244 | - // An issue has been opened with the AAM plugin author as well. |
|
| 245 | - // |
|
| 246 | - // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 247 | - // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 248 | - 'fields' => 'all', |
|
| 249 | - ) ); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Get an entity type term given its URI. |
|
| 254 | - * |
|
| 255 | - * @since 3.15.0 |
|
| 256 | - * |
|
| 257 | - * @param int $post_id The {@link WP_Post} id. |
|
| 258 | - * @param string $uri The entity type URI. |
|
| 259 | - * |
|
| 260 | - * @return array|null { |
|
| 261 | - * An array of entity type properties or null if no term is associated |
|
| 262 | - * |
|
| 263 | - * @type string css_class The css class, e.g. `wl-thing`. |
|
| 264 | - * @type string uri The schema.org class URI, e.g. `http://schema.org/Thing`. |
|
| 265 | - * @type array same_as An array of same as attributes. |
|
| 266 | - * @type array custom_fields An array of custom fields. |
|
| 267 | - * } |
|
| 268 | - */ |
|
| 269 | - private function get_term_by_uri( $post_id, $uri ) { |
|
| 270 | - |
|
| 271 | - // Get the post terms bound to the specified post. |
|
| 272 | - $terms = $this->get_post_terms( $post_id ); |
|
| 273 | - |
|
| 274 | - // Look for a term if the specified URI. |
|
| 275 | - foreach ( $terms as $term ) { |
|
| 276 | - // Get the schema by slug. |
|
| 277 | - $schema = $this->schema_service->get_schema( $term->slug ); |
|
| 278 | - |
|
| 279 | - // Continue to the next one, if a schema isn't found (or hasn't got |
|
| 280 | - // and URI. |
|
| 281 | - if ( null === $schema || ! isset( $schema['uri'] ) ) { |
|
| 282 | - continue; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - // Return the schema if the URI matches. |
|
| 286 | - if ( $uri === $schema['uri'] ) { |
|
| 287 | - return $schema; |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - // Return null. |
|
| 292 | - return null; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Determines whether a post type can be used for entities. |
|
| 298 | - * |
|
| 299 | - * Criteria is that the post type is public. The list of valid post types |
|
| 300 | - * can be overridden with a filter. |
|
| 301 | - * |
|
| 302 | - * @since 3.15.0 |
|
| 303 | - * |
|
| 304 | - * @param string $post_type A post type name. |
|
| 305 | - * |
|
| 306 | - * @return bool Return true if the post type can be used for entities, otherwise false. |
|
| 307 | - */ |
|
| 308 | - public static function is_valid_entity_post_type( $post_type ) { |
|
| 309 | - |
|
| 310 | - return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ); |
|
| 311 | - } |
|
| 22 | + /** |
|
| 23 | + * The {@link Wordlift_Schema_Service} instance. |
|
| 24 | + * |
|
| 25 | + * @since 3.7.0 |
|
| 26 | + * @access private |
|
| 27 | + * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 28 | + */ |
|
| 29 | + private $schema_service; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * A {@link Wordlift_Log_Service} instance. |
|
| 33 | + * |
|
| 34 | + * @since 3.8.0 |
|
| 35 | + * @access private |
|
| 36 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 37 | + */ |
|
| 38 | + private $log; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 42 | + * |
|
| 43 | + * @since 3.7.0 |
|
| 44 | + * @access private |
|
| 45 | + * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 46 | + */ |
|
| 47 | + private static $instance; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Wordlift_Entity_Type_Service constructor. |
|
| 51 | + * |
|
| 52 | + * @since 3.7.0 |
|
| 53 | + * |
|
| 54 | + * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 55 | + */ |
|
| 56 | + public function __construct( $schema_service ) { |
|
| 57 | + |
|
| 58 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' ); |
|
| 59 | + |
|
| 60 | + $this->schema_service = $schema_service; |
|
| 61 | + |
|
| 62 | + self::$instance = $this; |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Get the {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 68 | + * |
|
| 69 | + * @since 3.7.0 |
|
| 70 | + * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 71 | + */ |
|
| 72 | + public static function get_instance() { |
|
| 73 | + |
|
| 74 | + return self::$instance; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Get the types associated with the specified entity post id. |
|
| 79 | + * |
|
| 80 | + * @since 3.7.0 |
|
| 81 | + * |
|
| 82 | + * @param int $post_id The post id. |
|
| 83 | + * |
|
| 84 | + * @return array|null { |
|
| 85 | + * An array of type properties or null if no term is associated |
|
| 86 | + * |
|
| 87 | + * @type string css_class The css class, e.g. `wl-thing`. |
|
| 88 | + * @type string uri The schema.org class URI, e.g. `http://schema.org/Thing`. |
|
| 89 | + * @type array same_as An array of same as attributes. |
|
| 90 | + * @type array custom_fields An array of custom fields. |
|
| 91 | + * @type array linked_data An array of {@link Wordlift_Sparql_Tuple_Rendition}. |
|
| 92 | + * } |
|
| 93 | + */ |
|
| 94 | + public function get( $post_id ) { |
|
| 95 | + |
|
| 96 | + $this->log->trace( "Getting the post type for post $post_id..." ); |
|
| 97 | + |
|
| 98 | + $post_type = get_post_type( $post_id ); |
|
| 99 | + |
|
| 100 | + // If it's not an entity post type return `Thing` by default. |
|
| 101 | + if ( ! self::is_valid_entity_post_type( $post_type ) ) { |
|
| 102 | + $this->log->info( "Returning `Thing` for post $post_id." ); |
|
| 103 | + |
|
| 104 | + return array( |
|
| 105 | + 'uri' => 'http://schema.org/Thing', |
|
| 106 | + 'css_class' => 'wl-thing', |
|
| 107 | + ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // Get the type from the associated classification. |
|
| 111 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 112 | + |
|
| 113 | + if ( is_wp_error( $terms ) ) { |
|
| 114 | + $this->log->error( "An error occurred while getting the post type for post $post_id: " . $terms->get_error_message() ); |
|
| 115 | + |
|
| 116 | + // TODO: handle error |
|
| 117 | + return null; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + // If there are not terms associated, default to article. |
|
| 121 | + if ( 0 === count( $terms ) ) { |
|
| 122 | + $this->log->debug( "Post $post_id has no terms, returning `Article`." ); |
|
| 123 | + |
|
| 124 | + return array( |
|
| 125 | + 'uri' => 'http://schema.org/Article', |
|
| 126 | + 'css_class' => 'wl-post', |
|
| 127 | + ); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $this->log->debug( "Found {$terms[0]->slug} term for post $post_id." ); |
|
| 131 | + |
|
| 132 | + // Return the entity type with the specified id. |
|
| 133 | + return $this->schema_service->get_schema( $terms[0]->slug ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Set the main type for the specified entity post, given the type URI. |
|
| 138 | + * |
|
| 139 | + * @since 3.8.0 |
|
| 140 | + * |
|
| 141 | + * @param int $post_id The post id. |
|
| 142 | + * @param string $type_uri The type URI. |
|
| 143 | + */ |
|
| 144 | + public function set( $post_id, $type_uri ) { |
|
| 145 | + |
|
| 146 | + // If the type URI is empty we remove the type. |
|
| 147 | + if ( empty( $type_uri ) ) { |
|
| 148 | + $this->log->debug( "Removing entity type for post $post_id..." ); |
|
| 149 | + |
|
| 150 | + wp_set_object_terms( $post_id, null, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 151 | + |
|
| 152 | + return; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $this->log->debug( "Setting entity type for post $post_id..." ); |
|
| 156 | + |
|
| 157 | + // Get all the terms bound to the wl_entity_type taxonomy. |
|
| 158 | + $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 159 | + 'hide_empty' => false, |
|
| 160 | + // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 161 | + // An issue has been opened with the AAM plugin author as well. |
|
| 162 | + // |
|
| 163 | + // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 164 | + // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 165 | + 'fields' => 'all', |
|
| 166 | + ) ); |
|
| 167 | + |
|
| 168 | + // Check which term matches the specified URI. |
|
| 169 | + foreach ( $terms as $term ) { |
|
| 170 | + |
|
| 171 | + $term_id = $term->term_id; |
|
| 172 | + $term_slug = $term->slug; |
|
| 173 | + |
|
| 174 | + $this->log->trace( "Parsing term {$term->slug}..." ); |
|
| 175 | + |
|
| 176 | + // Load the type data. |
|
| 177 | + $type = $this->schema_service->get_schema( $term_slug ); |
|
| 178 | + |
|
| 179 | + // Set the related term ID. |
|
| 180 | + if ( $type_uri === $type['uri'] || $type_uri === $type['css_class'] ) { |
|
| 181 | + |
|
| 182 | + $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term_id ][ term slug :: $term_slug ][ type uri :: {$type['uri']} ][ type css class :: {$type['css_class']} ]" ); |
|
| 183 | + |
|
| 184 | + wp_set_object_terms( $post_id, (int) $term_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 185 | + |
|
| 186 | + return; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + $this->log->error( "Type not found [ post id :: $post_id ][ type uri :: $type_uri ]" ); |
|
| 191 | + |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Check whether an entity type is set for the {@link WP_Post} with the |
|
| 196 | + * specified id. |
|
| 197 | + * |
|
| 198 | + * @since 3.15.0 |
|
| 199 | + * |
|
| 200 | + * @param int $post_id The {@link WP_Post}'s `id`. |
|
| 201 | + * @param string $uri The entity type URI. |
|
| 202 | + * |
|
| 203 | + * @return bool True if an entity type is set otherwise false. |
|
| 204 | + */ |
|
| 205 | + public function has_entity_type( $post_id, $uri = null ) { |
|
| 206 | + |
|
| 207 | + $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." ); |
|
| 208 | + |
|
| 209 | + // If an URI hasn't been specified just check whether we have at least |
|
| 210 | + // one entity type. |
|
| 211 | + if ( null === $uri ) { |
|
| 212 | + |
|
| 213 | + // Get the post terms for the specified post ID. |
|
| 214 | + $terms = $this->get_post_terms( $post_id ); |
|
| 215 | + |
|
| 216 | + $this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' ); |
|
| 217 | + |
|
| 218 | + // True if there's at least one term bound to the post. |
|
| 219 | + return ( 0 < count( $terms ) ); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + $has_entity_type = ( null !== $this->get_term_by_uri( $post_id, $uri ) ); |
|
| 223 | + |
|
| 224 | + $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) ); |
|
| 225 | + |
|
| 226 | + // Check whether the post has an entity type with that URI. |
|
| 227 | + return $has_entity_type; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Get the list of entity types' terms for the specified {@link WP_Post}. |
|
| 232 | + * |
|
| 233 | + * @since 3.15.0 |
|
| 234 | + * |
|
| 235 | + * @param int $post_id The {@link WP_Post} id. |
|
| 236 | + * |
|
| 237 | + * @return array|WP_Error An array of entity types' terms or {@link WP_Error}. |
|
| 238 | + */ |
|
| 239 | + private function get_post_terms( $post_id ) { |
|
| 240 | + |
|
| 241 | + return wp_get_post_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 242 | + 'hide_empty' => false, |
|
| 243 | + // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 244 | + // An issue has been opened with the AAM plugin author as well. |
|
| 245 | + // |
|
| 246 | + // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 247 | + // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 248 | + 'fields' => 'all', |
|
| 249 | + ) ); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Get an entity type term given its URI. |
|
| 254 | + * |
|
| 255 | + * @since 3.15.0 |
|
| 256 | + * |
|
| 257 | + * @param int $post_id The {@link WP_Post} id. |
|
| 258 | + * @param string $uri The entity type URI. |
|
| 259 | + * |
|
| 260 | + * @return array|null { |
|
| 261 | + * An array of entity type properties or null if no term is associated |
|
| 262 | + * |
|
| 263 | + * @type string css_class The css class, e.g. `wl-thing`. |
|
| 264 | + * @type string uri The schema.org class URI, e.g. `http://schema.org/Thing`. |
|
| 265 | + * @type array same_as An array of same as attributes. |
|
| 266 | + * @type array custom_fields An array of custom fields. |
|
| 267 | + * } |
|
| 268 | + */ |
|
| 269 | + private function get_term_by_uri( $post_id, $uri ) { |
|
| 270 | + |
|
| 271 | + // Get the post terms bound to the specified post. |
|
| 272 | + $terms = $this->get_post_terms( $post_id ); |
|
| 273 | + |
|
| 274 | + // Look for a term if the specified URI. |
|
| 275 | + foreach ( $terms as $term ) { |
|
| 276 | + // Get the schema by slug. |
|
| 277 | + $schema = $this->schema_service->get_schema( $term->slug ); |
|
| 278 | + |
|
| 279 | + // Continue to the next one, if a schema isn't found (or hasn't got |
|
| 280 | + // and URI. |
|
| 281 | + if ( null === $schema || ! isset( $schema['uri'] ) ) { |
|
| 282 | + continue; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + // Return the schema if the URI matches. |
|
| 286 | + if ( $uri === $schema['uri'] ) { |
|
| 287 | + return $schema; |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + // Return null. |
|
| 292 | + return null; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Determines whether a post type can be used for entities. |
|
| 298 | + * |
|
| 299 | + * Criteria is that the post type is public. The list of valid post types |
|
| 300 | + * can be overridden with a filter. |
|
| 301 | + * |
|
| 302 | + * @since 3.15.0 |
|
| 303 | + * |
|
| 304 | + * @param string $post_type A post type name. |
|
| 305 | + * |
|
| 306 | + * @return bool Return true if the post type can be used for entities, otherwise false. |
|
| 307 | + */ |
|
| 308 | + public static function is_valid_entity_post_type( $post_type ) { |
|
| 309 | + |
|
| 310 | + return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ); |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | 313 | } |
@@ -53,9 +53,9 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
| 55 | 55 | */ |
| 56 | - public function __construct( $schema_service ) { |
|
| 56 | + public function __construct($schema_service) { |
|
| 57 | 57 | |
| 58 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' ); |
|
| 58 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Type_Service'); |
|
| 59 | 59 | |
| 60 | 60 | $this->schema_service = $schema_service; |
| 61 | 61 | |
@@ -91,15 +91,15 @@ discard block |
||
| 91 | 91 | * @type array linked_data An array of {@link Wordlift_Sparql_Tuple_Rendition}. |
| 92 | 92 | * } |
| 93 | 93 | */ |
| 94 | - public function get( $post_id ) { |
|
| 94 | + public function get($post_id) { |
|
| 95 | 95 | |
| 96 | - $this->log->trace( "Getting the post type for post $post_id..." ); |
|
| 96 | + $this->log->trace("Getting the post type for post $post_id..."); |
|
| 97 | 97 | |
| 98 | - $post_type = get_post_type( $post_id ); |
|
| 98 | + $post_type = get_post_type($post_id); |
|
| 99 | 99 | |
| 100 | 100 | // If it's not an entity post type return `Thing` by default. |
| 101 | - if ( ! self::is_valid_entity_post_type( $post_type ) ) { |
|
| 102 | - $this->log->info( "Returning `Thing` for post $post_id." ); |
|
| 101 | + if ( ! self::is_valid_entity_post_type($post_type)) { |
|
| 102 | + $this->log->info("Returning `Thing` for post $post_id."); |
|
| 103 | 103 | |
| 104 | 104 | return array( |
| 105 | 105 | 'uri' => 'http://schema.org/Thing', |
@@ -108,18 +108,18 @@ discard block |
||
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // Get the type from the associated classification. |
| 111 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 111 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); |
|
| 112 | 112 | |
| 113 | - if ( is_wp_error( $terms ) ) { |
|
| 114 | - $this->log->error( "An error occurred while getting the post type for post $post_id: " . $terms->get_error_message() ); |
|
| 113 | + if (is_wp_error($terms)) { |
|
| 114 | + $this->log->error("An error occurred while getting the post type for post $post_id: ".$terms->get_error_message()); |
|
| 115 | 115 | |
| 116 | 116 | // TODO: handle error |
| 117 | 117 | return null; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | // If there are not terms associated, default to article. |
| 121 | - if ( 0 === count( $terms ) ) { |
|
| 122 | - $this->log->debug( "Post $post_id has no terms, returning `Article`." ); |
|
| 121 | + if (0 === count($terms)) { |
|
| 122 | + $this->log->debug("Post $post_id has no terms, returning `Article`."); |
|
| 123 | 123 | |
| 124 | 124 | return array( |
| 125 | 125 | 'uri' => 'http://schema.org/Article', |
@@ -127,10 +127,10 @@ discard block |
||
| 127 | 127 | ); |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | - $this->log->debug( "Found {$terms[0]->slug} term for post $post_id." ); |
|
| 130 | + $this->log->debug("Found {$terms[0]->slug} term for post $post_id."); |
|
| 131 | 131 | |
| 132 | 132 | // Return the entity type with the specified id. |
| 133 | - return $this->schema_service->get_schema( $terms[0]->slug ); |
|
| 133 | + return $this->schema_service->get_schema($terms[0]->slug); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -141,21 +141,21 @@ discard block |
||
| 141 | 141 | * @param int $post_id The post id. |
| 142 | 142 | * @param string $type_uri The type URI. |
| 143 | 143 | */ |
| 144 | - public function set( $post_id, $type_uri ) { |
|
| 144 | + public function set($post_id, $type_uri) { |
|
| 145 | 145 | |
| 146 | 146 | // If the type URI is empty we remove the type. |
| 147 | - if ( empty( $type_uri ) ) { |
|
| 148 | - $this->log->debug( "Removing entity type for post $post_id..." ); |
|
| 147 | + if (empty($type_uri)) { |
|
| 148 | + $this->log->debug("Removing entity type for post $post_id..."); |
|
| 149 | 149 | |
| 150 | - wp_set_object_terms( $post_id, null, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 150 | + wp_set_object_terms($post_id, null, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); |
|
| 151 | 151 | |
| 152 | 152 | return; |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - $this->log->debug( "Setting entity type for post $post_id..." ); |
|
| 155 | + $this->log->debug("Setting entity type for post $post_id..."); |
|
| 156 | 156 | |
| 157 | 157 | // Get all the terms bound to the wl_entity_type taxonomy. |
| 158 | - $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 158 | + $terms = get_terms(Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 159 | 159 | 'hide_empty' => false, |
| 160 | 160 | // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
| 161 | 161 | // An issue has been opened with the AAM plugin author as well. |
@@ -163,31 +163,31 @@ discard block |
||
| 163 | 163 | // see https://github.com/insideout10/wordlift-plugin/issues/334 |
| 164 | 164 | // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
| 165 | 165 | 'fields' => 'all', |
| 166 | - ) ); |
|
| 166 | + )); |
|
| 167 | 167 | |
| 168 | 168 | // Check which term matches the specified URI. |
| 169 | - foreach ( $terms as $term ) { |
|
| 169 | + foreach ($terms as $term) { |
|
| 170 | 170 | |
| 171 | 171 | $term_id = $term->term_id; |
| 172 | 172 | $term_slug = $term->slug; |
| 173 | 173 | |
| 174 | - $this->log->trace( "Parsing term {$term->slug}..." ); |
|
| 174 | + $this->log->trace("Parsing term {$term->slug}..."); |
|
| 175 | 175 | |
| 176 | 176 | // Load the type data. |
| 177 | - $type = $this->schema_service->get_schema( $term_slug ); |
|
| 177 | + $type = $this->schema_service->get_schema($term_slug); |
|
| 178 | 178 | |
| 179 | 179 | // Set the related term ID. |
| 180 | - if ( $type_uri === $type['uri'] || $type_uri === $type['css_class'] ) { |
|
| 180 | + if ($type_uri === $type['uri'] || $type_uri === $type['css_class']) { |
|
| 181 | 181 | |
| 182 | - $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term_id ][ term slug :: $term_slug ][ type uri :: {$type['uri']} ][ type css class :: {$type['css_class']} ]" ); |
|
| 182 | + $this->log->debug("Setting entity type [ post id :: $post_id ][ term id :: $term_id ][ term slug :: $term_slug ][ type uri :: {$type['uri']} ][ type css class :: {$type['css_class']} ]"); |
|
| 183 | 183 | |
| 184 | - wp_set_object_terms( $post_id, (int) $term_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 184 | + wp_set_object_terms($post_id, (int) $term_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); |
|
| 185 | 185 | |
| 186 | 186 | return; |
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - $this->log->error( "Type not found [ post id :: $post_id ][ type uri :: $type_uri ]" ); |
|
| 190 | + $this->log->error("Type not found [ post id :: $post_id ][ type uri :: $type_uri ]"); |
|
| 191 | 191 | |
| 192 | 192 | } |
| 193 | 193 | |
@@ -202,26 +202,26 @@ discard block |
||
| 202 | 202 | * |
| 203 | 203 | * @return bool True if an entity type is set otherwise false. |
| 204 | 204 | */ |
| 205 | - public function has_entity_type( $post_id, $uri = null ) { |
|
| 205 | + public function has_entity_type($post_id, $uri = null) { |
|
| 206 | 206 | |
| 207 | - $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." ); |
|
| 207 | + $this->log->debug("Checking if post $post_id has an entity type [ $uri ]..."); |
|
| 208 | 208 | |
| 209 | 209 | // If an URI hasn't been specified just check whether we have at least |
| 210 | 210 | // one entity type. |
| 211 | - if ( null === $uri ) { |
|
| 211 | + if (null === $uri) { |
|
| 212 | 212 | |
| 213 | 213 | // Get the post terms for the specified post ID. |
| 214 | - $terms = $this->get_post_terms( $post_id ); |
|
| 214 | + $terms = $this->get_post_terms($post_id); |
|
| 215 | 215 | |
| 216 | - $this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' ); |
|
| 216 | + $this->log->debug("Post $post_id has ".count($terms).' type(s).'); |
|
| 217 | 217 | |
| 218 | 218 | // True if there's at least one term bound to the post. |
| 219 | - return ( 0 < count( $terms ) ); |
|
| 219 | + return (0 < count($terms)); |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | - $has_entity_type = ( null !== $this->get_term_by_uri( $post_id, $uri ) ); |
|
| 222 | + $has_entity_type = (null !== $this->get_term_by_uri($post_id, $uri)); |
|
| 223 | 223 | |
| 224 | - $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) ); |
|
| 224 | + $this->log->debug("Post $post_id has $uri type: ".($has_entity_type ? 'yes' : 'no')); |
|
| 225 | 225 | |
| 226 | 226 | // Check whether the post has an entity type with that URI. |
| 227 | 227 | return $has_entity_type; |
@@ -236,9 +236,9 @@ discard block |
||
| 236 | 236 | * |
| 237 | 237 | * @return array|WP_Error An array of entity types' terms or {@link WP_Error}. |
| 238 | 238 | */ |
| 239 | - private function get_post_terms( $post_id ) { |
|
| 239 | + private function get_post_terms($post_id) { |
|
| 240 | 240 | |
| 241 | - return wp_get_post_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 241 | + return wp_get_post_terms($post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 242 | 242 | 'hide_empty' => false, |
| 243 | 243 | // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
| 244 | 244 | // An issue has been opened with the AAM plugin author as well. |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | // see https://github.com/insideout10/wordlift-plugin/issues/334 |
| 247 | 247 | // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
| 248 | 248 | 'fields' => 'all', |
| 249 | - ) ); |
|
| 249 | + )); |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | /** |
@@ -266,24 +266,24 @@ discard block |
||
| 266 | 266 | * @type array custom_fields An array of custom fields. |
| 267 | 267 | * } |
| 268 | 268 | */ |
| 269 | - private function get_term_by_uri( $post_id, $uri ) { |
|
| 269 | + private function get_term_by_uri($post_id, $uri) { |
|
| 270 | 270 | |
| 271 | 271 | // Get the post terms bound to the specified post. |
| 272 | - $terms = $this->get_post_terms( $post_id ); |
|
| 272 | + $terms = $this->get_post_terms($post_id); |
|
| 273 | 273 | |
| 274 | 274 | // Look for a term if the specified URI. |
| 275 | - foreach ( $terms as $term ) { |
|
| 275 | + foreach ($terms as $term) { |
|
| 276 | 276 | // Get the schema by slug. |
| 277 | - $schema = $this->schema_service->get_schema( $term->slug ); |
|
| 277 | + $schema = $this->schema_service->get_schema($term->slug); |
|
| 278 | 278 | |
| 279 | 279 | // Continue to the next one, if a schema isn't found (or hasn't got |
| 280 | 280 | // and URI. |
| 281 | - if ( null === $schema || ! isset( $schema['uri'] ) ) { |
|
| 281 | + if (null === $schema || ! isset($schema['uri'])) { |
|
| 282 | 282 | continue; |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | // Return the schema if the URI matches. |
| 286 | - if ( $uri === $schema['uri'] ) { |
|
| 286 | + if ($uri === $schema['uri']) { |
|
| 287 | 287 | return $schema; |
| 288 | 288 | } |
| 289 | 289 | } |
@@ -305,9 +305,9 @@ discard block |
||
| 305 | 305 | * |
| 306 | 306 | * @return bool Return true if the post type can be used for entities, otherwise false. |
| 307 | 307 | */ |
| 308 | - public static function is_valid_entity_post_type( $post_type ) { |
|
| 308 | + public static function is_valid_entity_post_type($post_type) { |
|
| 309 | 309 | |
| 310 | - return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ); |
|
| 310 | + return in_array($post_type, Wordlift_Entity_Service::valid_entity_post_types(), true); |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | } |
@@ -7,636 +7,636 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Entity_Service { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * The Log service. |
|
| 12 | - * |
|
| 13 | - * @since 3.2.0 |
|
| 14 | - * @access private |
|
| 15 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | - */ |
|
| 17 | - private $log; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The UI service. |
|
| 21 | - * |
|
| 22 | - * @since 3.2.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | - */ |
|
| 26 | - private $ui_service; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.15.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $relation_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * An array of URIs to post ID valid for the current request. |
|
| 39 | - * |
|
| 40 | - * @since 3.16.1 |
|
| 41 | - * @var array $uri_to_post An array of URIs to post ID valid for the current request. |
|
| 42 | - */ |
|
| 43 | - private $uri_to_post; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The entity post type name. |
|
| 47 | - * |
|
| 48 | - * @since 3.1.0 |
|
| 49 | - */ |
|
| 50 | - const TYPE_NAME = 'entity'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The alternative label meta key. |
|
| 54 | - * |
|
| 55 | - * @since 3.2.0 |
|
| 56 | - */ |
|
| 57 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The alternative label input template. |
|
| 61 | - * |
|
| 62 | - * @since 3.2.0 |
|
| 63 | - */ |
|
| 64 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 65 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 10 | + /** |
|
| 11 | + * The Log service. |
|
| 12 | + * |
|
| 13 | + * @since 3.2.0 |
|
| 14 | + * @access private |
|
| 15 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | + */ |
|
| 17 | + private $log; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The UI service. |
|
| 21 | + * |
|
| 22 | + * @since 3.2.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | + */ |
|
| 26 | + private $ui_service; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.15.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $relation_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * An array of URIs to post ID valid for the current request. |
|
| 39 | + * |
|
| 40 | + * @since 3.16.1 |
|
| 41 | + * @var array $uri_to_post An array of URIs to post ID valid for the current request. |
|
| 42 | + */ |
|
| 43 | + private $uri_to_post; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The entity post type name. |
|
| 47 | + * |
|
| 48 | + * @since 3.1.0 |
|
| 49 | + */ |
|
| 50 | + const TYPE_NAME = 'entity'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The alternative label meta key. |
|
| 54 | + * |
|
| 55 | + * @since 3.2.0 |
|
| 56 | + */ |
|
| 57 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The alternative label input template. |
|
| 61 | + * |
|
| 62 | + * @since 3.2.0 |
|
| 63 | + */ |
|
| 64 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 65 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 66 | 66 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 67 | 67 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 68 | 68 | <button class="button wl-delete-button">%s</button> |
| 69 | 69 | </div>'; |
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * A singleton instance of the Entity service. |
|
| 73 | - * |
|
| 74 | - * @since 3.2.0 |
|
| 75 | - * @access private |
|
| 76 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 77 | - */ |
|
| 78 | - private static $instance; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Create a Wordlift_Entity_Service instance. |
|
| 82 | - * |
|
| 83 | - * @since 3.2.0 |
|
| 84 | - * |
|
| 85 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 86 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 87 | - */ |
|
| 88 | - public function __construct( $ui_service, $relation_service ) { |
|
| 89 | - |
|
| 90 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 91 | - |
|
| 92 | - $this->ui_service = $ui_service; |
|
| 93 | - $this->relation_service = $relation_service; |
|
| 94 | - |
|
| 95 | - $this->reset_uris(); |
|
| 96 | - |
|
| 97 | - // Set the singleton instance. |
|
| 98 | - self::$instance = $this; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Get the singleton instance of the Entity service. |
|
| 103 | - * |
|
| 104 | - * @since 3.2.0 |
|
| 105 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 106 | - */ |
|
| 107 | - public static function get_instance() { |
|
| 108 | - |
|
| 109 | - return self::$instance; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Determines whether a post is an entity or not. Entity is in this context |
|
| 114 | - * something which is not an article. |
|
| 115 | - * |
|
| 116 | - * @since 3.1.0 |
|
| 117 | - * |
|
| 118 | - * @param int $post_id A post id. |
|
| 119 | - * |
|
| 120 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 121 | - */ |
|
| 122 | - public function is_entity( $post_id ) { |
|
| 123 | - |
|
| 124 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 125 | - |
|
| 126 | - if ( 0 === count( $terms ) ) { |
|
| 127 | - return false; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - // We don't consider an `article` to be an entity. |
|
| 131 | - if ( 'article' !== $terms[0]->slug ) { |
|
| 132 | - return true; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Get the proper classification scope for a given entity post |
|
| 140 | - * |
|
| 141 | - * @since 3.5.0 |
|
| 142 | - * |
|
| 143 | - * @param integer $post_id An entity post id. |
|
| 144 | - * |
|
| 145 | - * @param string $default The default classification scope, `what` if not |
|
| 146 | - * provided. |
|
| 147 | - * |
|
| 148 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 149 | - */ |
|
| 150 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 151 | - |
|
| 152 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 153 | - return $default; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - // Retrieve the entity type |
|
| 157 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 158 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 159 | - // Retrieve classification boxes configuration |
|
| 160 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 161 | - foreach ( $classification_boxes as $cb ) { |
|
| 162 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 163 | - return $cb['id']; |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return $default; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Check whether a {@link WP_Post} is used. |
|
| 172 | - * |
|
| 173 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 174 | - * |
|
| 175 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 176 | - */ |
|
| 177 | - public function is_used( $post_id ) { |
|
| 178 | - |
|
| 179 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 180 | - return null; |
|
| 181 | - } |
|
| 182 | - // Retrieve the post |
|
| 183 | - $entity = get_post( $post_id ); |
|
| 184 | - |
|
| 185 | - global $wpdb; |
|
| 186 | - // Retrieve Wordlift relation instances table name |
|
| 187 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 188 | - |
|
| 189 | - // Check is it's referenced / related to another post / entity |
|
| 190 | - $stmt = $wpdb->prepare( |
|
| 191 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 192 | - $entity->ID |
|
| 193 | - ); |
|
| 194 | - |
|
| 195 | - // Perform the query |
|
| 196 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 197 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 198 | - if ( 0 < $relation_instances ) { |
|
| 199 | - return true; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // Check if the entity uri is used as meta_value |
|
| 203 | - $stmt = $wpdb->prepare( |
|
| 204 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 205 | - $entity->ID, |
|
| 206 | - wl_get_entity_uri( $entity->ID ) |
|
| 207 | - ); |
|
| 208 | - // Perform the query |
|
| 209 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 210 | - |
|
| 211 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 212 | - if ( 0 < $meta_instances ) { |
|
| 213 | - return true; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - // If we are here, it means the current entity is not used at the moment |
|
| 217 | - return false; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Determines whether a given uri is an internal uri or not. |
|
| 222 | - * |
|
| 223 | - * @since 3.3.2 |
|
| 224 | - * |
|
| 225 | - * @param int $uri An uri. |
|
| 226 | - * |
|
| 227 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 228 | - */ |
|
| 229 | - public function is_internal_uri( $uri ) { |
|
| 230 | - |
|
| 231 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Preload the provided URIs in the local cache. |
|
| 236 | - * |
|
| 237 | - * This function will populate the local `$uri_to_post` array by running a |
|
| 238 | - * single query with all the URIs and returning the mappings in the array. |
|
| 239 | - * |
|
| 240 | - * @since 3.16.1 |
|
| 241 | - * |
|
| 242 | - * @param array $uris An array of URIs. |
|
| 243 | - */ |
|
| 244 | - public function preload_uris( $uris ) { |
|
| 245 | - |
|
| 246 | - $that = $this; |
|
| 247 | - $external_uris = array_filter( $uris, function ( $item ) use ( $that ) { |
|
| 248 | - return ! $that->is_internal_uri( $item ); |
|
| 249 | - } ); |
|
| 250 | - |
|
| 251 | - $query_args = array( |
|
| 252 | - // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
| 253 | - 'ignore_sticky_posts' => 1, |
|
| 254 | - 'post_status' => 'any', |
|
| 255 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 256 | - 'meta_query' => array( |
|
| 257 | - array( |
|
| 258 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
| 259 | - 'value' => $uris, |
|
| 260 | - 'compare' => 'IN', |
|
| 261 | - ), |
|
| 262 | - ), |
|
| 263 | - ); |
|
| 264 | - |
|
| 265 | - // Only if the current uri is not an internal uri, entity search is |
|
| 266 | - // performed also looking at sameAs values. |
|
| 267 | - // |
|
| 268 | - // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 269 | - if ( 0 < count( $external_uris ) ) { |
|
| 270 | - |
|
| 271 | - $query_args['meta_query']['relation'] = 'OR'; |
|
| 272 | - $query_args['meta_query'][] = array( |
|
| 273 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 274 | - 'value' => $external_uris, |
|
| 275 | - 'compare' => 'IN', |
|
| 276 | - ); |
|
| 277 | - |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - // Get the posts. |
|
| 281 | - $posts = get_posts( $query_args ); |
|
| 282 | - |
|
| 283 | - // Populate the array. We reinitialize the array on purpose because |
|
| 284 | - // we don't want these data to long live. |
|
| 285 | - $this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) use ( $that ) { |
|
| 286 | - return $carry |
|
| 287 | - // Get the URI related to the post and fill them with the item id. |
|
| 288 | - + array_fill_keys( $that->get_uris( $item->ID ), $item ); |
|
| 289 | - }, array() ); |
|
| 290 | - |
|
| 291 | - // Add the not found URIs. |
|
| 292 | - $this->uri_to_post += array_fill_keys( $uris, null ); |
|
| 293 | - |
|
| 294 | - $this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." ); |
|
| 295 | - |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Reset the URI to post local cache. |
|
| 300 | - * |
|
| 301 | - * @since 3.16.1 |
|
| 302 | - */ |
|
| 303 | - public function reset_uris() { |
|
| 304 | - |
|
| 305 | - $this->uri_to_post = array(); |
|
| 306 | - |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Get all the URIs (item id and same as) related to a post. |
|
| 311 | - * |
|
| 312 | - * @since 3.16.1 |
|
| 313 | - * |
|
| 314 | - * @param int $post_id The {@link WP_Post) id. |
|
| 315 | - * |
|
| 316 | - * @return array An array of URIs. |
|
| 317 | - */ |
|
| 318 | - public function get_uris( $post_id ) { |
|
| 319 | - |
|
| 320 | - return get_post_meta( $post_id, WL_ENTITY_URL_META_NAME ) |
|
| 321 | - + get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 327 | - * |
|
| 328 | - * @since 3.2.0 |
|
| 329 | - * |
|
| 330 | - * @param string $uri The entity URI. |
|
| 331 | - * |
|
| 332 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 333 | - */ |
|
| 334 | - public function get_entity_post_by_uri( $uri ) { |
|
| 335 | - |
|
| 336 | - $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
| 337 | - |
|
| 338 | - // Check if we've been provided with a value otherwise return null. |
|
| 339 | - if ( empty( $uri ) ) { |
|
| 340 | - return null; |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - // If the URI is cached, return the cached post. |
|
| 344 | - if ( array_key_exists( $uri, $this->uri_to_post ) ) { |
|
| 345 | - $this->log->debug( "Returning cached post for $uri..." ); |
|
| 346 | - |
|
| 347 | - return $this->uri_to_post[ $uri ]; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - $this->log->debug( "Querying post for $uri..." ); |
|
| 351 | - |
|
| 352 | - $query_args = array( |
|
| 353 | - // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
| 354 | - 'ignore_sticky_posts' => 1, |
|
| 355 | - 'posts_per_page' => 1, |
|
| 356 | - 'post_status' => 'any', |
|
| 357 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 358 | - 'meta_query' => array( |
|
| 359 | - array( |
|
| 360 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
| 361 | - 'value' => $uri, |
|
| 362 | - 'compare' => '=', |
|
| 363 | - ), |
|
| 364 | - ), |
|
| 365 | - // The following query can be avoided, it's superfluous since |
|
| 366 | - // we're looking for a post with a specific Entity URI. This query |
|
| 367 | - // may in fact consume up to 50% of the timing necessary to load a |
|
| 368 | - // page. |
|
| 369 | - // |
|
| 370 | - // See https://github.com/insideout10/wordlift-plugin/issues/674. |
|
| 371 | - // 'tax_query' => array( |
|
| 372 | - // 'relation' => 'AND', |
|
| 373 | - // array( |
|
| 374 | - // 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 375 | - // 'operator' => 'EXISTS', |
|
| 376 | - // ), |
|
| 377 | - // array( |
|
| 378 | - // 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 379 | - // 'field' => 'slug', |
|
| 380 | - // 'terms' => 'article', |
|
| 381 | - // 'operator' => 'NOT IN', |
|
| 382 | - // ), |
|
| 383 | - // ), |
|
| 384 | - ); |
|
| 385 | - |
|
| 386 | - // Only if the current uri is not an internal uri, entity search is |
|
| 387 | - // performed also looking at sameAs values. |
|
| 388 | - // |
|
| 389 | - // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 390 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 391 | - |
|
| 392 | - $query_args['meta_query']['relation'] = 'OR'; |
|
| 393 | - $query_args['meta_query'][] = array( |
|
| 394 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 395 | - 'value' => $uri, |
|
| 396 | - 'compare' => '=', |
|
| 397 | - ); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - $posts = get_posts( $query_args ); |
|
| 401 | - |
|
| 402 | - // Return null if no post is found. |
|
| 403 | - if ( 0 === count( $posts ) ) { |
|
| 404 | - $this->log->warn( "No post for URI $uri." ); |
|
| 405 | - |
|
| 406 | - return null; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - // Return the found post. |
|
| 410 | - return current( $posts ); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 415 | - * we check that the post we're saving is the current post. |
|
| 416 | - * |
|
| 417 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 418 | - * |
|
| 419 | - * @since 3.2.0 |
|
| 420 | - * |
|
| 421 | - * @param int $post_id Post ID. |
|
| 422 | - * @param WP_Post $post Post object. |
|
| 423 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 424 | - */ |
|
| 425 | - public function save_post( $post_id, $post, $update ) { |
|
| 426 | - |
|
| 427 | - // Avoid doing anything if post is autosave or a revision. |
|
| 428 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 429 | - return; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - // We're setting the alternative label that have been provided via the UI |
|
| 433 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 434 | - // programmatically by some other function: we need to check therefore if |
|
| 435 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 436 | - // |
|
| 437 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 438 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 439 | - return; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - // Get the alt labels from the request (or empty array). |
|
| 443 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 444 | - |
|
| 445 | - // Set the alternative labels. |
|
| 446 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 447 | - |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * Set the alternative labels. |
|
| 452 | - * |
|
| 453 | - * @since 3.2.0 |
|
| 454 | - * |
|
| 455 | - * @param int $post_id The post id. |
|
| 456 | - * @param array $alt_labels An array of labels. |
|
| 457 | - */ |
|
| 458 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 459 | - |
|
| 460 | - // Force $alt_labels to be an array |
|
| 461 | - if ( ! is_array( $alt_labels ) ) { |
|
| 462 | - $alt_labels = array( $alt_labels ); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 466 | - |
|
| 467 | - // Delete all the existing alternate labels. |
|
| 468 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 469 | - |
|
| 470 | - // Set the alternative labels. |
|
| 471 | - foreach ( $alt_labels as $alt_label ) { |
|
| 472 | - if ( ! empty( $alt_label ) ) { |
|
| 473 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Retrieve the alternate labels. |
|
| 481 | - * |
|
| 482 | - * @since 3.2.0 |
|
| 483 | - * |
|
| 484 | - * @param int $post_id Post id. |
|
| 485 | - * |
|
| 486 | - * @return mixed An array of alternative labels. |
|
| 487 | - */ |
|
| 488 | - public function get_alternative_labels( $post_id ) { |
|
| 489 | - |
|
| 490 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 495 | - * |
|
| 496 | - * @since 3.12.0 |
|
| 497 | - * |
|
| 498 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 499 | - * |
|
| 500 | - * @return array An array with the entity title and labels. |
|
| 501 | - */ |
|
| 502 | - public function get_labels( $post_id ) { |
|
| 503 | - |
|
| 504 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 509 | - * |
|
| 510 | - * @since 3.2.0 |
|
| 511 | - * |
|
| 512 | - * @param WP_Post $post Post object. |
|
| 513 | - */ |
|
| 514 | - public function edit_form_before_permalink( $post ) { |
|
| 515 | - |
|
| 516 | - // If it's not an entity, return. |
|
| 517 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 518 | - return; |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - // Print the input template. |
|
| 522 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 523 | - |
|
| 524 | - // Print all the currently set alternative labels. |
|
| 525 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 526 | - |
|
| 527 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 528 | - |
|
| 529 | - }; |
|
| 530 | - |
|
| 531 | - // Print the button. |
|
| 532 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 533 | - |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - /** |
|
| 537 | - * Get the URI for the entity with the specified post id. |
|
| 538 | - * |
|
| 539 | - * @since 3.6.0 |
|
| 540 | - * |
|
| 541 | - * @param int $post_id The entity post id. |
|
| 542 | - * |
|
| 543 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 544 | - */ |
|
| 545 | - public function get_uri( $post_id ) { |
|
| 546 | - |
|
| 547 | - // If a null is given, nothing to do |
|
| 548 | - if ( null == $post_id ) { |
|
| 549 | - return null; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 553 | - |
|
| 554 | - // If the dataset uri is not properly configured, null is returned |
|
| 555 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 556 | - return null; |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - // Set the URI if it isn't set yet. |
|
| 560 | - $post_status = get_post_status( $post_id ); |
|
| 561 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 562 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 563 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - return $uri; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * Get the alternative label input HTML code. |
|
| 572 | - * |
|
| 573 | - * @since 3.2.0 |
|
| 574 | - * |
|
| 575 | - * @param string $value The input value. |
|
| 576 | - * |
|
| 577 | - * @return string The input HTML code. |
|
| 578 | - */ |
|
| 579 | - private function get_alternative_label_input( $value = '' ) { |
|
| 580 | - |
|
| 581 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - /** |
|
| 585 | - * Get the number of entity posts published in this blog. |
|
| 586 | - * |
|
| 587 | - * @since 3.6.0 |
|
| 588 | - * |
|
| 589 | - * @return int The number of published entity posts. |
|
| 590 | - */ |
|
| 591 | - public function count() { |
|
| 592 | - |
|
| 593 | - $posts = get_posts( $this->add_criterias( array( |
|
| 594 | - 'post_status' => 'any', |
|
| 595 | - 'numberposts' => - 1, |
|
| 596 | - ) ) ); |
|
| 597 | - |
|
| 598 | - return count( $posts ); |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 603 | - * call. |
|
| 604 | - * |
|
| 605 | - * @since 3.15.0 |
|
| 606 | - * |
|
| 607 | - * @param array $args The arguments for a `get_posts` call. |
|
| 608 | - * |
|
| 609 | - * @return array The arguments for a `get_posts` call. |
|
| 610 | - */ |
|
| 611 | - public static function add_criterias( $args ) { |
|
| 612 | - |
|
| 613 | - // Build an optimal tax-query. |
|
| 614 | - $tax_query = array( |
|
| 615 | - 'relation' => 'AND', |
|
| 616 | - array( |
|
| 617 | - 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 618 | - 'operator' => 'EXISTS', |
|
| 619 | - ), |
|
| 620 | - array( |
|
| 621 | - 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 622 | - 'field' => 'slug', |
|
| 623 | - 'terms' => 'article', |
|
| 624 | - 'operator' => 'NOT IN', |
|
| 625 | - ), |
|
| 626 | - ); |
|
| 627 | - |
|
| 628 | - return $args + array( |
|
| 629 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 630 | - // Since 3.17.0: should this be faster? |
|
| 631 | - 'tax_query' => $tax_query, |
|
| 632 | - // 'tax_query' => array( |
|
| 633 | - // array( |
|
| 634 | - // 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 635 | - // 'terms' => self::get_entity_terms(), |
|
| 636 | - // ), |
|
| 637 | - // ), |
|
| 638 | - ); |
|
| 639 | - } |
|
| 71 | + /** |
|
| 72 | + * A singleton instance of the Entity service. |
|
| 73 | + * |
|
| 74 | + * @since 3.2.0 |
|
| 75 | + * @access private |
|
| 76 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 77 | + */ |
|
| 78 | + private static $instance; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Create a Wordlift_Entity_Service instance. |
|
| 82 | + * |
|
| 83 | + * @since 3.2.0 |
|
| 84 | + * |
|
| 85 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 86 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 87 | + */ |
|
| 88 | + public function __construct( $ui_service, $relation_service ) { |
|
| 89 | + |
|
| 90 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 91 | + |
|
| 92 | + $this->ui_service = $ui_service; |
|
| 93 | + $this->relation_service = $relation_service; |
|
| 94 | + |
|
| 95 | + $this->reset_uris(); |
|
| 96 | + |
|
| 97 | + // Set the singleton instance. |
|
| 98 | + self::$instance = $this; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Get the singleton instance of the Entity service. |
|
| 103 | + * |
|
| 104 | + * @since 3.2.0 |
|
| 105 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 106 | + */ |
|
| 107 | + public static function get_instance() { |
|
| 108 | + |
|
| 109 | + return self::$instance; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Determines whether a post is an entity or not. Entity is in this context |
|
| 114 | + * something which is not an article. |
|
| 115 | + * |
|
| 116 | + * @since 3.1.0 |
|
| 117 | + * |
|
| 118 | + * @param int $post_id A post id. |
|
| 119 | + * |
|
| 120 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 121 | + */ |
|
| 122 | + public function is_entity( $post_id ) { |
|
| 123 | + |
|
| 124 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 125 | + |
|
| 126 | + if ( 0 === count( $terms ) ) { |
|
| 127 | + return false; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + // We don't consider an `article` to be an entity. |
|
| 131 | + if ( 'article' !== $terms[0]->slug ) { |
|
| 132 | + return true; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Get the proper classification scope for a given entity post |
|
| 140 | + * |
|
| 141 | + * @since 3.5.0 |
|
| 142 | + * |
|
| 143 | + * @param integer $post_id An entity post id. |
|
| 144 | + * |
|
| 145 | + * @param string $default The default classification scope, `what` if not |
|
| 146 | + * provided. |
|
| 147 | + * |
|
| 148 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 149 | + */ |
|
| 150 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 151 | + |
|
| 152 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 153 | + return $default; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + // Retrieve the entity type |
|
| 157 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 158 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 159 | + // Retrieve classification boxes configuration |
|
| 160 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 161 | + foreach ( $classification_boxes as $cb ) { |
|
| 162 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 163 | + return $cb['id']; |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return $default; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Check whether a {@link WP_Post} is used. |
|
| 172 | + * |
|
| 173 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 174 | + * |
|
| 175 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 176 | + */ |
|
| 177 | + public function is_used( $post_id ) { |
|
| 178 | + |
|
| 179 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 180 | + return null; |
|
| 181 | + } |
|
| 182 | + // Retrieve the post |
|
| 183 | + $entity = get_post( $post_id ); |
|
| 184 | + |
|
| 185 | + global $wpdb; |
|
| 186 | + // Retrieve Wordlift relation instances table name |
|
| 187 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 188 | + |
|
| 189 | + // Check is it's referenced / related to another post / entity |
|
| 190 | + $stmt = $wpdb->prepare( |
|
| 191 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 192 | + $entity->ID |
|
| 193 | + ); |
|
| 194 | + |
|
| 195 | + // Perform the query |
|
| 196 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 197 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 198 | + if ( 0 < $relation_instances ) { |
|
| 199 | + return true; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + // Check if the entity uri is used as meta_value |
|
| 203 | + $stmt = $wpdb->prepare( |
|
| 204 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 205 | + $entity->ID, |
|
| 206 | + wl_get_entity_uri( $entity->ID ) |
|
| 207 | + ); |
|
| 208 | + // Perform the query |
|
| 209 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 210 | + |
|
| 211 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 212 | + if ( 0 < $meta_instances ) { |
|
| 213 | + return true; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + // If we are here, it means the current entity is not used at the moment |
|
| 217 | + return false; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Determines whether a given uri is an internal uri or not. |
|
| 222 | + * |
|
| 223 | + * @since 3.3.2 |
|
| 224 | + * |
|
| 225 | + * @param int $uri An uri. |
|
| 226 | + * |
|
| 227 | + * @return true if the uri internal to the current dataset otherwise false. |
|
| 228 | + */ |
|
| 229 | + public function is_internal_uri( $uri ) { |
|
| 230 | + |
|
| 231 | + return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Preload the provided URIs in the local cache. |
|
| 236 | + * |
|
| 237 | + * This function will populate the local `$uri_to_post` array by running a |
|
| 238 | + * single query with all the URIs and returning the mappings in the array. |
|
| 239 | + * |
|
| 240 | + * @since 3.16.1 |
|
| 241 | + * |
|
| 242 | + * @param array $uris An array of URIs. |
|
| 243 | + */ |
|
| 244 | + public function preload_uris( $uris ) { |
|
| 245 | + |
|
| 246 | + $that = $this; |
|
| 247 | + $external_uris = array_filter( $uris, function ( $item ) use ( $that ) { |
|
| 248 | + return ! $that->is_internal_uri( $item ); |
|
| 249 | + } ); |
|
| 250 | + |
|
| 251 | + $query_args = array( |
|
| 252 | + // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
| 253 | + 'ignore_sticky_posts' => 1, |
|
| 254 | + 'post_status' => 'any', |
|
| 255 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 256 | + 'meta_query' => array( |
|
| 257 | + array( |
|
| 258 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
| 259 | + 'value' => $uris, |
|
| 260 | + 'compare' => 'IN', |
|
| 261 | + ), |
|
| 262 | + ), |
|
| 263 | + ); |
|
| 264 | + |
|
| 265 | + // Only if the current uri is not an internal uri, entity search is |
|
| 266 | + // performed also looking at sameAs values. |
|
| 267 | + // |
|
| 268 | + // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 269 | + if ( 0 < count( $external_uris ) ) { |
|
| 270 | + |
|
| 271 | + $query_args['meta_query']['relation'] = 'OR'; |
|
| 272 | + $query_args['meta_query'][] = array( |
|
| 273 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 274 | + 'value' => $external_uris, |
|
| 275 | + 'compare' => 'IN', |
|
| 276 | + ); |
|
| 277 | + |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + // Get the posts. |
|
| 281 | + $posts = get_posts( $query_args ); |
|
| 282 | + |
|
| 283 | + // Populate the array. We reinitialize the array on purpose because |
|
| 284 | + // we don't want these data to long live. |
|
| 285 | + $this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) use ( $that ) { |
|
| 286 | + return $carry |
|
| 287 | + // Get the URI related to the post and fill them with the item id. |
|
| 288 | + + array_fill_keys( $that->get_uris( $item->ID ), $item ); |
|
| 289 | + }, array() ); |
|
| 290 | + |
|
| 291 | + // Add the not found URIs. |
|
| 292 | + $this->uri_to_post += array_fill_keys( $uris, null ); |
|
| 293 | + |
|
| 294 | + $this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." ); |
|
| 295 | + |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Reset the URI to post local cache. |
|
| 300 | + * |
|
| 301 | + * @since 3.16.1 |
|
| 302 | + */ |
|
| 303 | + public function reset_uris() { |
|
| 304 | + |
|
| 305 | + $this->uri_to_post = array(); |
|
| 306 | + |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Get all the URIs (item id and same as) related to a post. |
|
| 311 | + * |
|
| 312 | + * @since 3.16.1 |
|
| 313 | + * |
|
| 314 | + * @param int $post_id The {@link WP_Post) id. |
|
| 315 | + * |
|
| 316 | + * @return array An array of URIs. |
|
| 317 | + */ |
|
| 318 | + public function get_uris( $post_id ) { |
|
| 319 | + |
|
| 320 | + return get_post_meta( $post_id, WL_ENTITY_URL_META_NAME ) |
|
| 321 | + + get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 327 | + * |
|
| 328 | + * @since 3.2.0 |
|
| 329 | + * |
|
| 330 | + * @param string $uri The entity URI. |
|
| 331 | + * |
|
| 332 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 333 | + */ |
|
| 334 | + public function get_entity_post_by_uri( $uri ) { |
|
| 335 | + |
|
| 336 | + $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
| 337 | + |
|
| 338 | + // Check if we've been provided with a value otherwise return null. |
|
| 339 | + if ( empty( $uri ) ) { |
|
| 340 | + return null; |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + // If the URI is cached, return the cached post. |
|
| 344 | + if ( array_key_exists( $uri, $this->uri_to_post ) ) { |
|
| 345 | + $this->log->debug( "Returning cached post for $uri..." ); |
|
| 346 | + |
|
| 347 | + return $this->uri_to_post[ $uri ]; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + $this->log->debug( "Querying post for $uri..." ); |
|
| 351 | + |
|
| 352 | + $query_args = array( |
|
| 353 | + // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
| 354 | + 'ignore_sticky_posts' => 1, |
|
| 355 | + 'posts_per_page' => 1, |
|
| 356 | + 'post_status' => 'any', |
|
| 357 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 358 | + 'meta_query' => array( |
|
| 359 | + array( |
|
| 360 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
| 361 | + 'value' => $uri, |
|
| 362 | + 'compare' => '=', |
|
| 363 | + ), |
|
| 364 | + ), |
|
| 365 | + // The following query can be avoided, it's superfluous since |
|
| 366 | + // we're looking for a post with a specific Entity URI. This query |
|
| 367 | + // may in fact consume up to 50% of the timing necessary to load a |
|
| 368 | + // page. |
|
| 369 | + // |
|
| 370 | + // See https://github.com/insideout10/wordlift-plugin/issues/674. |
|
| 371 | + // 'tax_query' => array( |
|
| 372 | + // 'relation' => 'AND', |
|
| 373 | + // array( |
|
| 374 | + // 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 375 | + // 'operator' => 'EXISTS', |
|
| 376 | + // ), |
|
| 377 | + // array( |
|
| 378 | + // 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 379 | + // 'field' => 'slug', |
|
| 380 | + // 'terms' => 'article', |
|
| 381 | + // 'operator' => 'NOT IN', |
|
| 382 | + // ), |
|
| 383 | + // ), |
|
| 384 | + ); |
|
| 385 | + |
|
| 386 | + // Only if the current uri is not an internal uri, entity search is |
|
| 387 | + // performed also looking at sameAs values. |
|
| 388 | + // |
|
| 389 | + // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 390 | + if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 391 | + |
|
| 392 | + $query_args['meta_query']['relation'] = 'OR'; |
|
| 393 | + $query_args['meta_query'][] = array( |
|
| 394 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 395 | + 'value' => $uri, |
|
| 396 | + 'compare' => '=', |
|
| 397 | + ); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + $posts = get_posts( $query_args ); |
|
| 401 | + |
|
| 402 | + // Return null if no post is found. |
|
| 403 | + if ( 0 === count( $posts ) ) { |
|
| 404 | + $this->log->warn( "No post for URI $uri." ); |
|
| 405 | + |
|
| 406 | + return null; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + // Return the found post. |
|
| 410 | + return current( $posts ); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 415 | + * we check that the post we're saving is the current post. |
|
| 416 | + * |
|
| 417 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 418 | + * |
|
| 419 | + * @since 3.2.0 |
|
| 420 | + * |
|
| 421 | + * @param int $post_id Post ID. |
|
| 422 | + * @param WP_Post $post Post object. |
|
| 423 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 424 | + */ |
|
| 425 | + public function save_post( $post_id, $post, $update ) { |
|
| 426 | + |
|
| 427 | + // Avoid doing anything if post is autosave or a revision. |
|
| 428 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 429 | + return; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + // We're setting the alternative label that have been provided via the UI |
|
| 433 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 434 | + // programmatically by some other function: we need to check therefore if |
|
| 435 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 436 | + // |
|
| 437 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 438 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 439 | + return; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + // Get the alt labels from the request (or empty array). |
|
| 443 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 444 | + |
|
| 445 | + // Set the alternative labels. |
|
| 446 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 447 | + |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * Set the alternative labels. |
|
| 452 | + * |
|
| 453 | + * @since 3.2.0 |
|
| 454 | + * |
|
| 455 | + * @param int $post_id The post id. |
|
| 456 | + * @param array $alt_labels An array of labels. |
|
| 457 | + */ |
|
| 458 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 459 | + |
|
| 460 | + // Force $alt_labels to be an array |
|
| 461 | + if ( ! is_array( $alt_labels ) ) { |
|
| 462 | + $alt_labels = array( $alt_labels ); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 466 | + |
|
| 467 | + // Delete all the existing alternate labels. |
|
| 468 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 469 | + |
|
| 470 | + // Set the alternative labels. |
|
| 471 | + foreach ( $alt_labels as $alt_label ) { |
|
| 472 | + if ( ! empty( $alt_label ) ) { |
|
| 473 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Retrieve the alternate labels. |
|
| 481 | + * |
|
| 482 | + * @since 3.2.0 |
|
| 483 | + * |
|
| 484 | + * @param int $post_id Post id. |
|
| 485 | + * |
|
| 486 | + * @return mixed An array of alternative labels. |
|
| 487 | + */ |
|
| 488 | + public function get_alternative_labels( $post_id ) { |
|
| 489 | + |
|
| 490 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 495 | + * |
|
| 496 | + * @since 3.12.0 |
|
| 497 | + * |
|
| 498 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 499 | + * |
|
| 500 | + * @return array An array with the entity title and labels. |
|
| 501 | + */ |
|
| 502 | + public function get_labels( $post_id ) { |
|
| 503 | + |
|
| 504 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 509 | + * |
|
| 510 | + * @since 3.2.0 |
|
| 511 | + * |
|
| 512 | + * @param WP_Post $post Post object. |
|
| 513 | + */ |
|
| 514 | + public function edit_form_before_permalink( $post ) { |
|
| 515 | + |
|
| 516 | + // If it's not an entity, return. |
|
| 517 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 518 | + return; |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + // Print the input template. |
|
| 522 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 523 | + |
|
| 524 | + // Print all the currently set alternative labels. |
|
| 525 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 526 | + |
|
| 527 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 528 | + |
|
| 529 | + }; |
|
| 530 | + |
|
| 531 | + // Print the button. |
|
| 532 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 533 | + |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * Get the URI for the entity with the specified post id. |
|
| 538 | + * |
|
| 539 | + * @since 3.6.0 |
|
| 540 | + * |
|
| 541 | + * @param int $post_id The entity post id. |
|
| 542 | + * |
|
| 543 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 544 | + */ |
|
| 545 | + public function get_uri( $post_id ) { |
|
| 546 | + |
|
| 547 | + // If a null is given, nothing to do |
|
| 548 | + if ( null == $post_id ) { |
|
| 549 | + return null; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 553 | + |
|
| 554 | + // If the dataset uri is not properly configured, null is returned |
|
| 555 | + if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 556 | + return null; |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + // Set the URI if it isn't set yet. |
|
| 560 | + $post_status = get_post_status( $post_id ); |
|
| 561 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 562 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 563 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + return $uri; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * Get the alternative label input HTML code. |
|
| 572 | + * |
|
| 573 | + * @since 3.2.0 |
|
| 574 | + * |
|
| 575 | + * @param string $value The input value. |
|
| 576 | + * |
|
| 577 | + * @return string The input HTML code. |
|
| 578 | + */ |
|
| 579 | + private function get_alternative_label_input( $value = '' ) { |
|
| 580 | + |
|
| 581 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + /** |
|
| 585 | + * Get the number of entity posts published in this blog. |
|
| 586 | + * |
|
| 587 | + * @since 3.6.0 |
|
| 588 | + * |
|
| 589 | + * @return int The number of published entity posts. |
|
| 590 | + */ |
|
| 591 | + public function count() { |
|
| 592 | + |
|
| 593 | + $posts = get_posts( $this->add_criterias( array( |
|
| 594 | + 'post_status' => 'any', |
|
| 595 | + 'numberposts' => - 1, |
|
| 596 | + ) ) ); |
|
| 597 | + |
|
| 598 | + return count( $posts ); |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 603 | + * call. |
|
| 604 | + * |
|
| 605 | + * @since 3.15.0 |
|
| 606 | + * |
|
| 607 | + * @param array $args The arguments for a `get_posts` call. |
|
| 608 | + * |
|
| 609 | + * @return array The arguments for a `get_posts` call. |
|
| 610 | + */ |
|
| 611 | + public static function add_criterias( $args ) { |
|
| 612 | + |
|
| 613 | + // Build an optimal tax-query. |
|
| 614 | + $tax_query = array( |
|
| 615 | + 'relation' => 'AND', |
|
| 616 | + array( |
|
| 617 | + 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 618 | + 'operator' => 'EXISTS', |
|
| 619 | + ), |
|
| 620 | + array( |
|
| 621 | + 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 622 | + 'field' => 'slug', |
|
| 623 | + 'terms' => 'article', |
|
| 624 | + 'operator' => 'NOT IN', |
|
| 625 | + ), |
|
| 626 | + ); |
|
| 627 | + |
|
| 628 | + return $args + array( |
|
| 629 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 630 | + // Since 3.17.0: should this be faster? |
|
| 631 | + 'tax_query' => $tax_query, |
|
| 632 | + // 'tax_query' => array( |
|
| 633 | + // array( |
|
| 634 | + // 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 635 | + // 'terms' => self::get_entity_terms(), |
|
| 636 | + // ), |
|
| 637 | + // ), |
|
| 638 | + ); |
|
| 639 | + } |
|
| 640 | 640 | |
| 641 | 641 | // /** |
| 642 | 642 | // * Get the entity terms IDs which represent an entity. |
@@ -666,97 +666,97 @@ discard block |
||
| 666 | 666 | // } ) ); |
| 667 | 667 | // } |
| 668 | 668 | |
| 669 | - /** |
|
| 670 | - * Create a new entity. |
|
| 671 | - * |
|
| 672 | - * @since 3.9.0 |
|
| 673 | - * |
|
| 674 | - * @param string $name The entity name. |
|
| 675 | - * @param string $type_uri The entity's type URI. |
|
| 676 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 677 | - * @param string $status The post status, by default 'publish'. |
|
| 678 | - * |
|
| 679 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 680 | - */ |
|
| 681 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 682 | - |
|
| 683 | - // Create an entity for the publisher. |
|
| 684 | - $post_id = wp_insert_post( array( |
|
| 685 | - 'post_type' => self::TYPE_NAME, |
|
| 686 | - 'post_title' => $name, |
|
| 687 | - 'post_status' => $status, |
|
| 688 | - 'post_content' => '', |
|
| 689 | - ) ); |
|
| 690 | - |
|
| 691 | - // Return the error if any. |
|
| 692 | - if ( is_wp_error( $post_id ) ) { |
|
| 693 | - return $post_id; |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - // Set the entity logo. |
|
| 697 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 698 | - set_post_thumbnail( $post_id, $logo ); |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - // Set the entity type. |
|
| 702 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 703 | - |
|
| 704 | - return $post_id; |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - /** |
|
| 708 | - * Get the entities related to the one with the specified id. By default only |
|
| 709 | - * published entities will be returned. |
|
| 710 | - * |
|
| 711 | - * @since 3.10.0 |
|
| 712 | - * |
|
| 713 | - * @param int $id The post id. |
|
| 714 | - * @param string $post_status The target post status (default = publish). |
|
| 715 | - * |
|
| 716 | - * @return array An array of post ids. |
|
| 717 | - */ |
|
| 718 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 719 | - |
|
| 720 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - /** |
|
| 724 | - * Get the list of entities. |
|
| 725 | - * |
|
| 726 | - * @since 3.12.2 |
|
| 727 | - * |
|
| 728 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 729 | - * |
|
| 730 | - * @return array An array of entity posts. |
|
| 731 | - */ |
|
| 732 | - public function get( $params = array() ) { |
|
| 733 | - |
|
| 734 | - // Set the defaults. |
|
| 735 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 736 | - |
|
| 737 | - // Merge the defaults with the provided parameters. |
|
| 738 | - $args = wp_parse_args( $params, $defaults ); |
|
| 739 | - |
|
| 740 | - // Call the `get_posts` function. |
|
| 741 | - return get_posts( $args ); |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - /** |
|
| 745 | - * The list of post type names which can be used for entities |
|
| 746 | - * |
|
| 747 | - * Criteria is that the post type is public. The list of valid post types |
|
| 748 | - * can be overridden with a filter. |
|
| 749 | - * |
|
| 750 | - * @since 3.15.0 |
|
| 751 | - * |
|
| 752 | - * @return array Array containing the names of the valid post types. |
|
| 753 | - */ |
|
| 754 | - static function valid_entity_post_types() { |
|
| 755 | - |
|
| 756 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 757 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 758 | - |
|
| 759 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 760 | - } |
|
| 669 | + /** |
|
| 670 | + * Create a new entity. |
|
| 671 | + * |
|
| 672 | + * @since 3.9.0 |
|
| 673 | + * |
|
| 674 | + * @param string $name The entity name. |
|
| 675 | + * @param string $type_uri The entity's type URI. |
|
| 676 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 677 | + * @param string $status The post status, by default 'publish'. |
|
| 678 | + * |
|
| 679 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 680 | + */ |
|
| 681 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 682 | + |
|
| 683 | + // Create an entity for the publisher. |
|
| 684 | + $post_id = wp_insert_post( array( |
|
| 685 | + 'post_type' => self::TYPE_NAME, |
|
| 686 | + 'post_title' => $name, |
|
| 687 | + 'post_status' => $status, |
|
| 688 | + 'post_content' => '', |
|
| 689 | + ) ); |
|
| 690 | + |
|
| 691 | + // Return the error if any. |
|
| 692 | + if ( is_wp_error( $post_id ) ) { |
|
| 693 | + return $post_id; |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + // Set the entity logo. |
|
| 697 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 698 | + set_post_thumbnail( $post_id, $logo ); |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + // Set the entity type. |
|
| 702 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 703 | + |
|
| 704 | + return $post_id; |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + /** |
|
| 708 | + * Get the entities related to the one with the specified id. By default only |
|
| 709 | + * published entities will be returned. |
|
| 710 | + * |
|
| 711 | + * @since 3.10.0 |
|
| 712 | + * |
|
| 713 | + * @param int $id The post id. |
|
| 714 | + * @param string $post_status The target post status (default = publish). |
|
| 715 | + * |
|
| 716 | + * @return array An array of post ids. |
|
| 717 | + */ |
|
| 718 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 719 | + |
|
| 720 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + /** |
|
| 724 | + * Get the list of entities. |
|
| 725 | + * |
|
| 726 | + * @since 3.12.2 |
|
| 727 | + * |
|
| 728 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 729 | + * |
|
| 730 | + * @return array An array of entity posts. |
|
| 731 | + */ |
|
| 732 | + public function get( $params = array() ) { |
|
| 733 | + |
|
| 734 | + // Set the defaults. |
|
| 735 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 736 | + |
|
| 737 | + // Merge the defaults with the provided parameters. |
|
| 738 | + $args = wp_parse_args( $params, $defaults ); |
|
| 739 | + |
|
| 740 | + // Call the `get_posts` function. |
|
| 741 | + return get_posts( $args ); |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + /** |
|
| 745 | + * The list of post type names which can be used for entities |
|
| 746 | + * |
|
| 747 | + * Criteria is that the post type is public. The list of valid post types |
|
| 748 | + * can be overridden with a filter. |
|
| 749 | + * |
|
| 750 | + * @since 3.15.0 |
|
| 751 | + * |
|
| 752 | + * @return array Array containing the names of the valid post types. |
|
| 753 | + */ |
|
| 754 | + static function valid_entity_post_types() { |
|
| 755 | + |
|
| 756 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 757 | + $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 758 | + |
|
| 759 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 760 | + } |
|
| 761 | 761 | |
| 762 | 762 | } |
@@ -85,9 +85,9 @@ discard block |
||
| 85 | 85 | * @param \Wordlift_UI_Service $ui_service The UI service. |
| 86 | 86 | * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
| 87 | 87 | */ |
| 88 | - public function __construct( $ui_service, $relation_service ) { |
|
| 88 | + public function __construct($ui_service, $relation_service) { |
|
| 89 | 89 | |
| 90 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 90 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 91 | 91 | |
| 92 | 92 | $this->ui_service = $ui_service; |
| 93 | 93 | $this->relation_service = $relation_service; |
@@ -119,16 +119,16 @@ discard block |
||
| 119 | 119 | * |
| 120 | 120 | * @return bool Return true if the post is an entity otherwise false. |
| 121 | 121 | */ |
| 122 | - public function is_entity( $post_id ) { |
|
| 122 | + public function is_entity($post_id) { |
|
| 123 | 123 | |
| 124 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 124 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); |
|
| 125 | 125 | |
| 126 | - if ( 0 === count( $terms ) ) { |
|
| 126 | + if (0 === count($terms)) { |
|
| 127 | 127 | return false; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | // We don't consider an `article` to be an entity. |
| 131 | - if ( 'article' !== $terms[0]->slug ) { |
|
| 131 | + if ('article' !== $terms[0]->slug) { |
|
| 132 | 132 | return true; |
| 133 | 133 | } |
| 134 | 134 | |
@@ -147,19 +147,19 @@ discard block |
||
| 147 | 147 | * |
| 148 | 148 | * @return string Returns a classification scope (e.g. 'what'). |
| 149 | 149 | */ |
| 150 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 150 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
| 151 | 151 | |
| 152 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 152 | + if (false === $this->is_entity($post_id)) { |
|
| 153 | 153 | return $default; |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | // Retrieve the entity type |
| 157 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 158 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 157 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 158 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 159 | 159 | // Retrieve classification boxes configuration |
| 160 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 161 | - foreach ( $classification_boxes as $cb ) { |
|
| 162 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 160 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 161 | + foreach ($classification_boxes as $cb) { |
|
| 162 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 163 | 163 | return $cb['id']; |
| 164 | 164 | } |
| 165 | 165 | } |
@@ -174,13 +174,13 @@ discard block |
||
| 174 | 174 | * |
| 175 | 175 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
| 176 | 176 | */ |
| 177 | - public function is_used( $post_id ) { |
|
| 177 | + public function is_used($post_id) { |
|
| 178 | 178 | |
| 179 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 179 | + if (false === $this->is_entity($post_id)) { |
|
| 180 | 180 | return null; |
| 181 | 181 | } |
| 182 | 182 | // Retrieve the post |
| 183 | - $entity = get_post( $post_id ); |
|
| 183 | + $entity = get_post($post_id); |
|
| 184 | 184 | |
| 185 | 185 | global $wpdb; |
| 186 | 186 | // Retrieve Wordlift relation instances table name |
@@ -193,9 +193,9 @@ discard block |
||
| 193 | 193 | ); |
| 194 | 194 | |
| 195 | 195 | // Perform the query |
| 196 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 196 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 197 | 197 | // If there is at least one relation instance for the current entity, then it's used |
| 198 | - if ( 0 < $relation_instances ) { |
|
| 198 | + if (0 < $relation_instances) { |
|
| 199 | 199 | return true; |
| 200 | 200 | } |
| 201 | 201 | |
@@ -203,13 +203,13 @@ discard block |
||
| 203 | 203 | $stmt = $wpdb->prepare( |
| 204 | 204 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 205 | 205 | $entity->ID, |
| 206 | - wl_get_entity_uri( $entity->ID ) |
|
| 206 | + wl_get_entity_uri($entity->ID) |
|
| 207 | 207 | ); |
| 208 | 208 | // Perform the query |
| 209 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 209 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 210 | 210 | |
| 211 | 211 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 212 | - if ( 0 < $meta_instances ) { |
|
| 212 | + if (0 < $meta_instances) { |
|
| 213 | 213 | return true; |
| 214 | 214 | } |
| 215 | 215 | |
@@ -226,9 +226,9 @@ discard block |
||
| 226 | 226 | * |
| 227 | 227 | * @return true if the uri internal to the current dataset otherwise false. |
| 228 | 228 | */ |
| 229 | - public function is_internal_uri( $uri ) { |
|
| 229 | + public function is_internal_uri($uri) { |
|
| 230 | 230 | |
| 231 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 231 | + return (0 === strrpos($uri, wl_configuration_get_redlink_dataset_uri())); |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | /** |
@@ -241,11 +241,11 @@ discard block |
||
| 241 | 241 | * |
| 242 | 242 | * @param array $uris An array of URIs. |
| 243 | 243 | */ |
| 244 | - public function preload_uris( $uris ) { |
|
| 244 | + public function preload_uris($uris) { |
|
| 245 | 245 | |
| 246 | 246 | $that = $this; |
| 247 | - $external_uris = array_filter( $uris, function ( $item ) use ( $that ) { |
|
| 248 | - return ! $that->is_internal_uri( $item ); |
|
| 247 | + $external_uris = array_filter($uris, function($item) use ($that) { |
|
| 248 | + return ! $that->is_internal_uri($item); |
|
| 249 | 249 | } ); |
| 250 | 250 | |
| 251 | 251 | $query_args = array( |
@@ -266,7 +266,7 @@ discard block |
||
| 266 | 266 | // performed also looking at sameAs values. |
| 267 | 267 | // |
| 268 | 268 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
| 269 | - if ( 0 < count( $external_uris ) ) { |
|
| 269 | + if (0 < count($external_uris)) { |
|
| 270 | 270 | |
| 271 | 271 | $query_args['meta_query']['relation'] = 'OR'; |
| 272 | 272 | $query_args['meta_query'][] = array( |
@@ -278,20 +278,20 @@ discard block |
||
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | // Get the posts. |
| 281 | - $posts = get_posts( $query_args ); |
|
| 281 | + $posts = get_posts($query_args); |
|
| 282 | 282 | |
| 283 | 283 | // Populate the array. We reinitialize the array on purpose because |
| 284 | 284 | // we don't want these data to long live. |
| 285 | - $this->uri_to_post = array_reduce( $posts, function ( $carry, $item ) use ( $that ) { |
|
| 285 | + $this->uri_to_post = array_reduce($posts, function($carry, $item) use ($that) { |
|
| 286 | 286 | return $carry |
| 287 | 287 | // Get the URI related to the post and fill them with the item id. |
| 288 | - + array_fill_keys( $that->get_uris( $item->ID ), $item ); |
|
| 289 | - }, array() ); |
|
| 288 | + + array_fill_keys($that->get_uris($item->ID), $item); |
|
| 289 | + }, array()); |
|
| 290 | 290 | |
| 291 | 291 | // Add the not found URIs. |
| 292 | - $this->uri_to_post += array_fill_keys( $uris, null ); |
|
| 292 | + $this->uri_to_post += array_fill_keys($uris, null); |
|
| 293 | 293 | |
| 294 | - $this->log->debug( count( $this->uri_to_post ) . " URI(s) preloaded." ); |
|
| 294 | + $this->log->debug(count($this->uri_to_post)." URI(s) preloaded."); |
|
| 295 | 295 | |
| 296 | 296 | } |
| 297 | 297 | |
@@ -315,10 +315,10 @@ discard block |
||
| 315 | 315 | * |
| 316 | 316 | * @return array An array of URIs. |
| 317 | 317 | */ |
| 318 | - public function get_uris( $post_id ) { |
|
| 318 | + public function get_uris($post_id) { |
|
| 319 | 319 | |
| 320 | - return get_post_meta( $post_id, WL_ENTITY_URL_META_NAME ) |
|
| 321 | - + get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_SAME_AS ); |
|
| 320 | + return get_post_meta($post_id, WL_ENTITY_URL_META_NAME) |
|
| 321 | + + get_post_meta($post_id, Wordlift_Schema_Service::FIELD_SAME_AS); |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | |
@@ -331,23 +331,23 @@ discard block |
||
| 331 | 331 | * |
| 332 | 332 | * @return WP_Post|null A WP_Post instance or null if not found. |
| 333 | 333 | */ |
| 334 | - public function get_entity_post_by_uri( $uri ) { |
|
| 334 | + public function get_entity_post_by_uri($uri) { |
|
| 335 | 335 | |
| 336 | - $this->log->trace( "Getting an entity post for URI $uri..." ); |
|
| 336 | + $this->log->trace("Getting an entity post for URI $uri..."); |
|
| 337 | 337 | |
| 338 | 338 | // Check if we've been provided with a value otherwise return null. |
| 339 | - if ( empty( $uri ) ) { |
|
| 339 | + if (empty($uri)) { |
|
| 340 | 340 | return null; |
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | // If the URI is cached, return the cached post. |
| 344 | - if ( array_key_exists( $uri, $this->uri_to_post ) ) { |
|
| 345 | - $this->log->debug( "Returning cached post for $uri..." ); |
|
| 344 | + if (array_key_exists($uri, $this->uri_to_post)) { |
|
| 345 | + $this->log->debug("Returning cached post for $uri..."); |
|
| 346 | 346 | |
| 347 | - return $this->uri_to_post[ $uri ]; |
|
| 347 | + return $this->uri_to_post[$uri]; |
|
| 348 | 348 | } |
| 349 | 349 | |
| 350 | - $this->log->debug( "Querying post for $uri..." ); |
|
| 350 | + $this->log->debug("Querying post for $uri..."); |
|
| 351 | 351 | |
| 352 | 352 | $query_args = array( |
| 353 | 353 | // See https://github.com/insideout10/wordlift-plugin/issues/654. |
@@ -387,7 +387,7 @@ discard block |
||
| 387 | 387 | // performed also looking at sameAs values. |
| 388 | 388 | // |
| 389 | 389 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
| 390 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 390 | + if ( ! $this->is_internal_uri($uri)) { |
|
| 391 | 391 | |
| 392 | 392 | $query_args['meta_query']['relation'] = 'OR'; |
| 393 | 393 | $query_args['meta_query'][] = array( |
@@ -397,17 +397,17 @@ discard block |
||
| 397 | 397 | ); |
| 398 | 398 | } |
| 399 | 399 | |
| 400 | - $posts = get_posts( $query_args ); |
|
| 400 | + $posts = get_posts($query_args); |
|
| 401 | 401 | |
| 402 | 402 | // Return null if no post is found. |
| 403 | - if ( 0 === count( $posts ) ) { |
|
| 404 | - $this->log->warn( "No post for URI $uri." ); |
|
| 403 | + if (0 === count($posts)) { |
|
| 404 | + $this->log->warn("No post for URI $uri."); |
|
| 405 | 405 | |
| 406 | 406 | return null; |
| 407 | 407 | } |
| 408 | 408 | |
| 409 | 409 | // Return the found post. |
| 410 | - return current( $posts ); |
|
| 410 | + return current($posts); |
|
| 411 | 411 | } |
| 412 | 412 | |
| 413 | 413 | /** |
@@ -422,10 +422,10 @@ discard block |
||
| 422 | 422 | * @param WP_Post $post Post object. |
| 423 | 423 | * @param bool $update Whether this is an existing post being updated or not. |
| 424 | 424 | */ |
| 425 | - public function save_post( $post_id, $post, $update ) { |
|
| 425 | + public function save_post($post_id, $post, $update) { |
|
| 426 | 426 | |
| 427 | 427 | // Avoid doing anything if post is autosave or a revision. |
| 428 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 428 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 429 | 429 | return; |
| 430 | 430 | } |
| 431 | 431 | |
@@ -435,15 +435,15 @@ discard block |
||
| 435 | 435 | // the $post_id in the save_post call matches the post id set in the request. |
| 436 | 436 | // |
| 437 | 437 | // If this is not the current post being saved or if it's not an entity, return. |
| 438 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 438 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 439 | 439 | return; |
| 440 | 440 | } |
| 441 | 441 | |
| 442 | 442 | // Get the alt labels from the request (or empty array). |
| 443 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 443 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 444 | 444 | |
| 445 | 445 | // Set the alternative labels. |
| 446 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 446 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 447 | 447 | |
| 448 | 448 | } |
| 449 | 449 | |
@@ -455,22 +455,22 @@ discard block |
||
| 455 | 455 | * @param int $post_id The post id. |
| 456 | 456 | * @param array $alt_labels An array of labels. |
| 457 | 457 | */ |
| 458 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 458 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 459 | 459 | |
| 460 | 460 | // Force $alt_labels to be an array |
| 461 | - if ( ! is_array( $alt_labels ) ) { |
|
| 462 | - $alt_labels = array( $alt_labels ); |
|
| 461 | + if ( ! is_array($alt_labels)) { |
|
| 462 | + $alt_labels = array($alt_labels); |
|
| 463 | 463 | } |
| 464 | 464 | |
| 465 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 465 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 466 | 466 | |
| 467 | 467 | // Delete all the existing alternate labels. |
| 468 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 468 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 469 | 469 | |
| 470 | 470 | // Set the alternative labels. |
| 471 | - foreach ( $alt_labels as $alt_label ) { |
|
| 472 | - if ( ! empty( $alt_label ) ) { |
|
| 473 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 471 | + foreach ($alt_labels as $alt_label) { |
|
| 472 | + if ( ! empty($alt_label)) { |
|
| 473 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 474 | 474 | } |
| 475 | 475 | } |
| 476 | 476 | |
@@ -485,9 +485,9 @@ discard block |
||
| 485 | 485 | * |
| 486 | 486 | * @return mixed An array of alternative labels. |
| 487 | 487 | */ |
| 488 | - public function get_alternative_labels( $post_id ) { |
|
| 488 | + public function get_alternative_labels($post_id) { |
|
| 489 | 489 | |
| 490 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 490 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 491 | 491 | } |
| 492 | 492 | |
| 493 | 493 | /** |
@@ -499,9 +499,9 @@ discard block |
||
| 499 | 499 | * |
| 500 | 500 | * @return array An array with the entity title and labels. |
| 501 | 501 | */ |
| 502 | - public function get_labels( $post_id ) { |
|
| 502 | + public function get_labels($post_id) { |
|
| 503 | 503 | |
| 504 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 504 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 505 | 505 | } |
| 506 | 506 | |
| 507 | 507 | /** |
@@ -511,25 +511,25 @@ discard block |
||
| 511 | 511 | * |
| 512 | 512 | * @param WP_Post $post Post object. |
| 513 | 513 | */ |
| 514 | - public function edit_form_before_permalink( $post ) { |
|
| 514 | + public function edit_form_before_permalink($post) { |
|
| 515 | 515 | |
| 516 | 516 | // If it's not an entity, return. |
| 517 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 517 | + if ( ! $this->is_entity($post->ID)) { |
|
| 518 | 518 | return; |
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | // Print the input template. |
| 522 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 522 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 523 | 523 | |
| 524 | 524 | // Print all the currently set alternative labels. |
| 525 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 525 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 526 | 526 | |
| 527 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 527 | + echo $this->get_alternative_label_input($alt_label); |
|
| 528 | 528 | |
| 529 | 529 | }; |
| 530 | 530 | |
| 531 | 531 | // Print the button. |
| 532 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 532 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 533 | 533 | |
| 534 | 534 | } |
| 535 | 535 | |
@@ -542,25 +542,25 @@ discard block |
||
| 542 | 542 | * |
| 543 | 543 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
| 544 | 544 | */ |
| 545 | - public function get_uri( $post_id ) { |
|
| 545 | + public function get_uri($post_id) { |
|
| 546 | 546 | |
| 547 | 547 | // If a null is given, nothing to do |
| 548 | - if ( null == $post_id ) { |
|
| 548 | + if (null == $post_id) { |
|
| 549 | 549 | return null; |
| 550 | 550 | } |
| 551 | 551 | |
| 552 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 552 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 553 | 553 | |
| 554 | 554 | // If the dataset uri is not properly configured, null is returned |
| 555 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 555 | + if ('' === wl_configuration_get_redlink_dataset_uri()) { |
|
| 556 | 556 | return null; |
| 557 | 557 | } |
| 558 | 558 | |
| 559 | 559 | // Set the URI if it isn't set yet. |
| 560 | - $post_status = get_post_status( $post_id ); |
|
| 561 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 562 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 563 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 560 | + $post_status = get_post_status($post_id); |
|
| 561 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 562 | + $uri = wl_build_entity_uri($post_id); |
|
| 563 | + wl_set_entity_uri($post_id, $uri); |
|
| 564 | 564 | } |
| 565 | 565 | |
| 566 | 566 | return $uri; |
@@ -576,9 +576,9 @@ discard block |
||
| 576 | 576 | * |
| 577 | 577 | * @return string The input HTML code. |
| 578 | 578 | */ |
| 579 | - private function get_alternative_label_input( $value = '' ) { |
|
| 579 | + private function get_alternative_label_input($value = '') { |
|
| 580 | 580 | |
| 581 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 581 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 582 | 582 | } |
| 583 | 583 | |
| 584 | 584 | /** |
@@ -590,12 +590,12 @@ discard block |
||
| 590 | 590 | */ |
| 591 | 591 | public function count() { |
| 592 | 592 | |
| 593 | - $posts = get_posts( $this->add_criterias( array( |
|
| 593 | + $posts = get_posts($this->add_criterias(array( |
|
| 594 | 594 | 'post_status' => 'any', |
| 595 | - 'numberposts' => - 1, |
|
| 596 | - ) ) ); |
|
| 595 | + 'numberposts' => -1, |
|
| 596 | + ))); |
|
| 597 | 597 | |
| 598 | - return count( $posts ); |
|
| 598 | + return count($posts); |
|
| 599 | 599 | } |
| 600 | 600 | |
| 601 | 601 | /** |
@@ -608,7 +608,7 @@ discard block |
||
| 608 | 608 | * |
| 609 | 609 | * @return array The arguments for a `get_posts` call. |
| 610 | 610 | */ |
| 611 | - public static function add_criterias( $args ) { |
|
| 611 | + public static function add_criterias($args) { |
|
| 612 | 612 | |
| 613 | 613 | // Build an optimal tax-query. |
| 614 | 614 | $tax_query = array( |
@@ -678,28 +678,28 @@ discard block |
||
| 678 | 678 | * |
| 679 | 679 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
| 680 | 680 | */ |
| 681 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 681 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 682 | 682 | |
| 683 | 683 | // Create an entity for the publisher. |
| 684 | - $post_id = wp_insert_post( array( |
|
| 684 | + $post_id = wp_insert_post(array( |
|
| 685 | 685 | 'post_type' => self::TYPE_NAME, |
| 686 | 686 | 'post_title' => $name, |
| 687 | 687 | 'post_status' => $status, |
| 688 | 688 | 'post_content' => '', |
| 689 | - ) ); |
|
| 689 | + )); |
|
| 690 | 690 | |
| 691 | 691 | // Return the error if any. |
| 692 | - if ( is_wp_error( $post_id ) ) { |
|
| 692 | + if (is_wp_error($post_id)) { |
|
| 693 | 693 | return $post_id; |
| 694 | 694 | } |
| 695 | 695 | |
| 696 | 696 | // Set the entity logo. |
| 697 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 698 | - set_post_thumbnail( $post_id, $logo ); |
|
| 697 | + if ($logo && is_numeric($logo)) { |
|
| 698 | + set_post_thumbnail($post_id, $logo); |
|
| 699 | 699 | } |
| 700 | 700 | |
| 701 | 701 | // Set the entity type. |
| 702 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 702 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 703 | 703 | |
| 704 | 704 | return $post_id; |
| 705 | 705 | } |
@@ -715,9 +715,9 @@ discard block |
||
| 715 | 715 | * |
| 716 | 716 | * @return array An array of post ids. |
| 717 | 717 | */ |
| 718 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 718 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 719 | 719 | |
| 720 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 720 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
| 721 | 721 | } |
| 722 | 722 | |
| 723 | 723 | /** |
@@ -729,16 +729,16 @@ discard block |
||
| 729 | 729 | * |
| 730 | 730 | * @return array An array of entity posts. |
| 731 | 731 | */ |
| 732 | - public function get( $params = array() ) { |
|
| 732 | + public function get($params = array()) { |
|
| 733 | 733 | |
| 734 | 734 | // Set the defaults. |
| 735 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 735 | + $defaults = array('post_type' => 'entity'); |
|
| 736 | 736 | |
| 737 | 737 | // Merge the defaults with the provided parameters. |
| 738 | - $args = wp_parse_args( $params, $defaults ); |
|
| 738 | + $args = wp_parse_args($params, $defaults); |
|
| 739 | 739 | |
| 740 | 740 | // Call the `get_posts` function. |
| 741 | - return get_posts( $args ); |
|
| 741 | + return get_posts($args); |
|
| 742 | 742 | } |
| 743 | 743 | |
| 744 | 744 | /** |
@@ -754,9 +754,9 @@ discard block |
||
| 754 | 754 | static function valid_entity_post_types() { |
| 755 | 755 | |
| 756 | 756 | // Ignore builtins in the call to avoid getting attachments. |
| 757 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 757 | + $post_types = array('post', 'page', self::TYPE_NAME); |
|
| 758 | 758 | |
| 759 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 759 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
| 760 | 760 | } |
| 761 | 761 | |
| 762 | 762 | } |