WordPress.org

Plugin Directory

Changeset 469928


Ignore:
Timestamp:
12/01/11 16:37:40 (2 years ago)
Author:
csixty4
Message:

Cleaned up code formatting

Location:
daves-wordpress-live-search/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • daves-wordpress-live-search/trunk/DWLSTransients.php

    r350219 r469928  
    1616 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR  
    1717 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
    18  **/ 
     18 * */ 
    1919 
    2020/** 
     
    2222 */ 
    2323class DWLSTransients { 
    24     static function clear() { 
    25         $a = 255; 
    26         while($a > -1) { 
    27             $prefix = dechex($a); 
    28             $index = get_transient("dwls_index_{$prefix}"); 
    29             if($index) { 
    30                 foreach($index as $hash=>$value) { 
    31                     delete_transient("dwls_result_{$hash}"); 
    32                 } 
    33                 delete_transient("dwls_index_{$prefix}"); 
    34             } 
    35             $a -= 1; 
    36         } 
    37     } 
    38      
    39     static function set($key, $value, $expiration) { 
    40         $hash = md5($key); 
    41         $prefix = substr($hash, 0, 2); 
    42         $index = get_transient("dwls_index_{$prefix}"); 
    43         if(!$index) { 
    44             $index = array(); 
    45         } 
    46         if(!array_key_exists($hash, $index)) { 
    47             $index[$hash] = $hash; 
    48             set_transient("dwls_index_{$prefix}", $index); 
    49         } 
    50         set_transient("dwls_result_{$hash}", $value); 
    51     } 
    52      
    53     static function get($key) { 
    54         $hash = md5($key); 
    55         $prefix = substr($hash, 0, 2); 
    56         $index = get_transient("dwls_index_{$prefix}"); 
    57         if(!$index) { 
    58             $index = array(); 
    59         } 
    60         if(array_key_exists($hash, $index)) { 
    61             return get_transient("dwls_result_{$hash}");     
    62         } 
    63          
    64         return FALSE; 
    65     } 
    66      
    67     static function indexes() { 
    68         $all_indexes = array(); 
    69          
    70         $a = 255; 
    71         while($a > -1) { 
    72             $prefix = dechex($a); 
    73             $index = get_transient("dwls_index_{$prefix}"); 
    74             if($index) { 
    75                 $all_indexes[] = "dwls_index_{$prefix}"; 
    76             } 
    77             $a -= 1; 
    78         } 
    79         return array_reverse($all_indexes); 
    80     } 
     24 
     25  static function clear() { 
     26    $a = 255; 
     27    while ($a > -1) { 
     28      $prefix = dechex($a); 
     29      $index = get_transient("dwls_index_{$prefix}"); 
     30      if ($index) { 
     31        foreach ($index as $hash => $value) { 
     32          delete_transient("dwls_result_{$hash}"); 
     33        } 
     34        delete_transient("dwls_index_{$prefix}"); 
     35      } 
     36      $a -= 1; 
     37    } 
     38  } 
     39 
     40  static function set($key, $value, $expiration) { 
     41    $hash = md5($key); 
     42    $prefix = substr($hash, 0, 2); 
     43    $index = get_transient("dwls_index_{$prefix}"); 
     44    if (!$index) { 
     45      $index = array(); 
     46    } 
     47    if (!array_key_exists($hash, $index)) { 
     48      $index[$hash] = $hash; 
     49      set_transient("dwls_index_{$prefix}", $index); 
     50    } 
     51    set_transient("dwls_result_{$hash}", $value); 
     52  } 
     53 
     54  static function get($key) { 
     55    $hash = md5($key); 
     56    $prefix = substr($hash, 0, 2); 
     57    $index = get_transient("dwls_index_{$prefix}"); 
     58    if (!$index) { 
     59      $index = array(); 
     60    } 
     61    if (array_key_exists($hash, $index)) { 
     62      return get_transient("dwls_result_{$hash}"); 
     63    } 
     64 
     65    return FALSE; 
     66  } 
     67 
     68  static function indexes() { 
     69    $all_indexes = array(); 
     70 
     71    $a = 255; 
     72    while ($a > -1) { 
     73      $prefix = dechex($a); 
     74      $index = get_transient("dwls_index_{$prefix}"); 
     75      if ($index) { 
     76        $all_indexes[] = "dwls_index_{$prefix}"; 
     77      } 
     78      $a -= 1; 
     79    } 
     80    return array_reverse($all_indexes); 
     81  } 
     82 
    8183} 
  • daves-wordpress-live-search/trunk/DavesWordPressLiveSearch.php

    r469927 r469928  
    1818 * */ 
    1919class DavesWordPressLiveSearch { 
    20     /////////////////// 
    21     // Initialization 
    22     /////////////////// 
    23  
    24     /** 
    25      * Initialize the live search object & enqueuing scripts 
    26      * @return void 
    27      */ 
    28     public static function advanced_search_init() { 
    29         if (self::isSearchablePage()) { 
    30             wp_enqueue_script('jquery'); 
    31              
    32             wp_enqueue_script('daves-wordpress-live-search', plugin_dir_url(__FILE__).'js/daves-wordpress-live-search.js', 'jquery'); 
    33             wp_enqueue_script('excanvas', plugin_dir_url(__FILE__).'js/excanvas.compiled.js', 'jquery'); 
    34  
    35             wp_enqueue_script('spinners', plugin_dir_url(__FILE__).'js/spinners.min.js', 'explorercanvas'); 
    36  
    37             self::inlineSettings(); 
     20  /////////////////// 
     21  // Initialization 
     22  /////////////////// 
     23 
     24  /** 
     25   * Initialize the live search object & enqueuing scripts 
     26   * @return void 
     27   */ 
     28  public static function advanced_search_init() { 
     29    if (self::isSearchablePage()) { 
     30      wp_enqueue_script('jquery'); 
     31 
     32      wp_enqueue_script('daves-wordpress-live-search', plugin_dir_url(__FILE__) . 'js/daves-wordpress-live-search.js', 'jquery'); 
     33      wp_enqueue_script('excanvas', plugin_dir_url(__FILE__) . 'js/excanvas.compiled.js', 'jquery'); 
     34 
     35      wp_enqueue_script('spinners', plugin_dir_url(__FILE__) . 'js/spinners.min.js', 'explorercanvas'); 
     36 
     37      self::inlineSettings(); 
     38    } 
     39 
     40    load_plugin_textdomain('dwls', false, dirname(plugin_basename(__FILE__)) . '/languages/'); 
     41  } 
     42 
     43  public static function head() { 
     44 
     45    if (self::isSearchablePage()) { 
     46      $cssOption = get_option('daves-wordpress-live-search_css_option'); 
     47 
     48      $themeDir = get_bloginfo("stylesheet_directory"); 
     49 
     50      switch ($cssOption) { 
     51        case 'theme': 
     52          $style = $themeDir . '/daves-wordpress-live-search.css'; 
     53          break; 
     54          break; 
     55        case 'default_red': 
     56        case 'default_blue': 
     57        case 'default_gray': 
     58          $style = plugin_dir_url(__FILE__) . 'css/daves-wordpress-live-search_' . $cssOption . '.css'; 
     59          break; 
     60        case 'notheme': 
     61        default: 
     62          $style = false; 
     63      } 
     64 
     65      if ($style) { 
     66        if (function_exists('wp_register_style') && function_exists('wp_enqueue_style')) { 
     67          // WordPress >= 2.6 
     68          wp_register_style('daves-wordpress-live-search', $style); 
     69          wp_enqueue_style('daves-wordpress-live-search'); 
     70          wp_print_styles(); 
     71        } else { 
     72          // WordPress < 2.6 
     73          echo('<link rel="stylesheet" href="' . $style . '" type="text/css" media="screen" />'); 
    3874        } 
    39          
    40         load_plugin_textdomain('dwls', false, dirname( plugin_basename( __FILE__ )) . '/languages/'); 
    41     } 
    42  
    43     public static function head() { 
    44  
    45         if (self::isSearchablePage()) { 
    46             $cssOption = get_option('daves-wordpress-live-search_css_option'); 
    47  
    48             $themeDir = get_bloginfo("stylesheet_directory"); 
    49  
    50             switch ($cssOption) { 
    51                 case 'theme': 
    52                     $style = $themeDir . '/daves-wordpress-live-search.css'; 
    53                     break; 
    54                     break; 
    55                 case 'default_red': 
    56                 case 'default_blue': 
    57                 case 'default_gray': 
    58                     $style = plugin_dir_url(__FILE__).'css/daves-wordpress-live-search_'.$cssOption.'.css'; 
    59                     break; 
    60                 case 'notheme': 
    61                 default: 
    62                     $style = false; 
    63             } 
    64  
    65             if ($style) { 
    66                 if (function_exists('wp_register_style') && function_exists('wp_enqueue_style')) { 
    67                     // WordPress >= 2.6 
    68                     wp_register_style('daves-wordpress-live-search', $style); 
    69                     wp_enqueue_style('daves-wordpress-live-search'); 
    70                     wp_print_styles(); 
    71                 } else { 
    72                     // WordPress < 2.6 
    73                     echo('<link rel="stylesheet" href="' . $style . '" type="text/css" media="screen" />'); 
    74                 } 
    75             } 
    76         } 
    77     } 
    78  
    79     private static function inlineSettings() { 
    80         global $wps_subdomains; 
    81  
    82         $resultsDirection = stripslashes(get_option('daves-wordpress-live-search_results_direction')); 
    83         $showThumbs = intval(("true" == get_option('daves-wordpress-live-search_thumbs'))); 
    84         $showExcerpt = intval(("true" == get_option('daves-wordpress-live-search_excerpt'))); 
    85         $showMoreResultsLink = intval(("true" == get_option('daves-wordpress-live-search_more_results', true))); 
    86         $minCharsToSearch = intval(get_option('daves-wordpress-live-search_minchars')); 
    87         $xOffset = intval(get_option('daves-wordpress-live-search_xoffset')); 
    88     $yOffset = intval(get_option('daves-wordpress-live-search_yoffset')); 
    89  
    90         $indicatorURL = plugin_dir_url(__FILE__).'indicator.gif'; 
    91         $indicatorWidth = getimagesize(dirname(__FILE__) . "/indicator.gif"); 
    92         $indicatorWidth = $indicatorWidth[0]; 
    93  
    94         // Translations 
    95         $moreResultsText = __( 'View more results', 'dwls' ); 
    96         $outdatedJQueryText = __( "Dave's WordPress Live Search requires jQuery 1.2.6 or higher. WordPress ships with current jQuery versions. But if you are seeing this message, it's likely that another plugin is including an earlier version.", 'dwls' ); 
    97  
    98         // Neat trick: use wp_localize_script to generate the config object 
    99         // "This way, you won’t have to use PHP to print out JavaScript code, 
    100         // which is both ugly and non-cacheable." 
    101         // @see http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/#js-global 
    102         wp_localize_script( 'daves-wordpress-live-search', 'DavesWordPressLiveSearchConfig', array( 
    103             'resultsDirection' => $resultsDirection, 
    104             'showThumbs' => ($showThumbs == 1) ? 'true' : 'false', 
    105             'showExcerpt' => ($showExcerpt == 1) ? 'true' : 'false', 
    106             'showMoreResultsLink' => ($showMoreResultsLink == 1) ? 'true' : 'false', 
    107             'minCharsToSearch' => $minCharsToSearch,         
    108             'xOffset' => $xOffset, 
    109         'yOffset' => $yOffset, 
    110  
    111             'blogURL' => get_bloginfo('url'), 
    112             'ajaxURL' => admin_url('admin-ajax.php'), 
    113             'indicatorURL' => $indicatorURL, 
    114             'indicatorWidth' => $indicatorWidth, 
    115              
    116             'viewMoreText' => $moreResultsText, 
    117             'outdatedJQuery' => $outdatedJQueryText, 
    118             )); 
    119              
    120     } 
    121  
    122     /////////////// 
    123     // Admin Pages 
    124     /////////////// 
    125  
    126     /** 
    127      * Include the Live Search options page in the admin menu 
    128      * @return void 
    129      */ 
    130     public static function admin_menu() { 
    131         add_options_page("Dave's WordPress Live Search Options", __('Live Search', 'dwls'), 'manage_options', __FILE__, array('DavesWordPressLiveSearch', 'plugin_options')); 
    132     } 
    133  
    134     /** 
    135      * Display & process the Live Search admin options 
    136      * @return void 
    137      */ 
    138     public static function plugin_options() { 
    139         $tab = $_REQUEST['tab']; 
    140         switch ($tab) { 
    141             case 'advanced': 
    142                 return self::plugin_options_advanced(); 
    143             case 'debug': 
    144                 return self::plugin_options_debug(); 
    145             case 'settings': 
    146             default: 
    147                 return self::plugin_options_settings(); 
    148         } 
    149     } 
    150  
    151     private static function plugin_options_settings() { 
    152         $thisPluginsDirectory = dirname(__FILE__); 
    153         $enableDebugger = (bool) get_option('daves-wordpress-live-search_debug'); 
    154  
    155         if (array_key_exists('daves-wordpress-live-search_submit', $_POST) && current_user_can('manage_options')) { 
    156             check_admin_referer('daves-wordpress-live-search-config'); 
    157  
    158             // Read their posted value 
    159             $maxResults = max(intval($_POST['daves-wordpress-live-search_max_results']), 0); 
    160             $resultsDirection = $_POST['daves-wordpress-live-search_results_direction']; 
    161             $displayPostMeta = ("true" == $_POST['daves-wordpress-live-search_display_post_meta']); 
    162             $cssOption = $_POST['daves-wordpress-live-search_css']; 
    163             $showThumbs = $_POST['daves-wordpress-live-search_thumbs']; 
    164             $showExcerpt = $_POST['daves-wordpress-live-search_excerpt']; 
    165             $excerptLength = $_POST['daves-wordpress-live-search_excerpt_length']; 
    166             $showMoreResultsLink = $_POST['daves-wordpress-live-search_more_results']; 
    167             $minCharsToSearch = intval($_POST['daves-wordpress-live-search_minchars']); 
    168             $searchSource = intval($_POST['daves-wordpress-live-search_source']); 
    169  
    170             // Save the posted value in the database 
    171             update_option('daves-wordpress-live-search_max_results', $maxResults); 
    172             update_option('daves-wordpress-live-search_results_direction', $resultsDirection); 
    173             update_option('daves-wordpress-live-search_display_post_meta', (string) $displayPostMeta); 
    174             update_option('daves-wordpress-live-search_css_option', $cssOption); 
    175             update_option('daves-wordpress-live-search_thumbs', $showThumbs); 
    176             update_option('daves-wordpress-live-search_excerpt', $showExcerpt); 
    177             update_option('daves-wordpress-live-search_excerpt_length', $excerptLength); 
    178             update_option('daves-wordpress-live-search_more_results', $showMoreResultsLink); 
    179             update_option('daves-wordpress-live-search_minchars', $minCharsToSearch); 
    180             update_option('daves-wordpress-live-search_source', $searchSource); 
    181  
    182             // Translate the "Options saved" message...just in case. 
    183             // You know...the code I was copying for this does it, thought it might be a good idea to leave it 
    184             $updateMessage = __('Options saved.', 'dwls'); 
    185  
    186             echo "<div class=\"updated fade\"><p><strong>$updateMessage</strong></p></div>"; 
    187         } else { 
    188             $maxResults = intval(get_option('daves-wordpress-live-search_max_results')); 
    189             $resultsDirection = stripslashes(get_option('daves-wordpress-live-search_results_direction')); 
    190             $displayPostMeta = (bool) get_option('daves-wordpress-live-search_display_post_meta'); 
    191             $cssOption = get_option('daves-wordpress-live-search_css_option'); 
    192             $showThumbs = (bool) get_option('daves-wordpress-live-search_thumbs'); 
    193             $showExcerpt = (bool) get_option('daves-wordpress-live-search_excerpt'); 
    194             $excerptLength = intval(get_option('daves-wordpress-live-search_excerpt_length')); 
    195             $showMoreResultsLink = intval("true" == get_option('daves-wordpress-live-search_more_results')); 
    196             $minCharsToSearch = intval(get_option('daves-wordpress-live-search_minchars')); 
    197             $searchSource = intval(get_option('daves-wordpress-live-search_source')); 
    198         } 
    199  
    200         include("$thisPluginsDirectory/admin/daves-wordpress-live-search-admin.tpl"); 
    201     } 
    202  
    203     private static function plugin_options_advanced() { 
    204         $thisPluginsDirectory = dirname(__FILE__); 
    205         if (array_key_exists('daves-wordpress-live-search_submit', $_POST) && current_user_can('manage_options')) { 
    206             check_admin_referer('daves-wordpress-live-search-config'); 
    207  
    208             // Read their posted value 
    209             $xOffset = intval($_POST['daves-wordpress-live-search_xoffset']); 
    210         $yOffset = intval($_POST['daves-wordpress-live-search_yoffset']); 
    211             $exceptions = $_POST['daves-wordpress-live-search_exceptions']; 
    212             $cacheLifetime = $_POST['daves-wordpress-live-search_cache_lifetime']; 
    213             if ("" == trim($cacheLifetime)) { 
    214                 $cacheLifetime = 3600; 
    215             } 
    216             $enableDebugger = ("true" == $_POST['daves-wordpress-live-search_debug']); 
    217  
    218             update_option('daves-wordpress-live-search_exceptions', $exceptions); 
    219             update_option('daves-wordpress-live-search_xoffset', intval($xOffset)); 
    220         update_option('daves-wordpress-live-search_yoffset', intval($yOffset)); 
    221             update_option('daves-wordpress-live-search_cache_lifetime', intval($cacheLifetime)); 
    222             update_option('daves-wordpress-live-search_debug', $enableDebugger); 
    223  
    224             // Translate the "Options saved" message...just in case. 
    225             // You know...the code I was copying for this does it, thought it might be a good idea to leave it 
    226             $updateMessage = __('Options saved.', 'dwls'); 
    227  
    228             echo "<div class=\"updated fade\"><p><strong>$updateMessage</strong></p></div>"; 
    229         } else { 
    230             if (array_key_exists('daves-wordpress-live-search_submit', $_POST) && "Clear Cache" == $_POST['daves-wordpress-live-search_submit'] && current_user_can('manage_options')) { 
    231                 // Clear the cache 
    232                 DWLSTransients::clear(); 
    233                 $clearedMessage = __('Cache cleared.', 'mt_trans_domain'); 
    234  
    235                 echo "<div class=\"updated fade\"><p><strong>$clearedMessage</strong></p></div>"; 
    236             } 
    237  
    238             $exceptions = get_option('daves-wordpress-live-search_exceptions'); 
    239             $xOffset = intval(get_option('daves-wordpress-live-search_xoffset')); 
    240             $yOffset = intval(get_option('daves-wordpress-live-search_yoffset')); 
    241             $cacheLifetime = get_option('daves-wordpress-live-search_cache_lifetime'); 
    242             if ("" == trim($cacheLifetime)) { 
    243                 $cacheLifetime = 3600; 
    244             } else { 
    245                 $cacheLifetime = intval($cacheLifetime); 
    246             } 
    247  
    248             $enableDebugger = (bool) get_option('daves-wordpress-live-search_debug'); 
    249         } 
    250  
    251         include("$thisPluginsDirectory/admin/daves-wordpress-live-search-admin-advanced.tpl"); 
    252     } 
    253  
    254     private static function plugin_options_debug() { 
    255         $thisPluginsDirectory = dirname(__FILE__); 
    256         $enableDebugger = (bool) get_option('daves-wordpress-live-search_debug'); 
    257  
    258         $debug_output = array(); 
    259  
    260         $debug_output[] = "Cache contents:"; 
    261         $cache_indexes = DWLSTransients::indexes(); 
    262  
    263         // Output the cache indexes 
    264         foreach ($cache_indexes as $cache_index) { 
    265             $debug_output[] = $cache_index; 
    266         } 
    267  
    268         $debug_output[] = "----------"; 
    269  
    270         foreach ($cache_indexes as $cache_index) { 
    271             $debug_output[] = "{$cache_index}:"; 
    272             $hashes = get_transient($cache_index); 
    273             foreach ($hashes as $hash) { 
    274                 $debug_output[] = $hash; 
    275             } 
    276         } 
    277  
    278         $debug_output[] = "----------"; 
    279  
    280         // Output the cache contents for each index 
    281         foreach ($cache_indexes as $cache_index) { 
    282             $hashes = get_transient($cache_index); 
    283             foreach ($hashes as $hash) { 
    284                 $contents = get_transient("dwls_result_{$hash}"); 
    285                 $debug_output[] = "dwls_result_{$hash}:"; 
    286                 $debug_output[] = var_export($contents, TRUE); 
    287             } 
    288         } 
    289  
    290         $debug_output = implode("<br><br>", $debug_output); 
    291  
    292         include("$thisPluginsDirectory/admin/daves-wordpress-live-search-admin-debug.tpl"); 
    293     } 
    294  
    295     public static function admin_notices() { 
    296         $cssOption = get_option('daves-wordpress-live-search_css_option'); 
    297         if ('theme' == $cssOption) { 
    298             $themeDir = get_theme_root() . '/' . get_stylesheet(); 
    299  
    300             // Make sure there's a daves-wordpress-live-search.css file in the theme 
    301             if (!file_exists($themeDir . "/daves-wordpress-live-search.css")) { 
    302                 $alertMessage = sprintf(__("The %sDave's WordPress Live Search%s plugin is configured to use a theme-specific CSS file, but the current theme does not contain a daves-wordpress-live-search.css file."), '<em>', '</em>'); 
    303                 echo "<div class=\"updated fade\"><p><strong>$alertMessage</strong></p></div>"; 
    304             } 
    305         } 
    306     } 
    307  
    308     private static function isSearchablePage() { 
    309         if (is_admin ()) 
    310             return false; 
    311  
    312         $searchable = true; 
    313         $exceptions = explode("\n", get_option('daves-wordpress-live-search_exceptions')); 
    314  
    315         foreach ($exceptions as $exception) { 
    316  
    317             $regexp = trim($exception); 
    318  
    319             // Blank paths were slipping through. Ignore them. 
    320             if (empty($regexp)) { 
    321                 continue; 
    322             } 
    323  
    324             if ('<front>' == $regexp) { 
    325                 $regexp = ''; 
    326             } 
    327  
    328             // These checks can probably be turned into regexps themselves, 
    329             // but it's too early in the morning to be writing regexps 
    330             if ('*' == substr($regexp, 0, 1)) { 
    331                 $regexp = substr($regexp, 1); 
    332             } else { 
    333                 $regexp = '^' . $regexp; 
    334             } 
    335  
    336             if ('*' == substr($regexp, -1)) { 
    337                 $regexp = substr($regexp, 0, -1); 
    338             } else { 
    339                 $regexp = $regexp . '$'; 
    340             } 
    341  
    342             $regexp = '|' . $regexp . '|'; 
    343             if (preg_match($regexp, substr($_SERVER['REQUEST_URI'], 1)) > 0) { 
    344                 return false; 
    345             } 
    346         } 
    347  
    348         // Fall-through, search everything by default 
    349         return true; 
    350     } 
    351  
    352     /** 
    353      * Set some decent defaults 
    354      */ 
    355     public static function activate() { 
    356        
    357       add_option('daves-wordpress-live-search_max_results', 10); 
    358       add_option('daves-wordpress-live-search_results_direction', 'down'); 
    359       add_option('daves-wordpress-live-search_display_post_meta', 'true'); 
    360       add_option('daves-wordpress-live-search_css_option', 'default_gray'); 
    361       add_option('daves-wordpress-live-search_thumbs', 'true'); 
    362       add_option('daves-wordpress-live-search_excerpt', 'true'); 
    363       add_option('daves-wordpress-live-search_excerpt_length', 100); 
    364       add_option('daves-wordpress-live-search_more_results', 'true'); 
    365       add_option('daves-wordpress-live-search_minchars', 3); 
    366       add_option('daves-wordpress-live-search_source', DavesWordPressLiveSearchResults::SEARCH_CONTENT); 
    367        
    368     } 
     75      } 
     76    } 
     77  } 
     78 
     79  private static function inlineSettings() { 
     80    global $wps_subdomains; 
     81 
     82    $resultsDirection = stripslashes(get_option('daves-wordpress-live-search_results_direction')); 
     83    $showThumbs = intval(("true" == get_option('daves-wordpress-live-search_thumbs'))); 
     84    $showExcerpt = intval(("true" == get_option('daves-wordpress-live-search_excerpt'))); 
     85    $showMoreResultsLink = intval(("true" == get_option('daves-wordpress-live-search_more_results', true))); 
     86    $minCharsToSearch = intval(get_option('daves-wordpress-live-search_minchars')); 
     87    $xOffset = intval(get_option('daves-wordpress-live-search_xoffset')); 
     88    $yOffset = intval(get_option('daves-wordpress-live-search_yoffset')); 
     89 
     90    $indicatorURL = plugin_dir_url(__FILE__) . 'indicator.gif'; 
     91    $indicatorWidth = getimagesize(dirname(__FILE__) . "/indicator.gif"); 
     92    $indicatorWidth = $indicatorWidth[0]; 
     93 
     94    // Translations 
     95    $moreResultsText = __('View more results', 'dwls'); 
     96    $outdatedJQueryText = __("Dave's WordPress Live Search requires jQuery 1.2.6 or higher. WordPress ships with current jQuery versions. But if you are seeing this message, it's likely that another plugin is including an earlier version.", 'dwls'); 
     97 
     98    // Neat trick: use wp_localize_script to generate the config object 
     99    // "This way, you won’t have to use PHP to print out JavaScript code, 
     100    // which is both ugly and non-cacheable." 
     101    // @see http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/#js-global 
     102    wp_localize_script('daves-wordpress-live-search', 'DavesWordPressLiveSearchConfig', array( 
     103        'resultsDirection' => $resultsDirection, 
     104        'showThumbs' => ($showThumbs == 1) ? 'true' : 'false', 
     105        'showExcerpt' => ($showExcerpt == 1) ? 'true' : 'false', 
     106        'showMoreResultsLink' => ($showMoreResultsLink == 1) ? 'true' : 'false', 
     107        'minCharsToSearch' => $minCharsToSearch, 
     108        'xOffset' => $xOffset, 
     109        'yOffset' => $yOffset, 
     110        'blogURL' => get_bloginfo('url'), 
     111        'ajaxURL' => admin_url('admin-ajax.php'), 
     112        'indicatorURL' => $indicatorURL, 
     113        'indicatorWidth' => $indicatorWidth, 
     114        'viewMoreText' => $moreResultsText, 
     115        'outdatedJQuery' => $outdatedJQueryText, 
     116    )); 
     117  } 
     118 
     119  /////////////// 
     120  // Admin Pages 
     121  /////////////// 
     122 
     123  /** 
     124   * Include the Live Search options page in the admin menu 
     125   * @return void 
     126   */ 
     127  public static function admin_menu() { 
     128    add_options_page("Dave's WordPress Live Search Options", __('Live Search', 'dwls'), 'manage_options', __FILE__, array('DavesWordPressLiveSearch', 'plugin_options')); 
     129  } 
     130 
     131  /** 
     132   * Display & process the Live Search admin options 
     133   * @return void 
     134   */ 
     135  public static function plugin_options() { 
     136    $tab = $_REQUEST['tab']; 
     137    switch ($tab) { 
     138      case 'advanced': 
     139        return self::plugin_options_advanced(); 
     140      case 'debug': 
     141        return self::plugin_options_debug(); 
     142      case 'settings': 
     143      default: 
     144        return self::plugin_options_settings(); 
     145    } 
     146  } 
     147 
     148  private static function plugin_options_settings() { 
     149    $thisPluginsDirectory = dirname(__FILE__); 
     150    $enableDebugger = (bool) get_option('daves-wordpress-live-search_debug'); 
     151 
     152    if (array_key_exists('daves-wordpress-live-search_submit', $_POST) && current_user_can('manage_options')) { 
     153      check_admin_referer('daves-wordpress-live-search-config'); 
     154 
     155      // Read their posted value 
     156      $maxResults = max(intval($_POST['daves-wordpress-live-search_max_results']), 0); 
     157      $resultsDirection = $_POST['daves-wordpress-live-search_results_direction']; 
     158      $displayPostMeta = ("true" == $_POST['daves-wordpress-live-search_display_post_meta']); 
     159      $cssOption = $_POST['daves-wordpress-live-search_css']; 
     160      $showThumbs = $_POST['daves-wordpress-live-search_thumbs']; 
     161      $showExcerpt = $_POST['daves-wordpress-live-search_excerpt']; 
     162      $excerptLength = $_POST['daves-wordpress-live-search_excerpt_length']; 
     163      $showMoreResultsLink = $_POST['daves-wordpress-live-search_more_results']; 
     164      $minCharsToSearch = intval($_POST['daves-wordpress-live-search_minchars']); 
     165      $searchSource = intval($_POST['daves-wordpress-live-search_source']); 
     166 
     167      // Save the posted value in the database 
     168      update_option('daves-wordpress-live-search_max_results', $maxResults); 
     169      update_option('daves-wordpress-live-search_results_direction', $resultsDirection); 
     170      update_option('daves-wordpress-live-search_display_post_meta', (string) $displayPostMeta); 
     171      update_option('daves-wordpress-live-search_css_option', $cssOption); 
     172      update_option('daves-wordpress-live-search_thumbs', $showThumbs); 
     173      update_option('daves-wordpress-live-search_excerpt', $showExcerpt); 
     174      update_option('daves-wordpress-live-search_excerpt_length', $excerptLength); 
     175      update_option('daves-wordpress-live-search_more_results', $showMoreResultsLink); 
     176      update_option('daves-wordpress-live-search_minchars', $minCharsToSearch); 
     177      update_option('daves-wordpress-live-search_source', $searchSource); 
     178 
     179      // Translate the "Options saved" message...just in case. 
     180      // You know...the code I was copying for this does it, thought it might be a good idea to leave it 
     181      $updateMessage = __('Options saved.', 'dwls'); 
     182 
     183      echo "<div class=\"updated fade\"><p><strong>$updateMessage</strong></p></div>"; 
     184    } else { 
     185      $maxResults = intval(get_option('daves-wordpress-live-search_max_results')); 
     186      $resultsDirection = stripslashes(get_option('daves-wordpress-live-search_results_direction')); 
     187      $displayPostMeta = (bool) get_option('daves-wordpress-live-search_display_post_meta'); 
     188      $cssOption = get_option('daves-wordpress-live-search_css_option'); 
     189      $showThumbs = (bool) get_option('daves-wordpress-live-search_thumbs'); 
     190      $showExcerpt = (bool) get_option('daves-wordpress-live-search_excerpt'); 
     191      $excerptLength = intval(get_option('daves-wordpress-live-search_excerpt_length')); 
     192      $showMoreResultsLink = intval("true" == get_option('daves-wordpress-live-search_more_results')); 
     193      $minCharsToSearch = intval(get_option('daves-wordpress-live-search_minchars')); 
     194      $searchSource = intval(get_option('daves-wordpress-live-search_source')); 
     195    } 
     196 
     197    include("$thisPluginsDirectory/admin/daves-wordpress-live-search-admin.tpl"); 
     198  } 
     199 
     200  private static function plugin_options_advanced() { 
     201    $thisPluginsDirectory = dirname(__FILE__); 
     202    if (array_key_exists('daves-wordpress-live-search_submit', $_POST) && current_user_can('manage_options')) { 
     203      check_admin_referer('daves-wordpress-live-search-config'); 
     204 
     205      // Read their posted value 
     206      $xOffset = intval($_POST['daves-wordpress-live-search_xoffset']); 
     207      $yOffset = intval($_POST['daves-wordpress-live-search_yoffset']); 
     208      $exceptions = $_POST['daves-wordpress-live-search_exceptions']; 
     209      $cacheLifetime = $_POST['daves-wordpress-live-search_cache_lifetime']; 
     210      if ("" == trim($cacheLifetime)) { 
     211        $cacheLifetime = 3600; 
     212      } 
     213      $enableDebugger = ("true" == $_POST['daves-wordpress-live-search_debug']); 
     214 
     215      update_option('daves-wordpress-live-search_exceptions', $exceptions); 
     216      update_option('daves-wordpress-live-search_xoffset', intval($xOffset)); 
     217      update_option('daves-wordpress-live-search_yoffset', intval($yOffset)); 
     218      update_option('daves-wordpress-live-search_cache_lifetime', intval($cacheLifetime)); 
     219      update_option('daves-wordpress-live-search_debug', $enableDebugger); 
     220 
     221      // Translate the "Options saved" message...just in case. 
     222      // You know...the code I was copying for this does it, thought it might be a good idea to leave it 
     223      $updateMessage = __('Options saved.', 'dwls'); 
     224 
     225      echo "<div class=\"updated fade\"><p><strong>$updateMessage</strong></p></div>"; 
     226    } else { 
     227      if (array_key_exists('daves-wordpress-live-search_submit', $_POST) && "Clear Cache" == $_POST['daves-wordpress-live-search_submit'] && current_user_can('manage_options')) { 
     228        // Clear the cache 
     229        DWLSTransients::clear(); 
     230        $clearedMessage = __('Cache cleared.', 'mt_trans_domain'); 
     231 
     232        echo "<div class=\"updated fade\"><p><strong>$clearedMessage</strong></p></div>"; 
     233      } 
     234 
     235      $exceptions = get_option('daves-wordpress-live-search_exceptions'); 
     236      $xOffset = intval(get_option('daves-wordpress-live-search_xoffset')); 
     237      $yOffset = intval(get_option('daves-wordpress-live-search_yoffset')); 
     238      $cacheLifetime = get_option('daves-wordpress-live-search_cache_lifetime'); 
     239      if ("" == trim($cacheLifetime)) { 
     240        $cacheLifetime = 3600; 
     241      } else { 
     242        $cacheLifetime = intval($cacheLifetime); 
     243      } 
     244 
     245      $enableDebugger = (bool) get_option('daves-wordpress-live-search_debug'); 
     246    } 
     247 
     248    include("$thisPluginsDirectory/admin/daves-wordpress-live-search-admin-advanced.tpl"); 
     249  } 
     250 
     251  private static function plugin_options_debug() { 
     252    $thisPluginsDirectory = dirname(__FILE__); 
     253    $enableDebugger = (bool) get_option('daves-wordpress-live-search_debug'); 
     254 
     255    $debug_output = array(); 
     256 
     257    $debug_output[] = "Cache contents:"; 
     258    $cache_indexes = DWLSTransients::indexes(); 
     259 
     260    // Output the cache indexes 
     261    foreach ($cache_indexes as $cache_index) { 
     262      $debug_output[] = $cache_index; 
     263    } 
     264 
     265    $debug_output[] = "----------"; 
     266 
     267    foreach ($cache_indexes as $cache_index) { 
     268      $debug_output[] = "{$cache_index}:"; 
     269      $hashes = get_transient($cache_index); 
     270      foreach ($hashes as $hash) { 
     271        $debug_output[] = $hash; 
     272      } 
     273    } 
     274 
     275    $debug_output[] = "----------"; 
     276 
     277    // Output the cache contents for each index 
     278    foreach ($cache_indexes as $cache_index) { 
     279      $hashes = get_transient($cache_index); 
     280      foreach ($hashes as $hash) { 
     281        $contents = get_transient("dwls_result_{$hash}"); 
     282        $debug_output[] = "dwls_result_{$hash}:"; 
     283        $debug_output[] = var_export($contents, TRUE); 
     284      } 
     285    } 
     286 
     287    $debug_output = implode("<br><br>", $debug_output); 
     288 
     289    include("$thisPluginsDirectory/admin/daves-wordpress-live-search-admin-debug.tpl"); 
     290  } 
     291 
     292  public static function admin_notices() { 
     293    $cssOption = get_option('daves-wordpress-live-search_css_option'); 
     294    if ('theme' == $cssOption) { 
     295      $themeDir = get_theme_root() . '/' . get_stylesheet(); 
     296 
     297      // Make sure there's a daves-wordpress-live-search.css file in the theme 
     298      if (!file_exists($themeDir . "/daves-wordpress-live-search.css")) { 
     299        $alertMessage = sprintf(__("The %sDave's WordPress Live Search%s plugin is configured to use a theme-specific CSS file, but the current theme does not contain a daves-wordpress-live-search.css file."), '<em>', '</em>'); 
     300        echo "<div class=\"updated fade\"><p><strong>$alertMessage</strong></p></div>"; 
     301      } 
     302    } 
     303  } 
     304 
     305  private static function isSearchablePage() { 
     306    if (is_admin()) 
     307      return false; 
     308 
     309    $searchable = true; 
     310    $exceptions = explode("\n", get_option('daves-wordpress-live-search_exceptions')); 
     311 
     312    foreach ($exceptions as $exception) { 
     313 
     314      $regexp = trim($exception); 
     315 
     316      // Blank paths were slipping through. Ignore them. 
     317      if (empty($regexp)) { 
     318        continue; 
     319      } 
     320 
     321      if ('<front>' == $regexp) { 
     322        $regexp = ''; 
     323      } 
     324 
     325      // These checks can probably be turned into regexps themselves, 
     326      // but it's too early in the morning to be writing regexps 
     327      if ('*' == substr($regexp, 0, 1)) { 
     328        $regexp = substr($regexp, 1); 
     329      } else { 
     330        $regexp = '^' . $regexp; 
     331      } 
     332 
     333      if ('*' == substr($regexp, -1)) { 
     334        $regexp = substr($regexp, 0, -1); 
     335      } else { 
     336        $regexp = $regexp . '$'; 
     337      } 
     338 
     339      $regexp = '|' . $regexp . '|'; 
     340      if (preg_match($regexp, substr($_SERVER['REQUEST_URI'], 1)) > 0) { 
     341        return false; 
     342      } 
     343    } 
     344 
     345    // Fall-through, search everything by default 
     346    return true; 
     347  } 
     348 
     349  /** 
     350   * Set some decent defaults 
     351   */ 
     352  public static function activate() { 
     353 
     354    add_option('daves-wordpress-live-search_max_results', 10); 
     355    add_option('daves-wordpress-live-search_results_direction', 'down'); 
     356    add_option('daves-wordpress-live-search_display_post_meta', 'true'); 
     357    add_option('daves-wordpress-live-search_css_option', 'default_gray'); 
     358    add_option('daves-wordpress-live-search_thumbs', 'true'); 
     359    add_option('daves-wordpress-live-search_excerpt', 'true'); 
     360    add_option('daves-wordpress-live-search_excerpt_length', 100); 
     361    add_option('daves-wordpress-live-search_more_results', 'true'); 
     362    add_option('daves-wordpress-live-search_minchars', 3); 
     363    add_option('daves-wordpress-live-search_source', DavesWordPressLiveSearchResults::SEARCH_CONTENT); 
     364  } 
     365 
    369366} 
    370367 
     
    381378); 
    382379foreach ($dwls_update_hooks as $dwls_update_hook) { 
    383     add_action($dwls_update_hook, array("DWLSTransients", "clear")); 
     380  add_action($dwls_update_hook, array("DWLSTransients", "clear")); 
    384381} 
  • daves-wordpress-live-search/trunk/DavesWordPressLiveSearchResults.php

    r468383 r469928  
    77 */ 
    88class DavesWordPressLiveSearchResults { 
    9  
    10     // Search sources 
    11     const SEARCH_CONTENT = 0; 
    12     const SEARCH_WPCOMMERCE = 1; 
    13          
    14     public $searchTerms; 
    15     public $results; 
    16     public $displayPostMeta; 
    17      
    18     /** 
    19      * @param int $source search source constant 
    20      * @param string $searchTerms 
    21      * @param boolean $displayPostMeta Show author & date for each post. Defaults to TRUE to keep original bahavior from before I added this flag 
    22      */ 
    23     function DavesWordPressLiveSearchResults($source, $searchTerms, $displayPostMeta = true, $maxResults = -1) { 
    24          
    25         $this->results = array(); 
    26  
    27                 switch($source) { 
    28                     case self::SEARCH_CONTENT: 
    29                         $this->populate($searchTerms, $displayPostMeta, $maxResults); 
    30                         break; 
    31                     case self::SEARCH_WPCOMMERCE: 
    32                         $this->populateFromWPCommerce($searchTerms, $displayPostMeta, $maxResults); 
    33                         break; 
    34                     default: 
    35                         // Unrecognized 
    36                 } 
    37         $this->displayPostMeta = $displayPostMeta; 
    38     } 
    39      
    40     private function populate($wpQueryResults, $displayPostMeta, $maxResults) { 
    41          
    42         global $wp_locale; 
    43         global $wp_query; 
    44          
    45         $dateFormat = get_option('date_format'); 
    46          
    47         // Some other plugin threw a fit once if I didn't instantiate 
    48         // WP_Query once to initialize everything and then call it 
    49         // for real. Might have been "Search Everything". I think there's 
    50         // a comment about it in an old version of DWLS. 
    51         $wp_query = $wpQueryResults = new WP_Query(); 
    52         $wp_query = $wpQueryResults = new WP_Query(); 
    53                       
    54         if(function_exists(relevanssi_do_query)) { 
    55             // Relevanssi isn't treating 0 as "unlimited" results 
    56             // like WordPress's native search does. So we'll replace 
    57             // $maxResults with a really big number, the biggest one 
    58             // PHP knows how to represent, if $maxResults == -1 
    59             // (unlimited) 
    60             if(-1 == $maxResults) { 
    61                 $maxResults = PHP_INT_MAX; 
    62             } 
    63         } 
    64      
    65         $wpQueryParams = array( 
    66           's' => $_GET['s'], 
    67           'showposts' => $maxResults, 
    68           'post_type' => 'any', 
    69           'post_status' => 'publish', 
    70         ); 
    71  
    72         // WPML compatibility 
    73         // see http://wpml.org/documentation/support/creating-multilingual-wordpress-themes/search-form/ 
    74         if(isset($_GET['lang'])) { 
    75           $wpQueryParams['lang'] = $_GET['lang']; 
    76         } 
    77  
    78         $wpQueryResults->query($wpQueryParams); 
    79          
    80         $this->searchTerms = $wpQueryResults->query_vars['s']; 
    81          
    82         $wpQueryResults = apply_filters('dwls_alter_results', $wpQueryResults, $maxResults); 
    83          
    84         foreach($wpQueryResults->posts as $result) 
    85         //foreach($posts as $result) 
    86         { 
    87             // Add author names & permalinks 
    88             if($displayPostMeta) { 
    89                 $result->post_author_nicename = $this->authorName($result->post_author); 
    90             } 
    91                  
    92             $result->permalink = get_permalink($result->ID); 
    93              
    94             if(function_exists('get_post_image_id')) { 
    95                 // Support for WP 2.9 post thumbnails 
    96                 $postImageID = get_post_image_id($result->ID); 
    97                 $postImageData = wp_get_attachment_image_src($postImageID, apply_filters('post_image_size', 'thumbnail')); 
    98                  $result->attachment_thumbnail = $postImageData[0]; 
    99             } 
    100             else { 
    101                 // If no post thumbnail, grab the first image from the post 
    102                 $content = apply_filters('the_content', $result->post_content); 
    103                 $content = str_replace(']]>', ']]&gt;', $content); 
    104                 $result->attachment_thumbnail = $this->firstImg($content); 
    105             } 
    106  
    107             $result->attachment_thumbnail = apply_filters('dwls_attachment_thumbnail', $result->attachment_thumbnail); 
    108  
    109             $result->post_excerpt = $this->excerpt($result); 
    110              
    111             $result->post_date = date_i18n($dateFormat, strtotime($result->post_date)); 
    112             $result->post_date = apply_filters('dwls_post_date', $result->post_date); 
    113              
    114             // We don't want to send all this content to the browser 
    115             unset($result->post_content); 
    116  
    117             // xLocalization 
    118             $result->post_title = apply_filters("localization", $result->post_title);  
    119  
    120             $result->post_title = apply_filters('dwls_post_title', $result->post_title); 
    121              
    122                     $result->show_more = true; 
    123              
    124             $this->results[] = $result;  
    125         } 
    126     } 
    127  
    128         private function populateFromWPCommerce($wpQueryResults, $displayPostMeta, $maxResults) { 
    129             global $wpdb; 
    130  
    131             $this->searchTerms = $_GET['s']; 
    132  
    133             $tagQuery = "SELECT * FROM `{$wpdb->terms}` WHERE slug LIKE '%{$this->searchTerms}%'"; 
    134             $tagresults = $wpdb->get_results($tagQuery); 
    135  
    136             $metaQuery = "SELECT * FROM ".WPSC_TABLE_PRODUCTMETA." WHERE meta_value LIKE '%".$this->searchTerms."%'"; 
    137             $metaresults = $wpdb->get_results($metaQuery); 
    138  
    139             if($tagresults){ 
    140                   $term_id = $tagresults[0]->term_id; 
    141                    
    142                   $tagresults = $wpdb->get_results("SELECT * FROM `{$wpdb->term_taxonomy}` WHERE term_id = '".$term_id."' AND taxonomy='product_tag'"); 
    143                   $taxonomy_id = $tagresults[0]->term_taxonomy_id;   
    144                    
    145                   $tagresults = $wpdb->get_results("SELECT * FROM `{$wpdb->term_relationships}` WHERE term_taxonomy_id = '".$taxonomy_id."'"); 
    146                    
    147                   foreach ($tagresults as $result) { 
    148                     $product_ids[] = $result->object_id;  
    149                   } 
    150                    
    151                     $product_id = implode(",",$product_ids); 
    152                     $sql = "SELECT list.id,list.name,list.description, list.price,image.image,list.special,list.special_price 
    153                             FROM ".WPSC_TABLE_PRODUCT_LIST." AS list 
    154                             LEFT JOIN ".WPSC_TABLE_PRODUCT_IMAGES." AS image 
     9  // Search sources 
     10  const SEARCH_CONTENT = 0; 
     11  const SEARCH_WPCOMMERCE = 1; 
     12 
     13  public $searchTerms; 
     14  public $results; 
     15  public $displayPostMeta; 
     16 
     17  /** 
     18   * @param int $source search source constant 
     19   * @param string $searchTerms 
     20   * @param boolean $displayPostMeta Show author & date for each post. Defaults to TRUE to keep original bahavior from before I added this flag 
     21   */ 
     22  function DavesWordPressLiveSearchResults($source, $searchTerms, $displayPostMeta = true, $maxResults = -1) { 
     23 
     24    $this->results = array(); 
     25 
     26    switch ($source) { 
     27      case self::SEARCH_CONTENT: 
     28        $this->populate($searchTerms, $displayPostMeta, $maxResults); 
     29        break; 
     30      case self::SEARCH_WPCOMMERCE: 
     31        $this->populateFromWPCommerce($searchTerms, $displayPostMeta, $maxResults); 
     32        break; 
     33      default: 
     34      // Unrecognized 
     35    } 
     36    $this->displayPostMeta = $displayPostMeta; 
     37  } 
     38 
     39  private function populate($wpQueryResults, $displayPostMeta, $maxResults) { 
     40 
     41    global $wp_locale; 
     42    global $wp_query; 
     43 
     44    $dateFormat = get_option('date_format'); 
     45 
     46    // Some other plugin threw a fit once if I didn't instantiate 
     47    // WP_Query once to initialize everything and then call it 
     48    // for real. Might have been "Search Everything". I think there's 
     49    // a comment about it in an old version of DWLS. 
     50    $wp_query = $wpQueryResults = new WP_Query(); 
     51    $wp_query = $wpQueryResults = new WP_Query(); 
     52 
     53    if (function_exists(relevanssi_do_query)) { 
     54      // Relevanssi isn't treating 0 as "unlimited" results 
     55      // like WordPress's native search does. So we'll replace 
     56      // $maxResults with a really big number, the biggest one 
     57      // PHP knows how to represent, if $maxResults == -1 
     58      // (unlimited) 
     59      if (-1 == $maxResults) { 
     60        $maxResults = PHP_INT_MAX; 
     61      } 
     62    } 
     63 
     64    $wpQueryParams = array( 
     65        's' => $_GET['s'], 
     66        'showposts' => $maxResults, 
     67        'post_type' => 'any', 
     68        'post_status' => 'publish', 
     69    ); 
     70 
     71    // WPML compatibility 
     72    // see http://wpml.org/documentation/support/creating-multilingual-wordpress-themes/search-form/ 
     73    if (isset($_GET['lang'])) { 
     74      $wpQueryParams['lang'] = $_GET['lang']; 
     75    } 
     76 
     77    $wpQueryResults->query($wpQueryParams); 
     78 
     79    $this->searchTerms = $wpQueryResults->query_vars['s']; 
     80 
     81    $wpQueryResults = apply_filters('dwls_alter_results', $wpQueryResults, $maxResults); 
     82 
     83    foreach ($wpQueryResults->posts as $result) { 
     84    //foreach($posts as $result) 
     85      // Add author names & permalinks 
     86      if ($displayPostMeta) { 
     87        $result->post_author_nicename = $this->authorName($result->post_author); 
     88      } 
     89 
     90      $result->permalink = get_permalink($result->ID); 
     91 
     92      if (function_exists('get_post_image_id')) { 
     93        // Support for WP 2.9 post thumbnails 
     94        $postImageID = get_post_image_id($result->ID); 
     95        $postImageData = wp_get_attachment_image_src($postImageID, apply_filters('post_image_size', 'thumbnail')); 
     96        $result->attachment_thumbnail = $postImageData[0]; 
     97      } else { 
     98        // If no post thumbnail, grab the first image from the post 
     99        $content = apply_filters('the_content', $result->post_content); 
     100        $content = str_replace(']]>', ']]&gt;', $content); 
     101        $result->attachment_thumbnail = $this->firstImg($content); 
     102      } 
     103 
     104      $result->attachment_thumbnail = apply_filters('dwls_attachment_thumbnail', $result->attachment_thumbnail); 
     105 
     106      $result->post_excerpt = $this->excerpt($result); 
     107 
     108      $result->post_date = date_i18n($dateFormat, strtotime($result->post_date)); 
     109      $result->post_date = apply_filters('dwls_post_date', $result->post_date); 
     110 
     111      // We don't want to send all this content to the browser 
     112      unset($result->post_content); 
     113 
     114      // xLocalization 
     115      $result->post_title = apply_filters("localization", $result->post_title); 
     116 
     117      $result->post_title = apply_filters('dwls_post_title', $result->post_title); 
     118 
     119      $result->show_more = true; 
     120 
     121      $this->results[] = $result; 
     122    } 
     123  } 
     124 
     125  private function populateFromWPCommerce($wpQueryResults, $displayPostMeta, $maxResults) { 
     126    global $wpdb; 
     127 
     128    $this->searchTerms = $_GET['s']; 
     129 
     130    $tagQuery = "SELECT * FROM `{$wpdb->terms}` WHERE slug LIKE '%{$this->searchTerms}%'"; 
     131    $tagresults = $wpdb->get_results($tagQuery); 
     132 
     133    $metaQuery = "SELECT * FROM " . WPSC_TABLE_PRODUCTMETA . " WHERE meta_value LIKE '%" . $this->searchTerms . "%'"; 
     134    $metaresults = $wpdb->get_results($metaQuery); 
     135 
     136    if ($tagresults) { 
     137      $term_id = $tagresults[0]->term_id; 
     138 
     139      $tagresults = $wpdb->get_results("SELECT * FROM `{$wpdb->term_taxonomy}` WHERE term_id = '" . $term_id . "' AND taxonomy='product_tag'"); 
     140      $taxonomy_id = $tagresults[0]->term_taxonomy_id; 
     141 
     142      $tagresults = $wpdb->get_results("SELECT * FROM `{$wpdb->term_relationships}` WHERE term_taxonomy_id = '" . $taxonomy_id . "'"); 
     143 
     144      foreach ($tagresults as $result) { 
     145        $product_ids[] = $result->object_id; 
     146      } 
     147 
     148      $product_id = implode(",", $product_ids); 
     149      $sql = "SELECT list.id,list.name,list.description, list.price,image.image,list.special,list.special_price 
     150                            FROM " . WPSC_TABLE_PRODUCT_LIST . " AS list 
     151                            LEFT JOIN " . WPSC_TABLE_PRODUCT_IMAGES . " AS image 
    155152                            ON list.image=image.id 
    156                             WHERE list.id IN (".$product_id.")  
     153                            WHERE list.id IN (" . $product_id . ")  
    157154                            AND list.publish=1 
    158                             AND list.active=1";  
    159                              
    160                 $wp_query = new WP_Query(); 
    161                  
    162                 $wp_query->query(array('tag_slug__and' => $_GET['s'], 'showposts' => $maxResults)); 
    163                              
    164             } 
    165             elseif($metaresults){ 
    166                   foreach($metaresults as $result){ 
    167                            $mprod_id = $result->product_id; 
    168                       } 
    169                        
    170                      $sql = "SELECT list.id,list.name,list.description,list.price,image.image,list.special,list.special_price 
    171                             FROM ".WPSC_TABLE_PRODUCT_LIST." AS list 
    172                             LEFT JOIN ".WPSC_TABLE_PRODUCT_IMAGES." AS image 
     155                            AND list.active=1"; 
     156 
     157      $wp_query = new WP_Query(); 
     158 
     159      $wp_query->query(array('tag_slug__and' => $_GET['s'], 'showposts' => $maxResults)); 
     160    } elseif ($metaresults) { 
     161      foreach ($metaresults as $result) { 
     162        $mprod_id = $result->product_id; 
     163      } 
     164 
     165      $sql = "SELECT list.id,list.name,list.description,list.price,image.image,list.special,list.special_price 
     166                            FROM " . WPSC_TABLE_PRODUCT_LIST . " AS list 
     167                            LEFT JOIN " . WPSC_TABLE_PRODUCT_IMAGES . " AS image 
    173168                            ON list.image=image.id 
    174                             WHERE list.id IN (".$mprod_id.") OR (list.name LIKE '%".$this->searchTerms."%' OR list.description LIKE '%".$this->searchTerms."%') 
     169                            WHERE list.id IN (" . $mprod_id . ") OR (list.name LIKE '%" . $this->searchTerms . "%' OR list.description LIKE '%" . $this->searchTerms . "%') 
    175170                            AND list.publish=1 
    176                             AND list.active=1";   
    177                      
    178                 $wp_query = new WP_Query(); 
    179                  
    180                 $wp_query->query(array('s' => $_GET['s'], 'showposts' => $maxResults)); 
    181                              
    182             } 
    183             else { 
    184                   $sql="SELECT list.id,list.name,list.description,list.price,image.image,list.special,list.special_price 
    185                     FROM ".WPSC_TABLE_PRODUCT_LIST." AS list 
    186                     LEFT JOIN ".WPSC_TABLE_PRODUCT_IMAGES." AS image 
     171                            AND list.active=1"; 
     172 
     173      $wp_query = new WP_Query(); 
     174 
     175      $wp_query->query(array('s' => $_GET['s'], 'showposts' => $maxResults)); 
     176    } else { 
     177      $sql = "SELECT list.id,list.name,list.description,list.price,image.image,list.special,list.special_price 
     178                    FROM " . WPSC_TABLE_PRODUCT_LIST . " AS list 
     179                    LEFT JOIN " . WPSC_TABLE_PRODUCT_IMAGES . " AS image 
    187180                    ON list.image=image.id 
    188                     WHERE (list.name LIKE '%".$this->searchTerms."%' OR list.description LIKE '%".$this->searchTerms."%') 
     181                    WHERE (list.name LIKE '%" . $this->searchTerms . "%' OR list.description LIKE '%" . $this->searchTerms . "%') 
    189182                    AND list.publish=1 
    190183                    AND list.active=1 
    191184                   "; 
    192                     
    193                 $wp_query = new WP_Query(); 
    194                  
    195                 $wp_query->query(array('s' => $_GET['s'], 'showposts' => $maxResults)); 
    196                      
    197             } 
    198                    
    199             $results = $wpdb->get_results($sql, OBJECT); 
    200  
    201             foreach($results as $result) { 
    202                 $resultObj = new stdClass(); 
    203                 $resultObj->permalink = wpsc_product_url($result->id); 
    204                 $resultObj->post_title = apply_filters("localization", $result->name);  
    205                 $resultObj->post_content = $result->description; 
    206                 $resultObj->post_excerpt = $result->description; 
    207                 $resultObj->post_excerpt = $this->excerpt($resultObj); 
    208                  
    209                 $resultObj->post_price = $result->price; 
    210                 $resultObj->show_more = false; 
    211  
    212                 if(!empty($result->image)) { 
    213                     $resultObj->attachment_thumbnail = WPSC_THUMBNAIL_URL.$result->image; 
    214                 } 
    215  
    216                 // Fields that don't really apply here 
    217                 //$resultObj->post_date = 
    218                 //$resultObj->post_author_nicename = 
    219                  
    220                 $this->results[] = $resultObj; 
    221             } 
    222  
    223         } 
    224  
    225     private function excerpt($result) { 
    226          
    227         static $excerptLength = null; 
    228         // Only grab this value once 
    229         if(null == $excerptLength) { 
    230             $excerptLength = intval(get_option('daves-wordpress-live-search_excerpt_length')); 
    231         } 
    232         // Default value 
    233         if(0 == $excerptLength) { 
    234             $excerptLength = 100; 
    235         } 
    236          
    237         if (empty($result->post_excerpt)) { 
    238              $content = apply_filters("localization", $result->post_content); 
    239              $excerpt = explode(" ",strrev(substr(strip_tags($content), 0, $excerptLength)),2); 
    240              $excerpt = strrev($excerpt[1]); 
    241              $excerpt .= " [...]"; 
    242         } 
    243         else { 
    244             $excerpt = apply_filters("localization", $result->post_excerpt); 
    245         } 
    246  
    247         $excerpt = apply_filters('the_excerpt', $excerpt); 
    248         $excerpt = apply_filters('dwls_the_excerpt', $excerpt); 
    249          
    250         return $excerpt; 
    251     } 
    252      
    253     /** 
    254      * @return string 
    255      */ 
    256     private function authorName($authorID) { 
    257         static $authorCache = array(); 
    258          
    259         if(array_key_exists($authorID, $authorCache)) 
    260         { 
    261             $authorName = $authorCache[$authorID]; 
    262         } 
    263         else 
    264         { 
    265             $authorData = get_userdata($authorID); 
    266             $authorName = $authorData->display_name; 
    267             $authorCache[$authorID] = $authorName; 
    268         } 
    269  
    270         $authorName = apply_filters('dwls_author_name', $authorName); 
    271          
    272         return $authorName; 
    273     } 
    274  
    275     public function firstImg($post_content) { 
    276         $matches = array(); 
    277         $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post_content, $matches); 
    278         $first_img = $matches[1][0]; 
    279  
    280         if(empty($first_img)) { 
    281             return ''; 
    282         } 
    283         return $first_img; 
    284     } 
    285      
    286     public function ajaxSearch() { 
    287         $maxResults = intval(get_option('daves-wordpress-live-search_max_results')); 
    288         if($maxResults === 0) $maxResults = -1; 
    289          
    290         $cacheLifetime = intval(get_option('daves-wordpress-live-search_cache_lifetime')); 
    291         if(!is_user_logged_in() && 0 < $cacheLifetime) { 
    292             $doCache = TRUE; 
    293         } 
    294         else { 
    295             $doCache = FALSE; 
    296         } 
    297          
    298         if($doCache) { 
    299             $cachedResults = DWLSTransients::get($_REQUEST['s']); 
    300         } 
    301          
    302         if((!$doCache) || (FALSE === $cachedResults)) { 
    303          
    304             // Initialize the $wp global object 
    305             // See class WP in classes.php 
    306             // The Relevanssi plugin is using this instead of 
    307             // the global $wp_query object 
    308             $wp =& new WP(); 
    309             $wp->init();  // Sets up current user. 
    310             $wp->parse_request(); 
    311          
    312             $displayPostMeta = (bool)get_option('daves-wordpress-live-search_display_post_meta'); 
    313             if(array_key_exists('search_source', $_REQUEST)) { 
    314                 $searchSource = $_GET['search_source']; 
    315             } 
    316             else { 
    317                 $searchSource = intval(get_option('daves-wordpress-live-search_source')); 
    318             } 
    319          
    320             $results = new DavesWordPressLiveSearchResults($searchSource, $searchTerms, $displayPostMeta, $maxResults); 
    321          
    322             if($doCache) { 
    323                 DWLSTransients::set($_REQUEST['s'], $results, $cacheLifetime); 
    324             } 
    325         } 
    326         else { 
    327             // Found it in the cache. Return the results. 
    328             $results = $cachedResults; 
    329         } 
    330          
    331         $json = json_encode($results); 
    332      
    333         // If we don't output the text we want outputted here and 
    334         // then die(), the wp_ajax code will die(0) or die(-1) after 
    335         // this function returns and that value will get echoed out 
    336         // to the browser, resulting in a JSON parsing error. 
    337         die($json); 
    338     }    
     185 
     186      $wp_query = new WP_Query(); 
     187 
     188      $wp_query->query(array('s' => $_GET['s'], 'showposts' => $maxResults)); 
     189    } 
     190 
     191    $results = $wpdb->get_results($sql, OBJECT); 
     192 
     193    foreach ($results as $result) { 
     194      $resultObj = new stdClass(); 
     195      $resultObj->permalink = wpsc_product_url($result->id); 
     196      $resultObj->post_title = apply_filters("localization", $result->name); 
     197      $resultObj->post_content = $result->description; 
     198      $resultObj->post_excerpt = $result->description; 
     199      $resultObj->post_excerpt = $this->excerpt($resultObj); 
     200 
     201      $resultObj->post_price = $result->price; 
     202      $resultObj->show_more = false; 
     203 
     204      if (!empty($result->image)) { 
     205        $resultObj->attachment_thumbnail = WPSC_THUMBNAIL_URL . $result->image; 
     206      } 
     207 
     208      // Fields that don't really apply here 
     209      //$resultObj->post_date = 
     210      //$resultObj->post_author_nicename = 
     211 
     212      $this->results[] = $resultObj; 
     213    } 
     214  } 
     215 
     216  private function excerpt($result) { 
     217 
     218    static $excerptLength = null; 
     219    // Only grab this value once 
     220    if (null == $excerptLength) { 
     221      $excerptLength = intval(get_option('daves-wordpress-live-search_excerpt_length')); 
     222    } 
     223    // Default value 
     224    if (0 == $excerptLength) { 
     225      $excerptLength = 100; 
     226    } 
     227 
     228    if (empty($result->post_excerpt)) { 
     229      $content = apply_filters("localization", $result->post_content); 
     230      $excerpt = explode(" ", strrev(substr(strip_tags($content), 0, $excerptLength)), 2); 
     231      $excerpt = strrev($excerpt[1]); 
     232      $excerpt .= " [...]"; 
     233    } else { 
     234      $excerpt = apply_filters("localization", $result->post_excerpt); 
     235    } 
     236 
     237    $excerpt = apply_filters('the_excerpt', $excerpt); 
     238    $excerpt = apply_filters('dwls_the_excerpt', $excerpt); 
     239 
     240    return $excerpt; 
     241  } 
     242 
     243  /** 
     244   * @return string 
     245   */ 
     246  private function authorName($authorID) { 
     247    static $authorCache = array(); 
     248 
     249    if (array_key_exists($authorID, $authorCache)) { 
     250      $authorName = $authorCache[$authorID]; 
     251    } else { 
     252      $authorData = get_userdata($authorID); 
     253      $authorName = $authorData->display_name; 
     254      $authorCache[$authorID] = $authorName; 
     255    } 
     256 
     257    $authorName = apply_filters('dwls_author_name', $authorName); 
     258 
     259    return $authorName; 
     260  } 
     261 
     262  public function firstImg($post_content) { 
     263    $matches = array(); 
     264    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post_content, $matches); 
     265    $first_img = $matches[1][0]; 
     266 
     267    if (empty($first_img)) { 
     268      return ''; 
     269    } 
     270    return $first_img; 
     271  } 
     272 
     273  public function ajaxSearch() { 
     274    $maxResults = intval(get_option('daves-wordpress-live-search_max_results')); 
     275    if ($maxResults === 0) 
     276      $maxResults = -1; 
     277 
     278    $cacheLifetime = intval(get_option('daves-wordpress-live-search_cache_lifetime')); 
     279    if (!is_user_logged_in() && 0 < $cacheLifetime) { 
     280      $doCache = TRUE; 
     281    } else { 
     282      $doCache = FALSE; 
     283    } 
     284 
     285    if ($doCache) { 
     286      $cachedResults = DWLSTransients::get($_REQUEST['s']); 
     287    } 
     288 
     289    if ((!$doCache) || (FALSE === $cachedResults)) { 
     290 
     291      // Initialize the $wp global object 
     292      // See class WP in classes.php 
     293      // The Relevanssi plugin is using this instead of 
     294      // the global $wp_query object 
     295      $wp = & new WP(); 
     296      $wp->init();  // Sets up current user. 
     297      $wp->parse_request(); 
     298 
     299      $displayPostMeta = (bool) get_option('daves-wordpress-live-search_display_post_meta'); 
     300      if (array_key_exists('search_source', $_REQUEST)) { 
     301        $searchSource = $_GET['search_source']; 
     302      } else { 
     303        $searchSource = intval(get_option('daves-wordpress-live-search_source')); 
     304      } 
     305 
     306      $results = new DavesWordPressLiveSearchResults($searchSource, $searchTerms, $displayPostMeta, $maxResults); 
     307 
     308      if ($doCache) { 
     309        DWLSTransients::set($_REQUEST['s'], $results, $cacheLifetime); 
     310      } 
     311    } else { 
     312      // Found it in the cache. Return the results. 
     313      $results = $cachedResults; 
     314    } 
     315 
     316    $json = json_encode($results); 
     317 
     318    // If we don't output the text we want outputted here and 
     319    // then die(), the wp_ajax code will die(0) or die(-1) after 
     320    // this function returns and that value will get echoed out 
     321    // to the browser, resulting in a JSON parsing error. 
     322    die($json); 
     323  } 
     324 
    339325} 
    340326 
Note: See TracChangeset for help on using the changeset viewer.