.config
2 years ago
action-types
2 years ago
admin
2 years ago
assets
2 years ago
constraints
2 years ago
export
2 years ago
models
2 years ago
query-views
2 years ago
rest-endpoints
2 years ago
.babelrc
2 years ago
.gitattributes
2 years ago
controller.php
2 years ago
module.php
2 years ago
package.json
2 years ago
records-rest-controller.php
2 years ago
tools.php
2 years ago
module.php
236 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Form_Record; |
| 5 | |
| 6 | use Jet_Form_Builder\Actions\Manager; |
| 7 | use Jet_Form_Builder\Admin\Single_Pages\Base_Single_Page; |
| 8 | use Jet_Form_Builder\Admin\Single_Pages\Meta_Containers\Base_Meta_Container; |
| 9 | use Jet_Form_Builder\Admin\Single_Pages\Meta_Containers\Side_Meta_Container; |
| 10 | use Jet_Form_Builder\Classes\Post\Post_Tools; |
| 11 | use Jet_Form_Builder\Exceptions\Handler_Exception; |
| 12 | use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 13 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 14 | use JFB_Components\Admin\Print_Page\Header; |
| 15 | use JFB_Modules\Form_Record\Admin\Meta_Boxes\Form_Record_Print_Values_Box; |
| 16 | use JFB_Modules\Form_Record\Admin\Pages\Export_Page; |
| 17 | use JFB_Modules\Form_Record\Admin\Pages\Print_Page; |
| 18 | use JFB_Modules\Form_Record\Admin\Pages\Single_Form_Record_Print_Page; |
| 19 | use JFB_Modules\Form_Record\Query_Views\Record_View; |
| 20 | use JFB_Modules\Gateways\Scenarios_Abstract\Scenario_Logic_Base; |
| 21 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 22 | use JFB_Components\Module\Base_Module_Dir_It; |
| 23 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 24 | use JFB_Components\Module\Base_Module_Handle_It; |
| 25 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 26 | use JFB_Components\Module\Base_Module_It; |
| 27 | use JFB_Components\Module\Base_Module_Url_It; |
| 28 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 29 | use JFB_Modules\Form_Record\Action_Types\Save_Record; |
| 30 | use JFB_Modules\Form_Record\Admin\Meta_Boxes\Record_To_Payment_Box; |
| 31 | use JFB_Modules\Form_Record\Admin\Pages\Form_Records; |
| 32 | use JFB_Modules\Form_Record\Admin\Pages\Single_Form_Record_Page; |
| 33 | use JFB_Modules\Dev; |
| 34 | |
| 35 | // If this file is called directly, abort. |
| 36 | if ( ! defined( 'WPINC' ) ) { |
| 37 | die; |
| 38 | } |
| 39 | |
| 40 | final class Module implements |
| 41 | Base_Module_It, |
| 42 | Base_Module_Url_It, |
| 43 | Base_Module_Dir_It, |
| 44 | Base_Module_After_Install_It, |
| 45 | Base_Module_Handle_It { |
| 46 | |
| 47 | use Base_Module_Handle_Trait; |
| 48 | use Base_Module_Url_Trait; |
| 49 | use Base_Module_Dir_Trait; |
| 50 | |
| 51 | private $rest; |
| 52 | |
| 53 | public function rep_item_id() { |
| 54 | return 'form-record'; |
| 55 | } |
| 56 | |
| 57 | public function condition(): bool { |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | public function on_install() { |
| 62 | $this->rest = new Records_Rest_Controller(); |
| 63 | } |
| 64 | |
| 65 | public function on_uninstall() { |
| 66 | unset( $this->rest ); |
| 67 | } |
| 68 | |
| 69 | public function init_hooks() { |
| 70 | // actions |
| 71 | add_action( |
| 72 | 'rest_api_init', |
| 73 | array( $this->get_rest(), 'register_routes' ) |
| 74 | ); |
| 75 | add_action( |
| 76 | 'jet-form-builder/actions/register', |
| 77 | array( $this, 'register_actions' ) |
| 78 | ); |
| 79 | add_action( |
| 80 | 'jet-form-builder/gateways/before-send', |
| 81 | array( $this, 'before_send_gateway' ), |
| 82 | 10, |
| 83 | 3 |
| 84 | ); |
| 85 | add_action( |
| 86 | 'jet-form-builder/before-print-page/header', |
| 87 | array( $this, 'before_print_page' ), |
| 88 | 10, |
| 89 | 2 |
| 90 | ); |
| 91 | |
| 92 | // filters |
| 93 | add_filter( |
| 94 | 'jet-form-builder/admin/pages', |
| 95 | array( $this, 'add_admin_pages' ) |
| 96 | ); |
| 97 | add_filter( |
| 98 | 'jet-form-builder/admin/single-pages', |
| 99 | array( $this, 'add_single_admin_pages' ) |
| 100 | ); |
| 101 | add_filter( |
| 102 | 'jet-form-builder/page-containers/jfb-payments-single', |
| 103 | array( $this, 'add_box_to_single_payment' ) |
| 104 | ); |
| 105 | add_filter( |
| 106 | 'jet-form-builder/admin/action-pages', |
| 107 | array( $this, 'add_action_admin_pages' ) |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | public function remove_hooks() { |
| 112 | // actions |
| 113 | remove_action( |
| 114 | 'rest_api_init', |
| 115 | array( $this->get_rest(), 'register_routes' ) |
| 116 | ); |
| 117 | remove_action( |
| 118 | 'jet-form-builder/actions/register', |
| 119 | array( $this, 'register_actions' ) |
| 120 | ); |
| 121 | remove_action( |
| 122 | 'jet-form-builder/gateways/before-send', |
| 123 | array( $this, 'before_send_gateway' ) |
| 124 | ); |
| 125 | remove_action( |
| 126 | 'jet-form-builder/before-print-page/header', |
| 127 | array( $this, 'before_print_page' ) |
| 128 | ); |
| 129 | |
| 130 | // filters |
| 131 | remove_filter( |
| 132 | 'jet-form-builder/admin/pages', |
| 133 | array( $this, 'add_admin_pages' ) |
| 134 | ); |
| 135 | remove_filter( |
| 136 | 'jet-form-builder/admin/single-pages', |
| 137 | array( $this, 'add_single_admin_pages' ) |
| 138 | ); |
| 139 | remove_filter( |
| 140 | 'jet-form-builder/page-containers/jfb-payments-single', |
| 141 | array( $this, 'add_box_to_single_payment' ) |
| 142 | ); |
| 143 | remove_filter( |
| 144 | 'jet-form-builder/admin/action-pages', |
| 145 | array( $this, 'add_action_admin_pages' ) |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | public function register_actions( Manager $manager ) { |
| 150 | $manager->register_action_type( new Save_Record() ); |
| 151 | } |
| 152 | |
| 153 | public function add_admin_pages( array $pages ): array { |
| 154 | $pages[] = new Form_Records(); |
| 155 | |
| 156 | return $pages; |
| 157 | } |
| 158 | |
| 159 | public function add_single_admin_pages( array $pages ): array { |
| 160 | $pages[] = new Single_Form_Record_Page(); |
| 161 | |
| 162 | return $pages; |
| 163 | } |
| 164 | |
| 165 | public function add_action_admin_pages( array $pages ): array { |
| 166 | array_push( |
| 167 | $pages, |
| 168 | new Export_Page(), |
| 169 | new Print_Page() |
| 170 | ); |
| 171 | |
| 172 | return $pages; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @param Header $header |
| 177 | * @param Base_Single_Page $page |
| 178 | */ |
| 179 | public function before_print_page( Header $header, Base_Single_Page $page ) { |
| 180 | if ( ! ( $page instanceof Single_Form_Record_Print_Page ) ) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | try { |
| 185 | $record = Record_View::findById( $page->get_id() ); |
| 186 | } catch ( Query_Builder_Exception $exception ) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | $form_title = Post_Tools::get_title( $record['form_id'] ); |
| 191 | |
| 192 | $header->set_title( |
| 193 | sprintf( |
| 194 | /* translators: %s - form title */ |
| 195 | __( '%s ‹ JetFormBuilder Record', 'jet-form-builder' ), |
| 196 | $form_title |
| 197 | ) |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @param Base_Meta_Container[] $containers |
| 203 | * |
| 204 | * @return array |
| 205 | */ |
| 206 | public function add_box_to_single_payment( array $containers ): array { |
| 207 | $containers[1]->add_meta_box( new Record_To_Payment_Box() ); |
| 208 | |
| 209 | return $containers; |
| 210 | } |
| 211 | |
| 212 | public function before_send_gateway( $status, $action_error, Scenario_Logic_Base $scenario ) { |
| 213 | // prepare record controller, for saving errors & actions |
| 214 | $record = $scenario->get_scenario_row( 'record' ); |
| 215 | $controller = ( new Controller() )->set_record_id( $record['id'] ); |
| 216 | $controller->set_setting( |
| 217 | 'save_errors', |
| 218 | jet_form_builder()->has_module( Dev\Module::class ) |
| 219 | ); |
| 220 | |
| 221 | try { |
| 222 | $controller->save_fields(); |
| 223 | $controller->save_actions(); |
| 224 | $controller->save_errors(); |
| 225 | |
| 226 | } catch ( Handler_Exception $exception ) { |
| 227 | // do nothing |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | public function get_rest(): Records_Rest_Controller { |
| 232 | return $this->rest; |
| 233 | } |
| 234 | |
| 235 | } |
| 236 |