display-conditions
5 months ago
front
2 days ago
helpers
1 month ago
metas
3 months ago
multisite
5 months ago
palettes
3 years ago
provider
1 month ago
providers
2 days ago
templates
3 years ago
update
5 months ago
class-hustle-admin-page-abstract.php
1 month ago
class-hustle-auth-token.php
5 months ago
class-hustle-condition-factory.php
6 years ago
class-hustle-cross-sell.php
10 months ago
class-hustle-dashboard-admin.php
1 month ago
class-hustle-data.php
1 month ago
class-hustle-db.php
2 years ago
class-hustle-installer.php
4 years ago
class-hustle-meta.php
3 years ago
class-hustle-module-admin.php
1 month ago
class-hustle-module-collection.php
5 months ago
class-hustle-module-fields.php
3 months ago
class-hustle-module-page-abstract.php
1 month ago
class-hustle-module-validator.php
3 months ago
class-hustle-notifications.php
2 days ago
class-hustle-settings-admin.php
1 month ago
class-hustle-wp-dashboard-page.php
5 months ago
class-opt-in.php
2 days ago
hustle-background-conversion-log.php
3 months ago
hustle-deletion.php
3 months ago
hustle-embedded-admin.php
3 years ago
hustle-entries-admin.php
5 months ago
hustle-entry-model.php
1 month ago
hustle-general-data-protection.php
5 months ago
hustle-init.php
1 month ago
hustle-mail.php
5 months ago
hustle-migration.php
5 months ago
hustle-model.php
2 days ago
hustle-module-model.php
1 month ago
hustle-module-widget-legacy.php
5 months ago
hustle-module-widget.php
5 months ago
hustle-modules-common-admin-ajax.php
1 month ago
hustle-popup-admin.php
3 years ago
hustle-providers-admin.php
1 month ago
hustle-providers.php
5 months ago
hustle-settings-admin-ajax.php
1 month ago
hustle-settings-page.php
3 years ago
hustle-slidein-admin.php
3 years ago
hustle-sshare-admin.php
2 days ago
hustle-sshare-model.php
2 days ago
hustle-tracking-model.php
3 months ago
interface-hustle-auth-provider.php
5 months ago
opt-in-geo.php
5 months ago
opt-in-utils.php
2 days ago
opt-in-wpmudev-api.php
5 months ago
opt-in-wpmudev-api.php
151 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Opt_In_WPMUDEV_API |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Class Opt_In_WPMUDEV_API |
| 10 | */ |
| 11 | class Opt_In_WPMUDEV_API { |
| 12 | const DOMAIN = 'https://wpmudev.com'; |
| 13 | const REDIRECT_URI = 'https://wpmudev.com/api/hustle/v1/provider'; |
| 14 | |
| 15 | /** |
| 16 | * Nonce option name |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | private $nonce_option_name = 'hustle_custom_nonce'; |
| 21 | |
| 22 | /** |
| 23 | * Helper function to generate unique none changeable nonce. |
| 24 | * |
| 25 | * @return string The unique nonce value. |
| 26 | */ |
| 27 | public function get_nonce_value() { |
| 28 | $nonce = get_option( $this->nonce_option_name ); |
| 29 | |
| 30 | if ( empty( $nonce ) ) { |
| 31 | /** |
| 32 | * Generate the nonce value only once to avoid error response |
| 33 | * when retrieving access token. |
| 34 | */ |
| 35 | $nonce = wp_generate_password( 40, false, false ); |
| 36 | |
| 37 | update_option( $this->nonce_option_name, $nonce ); |
| 38 | } |
| 39 | |
| 40 | return $nonce; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Helper function to validate nonce value. |
| 45 | * |
| 46 | * @param string $nonce Nonce. |
| 47 | * |
| 48 | * @return bool |
| 49 | */ |
| 50 | public function verify_nonce( $nonce ) { |
| 51 | return $nonce === $this->get_nonce_value(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get redirect URL |
| 56 | * |
| 57 | * @param string $provider Provider. |
| 58 | * @param string $action Action. |
| 59 | * @param array $params Params. |
| 60 | * @param bool $migration Migration. |
| 61 | * @return string |
| 62 | */ |
| 63 | public function redirect_uri( $provider, $action, $params = array(), $migration = 0 ) { |
| 64 | $params = wp_parse_args( |
| 65 | $params, |
| 66 | array( |
| 67 | 'action' => $action, |
| 68 | 'provider' => $provider, |
| 69 | 'wpnonce' => $this->get_nonce_value(), |
| 70 | 'migrate' => true === $migration ? 1 : 0, |
| 71 | 'state' => $this->prepare_state_param(), |
| 72 | ) |
| 73 | ); |
| 74 | |
| 75 | return add_query_arg( $params, self::get_remote_api_url() ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Prepare state param for OAuth requests. |
| 80 | * |
| 81 | * @return string |
| 82 | */ |
| 83 | protected function prepare_state_param() { |
| 84 | $state_data = $this->get_state_params(); |
| 85 | return base64_encode( wp_json_encode( $state_data ) ); // phpcs:ignore |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get state params |
| 90 | * |
| 91 | * @return array |
| 92 | */ |
| 93 | protected function get_state_params() { |
| 94 | return array( |
| 95 | 'nonce' => $this->get_nonce_value(), |
| 96 | 'redirect_uri' => site_url( '/' ), |
| 97 | 'hub_api_key' => Opt_In_Utils::get_hub_api_key(), |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Validates request callback from WPMU DEV |
| 103 | * |
| 104 | * @param string $provider Provider. |
| 105 | * @return bool |
| 106 | */ |
| 107 | public function validate_callback_request( $provider ) { |
| 108 | $wpnonce = filter_input( INPUT_GET, 'wpnonce', FILTER_SANITIZE_SPECIAL_CHARS ); |
| 109 | $domain = filter_input( INPUT_GET, 'domain', FILTER_VALIDATE_URL ); |
| 110 | $provider_input = filter_input( INPUT_GET, 'provider', FILTER_SANITIZE_SPECIAL_CHARS ); |
| 111 | |
| 112 | return ! empty( $wpnonce ) && $this->verify_nonce( $wpnonce ) |
| 113 | && self::DOMAIN === $domain && $provider === $provider_input; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Print error page on failed integration. |
| 118 | * |
| 119 | * @param string $message Message. |
| 120 | * @param string $retry_url Retry URL. |
| 121 | * @param string $cancel_url Cancel URL. |
| 122 | */ |
| 123 | public function api_die( $message, $retry_url = '', $cancel_url = '' ) { |
| 124 | $html = sprintf( '<p><img src="%s" /></p>', esc_url( Opt_In::$plugin_url . 'assets/img/hustle.png' ) ); |
| 125 | $html .= sprintf( '<p>%s</p>', esc_html( $message ) ); |
| 126 | |
| 127 | if ( ! empty( $retry_url ) ) { |
| 128 | $html .= sprintf( '<a href="%s" class="button button-large">%s</a>', esc_url( $retry_url ), esc_html__( 'Retry', 'hustle' ) ); } |
| 129 | |
| 130 | if ( ! empty( $cancel_url ) ) { |
| 131 | $html .= sprintf( ' <a href="%s" class="button button-large">%s</a>', esc_url( $cancel_url ), esc_html__( 'Cancel', 'hustle' ) ); } |
| 132 | |
| 133 | $html = sprintf( '<div style="text-align: center;">%s</div>', $html ); |
| 134 | |
| 135 | /* translators: Plugin name */ |
| 136 | wp_die( wp_kses_post( $html ), esc_html( sprintf( __( '%s failure notice.', 'hustle' ), Opt_In_Utils::get_plugin_name() ) ), 403 ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get WPMU DEV API URL |
| 141 | * |
| 142 | * @return string |
| 143 | */ |
| 144 | public static function get_remote_api_url() { |
| 145 | if ( defined( 'WPMUDEV_CUSTOM_API_SERVER' ) && ! empty( WPMUDEV_CUSTOM_API_SERVER ) ) { |
| 146 | return untrailingslashit( WPMUDEV_CUSTOM_API_SERVER ) . '/api/hustle/v1/provider'; |
| 147 | } |
| 148 | return self::REDIRECT_URI; |
| 149 | } |
| 150 | } |
| 151 |