/wp-content/plugins/wordpress-seo/admin/links/class-link-storage.php

https://bitbucket.org/carloskikea/helpet · PHP · 152 lines · 77 code · 19 blank · 56 comment · 4 complexity · c5505f2ef85bb01f8eafb3ea8be08f01 MD5 · raw file

  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 */
  15. protected $table_prefix;
  16. /**
  17. * Sets the table prefix.
  18. *
  19. * @param string $table_prefix Optional. The prefix to use for the table.
  20. */
  21. public function __construct( $table_prefix = null ) {
  22. if ( null === $table_prefix ) {
  23. $table_prefix = $GLOBALS['wpdb']->get_blog_prefix();
  24. }
  25. $this->table_prefix = $table_prefix;
  26. $this->database_proxy = new WPSEO_Database_Proxy( $GLOBALS['wpdb'], $this->get_table_name(), true );
  27. }
  28. /**
  29. * Returns the table name to use.
  30. *
  31. * @return string The table name.
  32. */
  33. public function get_table_name() {
  34. return $this->table_prefix . self::TABLE_NAME;
  35. }
  36. /**
  37. * Creates the database table.
  38. *
  39. * @return boolean True if the table was created, false if something went wrong.
  40. */
  41. public function install() {
  42. return $this->database_proxy->create_table(
  43. array(
  44. 'id bigint(20) unsigned NOT NULL AUTO_INCREMENT',
  45. 'url varchar(255) NOT NULL',
  46. 'post_id bigint(20) unsigned NOT NULL',
  47. 'target_post_id bigint(20) unsigned NOT NULL',
  48. 'type VARCHAR(8) NOT NULL',
  49. ),
  50. array(
  51. 'PRIMARY KEY (id)',
  52. 'KEY link_direction (post_id, type)',
  53. )
  54. );
  55. }
  56. /**
  57. * Returns an array of links from the database.
  58. *
  59. * @param int $post_id The post to get the links for.
  60. *
  61. * @return WPSEO_Link[] The links connected to the post.
  62. */
  63. public function get_links( $post_id ) {
  64. global $wpdb;
  65. $results = $this->database_proxy->get_results(
  66. $wpdb->prepare( '
  67. SELECT url, post_id, target_post_id, type
  68. FROM ' . $this->get_table_name() . '
  69. WHERE post_id = %d',
  70. $post_id
  71. )
  72. );
  73. if ( $this->database_proxy->has_error() ) {
  74. WPSEO_Link_Table_Accessible::set_inaccessible();
  75. }
  76. $links = array();
  77. foreach ( $results as $link ) {
  78. $links[] = WPSEO_Link_Factory::get_link( $link->url, $link->target_post_id, $link->type );
  79. }
  80. return $links;
  81. }
  82. /**
  83. * Walks the given links to save them.
  84. *
  85. * @param integer $post_id The post id to save.
  86. * @param WPSEO_Link[] $links The link to save.
  87. *
  88. * @return void
  89. */
  90. public function save_links( $post_id, array $links ) {
  91. array_walk( $links, array( $this, 'save_link' ), $post_id );
  92. }
  93. /**
  94. * Removes all records for given post_id.
  95. *
  96. * @param int $post_id The post_id to remove the records for.
  97. *
  98. * @return int|false The number of rows updated, or false on error.
  99. */
  100. public function cleanup( $post_id ) {
  101. $is_deleted = $this->database_proxy->delete(
  102. array( 'post_id' => $post_id ),
  103. array( '%d' )
  104. );
  105. if ( $is_deleted === false ) {
  106. WPSEO_Link_Table_Accessible::set_inaccessible();
  107. }
  108. return $is_deleted;
  109. }
  110. /**
  111. * Inserts the link into the database.
  112. *
  113. * @param WPSEO_Link $link The link to save.
  114. * @param int $link_key The link key. Unused.
  115. * @param int $post_id The post id to save the link for.
  116. *
  117. * @return void
  118. */
  119. protected function save_link( WPSEO_Link $link, $link_key, $post_id ) {
  120. $inserted = $this->database_proxy->insert(
  121. array(
  122. 'url' => $link->get_url(),
  123. 'post_id' => $post_id,
  124. 'target_post_id' => $link->get_target_post_id(),
  125. 'type' => $link->get_type(),
  126. ),
  127. array( '%s', '%d', '%d', '%s' )
  128. );
  129. if ( $inserted === false ) {
  130. WPSEO_Link_Table_Accessible::set_inaccessible();
  131. }
  132. }
  133. }