@@ -9,65 +9,65 @@ discard block |
||
| 9 | 9 | // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledClassName |
| 10 | 10 | class Wordpress_Permalink_Content_Service implements Content_Service { |
| 11 | 11 | |
| 12 | - private static $instance = null; |
|
| 13 | - |
|
| 14 | - protected function __construct() { |
|
| 15 | - } |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * The singleton instance. |
|
| 19 | - * |
|
| 20 | - * @return Content_Service |
|
| 21 | - */ |
|
| 22 | - public static function get_instance() { |
|
| 23 | - if ( ! isset( self::$instance ) ) { |
|
| 24 | - self::$instance = new self(); |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - return self::$instance; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @param $uri string In the form https://example.org/path/post#post/1 |
|
| 32 | - * |
|
| 33 | - * @return Wordpress_Content|null |
|
| 34 | - */ |
|
| 35 | - public function get_by_entity_id( $uri ) { |
|
| 36 | - if ( ! preg_match( '@.*#(\w+)/(\d+)@', $uri, $matches ) ) { |
|
| 37 | - return null; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - $type_name = $matches[1]; |
|
| 41 | - $id = $matches[2]; |
|
| 42 | - switch ( Object_Type_Enum::from_string( $type_name ) ) { |
|
| 43 | - case Object_Type_Enum::POST: |
|
| 44 | - return new Wordpress_Content( get_post( $id ) ); |
|
| 45 | - case Object_Type_Enum::TERM: |
|
| 46 | - return new Wordpress_Content( get_term( $id ) ); |
|
| 47 | - case Object_Type_Enum::USER: |
|
| 48 | - return new Wordpress_Content( get_user_by( 'ID', $id ) ); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - return null; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Get a |
|
| 56 | - * |
|
| 57 | - * @throws Exception in case of error. |
|
| 58 | - */ |
|
| 59 | - public function get_by_entity_id_or_same_as( $uri ) { |
|
| 60 | - // If the URL is in the local site URL, then try to find a corresponding post. |
|
| 61 | - if ( 0 === strpos( $uri, site_url() ) ) { |
|
| 62 | - return $this->get_by_entity_id( $uri ); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - // Otherwise look in sameAs. |
|
| 66 | - global $wpdb; |
|
| 67 | - |
|
| 68 | - $row = $wpdb->get_row( |
|
| 69 | - $wpdb->prepare( |
|
| 70 | - " |
|
| 12 | + private static $instance = null; |
|
| 13 | + |
|
| 14 | + protected function __construct() { |
|
| 15 | + } |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * The singleton instance. |
|
| 19 | + * |
|
| 20 | + * @return Content_Service |
|
| 21 | + */ |
|
| 22 | + public static function get_instance() { |
|
| 23 | + if ( ! isset( self::$instance ) ) { |
|
| 24 | + self::$instance = new self(); |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + return self::$instance; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @param $uri string In the form https://example.org/path/post#post/1 |
|
| 32 | + * |
|
| 33 | + * @return Wordpress_Content|null |
|
| 34 | + */ |
|
| 35 | + public function get_by_entity_id( $uri ) { |
|
| 36 | + if ( ! preg_match( '@.*#(\w+)/(\d+)@', $uri, $matches ) ) { |
|
| 37 | + return null; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + $type_name = $matches[1]; |
|
| 41 | + $id = $matches[2]; |
|
| 42 | + switch ( Object_Type_Enum::from_string( $type_name ) ) { |
|
| 43 | + case Object_Type_Enum::POST: |
|
| 44 | + return new Wordpress_Content( get_post( $id ) ); |
|
| 45 | + case Object_Type_Enum::TERM: |
|
| 46 | + return new Wordpress_Content( get_term( $id ) ); |
|
| 47 | + case Object_Type_Enum::USER: |
|
| 48 | + return new Wordpress_Content( get_user_by( 'ID', $id ) ); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + return null; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Get a |
|
| 56 | + * |
|
| 57 | + * @throws Exception in case of error. |
|
| 58 | + */ |
|
| 59 | + public function get_by_entity_id_or_same_as( $uri ) { |
|
| 60 | + // If the URL is in the local site URL, then try to find a corresponding post. |
|
| 61 | + if ( 0 === strpos( $uri, site_url() ) ) { |
|
| 62 | + return $this->get_by_entity_id( $uri ); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + // Otherwise look in sameAs. |
|
| 66 | + global $wpdb; |
|
| 67 | + |
|
| 68 | + $row = $wpdb->get_row( |
|
| 69 | + $wpdb->prepare( |
|
| 70 | + " |
|
| 71 | 71 | SELECT content_type, content_id |
| 72 | 72 | FROM ( |
| 73 | 73 | SELECT %d AS content_type, post_id AS content_id |
@@ -84,94 +84,94 @@ discard block |
||
| 84 | 84 | ) _tmp_same_as |
| 85 | 85 | LIMIT 1 |
| 86 | 86 | ", |
| 87 | - Object_Type_Enum::POST, |
|
| 88 | - $uri, |
|
| 89 | - Object_Type_Enum::TERM, |
|
| 90 | - $uri, |
|
| 91 | - Object_Type_Enum::USER, |
|
| 92 | - $uri |
|
| 93 | - ) |
|
| 94 | - ); |
|
| 95 | - |
|
| 96 | - if ( ! isset( $row ) ) { |
|
| 97 | - return null; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - switch ( (int) $row->content_type ) { |
|
| 101 | - case Object_Type_Enum::POST: |
|
| 102 | - return new Wordpress_Content( get_post( $row->content_id ) ); |
|
| 103 | - case Object_Type_Enum::TERM: |
|
| 104 | - return new Wordpress_Content( get_term( $row->content_id ) ); |
|
| 105 | - case Object_Type_Enum::USER: |
|
| 106 | - return new Wordpress_Content( get_user_by( 'ID', $row->content_id ) ); |
|
| 107 | - default: |
|
| 108 | - return null; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @param Wordpress_Content_Id $content_id |
|
| 115 | - * |
|
| 116 | - * @return string|void|null |
|
| 117 | - */ |
|
| 118 | - public function get_entity_id( $content_id ) { |
|
| 119 | - $type = $content_id->get_type(); |
|
| 120 | - $id = $content_id->get_id(); |
|
| 121 | - |
|
| 122 | - switch ( $type ) { |
|
| 123 | - case Object_Type_Enum::POST: |
|
| 124 | - $base_uri = get_permalink( $id ); |
|
| 125 | - break; |
|
| 126 | - case Object_Type_Enum::TERM: |
|
| 127 | - $base_uri = get_term_link( $id ); |
|
| 128 | - break; |
|
| 129 | - case Object_Type_Enum::USER: |
|
| 130 | - $base_uri = get_author_posts_url( $id ); |
|
| 131 | - break; |
|
| 132 | - default: |
|
| 133 | - return null; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $type_name = Object_Type_Enum::to_string( $type ); |
|
| 137 | - |
|
| 138 | - return "$base_uri#$type_name/$id"; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 142 | - public function set_entity_id( $content_id, $uri ) { |
|
| 143 | - // do nothing. |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @param Wordpress_Content_Id $content_id |
|
| 148 | - * |
|
| 149 | - * @return bool|void |
|
| 150 | - */ |
|
| 151 | - public function supports( $content_id ) { |
|
| 152 | - return in_array( |
|
| 153 | - $content_id->get_type(), |
|
| 154 | - array( |
|
| 155 | - Object_Type_Enum::POST, |
|
| 156 | - Object_Type_Enum::TERM, |
|
| 157 | - Object_Type_Enum::USER, |
|
| 158 | - ), |
|
| 159 | - true |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 164 | - public function delete( $content_id ) { |
|
| 165 | - // Ignore, we don't store any data. |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 169 | - public function get_about_jsonld( $content_id ) { |
|
| 170 | - // This is not implemented as of now |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 174 | - public function set_about_jsonld( $content_id, $value ) { |
|
| 175 | - // This is not implemented as of now |
|
| 176 | - } |
|
| 87 | + Object_Type_Enum::POST, |
|
| 88 | + $uri, |
|
| 89 | + Object_Type_Enum::TERM, |
|
| 90 | + $uri, |
|
| 91 | + Object_Type_Enum::USER, |
|
| 92 | + $uri |
|
| 93 | + ) |
|
| 94 | + ); |
|
| 95 | + |
|
| 96 | + if ( ! isset( $row ) ) { |
|
| 97 | + return null; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + switch ( (int) $row->content_type ) { |
|
| 101 | + case Object_Type_Enum::POST: |
|
| 102 | + return new Wordpress_Content( get_post( $row->content_id ) ); |
|
| 103 | + case Object_Type_Enum::TERM: |
|
| 104 | + return new Wordpress_Content( get_term( $row->content_id ) ); |
|
| 105 | + case Object_Type_Enum::USER: |
|
| 106 | + return new Wordpress_Content( get_user_by( 'ID', $row->content_id ) ); |
|
| 107 | + default: |
|
| 108 | + return null; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @param Wordpress_Content_Id $content_id |
|
| 115 | + * |
|
| 116 | + * @return string|void|null |
|
| 117 | + */ |
|
| 118 | + public function get_entity_id( $content_id ) { |
|
| 119 | + $type = $content_id->get_type(); |
|
| 120 | + $id = $content_id->get_id(); |
|
| 121 | + |
|
| 122 | + switch ( $type ) { |
|
| 123 | + case Object_Type_Enum::POST: |
|
| 124 | + $base_uri = get_permalink( $id ); |
|
| 125 | + break; |
|
| 126 | + case Object_Type_Enum::TERM: |
|
| 127 | + $base_uri = get_term_link( $id ); |
|
| 128 | + break; |
|
| 129 | + case Object_Type_Enum::USER: |
|
| 130 | + $base_uri = get_author_posts_url( $id ); |
|
| 131 | + break; |
|
| 132 | + default: |
|
| 133 | + return null; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $type_name = Object_Type_Enum::to_string( $type ); |
|
| 137 | + |
|
| 138 | + return "$base_uri#$type_name/$id"; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 142 | + public function set_entity_id( $content_id, $uri ) { |
|
| 143 | + // do nothing. |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @param Wordpress_Content_Id $content_id |
|
| 148 | + * |
|
| 149 | + * @return bool|void |
|
| 150 | + */ |
|
| 151 | + public function supports( $content_id ) { |
|
| 152 | + return in_array( |
|
| 153 | + $content_id->get_type(), |
|
| 154 | + array( |
|
| 155 | + Object_Type_Enum::POST, |
|
| 156 | + Object_Type_Enum::TERM, |
|
| 157 | + Object_Type_Enum::USER, |
|
| 158 | + ), |
|
| 159 | + true |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 164 | + public function delete( $content_id ) { |
|
| 165 | + // Ignore, we don't store any data. |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 169 | + public function get_about_jsonld( $content_id ) { |
|
| 170 | + // This is not implemented as of now |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 174 | + public function set_about_jsonld( $content_id, $value ) { |
|
| 175 | + // This is not implemented as of now |
|
| 176 | + } |
|
| 177 | 177 | } |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | * @return Content_Service |
| 21 | 21 | */ |
| 22 | 22 | public static function get_instance() { |
| 23 | - if ( ! isset( self::$instance ) ) { |
|
| 23 | + if ( ! isset(self::$instance)) { |
|
| 24 | 24 | self::$instance = new self(); |
| 25 | 25 | } |
| 26 | 26 | |
@@ -32,20 +32,20 @@ discard block |
||
| 32 | 32 | * |
| 33 | 33 | * @return Wordpress_Content|null |
| 34 | 34 | */ |
| 35 | - public function get_by_entity_id( $uri ) { |
|
| 36 | - if ( ! preg_match( '@.*#(\w+)/(\d+)@', $uri, $matches ) ) { |
|
| 35 | + public function get_by_entity_id($uri) { |
|
| 36 | + if ( ! preg_match('@.*#(\w+)/(\d+)@', $uri, $matches)) { |
|
| 37 | 37 | return null; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | $type_name = $matches[1]; |
| 41 | 41 | $id = $matches[2]; |
| 42 | - switch ( Object_Type_Enum::from_string( $type_name ) ) { |
|
| 42 | + switch (Object_Type_Enum::from_string($type_name)) { |
|
| 43 | 43 | case Object_Type_Enum::POST: |
| 44 | - return new Wordpress_Content( get_post( $id ) ); |
|
| 44 | + return new Wordpress_Content(get_post($id)); |
|
| 45 | 45 | case Object_Type_Enum::TERM: |
| 46 | - return new Wordpress_Content( get_term( $id ) ); |
|
| 46 | + return new Wordpress_Content(get_term($id)); |
|
| 47 | 47 | case Object_Type_Enum::USER: |
| 48 | - return new Wordpress_Content( get_user_by( 'ID', $id ) ); |
|
| 48 | + return new Wordpress_Content(get_user_by('ID', $id)); |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | return null; |
@@ -56,10 +56,10 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @throws Exception in case of error. |
| 58 | 58 | */ |
| 59 | - public function get_by_entity_id_or_same_as( $uri ) { |
|
| 59 | + public function get_by_entity_id_or_same_as($uri) { |
|
| 60 | 60 | // If the URL is in the local site URL, then try to find a corresponding post. |
| 61 | - if ( 0 === strpos( $uri, site_url() ) ) { |
|
| 62 | - return $this->get_by_entity_id( $uri ); |
|
| 61 | + if (0 === strpos($uri, site_url())) { |
|
| 62 | + return $this->get_by_entity_id($uri); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | // Otherwise look in sameAs. |
@@ -93,17 +93,17 @@ discard block |
||
| 93 | 93 | ) |
| 94 | 94 | ); |
| 95 | 95 | |
| 96 | - if ( ! isset( $row ) ) { |
|
| 96 | + if ( ! isset($row)) { |
|
| 97 | 97 | return null; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - switch ( (int) $row->content_type ) { |
|
| 100 | + switch ((int) $row->content_type) { |
|
| 101 | 101 | case Object_Type_Enum::POST: |
| 102 | - return new Wordpress_Content( get_post( $row->content_id ) ); |
|
| 102 | + return new Wordpress_Content(get_post($row->content_id)); |
|
| 103 | 103 | case Object_Type_Enum::TERM: |
| 104 | - return new Wordpress_Content( get_term( $row->content_id ) ); |
|
| 104 | + return new Wordpress_Content(get_term($row->content_id)); |
|
| 105 | 105 | case Object_Type_Enum::USER: |
| 106 | - return new Wordpress_Content( get_user_by( 'ID', $row->content_id ) ); |
|
| 106 | + return new Wordpress_Content(get_user_by('ID', $row->content_id)); |
|
| 107 | 107 | default: |
| 108 | 108 | return null; |
| 109 | 109 | } |
@@ -115,31 +115,31 @@ discard block |
||
| 115 | 115 | * |
| 116 | 116 | * @return string|void|null |
| 117 | 117 | */ |
| 118 | - public function get_entity_id( $content_id ) { |
|
| 118 | + public function get_entity_id($content_id) { |
|
| 119 | 119 | $type = $content_id->get_type(); |
| 120 | 120 | $id = $content_id->get_id(); |
| 121 | 121 | |
| 122 | - switch ( $type ) { |
|
| 122 | + switch ($type) { |
|
| 123 | 123 | case Object_Type_Enum::POST: |
| 124 | - $base_uri = get_permalink( $id ); |
|
| 124 | + $base_uri = get_permalink($id); |
|
| 125 | 125 | break; |
| 126 | 126 | case Object_Type_Enum::TERM: |
| 127 | - $base_uri = get_term_link( $id ); |
|
| 127 | + $base_uri = get_term_link($id); |
|
| 128 | 128 | break; |
| 129 | 129 | case Object_Type_Enum::USER: |
| 130 | - $base_uri = get_author_posts_url( $id ); |
|
| 130 | + $base_uri = get_author_posts_url($id); |
|
| 131 | 131 | break; |
| 132 | 132 | default: |
| 133 | 133 | return null; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - $type_name = Object_Type_Enum::to_string( $type ); |
|
| 136 | + $type_name = Object_Type_Enum::to_string($type); |
|
| 137 | 137 | |
| 138 | 138 | return "$base_uri#$type_name/$id"; |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 142 | - public function set_entity_id( $content_id, $uri ) { |
|
| 142 | + public function set_entity_id($content_id, $uri) { |
|
| 143 | 143 | // do nothing. |
| 144 | 144 | } |
| 145 | 145 | |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @return bool|void |
| 150 | 150 | */ |
| 151 | - public function supports( $content_id ) { |
|
| 151 | + public function supports($content_id) { |
|
| 152 | 152 | return in_array( |
| 153 | 153 | $content_id->get_type(), |
| 154 | 154 | array( |
@@ -161,17 +161,17 @@ discard block |
||
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 164 | - public function delete( $content_id ) { |
|
| 164 | + public function delete($content_id) { |
|
| 165 | 165 | // Ignore, we don't store any data. |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 169 | - public function get_about_jsonld( $content_id ) { |
|
| 169 | + public function get_about_jsonld($content_id) { |
|
| 170 | 170 | // This is not implemented as of now |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 174 | - public function set_about_jsonld( $content_id, $value ) { |
|
| 174 | + public function set_about_jsonld($content_id, $value) { |
|
| 175 | 175 | // This is not implemented as of now |
| 176 | 176 | } |
| 177 | 177 | } |