| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
/** |
| 6 |
* The Import Cursor Helper. |
| 7 |
*/ |
| 8 |
class Import_Cursor_Helper { |
| 9 |
|
| 10 |
/** |
| 11 |
* The Options_Helper. |
| 12 |
* |
| 13 |
* @var Options_Helper |
| 14 |
*/ |
| 15 |
public $options; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class constructor. |
| 19 |
* |
| 20 |
* @param Options_Helper $options The options helper. |
| 21 |
*/ |
| 22 |
public function __construct( Options_Helper $options ) { |
| 23 |
$this->options = $options; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Returns the stored cursor value. |
| 28 |
* |
| 29 |
* @param string $cursor_id The cursor id. |
| 30 |
* @param mixed $default_value The default value if no cursor has been set yet. |
| 31 |
* |
| 32 |
* @return int The stored cursor value. |
| 33 |
*/ |
| 34 |
public function get_cursor( $cursor_id, $default_value = 0 ) { |
| 35 |
$import_cursors = $this->options->get( 'import_cursors', [] ); |
| 36 |
|
| 37 |
return ( isset( $import_cursors[ $cursor_id ] ) ) ? $import_cursors[ $cursor_id ] : $default_value; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Stores the current cursor value. |
| 42 |
* |
| 43 |
* @param string $cursor_id The cursor id. |
| 44 |
* @param int $last_imported_id The id of the lastly imported entry. |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function set_cursor( $cursor_id, $last_imported_id ) { |
| 49 |
$current_cursors = $this->options->get( 'import_cursors', [] ); |
| 50 |
|
| 51 |
if ( ! isset( $current_cursors[ $cursor_id ] ) || $current_cursors[ $cursor_id ] < $last_imported_id ) { |
| 52 |
$current_cursors[ $cursor_id ] = $last_imported_id; |
| 53 |
$this->options->set( 'import_cursors', $current_cursors ); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|