| @@ -46,13 +46,55 @@ | ||
| 46 | 46 | ->find_many(); |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | + * Retrieves an SEO Link by url. | |
| 51 | + * | |
| 52 | + * @param string $url The SEO Link's url. | |
| 53 | + * | |
| 54 | + * @return SEO_Links|false The SEO Link, or false if none found. | |
| 55 | + */ | |
| 56 | + public function find_one_by_url( $url ) { | |
| 57 | + return $this->query() | |
| 58 | + ->select( 'target_post_id' ) | |
| 59 | + ->where( 'url', $url ) | |
| 60 | + ->find_one(); | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Retrieves all SEO Links by target post ID. | |
| 65 | + * | |
| 66 | + * @param string $target_post_id The SEO Link's target post ID. | |
| 67 | + * | |
| 68 | + * @return SEO_Links[] The SEO Links. | |
| 69 | + */ | |
| 70 | + public function find_all_by_target_post_id( $target_post_id ) { | |
| 71 | + return $this->query() | |
| 72 | + ->where( 'target_post_id', $target_post_id ) | |
| 73 | + ->find_many(); | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Updates the ID of the target indexable of a link. | |
| 78 | + * | |
| 79 | + * @param int $link_id The ID of the link to be updated. | |
| 80 | + * @param int $target_indexable_id The ID of the target indexable. | |
| 81 | + * | |
| 82 | + * @return bool Whether or not the update was succeful. | |
| 83 | + */ | |
| 84 | + public function update_target_indexable_id( $link_id, $target_indexable_id ) { | |
| 85 | + return (bool) $this->query() | |
| 86 | + ->set( 'target_indexable_id', $target_indexable_id ) | |
| 87 | + ->where( 'id', $link_id ) | |
| 88 | + ->update_many(); | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 50 | 92 | * Clears all SEO Links by post ID. |
| 51 | 93 | * |
| 52 | 94 | * @param int $post_id The post ID. |
| 53 | 95 | * |
| 54 | - * @return bool Whether or not the delete was succesfull. | |
| 96 | + * @return bool Whether or not the delete was successful. | |
| 55 | 97 | */ |
| 56 | 98 | public function delete_all_by_post_id( $post_id ) { |
| 57 | 99 | return $this->query() |
| 58 | 100 | ->where( 'post_id', $post_id ) |
| @@ -63,9 +105,9 @@ | ||
| 63 | 105 | * Clears all SEO Links by post ID where the indexable id is null. |
| 64 | 106 | * |
| 65 | 107 | * @param int $post_id The post ID. |
| 66 | 108 | * |
| 67 | - * @return bool Whether or not the delete was succesfull. | |
| 109 | + * @return bool Whether or not the delete was successful. | |
| 68 | 110 | */ |
| 69 | 111 | public function delete_all_by_post_id_where_indexable_id_null( $post_id ) { |
| 70 | 112 | return $this->query() |
| 71 | 113 | ->where( 'post_id', $post_id ) |
| @@ -77,9 +119,9 @@ | ||
| 77 | 119 | * Clears all SEO Links by indexable ID. |
| 78 | 120 | * |
| 79 | 121 | * @param int $indexable_id The indexable ID. |
| 80 | 122 | * |
| 81 | - * @return bool Whether or not the delete was succesfull. | |
| 123 | + * @return bool Whether or not the delete was successful. | |
| 82 | 124 | */ |
| 83 | 125 | public function delete_all_by_indexable_id( $indexable_id ) { |
| 84 | 126 | return $this->query() |
| 85 | 127 | ->where( 'indexable_id', $indexable_id ) |
| @@ -109,8 +151,12 @@ | ||
| 109 | 151 | * |
| 110 | 152 | * @return array An array of associative arrays, each containing a indexable id and incoming property. |
| 111 | 153 | */ |
| 112 | 154 | public function get_incoming_link_counts_for_indexable_ids( $indexable_ids ) { |
| 155 | + if ( empty( $indexable_ids ) ) { | |
| 156 | + return []; | |
| 157 | + } | |
| 158 | + | |
| 113 | 159 | // This query only returns ID's with an incoming count > 0. We need to restore any ID's with 0 incoming links later. |
| 114 | 160 | $indexable_counts = $this->query() |
| 115 | 161 | ->select_expr( 'COUNT( id )', 'incoming' ) |
| 116 | 162 | ->select( 'target_indexable_id' ) |
| @@ -128,9 +174,9 @@ | ||
| 128 | 174 | |
| 129 | 175 | // Loop over the original ID's and search them in the returned ID's. If they don't exist, add them with an incoming count of 0. |
| 130 | 176 | foreach ( $indexable_ids as $id ) { |
| 131 | 177 | // Cast the ID to string, as the arrays only contain stringified versions of the ID. |
| 132 | - $id = \strval( $id ); | |
| 178 | + $id = (string) $id; | |
| 133 | 179 | if ( isset( $returned_ids[ $id ] ) === false ) { |
| 134 | 180 | $indexable_counts[] = [ |
| 135 | 181 | 'incoming' => '0', |
| 136 | 182 | 'target_indexable_id' => $id, |
| @@ -145,9 +191,9 @@ | ||
| 145 | 191 | * Deletes all seo links for the given ids. |
| 146 | 192 | * |
| 147 | 193 | * @param int[] $ids The seo link ids. |
| 148 | 194 | * |
| 149 | - * @return bool Whether or not the delete was succesfull. | |
| 195 | + * @return bool Whether or not the delete was successful. | |
| 150 | 196 | */ |
| 151 | 197 | public function delete_many_by_id( $ids ) { |
| 152 | 198 | return $this->query() |
| 153 | 199 | ->where_in( 'id', $ids ) |
| @@ -158,9 +204,9 @@ | ||
| 158 | 204 | * Insert multiple seo links. |
| 159 | 205 | * |
| 160 | 206 | * @param SEO_Links[] $links The seo links to be inserted. |
| 161 | 207 | * |
| 162 | - * @return bool Whether or not the insert was succesfull. | |
| 208 | + * @return bool Whether or not the insert was successful. | |
| 163 | 209 | */ |
| 164 | 210 | public function insert_many( $links ) { |
| 165 | 211 | return $this->query() |
| 166 | 212 | ->insert_many( $links ); |