| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Components\Module; |
| 5 |
|
| 6 |
// If this file is called directly, abort. |
| 7 |
use JFB_Components\Repository\Repository_Pattern_Trait; |
| 8 |
use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 9 |
|
| 10 |
if ( ! defined( 'WPINC' ) ) { |
| 11 |
die; |
| 12 |
} |
| 13 |
|
| 14 |
trait Module_Controller_Trait { |
| 15 |
|
| 16 |
use Repository_Pattern_Trait; |
| 17 |
|
| 18 |
public function init_hooks() { |
| 19 |
/** @var Base_Module_It $item */ |
| 20 |
foreach ( $this->rep_generate_items() as $item ) { |
| 21 |
$item->init_hooks(); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @param string $name_or_class |
| 27 |
* |
| 28 |
* @return Base_Module_It|Base_Module_Handle_It|Base_Module_Url_It|Base_Module_Dir_It|Base_Module_After_Install_It |
| 29 |
* @throws Repository_Exception |
| 30 |
*/ |
| 31 |
public function module( string $name_or_class ): Base_Module_It { |
| 32 |
return $this->rep_get_item( $name_or_class ); |
| 33 |
} |
| 34 |
|
| 35 |
public function has_module( string $name_or_class ): bool { |
| 36 |
try { |
| 37 |
$this->module( $name_or_class ); |
| 38 |
|
| 39 |
return true; |
| 40 |
} catch ( Repository_Exception $exception ) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
public function install( Base_Module_It $item ): bool { |
| 46 |
return $this->rep_install_item_soft( $item ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @param Base_Module_It|string $item |
| 51 |
*/ |
| 52 |
public function uninstall( $item ) { |
| 53 |
if ( is_object( $item ) ) { |
| 54 |
$item = $item->rep_item_id(); |
| 55 |
} |
| 56 |
try { |
| 57 |
$module = $this->module( $item ); |
| 58 |
} catch ( Repository_Exception $exception ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
$module->remove_hooks(); |
| 62 |
|
| 63 |
if ( $module instanceof Base_Module_After_Install_It ) { |
| 64 |
$module->on_uninstall(); |
| 65 |
} |
| 66 |
|
| 67 |
$this->rep_remove( $module ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @param $item Base_Module_It |
| 72 |
* |
| 73 |
* @throws Repository_Exception |
| 74 |
*/ |
| 75 |
public function rep_before_install_item( $item ) { |
| 76 |
if ( |
| 77 |
! is_object( $item ) || |
| 78 |
! ( $item instanceof Base_Module_It ) || |
| 79 |
! $item->condition() |
| 80 |
) { |
| 81 |
$this->_rep_abort_this(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @param $item Base_Module_It |
| 87 |
*/ |
| 88 |
public function rep_after_install_item( $item ) { |
| 89 |
if ( $item instanceof Base_Module_After_Install_It ) { |
| 90 |
$item->on_install(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|