Skip to content

Commit

Permalink
Adds support for BP group URLs
Browse files Browse the repository at this point in the history
git-svn-id: http://plugins.svn.wordpress.org/yourls-wordpress-to-twitter/trunk@397673 b8457f37-d9ea-0310-8a92-e5e31aec5664
  • Loading branch information
boonebgorges committed Jun 15, 2011
1 parent cca45bc commit 76d0774
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 67 deletions.
62 changes: 39 additions & 23 deletions inc/buddypress/bp-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,71 +12,87 @@ function wp_ozh_yourls_bp_admin_markup() {
$can_customize = wp_ozh_yourls_service_allows_custom_urls();

// Set some of the checkmark indexes to avoid WP_DEBUG errors
$indexes = array( 'bp_members', 'bp_members_pretty', 'bp_members_can_edit', 'bp_groups', 'bp_topics' );
$indexes = array( 'bp_members', 'bp_members_pretty', 'bp_members_can_edit',
'bp_groups', 'bp_groups_pretty', 'bp_groups_can_edit',
'bp_topics' );

foreach( $indexes as $index ) {
if ( !isset( $ozh_yourls[$index] ) )
$ozh_yourls[$index] = '';
}

if ( empty( $ozh_yourls['bp_shortener_base'] ) ) {
$is_a_guess = true;
$ozh_yourls['bp_shortener_base'] = wp_ozh_yourls_guess_base_url();
} else {
$is_a_guess = false;
}


?>

<h3>BuddyPress settings <span class="h3_toggle expand" id="h3_buddypress">+</span> <span id="h3_check_buddypress" class="h3_check">*</span></h3>

<div class="div_h3" id="div_h3_buddypress">

<h4><?php _e( 'Members', 'wp-ozh-yourls' ) ?></h4>

<table class="form-table">

<tr valign="top">
<th scope="row"><?php _e( 'URL shortener base', 'wp-ozh-yourls' ) ?></th>
<th scope="row"><?php _e( 'Each member gets a short URL', 'wp-ozh-yourls' ) ?></th>
<td>
http://<input name="ozh_yourls[bp_shortener_base]" type="text" value="<?php echo $ozh_yourls['bp_shortener_base'] ?>" />
<p class="description"><?php _e( 'This value is displayed wherever users can edit their short URLs.', 'wp-ozh-yourls' ) ?></p>
<?php if ( $is_a_guess ) : ?>
<p class="description"><?php _e( 'We\'ve guessed at the URL, based on your URL Shortener Settings above.', 'wp-ozh-yourls' ) ?></p>
<?php endif ?>
<input name="ozh_yourls[bp_members]" type="checkbox" <?php checked( $ozh_yourls['bp_members'], 'on' ) ?> />
<span class="description"><?php _e( 'Each short URL will be created automatically the next time the member profile is viewed', 'wp-ozh-yourls' ) ?></span>
</td>
</tr>

<?php if ( $can_customize ) : ?>
<tr valign="top">
<th scope="row"><?php _e( 'Create short URLs from usernames', 'wp-ozh-yourls' ) ?></th>
<td>
<input name="ozh_yourls[bp_members_pretty]" type="checkbox" <?php checked( $ozh_yourls['bp_members_pretty'], 'on' ) ?> />
<span class="description"><?php printf( __( 'When checked, member short URLs will look like <code>%s<strong>username</strong></code>, rather than a random string', 'wp-ozh-yourls' ), wp_ozh_yourls_get_shortener_base_url() ) ?></span>
</td>
</tr>

<tr valign="top">
<th scope="row"><?php _e( 'Users can edit their short URLs', 'wp-ozh-yourls' ) ?></th>
<td>
<input name="ozh_yourls[bp_members_can_edit]" type="checkbox" <?php checked( $ozh_yourls['bp_members_can_edit'], 'on' ) ?> />
<span class="description"><?php _e( 'You must set <code>define( \'YOURLS_UNIQUE_URLS\', false );</code> in <a href="http://yourls.org/#Config">your YOURLS configuration file.', 'wp-ozh-yourls' ) ?></span>
</td>
</tr>
<?php endif ?>

</table>

<h4><?php _e( 'Members', 'wp-ozh-yourls' ) ?></h4>

<h4><?php _e( 'Groups', 'wp-ozh-yourls' ) ?></h4>

<table class="form-table">

<tr valign="top">
<th scope="row"><?php _e( 'Each member gets a short URL', 'wp-ozh-yourls' ) ?></th>
<th scope="row"><?php _e( 'Each group gets a short URL', 'wp-ozh-yourls' ) ?></th>
<td>
<input name="ozh_yourls[bp_members]" type="checkbox" <?php checked( $ozh_yourls['bp_members'], 'on' ) ?> />
<input name="ozh_yourls[bp_groups]" type="checkbox" <?php checked( $ozh_yourls['bp_groups'], 'on' ) ?> />
<span class="description"><?php _e( 'Each short URL will be created automatically the next time the group page is viewed', 'wp-ozh-yourls' ) ?></span>
</td>
</tr>

<?php if ( $can_customize ) : ?>
<tr valign="top">
<th scope="row"><?php _e( 'Create short URLs from usernames', 'wp-ozh-yourls' ) ?></th>
<th scope="row"><?php _e( 'Create short URLs from group slugs', 'wp-ozh-yourls' ) ?></th>
<td>
<input name="ozh_yourls[bp_members_pretty]" type="checkbox" <?php checked( $ozh_yourls['bp_members_pretty'], 'on' ) ?> />
<span class="description"><?php _e( 'When checked, member short URLs will look like <code>http://bit.ly/<strong>username</strong></code>, rather than a random string', 'wp-ozh-yourls' ) ?></span>
<input name="ozh_yourls[bp_groups_pretty]" type="checkbox" <?php checked( $ozh_yourls['bp_groups_pretty'], 'on' ) ?> />
<span class="description"><?php printf( __( 'When checked, group short URLs will look like <code>%s<strong>group-name</strong></code>, rather than a random string', 'wp-ozh-yourls' ), wp_ozh_yourls_get_shortener_base_url() ) ?></span>
</td>
</tr>

<tr valign="top">
<th scope="row"><?php _e( 'Users can edit their short URLs', 'wp-ozh-yourls' ) ?></th>
<td>
<input name="ozh_yourls[bp_members_can_edit]" type="checkbox" <?php checked( $ozh_yourls['bp_members_can_edit'], 'on' ) ?> />
<span class="description"><?php _e( 'If you\'re using YOURLS, you must set <code>define( \'YOURLS_UNIQUE_URLS\', false );</code> in <a href="http://yourls.org/#Config">your configuration file.', 'wp-ozh-yourls' ) ?></span>
<input name="ozh_yourls[bp_groups_can_edit]" type="checkbox" <?php checked( $ozh_yourls['bp_groups_can_edit'], 'on' ) ?> />
<span class="description"><?php _e( 'You must set <code>define( \'YOURLS_UNIQUE_URLS\', false );</code> in <a href="http://yourls.org/#Config">your YOURLS configuration file.', 'wp-ozh-yourls' ) ?></span>
</td>
</tr>
<?php endif ?>

</table>


</div> <!-- div_h3_buddypress -->

<?php
Expand Down
254 changes: 254 additions & 0 deletions inc/buddypress/bp-groups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
<?php

/**
* YOURLS BP Groups functions
*/

/**
* Create a shorturl for a BP group
*
* @package YOURLS WordPress to Twitter
* @since 1.5
*
* @param int $group_id The ID of the group whose page should be the URL for the shorturl
* @param str $type 'pretty' if you want the shorturl slug to be created from the group slug,
* or from the $keyword param. Otherwise 'normal' will create a randomly generated URL, as per
* the service's API. Note that $type and $keyword only do anything with YOURLS (not bit.ly, etc)
* @param str $keyword The desired 'keyword' or slug of the shorturl. Note that this param is
* ignored if $type is not set to 'pretty'. Defaults to the group slug if $type == 'pretty'
*/
function wp_ozh_yourls_create_bp_group_url( $group_id, $type = 'normal', $keyword = false ) {
global $bp;

// Check plugin is configured
$service = wp_ozh_yourls_service();
if( !$service )
return 'Plugin not configured: cannot find which URL shortening service to use';

// Mark this post as "I'm currently fetching the page to get its title"
if( $group_id && !groups_get_groupmeta( $group_id, 'yourls_shorturl' ) ) {
groups_update_groupmeta( $group_id, 'yourls_fetching', 1 );
groups_update_groupmeta( $group_id, 'yourls_shorturl', '&nbsp;' ); // temporary empty title to avoid loop on creating short URL
}

// Avoid a DB query if we can
if ( isset( $bp->groups->current_group->id ) && $bp->groups->current_group->id == $group_id ) {
$group = $bp->groups->current_group;
} else {
$group = new BP_Groups_Group( $group_id );
}

if ( empty( $group ) )
return false;

$url = bp_get_group_permalink( $group );
$title = $group->name;

// Only send a keyword if this is a pretty URL
if ( 'pretty' == $type ) {
if ( !$keyword )
$keyword = $group->slug;
} else {
$keyword = false;
}

// Get short URL
$shorturl = wp_ozh_yourls_api_call( $service, $url, $keyword, $title );

// Remove fetching flag
if( $group_id )
groups_delete_groupmeta( $group_id, 'yourls_fetching' );

// Store short URL in a custom field
if ( $group_id && $shorturl ) {
groups_update_groupmeta( $group_id, 'yourls_shorturl', $shorturl );

if ( $keyword )
groups_update_groupmeta( $group_id, 'yourls_shorturl_name', $keyword );
}

return $shorturl;
}

/**
* Outputs the current group's shorturl in the header
*
* Don't like the way this looks? Put the following in your theme's functions.php:
*
* remove_action( 'bp_before_group_header_meta', 'wp_ozh_yourls_display_group_url' );
*
* and then use the template tags wp_ozh_yourls_get_displayed_user_url() and
* wp_ozh_yourls_edit_link() to create your own markup in your theme.
*
* @package YOURLS WordPress to Twitter
* @since 1.5
*/
function wp_ozh_yourls_display_group_url() {
$shorturl = wp_ozh_yourls_get_current_group_url();

if ( $shorturl ) {
?>
<span class="highlight shorturl">
<?php printf( __( 'Short URL: <code>%s</code>', 'wp-ozh-yourls' ), $shorturl ) ?> <?php if ( wp_ozh_user_can_edit_url() ) : ?>&nbsp;<?php wp_ozh_yourls_group_edit_link() ?><?php endif ?>
</span>
<?php
}
}
add_action( 'bp_before_group_header_meta', 'wp_ozh_yourls_display_group_url' );

/**
* Echo the content of wp_ozh_yourls_get_current_group_url()
*
* @package YOURLS WordPress to Twitter
* @since 1.5
*/
function wp_ozh_yourls_current_group_url() {
echo wp_ozh_yourls_get_current_group_url();
}
/**
* Return the current group's shorturl
*
* @package YOURLS WordPress to Twitter
* @since 1.5
*
* @return str $url The shorturt
*/
function wp_ozh_yourls_get_current_group_url() {
global $bp;

$url = isset( $bp->groups->current_group->shorturl ) ? $bp->groups->current_group->shorturl : '';

return $url;
}

/**
* Echo the content of wp_ozh_yourls_get_group_edit_link()
*
* @package YOURLS WordPress to Twitter
* @since 1.5
*
* @param int $user_id The id of the user. Defaults to the displayed user, then to the loggedin user
* @param str $return 'html' to return a full link, otherwise just retrieve the URL
*/
function wp_ozh_yourls_group_edit_link( $group_id = false, $return = 'html' ) {
echo wp_ozh_yourls_get_group_edit_link( $group_id, $return );
}
/**
* Return the URL to a group's Admin screen, where he can edit his shorturl
*
* @package YOURLS WordPress to Twitter
* @since 1.5
*
* @param int $group_id The id of the group. Defaults to the current group id
* @param str $return 'html' to return a full link, otherwise just retrieve the URL
* @return str $link The link
*/
function wp_ozh_yourls_get_group_edit_link( $group_id = false, $return = 'html' ) {
global $bp;

// If no group_id is passed, first try to default to the current group
if ( !$group_id ) {
$group_id = !empty( $bp->groups->current_group->id ) ? $bp->groups->current_group->id : false;
$group = !empty( $bp->groups->current_group ) ? $bp->groups->current_group : false;
}

// If there's no group_id, bail
if ( !$group_id ) {
return false;
}

// If a group has not been set yet, pull it up
if ( !$group ) {
$group = new BP_Groups_Group( $group_id );
}

if ( empty( $group ) ) {
return false;
}

// Create the URL to the admin page
$link = bp_get_group_permalink( $group ) . 'admin/group-settings/';

// Add the markup if necessary
if ( 'html' == $return ) {
$link = sprintf( '<a href="%1$s">%2$s</a>', $link, __( 'Edit', 'wp-ozh-yourls' ) );
}

return $link;
}

/**
* GROUP SHORTURL EDITING
*/

/**
* Renders the Edit field on the Group Settings page
*
* @package YOURLS WordPress to Twitter
* @since 1.5
*/
function wp_ozh_yourls_render_group_edit_field() {
if ( !wp_ozh_user_can_edit_url() )
return;

$shorturl_name = groups_get_groupmeta( bp_get_group_id(), 'yourls_shorturl_name' );

?>

<label for="shorturl"><?php _e( 'Short URL: ', 'wp-ozh-yourls' ) ?></label>
<code><?php wp_ozh_yourls_shortener_base_url() ?></code><input type="text" name="shorturl" id="shorturl" value="<?php echo $shorturl_name ?>" class="settings-input" />
<p class="description"><?php _e( 'Please note that YOURLS only supports a limited character set for short URLs. See <a href="http://yourls.org/#FAQ">the YOURLS FAQ</a> for more information on 32 vs 64 bit encoding.', 'wp-ozh-yourls' ) ?></p>

<hr />
<?php
}
add_action( 'bp_before_group_settings_admin', 'wp_ozh_yourls_render_group_edit_field' );

/**
* Processes shorturl edits by the member and displays proper success/error messages
*
* @package YOURLS WordPress to Twitter
* @since 1.5
*/
function wp_ozh_yourls_save_group_edit( $group_id ) {
global $bp;

if ( isset( $_POST['shorturl'] ) ) {
$shorturl_name = untrailingslashit( trim( $_POST['shorturl'] ) );

// Remove the limitation on duplicate shorturls
// This is a temporary workaround
define( 'YOURLS_UNIQUE_URLS', false );
add_filter( 'yourls_remote_params', 'wp_ozh_yourls_remote_allow_dupes' );

// First, try to create a URL with this name
$shorturl = wp_ozh_yourls_create_bp_group_url( $group_id, 'pretty', $shorturl_name );

remove_filter( 'yourls_remote_params', 'wp_ozh_yourls_remote_allow_dupes' );

if ( !$shorturl ) {
// Something has gone wrong. Check to see whether this is a reversion to a
// previous shorturl
$expand = wp_ozh_yourls_api_call_expand( 'yourls-remote', $shorturl_name );

if ( empty( $expand->longurl ) || $expand->longurl != bp_get_group_permalink( $bp->groups->current_group ) ) {
// No match.
bp_core_add_message( __( 'That URL is unavailable. Please choose another.', 'wp-ozh-yourls' ), 'error' );
} else {
$shorturl = $expand->shorturl;
}
}

if ( $shorturl ) {
groups_update_groupmeta( $group_id, 'yourls_shorturl', $shorturl );
groups_update_groupmeta( $group_id, 'yourls_shorturl_name', $shorturl_name );

// Just in case this needs to be refreshed
$bp->groups->current_group->shorturl = $shorturl;
}
}
}
add_action( 'groups_group_settings_edited', 'wp_ozh_yourls_save_group_edit' );


?>
Loading

0 comments on commit 76d0774

Please sign in to comment.