actions
6 months ago
assets
2 years ago
meta
2 months 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 year ago
module.php
381 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 | |
| 93 | /** |
| 94 | * @since 3.0.1 |
| 95 | */ |
| 96 | add_filter( 'gutenberg_can_edit_post_type', array( $this, 'can_edit_post_type' ), 150, 2 ); |
| 97 | add_filter( 'use_block_editor_for_post_type', array( $this, 'can_edit_post_type' ), 150, 2 ); |
| 98 | |
| 99 | /** @since 3.1.1 */ |
| 100 | // watch on this methods performance because it executed multiple times on each page load |
| 101 | add_filter( 'user_has_cap', array( $this, 'add_admin_capabilities' ) ); |
| 102 | |
| 103 | add_filter( 'post_row_actions', array( $this->get_post_actions(), 'base_add_action_links' ), 10, 2 ); |
| 104 | add_action( 'admin_enqueue_scripts', array( $this, 'import_form_js' ) ); |
| 105 | |
| 106 | require_once $this->get_dir( 'actions/set-default-args.php' ); |
| 107 | } |
| 108 | |
| 109 | public function remove_hooks() { |
| 110 | remove_action( 'init', array( $this, 'register_post_type' ) ); |
| 111 | remove_action( 'current_screen', array( $this, 'set_current_screen' ) ); |
| 112 | |
| 113 | /** |
| 114 | * @since 3.0.1 |
| 115 | */ |
| 116 | remove_filter( 'gutenberg_can_edit_post_type', array( $this, 'can_edit_post_type' ), 150 ); |
| 117 | remove_filter( 'use_block_editor_for_post_type', array( $this, 'can_edit_post_type' ), 150 ); |
| 118 | |
| 119 | /** @since 3.1.1 */ |
| 120 | // watch on this methods performance because it executed multiple times on each page load |
| 121 | remove_filter( 'user_has_cap', array( $this, 'add_admin_capabilities' ) ); |
| 122 | |
| 123 | remove_filter( 'post_row_actions', array( $this->get_post_actions(), 'base_add_action_links' ) ); |
| 124 | remove_action( 'admin_enqueue_scripts', array( $this, 'import_form_js' ) ); |
| 125 | } |
| 126 | |
| 127 | public function set_current_screen() { |
| 128 | $screen = get_current_screen(); |
| 129 | |
| 130 | if ( ! $screen || ! $screen->is_block_editor ) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | if ( self::SLUG === $screen->id ) { |
| 135 | $this->is_form_editor = true; |
| 136 | |
| 137 | } elseif ( self::SLUG !== $screen->id ) { |
| 138 | $this->is_form_editor = false; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | public static function is_form_list_page(): bool { |
| 143 | if ( ! did_action( 'current_screen' ) ) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | $screen = get_current_screen(); |
| 148 | |
| 149 | return ( 'edit-' . self::SLUG ) === $screen->id; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param $can |
| 154 | * @param $post_type |
| 155 | * |
| 156 | * @return bool |
| 157 | * @since 3.0.1 |
| 158 | */ |
| 159 | public function can_edit_post_type( $can, $post_type ): bool { |
| 160 | return self::SLUG === $post_type ? true : $can; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Register templates post type |
| 165 | * |
| 166 | * @return void |
| 167 | */ |
| 168 | public function register_post_type() { |
| 169 | |
| 170 | $args = array( |
| 171 | 'labels' => array( |
| 172 | 'name' => __( 'Forms', 'jet-form-builder' ), |
| 173 | 'all_items' => __( 'Forms', 'jet-form-builder' ), |
| 174 | 'add_new' => __( 'Add New', 'jet-form-builder' ), |
| 175 | 'add_new_item' => __( 'Add New Form', 'jet-form-builder' ), |
| 176 | 'edit_item' => __( 'Edit Form', 'jet-form-builder' ), |
| 177 | 'new_item' => __( 'New Form', 'jet-form-builder' ), |
| 178 | 'view_item' => __( 'View Form', 'jet-form-builder' ), |
| 179 | 'search_items' => __( 'Search Form', 'jet-form-builder' ), |
| 180 | 'not_found' => __( 'No Forms Found', 'jet-form-builder' ), |
| 181 | 'not_found_in_trash' => __( 'No Forms Found In Trash', 'jet-form-builder' ), |
| 182 | 'singular_name' => __( 'JetForm', 'jet-form-builder' ), |
| 183 | 'menu_name' => __( 'JetFormBuilder', 'jet-form-builder' ), |
| 184 | ), |
| 185 | 'public' => true, |
| 186 | 'show_ui' => true, |
| 187 | 'show_in_admin_bar' => true, |
| 188 | 'show_in_menu' => true, |
| 189 | 'show_in_nav_menus' => false, |
| 190 | 'show_in_rest' => true, |
| 191 | 'rest_controller_class' => Forms_Post_Type_Controller::class, |
| 192 | 'publicly_queryable' => false, |
| 193 | 'exclude_from_search' => true, |
| 194 | 'has_archive' => false, |
| 195 | 'query_var' => false, |
| 196 | 'can_export' => true, |
| 197 | 'rewrite' => false, |
| 198 | 'capability_type' => 'jet_fb_form', |
| 199 | 'menu_icon' => $this->get_post_type_icon(), |
| 200 | 'menu_position' => 120, |
| 201 | 'supports' => array( 'title', 'editor', 'custom-fields', 'revisions' ), |
| 202 | ); |
| 203 | |
| 204 | register_post_type( |
| 205 | self::SLUG, |
| 206 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 207 | apply_filters( 'jet-form-builder/post-type/args', $args ) |
| 208 | ); |
| 209 | |
| 210 | $this->get_meta()->after_register_post_type(); |
| 211 | } |
| 212 | |
| 213 | public function add_admin_capabilities( $allcaps ) { |
| 214 | $capability = apply_filters( 'jet-form-builder/capability/form', 'manage_options' ); |
| 215 | |
| 216 | if ( empty( $allcaps[ $capability ] ) ) { |
| 217 | return $allcaps; |
| 218 | } |
| 219 | |
| 220 | foreach ( self::CAPABILITIES as $capability ) { |
| 221 | $allcaps[ $capability ] = true; |
| 222 | } |
| 223 | |
| 224 | return $allcaps; |
| 225 | } |
| 226 | |
| 227 | public function import_form_js() { |
| 228 | if ( ! self::is_form_list_page() ) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | $action = $this->get_post_actions()->get( 'jet_fb_import' ); |
| 233 | |
| 234 | $import_button = __( 'Start Import', 'jet-form-builder' ); |
| 235 | |
| 236 | ob_start(); |
| 237 | include $this->get_dir( 'templates/import-form.php' ); |
| 238 | $import_template = ob_get_clean(); |
| 239 | |
| 240 | $handle = $this->get_handle(); |
| 241 | |
| 242 | wp_enqueue_script( |
| 243 | $handle, |
| 244 | $this->get_url( 'assets/build/js/import-form.js' ), |
| 245 | array( 'jquery' ), |
| 246 | jet_form_builder()->get_version(), |
| 247 | true |
| 248 | ); |
| 249 | |
| 250 | wp_localize_script( |
| 251 | $handle, |
| 252 | 'JetFormBuilderImportForm', |
| 253 | array( |
| 254 | 'id' => $action->get_id(), |
| 255 | 'template' => $import_template, |
| 256 | ) |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | private function get_post_type_icon() { |
| 261 | $path = $this->get_dir( 'icon.php' ); |
| 262 | |
| 263 | return include_once $path; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | public function maybe_get_jet_sm_ready_styles( $form_id ) { |
| 268 | return Compatibility::has_jet_sm() ? get_post_meta( $form_id, '_jet_sm_ready_style', true ) : ''; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Returns form meta arguments: |
| 273 | * fields_layout, submit_type, captcha and required_mark |
| 274 | * in assoc array |
| 275 | * |
| 276 | * @param int|false $form_id |
| 277 | * |
| 278 | * @return array |
| 279 | */ |
| 280 | public function get_args( $form_id = false ): array { |
| 281 | return $this->get_meta()->get_args( $form_id ); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Returns form messages |
| 286 | * |
| 287 | * @param int|false $form_id |
| 288 | * |
| 289 | * @return array |
| 290 | */ |
| 291 | public function get_messages( $form_id = false ) { |
| 292 | return $this->get_meta()->get_messages( $form_id ); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Returns form actions |
| 297 | * |
| 298 | * @param int|false $form_id |
| 299 | * |
| 300 | * @return array |
| 301 | */ |
| 302 | public function get_preset( $form_id = false ) { |
| 303 | return $this->get_meta()->get_preset( $form_id ); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Returns captcha settings |
| 308 | * |
| 309 | * @param int|false $form_id |
| 310 | * |
| 311 | * @return array |
| 312 | */ |
| 313 | public function get_captcha( $form_id = false ) { |
| 314 | return $this->get_meta()->get_captcha( $form_id ); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Returns form gateways |
| 319 | * |
| 320 | * @param int|false $form_id |
| 321 | * |
| 322 | * @return array |
| 323 | */ |
| 324 | public function get_gateways( $form_id = false ): array { |
| 325 | return $this->get_meta()->get_gateways( $form_id ); |
| 326 | } |
| 327 | |
| 328 | public function get_actions( $form_id = false ): array { |
| 329 | return $this->get_meta()->get_actions( $form_id ); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * @param string $name |
| 334 | * @param false|bool $form_id |
| 335 | * |
| 336 | * @return array|mixed |
| 337 | * @since 3.3.0 |
| 338 | */ |
| 339 | public function query_meta( string $name, $form_id = false ) { |
| 340 | return $this->get_meta()->get_meta( $name )->query( $form_id ); |
| 341 | } |
| 342 | |
| 343 | public function get_meta(): Meta_Repository { |
| 344 | return $this->meta; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * @return Actions_Repository |
| 349 | */ |
| 350 | public function get_post_actions(): Actions_Repository { |
| 351 | return $this->post_actions; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * @deprecated 3.2.0 |
| 356 | * |
| 357 | * @return string |
| 358 | */ |
| 359 | public function slug(): string { |
| 360 | return self::SLUG; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * @return Actions_Repository |
| 365 | */ |
| 366 | public function get_form_meta( $meta_key, $form_id = false ) { |
| 367 | if ( false === $form_id ) { |
| 368 | $form_id = jet_fb_live()->form_id; |
| 369 | } |
| 370 | |
| 371 | return Tools::decode_json( |
| 372 | get_post_meta( |
| 373 | $form_id, |
| 374 | $meta_key, |
| 375 | true |
| 376 | ) |
| 377 | ); |
| 378 | } |
| 379 | |
| 380 | } |
| 381 |