| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Services\Importing; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Actions\Importing\Importing_Action_Interface; |
| 6 |
|
| 7 |
/** |
| 8 |
* Detects if any data from other SEO plugins is available for importing. |
| 9 |
*/ |
| 10 |
class Importable_Detector_Service { |
| 11 |
|
| 12 |
/** |
| 13 |
* All known import actions |
| 14 |
* |
| 15 |
* @var array|Importing_Action_Interface[] |
| 16 |
*/ |
| 17 |
protected $importers; |
| 18 |
|
| 19 |
/** |
| 20 |
* Importable_Detector_Service constructor. |
| 21 |
* |
| 22 |
* @param Importing_Action_Interface ...$importers All of the known importers. |
| 23 |
*/ |
| 24 |
public function __construct( Importing_Action_Interface ...$importers ) { |
| 25 |
$this->importers = $importers; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Returns the detected importers that have data to work with. |
| 30 |
* |
| 31 |
* @param string $plugin The plugin name of the importer. |
| 32 |
* @param string $type The type of the importer. |
| 33 |
* |
| 34 |
* @return array The detected importers that have data to work with. |
| 35 |
*/ |
| 36 |
public function detect_importers( $plugin = null, $type = null ) { |
| 37 |
$detectors = $this->filter_actions( $this->importers, $plugin, $type ); |
| 38 |
|
| 39 |
$detected = []; |
| 40 |
foreach ( $detectors as $detector ) { |
| 41 |
if ( $detector->is_enabled() && $detector->get_type() !== 'cleanup' && ! $detector->get_completed() && $detector->get_limited_unindexed_count( 1 ) > 0 ) { |
| 42 |
$detected[ $detector->get_plugin() ][] = $detector->get_type(); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
return $detected; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Returns the detected cleanups that have data to work with. |
| 51 |
* |
| 52 |
* @param string $plugin The plugin name of the cleanup. |
| 53 |
* |
| 54 |
* @return array The detected importers that have data to work with. |
| 55 |
*/ |
| 56 |
public function detect_cleanups( $plugin = null ) { |
| 57 |
$detectors = $this->filter_actions( $this->importers, $plugin, 'cleanup' ); |
| 58 |
|
| 59 |
$detected = []; |
| 60 |
foreach ( $detectors as $detector ) { |
| 61 |
if ( $detector->is_enabled() && ! $detector->get_completed() && $detector->get_limited_unindexed_count( 1 ) > 0 ) { |
| 62 |
$detected[ $detector->get_plugin() ][] = $detector->get_type(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return $detected; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Filters all import actions from a list that do not match the given Plugin or Type. |
| 71 |
* |
| 72 |
* @param Importing_Action_Interface[] $all_actions The complete list of actions. |
| 73 |
* @param string $plugin The Plugin name whose actions to keep. |
| 74 |
* @param string $type The type of actions to keep. |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function filter_actions( $all_actions, $plugin = null, $type = null ) { |
| 79 |
return \array_filter( |
| 80 |
$all_actions, |
| 81 |
function( $action ) use ( $plugin, $type ) { |
| 82 |
return $action->is_compatible_with( $plugin, $type ); |
| 83 |
} |
| 84 |
); |
| 85 |
} |
| 86 |
} |
| 87 |
|