class-recalculate.php 2.1 KB

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