editor.php
404 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Admin; |
| 4 | |
| 5 | use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager; |
| 6 | use Jet_Form_Builder\Classes\Condition_Helper; |
| 7 | use Jet_Form_Builder\Classes\Tools; |
| 8 | use Jet_Form_Builder\Gateways\Gateway_Manager; |
| 9 | use Jet_Form_Builder\Plugin; |
| 10 | |
| 11 | /** |
| 12 | * Form editor class |
| 13 | * Thanks Tom J Nowell for initial editor idea and inspiration! |
| 14 | */ |
| 15 | |
| 16 | // If this file is called directly, abort. |
| 17 | if ( ! defined( 'WPINC' ) ) { |
| 18 | die; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Define Editor class |
| 23 | */ |
| 24 | class Editor { |
| 25 | |
| 26 | const EDITOR_HANDLE = 'jet-form-builder-editor'; |
| 27 | const EDITOR_PACKAGE_HANDLE = 'jet-form-builder-editor-package'; |
| 28 | |
| 29 | /** |
| 30 | * Returns taxonomies list for the config |
| 31 | * |
| 32 | * @return [type] [description] |
| 33 | */ |
| 34 | public function get_taxonomies_list() { |
| 35 | |
| 36 | $taxonomies = get_taxonomies( array(), 'objects' ); |
| 37 | |
| 38 | $result = array(); |
| 39 | |
| 40 | foreach ( $taxonomies as $tax ) { |
| 41 | $result[] = array( |
| 42 | 'value' => $tax->name, |
| 43 | 'label' => sprintf( '%1$s (%2$s)', $tax->label, $tax->name ), |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | return $result; |
| 48 | |
| 49 | } |
| 50 | |
| 51 | public function get_preset_config() { |
| 52 | return apply_filters( |
| 53 | 'jet-form-builder/editor/preset-config', |
| 54 | array( |
| 55 | 'global_fields' => array( |
| 56 | array( |
| 57 | 'name' => 'from', |
| 58 | 'label' => __( 'Source:', 'jet-form-builder' ), |
| 59 | 'type' => 'select', |
| 60 | 'options' => Tools::with_placeholder( |
| 61 | array( |
| 62 | array( |
| 63 | 'value' => 'post', |
| 64 | 'label' => __( 'Post', 'jet-form-builder' ), |
| 65 | ), |
| 66 | array( |
| 67 | 'value' => 'user', |
| 68 | 'label' => __( 'User', 'jet-form-builder' ), |
| 69 | ), |
| 70 | array( |
| 71 | 'value' => 'query_var', |
| 72 | 'label' => __( 'URL Query Variable', 'jet-form-builder' ), |
| 73 | ), |
| 74 | ) |
| 75 | ), |
| 76 | ), |
| 77 | array( |
| 78 | 'name' => 'post_from', |
| 79 | 'label' => __( 'Get post ID from:', 'jet-form-builder' ), |
| 80 | 'type' => 'select', |
| 81 | 'options' => Tools::with_placeholder( |
| 82 | array( |
| 83 | array( |
| 84 | 'value' => 'current_post', |
| 85 | 'label' => __( 'Current post', 'jet-form-builder' ), |
| 86 | ), |
| 87 | array( |
| 88 | 'value' => 'query_var', |
| 89 | 'label' => __( 'URL Query Variable', 'jet-form-builder' ), |
| 90 | ), |
| 91 | ) |
| 92 | ), |
| 93 | 'condition' => array( |
| 94 | 'field' => 'from', |
| 95 | 'value' => 'post', |
| 96 | ), |
| 97 | ), |
| 98 | array( |
| 99 | 'name' => 'user_from', |
| 100 | 'label' => __( 'Get user ID from:', 'jet-form-builder' ), |
| 101 | 'type' => 'select', |
| 102 | 'options' => Tools::with_placeholder( |
| 103 | array( |
| 104 | array( |
| 105 | 'value' => 'current_user', |
| 106 | 'label' => __( 'Current user', 'jet-form-builder' ), |
| 107 | ), |
| 108 | array( |
| 109 | 'value' => 'query_var', |
| 110 | 'label' => __( 'URL Query Variable', 'jet-form-builder' ), |
| 111 | ), |
| 112 | ) |
| 113 | ), |
| 114 | 'condition' => array( |
| 115 | 'field' => 'from', |
| 116 | 'value' => 'user', |
| 117 | ), |
| 118 | ), |
| 119 | array( |
| 120 | 'name' => 'query_var', |
| 121 | 'label' => __( 'Query variable name:', 'jet-form-builder' ), |
| 122 | 'type' => 'text', |
| 123 | 'custom_condition' => 'query_var', |
| 124 | 'position' => 'dynamic', |
| 125 | ), |
| 126 | ), |
| 127 | 'map_fields' => array( |
| 128 | array( |
| 129 | 'name' => 'key', |
| 130 | 'label' => __( 'Query variable key', 'jet-form-builder' ), |
| 131 | 'type' => 'text', |
| 132 | 'position' => 'general', |
| 133 | 'parent_condition' => array( |
| 134 | 'field' => 'from', |
| 135 | 'value' => 'query_var', |
| 136 | ), |
| 137 | ), |
| 138 | array( |
| 139 | 'name' => 'prop', |
| 140 | 'label' => __( 'Post property', 'jet-form-builder' ), |
| 141 | 'type' => 'select', |
| 142 | 'options' => Tools::with_placeholder( |
| 143 | array( |
| 144 | array( |
| 145 | 'value' => 'ID', |
| 146 | 'label' => __( 'Post ID', 'jet-form-builder' ), |
| 147 | ), |
| 148 | array( |
| 149 | 'value' => 'post_title', |
| 150 | 'label' => __( 'Post Title', 'jet-form-builder' ), |
| 151 | ), |
| 152 | array( |
| 153 | 'value' => 'post_content', |
| 154 | 'label' => __( 'Post Content', 'jet-form-builder' ), |
| 155 | ), |
| 156 | array( |
| 157 | 'value' => 'post_excerpt', |
| 158 | 'label' => __( 'Post Excerpt', 'jet-form-builder' ), |
| 159 | ), |
| 160 | array( |
| 161 | 'value' => 'post_date', |
| 162 | 'label' => __( 'Post Date', 'jet-form-builder' ), |
| 163 | ), |
| 164 | array( |
| 165 | 'value' => 'post_date_gmt', |
| 166 | 'label' => __( 'Post Date GMT', 'jet-form-builder' ), |
| 167 | ), |
| 168 | array( |
| 169 | 'value' => 'post_thumb', |
| 170 | 'label' => __( 'Post Thumbnail', 'jet-form-builder' ), |
| 171 | ), |
| 172 | array( |
| 173 | 'value' => 'post_meta', |
| 174 | 'label' => __( 'Post Meta', 'jet-form-builder' ), |
| 175 | ), |
| 176 | array( |
| 177 | 'value' => 'post_terms', |
| 178 | 'label' => __( 'Post Terms', 'jet-form-builder' ), |
| 179 | ), |
| 180 | ) |
| 181 | ), |
| 182 | 'parent_condition' => array( |
| 183 | 'field' => 'from', |
| 184 | 'value' => 'post', |
| 185 | ), |
| 186 | ), |
| 187 | array( |
| 188 | 'name' => 'key', |
| 189 | 'label' => __( 'Taxonomy', 'jet-form-builder' ), |
| 190 | 'type' => 'select', |
| 191 | 'options' => Tools::with_placeholder( $this->get_taxonomies_list() ), |
| 192 | 'parent_condition' => array( |
| 193 | 'field' => 'from', |
| 194 | 'value' => 'post', |
| 195 | ), |
| 196 | 'condition' => array( |
| 197 | 'field' => 'prop', |
| 198 | 'value' => 'post_terms', |
| 199 | ), |
| 200 | ), |
| 201 | array( |
| 202 | 'name' => 'key', |
| 203 | 'label' => __( 'Meta field key', 'jet-form-builder' ), |
| 204 | 'type' => 'text', |
| 205 | 'parent_condition' => array( |
| 206 | 'field' => 'from', |
| 207 | 'value' => 'post', |
| 208 | ), |
| 209 | 'condition' => array( |
| 210 | 'field' => 'prop', |
| 211 | 'value' => 'post_meta', |
| 212 | ), |
| 213 | ), |
| 214 | array( |
| 215 | 'name' => 'prop', |
| 216 | 'label' => __( 'User field', 'jet-form-builder' ), |
| 217 | 'type' => 'select', |
| 218 | 'options' => Tools::with_placeholder( |
| 219 | array( |
| 220 | array( |
| 221 | 'value' => 'ID', |
| 222 | 'label' => __( 'User ID', 'jet-form-builder' ), |
| 223 | ), |
| 224 | array( |
| 225 | 'value' => 'user_login', |
| 226 | 'label' => __( 'User Login', 'jet-form-builder' ), |
| 227 | ), |
| 228 | array( |
| 229 | 'value' => 'user_email', |
| 230 | 'label' => __( 'Email', 'jet-form-builder' ), |
| 231 | ), |
| 232 | array( |
| 233 | 'value' => 'password', |
| 234 | 'label' => __( 'Password', 'jet-form-builder' ), |
| 235 | ), |
| 236 | array( |
| 237 | 'value' => 'first_name', |
| 238 | 'label' => __( 'First Name', 'jet-form-builder' ), |
| 239 | ), |
| 240 | array( |
| 241 | 'value' => 'last_name', |
| 242 | 'label' => __( 'Last Name', 'jet-form-builder' ), |
| 243 | ), |
| 244 | array( |
| 245 | 'value' => 'user_url', |
| 246 | 'label' => __( 'User URL', 'jet-form-builder' ), |
| 247 | ), |
| 248 | array( |
| 249 | 'value' => 'user_meta', |
| 250 | 'label' => __( 'User Meta', 'jet-form-builder' ), |
| 251 | ), |
| 252 | ) |
| 253 | ), |
| 254 | 'parent_condition' => array( |
| 255 | 'field' => 'from', |
| 256 | 'value' => 'user', |
| 257 | ), |
| 258 | ), |
| 259 | array( |
| 260 | 'name' => 'key', |
| 261 | 'label' => __( 'Meta field key', 'jet-form-builder' ), |
| 262 | 'type' => 'text', |
| 263 | 'parent_condition' => array( |
| 264 | 'field' => 'from', |
| 265 | 'value' => 'user', |
| 266 | ), |
| 267 | 'condition' => array( |
| 268 | 'field' => 'prop', |
| 269 | 'value' => 'user_meta', |
| 270 | ), |
| 271 | ), |
| 272 | ), |
| 273 | ) |
| 274 | ); |
| 275 | } |
| 276 | |
| 277 | public function get_messages_default() { |
| 278 | return Plugin::instance()->post_type->get_messages_default(); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Enqueue editor assets |
| 283 | * |
| 284 | * @return void |
| 285 | */ |
| 286 | public function enqueue_assets() { |
| 287 | do_action( 'jet-form-builder/editor-package/before', $this, self::EDITOR_PACKAGE_HANDLE ); |
| 288 | |
| 289 | wp_enqueue_script( |
| 290 | self::EDITOR_PACKAGE_HANDLE, |
| 291 | JET_FORM_BUILDER_URL . 'assets/js/package.js', |
| 292 | array( |
| 293 | 'wp-editor', |
| 294 | 'wp-core-data', |
| 295 | 'wp-data', |
| 296 | 'wp-block-library', |
| 297 | 'wp-format-library', |
| 298 | 'wp-api-fetch', |
| 299 | ), |
| 300 | JET_FORM_BUILDER_VERSION, |
| 301 | true |
| 302 | ); |
| 303 | |
| 304 | wp_set_script_translations( |
| 305 | self::EDITOR_PACKAGE_HANDLE, |
| 306 | 'jet-form-builder', |
| 307 | Plugin::instance()->plugin_dir( 'languages' ) |
| 308 | ); |
| 309 | |
| 310 | do_action( 'jet-form-builder/editor-assets/before', $this, self::EDITOR_HANDLE ); |
| 311 | |
| 312 | wp_enqueue_script( |
| 313 | self::EDITOR_HANDLE, |
| 314 | JET_FORM_BUILDER_URL . 'assets/js/editor.js', |
| 315 | array(), |
| 316 | JET_FORM_BUILDER_VERSION, |
| 317 | true |
| 318 | ); |
| 319 | |
| 320 | wp_enqueue_style( |
| 321 | self::EDITOR_HANDLE, |
| 322 | JET_FORM_BUILDER_URL . 'assets/css/editor.css', |
| 323 | array( |
| 324 | 'media', |
| 325 | 'l10n', |
| 326 | 'buttons', |
| 327 | 'wp-edit-blocks', |
| 328 | 'wp-editor', |
| 329 | ), |
| 330 | JET_FORM_BUILDER_VERSION, |
| 331 | 'all' |
| 332 | ); |
| 333 | |
| 334 | $conditions_settings = jet_form_builder()->form_handler->action_handler->condition_manager()->get_settings(); |
| 335 | |
| 336 | wp_localize_script( |
| 337 | self::EDITOR_PACKAGE_HANDLE, |
| 338 | 'JetFormEditorData', |
| 339 | array( |
| 340 | 'presetConfig' => $this->get_preset_config(), |
| 341 | 'messagesDefault' => $this->get_messages_default(), |
| 342 | 'gateways' => Gateway_Manager::instance()->editor_data(), |
| 343 | 'helpForRepeaters' => $this->get_help_for_repeaters(), |
| 344 | 'global_settings' => Tab_Handler_Manager::instance()->all(), |
| 345 | 'jetEngineVersion' => Tools::get_jet_engine_version(), |
| 346 | 'actionConditionSettings' => $conditions_settings, |
| 347 | 'argumentsSource' => Tools::get_form_settings_options() |
| 348 | ) |
| 349 | ); |
| 350 | |
| 351 | do_action( 'jet-form-builder/editor-assets/after', $this, self::EDITOR_HANDLE ); |
| 352 | } |
| 353 | |
| 354 | private function get_help_for_repeaters() { |
| 355 | return array( |
| 356 | 'conditional_block' => array( |
| 357 | 'label' => __( 'With many conditions for the block, they are checked with the AND operator', 'jet-form-builder' ), |
| 358 | ), |
| 359 | 'conditional_block_or' => array( |
| 360 | 'label' => __( 'With many conditions for the block, they are checked with the OR operator', 'jet-form-builder' ), |
| 361 | ), |
| 362 | 'conditional_action' => array( |
| 363 | 'label' => __( 'With many conditions for the action, they are checked with the AND operator', 'jet-form-builder' ), |
| 364 | ), |
| 365 | 'conditional_action_or' => array( |
| 366 | 'label' => __( 'With many conditions for the action, they are checked with the OR operator', 'jet-form-builder' ), |
| 367 | ), |
| 368 | ); |
| 369 | } |
| 370 | |
| 371 | public function enqueue_form_assets() { |
| 372 | |
| 373 | $handle = 'jet-form-builder/form'; |
| 374 | |
| 375 | do_action( 'jet-form-builder/other-editor-assets/before', $this, $handle ); |
| 376 | |
| 377 | wp_enqueue_script( |
| 378 | $handle, |
| 379 | JET_FORM_BUILDER_URL . 'assets/js/form-block.js', |
| 380 | array( |
| 381 | 'wp-editor', |
| 382 | 'wp-core-data', |
| 383 | 'wp-data', |
| 384 | 'wp-block-library', |
| 385 | 'wp-format-library', |
| 386 | 'wp-api-fetch', |
| 387 | ), |
| 388 | JET_FORM_BUILDER_VERSION, |
| 389 | true |
| 390 | ); |
| 391 | |
| 392 | wp_enqueue_style( |
| 393 | 'jet-form-builder-others', |
| 394 | Plugin::instance()->plugin_url( 'assets/css/frontend.css' ), |
| 395 | array(), |
| 396 | Plugin::instance()->get_version() |
| 397 | ); |
| 398 | |
| 399 | do_action( 'jet-form-builder/other-editor-assets/after', $this, $handle ); |
| 400 | |
| 401 | } |
| 402 | |
| 403 | } |
| 404 |