class-meta-storage.php 2.8 KB

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