redirection-create-login-redirect.php
3 months ago
redirection-create-role-redirect.php
3 months ago
redirection-create-url-redirect.php
3 months ago
redirection-delete-redirect.php
3 months ago
redirection-toggle-redirect.php
3 months ago
redirection-toggle-redirect.php
133 lines
| 1 | <?php |
| 2 | /** |
| 3 | * RedirectionToggleRedirect. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category RedirectionToggleRedirect |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\Redirection\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Integrations\Redirection\Redirection; |
| 19 | use SureTriggers\Traits\SingletonLoader; |
| 20 | |
| 21 | /** |
| 22 | * RedirectionToggleRedirect |
| 23 | * |
| 24 | * @category RedirectionToggleRedirect |
| 25 | * @package SureTriggers |
| 26 | * @author BSF <username@example.com> |
| 27 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 | * @link https://www.brainstormforce.com/ |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | class RedirectionToggleRedirect extends AutomateAction { |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'Redirection'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'rd_toggle_redirect'; |
| 46 | |
| 47 | use SingletonLoader; |
| 48 | |
| 49 | /** |
| 50 | * Register a action. |
| 51 | * |
| 52 | * @param array $actions actions. |
| 53 | * @return array |
| 54 | */ |
| 55 | public function register( $actions ) { |
| 56 | $actions[ $this->integration ][ $this->action ] = [ |
| 57 | 'label' => __( 'Enable or Disable Redirect', 'suretriggers' ), |
| 58 | 'action' => $this->action, |
| 59 | 'function' => [ $this, 'action_listener' ], |
| 60 | ]; |
| 61 | return $actions; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Action listener. |
| 66 | * |
| 67 | * @param int $user_id user_id. |
| 68 | * @param int $automation_id automation_id. |
| 69 | * @param array $fields fields. |
| 70 | * @param array $selected_options selectedOptions. |
| 71 | * @throws Exception Throws exception. |
| 72 | * |
| 73 | * @return array |
| 74 | */ |
| 75 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 76 | if ( ! class_exists( 'Red_Item' ) ) { |
| 77 | return [ |
| 78 | 'status' => esc_attr__( 'Error', 'suretriggers' ), |
| 79 | 'response' => esc_attr__( 'Redirection plugin is not active.', 'suretriggers' ), |
| 80 | ]; |
| 81 | } |
| 82 | |
| 83 | $redirect_id = isset( $selected_options['redirect_id'] ) ? absint( $selected_options['redirect_id'] ) : 0; |
| 84 | $toggle = isset( $selected_options['toggle'] ) ? sanitize_text_field( $selected_options['toggle'] ) : ''; |
| 85 | |
| 86 | if ( empty( $redirect_id ) ) { |
| 87 | return [ |
| 88 | 'status' => esc_attr__( 'Error', 'suretriggers' ), |
| 89 | 'response' => esc_attr__( 'Redirect ID is required.', 'suretriggers' ), |
| 90 | ]; |
| 91 | } |
| 92 | |
| 93 | if ( ! in_array( $toggle, [ 'enable', 'disable' ], true ) ) { |
| 94 | return [ |
| 95 | 'status' => esc_attr__( 'Error', 'suretriggers' ), |
| 96 | 'response' => esc_attr__( 'Toggle value must be "enable" or "disable".', 'suretriggers' ), |
| 97 | ]; |
| 98 | } |
| 99 | |
| 100 | $redirect = \Red_Item::get_by_id( $redirect_id ); |
| 101 | if ( false === $redirect ) { |
| 102 | return [ |
| 103 | 'status' => esc_attr__( 'Error', 'suretriggers' ), |
| 104 | 'response' => esc_attr__( 'Redirect not found with the specified ID.', 'suretriggers' ), |
| 105 | ]; |
| 106 | } |
| 107 | |
| 108 | if ( 'enable' === $toggle ) { |
| 109 | $redirect->enable(); |
| 110 | } else { |
| 111 | $redirect->disable(); |
| 112 | } |
| 113 | |
| 114 | $updated_redirect = \Red_Item::get_by_id( $redirect_id ); |
| 115 | if ( false === $updated_redirect ) { |
| 116 | return [ |
| 117 | 'status' => esc_attr__( 'Error', 'suretriggers' ), |
| 118 | 'response' => esc_attr__( 'Failed to retrieve updated redirect.', 'suretriggers' ), |
| 119 | ]; |
| 120 | } |
| 121 | |
| 122 | return [ |
| 123 | 'status' => esc_attr__( 'Success', 'suretriggers' ), |
| 124 | 'response' => 'enable' === $toggle |
| 125 | ? esc_attr__( 'Redirect enabled successfully.', 'suretriggers' ) |
| 126 | : esc_attr__( 'Redirect disabled successfully.', 'suretriggers' ), |
| 127 | 'redirect' => Redirection::get_redirect_context( $updated_redirect ), |
| 128 | ]; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | RedirectionToggleRedirect::get_instance(); |
| 133 |