class-meta-table-accessible.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_Meta_Table_Accessible {
  11. const ACCESSIBLE = '0';
  12. const INACCESSBILE = '1';
  13. /**
  14. * Checks if the given table name exists.
  15. *
  16. * @return bool True when table is accessible.
  17. */
  18. public static function is_accessible() {
  19. $value = get_transient( self::transient_name() );
  20. // If the value is not set, check the table.
  21. if ( false === $value ) {
  22. return self::check_table();
  23. }
  24. return $value === self::ACCESSIBLE;
  25. }
  26. /**
  27. * Sets the transient value to 1, to indicate the table is not accessible.
  28. *
  29. * @return void
  30. */
  31. public static function set_inaccessible() {
  32. set_transient( self::transient_name(), self::INACCESSBILE, HOUR_IN_SECONDS );
  33. }
  34. /**
  35. * Removes the transient.
  36. *
  37. * @return void
  38. */
  39. public static function cleanup() {
  40. delete_transient( self::transient_name() );
  41. }
  42. /**
  43. * Sets the transient value to 0, to indicate the table is accessible.
  44. *
  45. * @return void
  46. */
  47. protected static function set_accessible() {
  48. /*
  49. * Prefer to set a 0 timeout, but if the timeout was set before WordPress will not delete the transient
  50. * correctly when overridden with a zero value.
  51. *
  52. * Setting a YEAR_IN_SECONDS instead.
  53. */
  54. set_transient( self::transient_name(), self::ACCESSIBLE, YEAR_IN_SECONDS );
  55. }
  56. /**
  57. * Checks if the table exists if not, set the transient to indicate the inaccessible table.
  58. *
  59. * @return bool True if table is accessible.
  60. */
  61. protected static function check_table() {
  62. global $wpdb;
  63. $storage = new WPSEO_Meta_Storage();
  64. $query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $storage->get_table_name() );
  65. if ( $wpdb->get_var( $query ) !== $storage->get_table_name() ) {
  66. self::set_inaccessible();
  67. return false;
  68. }
  69. self::set_accessible();
  70. return true;
  71. }
  72. /**
  73. * Returns the name of the transient.
  74. *
  75. * @return string The name of the transient to use.
  76. */
  77. protected static function transient_name() {
  78. return 'wpseo_meta_table_inaccessible';
  79. }
  80. /**
  81. * Checks if the table exists if not, set the transient to indicate the inaccessible table.
  82. *
  83. * @deprecated 6.0
  84. *
  85. * @return bool True if table is accessible.
  86. */
  87. public static function check_table_is_accessible() {
  88. _deprecated_function( __FUNCTION__, '6.0', __CLASS__ . '::is_accessible' );
  89. return self::is_accessible();
  90. }
  91. }