| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions\Importing; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 6 |
|
| 7 |
/** |
| 8 |
* The Cursor Manager trait. |
| 9 |
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 10 |
*/ |
| 11 |
trait Import_Cursor_Manager_Trait { |
| 12 |
|
| 13 |
/** |
| 14 |
* Returns the stored cursor value. |
| 15 |
* |
| 16 |
* @param Options_Helper $options_helper The options helper. |
| 17 |
* @param string $cursor_id The cursor id. |
| 18 |
* @param mixed $default The default value if no cursor has been set yet. |
| 19 |
* |
| 20 |
* @return int The stored cursor value. |
| 21 |
*/ |
| 22 |
public function get_cursor( $options_helper, $cursor_id, $default = 0 ) { |
| 23 |
$import_cursors = $options_helper->get( 'import_cursors', [] ); |
| 24 |
|
| 25 |
return ( isset( $import_cursors[ $cursor_id ] ) ) ? $import_cursors[ $cursor_id ] : $default; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Stores the current cursor value. |
| 30 |
* |
| 31 |
* @param Options_Helper $options_helper The options helper. |
| 32 |
* @param string $cursor_id The cursor id. |
| 33 |
* @param int $last_imported_id The id of the lastly imported entry. |
| 34 |
* |
| 35 |
* @return void. |
| 36 |
*/ |
| 37 |
public function set_cursor( $options_helper, $cursor_id, $last_imported_id ) { |
| 38 |
$current_cursors = $options_helper->get( 'import_cursors', [] ); |
| 39 |
|
| 40 |
if ( ! isset( $current_cursors[ $cursor_id ] ) || $current_cursors[ $cursor_id ] < $last_imported_id ) { |
| 41 |
$current_cursors[ $cursor_id ] = $last_imported_id; |
| 42 |
$options_helper->set( 'import_cursors', $current_cursors ); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|