Skip to content

Commit

Permalink
Merge pull request #1 from norcross/feature/wow-this-is-outdated
Browse files Browse the repository at this point in the history
total rewrite
  • Loading branch information
norcross authored Nov 18, 2024
2 parents aa34d12 + 40b2c62 commit a461abf
Show file tree
Hide file tree
Showing 11 changed files with 516 additions and 266 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#### Version 2.0.0 - 2024/11/18
* total rewrite because the previous version was ugly

#### Version 1.2.0 - 2019/03/19
* adding bbPress support

#### Version 1.1 / Version 1.0 - the before times
* the history is ugly so who cares

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Andrew Norcross
Copyright (c) 2019 Andrew Norcross

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
58 changes: 54 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,56 @@
# Extra Authors Redirect
Adds a checkbox to author profiles for redirecting to other parts of the site.
Extra Authors Redirect
========================

## Contributors
* [Andrew Norcross](https://github.com/norcross)

### To Do
* this readme file, clearly.
## About

Adds a checkbox to redirect author profiles. Useful for sites that have occasional authors or user accounts not intended for display.

## Features

* a checkbox on each author profile
* a global setting for site-wide redirect
* filterable redirect location
* includes bbPress support

#### Does this affect posts / pages / admin / ???

No. this only affects people going to the /author/ template page on a site, and only affects those that have been marked to redirect or if the global settings is enabled.

#### Can I change where the redirect goes?

Yes. There is a filter to change the default behavior. Example:

~~~php
/**
* Change the URL that a redirected author profile goes to.
*
* @param string $location The current URL set.
*
* @return string The new updated one.
*/
function set_custom_redirect_url( $location ) {
return 'https://www.google.com';
}
add_filter( 'exar_redirect_url', 'set_custom_redirect_url' );
~~~

#### Can the redirect be something other than a 301?

Sure can. Change it to whatever you want. Example:

~~~php
/**
* Change the HTTP response code.
*
* @param integer $response_code The current HTTP response.
*
* @return integer The new updated one.
*/
function set_custom_http_response( $response_code ) {
return 302;
}
add_filter( 'exar_redirect_http_code', 'set_custom_http_response' );
~~~
219 changes: 44 additions & 175 deletions extra-authors-redirect.php
Original file line number Diff line number Diff line change
@@ -1,175 +1,44 @@
<?php
/*
Plugin Name: Extra Authors Redirect
Plugin URI: http://andrewnorcross.com/plugins/
Description: Adds a checkbox to author profiles to redirect.
Version: 1.2
Author: Andrew Norcross
Author URI: http://andrewnorcross.com
*/

// Start up the engine
class Extra_Authors_Redirect
{

/**
* Static property to hold our singleton instance
*
* @since 1.0
*/
static $instance = false;

/**
* This is our constructor
*
* @since 1.0
*/
public function __construct() {
add_action ( 'plugins_loaded', array( $this, 'textdomain' ) );
add_action ( 'personal_options', array( $this, 'author_redirect_show' ), 20 );
add_action ( 'personal_options_update', array( $this, 'author_redirect_save' ) );
add_action ( 'edit_user_profile_update', array( $this, 'author_redirect_save' ) );
add_action ( 'template_redirect', array( $this, 'author_redirect_fire' ), 1 );
add_action ( 'template_redirect', array( $this, 'forum_redirect_fire' ), 1 );
}

/**
* If an instance exists, this returns it. If not, it creates one and
* retuns it.
*
* @since 1.0
*/

public static function getInstance() {
if ( !self::$instance )
self::$instance = new self;
return self::$instance;
}

/**
* load textdomain
*
* @since 1.0
*/


public function textdomain() {

load_plugin_textdomain( 'exar', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}

/**
* save new updated item
*
* @since 1.0
*/

public function author_redirect_save( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) )
return false;

if ( isset( $_POST['author_redirect'] ) )
update_usermeta( $user_id, 'author_redirect', $_POST['author_redirect'] );

}

/**
* display checkbox option
*
* @since 1.0
*/

public function author_redirect_show( $profileuser ) {

// get potential metadata
$redirect = get_user_meta( $profileuser->ID, 'author_redirect', true );
?>
<table class="form-table">
<tr>
<th scope="row"><?php _e( 'Author Redirect', 'exar' ); ?></th>
<td>
<label for="author_redirect">
<input type="checkbox" name="author_redirect" id="author_redirect" value="true" <?php if ( !empty($profileuser->author_redirect) ) checked('true', $profileuser->author_redirect); ?> /> <?php _e('Redirect this author template page to the home page', 'exar'); ?>
</label>
</td>
</tr>
</table>
<?php
}

/**
* redirect author template if applicable
*
* @since 1.0
*/

public function author_redirect_fire() {

// bail if we aren't on an author page
if ( !is_author() )
return;

// get author ID of template
$author_id = get_query_var('author');

// bail if we somehow came back with empty
if ( empty( $author_id ) )
return;

$redirect = get_user_meta( $author_id, 'author_redirect', true );

if ( empty ($redirect) )
return;

// get default URL and apply filter
$location = get_bloginfo('url').'/';
$location = apply_filters( 'extra_author_redirect_url', $location );

wp_redirect( esc_url_raw( $location ), 301 );
exit();

}

/**
* redirect forum profile template if applicable
*
* @since 1.0
*/

public function forum_redirect_fire() {

// Bail if no bbPress
if ( !function_exists( 'is_bbpress') )
return;

// bail if we aren't on an author page
if ( !bbp_is_single_user_profile() )
return;

// get author ID of template
$author_id = get_query_var('author');

// bail if we somehow came back with empty
if ( empty( $author_id ) )
return;

$redirect = get_user_meta( $author_id, 'author_redirect', true );

if ( empty ($redirect) )
return;

// get default URL and apply filter
$location = get_bloginfo('url').'/';
$location = apply_filters( 'extra_author_redirect_url', $location );

wp_redirect( esc_url_raw( $location ), 301 );
exit();

}

/// end class
}

// Instantiate our class
$Extra_Authors_Redirect = Extra_Authors_Redirect::getInstance();
<?php
/**
* Plugin Name: Extra Authors Redirect
* Plugin URI: https://github.com/norcross/extra-authors-redirect
* Description: Adds a checkbox to redirect author profiles.
* Author: Andrew Norcross
* Author URI: https://andrewnorcross.com
* Text Domain: extra-authors-redirect
* Domain Path: /languages
* Version: 2.0.0
*
* @package ExtraAuthorsRedirect
*/

// Call our namepsace.
namespace Norcross\ExtraAuthorsRedirect;

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

// Define our version.
define( __NAMESPACE__ . '\VERS', '2.0.0' );

// Plugin root file.
define( __NAMESPACE__ . '\FILE', __FILE__ );

// Define our file base.
define( __NAMESPACE__ . '\BASE', plugin_basename( __FILE__ ) );

// Plugin Folder URL.
define( __NAMESPACE__ . '\URL', plugin_dir_url( __FILE__ ) );

// Set the prefix for our actions and filters.
define( __NAMESPACE__ . '\HOOK_PREFIX', 'exar_' );

// Set the single option and usermeta keys we store.
define( __NAMESPACE__ . '\OPTION_KEY', 'exar_enable_all' );
define( __NAMESPACE__ . '\UMETA_KEY', 'exar_enable_user' );

// Go and load our files.
require_once __DIR__ . '/includes/helpers.php';
require_once __DIR__ . '/includes/process.php';
require_once __DIR__ . '/includes/settings-api.php';
require_once __DIR__ . '/includes/user-settings.php';
Loading

0 comments on commit a461abf

Please sign in to comment.