| 1 |
<?php |
| 2 |
|
| 3 |
namespace Barn2\Plugin\Posts_Table_Search_Sort; |
| 4 |
|
| 5 |
use Barn2\Plugin\Posts_Table_Search_Sort\Admin\Wizard\Starter; |
| 6 |
use Barn2\Plugin\Posts_Table_Search_Sort\Dependencies\Lib\Plugin\Plugin; |
| 7 |
use Barn2\Plugin\Posts_Table_Search_Sort\Dependencies\Lib\Plugin\Plugin_Activation_Listener; |
| 8 |
use Barn2\Plugin\Posts_Table_Search_Sort\Dependencies\Lib\Util as Lib_Util; |
| 9 |
use Barn2\Plugin\Posts_Table_Search_Sort\Dependencies\Lib\Registerable; |
| 10 |
use Barn2\Plugin\Posts_Table_Search_Sort\Dependencies\Lib\Service\Standard_Service; |
| 11 |
|
| 12 |
/** |
| 13 |
* Plugin Setup |
| 14 |
* |
| 15 |
* @package Barn2/posts-table-search-sort |
| 16 |
* @author Barn2 Plugins <info@barn2.com> |
| 17 |
* @license GPL-3.0 |
| 18 |
* @copyright Barn2 Media Ltd |
| 19 |
*/ |
| 20 |
class Plugin_Setup implements Registerable, Standard_Service { |
| 21 |
/** |
| 22 |
* Plugin's entry file |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $file; |
| 27 |
|
| 28 |
/** |
| 29 |
* Plugin instance |
| 30 |
* |
| 31 |
* @var Plugin |
| 32 |
*/ |
| 33 |
private $plugin; |
| 34 |
|
| 35 |
/** |
| 36 |
* Wizard starter. |
| 37 |
* |
| 38 |
* @var Starter |
| 39 |
*/ |
| 40 |
private $starter; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor. |
| 44 |
* |
| 45 |
* @param mixed $file |
| 46 |
* @param Plugin $plugin |
| 47 |
*/ |
| 48 |
public function __construct( $file, Plugin $plugin ) { |
| 49 |
$this->file = $file; |
| 50 |
$this->plugin = $plugin; |
| 51 |
$this->starter = new Starter( $this->plugin ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Register the service. |
| 56 |
*/ |
| 57 |
public function register() { |
| 58 |
register_activation_hook( $this->file, [ $this, 'on_activate' ] ); |
| 59 |
add_action( 'admin_init', [ $this, 'after_plugin_activation' ] ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* On activation. |
| 64 |
* |
| 65 |
* @param mixed $network_wide |
| 66 |
*/ |
| 67 |
public function on_activate( $network_wide ) { |
| 68 |
if ( $this->starter->should_start() ) { |
| 69 |
$this->starter->create_transient(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Do nothing. |
| 75 |
* |
| 76 |
* @param bool $network_wide |
| 77 |
*/ |
| 78 |
public function on_deactivate( $network_wide ) { |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Detect the transient and redirect to wizard. |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function after_plugin_activation() { |
| 87 |
|
| 88 |
if ( ! $this->starter->detected() ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$this->starter->delete_transient(); |
| 93 |
$this->starter->redirect(); |
| 94 |
} |
| 95 |
} |
| 96 |
|