$rule ){
if( $new_tags_object->term_id == $rule['atr_tag'][0] ){
$new_tag_link = get_permalink( intval($rule['atr_redirect'][0]) );
break;
}
}
return $new_tag_link;
}
//Value 'type_rules' can be 'string' or 'array'
private function get_saving_tag_rules( $tag_rules = array(), $type_rules = 'string' ){
if( !is_array($tag_rules) && empty($tag_rules) ) return false;
$tag_ids = array();
foreach ($tag_rules as $key => $value) {
$tag_ids[] = $value['atr_tag'][0];
}
$rules = ($type_rules != 'string') ? $tag_ids : implode(',', $tag_ids);
return $rules;
}
//Get attribute to redirect (page_id OR post_id)
private function get_atr_redirect_by_tag_id( $tag_id ){
$array_atr_rules = get_option( 'atr_option' ) ? get_option( 'atr_option' ) : array();
if( empty($array_atr_rules) ) return false;
foreach( $array_atr_rules as $key_rule => $rule ){
if( $rule['atr_tag'][0] == $tag_id ) return $rule['atr_redirect'][0];
}
return false;
}
private function get_saved_result($notice_type, $notice_text) {
$color = ($notice_type === 'success') ? 'green' : 'red';
return ''. $notice_text .'';
}
public function atr_template_output() {
global $title;
$notice = $rules = '';
printf('
%s
', $title);
$array_tags = get_terms( 'post_tag', array() ) ? : array();
if( empty($array_tags) ){
printf('%s
', __('Tags not found.', self::$prefix));
return;
}
$args = array( 'posts_per_page' => -1, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = new WP_Query( $args );
if( $array_tags && !empty($postslist) ){
// ADD
if( !empty( $_POST['is_atr_submit'] ) ){
$array_atr_rules = get_option( 'atr_option' ) ? get_option( 'atr_option' ) : array();
$array_atr_rules[] = array(
'atr_tag' => $_POST['atr_tag'],
'atr_redirect' => $_POST['atr_redirect']
);
$notice = ( update_option('atr_option', $array_atr_rules ) != false ) ? $this->get_saved_result('success', __('Saved successfully', self::$prefix)) : '';
}
// REMOVE
if( !empty( $_POST['is_atr_remove'] ) ){
if( $_POST['atr_remove_rule'] == 'atr_all_tags' ){
$notice = ( delete_option('atr_option') != false ) ? $this->get_saved_result('success', __('Rules was removed successfully.', self::$prefix)) : $this->get_saved_result('error', __('Rule not removed.', self::$prefix));
}else{
$array_atr_rules = get_option( 'atr_option' ) ? get_option( 'atr_option' ) : array();
foreach ($array_atr_rules as $key => $rule) {
if( $rule['atr_tag'][0] == $_POST['atr_remove_rule'] ){
unset($array_atr_rules[$key]);
break;
}
}
$notice = ( update_option('atr_option', $array_atr_rules ) != false ) ? $this->get_saved_result('success', __('Current rule was removed successfully.', self::$prefix)) : '';
}
}
$array_atr_rules = get_option( 'atr_option' ) ? get_option( 'atr_option' ) : array();
$rules = $this->get_saving_tag_rules( $array_atr_rules );
$args_tag_hide = $args_tag_show = array();
$not_all_tags = $only_tags = array();
if( $rules != false ){
$args_tag_hide['exclude'] = $rules;
$args_tag_show['include'] = $rules;
}
$not_all_tags = get_tags( $args_tag_hide );
$only_tags = get_tags( $args_tag_show );
?>
$value) {
$post_link = get_permalink( intval($value['atr_redirect'][0]) );
$curr_tag = get_term_by('id', intval($value['atr_tag'][0]), 'post_tag' );
if( strpos( $_SERVER['REQUEST_URI'], '/tag/' . $curr_tag->slug) !== false || strpos( $_SERVER['REQUEST_URI'], '/tag/' . $curr_tag->name) !== false){
wp_redirect( $post_link );
die();
}
}
}
}
#showing the converted post tags
public function atr_replace_link( $tags ){
$array_atr_rules = get_option( 'atr_option' ) ? get_option( 'atr_option' ) : array();
if( empty( $array_atr_rules ) ) return $tags;
$custom_tag_ids = $this->get_saving_tag_rules( $array_atr_rules, $type_rules = 'array' );
$new_tags = $custom_tags = array();
if( !empty( $tags ) ){
foreach ( $tags as $tag ) {
preg_match_all( '/href\=\"(.*)\"/U', $tag, $matches );
$new_tags = explode( '/tag/', $matches[1][0] );
$new_tags = substr( $new_tags[1], 0, -1 );
$new_tags_object = get_term_by( 'slug', $new_tags, 'post_tag' );
if( in_array( $new_tags_object->term_id, $custom_tag_ids ) ){
$atr_redirect = $this->get_atr_redirect_by_tag_id( $new_tags_object->term_id );
if( $atr_redirect ) $custom_tags[] = ''. $new_tags_object->name .'';
}else{
$custom_tags[] = $tag;
}
}
return $custom_tags;
}
return $tags;
}
public function register_admin_assets(){
// load script
wp_register_script('atrAdminJs', self::$plugin_url . 'assets/js/admin.js', array( 'jquery' ), self::$version );
wp_enqueue_script('atrAdminJs');
// load style
wp_register_style ( 'atrAdminCss', self::$plugin_url . 'assets/css/admin.css', array(), self::$version );
wp_enqueue_style ( 'atrAdminCss' );
}
/**
* Do things on plugin activation.
*/
public function activate() {
return true;
}
/**
* Flush permalinks on plugin deactivation.
*/
public function deactivate() {
flush_rewrite_rules();
}
/**
* Do things on plugin uninstall.
*/
public function uninstall() {
if ( ! current_user_can( 'activate_plugins' ) ) return;
check_admin_referer( 'bulk-plugins' );
if ( __FILE__ != WP_UNINSTALL_PLUGIN ) return;
}
}
$atr_plugin = new ATR_Plugin();
$atr_plugin->load();
}
?>