class-meta-storage.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Links
  6. */
  7. /**
  8. * Represents the link count storage.
  9. */
  10. class WPSEO_Meta_Storage implements WPSEO_Installable {
  11. /**
  12. * Table name for the meta storage.
  13. *
  14. * @var string
  15. */
  16. const TABLE_NAME = 'yoast_seo_meta';
  17. /**
  18. * Holds the database's proxy.
  19. *
  20. * @var WPSEO_Database_Proxy
  21. */
  22. protected $database_proxy;
  23. /**
  24. * Holds the prefix of the table.
  25. *
  26. * @deprecated 7.4
  27. *
  28. * @var null|string
  29. */
  30. protected $table_prefix;
  31. /**
  32. * Initializes the database table.
  33. *
  34. * @param string $table_prefix Optional. Deprecated argument.
  35. */
  36. public function __construct( $table_prefix = null ) {
  37. if ( $table_prefix !== null ) {
  38. _deprecated_argument( __METHOD__, 'WPSEO 7.4' );
  39. }
  40. $this->database_proxy = new WPSEO_Database_Proxy( $GLOBALS['wpdb'], self::TABLE_NAME, true );
  41. }
  42. /**
  43. * Returns the table name to use.
  44. *
  45. * @return string The table name.
  46. */
  47. public function get_table_name() {
  48. return $this->database_proxy->get_table_name();
  49. }
  50. /**
  51. * Creates the database table.
  52. *
  53. * @return boolean True if the table was created, false if something went wrong.
  54. */
  55. public function install() {
  56. return $this->database_proxy->create_table(
  57. [
  58. 'object_id bigint(20) UNSIGNED NOT NULL',
  59. 'internal_link_count int(10) UNSIGNED NULL DEFAULT NULL',
  60. 'incoming_link_count int(10) UNSIGNED NULL DEFAULT NULL',
  61. ],
  62. [
  63. 'UNIQUE KEY object_id (object_id)',
  64. ]
  65. );
  66. }
  67. /**
  68. * Saves the link count to the database.
  69. *
  70. * @param int $meta_id The id to save the link count for.
  71. * @param array $meta_data The total amount of links.
  72. */
  73. public function save_meta_data( $meta_id, array $meta_data ) {
  74. $where = [ 'object_id' => $meta_id ];
  75. $saved = $this->database_proxy->upsert(
  76. array_merge( $where, $meta_data ),
  77. $where
  78. );
  79. if ( $saved === false ) {
  80. WPSEO_Meta_Table_Accessible::set_inaccessible();
  81. }
  82. }
  83. /**
  84. * Updates the incoming link count.
  85. *
  86. * @param array $post_ids The posts to update the incoming link count for.
  87. * @param WPSEO_Link_Storage $storage The link storage object.
  88. */
  89. public function update_incoming_link_count( array $post_ids, WPSEO_Link_Storage $storage ) {
  90. global $wpdb;
  91. $query = $wpdb->prepare(
  92. '
  93. SELECT COUNT( id ) AS incoming, target_post_id AS post_id
  94. FROM ' . $storage->get_table_name() . '
  95. WHERE target_post_id IN(' . implode( ',', array_fill( 0, count( $post_ids ), '%d' ) ) . ')
  96. GROUP BY target_post_id',
  97. $post_ids
  98. );
  99. $results = $wpdb->get_results( $query );
  100. $post_ids_non_zero = [];
  101. foreach ( $results as $result ) {
  102. $this->save_meta_data( $result->post_id, [ 'incoming_link_count' => $result->incoming ] );
  103. $post_ids_non_zero[] = $result->post_id;
  104. }
  105. $post_ids_zero = array_diff( $post_ids, $post_ids_non_zero );
  106. foreach ( $post_ids_zero as $post_id ) {
  107. $this->save_meta_data( $post_id, [ 'incoming_link_count' => 0 ] );
  108. }
  109. }
  110. }