actions
7 months ago
assets
2 years ago
meta
1 week ago
rest-api
2 years ago
templates
2 years ago
actions-repository.php
2 years ago
icon.php
2 years ago
meta-repository.php
2 years ago
module.php
1 week ago
module.php
413 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Modules\Post_Type; |
| 4 | |
| 5 | // If this file is called directly, abort. |
| 6 | if ( ! defined( 'WPINC' ) ) { |
| 7 | die; |
| 8 | } |
| 9 | |
| 10 | use Jet_Form_Builder\Classes\Compatibility; |
| 11 | use Jet_Form_Builder\Classes\Tools; |
| 12 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 13 | use JFB_Components\Module\Base_Module_Dir_It; |
| 14 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 15 | use JFB_Components\Module\Base_Module_Handle_It; |
| 16 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 17 | use JFB_Components\Module\Base_Module_It; |
| 18 | use JFB_Components\Module\Base_Module_Url_It; |
| 19 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 20 | use JFB_Modules\Post_Type\Rest_Api\Forms_Post_Type_Controller; |
| 21 | |
| 22 | /** |
| 23 | * @since 3.2.0 |
| 24 | * |
| 25 | * Class Module |
| 26 | * @package JFB_Modules\Post_Type |
| 27 | */ |
| 28 | class Module implements |
| 29 | Base_Module_It, |
| 30 | Base_Module_Url_It, |
| 31 | Base_Module_Handle_It, |
| 32 | Base_Module_Dir_It, |
| 33 | Base_Module_After_Install_It { |
| 34 | |
| 35 | use Base_Module_Url_Trait; |
| 36 | use Base_Module_Handle_Trait; |
| 37 | use Base_Module_Dir_Trait; |
| 38 | |
| 39 | /** |
| 40 | * Used to define the editor |
| 41 | * |
| 42 | * @var boolean |
| 43 | */ |
| 44 | public $is_form_editor = false; |
| 45 | |
| 46 | /** @var Meta_Repository */ |
| 47 | private $meta; |
| 48 | |
| 49 | /** @var Actions_Repository */ |
| 50 | private $post_actions; |
| 51 | |
| 52 | const CAPABILITIES = array( |
| 53 | 'edit_jet_fb_form', |
| 54 | 'read_jet_fb_form', |
| 55 | 'delete_jet_fb_form', |
| 56 | 'edit_jet_fb_forms', |
| 57 | 'edit_others_jet_fb_forms', |
| 58 | 'delete_jet_fb_forms', |
| 59 | 'publish_jet_fb_forms', |
| 60 | 'read_private_jet_fb_forms', |
| 61 | ); |
| 62 | |
| 63 | const SLUG = 'jet-form-builder'; |
| 64 | |
| 65 | public function rep_item_id() { |
| 66 | return 'post-type'; |
| 67 | } |
| 68 | |
| 69 | public function condition(): bool { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | public function on_install() { |
| 74 | $this->meta = new Meta_Repository(); |
| 75 | $this->post_actions = new Actions_Repository(); |
| 76 | |
| 77 | $this->get_meta()->rep_install(); |
| 78 | |
| 79 | if ( is_admin() ) { |
| 80 | $this->post_actions->rep_install(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public function on_uninstall() { |
| 85 | $this->get_meta()->rep_clear(); |
| 86 | $this->get_post_actions()->rep_clear(); |
| 87 | } |
| 88 | |
| 89 | public function init_hooks() { |
| 90 | add_action( 'init', array( $this, 'register_post_type' ) ); |
| 91 | add_action( 'current_screen', array( $this, 'set_current_screen' ) ); |
| 92 | add_action( 'save_post_' . self::SLUG, array( $this, 'set_new_form_gateway_defaults' ), 10, 3 ); |
| 93 | |
| 94 | /** |
| 95 | * @since 3.0.1 |
| 96 | */ |
| 97 | add_filter( 'gutenberg_can_edit_post_type', array( $this, 'can_edit_post_type' ), 150, 2 ); |
| 98 | add_filter( 'use_block_editor_for_post_type', array( $this, 'can_edit_post_type' ), 150, 2 ); |
| 99 | |
| 100 | /** @since 3.1.1 */ |
| 101 | // watch on this methods performance because it executed multiple times on each page load |
| 102 | add_filter( 'user_has_cap', array( $this, 'add_admin_capabilities' ) ); |
| 103 | |
| 104 | add_filter( 'post_row_actions', array( $this->get_post_actions(), 'base_add_action_links' ), 10, 2 ); |
| 105 | add_action( 'admin_enqueue_scripts', array( $this, 'import_form_js' ) ); |
| 106 | |
| 107 | require_once $this->get_dir( 'actions/set-default-args.php' ); |
| 108 | } |
| 109 | |
| 110 | public function remove_hooks() { |
| 111 | remove_action( 'init', array( $this, 'register_post_type' ) ); |
| 112 | remove_action( 'current_screen', array( $this, 'set_current_screen' ) ); |
| 113 | remove_action( 'save_post_' . self::SLUG, array( $this, 'set_new_form_gateway_defaults' ), 10 ); |
| 114 | |
| 115 | /** |
| 116 | * @since 3.0.1 |
| 117 | */ |
| 118 | remove_filter( 'gutenberg_can_edit_post_type', array( $this, 'can_edit_post_type' ), 150 ); |
| 119 | remove_filter( 'use_block_editor_for_post_type', array( $this, 'can_edit_post_type' ), 150 ); |
| 120 | |
| 121 | /** @since 3.1.1 */ |
| 122 | // watch on this methods performance because it executed multiple times on each page load |
| 123 | remove_filter( 'user_has_cap', array( $this, 'add_admin_capabilities' ) ); |
| 124 | |
| 125 | remove_filter( 'post_row_actions', array( $this->get_post_actions(), 'base_add_action_links' ) ); |
| 126 | remove_action( 'admin_enqueue_scripts', array( $this, 'import_form_js' ) ); |
| 127 | } |
| 128 | |
| 129 | public function set_current_screen() { |
| 130 | $screen = get_current_screen(); |
| 131 | |
| 132 | if ( ! $screen || ! $screen->is_block_editor ) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | if ( self::SLUG === $screen->id ) { |
| 137 | $this->is_form_editor = true; |
| 138 | |
| 139 | } elseif ( self::SLUG !== $screen->id ) { |
| 140 | $this->is_form_editor = false; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Enable payment amount protection for newly created forms only. |
| 146 | * |
| 147 | * Existing or imported gateway settings are preserved here. A separate |
| 148 | * migration enables protection for strictly verified static price sources. |
| 149 | * |
| 150 | * @param int $post_id Form post ID. |
| 151 | * @param \WP_Post $post Form post object. |
| 152 | * @param bool $update Whether an existing form is being updated. |
| 153 | */ |
| 154 | public function set_new_form_gateway_defaults( $post_id, $post, $update ) { |
| 155 | if ( |
| 156 | $update || |
| 157 | ! $post instanceof \WP_Post || |
| 158 | metadata_exists( 'post', $post_id, Meta\Gateways_Meta::META_KEY ) |
| 159 | ) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | update_post_meta( |
| 164 | $post_id, |
| 165 | Meta\Gateways_Meta::META_KEY, |
| 166 | wp_json_encode( |
| 167 | array( |
| 168 | 'protect_price_field' => true, |
| 169 | ) |
| 170 | ) |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | public static function is_form_list_page(): bool { |
| 175 | if ( ! did_action( 'current_screen' ) ) { |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | $screen = get_current_screen(); |
| 180 | |
| 181 | return ( 'edit-' . self::SLUG ) === $screen->id; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @param $can |
| 186 | * @param $post_type |
| 187 | * |
| 188 | * @return bool |
| 189 | * @since 3.0.1 |
| 190 | */ |
| 191 | public function can_edit_post_type( $can, $post_type ): bool { |
| 192 | return self::SLUG === $post_type ? true : $can; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Register templates post type |
| 197 | * |
| 198 | * @return void |
| 199 | */ |
| 200 | public function register_post_type() { |
| 201 | |
| 202 | $args = array( |
| 203 | 'labels' => array( |
| 204 | 'name' => __( 'Forms', 'jet-form-builder' ), |
| 205 | 'all_items' => __( 'Forms', 'jet-form-builder' ), |
| 206 | 'add_new' => __( 'Add New', 'jet-form-builder' ), |
| 207 | 'add_new_item' => __( 'Add New Form', 'jet-form-builder' ), |
| 208 | 'edit_item' => __( 'Edit Form', 'jet-form-builder' ), |
| 209 | 'new_item' => __( 'New Form', 'jet-form-builder' ), |
| 210 | 'view_item' => __( 'View Form', 'jet-form-builder' ), |
| 211 | 'search_items' => __( 'Search Form', 'jet-form-builder' ), |
| 212 | 'not_found' => __( 'No Forms Found', 'jet-form-builder' ), |
| 213 | 'not_found_in_trash' => __( 'No Forms Found In Trash', 'jet-form-builder' ), |
| 214 | 'singular_name' => __( 'JetForm', 'jet-form-builder' ), |
| 215 | 'menu_name' => __( 'JetFormBuilder', 'jet-form-builder' ), |
| 216 | ), |
| 217 | 'public' => true, |
| 218 | 'show_ui' => true, |
| 219 | 'show_in_admin_bar' => true, |
| 220 | 'show_in_menu' => true, |
| 221 | 'show_in_nav_menus' => false, |
| 222 | 'show_in_rest' => true, |
| 223 | 'rest_controller_class' => Forms_Post_Type_Controller::class, |
| 224 | 'publicly_queryable' => false, |
| 225 | 'exclude_from_search' => true, |
| 226 | 'has_archive' => false, |
| 227 | 'query_var' => false, |
| 228 | 'can_export' => true, |
| 229 | 'rewrite' => false, |
| 230 | 'capability_type' => 'jet_fb_form', |
| 231 | 'menu_icon' => $this->get_post_type_icon(), |
| 232 | 'menu_position' => 120, |
| 233 | 'supports' => array( 'title', 'editor', 'custom-fields', 'revisions' ), |
| 234 | ); |
| 235 | |
| 236 | register_post_type( |
| 237 | self::SLUG, |
| 238 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 239 | apply_filters( 'jet-form-builder/post-type/args', $args ) |
| 240 | ); |
| 241 | |
| 242 | $this->get_meta()->after_register_post_type(); |
| 243 | } |
| 244 | |
| 245 | public function add_admin_capabilities( $allcaps ) { |
| 246 | $capability = apply_filters( 'jet-form-builder/capability/form', 'manage_options' ); |
| 247 | |
| 248 | if ( empty( $allcaps[ $capability ] ) ) { |
| 249 | return $allcaps; |
| 250 | } |
| 251 | |
| 252 | foreach ( self::CAPABILITIES as $capability ) { |
| 253 | $allcaps[ $capability ] = true; |
| 254 | } |
| 255 | |
| 256 | return $allcaps; |
| 257 | } |
| 258 | |
| 259 | public function import_form_js() { |
| 260 | if ( ! self::is_form_list_page() ) { |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | $action = $this->get_post_actions()->get( 'jet_fb_import' ); |
| 265 | |
| 266 | $import_button = __( 'Start Import', 'jet-form-builder' ); |
| 267 | |
| 268 | ob_start(); |
| 269 | include $this->get_dir( 'templates/import-form.php' ); |
| 270 | $import_template = ob_get_clean(); |
| 271 | |
| 272 | $handle = $this->get_handle(); |
| 273 | |
| 274 | wp_enqueue_script( |
| 275 | $handle, |
| 276 | $this->get_url( 'assets/build/js/import-form.js' ), |
| 277 | array( 'jquery' ), |
| 278 | jet_form_builder()->get_version(), |
| 279 | true |
| 280 | ); |
| 281 | |
| 282 | wp_localize_script( |
| 283 | $handle, |
| 284 | 'JetFormBuilderImportForm', |
| 285 | array( |
| 286 | 'id' => $action->get_id(), |
| 287 | 'template' => $import_template, |
| 288 | ) |
| 289 | ); |
| 290 | } |
| 291 | |
| 292 | private function get_post_type_icon() { |
| 293 | $path = $this->get_dir( 'icon.php' ); |
| 294 | |
| 295 | return include_once $path; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | public function maybe_get_jet_sm_ready_styles( $form_id ) { |
| 300 | return Compatibility::has_jet_sm() ? get_post_meta( $form_id, '_jet_sm_ready_style', true ) : ''; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Returns form meta arguments: |
| 305 | * fields_layout, submit_type, captcha and required_mark |
| 306 | * in assoc array |
| 307 | * |
| 308 | * @param int|false $form_id |
| 309 | * |
| 310 | * @return array |
| 311 | */ |
| 312 | public function get_args( $form_id = false ): array { |
| 313 | return $this->get_meta()->get_args( $form_id ); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Returns form messages |
| 318 | * |
| 319 | * @param int|false $form_id |
| 320 | * |
| 321 | * @return array |
| 322 | */ |
| 323 | public function get_messages( $form_id = false ) { |
| 324 | return $this->get_meta()->get_messages( $form_id ); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Returns form actions |
| 329 | * |
| 330 | * @param int|false $form_id |
| 331 | * |
| 332 | * @return array |
| 333 | */ |
| 334 | public function get_preset( $form_id = false ) { |
| 335 | return $this->get_meta()->get_preset( $form_id ); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Returns captcha settings |
| 340 | * |
| 341 | * @param int|false $form_id |
| 342 | * |
| 343 | * @return array |
| 344 | */ |
| 345 | public function get_captcha( $form_id = false ) { |
| 346 | return $this->get_meta()->get_captcha( $form_id ); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Returns form gateways |
| 351 | * |
| 352 | * @param int|false $form_id |
| 353 | * |
| 354 | * @return array |
| 355 | */ |
| 356 | public function get_gateways( $form_id = false ): array { |
| 357 | return $this->get_meta()->get_gateways( $form_id ); |
| 358 | } |
| 359 | |
| 360 | public function get_actions( $form_id = false ): array { |
| 361 | return $this->get_meta()->get_actions( $form_id ); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * @param string $name |
| 366 | * @param false|bool $form_id |
| 367 | * |
| 368 | * @return array|mixed |
| 369 | * @since 3.3.0 |
| 370 | */ |
| 371 | public function query_meta( string $name, $form_id = false ) { |
| 372 | return $this->get_meta()->get_meta( $name )->query( $form_id ); |
| 373 | } |
| 374 | |
| 375 | public function get_meta(): Meta_Repository { |
| 376 | return $this->meta; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * @return Actions_Repository |
| 381 | */ |
| 382 | public function get_post_actions(): Actions_Repository { |
| 383 | return $this->post_actions; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * @deprecated 3.2.0 |
| 388 | * |
| 389 | * @return string |
| 390 | */ |
| 391 | public function slug(): string { |
| 392 | return self::SLUG; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * @return Actions_Repository |
| 397 | */ |
| 398 | public function get_form_meta( $meta_key, $form_id = false ) { |
| 399 | if ( false === $form_id ) { |
| 400 | $form_id = jet_fb_live()->form_id; |
| 401 | } |
| 402 | |
| 403 | return Tools::decode_json( |
| 404 | get_post_meta( |
| 405 | $form_id, |
| 406 | $meta_key, |
| 407 | true |
| 408 | ) |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | } |
| 413 |