class-gsc-issue.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Google_Search_Console
  6. */
  7. /**
  8. * Class WPSEO_GSC_Issue
  9. */
  10. class WPSEO_GSC_Issue {
  11. /**
  12. * @var string
  13. */
  14. private $url;
  15. /**
  16. * @var DateTime
  17. */
  18. private $first_detected;
  19. /**
  20. * @var DateTime
  21. */
  22. private $last_crawled;
  23. /**
  24. * @var string
  25. */
  26. private $response_code;
  27. /**
  28. * Search Console issue class constructor.
  29. *
  30. * @param string $url URL of the issue.
  31. * @param DateTime $first_detected Time of first discovery.
  32. * @param DateTime $last_crawled Time of last crawl.
  33. * @param string $response_code HTTP response code.
  34. */
  35. public function __construct( $url, DateTime $first_detected, DateTime $last_crawled, $response_code ) {
  36. $this->url = $url;
  37. $this->first_detected = $first_detected;
  38. $this->last_crawled = $last_crawled;
  39. $this->response_code = $response_code;
  40. }
  41. /**
  42. * Put the class properties in array
  43. *
  44. * @return array
  45. */
  46. public function to_array() {
  47. return array(
  48. 'url' => $this->url,
  49. 'first_detected' => $this->to_date_format( $this->first_detected ),
  50. 'first_detected_raw' => $this->to_timestamp( $this->first_detected ),
  51. 'last_crawled' => $this->to_date_format( $this->last_crawled ),
  52. 'last_crawled_raw' => $this->to_timestamp( $this->last_crawled ),
  53. 'response_code' => $this->response_code,
  54. );
  55. }
  56. /**
  57. * Converting the date to a date format
  58. *
  59. * @param DateTime $date_to_convert Date instance.
  60. * @param string $format Format string.
  61. *
  62. * @return string
  63. */
  64. private function to_date_format( DateTime $date_to_convert, $format = '' ) {
  65. if ( empty( $format ) ) {
  66. $format = get_option( 'date_format' );
  67. }
  68. return date_i18n( $format, $date_to_convert->format( 'U' ) );
  69. }
  70. /**
  71. * Converting the date to a timestamp
  72. *
  73. * @param DateTime $date_to_convert Date object instance.
  74. *
  75. * @return string
  76. */
  77. private function to_timestamp( DateTime $date_to_convert ) {
  78. return $date_to_convert->format( 'U' );
  79. }
  80. }