Social plugin, please install this plugin.', AKTT::$text_domain), 'http://wordpress.org/extend/plugins/social/'));
}
// Set logging to what's in the plugin settings
AKTT::$debug = AKTT::get_option('debug');
}
/**
* Sets the default settings for the plugin
*
* @return void
*/
static function set_default_settings() {
// Set default settings
AKTT::$default_settings = array(
'post_type_admin_ui' => array(
'value' => false,
'label' => __('Enable admin UI for post type?', AKTT::$text_domain),
'type' => 'int',
),
'post_type_visibility' => array(
'value' => false,
'label' => __('Should Twitter Tools post type be public?', AKTT::$text_domain),
'type' => 'int',
),
'taxonomy_admin_ui' => array(
'value' => false,
'label' => __('Enable admin UI for custom taxonomies?', AKTT::$text_domain),
'type' => 'int',
),
'taxonomy_visibility' => array(
'value' => false,
'label' => __('Should Twitter Tools custom taxonomies be public?', AKTT::$text_domain),
'type' => 'int',
),
'debug' => array(
'value' => 0,
'label' => __('Enable debug mode?', AKTT::$text_domain),
'type' => 'int',
),
);
}
/**
* Append a message of a certain type to the admin notices.
*
* @param string $type
* @param string $msg
* @return void
*/
static function add_admin_notice($type, $msg) {
AKTT::$admin_notices[] = array(
'type' => $type == 'error' ? $type : 'updated', // If it's not an error, set it to updated
'msg' => $msg
);
}
/**
* Displays admin notices
*
* @return void
*/
static function admin_notices() {
if (is_array(AKTT::$admin_notices)) {
foreach (AKTT::$admin_notices as $notice) {
extract($notice);
?>
array(
'name' => __('Tweets', AKTT::$text_domain),
'singular_name' => __('Tweet', AKTT::$text_domain)
),
'supports' => array(
'editor',
),
'public' => (bool) AKTT::get_option('post_type_visibility'),
'show_ui' => (bool) AKTT::get_option('post_type_admin_ui'),
'rewrite' => array(
'slug' => 'tweets',
'with_front' => false
),
));
add_permastruct(AKTT::$post_type, '/tweets/%post_id%', false, EP_PERMALINK);
}
/**
* Register our taxonomies.
*
* @return void
*/
static function register_taxonomies() {
$defaults = array(
'public' => (bool) AKTT::get_option('taxonomy_visibility'),
'show_ui' => (bool) AKTT::get_option('taxonomy_admin_ui'),
);
$taxonomies = array(
'aktt_mentions' => array_merge($defaults, array(
'labels' => array(
'name' => __('Mentions', AKTT::$text_domain),
'singular_name' => __('Mention', AKTT::$text_domain)
),
)),
'aktt_hashtags' => array_merge($defaults, array(
'labels' => array(
'name' => __('Hashtags', AKTT::$text_domain),
'singular_name' => __('Hashtag', AKTT::$text_domain)
),
)),
'aktt_twitter_user' => array_merge($defaults, array(
'labels' => array(
'name' => __('Twitter Usernames', AKTT::$text_domain),
'singular_name' => __('Twitter Username', AKTT::$text_domain)
),
)),
);
foreach ($taxonomies as $tax => $args) {
register_taxonomy($tax, AKTT::$post_type, $args);
}
}
/**
* Get default setting's value
*
* @param string $setting
* @return mixed
*/
static function get_default_setting($setting) {
return isset(AKTT::$default_settings[$setting]) ? AKTT::$default_settings[$setting]['value'] : null;
}
/**
* Get an option from the DB, and fall back to the default setting
*
* @param string $setting
* @return mixed
*/
static function get_option($setting) {
// Get a default
$val = AKTT::get_default_setting($setting);
// Now look inside the option to see if it's set there
$option = get_option(AKTT::$plugin_settings_option_name);
if (!empty($option) && is_array($option) && isset($option[$setting])) {
$val = $option[$setting];
}
return apply_filters(AKTT::$prefix.'get_option', $val, $setting);
}
// /** @DEPRECATED for using one large option instead.
// * Retrieves the actual WordPress option name for a passed setting.
// * For the time being, it just appends the "aktt_" prefix.
// *
// * @param string $setting
// * @return string
// */
// static function get_option_name_for_setting($setting) {
// return AKTT::$prefix.$setting;
// }
/**
* Updates a setting, if === null is passed as the value, it
* picks up the default setting
*
* @param string $setting
* @param mixed $value
* @return void
*/
static function save_setting($setting, $value = null) {
if (current_user_can(AKTT::$capability)) {
// If it's null, get the default value
$val = is_null($value) ? AKTT::get_default_setting($setting) : $value;
return update_option(AKTT::$prefix.$setting, $val);
}
}
/**
* Change the permalink for the tweets
*
* @param string $post_link
* @param object $post
* @return string
*/
static function get_tweet_permalink($post_link, $post) {
if ($post->post_type == AKTT::$post_type) {
$rewritecode = array(
'%post_id%',
);
$rewritereplace = array(
$post->ID,
);
$post_link = str_replace($rewritecode, $rewritereplace, $post_link);
}
return $post_link;
}
/**
* Prepends a "settings" link for our plugin on the plugins.php page
*
* @param array $links
* @param string $file -- filename of plugin
* @return array
*/
function plugin_action_links($links, $file) {
if (basename($file) == basename(__FILE__)) {
$settings_link = ''.__('Settings', 'twitter-tools').'';
array_unshift($links, $settings_link);
}
return $links;
}
/**
* Adds a link to the "Settings" menu in WP-Admin.
*/
public function admin_menu() {
add_options_page(
__('Twitter Tools Options', AKTT::$text_domain),
__('Twitter Tools', AKTT::$text_domain),
AKTT::$capability,
AKTT::$menu_page_slug,
array(AKTT, 'output_settings_page')
);
}
/**
* Initializes the plugin settings in WP admin, using the Settings API
*
* @return void
*/
static function init_settings() {
// Register our parent setting (it contains an array of all our plugin-wide settings)
register_setting(
AKTT::$menu_page_slug, // Page it belongs to
AKTT::$plugin_settings_option_name, // option name
array(AKTT, 'sanitize_plugin_settings') // Sanitize callback
);
// Register our parent setting (it contains an array of all our plugin-wide settings)
register_setting(
AKTT::$menu_page_slug, // Page it belongs to
AKTT_Account::$settings_option_name, // option name
array(AKTT, 'sanitize_account_settings') // Sanitize callback
);
// Add a section of settings to Twitter Tools' settings page
add_settings_section(
AKTT::$plugin_settings_section_slug, // group id
__('General Plugin Settings', AKTT::$text_domain), // title
array(AKTT, 'output_settings_section_text'), // callback for text
AKTT::$menu_page_slug // Page Handle
);
// Add a section for Account setting
add_settings_section(
AKTT::$account_settings_section_slug, // group id
__('Accounts', AKTT::$text_domain), // title
array(AKTT, 'output_account_settings_section'), // callback for text
AKTT::$menu_page_slug // Page Handle
);
// Register our settings' fields with WP
foreach (AKTT::$default_settings as $setting => $details) {
// Add the settings to the proper group
add_settings_field(
$setting, // unique ID for the field...not necessarily the option_name
$details['label'],
array(AKTT, 'output_settings_field'), // Callback to output the actual HTML field
AKTT::$menu_page_slug, // Page Handle
AKTT::$plugin_settings_section_slug, // Settings Group
array(
'setting' => $setting,
)
);
}
}
/**
* Sanitization of values
*
* @param mixed $value
* @return int
*/
static function sanitize_plugin_settings($value) {
foreach ($value as $k => $v) {
$value[$k] = AKTT::sanitize_plugin_setting($k, $v);
}
return $value;
}
/**
* Sanitizes the ACCOUNT settings from the Twitter Tools' admin page.
*
* ** Option Storage Format **
*
* $option_value = array(
* $this->id => array(
* 'settings' => array(
* 'post_author' => 0,
* 'post_cats' => array(),
* 'post_tags' => array(),
* 'hashtag' => '',
* ),
* ),
* );
*
* @param array $value
* @return array
*/
static function sanitize_account_settings($value) {
foreach ($value as $id => &$acct) {
// If we don't have a settings array, get rid of it
if (!isset($acct['settings'])) {
unset($value[$id]);
continue;
}
// Loop over each setting and sanitize
foreach (array_keys(AKTT_Account::$config) as $setting) {
$acct['settings'][$setting] = AKTT::sanitize_account_setting($setting, $acct['settings'][$setting]);
}
}
return $value;
}
static function sanitize_plugin_setting($setting, $value) {
return AKTT::sanitize_setting($setting, $value, AKTT::$default_settings[$setting]['type']);
}
static function sanitize_account_setting($setting, $value) {
return AKTT::sanitize_setting($setting, $value, AKTT_Account::$config[$setting]['type']);
}
/**
* Sanitizes a setting, based on a big switch statement
* that has each setting, and how to clean it.
*
* @param string $setting
* @param mixed $value
* @param string $type - type of setting (int, etc.)
* @return mixed - Clean value **If it matched a switch case**
*/
static function sanitize_setting($setting, $value, $type) {
switch ($type) {
case 'int':
$value = is_array($value) ? array_map('intval', $value) : intval($value);
break;
case 'no_html':
$value = is_array($value) ? array_map('wp_filter_nohtml_kses', $value) : wp_filter_nohtml_kses($value);
break;
case 'is_tag':
$term = get_term_by('name', $value, 'post_tag');
$value = (!$term) ? '' : $term->term_id;
break;
case 'is_cat':
$term = get_term_by('id', $value, 'category');
$value = (!$term) ? 0 : $term->term_id;
break;
}
return $value;
}
/**
* Outputs the plugin's settings form. Utilizes the "settings" API in WP
*
* @return void
*/
static function output_settings_page() {
?>
/>
Social plugin’s settings, to set up an account!', AKTT::$text_domain), admin_url('options-general.php?page=social.php')); ?>
output_account_config();
}
?>
'manual_tweet_download'), admin_url());
return wp_nonce_url($url, 'manual_tweet_download');
}
/**
* Loads the social twitter accounts into a static variable
*
* @return void
*/
static function get_social_accounts() {
$services = Social::$combined_services;
$social_twitter = empty($services['twitter']) ? null : $services['twitter'];
// If we don't have a Social_Twitter object, get out
if (is_null($social_twitter)) { return; }
// If we don't have any Social Twitter accounts, get out
$social_accounts = $social_twitter->accounts();
if (empty($social_accounts)) { return; }
/* Loop over our social twitter accounts and create AKTT_Account objects
that will store the various configuration options for the twitter accounts. */
foreach ($social_accounts as $obj_id => $acct_obj) {
// If this account has already been assigned, continue on
if (isset(AKTT::$accounts[$obj_id])) { continue; }
/* Call a static method to load the object, so we
can ensure it was instantiated properly */
$o = AKTT_Account::load(&$acct_obj);
// Assign the object, only if we were successfully created
if (is_a($o, 'AKTT_Account')) {
AKTT::$accounts[$obj_id] = $o;
}
}
}
/**
* Iterates over all the twitter accounts in social and downloads and imports the tweets.
*
* @return void
*/
function import_tweets() {
// load our accounts
AKTT::get_social_accounts();
// See if we have any accounts to loop over
if (!is_array(AKTT::$accounts) || empty(AKTT::$accounts)) {
return;
}
// iterate over each account and download the tweets
foreach (AKTT::$accounts as $id => $acct) {
// Download the tweets for that acct
$tweets = $acct->download_tweets();
// If we have tweets, save 'em
if ($tweets) {
$acct->save_tweets($tweets);
}
}
}
/**
* Load JS resources necessary for admin...only on the twitter tools' settings page
*
* @param string $hook_suffix
* @return void
*/
function admin_enqueue_scripts($hook_suffix) {
if ($hook_suffix == 'settings_page_twitter-tools') {
wp_enqueue_script('suggest');
wp_enqueue_script('aktt_admin_js', admin_url('?aktt_action=admin_js'), array('jquery', 'suggest'), AKTT::$ver);
}
}
/**
* Request handler for admin
*
* @return void
*/
function admin_request_handler(){
if (isset($_GET['aktt_action'])) {
switch ($_GET['aktt_action']) {
case 'admin_js':
AKTT::admin_js();
exit;
break;
case 'manual_tweet_download':
// Permission checking
if (!check_admin_referer('manual_tweet_download') || !current_user_can(AKTT::$capability)) {
wp_die('You should’t be here.');
}
AKTT::import_tweets();
wp_redirect(add_query_arg(array(
'page' => AKTT::$menu_page_slug,
'tweets_updated' => ''),
admin_url('options-general.php')
));
break;
}
}
}
/**
* Output the admin-side JavaScript
*
* @return void
*/
function admin_js() {
if (!headers_sent()) {
header('Content-type: text/javascript');
}
?>
jQuery(document).ready(function($){
$('.type-ahead').each(function(){
var tax = $(this).data('tax');
$(this).suggest(
ajaxurl + '?action=ajax-tag-search&tax=' + tax,
{
delay: 500,
minchars: 2,
multiple: false
}
);
});
});
$limit) {
$string = substr($string, 0, $limit)."...";
}
return $string;
}
}
/**
* You must flush the rewrite rules to activate this action.
*/
// function aktt_add_tweet_rewrites() {
// global $wp_rewrite;
//
// $rules = $wp_rewrite->generate_rewrite_rules('/tweets/%post_id%', EP_PERMALINK);
//
// foreach ($rules as &$rule) {
// $rule = str_replace('index.php?', 'index.php?post_type=aktt_tweet&', $rule);
// }
//
// // All, paginated
// $rules['tweets/page/([0-9]+)/?$'] = 'index.php?post_type=aktt_tweet&paged=$matches[1]';
// // all
// $rules['tweets/?$'] = 'index.php?post_type=aktt_tweet';
//
// $wp_rewrite->rules = $rules + $wp_rewrite->rules;
// }
// add_action('generate_rewrite_rules', 'aktt_add_tweet_rewrites');