redirect-to-page-action.php
124 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Modules\Actions_V2\Redirect_To_Page; |
| 4 | |
| 5 | // If this file is called directly, abort. |
| 6 | use Jet_Form_Builder\Actions\Action_Handler; |
| 7 | use Jet_Form_Builder\Actions\Types\Base; |
| 8 | use Jet_Form_Builder\Classes\Tools; |
| 9 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 10 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 11 | use JFB_Modules\Actions_V2\Insert_Post\Insert_Post_Action; |
| 12 | use JFB_Modules\Rich_Content; |
| 13 | |
| 14 | if ( ! defined( 'WPINC' ) ) { |
| 15 | die; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Define Base_Type class |
| 20 | */ |
| 21 | class Redirect_To_Page_Action extends Base { |
| 22 | |
| 23 | public function get_name() { |
| 24 | return __( 'Redirect to Page', 'jet-form-builder' ); |
| 25 | } |
| 26 | |
| 27 | public function get_id() { |
| 28 | return 'redirect_to_page'; |
| 29 | } |
| 30 | |
| 31 | public function get_redirect_url() { |
| 32 | $type = ! empty( $this->settings['redirect_type'] ) ? $this->settings['redirect_type'] : 'static_page'; |
| 33 | |
| 34 | switch ( $type ) { |
| 35 | case 'static_page': |
| 36 | $to_page = $this->settings['redirect_page'] ?? ''; |
| 37 | |
| 38 | return empty( $to_page ) ? false : get_permalink( $to_page ); |
| 39 | |
| 40 | case 'current_page': |
| 41 | return jet_fb_handler()->refer; |
| 42 | |
| 43 | case 'inserted_post': |
| 44 | /** @var Insert_Post_Action $insert_instance */ |
| 45 | try { |
| 46 | $insert_instance = jet_form_builder()->actions->get_action( 'insert_post' ); |
| 47 | } catch ( Repository_Exception $exception ) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | $context = $insert_instance->get_inserted_post_context(); |
| 52 | |
| 53 | if ( ! $context ) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | return get_permalink( $context['ID'] ); |
| 58 | |
| 59 | default: |
| 60 | $this->settings['redirect_url'] = Tools::to_string( |
| 61 | $this->settings['redirect_url'] ?? '' |
| 62 | ); |
| 63 | |
| 64 | return Rich_Content\Module::rich( $this->settings['redirect_url'] ); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public function get_url_with_hash( $url ) { |
| 69 | if ( empty( $this->settings['redirect_hash'] ) ) { |
| 70 | return $url; |
| 71 | } |
| 72 | |
| 73 | return trailingslashit( $url ) . '#' . $this->settings['redirect_hash']; |
| 74 | } |
| 75 | |
| 76 | public function get_url_with_args( $url ) { |
| 77 | $redirect_args = array(); |
| 78 | |
| 79 | if ( ! empty( $this->settings['redirect_args'] ) ) { |
| 80 | foreach ( $this->settings['redirect_args'] as $arg ) { |
| 81 | $value = jet_fb_context()->get_value( $arg ); |
| 82 | |
| 83 | $redirect_args[ $arg ] = ! empty( $value ) ? $value : 0; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | $redirect_args = apply_filters( 'jet-form-builder/actions/redirect-to-page/redirect-args', $redirect_args, $this ); |
| 88 | |
| 89 | return add_query_arg( urlencode_deep( $redirect_args ), $url ); |
| 90 | } |
| 91 | |
| 92 | public function get_completed_redirect_url( $url = '' ) { |
| 93 | if ( ! $url ) { |
| 94 | $url = $this->get_redirect_url(); |
| 95 | } |
| 96 | $url = $this->get_url_with_hash( $url ); |
| 97 | |
| 98 | return $this->get_url_with_args( $url ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @param array $request |
| 103 | * @param Action_Handler $handler |
| 104 | * |
| 105 | * @return mixed|void |
| 106 | * @throws Action_Exception |
| 107 | */ |
| 108 | public function do_action( array $request, Action_Handler $handler ) { |
| 109 | $to_url = $this->get_redirect_url(); |
| 110 | |
| 111 | if ( ! $to_url ) { |
| 112 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 113 | throw new Action_Exception( 'failed', $this->settings ); |
| 114 | } |
| 115 | |
| 116 | $to_url = $this->get_completed_redirect_url( $to_url ); |
| 117 | |
| 118 | $handler->response_data['open_in_new_tab'] = ! empty( $this->settings['open_in_new_tab'] ); |
| 119 | |
| 120 | $handler->response_data['redirect'] = $to_url; |
| 121 | } |
| 122 | |
| 123 | } |
| 124 |