Make WordPress Core


Ignore:
Timestamp:
02/27/2006 04:57:30 AM (21 years ago)
Author:
ryan
Message:

Bookmark/link rework. #2499

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/links.php

    r2795 r3570  
    2929    global $wpdb;
    3030    $cat_id = -1;
    31     $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
     31    $results = $wpdb->get_results("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$cat_name'");
    3232    if ($results) {
    3333        foreach ($results as $result) {
    34             $cat_id = $result->cat_id;
     34            $cat_id = $result->cat_ID;
    3535        }
    3636    }
     
    3939}
    4040
    41 function bool_from_yn($yn) {
    42     if ($yn == 'Y') return 1;
    43     return 0;
    44 }
    45 
    4641/** function wp_get_linksbyname()
    4742 ** Gets the links associated with the named category.
     
    5247        global $wpdb;
    5348
    54         $cat = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
    55                                                                                                 . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
    56                                                                                                 . " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_name='$category'");
    57 
    58         if (! $cat) {
     49        $cat_id = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$category' LIMIT 1");
     50
     51        if (! $cat_id)
    5952                return;
    60         }
    61 
    62         if (empty($args)) {
    63                 if ($cat->sort_desc == 'Y') {
    64                         $cat->sort_order = '_'.$cat->sort_order;
    65                 }
    66                 get_links($cat->cat_id, $cat->text_before_link, $cat->text_after_all,
    67                                                         $cat->text_after_link, bool_from_yn($cat->show_images), $cat->sort_order,
    68                                                         bool_from_yn($cat->show_description), bool_from_yn($cat->show_rating),
    69                                                         $cat->list_limit, bool_from_yn($cat->show_updated));
    70         } else {
    71                 $args = add_query_arg('category', $cat->cat_id, $args);
    72                 wp_get_links($args);
    73         }
     53
     54        $args = add_query_arg('category', $cat_id, $args);
     55        wp_get_links($args);
    7456} // end wp_get_linksbyname
    7557
     
    8466        global $wpdb;
    8567
    86         if (!empty($args) && false === strpos($args, '=')) {
    87                 // If args is not a query string, it's a category id.
    88                 $category = $args;
    89                 $cat = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
    90                                                                                                         . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
    91                                                                                                         . " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_id=$category");
    92                 if ($cat) {
    93                         if ($cat->sort_desc == 'Y') {
    94                                 $cat->sort_order = '_'.$cat->sort_order;
    95                         }
    96                         get_links($cat->cat_id, $cat->text_before_link, $cat->text_after_all,
    97                                                                 $cat->text_after_link, bool_from_yn($cat->show_images), $cat->sort_order,
    98                                                                 bool_from_yn($cat->show_description), bool_from_yn($cat->show_rating),
    99                                                                 $cat->list_limit, bool_from_yn($cat->show_updated));
    100                 }
    101         } else {
    102                 parse_str($args);
    103 
    104                 if (! isset($category)) $category = -1;
    105                 if (! isset($before)) $before = '';
    106                 if (! isset($after)) $after = '<br />';
    107                 if (! isset($between))  $between = ' ';
    108                 if (! isset($show_images)) $show_images = true;
    109                 if (! isset($orderby)) $orderby = 'name';
    110                 if (! isset($show_description)) $show_description = true;
    111                 if (! isset($show_rating)) $show_rating = false;
    112                 if (! isset($limit)) $limit = -1;
    113                 if (! isset($show_updated)) $show_updated = 1;
    114                 if (! isset($echo)) $echo = true;
    115 
    116                 return get_links($category, $before, $after, $between, $show_images, $orderby, $show_description, $show_rating, $limit, $show_updated, $echo);
    117         }
     68        if ( empty($args) )
     69                return;
     70
     71        if ( false === strpos($args, '=') ) {
     72                $cat_id = $args;
     73                $args = add_query_arg('category', $cat_id, $args);
     74        }
     75
     76        parse_str($args);
     77
     78        if (! isset($category)) $category = -1;
     79        if (! isset($before)) $before = '';
     80        if (! isset($after)) $after = '<br />';
     81        if (! isset($between))  $between = ' ';
     82        if (! isset($show_images)) $show_images = true;
     83        if (! isset($orderby)) $orderby = 'name';
     84        if (! isset($show_description)) $show_description = true;
     85        if (! isset($show_rating)) $show_rating = false;
     86        if (! isset($limit)) $limit = -1;
     87        if (! isset($show_updated)) $show_updated = 1;
     88        if (! isset($echo)) $echo = true;
     89
     90        return get_links($category, $before, $after, $between, $show_images, $orderby, $show_description, $show_rating, $limit, $show_updated, $echo);
    11891} // end wp_get_links
    11992
     
    155128        global $wpdb;
    156129
    157         $direction = ' ASC';
    158         $category_query = '';
    159         if ($category != -1) {
    160                 $category_query = " AND link_category = $category ";
    161         }
    162         if (get_settings('links_recently_updated_time')) {
    163                 $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_settings('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
    164         } else {
    165                 $recently_updated_test = '';
    166         }
    167         if ($show_updated) {
    168                 $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
    169         }
    170 
    171         $orderby = strtolower($orderby);
    172         if ($orderby == '')
    173                 $orderby = 'id';
    174         if (substr($orderby, 0, 1) == '_') {
    175                 $direction = ' DESC';
    176                 $orderby = substr($orderby, 1);
    177         }
    178 
    179         switch($orderby) {
    180                 case 'length':
    181                 $length = ", CHAR_LENGTH(link_name) AS length";
    182                 break;
    183                 case 'rand':
    184                         $orderby = 'rand()';
    185                         break;
    186                 default:
    187                         $orderby = " link_" . $orderby;
    188         }
    189 
    190         if (!isset($length)) {
    191                 $length = '';
    192         }
    193 
    194         $sql = "SELECT link_url, link_name, link_image, link_target, link_description, link_rating, link_rel $length $recently_updated_test $get_updated FROM $wpdb->links WHERE link_visible = 'Y' " . $category_query;
    195         $sql .= ' ORDER BY ' . $orderby . $direction;
    196         /* The next 2 lines implement LIMIT TO processing */
    197         if ($limit != -1)
    198                 $sql .= " LIMIT $limit";
    199         $results = $wpdb->get_results($sql);
     130        $results = get_linkz("category=$category&orderby=$orderby&show_updated=$show_updated&limit=$limit");
     131
    200132        if (!$results) {
    201133                return;
    202134        }
     135
    203136
    204137        $output = '';
     
    291224 ** }
    292225 **/
     226// Deprecate in favor of get_linkz().
    293227function get_linkobjectsbyname($cat_name = "noname" , $orderby = 'name', $limit = -1) {
    294228    global $wpdb;
    295229    $cat_id = -1;
    296     $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
     230    //$results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
     231    // TODO: Fix me.
    297232    if ($results) {
    298233        foreach ($results as $result) {
     
    338273 ** link_notes
    339274 **/
     275// Deprecate in favor of get_linkz().
    340276function get_linkobjects($category = -1, $orderby = 'name', $limit = -1) {
    341277    global $wpdb;
     
    446382 */
    447383function get_linkcatname($id = 0) {
    448     global $wpdb;
    449     $cat_name = '';
    450     if ('' != $id) {
    451         $cat_name = $wpdb->get_var("SELECT cat_name FROM $wpdb->linkcategories WHERE cat_id=$id");
    452     }
    453     return $cat_name;
     384    if ( empty($id) )
     385        return '';
     386 
     387        $cats = wp_get_link_cats($id);
     388
     389        if ( empty($cats) || ! is_array($cats) )
     390                return '';
     391
     392        $cat_id = $cats[0]; // Take the first cat.
     393
     394        $cat = get_category($cat_id);
     395        return $cat->cat_name;
    454396}
    455397
     
    460402 */
    461403function get_autotoggle($id = 0) {
    462     global $wpdb;
    463     $auto_toggle = $wpdb->get_var("SELECT auto_toggle FROM $wpdb->linkcategories WHERE cat_id=$id");
    464     if ('' == $auto_toggle)
    465         $auto_toggle = 'N';
    466     return $auto_toggle;
     404        return 0; 
    467405}
    468406
     
    512450 */
    513451function get_links_list($order = 'name', $hide_if_empty = 'obsolete') {
    514         global $wpdb;
    515 
    516452        $order = strtolower($order);
    517453
    518454        // Handle link category sorting
     455        $direction = 'ASC';
    519456        if (substr($order,0,1) == '_') {
    520                 $direction = ' DESC';
     457                $direction = 'DESC';
    521458                $order = substr($order,1);
    522459        }
    523460
    524         // if 'name' wasn't specified, assume 'id':
    525         $cat_order = ('name' == $order) ? 'cat_name' : 'cat_id';
    526 
    527461        if (!isset($direction)) $direction = '';
    528         // Fetch the link category data as an array of hashesa
    529         $cats = $wpdb->get_results("
    530                 SELECT DISTINCT link_category, cat_name, show_images,
    531                         show_description, show_rating, show_updated, sort_order,
    532                         sort_desc, list_limit
    533                 FROM `$wpdb->links`
    534                 LEFT JOIN `$wpdb->linkcategories` ON (link_category = cat_id)
    535                 WHERE link_visible =  'Y'
    536                         AND list_limit <> 0
    537                 ORDER BY $cat_order $direction ", ARRAY_A);
     462
     463        $cats = get_categories("type=link&orderby=$order&order=$direction");
    538464
    539465        // Display each category
     
    541467                foreach ($cats as $cat) {
    542468                        // Handle each category.
    543                         // First, fix the sort_order info
    544                         $orderby = $cat['sort_order'];
    545                         $orderby = (bool_from_yn($cat['sort_desc'])?'_':'') . $orderby;
    546469
    547470                        // Display the category name
    548                         echo '  <li id="linkcat-' . $cat['link_category'] . '"><h2>' . $cat['cat_name'] . "</h2>\n\t<ul>\n";
     471                        echo '  <li id="linkcat-' . $cat->cat_ID . '"><h2>' . $cat->cat_name . "</h2>\n\t<ul>\n";
    549472                        // Call get_links() with all the appropriate params
    550                         get_links($cat['link_category'],
    551                                 '<li>',"</li>","\n",
    552                                 bool_from_yn($cat['show_images']),
    553                                 $orderby,
    554                                 bool_from_yn($cat['show_description']),
    555                                 bool_from_yn($cat['show_rating']),
    556                                 $cat['list_limit'],
    557                                 bool_from_yn($cat['show_updated']));
     473                        get_links($cat->cat_ID,
     474                                '<li>',"</li>","\n");
    558475
    559476                        // Close the last category
     
    563480}
    564481
     482function get_linkz($args = '') {
     483        global $wpdb;
     484
     485        parse_str($args, $r);
     486
     487        if ( !isset($r['orderby']) )
     488                $r['orderby'] = 'name';
     489        if ( !isset($r['order']) )
     490                $r['order'] = 'ASC';
     491        if ( !isset($r['limit']) )
     492                $r['limit'] = -1;
     493        if ( !isset($r['category']) )
     494                $r['category'] = -1;
     495        if ( !isset($r['category_name']) )
     496                $r['category_name'] = '';
     497        if ( !isset($r['hide_invisible']) )
     498                $r['hide_invisible'] = 1;
     499        if ( !isset($r['show_updated']) )
     500                $r['show_updated'] = 0;
     501
     502        $exclusions = '';
     503        if ( !empty($r['exclude']) ) {
     504                $exlinks = preg_split('/[\s,]+/',$r['exclude']);
     505                if ( count($exlinks) ) {
     506                        foreach ( $exlinks as $exlink ) {
     507                                $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
     508                        }
     509                }
     510        }
     511
     512        extract($r);
     513
     514        if ( ! empty($category_name) ) {
     515                if ( $cat_id = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE cat_name='$category_name' LIMIT 1") )
     516                        $category = $cat_id;
     517        }
     518
     519        $category_query = '';
     520        $join = '';
     521        if ( $category != -1 && !empty($category) ) {
     522                $join = " LEFT JOIN $wpdb->link2cat ON ($wpdb->links.link_id = $wpdb->link2cat.link_id) ";
     523
     524        $category_query = " AND category_id = $category ";
     525        }
     526
     527        if (get_settings('links_recently_updated_time')) {
     528                $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_settings('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
     529        } else {
     530                $recently_updated_test = '';
     531        }
     532
     533        if ($show_updated) {
     534                $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
     535        }
     536
     537        $orderby = strtolower($r['orderby']);
     538        $length = '';
     539        switch ($orderby) {
     540                case 'length':
     541                        $length = ", CHAR_LENGTH(link_name) AS length";
     542                        break;
     543                case 'rand':
     544                        $orderby = 'rand()';
     545                        break;
     546                default:
     547                        $orderby = "link_" . $orderby;
     548        }
     549
     550        if ( 'link_id' == $orderby )
     551                $orderby = "$wpdb->links.link_id";
     552
     553        $visible = '';
     554        if ( $hide_invisible )
     555                $visible = "AND link_visible = 'Y'";
     556
     557        $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
     558        $query .= " ORDER BY $orderby $order";
     559        if ($limit != -1)
     560                $query .= " LIMIT $limit";
     561
     562        return $wpdb->get_results($query);
     563}
    565564?>
Note: See TracChangeset for help on using the changeset viewer.