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