| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Repositories; |
| 4 |
|
| 5 |
use Yoast\WP\Lib\Model; |
| 6 |
use Yoast\WP\Lib\ORM; |
| 7 |
use Yoast\WP\SEO\Models\SEO_Links; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class SEO_Links_Repository. |
| 11 |
*/ |
| 12 |
class SEO_Links_Repository { |
| 13 |
|
| 14 |
/** |
| 15 |
* Starts a query for this repository. |
| 16 |
* |
| 17 |
* @return ORM |
| 18 |
*/ |
| 19 |
public function query() { |
| 20 |
return Model::of_type( 'SEO_Links' ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Finds all SEO Links by post ID. |
| 25 |
* |
| 26 |
* @param int $post_id The post ID. |
| 27 |
* |
| 28 |
* @return SEO_Links[] The SEO Links. |
| 29 |
*/ |
| 30 |
public function find_all_by_post_id( $post_id ) { |
| 31 |
return $this->query() |
| 32 |
->where( 'post_id', $post_id ) |
| 33 |
->find_many(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Finds all SEO Links by indexable ID. |
| 38 |
* |
| 39 |
* @param int $indexable_id The indexable ID. |
| 40 |
* |
| 41 |
* @return SEO_Links[] The SEO Links. |
| 42 |
*/ |
| 43 |
public function find_all_by_indexable_id( $indexable_id ) { |
| 44 |
return $this->query() |
| 45 |
->where( 'indexable_id', $indexable_id ) |
| 46 |
->find_many(); |
| 47 |
} |
| 48 |
|
| 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 |
/** |
| 92 |
* Clears all SEO Links by post ID. |
| 93 |
* |
| 94 |
* @param int $post_id The post ID. |
| 95 |
* |
| 96 |
* @return bool Whether or not the delete was successful. |
| 97 |
*/ |
| 98 |
public function delete_all_by_post_id( $post_id ) { |
| 99 |
return $this->query() |
| 100 |
->where( 'post_id', $post_id ) |
| 101 |
->delete_many(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Clears all SEO Links by post ID where the indexable id is null. |
| 106 |
* |
| 107 |
* @param int $post_id The post ID. |
| 108 |
* |
| 109 |
* @return bool Whether or not the delete was successful. |
| 110 |
*/ |
| 111 |
public function delete_all_by_post_id_where_indexable_id_null( $post_id ) { |
| 112 |
return $this->query() |
| 113 |
->where( 'post_id', $post_id ) |
| 114 |
->where_null( 'indexable_id' ) |
| 115 |
->delete_many(); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Clears all SEO Links by indexable ID. |
| 120 |
* |
| 121 |
* @param int $indexable_id The indexable ID. |
| 122 |
* |
| 123 |
* @return bool Whether or not the delete was successful. |
| 124 |
*/ |
| 125 |
public function delete_all_by_indexable_id( $indexable_id ) { |
| 126 |
return $this->query() |
| 127 |
->where( 'indexable_id', $indexable_id ) |
| 128 |
->delete_many(); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns incoming link counts for a number of posts. |
| 133 |
* |
| 134 |
* @param array $post_ids The post IDs. |
| 135 |
* |
| 136 |
* @return array An array of associative arrays, each containing a post id and incoming property. |
| 137 |
*/ |
| 138 |
public function get_incoming_link_counts_for_post_ids( $post_ids ) { |
| 139 |
return $this->query() |
| 140 |
->select_expr( 'COUNT( id )', 'incoming' ) |
| 141 |
->select( 'target_post_id', 'post_id' ) |
| 142 |
->where_in( 'target_post_id', $post_ids ) |
| 143 |
->group_by( 'target_post_id' ) |
| 144 |
->find_array(); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns incoming link counts for a number of indexables. |
| 149 |
* |
| 150 |
* @param array $indexable_ids The indexable IDs. |
| 151 |
* |
| 152 |
* @return array An array of associative arrays, each containing a indexable id and incoming property. |
| 153 |
*/ |
| 154 |
public function get_incoming_link_counts_for_indexable_ids( $indexable_ids ) { |
| 155 |
if ( empty( $indexable_ids ) ) { |
| 156 |
return []; |
| 157 |
} |
| 158 |
|
| 159 |
// This query only returns ID's with an incoming count > 0. We need to restore any ID's with 0 incoming links later. |
| 160 |
$indexable_counts = $this->query() |
| 161 |
->select_expr( 'COUNT( id )', 'incoming' ) |
| 162 |
->select( 'target_indexable_id' ) |
| 163 |
->where_in( 'target_indexable_id', $indexable_ids ) |
| 164 |
->group_by( 'target_indexable_id' ) |
| 165 |
->find_array(); |
| 166 |
|
| 167 |
// If the above query fails, do not update anything. |
| 168 |
if ( ! \is_array( $indexable_counts ) ) { |
| 169 |
return []; |
| 170 |
} |
| 171 |
|
| 172 |
// Get all ID's returned from the query and set them as keys for easy access. |
| 173 |
$returned_ids = \array_flip( \array_column( $indexable_counts, 'target_indexable_id' ) ); |
| 174 |
|
| 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. |
| 176 |
foreach ( $indexable_ids as $id ) { |
| 177 |
// Cast the ID to string, as the arrays only contain stringified versions of the ID. |
| 178 |
$id = (string) $id; |
| 179 |
if ( isset( $returned_ids[ $id ] ) === false ) { |
| 180 |
$indexable_counts[] = [ |
| 181 |
'incoming' => '0', |
| 182 |
'target_indexable_id' => $id, |
| 183 |
]; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return $indexable_counts; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Deletes all seo links for the given ids. |
| 192 |
* |
| 193 |
* @param int[] $ids The seo link ids. |
| 194 |
* |
| 195 |
* @return bool Whether or not the delete was successful. |
| 196 |
*/ |
| 197 |
public function delete_many_by_id( $ids ) { |
| 198 |
return $this->query() |
| 199 |
->where_in( 'id', $ids ) |
| 200 |
->delete_many(); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Insert multiple seo links. |
| 205 |
* |
| 206 |
* @param SEO_Links[] $links The seo links to be inserted. |
| 207 |
* |
| 208 |
* @return bool Whether or not the insert was successful. |
| 209 |
*/ |
| 210 |
public function insert_many( $links ) { |
| 211 |
return $this->query() |
| 212 |
->insert_many( $links ); |
| 213 |
} |
| 214 |
} |
| 215 |
|