Plugin Directory

Changeset 1410982


Ignore:
Timestamp:
05/05/2016 12:43:58 PM (10 years ago)
Author:
upcast
Message:

Added search, tested for WP 4.5.1

Location:
upcast/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • upcast/trunk/settings.php

    r1175654 r1410982  
    11<?php
    2 define('UPCAST_FIELD_NAMES','thumbnail,date,title,author,subject,category,description,link,url,image_link,image_url,thumbnail_url,content,duration,size');
     2define('UPCAST_FIELD_NAMES','thumbnail,date,title,author,subject,category,description,link,url,image_link,image_url,thumbnail_url,content,duration,size,search');
    33define('UPCAST_HIDE_FIELD_NAMES','url,image_link,image_url,thumbnail_url');
    44define('UPCAST_FILTER_FIELD_NAMES','author,subject,category');
     
    200200
    201201function preg_match_simple($pattern, $subject) {
    202     return preg_match('/'.$pattern.'/', $subject);
     202    return preg_match('/'.$pattern.'/i', $subject);
    203203}
    204204
     
    293293                </div>
    294294                <div class="upcast-settings-header-text">
    295                     <h2>Shortcode Settings</h2><br>
     295                    <h2>Shortcode Settings</h2>
    296296                    <p>Add a podcast feed to any post or page by including the text <code>[upcast]</code></p>
     297                    <p>Include a search bar with the search attribute: <code>[upcast search="true"]</code></p>
     298                    <p>Display the feed's main image or thumbnail by including <code>[upcast_image]</code></p>
    297299                </div>
    298300            </div>
  • upcast/trunk/style.css

    r1175653 r1410982  
    22
    33input[type="text"].upcast { width: 150px }
     4input[type="text"].upcast.upcast_search {
     5    width : 100px;
     6    padding : 2px;
     7    background : white;
     8}
     9select.upcast.upcast_search {
     10    padding : 2px;
     11}
     12input[type="submit"].upcast.upcast_search {
     13    border-radius: 5px;
     14    display: inline-block;
     15    padding: 3px;
     16    text-decoration: none;
     17    height: inherit;
     18    text-transform: none;
     19    margin: 0px;
     20    background: none;
     21    background-color: #CCCCCC;
     22    border: inherit;
     23    box-shadow: none;
     24    color: #F7F7F7;
     25    text-shadow: 1px 1px 1px #000000;
     26}
     27.upcast.upcast_box {
     28    float: left;
     29    text-align: center;
     30    margin: 0px 15px 15px 0px;
     31}
     32.upcast.upcast_box img {
     33    border: 5px #CCCCCC solid;
     34}
     35.upcast.upcast_box h2 > a:hover {
     36    color: #333333;
     37}
     38.upcast.upcast_box img:hover {
     39    border: 5px #333333 solid;
     40}
     41
    442textarea.upcast { width: 100% }
    543input[type="text"].upcast-template { width : 300px }
  • upcast/trunk/upcast.php

    r1204863 r1410982  
    4444function upcast_rss($atts, $content = '') {
    4545    return upcast_feed($atts, $content);
     46}
     47
     48function upcast_group($atts, $content = '') {
     49    return upcast_feed($atts, $content, 'group');
    4650}
    4751
     
    128132    $default_columns = '';
    129133    $default_column_names = '';
     134   
    130135    foreach (explode(',', UPCAST_FIELD_NAMES) as $field) {
    131136        if (isset($options['column_'.$field]) && $options['column_'.$field] == 'on') {
     
    142147        $default_column_names = implode(',', array_map('ucwords', explode(',', UPCAST_DEFAULT_COLUMNS)));
    143148    }
    144        
     149   
     150    if (isset($_GET['group']))
     151        $options['group'] = $_GET['group'];
     152       
     153    $filters = array('title','desc','description','pubdate','date','category','subject','author');
     154    if (isset($_GET['search'])) {
     155        $options['search'] = true;
     156    }
     157    $search = isset($options['search']) && $options['search'];
     158    foreach ($filters as $f) {
     159        if (isset($_GET[$f])) {
     160            $options['filter_'.$f] = $_GET[$f];
     161            $search = false;
     162            //error_log('option '.$f.' = '.$options[$f]);
     163        }
     164    }
     165   
    145166    extract(shortcode_atts(Array(
    146167        'feed' => isset($options['rss_link']) ? $options['rss_link'] : false,
     
    155176        'headers' => $default_column_names,
    156177        'table' => true,
    157         'template' => isset($options['template_row']) ? $options['template_row'] : '',
     178        'template' => isset($options['template_row']) ? $options['template_row'] : '', 
     179        'search' => isset($options['search']) ? $options['search'] : false,     
     180        'group' => isset($options['filter_group']) ? $options['filter_group'] : false,
    158181        'title' => isset($options['filter_title']) ? $options['filter_title'] : false,
    159         'description' => isset($options['filter_desc']) ? $options['filter_desc'] : false,
     182        'description' => isset($options['filter_desc']) ? $options['filter_desc'] : (isset($options['filter_description']) ? $options['filter_description'] : false),
    160183        'pubdate' => isset($options['filter_pubdate']) ? $options['filter_pubdate'] : false,
    161184        'date' => isset($options['filter_date']) ? $options['filter_date'] : false,
     
    186209    $display_columns = explode(',',$columns);
    187210    $display_column_headers = explode(',',$headers);
     211    //error_log('display_columns='.count($display_columns));
     212    //error_log('display_column_headers='.count($display_column_headers));
     213    if ($search) {
     214        $search_columns = array();
     215        $search_column_headers = array();
     216        for ($c = 0; $c < count($display_columns); $c++) {
     217            if (in_array($display_columns[$c], $filters)) {
     218                $search_columns[] = $display_columns[$c];
     219                $search_column_headers[] = $display_column_headers[$c];
     220            }
     221        }
     222        $search_columns[] = 'search';
     223        $search_column_headers[] = '&nbsp;';
     224    }
     225    //error_log('search_columns='.count($search_columns));
    188226   
    189227    # If no feed, return with nothing
     
    219257    //error_log('rss_items='.count($rss_items));
    220258    $rsscount = 0;
    221        
     259   
     260    if ($search)
     261        $search_content = '<td>['.implode(']</td><td>[',$search_columns).']</td>';
     262   
     263    $custom = false;
    222264    if ($content == '') {
    223265        if ($template) {
    224266            $content = $template;
     267            $custom = true;
    225268        } else {
    226269            $content = '<td>['.implode(']</td><td>[',$display_columns).']</td>';
    227270            // Substitute any individual column templates
    228             foreach ($display_columns as $field)
    229                 if (isset($options['template_'.$field]) && $options['template_'.$field])
     271            foreach ($display_columns as $field) {
     272                if (isset($options['template_'.$field]) && $options['template_'.$field]) {
    230273                    $content = str_replace('['.$field.']', $options['template_'.$field], $content);
    231         }
     274                }
     275            }
     276        }
     277    } else {
     278        $custom = true;
    232279    }
    233280   
     
    235282    $matches = array();
    236283    $thead = '';
     284    $table = !$custom;
    237285    if (preg_match('/\s*<table>\s*(<thead>.*<\/thead>)?\s*(<tbody>)?\s*(<tr>)?\s*(.*?)\s*(<\/tr>)?\s*(<\/tbody>)?\s*<\/table>\s*/mis', $content, $matches)) {
    238286        //error_log('matches='.print_r($matches, true));
     
    241289        }
    242290        $content = $matches[4];
    243     }
     291        $table = true;
     292    }
     293   
     294    if ($search) {
     295        $output .= '<form name="upcast_search" action="">';
     296    }   
    244297   
    245298    if ($table)
    246299        $content = '<tr>'.$content.'</tr>';
    247300   
     301    $search_content = '<tr>'.$search_content.'</tr>';
     302   
    248303    // Display the headers
     304    $table_head = '';
    249305    if ($table) {
    250         $output .= '<table>';
     306        $table_head .= '<table>';
    251307        if ($thead) {
    252             $output .= $thead;
     308            $table_head .= $thead;
    253309        } elseif ($header) {
    254             $output .= '<thead><tr>';
     310            $table_head .= '<thead><tr>';
    255311            foreach ($display_column_headers as $header) {
    256                 $output .= '<th>'.$header.'</th>';
    257             }
    258             $output .= '</tr></thead>';
    259         }
    260         $output .= '<tbody>';
    261     }
     312                $table_head .= '<th>'.$header.'</th>';
     313            }
     314            $table_head .= '</tr></thead>';
     315        }
     316        $table_head .= '<tbody>';
     317    }
     318    if ($search) {
     319        $search_head = '<table><thead><tr>';
     320        foreach ($search_column_headers as $header) {
     321            $search_head .= '<th>'.$header.'</th>';
     322        }
     323        $search_head .= '</tr></thead><tbody>';
     324       
     325    }   
    262326   
    263327    if ($offset > 0) {
     
    267331    // Pre-find all the substitutions and locations in the content to be repeated for each row
    268332    $matches = array();
     333    $search_matches = array();
    269334    preg_match_all('/[\[{]('.str_replace(',','|',UPCAST_FIELD_NAMES).')[\]}]/i', $content, $matches, PREG_OFFSET_CAPTURE);
     335    if ($search) {
     336        //error_log('search_content='.$search_content);
     337        preg_match_all('/[\[{]('.str_replace(',','|',UPCAST_FIELD_NAMES).')[\]}]/i', $search_content, $search_matches, PREG_OFFSET_CAPTURE);
     338        //error_log("search_matches[1]=".print_r($search_matches[1],true));
     339    }
    270340//  error_log('content='.print_r($content, true));
    271341//  error_log(print_r($matches, true));
     342    $searches = array();
     343    $table_body = '';
    272344    foreach ($rss_items as $item)
    273345    {
     
    298370        if ($selected && $title) $selected = preg_match_simple($title,$item->get_title());
    299371        if ($selected && $subject && is_array($item_subject) && count($item_subject)) $selected = preg_match_simple($subject,$item_subject[0]['data']);     
    300         if ($dates) {
    301             if ($selected && $pubdate) $selected = preg_match_simple($pubdate,$item->get_gmdate($dates));
    302             if ($selected && $date) $selected = preg_match_simple($date,$item->get_local_date($dates));
    303         }
    304         else {
    305             if ($selected && $pubdate) $selected = preg_match_simple($pubdate,$item->get_gmdate());
    306             if ($selected && $date) $selected = preg_match_simple($date,$item->get_local_date());
    307         }
    308         if ($selected && $author && $item_author) $selected = preg_match_simple($author,$item_author->get_name());
     372        if ($selected && $pubdate) $selected = preg_match_simple($pubdate,$item->get_date('Y'));
     373        if ($selected && $date) $selected = preg_match_simple($date,$item->get_local_date('%G'));
     374        if ($selected && $author && $item_author) {
     375            $selected = preg_match_simple($author,$item_author->get_name());
     376        }
    309377                               
    310378        if ($selected)
     
    316384            if (isset($matches[1])) {
    317385                $row = $content;
     386                $search_val = '';
    318387                // work through in reverse so substitutions do not affect earlier offsets
    319388                for ($f = count($matches[1]) - 1; $f >= 0; $f--) {
    320389                    $text = '&nbsp;';
    321                     switch ($matches[1][$f][0]) {
     390                    $col = $matches[1][$f][0];
     391                    $search_val = '';
     392                    switch ($col) {
    322393                        case 'count':
    323394                            $text = $rsscount;
    324395                            break;
    325396                        case 'category':
    326                             if ($item_category) $text = $item_category->get_label();
     397                            if ($item_category) {
     398                                $text = $item_category->get_label();
     399                                $search_val = $text;
     400                            }                           
    327401                            break;
    328402                        case 'subject':
    329                             if (is_array($item_subject) && count($item_subject)) $text = $item_subject[0]['data'];
     403                            if (is_array($item_subject) && count($item_subject)) {
     404                                $text = $item_subject[0]['data'];
     405                                $search_val = $text;
     406                            }
    330407                            break;
    331408                        case 'description':
    332                             $text = $item->get_description();
     409                            $text = $item->get_description();                           
    333410                            break;
    334411                        case 'title':                       
     
    338415                            try {
    339416                                $t = date_create_from_format('Y-m-d G:i:s', $item->get_local_date('%G-%m-%d %T'));                         
    340                                 if ($t !== FALSE)
     417                                if ($t !== FALSE) {
    341418                                    $text = str_replace(' ', '&nbsp;', $t->format($dates));
     419                                    $search_val = $t->format('Y');
     420                                    //error_log("search_val=".$search_val);
     421                                }
    342422                            } catch (Exception $e) {
    343423                                error_log($e->getMessage());
     
    348428                            try {
    349429                                $t = date_create_from_format('Y-m-d G:i:s', $item->get_date('Y-m-d G:i:s'));                           
    350                                 if ($t !== FALSE)
     430                                if ($t !== FALSE) {
    351431                                    $text = str_replace(' ', '&nbsp;', $t->format($dates));
     432                                    $search_val = $t->format('Y');
     433                                    //error_log("search_val=".$search_val);                                 
     434                                }
    352435                            } catch (Exception $e) {
    353436                                error_log($e->getMessage());
     
    356439                            break;
    357440                        case 'author':
    358                             if ($item_author && $item_author->get_name() != $rss_author_name) $text = $item_author->get_name();
     441                            if ($item_author && $item_author->get_name() != $rss_author_name) {
     442                                $text = $item_author->get_name();
     443                                $search_val = $text;
     444                            }
    359445                            break;
    360446                        case 'url':
     
    414500                            break;     
    415501                    }
    416                     $row = substr_replace($row, $text, $matches[1][$f][1] - 1, strlen($matches[1][$f][0]) + 2);
     502                    if ($search) {
     503                        if ($search_val) {
     504                            if (!array_key_exists($col, $searches))
     505                                $searches[$col] = array('');
     506                            if (!in_array($search_val, $searches[$col]))
     507                                $searches[$col][] = $search_val;
     508                        }
     509                    }
     510                    //error_log('subbing "'.$col.'" as "'.$text.'"');
     511                    $row = substr_replace($row, $text, $matches[1][$f][1] - 1, strlen($col) + 2);
    417512                }
    418                 $output .= $row;
     513                $table_body .= $row;
    419514            } else {
    420                 $output .= $content;
    421             }
    422         }
    423     }
     515                $table_body .= $content;
     516            }
     517        }
     518    }
     519
     520    if ($search) {
     521        $output .= $search_head;
     522        //error_log('search='.print_r($searches,true));
     523        if (isset($search_matches[1])) {
     524            $row = $search_content;
     525            for ($f = count($search_matches[1]) - 1; $f >= 0; $f--) {
     526                $col = $search_matches[1][$f][0];
     527                //error_log('col='.$col);
     528                if (in_array($col, $filters)) {
     529                    if (array_key_exists($col, $searches)) {
     530                        sort($searches[$col], SORT_FLAG_CASE);
     531                        $text = '<select class="upcast upcast_search" name="'.$col.'">';
     532                        foreach ($searches[$col] as $opt) {
     533                            $text .= '<option value="'.$opt.'" '.((isset($_GET[$col]) && $_GET[$col] == $opt) ? 'SELECTED' : '').'>'.htmlspecialchars($opt).'</option>';
     534                        }
     535                        $text .= '</select>';
     536                    } else {
     537                        $text = '<input name="'.$col.'" placeholder="search text" class="upcast upcast_search" type="text" '.((isset($_GET[$col]) && $_GET[$col]) ? 'VALUE='.$_GET[$col] : '').'>';
     538                    }
     539                    $row = substr_replace($row, $text, $search_matches[1][$f][1] - 1, strlen($col) + 2);               
     540                } elseif ($col == 'search') {
     541                    $row = substr_replace($row, '<input class="upcast upcast_search" name="search" type="submit" value="Go">', $search_matches[1][$f][1] - 1, strlen($col) + 2);               
     542                } else {
     543                    $text = '&nbsp;';
     544                }
     545            }
     546            $output .= $row;
     547        } else {
     548            $output .= $content;           
     549        }
     550        $output .= '</tbody></table></form>';   
     551    } elseif ($table && !$rsscount) {
     552        $table_body .= '<td span="'.count($display_column_headers).'">No items found</td>';
     553    }
     554   
     555    if ($table)
     556        $output .= $table_head;
     557       
     558    $output .= $table_body;
    424559   
    425560    if ($table)
     
    441576add_shortcode('upcast_image', 'upcast_image');
    442577add_shortcode('upcast_thumbnail', 'upcast_thumbnail');
     578add_shortcode('upcast_group', 'upcast_group');
    443579function upcast_filter($content) {
    444580    $block = join("|",array("upcast","upcast_rss"));
     
    448584}
    449585add_filter("the_content", "upcast_filter");
    450 // register Foo_Widget widget
    451 //wp_register_style('upcastStylesheet', plugins_url('style.css', __FILE__) );
    452 //function enqueue_upcast_styles() {
    453 //   wp_enqueue_style('upcastStylesheet');
    454 //}
     586
    455587function register_upcast_widget() {
    456588    register_widget( 'UpCast_Widget' );
     
    459591//add_action( 'wp_enqueue_styles', 'enqueue_upcast_styles');
    460592
     593//wp_register_style('upcastStylesheet', plugins_url('style.css', __FILE__) );
     594
    461595?>
Note: See TracChangeset for help on using the changeset viewer.