Advertisement
Guest User

wp-content/plugins/nktagcloud/inc/page.php

a guest
Feb 5th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.94 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * TODO do we really need to do this all the time?
  5.  */
  6. $option = get_option( 'nktagcloud' );
  7. $config = $option['config'];
  8. if ( $config['homelink'] === 'No') {
  9.     add_action('wp_footer', 'nktagcloud_footer');
  10. }
  11. add_action('wp_head', 'nktagcloud_head');
  12.  
  13. /**
  14.  * The widget
  15.  *
  16.  * @param $args TODO
  17.  */
  18. function widget_nktagcloud($args) {
  19.     extract($args);
  20.     echo $before_widget;
  21.     $option = get_option('nktagcloud');
  22.     $config = $option['config'];
  23.     $title = apply_filters( 'the_title', $config['title'] );
  24.     // @fixme this is wrong. We should alway print $before_title etc but
  25.     // remove the <h2> tag if it exists
  26.     if ( ( $config['hideemptywidgetheader'] == 'Yes' && isset( $title ) && $title != '' ) || $config['hideemptywidgetheader'] != 'Yes') {
  27.         echo $before_title;
  28.         echo $title;
  29.         echo $after_title;
  30.     }
  31.     echo nktagcloud_the_cloud($config);
  32.     echo $after_widget;
  33. }
  34.  
  35. /**
  36.  * Output the tag cloud as configured
  37.  *
  38.  * @param array $option the plugin config
  39.  *
  40.  * @return string the tag cloud
  41.  *
  42.  * @since 0.8.1
  43.  */
  44. function nktagcloud_the_cloud( $config ) {
  45.     $smallest               = $config['smallest'];
  46.     $largest                = $config['largest'];
  47.     $unit                   = $config['unit'];
  48.     $number                 = $config['number'];
  49.     $format                 = $config['format'];
  50.     $order                  = $config['order'];
  51.     $orderby                = $config['orderby'];
  52.     $exclude                = $config['exclude'];
  53.     $include                = $config['include'];
  54.     $categories             = $config['categories'];
  55.     $replace                = $config['replace'];
  56.     /*
  57.      * The 'single' and 'post_id' settings are not configurable through the
  58.      * options page and only intended for the shortcodes.
  59.      */
  60.     $single                 = 'No';
  61.     $post_id                = null;
  62.     $mincount               = $config['mincount'];
  63.     $separator              = $config['separator'];
  64.     $hidelastseparator      = $config['hidelastseparator'];
  65.     $inject_count           = $config['inject_count'];
  66.     $inject_count_outside   = $config['inject_count_outside'];
  67.     $taxonomy               = $config['taxonomy'];
  68.     $nofollow               = $config['nofollow'];
  69.    
  70.     if ( function_exists( 'taxonomy_exists' ) ) {
  71.         if ( !taxonomy_exists( $taxonomy ) )
  72.             $taxonomy       = 'post_tag';
  73.     }
  74.     else
  75.         $taxonomy           = 'post_tag'; // force tags for < 3.0
  76.  
  77.     return nk_wp_tag_cloud("largest=$largest&smallest=$smallest&unit=$unit&number=$number&format=$format&order=$order&orderby=$orderby&exclude=$exclude&include=$include&categories=$categories&single=$single&replace=$replace&separator=$separator&hidelastseparator=$hidelastseparator&inject_count=$inject_count&inject_count_outside=$inject_count_outside&post_id=$post_id&mincount=$mincount&nofollow=$nofollow&taxonomy=$taxonomy");
  78. }
  79.  
  80. /**
  81.  * Print the default CSS styles in the <head>
  82.  *
  83.  * @param string $underline TODO
  84.  * @todo fix above
  85.  * @todo external file
  86.  */
  87. function nktagcloud_head( $underline ) {
  88.     $option = get_option( 'nktagcloud' );
  89.     $config = $option['config'];
  90.  
  91.     if ( !( $config['rmcss'] === 'Yes' ) && ( $config['format'] === 'list' )) { ?>
  92.         <style type="text/css">
  93.         .better-tag-cloud-shortcode li,
  94.         .better-tag-cloud-shortcode li a,
  95.         li#better-tag-cloud ul.wp-tag-cloud li,
  96.         li#better-tag-cloud ul.wp-tag-cloud li a {
  97.             display:    inline;
  98.             <?php
  99.                 if ($underline == 'none') {
  100.                     echo "text-decoration: none;";
  101.                 }
  102.                 elseif ($underline == 'underline') {
  103.                     echo "text-decoration: underline;";
  104.                 } ?>
  105.         }
  106.         </style> <?php
  107.     }
  108.     if ( $option['config']['colorize'] == 'Yes' ) {
  109.         global $nktagcloud; ?>
  110.         <link rel="stylesheet" href="<?php echo $nktagcloud['url'] . 'css/page.css?v=0.9.0' ?>" type="text/css" media="all" /> <?php
  111.     }
  112. }
  113.  
  114. /**
  115.  * Print the link to the plugin page
  116. */
  117. # TODO rename function?
  118. function nktagcloud_footer() {
  119.     _e('<a href="http://www.nkuttler.de/wordpress-plugin/a-better-tag-cloud-widget/">Better Tag Cloud</a>', 'nktagcloud' );
  120.     echo '<br />';
  121. }
  122.  
  123. /* This was copied from the wordpress source and modified, see
  124.  * wp-includes/category-template.php
  125. */
  126.  
  127. /**
  128.  * Display tag cloud.
  129.  *
  130.  * The text size is set by the 'smallest' and 'largest' arguments, which will
  131.  * use the 'unit' argument value for the CSS text size unit. The 'format'
  132.  * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
  133.  * 'format' argument will separate tags with spaces. The list value for the
  134.  * 'format' argument will format the tags in a UL HTML list. The array value for
  135.  * the 'format' argument will return in PHP array type format.
  136.  *
  137.  * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
  138.  * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
  139.  *
  140.  * The 'number' argument is how many tags to return. By default, the limit will
  141.  * be to return the top 45 tags in the tag cloud list.
  142.  *
  143.  * The 'topic_count_text_callback' argument is a function, which, given the count
  144.  * of the posts  with that tag, returns a text for the tooltip of the tag link.
  145.  *
  146.  * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
  147.  * function. Only one should be used, because only one will be used and the
  148.  * other ignored, if they are both set.
  149.  *
  150.  * The 'single' parameters is used to display only a single post's tags inside
  151.  * the loop.
  152.  *
  153.  * The 'categories' parameter will add categories to the tag cloud if set to 'Yes'
  154.  *
  155.  * The 'replace' paramter, if set to 'Yes', will change all blanks ' ' to &nbsp;
  156.  *
  157.  * The 'mincount' parameter will hide all tags that aren't used more often than
  158.  * mincount.
  159.  *
  160.  * The 'separator' will be printed between tags.
  161.  *
  162.  * 'inject_count' will add a counter to the tags is set to 'Yes'.
  163.  * 'inject_count_outside' will put this counter outside the tag link.
  164.  *
  165.  * @since 2.3.0
  166.  *
  167.  * @param array|string $args Optional. Override default arguments.
  168.  * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
  169.  */
  170. function nk_wp_tag_cloud( $args = '' ) {
  171.     $defaults = array(
  172.         'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
  173.         'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
  174.         'exclude' => '', 'include' => '', 'link' => 'view',
  175.         'single' => 'No',
  176.         'categories' => 'No',
  177.         'replace' => 'No',
  178.         'mincount' => '0',
  179.         'separator' => '',
  180.         'hidelastseparator' => 'No',
  181.         'inject_count' => 'No',
  182.         'inject_count_outside' => 'Yes',
  183.         'post_id' => null,
  184.         'nofollow' => 'No',
  185.         'taxonomy' => 'post_tag',
  186.     );
  187.     $args = wp_parse_args( $args, $defaults );
  188.  
  189.     if ( intval( $args['post_id'] ) != 0 ) {
  190.         $my_query = new WP_Query( "cat=$category" );
  191.         if ( $my_query->have_posts() ) {
  192.             while ( $my_query->have_posts() ) {
  193.                 $my_query->the_post();
  194.                 $tags = apply_filters( 'get_the_tags', get_the_terms( null, 'post_tag' ) );
  195.             }
  196.         }
  197.         $GLOBALS['post'] =  $GLOBALS['wp_query']->post; // restore $post
  198.     }
  199.     elseif ( $args['single'] == 'Yes' || $args['single'] == 'yes' ) {
  200.         $tags = apply_filters( 'get_the_tags', get_the_terms( null, $args['taxonomy'] ) );
  201.     }
  202.     else {
  203.         $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) );
  204.     }
  205.  
  206.     // nkuttler
  207.     if ( $args['categories'] == 'Yes' ) {
  208.         $tags = array_merge ( $tags, get_categories() );
  209.     }
  210.  
  211.     if ( empty( $tags ) )
  212.         return;
  213.  
  214.     foreach ( $tags as $key => $tag ) {
  215.         if ( 'edit' == $args['link'] ) {
  216.             $link = get_edit_tag_link( $tag->term_id );
  217.         }
  218.         /* use category or tag link, whatever is needed */
  219.         /* TODO check for duplicate cat/tag names.. */
  220.         else {
  221.             if ( isset( $tag->cat_ID ) ) {
  222.                 $link = get_category_link( $tag->term_id );
  223.             }
  224.             else {
  225.                 $link = get_term_link( $tag, $args['taxonomy'] );
  226.             }
  227.         }
  228.         if ( is_wp_error( $link ) ) {
  229.             return false;
  230.         }
  231.         $tags[ $key ]->link = $link;
  232.         $tags[ $key ]->id = $tag->term_id;
  233.     }
  234.  
  235.     $return = nk_wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
  236.  
  237.     $return = apply_filters( 'wp_tag_cloud', $return, $args );
  238.  
  239.     if ( 'array' == $args['format'] )
  240.         return $return;
  241.  
  242.     // nkuttler - always return
  243.     return $return;
  244.     //echo $return;
  245. }
  246.  
  247. /**
  248.  * Generates a tag cloud (heatmap) from provided data.
  249.  *
  250.  * The text size is set by the 'smallest' and 'largest' arguments, which will
  251.  * use the 'unit' argument value for the CSS text size unit. The 'format'
  252.  * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
  253.  * 'format' argument will separate tags with spaces. The list value for the
  254.  * 'format' argument will format the tags in a UL HTML list. The array value for
  255.  * the 'format' argument will return in PHP array type format.
  256.  *
  257.  * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
  258.  * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
  259.  * 'RAND'.
  260.  *
  261.  * The 'number' argument is how many tags to return. By default, the limit will
  262.  * be to return the entire tag cloud list.
  263.  *
  264.  * The 'topic_count_text_callback' argument is a function, which given the count
  265.  * of the posts  with that tag returns a text for the tooltip of the tag link.
  266.  *
  267.  * See above ( nk_wp_tag_cloud() ) for more parameter definitions.
  268.  *
  269.  * @todo Complete functionality.
  270.  * @since 2.3.0
  271.  *
  272.  * @param array $tags List of tags.
  273.  * @param string|array $args Optional, override default arguments.
  274.  * @return string
  275.  */
  276. function nk_wp_generate_tag_cloud( $tags, $args = '' ) {
  277.     global $wp_rewrite;
  278.     $defaults = array(
  279.         'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
  280.         'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
  281.         'topic_count_text_callback' => 'default_topic_count_text',
  282.         'replace' => 'No',
  283.         'mincount' => '0',
  284.         'separator' => '',
  285.         'inject_count' => 'No',
  286.         'inject_count_outside' => 'Yes',
  287.         'nofollow' => 'No',
  288.     );
  289.  
  290.     if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
  291.         $body = 'return sprintf (
  292.             __ngettext('.var_export($args['single_text'], true).', '.var_export($args['multiple_text'], true).', $count),
  293.             number_format_i18n( $count ));';
  294.         $args['topic_count_text_callback'] = create_function('$count', $body);
  295.     }
  296.  
  297.     $args = wp_parse_args( $args, $defaults );
  298.  
  299.     extract( $args );
  300.  
  301.     if ( empty( $tags ) )
  302.         return;
  303.  
  304.     // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
  305.     if ( 'name' == $orderby )
  306.         uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') );
  307.     else
  308.         uasort( $tags, create_function('$a, $b', 'return ($a->count > $b->count);') );
  309.  
  310.     if ( 'DESC' == $order )
  311.         $tags = array_reverse( $tags, true );
  312.     elseif ( 'RAND' == $order ) {
  313.         $keys = array_rand( $tags, count( $tags ) );
  314.         foreach ( $keys as $key )
  315.             $temp[$key] = $tags[$key];
  316.         $tags = $temp;
  317.         unset( $temp );
  318.     }
  319.  
  320.     /* nkuttler: support sorting by two criteria */
  321.     if ( $order == 'both') {
  322.         $orderby = 'both';
  323.     }
  324.     if ( 'both' == $orderby ) {
  325.         function nktagcloud_compare_both( $a, $b ) {
  326.             $retval = strnatcmp( $b->count, $a->count );
  327.             if( !$retval ) return strnatcmp( $a->name, $b->name );
  328.             return $retval;
  329.         }
  330.         usort( $tags, 'nktagcloud_compare_both' );
  331.     }
  332.     /* end changes */
  333.  
  334.     if ( $number > 0 )
  335.         $tags = array_slice($tags, 0, $number);
  336.  
  337.     $counts = array();
  338.     foreach ( (array) $tags as $key => $tag )
  339.         $counts[ $key ] = $tag->count;
  340.  
  341.     $min_count = min( $counts );
  342.     $spread = max( $counts ) - $min_count;
  343.     if ( $spread <= 0 )
  344.         $spread = 1;
  345.     $font_spread = $largest - $smallest;
  346.     if ( $font_spread < 0 )
  347.         $font_spread = 1;
  348.     $font_step = $font_spread / $spread;
  349.  
  350.     $a = array();
  351.  
  352.     $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
  353.  
  354.     /**
  355.      * Modifications my nkuttler
  356.      */
  357.      $tags_to_show = 0;
  358.     if ( isset( $separator ) && $separator != '' ) {
  359.         $the_separator .= '<span class="nktagcloud-separator">';
  360.         $the_separator .= $separator;
  361.         $the_separator .= '</span>';
  362.         /**
  363.          * @since 0.11.1 Loop to count the number of tags that will be printed
  364.          */
  365.  
  366.         foreach ( $tags as $key => $tag ) {
  367.             $count = $counts[ $key ];
  368.             if ( $count > $mincount )
  369.                 $tags_to_show++;
  370.         }
  371.  
  372.     }
  373.     $the_separator = '';
  374.     $tags_shown_so_far = 0;
  375.     foreach ( $tags as $key => $tag ) {
  376.         $count = $counts[ $key ];
  377.         $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
  378.         $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
  379.         $tag_name = $tags[ $key ]->name;
  380.         if ( ( $tags_shown_so_far == $tags_to_show - 1 ) && $hidelastseparator == 'Yes' )
  381.             $the_separator = '';
  382.  
  383.         /**
  384.          * Modifications by nkuttler
  385.          * Check and print if we should print the tag counter, in- or outside
  386.          * the tag link
  387.          *
  388.          * @since 0.8.5 calculate size value earlier, add class="nktagcloud-size"
  389.          * @since 0.8.5 add option minimum occurence needed to display a tag
  390.          * @since 0.11.0 add nofollow option
  391.          */
  392.         if ( $replace == 'Yes' ) {
  393.             $tag_name = preg_replace( '# #', '&nbsp;', $tag_name );
  394.             $tag_name = preg_replace( '#-#', '&#8209;', $tag_name );
  395.         }
  396.         $echo_nofollow = '';
  397.         if ( isset( $nofollow ) && $nofollow == 'Yes' )
  398.             $echo_nofollow = ' rel="nofollow" ';
  399.  
  400.         $tagsize = round( $smallest + ( ( $count - $min_count ) * $font_step ), 2 );
  401.         $tagsize_int = (int) $tagsize;
  402.  
  403.         if ( $count > $mincount ) {
  404.             $tags_shown_so_far++;
  405.             if ( $inject_count_outside == 'Yes' ) {
  406.                 $a[] = "<a href='$tag_link' $echo_nofollow class='tag-link-$tag_id nktagcloud-$tagsize_int' title='" . esc_attr( $topic_count_text_callback( $count ) ) . "'$rel style='font-size: " .  $tagsize . "$unit;'>$tag_name</a>" . nktagcloud_add_counter( $tag, $inject_count ) . $the_separator;
  407.             }
  408.             else {
  409.                 $a[] = "<a href='$tag_link' $echo_nofollow class='tag-link-$tag_id nktagcloud-$tagsize_int' title='" . esc_attr( $topic_count_text_callback( $count ) ) . "'$rel style='font-size: " .  $tagsize . "$unit;'>$tag_name" . nktagcloud_add_counter( $tag, $inject_count ) ."</a>" . $the_separator;
  410.             }
  411.         }
  412.     }
  413.     /* end changes */
  414.  
  415.     switch ( $format ) :
  416.     case 'array' :
  417.         $return =& $a;
  418.         break;
  419.     case 'list' :
  420.         $return = "<ul class='wp-tag-cloud'>\n\t<li>";
  421.         $return .= join( "</li>\n\t<li>", $a );
  422.         $return .= "</li>\n</ul>\n";
  423.         break;
  424.     default :
  425.         $return = join( "\n", $a );
  426.         break;
  427.     endswitch;
  428.  
  429.     return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
  430. }
  431.  
  432. /*
  433.  * Add a tag counter to the tags
  434.  *
  435.  * @param string $tag Tag
  436.  * @param array $config Config
  437.  */
  438. function nktagcloud_add_counter( $tag, $inject_count ) {
  439.     if ( $inject_count == 'Yes' ) {
  440.         $inject  = '<span class="nktagcloud_counter">';
  441.         $inject .= '(' . $tag->count . ')';
  442.         $inject .= '</span>';
  443.         return $inject;
  444.     }
  445. }
  446.  
  447. /**
  448.  * Create a shortcode
  449.  *
  450.  * @since 0.8.1
  451.  */
  452. function nktagcloud_shortcode( $atts ) {
  453.     $option = get_option( 'nktagcloud' );
  454.     $config = $option['config'];
  455.  
  456.     /*
  457.      * Take plugin config as defaults
  458.      */
  459.     extract( shortcode_atts( array(
  460.         'smallest' =>               $config['smallest'],
  461.         'largest' =>                $config['largest'],
  462.         'unit' =>                   $config['unit'],
  463.         'number' =>                 $config['number'],
  464.         'format' =>                 $config['format'],
  465.         'orderby' =>                $config['orderby'],
  466.         'order' =>                  $config['order'],
  467.         'exclude' =>                $config['exclude'],
  468.         'include' =>                $config['include'],
  469.         'link' =>                   $config['link'],
  470.         'single' =>                 'No',
  471.         'categories' =>             $config['categories'],
  472.         'replace' =>                $config['replace'],
  473.         'mincount' =>               $config['mincount'],
  474.         'separator' =>              $config['separator'],
  475.         'inject_count' =>           $config['inject_count'],
  476.         'inject_count_outside' =>   $config['inject_count_outside'],
  477.         'post_id' =>                null,
  478.     ), $atts) );
  479.  
  480.     $cloud = nk_wp_tag_cloud("largest=$largest&smallest=$smallest&unit=$unit&number=$number&format=$format&order=$order&orderby=$orderby&exclude=$exclude&include=$include&categories=$categories&single=$single&replace=$replace&separator=$separator&inject_count=$inject_count&inject_count_outside=$inject_count_outside&post_id=$post_id");
  481.  
  482.     $before = '<div class="better-tag-cloud-shortcode" >';
  483.     $after = '</div>';
  484.     return $before . $cloud . $after;
  485. }
  486.  
  487. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement