oliverhentairules

Untitled

Nov 1st, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.25 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Search Light
  4. Plugin URI: http://www.itsystempartner.de/index.php/technologie/wordpress-plugins/searchlight/
  5. Description: Provides an ajax search-dropdown for WordPress.
  6. Version: 1.4.2
  7. Author: Daniel Kowalski, Sai Liu, IT Systempartner
  8. Author URI: http://www.itsystempartner.de
  9. */
  10. define("ITSAS_SETTINGS_FILE","search-light/settings.php");
  11. define("ITSAS_THEME_DIR",WP_PLUGIN_DIR."/search-light/themes");
  12. define("ITSAS_THEME_URL",WP_PLUGIN_URL."/search-light/themes");
  13. function itsas_onAction_activate(){
  14.     update_option('itsas_version', '2.8.1');
  15.     add_option('itsas_search_form_id', 'searchform');
  16.     add_option('itsas_search_field_id', 's');
  17.     add_option('itsas_top', '0');
  18.     add_option('itsas_left', '0');
  19.     add_option('itsas_page', 'Seiten');
  20.     add_option('itsas_post', 'Artikel');
  21.     add_option('itsas_result_text', 'Treffer');
  22.     add_option('itsas_no_result_text', 'Keine Treffer gefunden');
  23.     add_option('itsas_displayed_page_count', '10');
  24.     add_option('itsas_displayed_post_count', '10');
  25.     add_option('itsas_all_result_display', 'Alle Treffer anzeigen');
  26.     add_option('itsas_theme',ITSAS_THEME_URL.'/default');
  27.     add_option('itsas_display_thumbnails', '1');
  28. }
  29. register_activation_hook(__FILE__,"itsas_onAction_activate");
  30.  
  31. function itsas_onAction_admin_menu()
  32. {
  33.     $base_url = ITSAS_SETTINGS_FILE;
  34.     add_options_page("Search Light Settings", "Search Light", 7, $base_url);
  35.    
  36.     if( function_exists( 'add_meta_box' )) {
  37.         add_meta_box( 'itsas_searchlight_box', 'Search Light',
  38.                     'itsas_searchlight_box', 'post', 'side','high' );
  39.         add_meta_box( 'itsas_searchlight_box', 'Search Light',
  40.                     'itsas_searchlight_box', 'page', 'side','high' );
  41.    }
  42. }
  43. add_action('admin_menu', 'itsas_onAction_admin_menu');
  44.  
  45. function itsas_onAction_init(){
  46.     if ($_GET['itsasAjaxSearch'] == true){
  47.         if ($_GET['a'] == "getSettings"){
  48.             echo '{"pageText":"'.htmlspecialchars (addcslashes  (get_option('itsas_page'),"\\")).'","postText":"'.htmlspecialchars (addcslashes(get_option('itsas_post'),"\\")).'","searchFormID":"'.get_option('itsas_search_form_id').'","searchFieldID":"'.get_option('itsas_search_field_id').'","top":'.get_option('itsas_top').', "left":'.get_option('itsas_left').',"theme":"'.get_option('itsas_theme').'"}';
  49.             exit();
  50.         }
  51.         $key = $_GET['key'];
  52.         itsas_search($key);
  53.         exit();
  54.     }
  55.     wp_enqueue_script('jquery');
  56.     wp_enqueue_script('itsys_ajax', WP_PLUGIN_URL."/search-light/ajax.js");
  57. }
  58. add_action('init','itsas_onAction_init');
  59.  
  60. function add_my_stylesheet() {
  61.     if (get_option('itsas_theme') == FALSE){
  62.         add_option('itsas_theme',ITSAS_THEME_URL.'/default');
  63.     }
  64.     wp_register_style('searchLightStyleSheets', get_option('itsas_theme').'/searchLight.css');
  65.     wp_enqueue_style( 'searchLightStyleSheets');
  66.     /*
  67.     $searchLightStyleUrl = WP_PLUGIN_URL . '/search-light/themes/dark/searchLight.css';
  68.     $searchLightStyleFile = WP_PLUGIN_DIR . '/search-light/themes/dark/searchLight.css';
  69.     if ( file_exists($searchLightStyleFile) ) {
  70.         wp_register_style('searchLightStyleSheets', $searchLightStyleUrl);
  71.         wp_enqueue_style( 'searchLightStyleSheets');
  72.     }
  73.     */
  74. }
  75. add_action('wp_print_styles', 'add_my_stylesheet');
  76.  
  77. function itsas_searchlight_box($post){
  78.     //global $post;
  79.     echo '<input type="hidden" name="itsas_showin_searchlight" id="itsas_showin_searchlight" value="' .
  80.     wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
  81.  
  82.     // The actual fields for data entry
  83.     echo '<p>';
  84.     echo '<input type="radio" class="searchlightNoneStyle" id="chkShowInSearchlight" name="chkShowInSearchlight" value="itsas_searchlight_showin" ';
  85.     if (get_post_meta($post->ID,'ShowInSearchlight',true) == "true"){
  86.         echo ' checked="checked" ';
  87.     }
  88.     echo '/>';
  89.     echo '&nbsp;<label for="chkShowInSearchlight">Tagged for Search Light</label>';
  90.    
  91.     echo '</p>';
  92.     // The actual fields for data entry
  93.     echo '<p>';
  94.     echo '<input type="radio" class="searchlightNoneStyle" id="chkNotShowInSearchlight" name="chkShowInSearchlight" value="itsas_searchlight_notshowin" ';
  95.     if (get_post_meta($post->ID,'NotShowInSearchlight',true) == "true"){
  96.         echo ' checked="checked" ';
  97.     }
  98.     echo '/>';
  99.     echo '&nbsp;<label for="chkNotShowInSearchlight">Hide from Search Light</label>';
  100.    
  101.     echo '</p>';
  102.     echo '<p>';
  103.     echo '<a href="#" onclick="document.getElementById(\'chkShowInSearchlight\').checked=false;document.getElementById(\'chkNotShowInSearchlight\').checked=false;" title="Clear options">Clear options</a>';
  104.     echo '</p>';
  105. }
  106.  
  107. function itsas_onAction_save_post($post_id){
  108.   if ( !wp_verify_nonce( $_POST['itsas_showin_searchlight'], plugin_basename(__FILE__) )) {
  109.     return $post_id;
  110.   }
  111.  
  112.   // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
  113.   // to do anything
  114.   if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
  115.     return $post_id;
  116.   }
  117.  
  118.   // Check permissions
  119.   if ( 'post' == $_POST['post_type'] ) {
  120.     if ( !current_user_can( 'edit_post', $post_id ) )
  121.       return $post_id;
  122.   }
  123.   if ( 'page' == $_POST['post_type'] ) {
  124.     if ( !current_user_can( 'edit_post', $post_id ) )
  125.       return $post_id;
  126.   }
  127.  
  128. /* version 1.4
  129.   if ($_POST['chkShowInSearchlight'] == 'itsas_searchlight_showin'){
  130.     update_post_meta($post_id,'ShowInSearchlight','true');
  131.   }else {
  132.     delete_post_meta($post_id,'ShowInSearchlight','true');
  133.   }
  134.   */
  135.   if ($_POST['chkShowInSearchlight'] == 'itsas_searchlight_showin'){
  136.     update_post_meta($post_id,'ShowInSearchlight','true');
  137.     delete_post_meta($post_id,'NotShowInSearchlight','true');
  138.   }else if  ($_POST['chkShowInSearchlight'] == 'itsas_searchlight_notshowin') {
  139.     update_post_meta($post_id,'NotShowInSearchlight','true');
  140.     delete_post_meta($post_id,'ShowInSearchlight','true');
  141.   }else{
  142.     delete_post_meta($post_id,'ShowInSearchlight','true');
  143.     delete_post_meta($post_id,'NotShowInSearchlight','true');
  144.   }
  145.  
  146.   return $post_id;
  147.    //return $mydata;
  148. }
  149. add_action('save_post', 'itsas_onAction_save_post');
  150.  
  151. function itsas_sqlWhere($search_key){
  152.     $sql_where = "";
  153.     if ($search_key != ""){
  154.         $sql_where = "post_status = 'publish' ";
  155.         $search_keys = explode(" ",$search_key);
  156.        
  157.         $count = count($search_keys);
  158.         if ($count < 1){
  159.             return "";
  160.         }else if ($count == 1){
  161.             if ($sql_where != "")
  162.                 $sql_where .= " AND ";
  163.             $sql_where .= " (post_title LIKE '%$search_keys[0]%' ";
  164.             $sql_where .= " OR post_content LIKE '%$search_keys[0]%')";
  165.         }else if ($count > 1){
  166.             if ($sql_where != "")
  167.                 $sql_where .= " AND ";
  168.             $sql_where .= " (post_title LIKE '%$search_keys[0]%' ";
  169.             $sql_where .= " OR post_content LIKE '%$search_keys[0]%')";
  170.             for($i = 1; $i < $count; $i++){
  171.                 $sql_where .= " AND (post_title LIKE '%$search_keys[$i]%' OR post_content LIKE '%$search_keys[$i]%') ";
  172.             }
  173.         }
  174.     }
  175.     return $sql_where;
  176. }
  177. function itsas_result_count($search_key){
  178.     global $wpdb;
  179.     $count = 0;
  180.     $sql_base = "SELECT COUNT(*) FROM {$wpdb->prefix}posts ";
  181.     $sql_where = itsas_sqlWhere($search_key);
  182.    
  183.     if ($sql_where != ""){
  184.         $sql_where = " WHERE " . $sql_where;
  185.         $sql = $sql_base.$sql_where; //echo $sql;
  186.         $count = $wpdb->get_var($sql);
  187.     }else{
  188.         $count = 0;
  189.     }
  190.     return $count;
  191. }
  192. function itsas_result($search_key, $post_type, $beginIx = 0,$endIx = 10){
  193.     global $wpdb;
  194.     $sql_base = "SELECT * FROM {$wpdb->prefix}posts ";
  195.     $sql_where = itsas_sqlWhere($search_key);
  196.     if ($sql_where != ""){
  197.         $sql_where = " WHERE post_type LIKE '$post_type' AND " . $sql_where;
  198.         $sql = $sql_base.$sql_where." ORDER BY post_date desc LIMIT $beginIx,$endIx";
  199.         $results = $wpdb->get_results($sql,ARRAY_A);
  200.     }else{
  201.         $results = null;
  202.     }
  203.     return $results;
  204. }
  205.  
  206. function itsas_search($search_key){
  207.     global $wpdb;
  208.     $isShowThumbnail = false;
  209.     if (get_option('itsas_display_thumbnails') === '1' && function_exists('current_theme_supports') && current_theme_supports( 'post-thumbnails' ) ){
  210.         $isShowThumbnail = true;
  211.     }
  212.    
  213.     $pageCount = get_option('itsas_displayed_page_count');
  214.     $postCount = get_option('itsas_displayed_post_count');
  215.    
  216.     echo '<div id="searchLightData" style="display:none"><span id="searchKey">'.$search_key.'</span>';
  217.     //echo '<span id="viewTop">'.get_option('itsas_top').'</span>';
  218.     //echo '<span id="viewLeft">'.get_option('itsas_left').'</span>';
  219.     echo '</div>';
  220.     $count = itsas_result_count($search_key);
  221.    
  222.     $showInPages = itsas_get_show_in_pages();//print_r($showInPages);
  223.     $showInPosts = itsas_get_show_in_posts();
  224.     $showInPostsWithoutSticky = array();
  225.     $stickyPosts = itsas_get_sticky_posts();
  226.     $notShowInPages = itsas_get_not_show_in_pages();
  227.     $notShowInPosts = itsas_get_not_show_in_posts();
  228.     foreach($stickyPosts as $key => $value){
  229.         if ($notShowInPosts[$key]){
  230.             unset($stickyPosts[$key]);
  231.         }
  232.     }
  233.     if ($count > 0
  234.         || count($showInPages) > 0
  235.         || count($showInPosts) > 0
  236.         || count($stickyPosts) > 0
  237.         ){
  238.         echo '<div id="searchLightStatus">';
  239.         echo '<h1>'.$count.' '. get_option('itsas_result_text').'</h1>';
  240.         echo '</div>';
  241.         $results = itsas_result($search_key,'page', 0, $pageCount);
  242.         if ($results || count($showInPages) > 0){
  243.             echo '<h2>'.get_option('itsas_page').'</h2>';
  244.             echo '<ul>';
  245.             // show in search light
  246.             foreach($showInPages as $key=>$title){
  247.                 echo '<li>';               
  248.                 echo '<a href="'.get_permalink($key).'" >';
  249.                 if ($isShowThumbnail){
  250.                     echo get_the_post_thumbnail($key);
  251.                 }
  252.                 echo $title.'</a>';
  253.                 echo '</li>';
  254.             }//echo '<li>...........</li>';
  255.             // search reslut
  256.             if ($results)
  257.             foreach($results as $row){
  258.                 if ($showInPages[$row['ID']]){
  259.                     continue;
  260.                 }
  261.                 if ($notShowInPages[$row['ID']]){
  262.                     continue;
  263.                 }
  264.                 echo '<li>';               
  265.                 echo '<a href="'.get_permalink($row['ID']).'" >';
  266.                 if ($isShowThumbnail){
  267.                     echo get_the_post_thumbnail($row['ID']);
  268.                 }
  269.                 echo $row['post_title'].'</a>';
  270.                 echo '</li>';
  271.             }
  272.             echo '</ul>';
  273.         }
  274.         $results = itsas_result($search_key,'post', 0, $postCount);
  275.         itsas_get_sticky_posts();
  276.         if ($results
  277.             || count($showInPosts) > 0
  278.             || count($stickyPosts) > 0
  279.             ){
  280.             echo '<h2>'.get_option('itsas_post').'</h2>';
  281.             echo '<ul>';
  282.             // show in search light and is sticky
  283.             foreach($showInPosts as $key=>$title){
  284.                 if (is_sticky($key)){
  285.                     echo '<li>';               
  286.                     echo '<a href="'.get_permalink($key).'" >';
  287.                     if ($isShowThumbnail){
  288.                         echo get_the_post_thumbnail($key);
  289.                     }
  290.                     echo $title.'</a>';
  291.                     echo '</li>';
  292.                     //unset($showInPosts[$key]);
  293.                     unset($stickyPosts[$key]);
  294.                 }else{
  295.                     $showInPostsWithoutSticky[$key] = $title;
  296.                 }
  297.             }//echo '<li>...........</li>';
  298.             // show in search
  299.             foreach($showInPostsWithoutSticky as $key=>$title){
  300.                     echo '<li>';               
  301.                     echo '<a href="'.get_permalink($key).'" >';
  302.                     if ($isShowThumbnail){
  303.                         echo get_the_post_thumbnail($key);
  304.                     }
  305.                     echo $title.'</a>';
  306.                     echo '</li>';
  307.             }//echo '<li>...........</li>';
  308.             // sticky posts
  309.             if (count($stickyPosts) > 0){
  310.                 $sql = "SELECT * FROM {$wpdb->prefix}posts WHERE ID IN(".implode(",", $stickyPosts).") ORDER BY post_date desc";
  311.                 //echo $sql;
  312.                 $stickyResult = $wpdb->get_results($sql,ARRAY_A);
  313.                 if ($stickyResult){
  314.                     foreach($stickyResult as $row){
  315.                         if ($notShowInPosts[$row['ID']]){
  316.                             continue;
  317.                         }
  318.                         echo '<li>';               
  319.                         echo '<a href="'.get_permalink($row['ID']).'" >';
  320.                         if ($isShowThumbnail){
  321.                             echo get_the_post_thumbnail($row['ID']);
  322.                         }
  323.                         echo $row['post_title'].'</a>';
  324.                         echo '</li>';
  325.                     }
  326.                 }
  327.             }
  328.             //echo '<li>...........</li>';
  329.             // search reslut
  330.             if ($results)
  331.             foreach($results as $row){
  332.                 if ($showInPosts[$row['ID']] || is_sticky($row['ID'])){
  333.                     continue;
  334.                 }
  335.                 if ($notShowInPosts[$row['ID']]){
  336.                     continue;
  337.                 }
  338.                 echo '<li>';               
  339.                 echo '<a href="'.get_permalink($row['ID']).'" >';
  340.                 if ($isShowThumbnail){
  341.                     echo get_the_post_thumbnail($row['ID']);
  342.                 }
  343.                 echo $row['post_title'].'</a>';
  344.                 echo '</li>';
  345.             }
  346.             echo '</ul>';
  347.         }
  348.         echo '<h3><a href="#" onclick="document.getElementById(itsas_searchFormId).submit(); return false;" title="">'.get_option('itsas_all_result_display').'</a></h3>';
  349.     }else{
  350.         echo '<div id="searchLightStatus">';
  351.         echo '<h1>'.get_option('itsas_no_result_text').'</h1>';
  352.         echo '</div>';
  353.     }
  354. }
  355.  
  356. function itsas_get_show_in_pages(){
  357.     global $wpdb;
  358.     $sql = "SELECT ID,post_title FROM {$wpdb->prefix}posts p, {$wpdb->prefix}postmeta m WHERE p.ID=m.post_id AND p.post_type='page' AND m.meta_key='ShowInSearchlight' AND m.meta_value='true' AND post_status = 'publish' ORDER BY p.post_date desc ";
  359.     $results = $wpdb->get_results($sql,ARRAY_A);
  360.     $rlt = array();
  361.     if ($results){
  362.         foreach($results as $row){
  363.             $rlt[$row['ID']] = $row['post_title'];
  364.         }
  365.     }
  366.     return $rlt;
  367. }
  368. function itsas_get_show_in_posts(){
  369.     global $wpdb;
  370.     $sql = "SELECT ID,post_title FROM {$wpdb->prefix}posts p, {$wpdb->prefix}postmeta m WHERE p.ID=m.post_id AND p.post_type='post' AND m.meta_key='ShowInSearchlight' AND m.meta_value='true' AND post_status = 'publish' ORDER BY p.post_date desc ";
  371.     $results = $wpdb->get_results($sql,ARRAY_A);
  372.     $rlt = array();
  373.     if ($results){
  374.         foreach($results as $row){
  375.             $rlt[$row['ID']] = $row['post_title'];
  376.         }
  377.     }
  378.     return $rlt;
  379. }
  380. function itsas_get_not_show_in_pages(){
  381.     global $wpdb;
  382.     $sql = "SELECT ID,post_title FROM {$wpdb->prefix}posts p, {$wpdb->prefix}postmeta m WHERE p.ID=m.post_id AND p.post_type='page' AND m.meta_key='NotShowInSearchlight' AND m.meta_value='true' AND post_status = 'publish' ORDER BY p.post_date desc ";
  383.     $results = $wpdb->get_results($sql,ARRAY_A);
  384.     $rlt = array();
  385.     if ($results){
  386.         foreach($results as $row){
  387.             $rlt[$row['ID']] = $row['post_title'];
  388.         }
  389.     }
  390.     return $rlt;
  391. }
  392. function itsas_get_not_show_in_posts(){
  393.     global $wpdb;
  394.     $sql = "SELECT ID,post_title FROM {$wpdb->prefix}posts p, {$wpdb->prefix}postmeta m WHERE p.ID=m.post_id AND p.post_type='post' AND m.meta_key='NotShowInSearchlight' AND m.meta_value='true' AND post_status = 'publish' ORDER BY p.post_date desc ";
  395.     $results = $wpdb->get_results($sql,ARRAY_A);
  396.     $rlt = array();
  397.     if ($results){
  398.         foreach($results as $row){
  399.             $rlt[$row['ID']] = $row['post_title'];
  400.         }
  401.     }
  402.     return $rlt;
  403. }
  404. function itsas_get_sticky_posts(){
  405.     global $wpdb;
  406.     $sticky = get_option('sticky_posts');
  407.     $rlt = array();
  408.     foreach($sticky as $value){
  409.         $rlt[$value] = $value;
  410.     }
  411.     return $rlt;
  412. }
  413. ?>
Add Comment
Please, Sign In to add comment