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