1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
<?php /** * WPSEO plugin file. * * @package WPSEO\Admin\Links */
/** * Represents the link column count. This class contains the count for each post id on the current page. */ class WPSEO_Link_Column_Count {
/** * @var array */ protected $count = array();
/** * Sets the counts for the set target field. * * @param array $post_ids The posts to get the count for. */ public function set( $post_ids ) { if ( empty( $post_ids ) ) { return; }
$this->count = $this->get_results( $post_ids ); }
/** * Gets the link count for given post id. * * @param int $post_id The post id. * @param string $target_field The field to show. * * @return int|null The total amount of links or null if the target field * does not exist for the given post id. */ public function get( $post_id, $target_field = 'internal_link_count' ) { if ( array_key_exists( $post_id, $this->count ) && array_key_exists( $target_field, $this->count[ $post_id ] ) ) { return $this->count[ $post_id ][ $target_field ]; }
return null; }
/** * Gets the link count for the given post ids. * * @param array $post_ids Array with post_ids. * * @return array */ protected function get_results( $post_ids ) { global $wpdb;
$storage = new WPSEO_Meta_Storage();
$results = $wpdb->get_results( $wpdb->prepare( ' SELECT internal_link_count, incoming_link_count, object_id FROM ' . $storage->get_table_name() . ' WHERE object_id IN (' . implode( ',', array_fill( 0, count( $post_ids ), '%d' ) ) . ')', $post_ids ), ARRAY_A );
$output = array(); foreach ( $results as $result ) { $output[ (int) $result['object_id'] ] = array( 'internal_link_count' => $result['internal_link_count'], 'incoming_link_count' => (int) $result['incoming_link_count'], ); }
// Set unfound items to zero. foreach ( $post_ids as $post_id ) { if ( ! array_key_exists( $post_id, $output ) ) { $output[ $post_id ] = array( 'internal_link_count' => null, 'incoming_link_count' => 0, ); } }
return $output; } }
|