interfaces
2 years ago
sources
2 years ago
base-source-resolver.php
2 years ago
countries-source-resolver.php
2 years ago
days-source-resolver.php
2 years ago
module.php
2 years ago
months-source-resolver.php
2 years ago
module.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Bulk_Options; |
| 5 | |
| 6 | use Jet_Form_Builder\Admin\Editor; |
| 7 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 8 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 9 | use JFB_Components\Module\Base_Module_Dir_It; |
| 10 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 11 | use JFB_Components\Module\Base_Module_Handle_It; |
| 12 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 13 | use JFB_Components\Module\Base_Module_It; |
| 14 | use JFB_Components\Module\Base_Module_Url_It; |
| 15 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 16 | use JFB_Components\Repository\Interfaces\Repository_Pattern_Interface; |
| 17 | use JFB_Components\Repository\Repository_Pattern_Trait; |
| 18 | use JFB_Modules\Bulk_Options\Interfaces\Source_Resolve_Interface; |
| 19 | |
| 20 | // If this file is called directly, abort. |
| 21 | if ( ! defined( 'WPINC' ) ) { |
| 22 | die; |
| 23 | } |
| 24 | |
| 25 | final class Module implements |
| 26 | Base_Module_It, |
| 27 | Base_Module_Url_It, |
| 28 | Base_Module_Handle_It, |
| 29 | Base_Module_Dir_It, |
| 30 | Repository_Pattern_Interface { |
| 31 | |
| 32 | use Base_Module_Dir_Trait; |
| 33 | use Base_Module_Url_Trait; |
| 34 | use Base_Module_Handle_Trait; |
| 35 | use Repository_Pattern_Trait; |
| 36 | |
| 37 | public function rep_item_id() { |
| 38 | return 'bulk-options'; |
| 39 | } |
| 40 | |
| 41 | public function condition(): bool { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | public function init_hooks() { |
| 46 | add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_admin_assets' ) ); |
| 47 | } |
| 48 | |
| 49 | public function remove_hooks() { |
| 50 | remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_admin_assets' ) ); |
| 51 | } |
| 52 | |
| 53 | public function enqueue_admin_assets() { |
| 54 | $this->rep_install(); |
| 55 | |
| 56 | wp_localize_script( |
| 57 | Editor::EDITOR_PACKAGE_HANDLE, |
| 58 | 'JetFBBulkOptions', |
| 59 | array( |
| 60 | 'sources' => iterator_to_array( $this->resolve_all() ), |
| 61 | 'list' => iterator_to_array( $this->resolve_list_sources() ), |
| 62 | ) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | public function rep_instances(): array { |
| 67 | return array( |
| 68 | new Base_Source_Resolver(), |
| 69 | new Days_Source_Resolver(), |
| 70 | new Months_Source_Resolver(), |
| 71 | new Countries_Source_Resolver(), |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | public function resolve_all(): \Generator { |
| 76 | /** @var Source_Resolve_Interface $item */ |
| 77 | foreach ( $this->rep_generate_items() as $item ) { |
| 78 | yield $item->rep_item_id() => $this->resolve_source( $item ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public function resolve_list_sources(): \Generator { |
| 83 | /** @var Source_Resolve_Interface $item */ |
| 84 | foreach ( $this->rep_generate_items() as $item ) { |
| 85 | yield array( |
| 86 | 'label' => $item->get_label(), |
| 87 | 'value' => $item->rep_item_id(), |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public function resolve_source( Source_Resolve_Interface $source ): array { |
| 93 | return apply_filters( |
| 94 | "jet-form-builder/bulk-options/{$source->rep_item_id()}", |
| 95 | $source->resolve() |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | public function install( Source_Resolve_Interface $source ): bool { |
| 100 | return $this->rep_install_item_soft( $source ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param string $id |
| 105 | * |
| 106 | * @return Source_Resolve_Interface |
| 107 | * @throws Repository_Exception |
| 108 | */ |
| 109 | public function get_source( string $id ): Source_Resolve_Interface { |
| 110 | if ( ! $this->rep_isset_item( 'base' ) ) { |
| 111 | $this->rep_install(); |
| 112 | } |
| 113 | |
| 114 | return $this->rep_get_item( $id ); |
| 115 | } |
| 116 | } |
| 117 |