| @@ -1,0 +1,207 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Admin | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\AWeber\Admin | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Shortcut function to get plugin options | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @param string $option_name | |
| 18 | + * @param bool $default | |
| 19 | + * | |
| 20 | + * @return mixed | |
| 21 | + */ | |
| 22 | +function automatorwp_aweber_get_option( $option_name, $default = false ) { | |
| 23 | + | |
| 24 | + $prefix = 'automatorwp_aweber_'; | |
| 25 | + | |
| 26 | + return automatorwp_get_option( $prefix . $option_name, $default ); | |
| 27 | +} | |
| 28 | + | |
| 29 | +/** | |
| 30 | + * Register plugin settings sections | |
| 31 | + * | |
| 32 | + * @since 1.0.0 | |
| 33 | + * | |
| 34 | + * @return array | |
| 35 | + */ | |
| 36 | +function automatorwp_aweber_settings_sections( $automatorwp_settings_sections ) { | |
| 37 | + | |
| 38 | + $automatorwp_settings_sections['aweber'] = array( | |
| 39 | + 'title' => __( 'AWeber', 'automatorwp-aweber' ), | |
| 40 | + 'icon' => 'dashicons-aweber', | |
| 41 | + ); | |
| 42 | + | |
| 43 | + return $automatorwp_settings_sections; | |
| 44 | + | |
| 45 | +} | |
| 46 | +add_filter( 'automatorwp_settings_sections', 'automatorwp_aweber_settings_sections' ); | |
| 47 | + | |
| 48 | +/** | |
| 49 | + * Register plugin settings meta boxes | |
| 50 | + * | |
| 51 | + * @since 1.0.0 | |
| 52 | + * | |
| 53 | + * @return array | |
| 54 | + */ | |
| 55 | +function automatorwp_aweber_settings_meta_boxes( $meta_boxes ) { | |
| 56 | + | |
| 57 | + $prefix = 'automatorwp_aweber_'; | |
| 58 | + | |
| 59 | + $meta_boxes['automatorwp-aweber-settings'] = array( | |
| 60 | + 'title' => automatorwp_dashicon( 'email' ) . __( 'AWeber', 'automatorwp-aweber' ), | |
| 61 | + 'fields' => apply_filters( 'automatorwp_aweber_settings_fields', array( | |
| 62 | + $prefix . 'client_id' => array( | |
| 63 | + 'name' => __( 'Client ID:', 'automatorwp-aweber' ), | |
| 64 | + 'desc' => __( 'Your AWeber API client ID.', 'automatorwp-aweber' ), | |
| 65 | + 'type' => 'text', | |
| 66 | + ), | |
| 67 | + $prefix . 'client_secret' => array( | |
| 68 | + 'name' => __( 'Client Secret:', 'automatorwp-aweber' ), | |
| 69 | + 'desc' => __( 'Your AWeber API client secret.', 'automatorwp-aweber' ), | |
| 70 | + 'type' => 'text', | |
| 71 | + ), | |
| 72 | + $prefix . 'redirect_url' => array( | |
| 73 | + 'type' => 'text', | |
| 74 | + 'render_row_cb' => 'automatorwp_aweber_redirect_url_display_cb', | |
| 75 | + ), | |
| 76 | + $prefix . 'authorize' => array( | |
| 77 | + 'type' => 'text', | |
| 78 | + 'render_row_cb' => 'automatorwp_aweber_authorize_display_cb', | |
| 79 | + ), | |
| 80 | + ) ), | |
| 81 | + ); | |
| 82 | + | |
| 83 | + return $meta_boxes; | |
| 84 | + | |
| 85 | +} | |
| 86 | +add_filter( "automatorwp_settings_aweber_meta_boxes", 'automatorwp_aweber_settings_meta_boxes' ); | |
| 87 | + | |
| 88 | +/** | |
| 89 | + * Display callback for the redirect URL setting | |
| 90 | + * | |
| 91 | + * @since 1.0.0 | |
| 92 | + * | |
| 93 | + * @param array $field_args Array of field arguments. | |
| 94 | + * @param CMB2_Field $field The field object | |
| 95 | + */ | |
| 96 | +function automatorwp_aweber_redirect_url_display_cb( $field_args, $field ) { | |
| 97 | + $admin_url = admin_url('admin.php?page=automatorwp_settings&tab=opt-tab-aweber'); | |
| 98 | + | |
| 99 | + ?> | |
| 100 | + <div class="cmb-row cmb-type-custom cmb2-id-automatorwp-aweber-redirect-url table-layout" data-fieldtype="custom"> | |
| 101 | + <div class="cmb-th"> | |
| 102 | + <label><?php echo __( 'Redirect URL:', 'automatorwp-aweber' ); ?></label> | |
| 103 | + </div> | |
| 104 | + <div class="cmb-td"> | |
| 105 | + <input type="text" class="regular-text" value="<?php echo $admin_url; ?>" readonly> | |
| 106 | + <p class="cmb2-metabox-description"><?php echo __( 'Copy this URL and place it in the authorized redirect URIs field.', 'automatorwp-aweber' ); ?></p> | |
| 107 | + </div> | |
| 108 | + </div> | |
| 109 | + <?php | |
| 110 | +} | |
| 111 | + | |
| 112 | +/** | |
| 113 | + * Display callback for the authorize setting | |
| 114 | + * | |
| 115 | + * @since 1.0.0 | |
| 116 | + * | |
| 117 | + * @param array $field_args Array of field arguments. | |
| 118 | + * @param CMB2_Field $field The field object | |
| 119 | + */ | |
| 120 | +function automatorwp_aweber_authorize_display_cb( $field_args, $field ) { | |
| 121 | + | |
| 122 | + $field_id = $field_args['id']; | |
| 123 | + | |
| 124 | + $client_id = automatorwp_aweber_get_option( 'client_id', '' ); | |
| 125 | + $client_secret = automatorwp_aweber_get_option( 'client_secret', '' ); | |
| 126 | + $auth = get_option( 'automatorwp_aweber_auth' ); | |
| 127 | + | |
| 128 | + ?> | |
| 129 | + <div class="cmb-row cmb-type-custom cmb2-id-automatorwp-aweber-authorize table-layout" data-fieldtype="custom"> | |
| 130 | + <div class="cmb-th"> | |
| 131 | + <label><?php echo __( 'Connect with AWeber:', 'automatorwp-aweber' ); ?></label> | |
| 132 | + </div> | |
| 133 | + <div class="cmb-td"> | |
| 134 | + <a id="<?php echo $field_id; ?>" class="button button-primary" href="#"><?php echo __( 'Authorize', 'automatorwp-aweber' ); ?></a> | |
| 135 | + <p class="cmb2-metabox-description"><?php echo __( 'Add your AWeber API client ID and secret fields and click on "Authorize" button to generate access keys for this site.', 'automatorwp-aweber' ); ?></p> | |
| 136 | + <?php if ( is_array( $auth ) ) : ?> | |
| 137 | + <div class="automatorwp-notice-success"><?php echo __( 'Site connected with AWeber successfully.', 'automatorwp-aweber' ); ?></div> | |
| 138 | + <p class="automatorwp-aweber-access-token"><strong><?php echo __( 'Access token:', 'automatorwp-aweber' ); ?></strong> <input type="text" value="<?php echo $auth['access_token']; ?>" readonly></p> | |
| 139 | + <p class="automatorwp-aweber-refresh-token"><strong><?php echo __( 'Refresh token:', 'automatorwp-aweber' ); ?></strong> <input type="text" value="<?php echo $auth['refresh_token']; ?>" readonly></p> | |
| 140 | + <?php elseif( ! empty( $client_id ) && ! empty( $client_secret ) ) : ?> | |
| 141 | + <div class="automatorwp-notice-error"><?php echo __( 'Site not connected with AWeber.', 'automatorwp-aweber' ); ?></div> | |
| 142 | + <?php endif; ?> | |
| 143 | + </div> | |
| 144 | + </div> | |
| 145 | + <?php | |
| 146 | +} | |
| 147 | + | |
| 148 | +/** | |
| 149 | + * Check if authorization process has been completed | |
| 150 | + * | |
| 151 | + * @since 1.0.0 | |
| 152 | + */ | |
| 153 | +function automatorwp_aweber_maybe_authorize_complete() { | |
| 154 | + | |
| 155 | + if( isset( $_GET['code'] ) | |
| 156 | + && isset( $_GET['page'] ) && $_GET['page'] == 'automatorwp_settings' | |
| 157 | + && isset( $_GET['tab'] ) && $_GET['tab'] == 'opt-tab-aweber' ) { | |
| 158 | + | |
| 159 | + $client_id = automatorwp_aweber_get_option( 'client_id', '' ); | |
| 160 | + $client_secret = automatorwp_aweber_get_option( 'client_secret', '' ); | |
| 161 | + | |
| 162 | + $params = array( | |
| 163 | + 'headers' => array( | |
| 164 | + 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8', | |
| 165 | + 'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $client_secret ), | |
| 166 | + 'Accept' => 'application/json', | |
| 167 | + ), | |
| 168 | + 'body' => array( | |
| 169 | + 'grant_type' => 'authorization_code', | |
| 170 | + 'redirect_uri' => str_replace( 'http://', 'http://', get_admin_url() ) . 'admin.php?page=automatorwp_settings&tab=opt-tab-aweber', | |
| 171 | + 'code' => $_GET['code'] | |
| 172 | + ) | |
| 173 | + ); | |
| 174 | + | |
| 175 | + $response = wp_remote_post( 'https://auth.aweber.com/oauth2/token', $params ); | |
| 176 | + | |
| 177 | + // Bail if can't contact with the server | |
| 178 | + if ( is_wp_error( $response ) ) { | |
| 179 | + return; | |
| 180 | + } | |
| 181 | + | |
| 182 | + $body = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 183 | + | |
| 184 | + // Bail on receive an error | |
| 185 | + if( isset( $body->error ) ) { | |
| 186 | + return; | |
| 187 | + } | |
| 188 | + | |
| 189 | + $auth = array( | |
| 190 | + 'access_token' => $body->access_token, | |
| 191 | + 'refresh_token' => $body->refresh_token, | |
| 192 | + 'token_type' => $body->token_type, | |
| 193 | + 'expires_in' => $body->expires_in, | |
| 194 | + 'scope' => $body->scope, | |
| 195 | + ); | |
| 196 | + | |
| 197 | + // Update the access and refresh tokens | |
| 198 | + update_option( 'automatorwp_aweber_auth', $auth ); | |
| 199 | + | |
| 200 | + // Redirect to settings again | |
| 201 | + wp_redirect( get_admin_url() . 'admin.php?page=automatorwp_settings&tab=opt-tab-aweber' ); | |
| 202 | + exit; | |
| 203 | + | |
| 204 | + } | |
| 205 | + | |
| 206 | +} | |
| 207 | +add_action( 'admin_init', 'automatorwp_aweber_maybe_authorize_complete' ); | |