class-link-table-accessible.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Links
  6. */
  7. /**
  8. * Represents the state of the table being accessible.
  9. */
  10. class WPSEO_Link_Table_Accessible {
  11. /**
  12. * Constant used to determine whether the link table is accessible.
  13. *
  14. * @var string
  15. */
  16. const ACCESSIBLE = '0';
  17. /**
  18. * Constant used to determine whether the link table is inaccessible.
  19. *
  20. * @var string
  21. */
  22. const INACCESSBILE = '1';
  23. /**
  24. * Checks if the given table name exists.
  25. *
  26. * @return bool True when table is accessible.
  27. */
  28. public static function is_accessible() {
  29. $value = get_transient( self::transient_name() );
  30. // If the value is not set, check the table.
  31. if ( false === $value ) {
  32. return self::check_table();
  33. }
  34. return $value === self::ACCESSIBLE;
  35. }
  36. /**
  37. * Sets the transient value to 1, to indicate the table is not accessible.
  38. *
  39. * @return void
  40. */
  41. public static function set_inaccessible() {
  42. set_transient( self::transient_name(), self::INACCESSBILE, HOUR_IN_SECONDS );
  43. }
  44. /**
  45. * Removes the transient.
  46. *
  47. * @return void
  48. */
  49. public static function cleanup() {
  50. delete_transient( self::transient_name() );
  51. }
  52. /**
  53. * Sets the transient value to 0, to indicate the table is accessible.
  54. *
  55. * @return void
  56. */
  57. protected static function set_accessible() {
  58. /*
  59. * Prefer to set a 0 timeout, but if the timeout was set before WordPress will not delete the transient
  60. * correctly when overridden with a zero value.
  61. *
  62. * Setting a YEAR_IN_SECONDS instead.
  63. */
  64. set_transient( self::transient_name(), self::ACCESSIBLE, YEAR_IN_SECONDS );
  65. }
  66. /**
  67. * Checks if the table exists if not, set the transient to indicate the inaccessible table.
  68. *
  69. * @return bool True if table is accessible.
  70. */
  71. protected static function check_table() {
  72. global $wpdb;
  73. $storage = new WPSEO_Link_Storage();
  74. $query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $storage->get_table_name() );
  75. if ( $wpdb->get_var( $query ) !== $storage->get_table_name() ) {
  76. self::set_inaccessible();
  77. return false;
  78. }
  79. self::set_accessible();
  80. return true;
  81. }
  82. /**
  83. * Returns the name of the transient.
  84. *
  85. * @return string The name of the transient to use.
  86. */
  87. protected static function transient_name() {
  88. return 'wpseo_link_table_inaccessible';
  89. }
  90. }