display-conditions
5 years ago
front
5 years ago
helpers
5 years ago
metas
5 years ago
palettes
5 years ago
provider
5 years ago
providers
5 years ago
templates
5 years ago
update
5 years ago
class-hustle-admin-page-abstract.php
5 years ago
class-hustle-condition-factory.php
6 years ago
class-hustle-dashboard-admin.php
5 years ago
class-hustle-data.php
5 years ago
class-hustle-db.php
6 years ago
class-hustle-module-admin.php
5 years ago
class-hustle-module-collection.php
5 years ago
class-hustle-module-decorator.php
5 years ago
class-hustle-module-page-abstract.php
5 years ago
class-hustle-notifications.php
5 years ago
class-hustle-settings-admin.php
5 years ago
class-hustle-upsell-page.php
5 years ago
class-hustle-wp-dashboard-page.php
5 years ago
hustle-collection.php
6 years ago
hustle-deletion.php
5 years ago
hustle-embedded-admin.php
6 years ago
hustle-entries-admin.php
5 years ago
hustle-entry-model.php
5 years ago
hustle-general-data-protection.php
6 years ago
hustle-init.php
5 years ago
hustle-mail.php
5 years ago
hustle-meta.php
5 years ago
hustle-migration.php
5 years ago
hustle-model.php
5 years ago
hustle-module-model.php
5 years ago
hustle-module-widget-legacy.php
5 years ago
hustle-module-widget.php
5 years ago
hustle-modules-common-admin-ajax.php
5 years ago
hustle-popup-admin.php
6 years ago
hustle-providers-admin.php
5 years ago
hustle-providers.php
6 years ago
hustle-settings-admin-ajax.php
5 years ago
hustle-settings-page.php
5 years ago
hustle-slidein-admin.php
6 years ago
hustle-sshare-admin.php
5 years ago
hustle-sshare-model.php
5 years ago
hustle-tracking-model.php
5 years ago
opt-in-geo.php
5 years ago
opt-in-utils.php
5 years ago
opt-in-wpmudev-api.php
6 years ago
opt-in-wpmudev-api.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | class Opt_In_WPMUDEV_API { |
| 4 | const DOMAIN = 'https://premium.wpmudev.org'; |
| 5 | const REDIRECT_URI = 'https://premium.wpmudev.org/api/hustle/v1/provider'; |
| 6 | |
| 7 | /** |
| 8 | * @var string |
| 9 | */ |
| 10 | private $nonce_option_name = 'hustle_custom_nonce'; |
| 11 | |
| 12 | /** |
| 13 | * Helper function to generate unique none changeable nonce. |
| 14 | * |
| 15 | * @return string The unique nonce value. |
| 16 | */ |
| 17 | public function get_nonce_value() { |
| 18 | $nonce = is_multisite() ? get_option( $this->nonce_option_name ) : get_option( $this->nonce_option_name ); |
| 19 | |
| 20 | if ( empty( $nonce ) ) { |
| 21 | /** |
| 22 | * Generate the nonce value only once to avoid error response |
| 23 | * when retrieving access token. |
| 24 | */ |
| 25 | $nonce = wp_generate_password( 40, false, false ); |
| 26 | |
| 27 | if ( is_multisite() ) { |
| 28 | update_option( $this->nonce_option_name, $nonce ); |
| 29 | } else { |
| 30 | update_option( $this->nonce_option_name, $nonce ); } |
| 31 | } |
| 32 | |
| 33 | return $nonce; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Helper function to validate nonce value. |
| 38 | * |
| 39 | * @param string $nonce |
| 40 | * |
| 41 | * @return bool |
| 42 | */ |
| 43 | public function verify_nonce( $nonce ) { |
| 44 | return $nonce === $this->get_nonce_value(); |
| 45 | } |
| 46 | |
| 47 | public function _get_redirect_uri( $provider, $action, $params = array(), $migration = 0 ) { |
| 48 | $params = wp_parse_args( |
| 49 | $params, |
| 50 | array( |
| 51 | 'action' => $action, |
| 52 | 'provider' => $provider, |
| 53 | 'wpnonce' => $this->get_nonce_value(), |
| 54 | 'migrate' => true === $migration ? 1 : 0, |
| 55 | 'redirect' => site_url( '/' ), |
| 56 | ) |
| 57 | ); |
| 58 | |
| 59 | return add_query_arg( $params, self::REDIRECT_URI ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Validates request callback from WPMU DEV |
| 64 | * |
| 65 | * @return bool |
| 66 | */ |
| 67 | public function validate_callback_request( $provider ) { |
| 68 | $wpnonce = filter_input( INPUT_GET, 'wpnonce', FILTER_SANITIZE_STRING ); |
| 69 | $domain = filter_input( INPUT_GET, 'domain', FILTER_VALIDATE_URL ); |
| 70 | $provider_input = filter_input( INPUT_GET, 'provider' ); |
| 71 | |
| 72 | return ! empty( $wpnonce ) && $this->verify_nonce( $wpnonce ) |
| 73 | && self::DOMAIN === $domain && $provider === $provider_input; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Print error page on failed integration. |
| 78 | * |
| 79 | * @param string $message |
| 80 | * @param string $retry_url |
| 81 | * @param string $cancel_url |
| 82 | */ |
| 83 | public function wp_die( $message, $retry_url = '', $cancel_url = '' ) { |
| 84 | //phpcs:ignore |
| 85 | $html = sprintf( '<p><img src="%s" /></p>', Opt_In::$plugin_url . 'assets/img/hustle.png' ); |
| 86 | $html .= sprintf( '<p>%s</p>', $message ); |
| 87 | |
| 88 | if ( ! empty( $retry_url ) ) { |
| 89 | $html .= sprintf( '<a href="%s" class="button button-large">%s</a>', esc_url( $retry_url ), __( 'Retry', 'hustle' ) ); } |
| 90 | |
| 91 | if ( ! empty( $cancel_url ) ) { |
| 92 | $html .= sprintf( ' <a href="%s" class="button button-large">%s</a>', esc_url( $cancel_url ), __( 'Cancel', 'hustle' ) ); } |
| 93 | |
| 94 | $html = sprintf( '<div style="text-align: center;">%s</div>', $html ); |
| 95 | |
| 96 | wp_die( esc_html( $html ), esc_html__( 'Hustle failure notice.', 'hustle' ), 403 ); |
| 97 | } |
| 98 | } |
| 99 |