class-gsc-settings.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Google_Search_Console
  6. */
  7. /**
  8. * Class WPSEO_GSC_Settings
  9. */
  10. class WPSEO_GSC_Settings {
  11. /**
  12. * Clear all data from the database
  13. *
  14. * @param WPSEO_GSC_Service $service Service class instance.
  15. */
  16. public static function clear_data( WPSEO_GSC_Service $service ) {
  17. // Remove issue and issue counts.
  18. self::remove();
  19. // Removes the GSC options.
  20. self::remove_gsc_option();
  21. // Clear the service data.
  22. $service->clear_data();
  23. }
  24. /**
  25. * Reloading all the issues
  26. */
  27. public static function reload_issues() {
  28. // Remove issue and issue counts.
  29. self::remove();
  30. }
  31. /**
  32. * When authorization is successful return true, otherwise false
  33. *
  34. * @param string $authorization_code Code to validate.
  35. * @param Yoast_Api_Google_Client $client Client object instance.
  36. *
  37. * @return bool
  38. */
  39. public static function validate_authorization( $authorization_code, Yoast_Api_Google_Client $client ) {
  40. return ( $authorization_code !== '' && $client->authenticate_client( $authorization_code ) );
  41. }
  42. /**
  43. * Get the GSC profile
  44. *
  45. * @return string
  46. */
  47. public static function get_profile() {
  48. // Get option.
  49. $option = get_option( WPSEO_GSC::OPTION_WPSEO_GSC, array( 'profile' => '' ) );
  50. // Set the profile.
  51. $profile = '';
  52. if ( ! empty( $option['profile'] ) ) {
  53. $profile = $option['profile'];
  54. }
  55. // Return the profile.
  56. return trim( $profile, '/' );
  57. }
  58. /**
  59. * Removes the issue counts and all the issues from the options
  60. */
  61. private static function remove() {
  62. // Remove the issue counts from the options.
  63. self::remove_issue_counts();
  64. // Removing all issues from the database.
  65. self::remove_issues();
  66. }
  67. /**
  68. * Remove the issue counts
  69. */
  70. private static function remove_issue_counts() {
  71. // Remove the options which are holding the counts.
  72. delete_option( WPSEO_GSC_Count::OPTION_CI_COUNTS );
  73. delete_option( WPSEO_GSC_Count::OPTION_CI_LAST_FETCH );
  74. }
  75. /**
  76. * Delete the issues and their meta data from the database
  77. */
  78. private static function remove_issues() {
  79. global $wpdb;
  80. // Remove local crawl issues by running a delete query.
  81. $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'wpseo-gsc-issues-%'" );
  82. }
  83. /**
  84. * Removes the options for GSC
  85. */
  86. private static function remove_gsc_option() {
  87. delete_option( WPSEO_GSC::OPTION_WPSEO_GSC );
  88. }
  89. }