WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/15/2010 11:48:24 PM (11 years ago)
Author:
nacin
Message:

Next pass on internal linking. Functional, but needs styling. props koopersmith, see #11420.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/js/tinymce/wp-mce-link-includes.php

    r16159 r16393  
    11<?php
     2// args expects optionally 'pagenum' and 's'
     3function wp_link_query( $args = array() ) {
     4    $pts = get_post_types( array( 'publicly_queryable' => true ), 'objects' );
     5    $pt_names = array_keys( $pts );
    26   
    3 class WP_Tab_Bar {
    4     var $tabs = array();
    5    
    6     var $id = '';
    7     var $classes = array();
    8    
    9     var $selected = '';
    10    
    11     function add( $id, $label, $url='' ) {
    12         array_push( $this->tabs, array(
    13             'label' => $label,
    14             'for' => $id,
    15             'url' => $url
    16         ));
    17     }
    18    
    19     function select( $id ) {
    20         $this->selected = $id;
    21     }
    22    
    23     function render( $echo=true ) {
    24         if ( empty( $this->selected ) )
    25             $this->selected = $this->tabs[0]['for'];
    26 
    27         array_unshift( $this->classes, 'wp-tab-bar' );
    28        
    29         $out = "<ul id='$this->id' class='" . esc_attr( implode( ' ', $this->classes ) ) . "'>";
    30         foreach( $this->tabs as $tab ) {
    31             if ( !isset($tab['url']) )
    32                 $tab['url'] = '';
    33 
    34             $out.= "<li class='";
    35             $out.= $this->selected == $tab['for'] ? 'wp-tab-active' : '';
    36             $out.= "'><input type='hidden' class='wp-tab-for-id' value='{$tab['for']}' />";
    37             $out.= "<a href='{$tab['url']}#{$tab['for']}'>";
    38             $out.= "{$tab['label']}</a></li>";
    39         }
    40         $out.= "</ul>";
    41 
    42         if ( $echo )
    43             echo $out;
    44 
    45         return $out;
    46     }
    47 }
    48 
    49 function wp_link_panel_custom() { ?>
    50     <div id="link-panel-id-custom" class="link-panel link-panel-custom link-panel-active">
    51         <input type="hidden" class="link-panel-type" value="custom" />
    52         <label>
    53             <span><?php _e('URL:'); ?></span><input class="url-field" type="text" />
    54         </label>
    55     </div>
    56 <?php }
    57 
    58 function wp_link_panel_structure( $panel_type, $name, $queries ) {
    59     $id = $panel_type . '-' . $name;
    60    
    61     ?>
    62     <div id="link-panel-id-<?php echo $id; ?>" class="link-panel link-panel-<?php echo $panel_type; ?>">
    63         <!-- <input type="hidden" class="link-panel-type" value="<?php echo $panel_type; ?>" /> -->
    64         <!-- <input type="hidden" class="link-panel-id" value="" /> -->
    65         <?php
    66        
    67         $tb = new WP_Tab_Bar();
    68         foreach( $queries as $i => $query ) {
    69             $queries[$i]['id'] = "$id-{$query['preset']}";
    70             $tb->add( $queries[$i]['id'], $query['label'] );
    71         }
    72         $tb->render();
    73 
    74         foreach( $queries as $query ): ?>
    75             <div id="<?php echo $query['id']; ?>" class="wp-tab-panel">
    76                 <input type="hidden" class="wp-tab-panel-query" value="<?php echo $query['preset']; ?>" />
    77                
    78             <?php if ( 'search' == $query['preset'] ): ?>
    79                 <label for="<?php echo $id; ?>-search-field" class="link-search-wrapper">
    80                     <span><?php _e('Search:'); ?></span>
    81                     <input type="text" id="<?php echo $id; ?>-search-field" class="link-search-field" />
    82                     <img class="waiting" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
    83                 </label>
    84                 <ul id="<?php echo $id; ?>-search-results" class="link-search-results"></ul>
    85                
    86             <?php else: ?>
    87                 <div class="wp-tab-panel-pagelinks wp-tab-panel-pagelinks-top"></div>
    88                 <ul>
    89                     <li class="wp-tab-panel-loading unselectable"><em><?php _e('Loading...'); ?></em></li>
    90                 </ul>
    91                 <div class="wp-tab-panel-pagelinks wp-tab-panel-pagelinks-bottom"></div>
    92             <?php endif; ?>
    93             </div>
    94         <?php endforeach; ?>
    95     </div>
    96 <?php }
    97 
    98 function wp_link_query_post_type( $pt_obj, $preset='all', $opts=array() ) {
    99     $args_base = array(
    100         'post_type' => $pt_obj->name,
     7    $query = array(
     8        'post_type' => $pt_names,
    1019        'suppress_filters' => true,
    10210        'update_post_term_cache' => false,
    10311        'update_post_meta_cache' => false,
    10412        'post_status' => 'publish',
     13        'order' => 'DESC',
     14        'orderby' => 'post_date',
     15        'posts_per_page' => 20,
    10516    );
    106    
    107     switch( $preset ) {
    108     case 'all':
    109         $args = array_merge( $args_base, array(
    110             'order' => 'ASC',
    111             'orderby' => 'title',
    112             'posts_per_page' => 20,
    113         ));
    114         break;
    115     case 'recent':
    116         $args = array_merge( $args_base, array(
    117             'order' => 'DESC',
    118             'orderby' => 'post_date',
    119             'posts_per_page' => 15,
    120         ));
    121         break;
    122     case 'search':
    123         $args = array_merge( $args_base, array(
    124             's' => isset($opts['search']) ? $opts['search'] : '',
    125             'posts_per_page' => 10
    126         ));
    127         break;
    128     }
    129    
    130     // Handle pages if a page number is specified.
    131     if ( isset( $opts['pagenum'] ) && isset( $args['posts_per_page'] ) ) {
    132         $pages = array(
    133             'current' => $opts['pagenum'],
    134             'per_page' => $args['posts_per_page']
    135         );
    136        
    137         if ( ! isset( $args['offset'] ) )
    138             $args['offset'] = 0 < $opts['pagenum'] ? $args['posts_per_page'] * ( $opts['pagenum'] - 1 ) : 0;
    139         $pages['offset'] = $args['offset'];
    140     }
    141    
    142     // Allow args to be extended.
    143     if ( isset( $opts['args'] ) )
    144         $args = array_merge( $args, $opts['args'] );
    145    
     17
     18    $args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1;
     19
     20    if ( isset( $args['s'] ) )
     21        $query['s'] = $args['s'];
     22
     23    $pagination = array(
     24        'current'  => $args['pagenum'],
     25        'per_page' => $query['posts_per_page'],
     26    );
     27
     28    $query['offset'] = $pagination['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;
     29
    14630    // Do main query.
    14731    $get_posts = new WP_Query;
    148     $posts = $get_posts->query( $args );
     32    $posts = $get_posts->query( $query );
    14933    // Check if any posts were found.
    15034    if ( ! $get_posts->post_count )
     
    15438    $results = array();
    15539    foreach ( $posts as $post ) {
     40        if ( 'post' == $post->post_type )
     41            $info = mysql2date( __( 'Y/m/d' ), $post->post_date );
     42        else
     43            $info = $pts[ $post->post_type ]->labels->singular_name;
     44       
    15645        $results[] = array(
    15746            'ID' => $post->ID,
    158             'title' => $post->post_title,
    159             'permalink' => get_permalink( $post->ID )
    160         );
     47            'title' => esc_html( strip_tags($post->post_title) ),
     48            'permalink' => get_permalink( $post->ID ),
     49            'info' => $info,
     50        );;
    16151    }
     52
     53    // Remaining pagination values.
     54    $pagination['max'] = $get_posts->max_num_pages;
     55    $pagination['page_links'] = paginate_links( array(
     56        'prev_text' => __('&laquo;'),
     57        'next_text' => __('&raquo;'),
     58        'total' => $pagination['max'],
     59        'current' => $pagination['current']
     60    ) );
     61
    16262    // Build response.
    16363    $resp = array(
    16464        'query' => $get_posts,
    16565        'objects' => $posts,
    166         'results' => $results
     66        'results' => $results,
     67        'pages' => $pagination,
    16768    );
    168    
    169     // Set remaining pages values.
    170     if ( isset( $pages ) ) {
    171         $pages['max'] = $resp['query']->max_num_pages;
    172         $pages['page_links'] = paginate_links( array(
    173             'prev_text' => __('&laquo;'),
    174             'next_text' => __('&raquo;'),
    175             'total' => $pages['max'],
    176             'current' => $pages['current']
    177         ));
    178         $resp['pages'] = $pages;
    179     }
    180    
     69
    18170    return $resp;
    18271}
    18372
    18473function wp_link_ajax( $request ) {
    185     if ( !isset($request['type']) || !isset($request['name']) || !isset($request['preset']) )
    186         die('-1');
     74    // Searches have a title term.
     75    if ( isset( $request['title'] ) )
     76        $args['s'] = stripslashes( $request['title'] );
     77    $args['pagenum'] = ! empty( $request['page'] ) ? absint( $request['page'] ) : 1;
    18778
    188     // Run only presets we recognize.
    189     if ( 'pt' != $request['type'] || ! in_array( $request['preset'], array('all','search','recent') ) )
    190         die('-1');
    191     // Searches must have a search term.
    192     else if ( 'search' == $request['preset'] && !isset($request['title']) )
    193         die('-1');
    194 
    195     $opts = array();
    196     if ( 'search' == $request['preset'] ) {
    197         $opts['search'] = $request['title'];
    198     } else if ( ! empty( $request['page'] ) ) {
    199         $opts['pagenum'] = $request['page'];
    200     }
    201        
    202     if ( 'pt' == $request['type'] && $obj = get_post_type_object($request['name']) )
    203         $resp = wp_link_query_post_type( $obj, $request['preset'], $opts );
     79    $resp = wp_link_query( $args );
    20480
    20581    if ( ! isset( $resp ) )
    206         die('0');
     82        die( '0' );
    20783
    20884    $json = array( 'results' => $resp['results'] );
     
    21389    echo "\n";
    21490}
     91
    21592?>
Note: See TracChangeset for help on using the changeset viewer.