Skip to content

Commit

Permalink
Merge pull request #421 from x-team/issue-420
Browse files Browse the repository at this point in the history
Makes the Generate new key link ajaxified
  • Loading branch information
frankiejarrett committed Apr 22, 2014
2 parents 11b0f1e + ecc1ee3 commit 7fe9eae
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 32 deletions.
4 changes: 3 additions & 1 deletion includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,11 @@ public static function admin_enqueue_scripts( $hook ) {

wp_enqueue_style( 'wp-stream-admin', WP_STREAM_URL . 'ui/admin.css', array() );

$script_screens = array( 'plugins.php', 'user-edit.php', 'user-new.php', 'profile.php' );

if ( 'index.php' === $hook ) {
wp_enqueue_script( 'wp-stream-admin-dashboard', WP_STREAM_URL . 'ui/dashboard.js', array( 'jquery', 'heartbeat' ) );
} elseif ( in_array( $hook, self::$screen_id ) || 'plugins.php' === $hook ) {
} elseif ( in_array( $hook, self::$screen_id ) || in_array( $hook, $script_screens ) ) {
wp_enqueue_script( 'select2' );
wp_enqueue_style( 'select2' );

Expand Down
112 changes: 81 additions & 31 deletions includes/feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class WP_Stream_Feeds {

const FEED_QUERY_VAR = 'stream';
const FEED_NETWORK_QUERY_VAR = 'network-stream';
const FEED_KEY_QUERY_VAR = 'key';
const FEED_TYPE_QUERY_VAR = 'type';
const USER_FEED_KEY = 'stream_user_feed_key';
const GENERATE_KEY_QUERY_VAR = 'stream_new_user_feed_key';
Expand Down Expand Up @@ -37,10 +36,44 @@ public static function load() {
add_action( 'show_user_profile', array( __CLASS__, 'user_feed_key' ) );
add_action( 'edit_user_profile', array( __CLASS__, 'user_feed_key' ) );

// Generate new Stream Feed Key
add_action( 'wp_ajax_wp_stream_feed_key_generate', array( __CLASS__, 'generate_user_feed_key' ) );

add_feed( self::FEED_QUERY_VAR, array( __CLASS__, 'feed_template' ) );
add_feed( self::FEED_NETWORK_QUERY_VAR, array( __CLASS__, 'feed_template' ) );
}

/**
* Sends a new user key when the
*
* @return void/json
*/
public static function generate_user_feed_key() {
check_ajax_referer( 'wp_stream_generate_key', 'nonce' );

$user_id = wp_stream_filter_input( INPUT_POST, 'user', FILTER_SANITIZE_NUMBER_INT );

if ( $user_id ) {
$feed_key = wp_generate_password( 32, false );
update_user_meta( $user_id, self::USER_FEED_KEY, $feed_key );

$link = self::get_user_feed_url( $feed_key );
$xml_feed = add_query_arg( array( 'type' => 'json' ), $link );
$json_feed = add_query_arg( array( 'type' => 'json' ), $link );

wp_send_json_success(
array(
'message' => 'User feed key successfully generated.',
'feed_key' => $feed_key,
'xml_feed' => $xml_feed,
'json_feed' => $json_feed,
)
);
} else {
wp_send_json_error( 'User ID error' );
}
}

/**
* Generates and saves a unique key as user meta if the user does not
* already have a key, or has requested a new one.
Expand All @@ -50,15 +83,20 @@ public static function load() {
* @return void
*/
public static function save_user_feed_key( $user ) {
if ( get_user_meta( $user->ID, self::USER_FEED_KEY, true ) && ! isset( $_GET[self::GENERATE_KEY_QUERY_VAR] ) ) {
$generate_key = isset( $_GET[ self::GENERATE_KEY_QUERY_VAR ] );
$verify_nonce = isset( $_GET['wp_stream_nonce'] ) && wp_verify_nonce( $_GET['wp_stream_nonce'], 'wp_stream_generate_key' );

if ( ! $generate_key && get_user_meta( $user->ID, self::USER_FEED_KEY, true ) ) {
return;
}

if ( ! isset( $_GET['wp_stream_nonce'] ) || ! wp_verify_nonce( $_GET['wp_stream_nonce'], 'wp_stream_generate_key' ) ) {
if ( $generate_key && ! $verify_nonce ) {
return;
}

update_user_meta( $user->ID, self::USER_FEED_KEY, wp_generate_password( 32, false ) );
$feed_key = wp_generate_password( 32, false );

update_user_meta( $user->ID, self::USER_FEED_KEY, $feed_key );
}

/**
Expand All @@ -77,23 +115,54 @@ public static function user_feed_key( $user ) {
return;
}

$key = get_user_meta( $user->ID, self::USER_FEED_KEY, true );
$query_var = is_network_admin() ? self::FEED_NETWORK_QUERY_VAR : self::FEED_QUERY_VAR;
$key = get_user_meta( $user->ID, self::USER_FEED_KEY, true );
$link = self::get_user_feed_url( $key );

$nonce = wp_create_nonce( 'wp_stream_generate_key' );
?>
<table class="form-table">
<tr>
<th><label for="<?php echo esc_attr( self::USER_FEED_KEY ) ?>"><?php esc_html_e( 'Stream Feeds Key', 'stream' ) ?></label></th>
<td>
<p class="wp-stream-feeds-key">
<?php wp_nonce_field( 'wp_stream_generate_key', 'wp_stream_generate_key_nonce' ) ?>
<input type="text" name="<?php echo esc_attr( self::USER_FEED_KEY ) ?>" id="<?php echo esc_attr( self::USER_FEED_KEY ) ?>" class="regular-text code" value="<?php echo esc_attr( $key ) ?>" readonly>
<small><a href="<?php echo esc_url( add_query_arg( array( self::GENERATE_KEY_QUERY_VAR => true, 'wp_stream_nonce' => $nonce ) ) ) ?>" id="<?php echo esc_attr( self::USER_FEED_KEY ) ?>_generate"><?php esc_html_e( 'Generate new key', 'stream' ) ?></a></small>
<span class="spinner" style="display: none;"></span>
</p>
<p class="description"><?php esc_html_e( 'This is your private key used for accessing feeds of Stream Records securely. You can change your key at any time by generating a new one using the link above.', 'stream' ) ?></p>
<p class="wp-stream-feeds-links">
<a href="<?php echo esc_url( add_query_arg( array( 'type' => 'xml' ), $link ) ) ?>" class="rss-feed" target="_blank"><?php echo esc_html_e( 'RSS Feed' ) ?></a>
|
<a href="<?php echo esc_url( add_query_arg( array( 'type' => 'json' ), $link ) ) ?>" class="json-feed" target="_blank"><?php echo esc_html_e( 'JSON Feed' ) ?></a>
</p>
</td>
</tr>
</table>
<?php
}

/**
* Return Stream Feed URL
*
* @return string
*/
public static function get_user_feed_url( $key ) {
$pretty_permalinks = get_option( 'permalink_structure' );
$query_var = is_network_admin() ? self::FEED_NETWORK_QUERY_VAR : self::FEED_QUERY_VAR;

if ( empty( $pretty_permalinks ) ) {
$link = add_query_arg(
array(
'feed' => $query_var,
self::FEED_KEY_QUERY_VAR => $key,
'feed' => $query_var,
$query_var => $key,
),
home_url( '/' )
);
} else {
$link = add_query_arg(
array(
self::FEED_KEY_QUERY_VAR => $key,
$query_var => $key,
),
home_url(
sprintf(
Expand All @@ -104,26 +173,7 @@ public static function user_feed_key( $user ) {
);
}

$nonce = wp_create_nonce( 'wp_stream_generate_key' );
?>
<table class="form-table">
<tr>
<th><label for="<?php echo esc_attr( self::USER_FEED_KEY ) ?>"><?php esc_html_e( 'Stream Feeds Key', 'stream' ) ?></label></th>
<td>
<p>
<input type="text" name="<?php echo esc_attr( self::USER_FEED_KEY ) ?>" id="<?php echo esc_attr( self::USER_FEED_KEY ) ?>" class="regular-text code" value="<?php echo esc_attr( $key ) ?>" readonly>
<small><a href="<?php echo esc_url( add_query_arg( array( self::GENERATE_KEY_QUERY_VAR => true, 'wp_stream_nonce' => $nonce ) ) . sprintf( '#wp-stream-highlight:%s', self::USER_FEED_KEY ) ) ?>"><?php esc_html_e( 'Generate new key', 'stream' ) ?></a></small>
</p>
<p class="description"><?php esc_html_e( 'This is your private key used for accessing feeds of Stream Records securely. You can change your key at any time by generating a new one using the link above.', 'stream' ) ?></p>
<p>
<a href="<?php echo esc_url( $link ) ?>" target="_blank"><?php echo esc_html_e( 'RSS Feed', 'stream' ) ?></a>
|
<a href="<?php echo esc_url( add_query_arg( array( 'type' => 'json' ), $link ) ) ?>" target="_blank"><?php echo esc_html_e( 'JSON Feed', 'stream' ) ?></a>
</p>
</td>
</tr>
</table>
<?php
return $link;
}

/**
Expand All @@ -135,13 +185,13 @@ public static function feed_template() {
$die_title = esc_html__( 'Access Denied', 'stream' );
$die_message = '<h1>' . $die_title .'</h1><p>' . esc_html__( 'You don\'t have permission to view this feed, please contact your site Administrator.', 'stream' ) . '</p>';

if ( ! isset( $_GET[self::FEED_KEY_QUERY_VAR] ) || empty( $_GET[self::FEED_KEY_QUERY_VAR] ) ) {
if ( ! isset( $_GET[self::FEED_QUERY_VAR] ) || empty( $_GET[self::FEED_QUERY_VAR] ) ) {
wp_die( $die_message, $die_title );
}

$args = array(
'meta_key' => self::USER_FEED_KEY,
'meta_value' => $_GET[self::FEED_KEY_QUERY_VAR],
'meta_value' => $_GET[self::FEED_QUERY_VAR],
'number' => 1,
);
$user = get_users( $args );
Expand Down
7 changes: 7 additions & 0 deletions ui/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,10 @@
-webkit-filter: grayscale(100%); /* Chrome 19+ & Safari 6+ */
-moz-filter: grayscale(100%); /* Firefox < 3.5 */
}

/* User Profile */
.wp-stream-feeds-key .spinner {
margin-top: 5px;
position: absolute;
float: none;
}
25 changes: 25 additions & 0 deletions ui/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,31 @@ jQuery(function($){
}
};

$( '.wp-stream-feeds-key #stream_user_feed_key_generate' ).click(function( e ) {
e.preventDefault();

var user = $('#user_id').val(),
nonce = $( '.wp-stream-feeds-key #wp_stream_generate_key_nonce' ).val();

$.ajax({
type: 'POST',
url: ajaxurl,
data: { action: 'wp_stream_feed_key_generate', nonce : nonce, user : user },
dataType: 'json',
beforeSend: function() {
$( '.wp-stream-feeds-key .spinner' ).show().css( { 'display' : 'inline-block' } );
},
success: function( response ) {
$( '.wp-stream-feeds-key .spinner' ).hide();
if ( response.success === true || response.data !== undefined ) {
$( '.wp-stream-feeds-key #stream_user_feed_key' ).val( response.data.feed_key );
$( '.wp-stream-feeds-links a.rss-feed' ).attr( 'href', response.data.xml_feed );
$( '.wp-stream-feeds-links a.json-feed' ).attr( 'href', response.data.json_feed );
}
}
});
});

$(document).ready( function() {
intervals.init( $('.date-interval') );
});
Expand Down

0 comments on commit 7fe9eae

Please sign in to comment.