View.php
97 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Tribe__Promoter__View |
| 5 | */ |
| 6 | class Tribe__Promoter__View extends Tribe__Template { |
| 7 | |
| 8 | /** |
| 9 | * Tribe__Promoter__View constructor. |
| 10 | * |
| 11 | * @since 4.9 |
| 12 | */ |
| 13 | public function __construct() { |
| 14 | $this->set_template_origin( Tribe__Main::instance() ); |
| 15 | $this->set_template_folder( 'src/views/promoter' ); |
| 16 | $this->set_template_context_extract( true ); |
| 17 | $this->set_template_folder_lookup( true ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Add the rewrite rules and tags. |
| 22 | * |
| 23 | * @since 4.9 |
| 24 | */ |
| 25 | public function add_rewrites() { |
| 26 | add_rewrite_rule( 'tribe-promoter-auth/?$', 'index.php?tribe-promoter-auth-check=1', 'top' ); |
| 27 | add_rewrite_tag( '%tribe-promoter-auth-check%', '([^&]+)' ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Get the redirect URL for finishing onboarding |
| 32 | * |
| 33 | * @since 4.9.6 |
| 34 | * |
| 35 | * @return string Redirect URL for completing onboarding. |
| 36 | */ |
| 37 | public function authorized_redirect_url() { |
| 38 | $url = 'https://promoter.theeventscalendar.com/welcome/review'; |
| 39 | |
| 40 | if ( defined( 'TRIBE_PROMOTER_AUTHORIZED_REDIRECT_URL' ) ) { |
| 41 | $url = TRIBE_PROMOTER_AUTHORIZED_REDIRECT_URL; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * The url for redirecting in order to complete onboarding |
| 46 | * |
| 47 | * @since 4.9.6 |
| 48 | * |
| 49 | * @param string $url Redirect URL. |
| 50 | */ |
| 51 | return apply_filters( 'tribe_promoter_authorized_redirect_url', $url ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Display the auth check page when the correct permalink is loaded. |
| 56 | * |
| 57 | * @since 4.9 |
| 58 | */ |
| 59 | public function display_auth_check_view() { |
| 60 | global $wp_query; |
| 61 | |
| 62 | $promoter_key = tribe_get_request_var( 'promoter_key' ); |
| 63 | $license_key = tribe_get_request_var( 'license_key' ); |
| 64 | |
| 65 | if ( empty( $promoter_key ) || empty( $wp_query->query_vars['tribe-promoter-auth-check'] ) ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | $is_admin = is_user_logged_in() && current_user_can( 'manage_options' ) && current_user_can( 'read_private_posts' ); |
| 70 | $authorized = false; |
| 71 | $auth_error = false; |
| 72 | |
| 73 | if ( $is_admin && ! empty( $_POST['promoter_authenticate'] ) ) { |
| 74 | /** @var Tribe__Promoter__Auth $promoter_auth */ |
| 75 | $promoter_auth = tribe( 'promoter.auth' ); |
| 76 | $authorized = $promoter_auth->authorize_with_connector(); |
| 77 | $auth_error = ! $authorized; |
| 78 | } |
| 79 | |
| 80 | if ( $authorized ) { |
| 81 | wp_redirect( esc_url_raw( $this->authorized_redirect_url() ) ); |
| 82 | } else { |
| 83 | $this->template( 'auth', [ |
| 84 | 'authorized' => $authorized, |
| 85 | 'auth_error' => $auth_error, |
| 86 | 'logged_in' => is_user_logged_in(), |
| 87 | 'admin' => $is_admin, |
| 88 | 'promoter_key' => $promoter_key, |
| 89 | 'license_key' => $license_key, |
| 90 | ], true ); |
| 91 | } |
| 92 | |
| 93 | tribe_exit(); |
| 94 | } |
| 95 | |
| 96 | } |
| 97 |