public
Last active

Creates and maintains the SysBot User (which has the role of "editor"). Purpose of this bot user: You're importing data into a WordPress CustomPostType (for e.g.: via a HTTP request). During import/fetch you need a user who is the author of those CPTs posts. You could go with a fake ID, but this will successfully prevent you to use taxonomies, as the `tax_input` argument doesn't allow to add taxonomies if the post hasn't got an author with the required capabilites. Advantages of using a SysBot: You can use author archives to determine if a post was already touched/edited by a real user or not. You can query posts by this author. How to use the SysBot Class: Open the file in your preferred IDE and search for `@TODO`. Those are the places you can edit. The bot user updates himself if you change those values. If you want to insert the bot as author, simply use `get_bot()->ID` to retrieve the `ID` of the bot. The public API function returns the whole user object.

  • Download Gist
sys_bot.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
<?php
defined( 'ABSPATH' ) OR exit;
/**
* Plugin Name: SysBot
* Description: Creates and maintains the SysBot User (which has the role of "editor")
* Author: Franz Josef Kaiser
*/
 
# PUBLIC API #
function get_bot()
{
$sys_bot = new sys_bot();
$sys_bot = $sys_bot::init();
$bot = get_user_by( 'email', $sys_bot->get_mail() );
return $bot;
}
 
add_action( 'plugins_loaded', array( 'sys_bot', 'init' ) );
/**
* Registers a bot user to map posts to
* @author Franz Josef Kaiser <wecodemore@gmail.com>
* @copyright 2013 Franz Josef Kaiser
* @license MIT
* @version 2013-01-25.1927
*/
class sys_bot
{
/**
* @access protected
* @static
* @var object Instance of the Class
*/
protected static $instance;
 
/**
* @static
* @var string Full Mailadress
*/
public static $mail = '';
 
/**
* @var boolean
*/
public $multibot = false;
 
/**
* @TODO Change this value to your bots mail slug
* @var string Email Address
*/
public $name = 'sysbot';
 
/**
* @TODO Change this value to your bots display/nick name
* @var string Nickname
*/
public $nick = 'SysBot';
 
/**
* @var object Debug Container
*/
public $debug;
 
/**
* @static
* @return object|sys_bot
*/
public static function init()
{
null === self::$instance AND self::$instance = new self;
return self::$instance;
}
 
/**
* Constructor
*/
public function __construct()
{
add_action( 'admin_init', array( $this, 'register_bot' ) );
 
if (
current_user_can( 'manage_options' )
AND ( defined( 'WP_DEBUG' ) AND WP_DEBUG )
)
add_action( 'shutdown', array( $this, 'debug' ) );
}
 
/**
* @return object WP_User or WP_Error on fail
*/
public function register_bot()
{
// Check if bot exists
$has_user = username_exists( $this->get_mail() );
if ( null !== $has_user )
{
$user = get_user_by( 'email', $this->get_mail() );
if (
$user->data->user_nicename === $this->nick
AND $user->data->user_nicename === $this->nick
)
return $user;
 
// Update Bot
return wp_update_user(
array(
'ID' => $user->ID,
'user_nickname' => $this->nick,
'user_nicename' => $this->nick
)
);
}
 
// Generate the password...
$password = wp_generate_password( 30, false );
// ...and create the user
$user_id = wp_create_user(
$this->get_mail(),
$password,
$this->get_mail()
);
// Set the bots nickname
wp_update_user(
array(
'ID' => $user_id,
'user_nickname' => $this->nick,
'user_nicename' => $this->nick
)
);
// Setup the bot
$user = new WP_User( $user_id );
// Set the bots role
$user->set_role( 'editor' );
 
return $this->debug = $user;
}
 
/**
* @return string
*/
public function set_mail()
{
return self::$mail = sprintf(
'%s@%s',
$this->get_name(),
$this->get_domain()
);
}
 
/**
* @return string
*/
public function get_mail()
{
empty( self::$mail ) AND self::$mail = $this->set_mail();
return self::$mail;
}
 
/**
* @return string $domain
*/
public function get_domain()
{
if ( ! is_multisite() )
return get_option( 'siteurl' );
 
// Get main domain
$domain = get_current_site()->domain;
is_subdomain_install() AND $domain = wpmu_current_site()->domain;
 
// Get subdomain
if ( $this->multi_bot )
{
$name_ext = "_".ltrim( PATH_CURRENT_SITE, '/' );
if ( is_subdomain_install() )
{
$scheme = 'http';
$scheme .= is_ssl() ? 's' : '';
$domain = str_replace( "{$scheme}://", '', get_option( 'home' ) );
}
}
 
return $domain;
}
 
/**
* @return string
*/
public function get_name()
{
if ( ! is_multisite() )
return sprintf(
'%s%s',
$this->name,
get_option( 'site_url' )
);
 
return sprintf(
'%s%s',
$this->name,
! is_subdomain_install()
? "_".ltrim( PATH_CURRENT_SITE, '/' )
: ''
);
}
 
/**
* @return string
*/
public function debug()
{
if ( is_wp_error( $this->debug ) )
return printf(
'<p>%s: %s</p>',
$this->debug->get_error_code(),
$this->debug->get_error_message()
);
 
return ! empty( $this->debug )
? sprintf(
'<pre>%s</pre>',
$this->debug
)
: ''
;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.