class-link-storage.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Links
  6. */
  7. /**
  8. * Represents the storage of an seo link.
  9. */
  10. class WPSEO_Link_Storage implements WPSEO_Installable {
  11. /**
  12. * Table name for the link storage.
  13. *
  14. * @var string
  15. */
  16. const TABLE_NAME = 'yoast_seo_links';
  17. /**
  18. * An instance of the database proxy class.
  19. *
  20. * @var WPSEO_Database_Proxy
  21. */
  22. protected $database_proxy;
  23. /**
  24. * Holds the prefix of the database 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. 'id bigint(20) unsigned NOT NULL AUTO_INCREMENT',
  59. 'url varchar(255) NOT NULL',
  60. 'post_id bigint(20) unsigned NOT NULL',
  61. 'target_post_id bigint(20) unsigned NOT NULL',
  62. 'type VARCHAR(8) NOT NULL',
  63. ],
  64. [
  65. 'PRIMARY KEY (id)',
  66. 'KEY link_direction (post_id, type)',
  67. ]
  68. );
  69. }
  70. /**
  71. * Returns an array of links from the database.
  72. *
  73. * @param int $post_id The post to get the links for.
  74. *
  75. * @return WPSEO_Link[] The links connected to the post.
  76. */
  77. public function get_links( $post_id ) {
  78. global $wpdb;
  79. $results = $this->database_proxy->get_results(
  80. $wpdb->prepare(
  81. '
  82. SELECT url, post_id, target_post_id, type
  83. FROM ' . $this->get_table_name() . '
  84. WHERE post_id = %d',
  85. $post_id
  86. )
  87. );
  88. if ( $this->database_proxy->has_error() ) {
  89. WPSEO_Link_Table_Accessible::set_inaccessible();
  90. }
  91. $links = [];
  92. foreach ( $results as $link ) {
  93. $links[] = WPSEO_Link_Factory::get_link( $link->url, $link->target_post_id, $link->type );
  94. }
  95. return $links;
  96. }
  97. /**
  98. * Walks the given links to save them.
  99. *
  100. * @param integer $post_id The post id to save.
  101. * @param WPSEO_Link[] $links The link to save.
  102. *
  103. * @return void
  104. */
  105. public function save_links( $post_id, array $links ) {
  106. array_walk( $links, [ $this, 'save_link' ], $post_id );
  107. }
  108. /**
  109. * Removes all records for given post_id.
  110. *
  111. * @param int $post_id The post_id to remove the records for.
  112. *
  113. * @return int|false The number of rows updated, or false on error.
  114. */
  115. public function cleanup( $post_id ) {
  116. $is_deleted = $this->database_proxy->delete(
  117. [ 'post_id' => $post_id ],
  118. [ '%d' ]
  119. );
  120. if ( $is_deleted === false ) {
  121. WPSEO_Link_Table_Accessible::set_inaccessible();
  122. }
  123. return $is_deleted;
  124. }
  125. /**
  126. * Inserts the link into the database.
  127. *
  128. * @param WPSEO_Link $link The link to save.
  129. * @param int $link_key The link key. Unused.
  130. * @param int $post_id The post id to save the link for.
  131. *
  132. * @return void
  133. */
  134. protected function save_link( WPSEO_Link $link, $link_key, $post_id ) {
  135. $inserted = $this->database_proxy->insert(
  136. [
  137. 'url' => $link->get_url(),
  138. 'post_id' => $post_id,
  139. 'target_post_id' => $link->get_target_post_id(),
  140. 'type' => $link->get_type(),
  141. ],
  142. [ '%s', '%d', '%d', '%s' ]
  143. );
  144. if ( $inserted === false ) {
  145. WPSEO_Link_Table_Accessible::set_inaccessible();
  146. }
  147. }
  148. }