settings = get_option( 'atui_settings' );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_plugin_action_links' ) );
add_filter( 'plugin_row_meta', array( $this, 'add_plugin_row_meta' ), 10, 2 );
register_activation_hook( __FILE__, array( $this, 'install_plugin' ) );
if ( $this->settings['number_to_show'] == 'all' ) {
add_filter( 'get_terms_args', array( $this, 'show_all_tags' ) );
}
$current_page = basename( $_SERVER['PHP_SELF'] );
// only run on add/edit post pages.
if ( ( $current_page == 'post.php' || $current_page == 'post-new.php' ) && ( ! isset( $_GET['post_type'] ) || $_GET['post_type'] != 'page' ) )
{
add_action( 'admin_head', array( $this, 'add_style_to_head' ) );
add_action( 'admin_footer-post.php', array( $this, 'add_script_to_footer' ) );
add_action( 'admin_footer-post-new.php', array( $this, 'add_script_to_footer' ) );
}
// only run on settings page
if ( ( $current_page == 'plugins.php' && isset( $_GET['page'] ) && $_GET['page'] == 'atui-page' ) || $current_page == 'options.php' )
{
require_once ( plugin_dir_path( __FILE__ ) . 'admin-tag-ui-settings.php' );
$settings_page = new Admin_Tag_UI_Settings_Page( $this );
}
}
/**
* Removes the limit on the number of tags to show
* - filter get_terms_args - __construct()
* @param $args
* @return mixed
*/
public function show_all_tags( $args )
{
if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['action'] ) && $_POST['action'] === 'get-tagcloud' )
{
unset ( $args['number'] );
$args['hide_empty'] = 0;
}
return $args;
}
/**
* Adds a settings link to the plugin's actions under plugins.php
* - filter plugin_action_links_ . plugin_basename( __FILE__ ) - __construct()
* @param $links
* @return array
*/
public function add_plugin_action_links( $links )
{
$add_links = array();
$add_links[] = 'Settings';
return array_merge( $add_links, $links );
}
/**
* Adds a view more link to the plugin's meta under plugins.php
* - filter plugin_row_meta - __construct()
* @param $links
* @param $file
* @return array
*/
public function add_plugin_row_meta( $links, $file )
{
$plugin = plugin_basename( __FILE__ );
$add_links = array();
if ( $file == $plugin ) {
$add_links[] = 'View more plugins';
}
return array_merge( $links, $add_links );
}
/**
* Disabled now, not sure if this will be used in the future.
* Was running from __construct:
* if ( $this->settings['number_to_show'] == 'all' ) {
* add_filter( 'gettext', array( $this, 'change_show_tags_text'), 1, 3 );
* }
*/
/*public function change_show_tags_text( $translated_text, $text, $domain )
{
if ( 'Choose from the most used tags' == $text ) // && 'your-post-type' == $typenow
{
$translated_text = __( 'Choose from all tags', 'admin-tag-ui' );
}
return $translated_text;
}*/
/**
* Default settings for the plugin. Returns an array if no parameters passed in. Pass in a name to get the default for that setting.
* @param string $setting
* @return array|mixed
*/
public function default_settings( $setting = '' )
{
$default = array(
'number_to_show' => 'all',
'number_of_columns' => 2,
'tag_size' => 'same',
'auto_reveal' => 1,
);
if ( empty( $setting ) )
{
return $default;
}
else
{
return $default[ $setting ];
}
}
/**
* Adds css
HTML;
echo $output;
}
/**
* Adds jquery to the footer
* Needs to be in the footer otherwise the click() command will run too early.
*
* - action admin_footer-post.php - __construct()
* - action admin_footer-post-new.php - __construct()
*
* 1.1.0
* - Moved {$tag_link_wording} within jQuery.ready() function
* - Prevented the screen from jumping back to the tag input field by disabling it temporarily (focus does not jump to disabled elements).
* - Added atui_check_tag_selection() to highlight tags which have been selected in the selectable tags section.
* - Used setTimeout() to let other events fire. May need to review this and attempt another approach?
*
* 1.1.1
* - Changed ".the-tagcloud > a[class^=tag-link-]" to ".the-tagcloud > a[class*=tag-link-]" (makes it compatible with WordPress 4.8)
* - find tags to highlight by using .filter(). :contains caused additional tags to be matched.
*
* 1.1.2
* - WordPress 4.9 changed tag cloud's html structure in the page. Selectors had to be changed.
* - Added listener to "Add" button. Will now run highlight tag function after being clicked.
*/
public function add_script_to_footer()
{
$auto_reveal = $this->settings['auto_reveal'] ? 'jQuery( ".tagcloud-link" ).click();' : '';
if ( $this->settings['number_to_show'] == 'all' )
{
$all = __( 'all', 'admin-tag-ui' );
$tag_link_wording = "var tag_wording = jQuery( '.tagcloud-link' ).text().replace( 'the most used', '{$all}' );\n";
$tag_link_wording .= "jQuery( '.tagcloud-link' ).text( tag_wording );\n";
$tag_link_wording .= "jQuery( '.tagcloud-link' ).css( 'visibility', 'visible' );\n";
}
else
{
$tag_link_wording = '';
}
$output = <<
jQuery( document ).ready( function() {
function atui_check_tag_selection()
{
// Removes the selected class from all tags
jQuery( '.the-tagcloud a[class*=tag-link-]' ).each( function() {
jQuery( this ).removeClass( 'atui-tag-selected' );
});
// Adds the selected class back to tags which have been selected
jQuery( '.tagchecklist span.screen-reader-text' ).each( function() {
selected_tag_text = jQuery( this ).text().replace( 'Remove term: ', '' ).trim().toLowerCase();
// console.log( 'selected tag: ' + jQuery( this ).text().replace( 'Remove term: ', '' ).trim().toLowerCase() + ' ' );
jQuery( '.the-tagcloud a[class*=tag-link-]' ).filter( function() {
// console.log( 'selectable tag: ' + jQuery( this ).text().trim().toLowerCase() + ' ' );
if ( jQuery( this ).text().trim().toLowerCase() === selected_tag_text ) {
jQuery( this ).addClass( 'atui-tag-selected' );
// console.log( 'matching tag: ' + jQuery( this ).text().trim().toLowerCase() + ' ' );
}
});
});
}
jQuery( '.tagchecklist' ).on(
{
mouseenter: function() {
jQuery( this ).parent( 'span' ).addClass( 'atui-delete-hover' ); // prior to wordpress 4.9
jQuery( this ).parent( 'li' ).addClass( 'atui-delete-hover' ); // wordpress 4.9+
},
mouseleave: function() {
jQuery( this ).parent( 'span' ).removeClass( 'atui-delete-hover' ); // prior to wordpress 4.9
jQuery( this ).parent( 'li' ).removeClass( 'atui-delete-hover' ); // wordpress 4.9+
},
mouseup: function() {
setTimeout( function() {
atui_check_tag_selection();
}, 100);
}
}, '.ntdelbutton' );
/** Disabling the newtag input prevents focus from jumping to it */
function atui_disable_newtag() {
jQuery( 'input.newtag' ).prop( 'disabled', true );
}
/**
* setTimeout() re-queues the statement at the end of the execution queue
* Allows other events to finish (e.g. showing the newly selected tag) before re-enabling input
*/
function atui_enable_newtag()
{
setTimeout( function() {
jQuery( 'input.newtag' ).prop( 'disabled', false );
jQuery( this ).blur();
}, 100);
}
jQuery( '.postbox-container' ).on(
{
mousedown: function() {
atui_disable_newtag();
},
keydown: function(e) {
// 13 is the enter key
if( e.which == 13 ) {
atui_disable_newtag();
}
},
mouseup: function() {
atui_enable_newtag();
setTimeout( function() {
atui_check_tag_selection();
}, 100);
},
keyup: function(e) {
// 13 is the enter key
if( e.which == 13 ) {
atui_enable_newtag();
setTimeout( function() {
atui_check_tag_selection();
}, 100);
}
}
}, '.the-tagcloud a[class*=tag-link-]' );
// highlights tags after clicking "Choose from all tags"
jQuery( '.postbox-container' ).on(
{
mouseup: function() {
setTimeout( function() {
atui_check_tag_selection();
}, 1500);
}
}, '.tagcloud-link' );
// highlights tags after clicking "Add" button
jQuery( '.postbox-container' ).on(
{
mouseup: function() {
setTimeout( function() {
atui_check_tag_selection();
}, 100);
}
}, '.tagadd' );
{$auto_reveal}
{$tag_link_wording}
setTimeout( function() {
atui_check_tag_selection();
}, 1500);
});
HTML;
echo $output;
}
/**
* Adds array of default settings to an option in the wp options table
* - register_activation_hook __FILE__
*/
public function install_plugin()
{
add_option( 'atui_settings', $this->default_settings() );
}
}
if ( is_admin() )
{
$admin_tag_ui_plugin = new Admin_Tag_UI_Plugin();
}