table_name = $wpdb->prefix . "terms_hit"; $this->tag_set = get_option('sj_tag_set'); if (!$this->tag_set) $this->tag_set = array(0 => 'Default Set'); # hooks register_activation_hook(__FILE__, array(&$this, 'activate_plgin')); add_action('parse_query', array(&$this, 'increase_tag_view_count')); add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts')); add_action('admin_menu', array(&$this, 'trigget_admin_menu')); add_action('plugins_loaded', array(&$this, 'load_plugin_textdomain')); } public static function getInstance() { if (!self::$instance) self::$instance = new self; return self::$instance; } # Activation Hook & Make Database # 구동 시 훅을 걸어 DB를 생성합니다. public function activate_plgin() { global $wpdb; if($wpdb->get_var("show tables like '$this->table_name'") != $this->table_name) { $sql = "CREATE TABLE $this->table_name ( term_id bigint(20) NOT NULL, hit bigint(20) DEFAULT 0 NOT NULL, UNIQUE KEY id(term_id) );"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); add_option("sj_tag_db_version", $jal_db_version); } } # When visitors click the post or tag # 내가 만든 DB에 데이터를 쑤셔 넣으라우! public function increase_tag_view_count() { if (is_single()) { global $post; if ($post) $this->increase_tag_view_count_of_single_post($post->ID); } if (is_tag() && $query->is_main_query()) { $term = get_queried_object(); if (!$term) return false; $this->increase_tag_view_count_of_tag($term->term_id); } } private function increase_tag_view_count_of_single_post($post_id) { $tags = get_the_tags($post_id); # For another post type # 포스트 톼잎이 틀리면 get_the_tags로 가져올 수 없더라 if (!$tags) { global $wp_query; $tags = get_the_terms($wp_query->query_vars['p'], 'post_tag'); } if ($tags) { foreach($tags as $tag) { $this->increase_tag_view_count_of_tag($tag->term_id); } } } private function increase_tag_view_count_of_tag($tag_id) { global $wpdb; if ($hit = $wpdb->get_var("SELECT hit FROM $this->table_name WHERE term_id = $tag_id")) { $hit++; $wpdb->update($this->table_name, array('hit' => $hit), array('term_id' => $tag_id)); } else { $wpdb->insert($this->table_name, array('term_id' => $tag_id, 'hit' => 1)); } } public function load_plugin_textdomain() { $lang_dir = basename(dirname(__FILE__)) . '/languages'; load_plugin_textdomain($this->text_domain, 'wp-content/plugins/' . $lang_dir, $lang_dir); } # 태그클라우드를 뽑아요! public function get_tag_cloud() { global $wpdb; $tag_config = get_option($this->set_guid); # initialize; $config = $this->parse_config($tag_config); $tag_config = $config['tag_config']; extract($config, EXTR_SKIP); $tag_config['color'] = $this->convert_css_style($tag_config['color']); $query_count = ' SELECT terms.term_id as term_id, terms.name as tag_name, taxonomy.count as post_count, count.hit as post_hit FROM ' . $wpdb->term_taxonomy . ' as taxonomy LEFT JOIN ' . $wpdb->terms . ' as terms ON taxonomy.term_id = terms.term_id LEFT JOIN ' . $wpdb->term_relationships . ' as relationship ON terms.term_id = relationship.term_taxonomy_id LEFT JOIN ' . $wpdb->posts . ' as post ON post.ID = relationship.object_ID LEFT JOIN ' . $this->table_name . ' as count ON count.term_id = terms.term_id WHERE taxonomy.taxonomy = "post_tag" AND count <> 0 GROUP BY terms.term_id ORDER BY post_count DESC LIMIT ' . $this->number_of_tags . ' '; $tags_count = $wpdb->get_results($query_count); // 포함수 $query_hit = ' SELECT terms.term_id as term_id, terms.name as tag_name, taxonomy.count as post_count, count.hit as post_hit FROM ' . $wpdb->term_taxonomy . ' as taxonomy LEFT JOIN ' . $wpdb->terms . ' as terms ON taxonomy.term_id = terms.term_id LEFT JOIN ' . $wpdb->term_relationships . ' as relationship ON terms.term_id = relationship.term_taxonomy_id LEFT JOIN ' . $wpdb->posts . ' as post ON post.ID = relationship.object_ID LEFT JOIN ' . $this->table_name . ' as count ON count.term_id = terms.term_id WHERE taxonomy.taxonomy = "post_tag" AND count <> 0 GROUP BY terms.term_id ORDER BY post_hit DESC LIMIT ' . $this->number_of_tags . ' '; $tags_hit = $wpdb->get_results($query_hit); // 히트수 $tags = array(); // 히트와 뷰를 한 개씩 섞는다 $k = 0; for ($i=0; $i<$this->number_of_tags; $i++) { if (isset($tags_count[$i])) { $tags[$tags_count[$i]->term_id] = $tags_count[$i]; if (!isset($tags[$tags_count[$i]->term_id])) { $k++; if ($k == $this->number_of_tags) break; } } if ($this->sort_by == 'intersection') { $j = $this->number_of_tags - $i; } else { $j = $i; } if (isset($tags_hit[$j])) { $tags[$tags_hit[$j]->term_id] = $tags_hit[$j]; if (!isset($tags[$tags_hit[$j]->term_id])) { $k++; if ($k == $this->number_of_tags) break; } } } # 한 자리에 몰아넣는 배열을 만든다, 민/맥스도 뽑고 각각의 녀석에게 스타일도 부여하기 위해 $hit = $count = $tags_out = array(); foreach ($tags as $tag) { $hit[$tag->term_id] = $tag->post_hit; $count[$tag->term_id] = $tag->post_count; } # 값으로 정렬 asort($count); asort($hit); # 한 단계에 몇 개의 태그가 들어가는지... $tag_step = count($tags) / $tag_step; # 두바퀴만 더 돌려 카운트와 히트를 스텝에 맞는 값으로 변환한다 $i = 0; $prev_value = -1; $prev_chanded = -1; foreach ($count as $key => &$value) { if ($prev_value == $value) { $value = $prev_chanded; } else { $prev_value = $value; $value = $prev_chanded = floor($i / $tag_step) + 1; // 0,1,2 대신 1,2,3을 사용했으니 편의상 +1 } $i++; } $i = 0; $prev_value = -1; $prev_chanded = -1; foreach ($hit as $key => &$value) { if ($prev_value == $value) { $value = $prev_chanded; } else { $prev_value = $value; $value = $prev_chanded = floor($i / $tag_step) + 1; // 0,1,2 대신 1,2,3을 사용했으니 편의상 +1 } $i++; } if ($this->sort_by == 'name') { $new_tag = array(); foreach ($tags as $tag) { $new_tag[strtolower($tag->tag_name)] = $tag; } ksort($new_tag); $tags = $new_tag; } # 준비는 끝났다 +_+ 이제 녀석들을 만들어보자 $i = 0; $tags_out = array(); foreach ($tags as $tag) { $link = get_tag_link($tag->term_id); if ($tag_method == 'click-color') { $tag_size = $count[$tag->term_id]; $tag_color = $hit[$tag->term_id] ? $hit[$tag->term_id] : 1; } else { $tag_color = $count[$tag->term_id]; $tag_size = $hit[$tag->term_id] ? $hit[$tag->term_id] : 1; } $tags_out[] = '' . $tag->tag_name . ''; $i++; } return '
' . implode($separator, $tags_out) . '
'; } public function admin_enqueue_scripts() { wp_enqueue_script('jquery'); wp_enqueue_script('jquery-ui-core'); wp_enqueue_script('jquery-ui-widget'); wp_enqueue_script('jquery-ui-button'); wp_enqueue_script('jquery-ui-spinner'); wp_enqueue_style('jquery-ui'); wp_enqueue_script('iris'); wp_enqueue_script('sujin_tag', plugin_dir_url( __FILE__ ) . '/assets/admin.js'); wp_enqueue_style('sujin_tag', plugin_dir_url( __FILE__ ) . '/assets/admin.css'); } public function print_css() { $tag_config = get_option($this->set_guid); # initialize; $config = $this->parse_config($tag_config); $tag_config = $config['tag_config']; extract($config, EXTR_SKIP); $tag_config['color'] = $this->convert_css_style($tag_config['color']); $style = 'margin-right:' . $margin_right . 'px !important; margin-bottom:' . $margin_bottom . 'px !important; display:inline-block !important; line-height:' . $line_height . $line_height_unit . ' !important; text-decoration:none !important;'; $output = 'body .sj_tagcloud_set_' . $this->set_number . ' a {' . $style . '}'; for($i=1; $i<=$tag_step; $i++) { if (!empty($tag_config['color'][$i]['color'])) $style_color = 'color:' . $tag_config['color'][$i]['color'] . ' !important;'; if (!empty($tag_config['color'][$i]['bgcolor'])) $style_color.= 'background-color:' . $tag_config['color'][$i]['bgcolor'] . ' !important;'; if (!empty($tag_config['color'][$i]['radius'])) $style_color.= 'border-radius:' . $tag_config['color'][$i]['radius'] . 'px !important;'; if (!empty($tag_config['color'][$i]['padding'])) $style_color.= 'padding:' . $tag_config['color'][$i]['padding'] . ' !important;'; if (!empty($tag_config['size'][$i])) $style_size = 'font-size:' . $tag_config['size'][$i] . 'px !important;'; $output.= 'body .sj_tagcloud_set_' . $this->set_number . ' a.size_' . $i . ' {' . $style_size . '}'; $output.= 'body .sj_tagcloud_set_' . $this->set_number . ' a.color_' . $i . ' {' . $style_color . '}'; } return $output; } private function convert_css_style($options) { foreach($options as &$color) { if(!$color['bgcolor']) $color['bgcolor'] = 'transparent'; $padding2 = $color['padding'] + 2; if ($color['padding']) $color['padding'] = $color['padding'] . 'px ' . $padding2 . 'px'; } return $options; } private function parse_config($options) { # Default Option if (!$options) { $tag_step = 1; $line_height = 1.3; $line_height_unit = 'em'; $margin_right = 5; $margin_bottom = 10; $tag_config = array( 'color' => array( 1 => array( 'color' => '#000000', 'bgcolor' => '', 'radius' => 0, 'padding' => 0 ) ), 'size' => array( 1 => 12 ) ); $tag_method = 'click-color'; } else { $tag_step = $options['tag_step']; $tag_method = $options['tag_method']; $line_height = $options['line_height']; $line_height_unit = $options['line_height_unit']; $margin_right = $options['margin_right']; $margin_bottom = $options['margin_bottom']; $tag_config = $options['tag_config']; } return array( 'tag_step' => $tag_step, 'tag_method' => $tag_method, 'line_height' => $line_height, 'line_height_unit' => $line_height_unit, 'margin_right' => $margin_right, 'margin_bottom' => $margin_bottom, 'tag_config' => $tag_config ); } public function trigget_admin_menu() { add_options_page(__('2D Tag Cloud', $this->text_domain), __('2D Tag Cloud', $this->text_domain), 'manage_options', '2D-tag-cloud-options', array(&$this, 'admin_menu')); } public function admin_menu() { if (isset($_POST['action']) && check_admin_referer($this->text_domain)) { switch($_POST['action']) { case 'update' : if (!$_POST['set_current_id']) { $this->set_by_number(0); } else { $this->set_by_number($_POST['set_current_id']); } $this->save_option(); break; case 'makenew' : $this->make_new_option(); break; case 'delete' : $this->delete_option(); break; } } if (!empty($_GET['set'])) $this->set_by_number($_GET['set']); $this->print_admin_page(); } public function save_option() { $tag_config = array(); for($i=1; $i<$_POST['tag_step']+1; $i++) { $tag_config['color'][$i] = array( 'color' => $_POST['tag_color_step_' . $i], 'bgcolor' => $_POST['tag_bgcolor_step_' . $i], 'radius' => $_POST['tag_radius_step_' . $i], 'padding' => $_POST['tag_padding_step_' . $i] ); $tag_config['size'][$i] =$_POST['tag_size_step_' . $i]; } $tag_config = array( 'tag_step' => $_POST['tag_step'], 'tag_method' => $_POST['tag_method'], 'setting_method' => $_POST['setting_method'], 'line_height' => $_POST['line_height'], 'line_height_unit' => $_POST['line_height_unit'], 'margin_right' => $_POST['margin_right'], 'margin_bottom' => $_POST['margin_bottom'], 'tag_config' => $tag_config ); update_option($this->set_guid, $tag_config); } private function make_new_option() { if (empty($_POST['set_name'])) { $this->redirect(get_site_url() . '/wp-admin/options-general.php?page=2D-tag-cloud-options'); } $this->set_name = $_POST['set_name']; $this->tag_set[] = $this->set_name; $this->set_tag_set(); end($this->tag_set); $this->set_by_number(key($this->tag_set)); $this->save_option(); $this->redirect(get_site_url() . '/wp-admin/options-general.php?page=2D-tag-cloud-options&set=' . $this->set_number); } private function delete_option() { if (empty($_POST['set_current_id'])) { $this->redirect(get_site_url() . '/wp-admin/options-general.php?page=2D-tag-cloud-options'); } unset($this->tag_set[$_POST['set_current_id']]); $this->set_tag_set(); $this->set_by_number($_POST['set_current_id']); delete_option($this->set_guid); $this->redirect(get_site_url() . '/wp-admin/options-general.php?page=2D-tag-cloud-options'); } private function set_tag_set() { update_option('sj_tag_set', $this->tag_set); } public function set_by_number($number) { $this->set_number = isset($number) ? $number : 0; $this->set_guid = ($number != 0) ? 'sj_tag_conifg_' . $number : 'sj_tag_conifg'; } public function set_by_name($name) { foreach($this->tag_set as $key => $value) { if ($value == $name) { $this->set_by_number($key); break; } } } public function set_cloud_option($number, $separator, $sort) { $this->number_of_tags = $number; $this->tag_separator = $separator; $this->sort_by = $sort; } private function redirect($url) { echo ''; die; } private function print_admin_page() { $tag_config = get_option($this->set_guid); $config = $this->parse_config($tag_config); $tag_config = $config['tag_config']; extract($config); ?>

text_domain); ?>

text_domain) ?>
text_domain); ?> text_domain); ?>

text_domain); ?>

$value) { ?> $value) { ?> $value) { ?> $value) { ?> $value) { ?> $value) { ?>
  text_domain); ?>
text_domain); ?>
text_domain); ?>
text_domain); ?>
text_domain); ?>
text_domain); ?>

text_domain); ?> text_domain); ?>

%s', $this->text_domain), __('this page', $this->text_domain)); ?>

text_domain); ?>

'; print_r($array); echo ''; } } $sj2DTag = sj2DTag::getInstance();