Exemple #1
0
/**
 * User settings content.
 *
 * Displayed on:
 *  1. A user's BuddyPress settings page - "Settings > General"
 *  2. A user's admin dashboard page - "Users > My Profile"
 */
function mexp_gdrive_user_settings_content()
{
    ?>

	<?php 
    if ('' === mexp_gdrive_get_refresh_token()) {
        ?>

		<div id="gauth">
			<button id="signinButton"><img src="https://developers.google.com/identity/images/btn_google_signin_dark_normal_web.png" alt="<?php 
        _e('Sign in with Google', 'gdrive');
        ?>
" /></button>
			<p class="description"><?php 
        _e("We'll open a new page to help you connect to your Google Drive account.", 'gdrive');
        ?>
</p>

			<p class="description"><?php 
        printf(__('Authenticating will allow you to easily embed items from your Google Drive via the familiar "%sAdd Media%s" button when writing new posts.', 'gdrive'), '<strong>', '</strong>');
        ?>
</p>
		</div>

		<script type="text/javascript">
		jQuery('#signinButton').click( function( e ) {
			e.preventDefault();

			auth2.grantOfflineAccess( {'redirect_uri': 'postmessage'} ).then( function( authResult ) {
				if ( authResult['code'] ) {
					// save the refresh token
					jQuery.post( ajaxurl, {
						action: 'mexp-gdrive-oauth',
						type: 'not-media',
						code: authResult['code']
					}, function( response ) {
						jQuery( '#gauth' ).html( response.data.message );
					} );
				}
			} );
		} );
		</script>

	<?php 
    } else {
        ?>

		<div id="gauth">
			<p><?php 
        _e('You have allowed us to access your Google Drive.', 'gdrive');
        ?>
</p>

			<p><?php 
        printf(__('To easily embed items from your Google Drive, click on the %s button when you are creating a new post.  Next, click on the %s link and proceed from there.', 'gdrive'), '<strong>' . __('Add Media', 'gdrive') . '</strong>', '<strong>' . __('Insert from Google Drive', 'gdrive') . '</strong>');
        ?>
</p>

			<p><?php 
        _e('To disallow access to your Google Drive, click on the button below:', 'gdrive');
        ?>
</p>

			<button type="button" class="button button-secondary" id="gauth-revoke" data-nonce="<?php 
        echo wp_create_nonce('mexp-gdrive-revoke');
        ?>
"><?php 
        _e('Disallow access', 'gdrive');
        ?>
</button>
		</div>

		<script type="text/javascript">
		jQuery('#gauth-revoke').click( function( e ) {
			e.preventDefault();

			jQuery.post( ajaxurl, {
				action: 'mexp-gdrive-revoke',
				'_ajax_nonce' : e.currentTarget.dataset.nonce
			}, function( response ) {
				jQuery( '#gauth' ).html( response.data.message );
			} );
		} );
		</script>

	<?php 
    }
    ?>

<?php 
}
Exemple #2
0
 /**
  * Loads the Google PHP API.
  *
  * @param  bool $auth Should we attempt to authenticate as well?
  * @return bool|WP_Error Boolean true on success. WP_Error object on failure.
  */
 protected function load_gapi($auth = false)
 {
     if (file_exists(MEXP_GDrive::$PATH . '/vendor/autoload.php')) {
         require_once MEXP_GDrive::$PATH . '/vendor/autoload.php';
     } else {
         return new WP_Error('mexp_gdrive_lib_required', __('Please install the Google PHP API Client.  You can do this by running "composer install" in your console from the "gdrive" directory', 'gdrive'));
     }
     // set up the client
     $this->client = new Google_Client();
     $this->client->setClientId($this->client_id);
     $this->client->setClientSecret($this->client_secret);
     $this->client->addScope('https://www.googleapis.com/auth/drive');
     // attempt to authorize
     if (true === $auth) {
         // user has already authorized app; so set access token
         $refresh_token = mexp_gdrive_get_refresh_token();
         if (!empty($refresh_token)) {
             // set access token
             try {
                 $this->client->refreshToken($refresh_token);
                 $access_token = $this->client->getAccessToken();
                 // set access token
                 if (!empty($access_token)) {
                     $this->client->setAccessToken($access_token);
                 }
                 // user has revoked access
             } catch (Google_Auth_Exception $e) {
                 delete_user_meta(get_current_user_id(), 'gdu_refresh_token');
                 do_action('mexp_gdrive_delete_refresh_token');
                 return new WP_Error('mexp_gdrive_revoke', __('You have revoked access to Google Drive.  Please refresh the page to start the authentication process again.', 'gdrive'));
             }
         }
     }
     return true;
 }