| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions\Importing; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Importing action interface. |
| 10 |
*/ |
| 11 |
abstract class Abstract_Importing_Action implements Importing_Action_Interface { |
| 12 |
|
| 13 |
/** |
| 14 |
* The plugin the class deals with. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
const PLUGIN = null; |
| 19 |
|
| 20 |
/** |
| 21 |
* The type the class deals with. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
const TYPE = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* The options helper. |
| 29 |
* |
| 30 |
* @var Options_Helper |
| 31 |
*/ |
| 32 |
protected $options; |
| 33 |
|
| 34 |
/** |
| 35 |
* Abstract_Importing_Action constructor. |
| 36 |
* |
| 37 |
* @param Options_Helper $options The options helper. |
| 38 |
*/ |
| 39 |
public function __construct( Options_Helper $options ) { |
| 40 |
$this->options = $options; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* The name of the plugin we import from. |
| 45 |
* |
| 46 |
* @return string The plugin we import from. |
| 47 |
* |
| 48 |
* @throws Exception If the PLUGIN constant is not set in the child class. |
| 49 |
*/ |
| 50 |
public function get_plugin() { |
| 51 |
$class = get_class( $this ); |
| 52 |
$plugin = $class::PLUGIN; |
| 53 |
|
| 54 |
if ( $plugin === null ) { |
| 55 |
throw new Exception( 'Importing action without explicit plugin' ); |
| 56 |
} |
| 57 |
|
| 58 |
return $plugin; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* The data type we import from the plugin. |
| 63 |
* |
| 64 |
* @return string The data type we import from the plugin. |
| 65 |
* |
| 66 |
* @throws Exception If the TYPE constant is not set in the child class. |
| 67 |
*/ |
| 68 |
public function get_type() { |
| 69 |
$class = get_class( $this ); |
| 70 |
$type = $class::TYPE; |
| 71 |
|
| 72 |
if ( $type === null ) { |
| 73 |
throw new Exception( 'Importing action without explicit type' ); |
| 74 |
} |
| 75 |
|
| 76 |
return $type; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Can the current action import the data from plugin $plugin of type $type? |
| 81 |
* |
| 82 |
* @param string $plugin The plugin to import from. |
| 83 |
* @param string $type The type of data to import. |
| 84 |
* |
| 85 |
* @return bool True if this action can handle the combination of Plugin and Type. |
| 86 |
* |
| 87 |
* @throws Exception If the TYPE constant is not set in the child class. |
| 88 |
*/ |
| 89 |
public function is_compatible_with( $plugin = null, $type = null ) { |
| 90 |
if ( empty( $plugin ) && empty( $type ) ) { |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
if ( $plugin === $this->get_plugin() && empty( $type ) ) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
if ( empty( $plugin ) && $type === $this->get_type() ) { |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
if ( $plugin === $this->get_plugin() && $type === $this->get_type() ) { |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Gets the completed id (to be used as a key for the importing_completed option). |
| 111 |
* |
| 112 |
* @return string The completed id. |
| 113 |
*/ |
| 114 |
public function get_completed_id() { |
| 115 |
return $this->get_cursor_id(); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns the stored state of completedness. |
| 120 |
* |
| 121 |
* @return int The stored state of completedness. |
| 122 |
*/ |
| 123 |
public function get_completed() { |
| 124 |
$completed_id = $this->get_completed_id(); |
| 125 |
$importers_completions = $this->options->get( 'importing_completed', [] ); |
| 126 |
|
| 127 |
return ( isset( $importers_completions[ $completed_id ] ) ) ? $importers_completions[ $completed_id ] : false; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Stores the current state of completedness. |
| 132 |
* |
| 133 |
* @param bool $completed Whether the importer is completed. |
| 134 |
* |
| 135 |
* @return void. |
| 136 |
*/ |
| 137 |
public function set_completed( $completed ) { |
| 138 |
$completed_id = $this->get_completed_id(); |
| 139 |
$current_importers_completions = $this->options->get( 'importing_completed', [] ); |
| 140 |
|
| 141 |
$current_importers_completions[ $completed_id ] = $completed; |
| 142 |
$this->options->set( 'importing_completed', $current_importers_completions ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Returns whether the importing action is enabled. |
| 147 |
* |
| 148 |
* @return bool True by default unless a child class overrides it. |
| 149 |
*/ |
| 150 |
public function is_enabled() { |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Gets the cursor id. |
| 156 |
* |
| 157 |
* @return string The cursor id. |
| 158 |
*/ |
| 159 |
protected function get_cursor_id() { |
| 160 |
return $this->get_plugin() . '_' . $this->get_type(); |
| 161 |
} |
| 162 |
} |
| 163 |
|