class-link-storage.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. const TABLE_NAME = 'yoast_seo_links';
  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. 'id bigint(20) unsigned NOT NULL AUTO_INCREMENT',
  44. 'url varchar(255) NOT NULL',
  45. 'post_id bigint(20) unsigned NOT NULL',
  46. 'target_post_id bigint(20) unsigned NOT NULL',
  47. 'type VARCHAR(8) NOT NULL',
  48. ),
  49. array(
  50. 'PRIMARY KEY (id)',
  51. 'KEY link_direction (post_id, type)',
  52. )
  53. );
  54. }
  55. /**
  56. * Returns an array of links from the database.
  57. *
  58. * @param int $post_id The post to get the links for.
  59. *
  60. * @return WPSEO_Link[] The links connected to the post.
  61. */
  62. public function get_links( $post_id ) {
  63. global $wpdb;
  64. $results = $this->database_proxy->get_results(
  65. $wpdb->prepare( '
  66. SELECT url, post_id, target_post_id, type
  67. FROM ' . $this->get_table_name() . '
  68. WHERE post_id = %d',
  69. $post_id
  70. )
  71. );
  72. if ( $this->database_proxy->has_error() ) {
  73. WPSEO_Link_Table_Accessible::set_inaccessible();
  74. }
  75. $links = array();
  76. foreach ( $results as $link ) {
  77. $links[] = WPSEO_Link_Factory::get_link( $link->url, $link->target_post_id, $link->type );
  78. }
  79. return $links;
  80. }
  81. /**
  82. * Walks the given links to save them.
  83. *
  84. * @param integer $post_id The post id to save.
  85. * @param WPSEO_Link[] $links The link to save.
  86. *
  87. * @return void
  88. */
  89. public function save_links( $post_id, array $links ) {
  90. array_walk( $links, array( $this, 'save_link' ), $post_id );
  91. }
  92. /**
  93. * Removes all records for given post_id.
  94. *
  95. * @param int $post_id The post_id to remove the records for.
  96. *
  97. * @return int|false The number of rows updated, or false on error.
  98. */
  99. public function cleanup( $post_id ) {
  100. $is_deleted = $this->database_proxy->delete(
  101. array( 'post_id' => $post_id ),
  102. array( '%d' )
  103. );
  104. if ( $is_deleted === false ) {
  105. WPSEO_Link_Table_Accessible::set_inaccessible();
  106. }
  107. return $is_deleted;
  108. }
  109. /**
  110. * Inserts the link into the database.
  111. *
  112. * @param WPSEO_Link $link The link to save.
  113. * @param int $link_key The link key. Unused.
  114. * @param int $post_id The post id to save the link for.
  115. *
  116. * @return void
  117. */
  118. protected function save_link( WPSEO_Link $link, $link_key, $post_id ) {
  119. $inserted = $this->database_proxy->insert(
  120. array(
  121. 'url' => $link->get_url(),
  122. 'post_id' => $post_id,
  123. 'target_post_id' => $link->get_target_post_id(),
  124. 'type' => $link->get_type(),
  125. ),
  126. array( '%s', '%d', '%d', '%s' )
  127. );
  128. if ( $inserted === false ) {
  129. WPSEO_Link_Table_Accessible::set_inaccessible();
  130. }
  131. }
  132. }