class-recalculate.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Abstract class to force methods in recalculate classes.
  9. */
  10. abstract class WPSEO_Recalculate {
  11. /**
  12. * @var int
  13. */
  14. protected $items_per_page = 20;
  15. /**
  16. * Saves the array with scores to the database.
  17. *
  18. * @param array $scores Array with the score for each item.
  19. */
  20. abstract public function save_scores( array $scores );
  21. /**
  22. * Gets the items and parses it to an response
  23. *
  24. * @param integer $paged The current page number.
  25. *
  26. * @return string
  27. */
  28. abstract protected function get_items( $paged );
  29. /**
  30. * Maps the items to an array for the response
  31. *
  32. * @param mixed $item Object with data to parse.
  33. *
  34. * @return array
  35. */
  36. abstract protected function item_to_response( $item );
  37. /**
  38. * Gets the items to recalculate
  39. *
  40. * @param int $paged The current page number.
  41. *
  42. * @return array Items that can be recalculated.
  43. */
  44. public function get_items_to_recalculate( $paged ) {
  45. $return = array();
  46. $paged = abs( $paged );
  47. $items = $this->get_items( $paged );
  48. $return['items'] = $this->parse_items( $items );
  49. $return['total_items'] = count( $items );
  50. if ( $return['total_items'] >= $this->items_per_page ) {
  51. $return['next_page'] = ( $paged + 1 );
  52. }
  53. return $return;
  54. }
  55. /**
  56. * Parse the posts|terms with the value we need
  57. *
  58. * @param array $items The items to parse.
  59. *
  60. * @return array
  61. */
  62. protected function parse_items( array $items ) {
  63. $return = array();
  64. foreach ( $items as $item ) {
  65. $response = $this->item_to_response( $item );
  66. if ( ! empty( $response ) ) {
  67. $return[] = $response;
  68. }
  69. }
  70. return $return;
  71. }
  72. /**
  73. * Get default from the options for given field
  74. *
  75. * @param string $field The field for which to get the default options.
  76. * @param string $suffix The post type.
  77. *
  78. * @return bool|string
  79. */
  80. protected function default_from_options( $field, $suffix ) {
  81. $target_option_field = $field . '-' . $suffix;
  82. if ( '' !== WPSEO_Options::get( $target_option_field, '' ) ) {
  83. return WPSEO_Options::get( $target_option_field );
  84. }
  85. return false;
  86. }
  87. }