assets
1 year ago
form-shortcode.php
1 year ago
module.php
1 year ago
onboarding-builder.php
1 year ago
shortcode.php
2 years ago
module.php
208 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Shortcode; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Arguments\Default_Form_Arguments; |
| 7 | use Jet_Form_Builder\Classes\Arguments\Form_Arguments; |
| 8 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 9 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 10 | use JFB_Components\Module\Base_Module_Dir_It; |
| 11 | use JFB_Components\Module\Base_Module_Dir_Trait; |
| 12 | use JFB_Components\Module\Base_Module_Handle_It; |
| 13 | use JFB_Components\Module\Base_Module_Handle_Trait; |
| 14 | use JFB_Components\Module\Base_Module_It; |
| 15 | use JFB_Components\Module\Base_Module_Url_It; |
| 16 | use JFB_Components\Module\Base_Module_Url_Trait; |
| 17 | use JFB_Components\Repository\Repository_Pattern_Trait; |
| 18 | |
| 19 | // If this file is called directly, abort. |
| 20 | if ( ! defined( 'WPINC' ) ) { |
| 21 | die; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Class Manager |
| 26 | * |
| 27 | * @package JFB_Modules\Shortcode |
| 28 | */ |
| 29 | final class Module implements |
| 30 | Base_Module_It, |
| 31 | Base_Module_After_Install_It, |
| 32 | Base_Module_Handle_It, |
| 33 | Base_Module_Url_It, |
| 34 | Base_Module_Dir_It { |
| 35 | |
| 36 | use Repository_Pattern_Trait; |
| 37 | use Base_Module_Dir_Trait; |
| 38 | use Base_Module_Handle_Trait; |
| 39 | use Base_Module_Url_Trait; |
| 40 | |
| 41 | /** |
| 42 | * @var Onboarding_Builder |
| 43 | */ |
| 44 | private $onboarding_builder; |
| 45 | |
| 46 | public function rep_item_id() { |
| 47 | return 'shortcode'; |
| 48 | } |
| 49 | |
| 50 | public function on_install() { |
| 51 | $this->rep_install(); |
| 52 | |
| 53 | $this->onboarding_builder = new Onboarding_Builder(); |
| 54 | } |
| 55 | |
| 56 | public function on_uninstall() { |
| 57 | $this->rep_clear(); |
| 58 | } |
| 59 | |
| 60 | public function condition(): bool { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | public function init_hooks() { |
| 65 | $slug = \JFB_Modules\Post_Type\Module::SLUG; |
| 66 | |
| 67 | // post-type related |
| 68 | add_filter( |
| 69 | "manage_{$slug}_posts_columns", |
| 70 | array( $this, 'filter_columns' ) |
| 71 | ); |
| 72 | add_action( |
| 73 | "manage_{$slug}_posts_custom_column", |
| 74 | array( $this, 'add_admin_column_content' ), |
| 75 | 10, |
| 76 | 2 |
| 77 | ); |
| 78 | add_action( |
| 79 | 'jet-form-builder/use-form/register-assets', |
| 80 | array( $this, 'block_editor_assets' ), |
| 81 | 20 |
| 82 | ); |
| 83 | |
| 84 | $this->get_onboarding_builder()->init_hooks(); |
| 85 | } |
| 86 | |
| 87 | public function remove_hooks() { |
| 88 | $slug = \JFB_Modules\Post_Type\Module::SLUG; |
| 89 | |
| 90 | // post-type related |
| 91 | remove_filter( |
| 92 | "manage_{$slug}_posts_columns", |
| 93 | array( $this, 'filter_columns' ) |
| 94 | ); |
| 95 | remove_action( |
| 96 | "manage_{$slug}_posts_custom_column", |
| 97 | array( $this, 'add_admin_column_content' ) |
| 98 | ); |
| 99 | remove_action( |
| 100 | 'jet-form-builder/use-form/register-assets', |
| 101 | array( $this, 'block_editor_assets' ), |
| 102 | 20 |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | public function rep_instances(): array { |
| 107 | return array( |
| 108 | new Form_Shortcode(), |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | public function filter_columns( $columns ) { |
| 113 | $after = array( 'date' => $columns['date'] ); |
| 114 | unset( $columns['date'] ); |
| 115 | |
| 116 | $columns['jfb_shortcode'] = __( 'Shortcode', 'jet-form-builder' ); |
| 117 | |
| 118 | return array_merge( $columns, $after ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param $column |
| 123 | * @param $form_id |
| 124 | * |
| 125 | * @return void |
| 126 | * @throws Repository_Exception |
| 127 | */ |
| 128 | public function add_admin_column_content( $column, $form_id ) { |
| 129 | if ( 'jfb_shortcode' !== $column ) { |
| 130 | return; |
| 131 | } |
| 132 | /** @var \JFB_Modules\Post_Type\Module $post_type */ |
| 133 | $post_type = jet_form_builder()->module( 'post-type' ); |
| 134 | $arguments = array_diff( $post_type->get_args( $form_id ), Form_Arguments::arguments() ); |
| 135 | |
| 136 | $arguments = array_merge( |
| 137 | array( 'form_id' => $form_id ), |
| 138 | Default_Form_Arguments::arguments(), |
| 139 | $arguments |
| 140 | ); |
| 141 | |
| 142 | if ( isset( $arguments['load_nonce'] ) ) { |
| 143 | unset( $arguments['load_nonce'] ); |
| 144 | } |
| 145 | if ( isset( $arguments['use_honeypot'] ) ) { |
| 146 | unset( $arguments['use_honeypot'] ); |
| 147 | } |
| 148 | if ( isset( $arguments['use_csrf'] ) ) { |
| 149 | unset( $arguments['use_csrf'] ); |
| 150 | } |
| 151 | |
| 152 | printf( |
| 153 | '<input readonly type="text" onclick="this.select()" value="%s" style="%s"/>', |
| 154 | esc_attr( $this->get_shortcode( 'jet_fb_form', $arguments ) ), |
| 155 | 'width: 100%' |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | public function get_shortcode( $type, $arguments ): string { |
| 160 | $format = '[%1$s %2$s]'; |
| 161 | |
| 162 | try { |
| 163 | $type = $this->rep_clone_item( $type ); |
| 164 | } catch ( Repository_Exception $exception ) { |
| 165 | return ''; |
| 166 | } |
| 167 | |
| 168 | return sprintf( $format, $type->get_name(), $this->generate_arguments_string( $arguments ) ); |
| 169 | } |
| 170 | |
| 171 | public function block_editor_assets() { |
| 172 | /** @var \JFB_Modules\Onboarding\Module $onboarding */ |
| 173 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 174 | $onboarding = jet_form_builder()->module( 'onboarding' ); |
| 175 | $script_asset = require_once $this->get_dir( 'assets/build/block.editor.asset.php' ); |
| 176 | |
| 177 | array_push( |
| 178 | $script_asset['dependencies'], |
| 179 | $onboarding->get_use_form()->get_handle() |
| 180 | ); |
| 181 | |
| 182 | wp_enqueue_script( |
| 183 | $this->get_handle( 'block-editor' ), |
| 184 | $this->get_url( 'assets/build/block.editor.js' ), |
| 185 | $script_asset['dependencies'], |
| 186 | $script_asset['version'], |
| 187 | true |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | private function generate_arguments_string( $arguments ): string { |
| 192 | $response = array(); |
| 193 | |
| 194 | foreach ( $arguments as $name => $value ) { |
| 195 | $response[] = "$name=\"$value\""; |
| 196 | } |
| 197 | |
| 198 | return implode( ' ', $response ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @return Onboarding_Builder |
| 203 | */ |
| 204 | public function get_onboarding_builder(): Onboarding_Builder { |
| 205 | return $this->onboarding_builder; |
| 206 | } |
| 207 | } |
| 208 |