actions(); $this->filters(); $this->shortcodes(); } function actions(){ //ACTIONS if(class_exists('AJAXY_SF_WIDGET')){ add_action( 'widgets_init', create_function( '', 'return register_widget( "AJAXY_SF_WIDGET" );' ) ); } add_action( 'wp_enqueue_scripts', array(&$this, "enqueue_scripts")); add_action( 'admin_enqueue_scripts', array(&$this, "enqueue_scripts")); add_action( "admin_menu",array(&$this, "menu_pages")); add_action( 'wp_head', array(&$this, 'head')); add_action( 'admin_head', array(&$this, 'head')); add_action( 'wp_footer', array(&$this, 'footer')); add_action( 'admin_footer', array(&$this, 'footer')); add_action( 'wp_ajax_ajaxy_sf', array(&$this, 'get_search_results')); add_action( 'wp_ajax_nopriv_ajaxy_sf', array(&$this, 'get_search_results')); add_action( 'wp_ajax_ajaxy_sf_shortcode', array(&$this, 'get_shortcode')); add_action( 'admin_notices', array(&$this, 'admin_notice') ); add_action( 'plugins_loaded', array(&$this, 'load_textdomain') ); } function filters(){ //FILTERS if($this->get_style_setting( 'hook_search_form', 1 ) > 0) { add_filter( 'get_search_form', array(&$this, 'form_shortcode'), 1); } add_filter( 'ajaxy-overview', array(&$this, 'admin_page'), 10 ); } function shortcodes() { add_shortcode( 'ajaxy-live-search', array(&$this, 'form_shortcode') ); add_shortcode( 'ajaxy-selective-search', array(&$this, 'selective_input_shortcode') ); } function overview(){ echo apply_filters('ajaxy-overview', 'main'); } function load_textdomain() { load_plugin_textdomain( 'ajaxy-sf', false, dirname( plugin_basename( __FILE__ ) ) . '/langs/' ); } function menu_page_exists( $menu_slug ) { global $menu; foreach ( $menu as $i => $item ) { if ( $menu_slug == $item[2] ) { return true; } } return false; } function menu_pages(){ if(!$this->menu_page_exists('ajaxy-page')){ add_menu_page( _n( 'Ajaxy', 'Ajaxy', 1, 'ajaxy' ), _n( 'Ajaxy', 'Ajaxy', 1 ), 'Ajaxy', 'ajaxy-page', array(&$this, 'overview'), AJAXY_SF_PLUGIN_URL.'/images/ico.png'); } add_submenu_page( 'ajaxy-page', __('Live Search'), __('Live Search'), 'manage_options', 'ajaxy_sf_admin', array(&$this, 'admin_page')); } function admin_page(){ $message = false; require_once('admin/classes/class-wp-ajaxy-sf-list-table.php'); require_once('admin/classes/class-wp-ajaxy-sf-themes-list-table.php'); if(isset($_GET['edit'])){ if($_GET['type'] == 'taxonomy'){ include_once('admin/admin-edit-taxonomy-form.php'); return true; }elseif($_GET['type'] == 'role'){ include_once('admin/admin-edit-role-form.php'); return true; }else{ include_once('admin/admin-edit-post-form.php'); return true; } } $tab = (!empty($_GET['tab']) ? trim($_GET['tab']) : false); $type = (!empty($_GET['type']) ? trim($_GET['type']) : false); //form data switch($tab) { case 'woocommerce': case 'taxonomy': case 'author': case 'post_type': case 'templates': $public = ($tab == 'author' ? false : true); if(isset($_POST['action'])){ $action = trim($_POST['action']); $ids = (isset($_POST['template_id']) ? (array)$_POST['template_id'] : false); if($action == 'hide' && $ids){ global $AjaxyLiveSearch; $k = 0; foreach($ids as $id){ $setting = (array)$AjaxyLiveSearch->get_setting($id, $public); $setting['show'] = 0; $AjaxyLiveSearch->set_setting($id, $setting); $k ++; } $message = sprintf(_('%s templates hidden'), $k); } elseif($action == 'show' && $ids){ global $AjaxyLiveSearch; $k = 0; foreach($ids as $id){ $setting = (array)$AjaxyLiveSearch->get_setting($id, $public); $setting['show'] = 1; $AjaxyLiveSearch->set_setting($id, $setting); $k ++; } $message = sprintf(_('%s templates shown'), $k); } } elseif(isset($_GET['show']) && isset($_GET['name'])){ global $AjaxyLiveSearch; if($tab == 'author'){ $setting = (array)$AjaxyLiveSearch->get_setting('role_'.$_GET['name'], $public); $setting['show'] = (int)$_GET['show']; $AjaxyLiveSearch->set_setting('role_'.$_GET['name'], $setting); }else{ $setting = (array)$AjaxyLiveSearch->get_setting($_GET['name'], $public); $setting['show'] = (int)$_GET['show']; $AjaxyLiveSearch->set_setting($_GET['name'], $setting); } $message = _('Template modified'); } break; case 'themes': if(isset($_GET['theme']) && isset($_GET['apply'])){ global $AjaxyLiveSearch; $AjaxyLiveSearch->set_style_setting('theme', $_GET['theme']); $message = $_GET['theme'].' theme applied'; } break; default: } ?>
]+>/i', $content, $matches); $imageCount = count ($matches); if ($imageCount >= 1) { if (isset ($matches[0][0])) { preg_match_all('/src=("[^"]*")/i', $matches[0][0], $src); if (isset ($src[1][0])) { $theImageSrc = str_replace('"', '', $src[1][0]); } } } if($this->get_style_setting('aspect_ratio', 0 ) > 0){ set_time_limit(0); try{ set_time_limit(1); list($width, $height, $type, $attr) = @getimagesize( $theImageSrc ); if($width > 0 && $height > 0){ if($width < $width_max && $height < $height_max){ return array('src' => $theImageSrc, 'width' => $width, 'height' => $height); } elseif($width > $width_max && $height > $height_max){ $percent_width = $width_max * 100/$width; $percent_height = $height_max * 100/$height; $percent = ($percent_height < $percent_width ? $percent_height : $percent_width); return array('src' => $theImageSrc, 'width' => intval($width * $percent / 100), 'height' => intval($height * $percent / 100)); } elseif($width < $width_max && $height > $height_max){ $percent = $height * 100/$height_max; return array('src' => $theImageSrc, 'width' => intval($width * $percent / 100), 'height' => intval($height * $percent / 100)); } else{ $percent = $width * 100/$width_max; return array('src' => $theImageSrc, 'width' => intval($width * $percent / 100), 'height' => intval($height * $percent / 100)); } } } catch(Exception $e){ set_time_limit(60); return array('src' => $theImageSrc, 'width' => $this->get_style_setting('thumb_width', 50 ) , 'height' => $this->get_style_setting('thumb_height', 50 ) ); } } else{ return array('src' => $theImageSrc, 'width' => $this->get_style_setting('thumb_width', 50 ) , 'height' => $this->get_style_setting('thumb_height', 50 ) ); } return false; } function get_post_types() { $post_types = get_post_types(array('_builtin' => false),'objects'); $post_types['post'] = get_post_type_object('post'); $post_types['page'] = get_post_type_object('page'); unset($post_types['wpsc-product-file']); return $post_types; } function get_excerpt_count() { return $this->get_style_setting('excerpt', 10); } function get_taxonomies() { $args = array( 'public' => true, '_builtin' => false ); $output = 'objects'; // or objects $operator = 'or'; // 'and' or 'or' $taxonomies = get_taxonomies( $args, $output, $operator ); if ( $taxonomies ) { return $taxonomies; } return null; } function get_search_objects($all = false, $objects = false, $specific_post_types = array(), $specific_taxonomies = array(), $specific_roles = array()) { $search = array(); $scat = (array)$this->get_setting('category'); $arg_category_show = isset($_POST['show_category']) ? $_POST['show_category'] : 1; $search_taxonomies = false; if($scat['show'] == 1 && $arg_category_show == 1){ $search_taxonomies = true; } $arg_post_category_show = isset($_POST['show_post_category']) ? $_POST['show_post_category'] : 1; $show_post_category = false; if($scat['ushow'] == 1 && $arg_post_category_show == 1){ $show_post_category = true; } $arg_authors_show = isset($_POST['show_authors']) ? $_POST['show_authors'] : 1; $show_authors = false; if($show_authors == 1){ $show_authors = true; } if(!$objects || $objects == 'post_type') { // get all post types that are ready for search $post_types = $this->get_post_types(); foreach($post_types as $post_type) { if(sizeof($specific_post_types) == 0) { $setting = $this->get_setting($post_type->name); if($setting -> show == 1 || $all){ $search[] = array( 'order' => $setting->order, 'name' => $post_type->name, 'label' => (empty($setting->title) ? $post_type->label : $setting->title), 'type' => 'post_type' ); } } elseif(in_array($post_type->name, $specific_post_types)) { $setting = $this->get_setting($post_type->name); $search[] = array( 'order' => $setting->order, 'name' => $post_type->name, 'label' => (empty($setting->title) ? $post_type->label : $setting->title), 'type' => 'post_type' ); } } } if((!$objects || $objects == 'taxonomy') && $search_taxonomies) { // override post_types from input $taxonomies = $this->get_taxonomies(); foreach($taxonomies as $taxonomy) { if(sizeof($specific_taxonomies) == 0) { $setting = $this->get_setting($taxonomy->name); if($setting -> show == 1 || $all){ $search[] = array( 'order' => $setting->order, 'name' => $taxonomy->name, 'label' => (empty($setting->title) ? $taxonomy->label : $setting->title), 'type' => 'taxonomy', 'show_posts' => $show_post_category ); } } elseif(in_array($taxonomy->name, $specific_taxonomies)) { $setting = $this->get_setting($taxonomy->name); $search[] = array( 'order' => $setting->order, 'name' => $taxonomy->name, 'label' => (empty($setting->title) ? $taxonomy->label : $setting->title), 'type' => 'taxonomy', 'show_posts' => $show_post_category ); } } }elseif((!$objects || $objects == 'taxonomy')) { // override post_types from input $taxonomies = $this->get_taxonomies(); foreach($taxonomies as $taxonomy) { if(sizeof($specific_taxonomies) == 0) { $setting = $this->get_setting($taxonomy->name); if($setting -> show == 1 || $all){ $search[] = array( 'order' => $setting->order, 'name' => $taxonomy->name, 'label' => (empty($setting->title) ? $taxonomy->label : $setting->title), 'type' => 'taxonomy', 'show_posts' => $show_post_category ); } } elseif(in_array($taxonomy->name, $specific_taxonomies)) { $setting = $this->get_setting($taxonomy->name); $search[] = array( 'order' => $setting->order, 'name' => $taxonomy->name, 'label' => (empty($setting->title) ? $taxonomy->label : $setting->title), 'type' => 'taxonomy', 'show_posts' => $show_post_category ); } } } if(!$objects || $objects == 'author') { global $wp_roles; $roles = $wp_roles->get_names(); foreach($roles as $role => $label) { if(sizeof($specific_roles) == 0) { $setting = $this->get_setting('role_'.$role, false); if($setting -> show == 1 || $all){ $search[] = array( 'order' => $setting->order, 'name' => $role, 'label' => (empty($setting->title) ? $label : $setting->title), 'type' => 'role' ); } } elseif(in_array($role, $specific_roles)) { $setting = $this->get_setting('role_'.$role, false); $search[] = array( 'order' => $setting->order, 'name' => $role, 'label' => (empty($setting->title) ? $label : $setting->title), 'type' => 'role' ); } } } uasort($search, array(&$this, 'sort_search_objects')); return $search; } function sort_search_objects($a, $b) { if ($a['order'] == $b['order']) { return 0; } return ($a['order'] < $b['order']) ? -1 : 1; } function set_templates($template, $html) { if(get_option('sf_template_'.$template) !== false) { update_option('sf_template_'.$template, stripslashes($html)); } else { add_option('sf_template_'.$template, stripslashes($html)); } } function set_setting($name, $value) { if(get_option('sf_setting_'.$name) !== false) { update_option('sf_setting_'.$name, json_encode($value)); } else { add_option('sf_setting_'.$name, json_encode($value)); } } function remove_setting($name){ delete_option('sf_setting_'.$name); } function get_setting($name, $public = true) { $defaults = array( 'title' => '', 'show' => 1, 'ushow' => 0, 'search_content' => 0, 'limit' => 5, 'order' => 0, 'order_results' => false ); if(!$public) { $defaults['show'] = 0; } if(get_option('sf_setting_'.$name) !== false) { $settings = json_decode(get_option('sf_setting_'.$name)); foreach($defaults as $key => $value) { if(!isset($settings->{$key})){ $settings->{$key} = $value; } } return $settings; } else { return (object)$defaults; } } function set_style_setting($name, $value) { update_option('sf_style_'.$name, $value); } function get_style_setting($name, $default = '') { if(get_option('sf_style_'.$name) !== false) { return get_option('sf_style_'.$name, $default); } else { return $default; } } function remove_style_setting($name) { return delete_option('sf_style_'.$name); } function remove_template($template) { delete_option('sf_template_'.$template); } function get_templates($template, $type='') { $template_post = ""; switch($type) { case 'more': $template_post = get_option('sf_template_more'); if(!$template_post) { $template_post = 'See more results for "{search_value}"Displaying top {total} results'; } break; case 'taxonomy': $template_post = get_option('sf_template_'.$template); if(!$template_post) { $template_post = '{name}'; } break; case 'author': case 'role': $template_post = get_option('sf_template_'.$template); if(!$template_post) { $template_post = '{user_nicename}'; } break; case 'post_type': $template_post = get_option('sf_template_'.$template); if(!$template_post && in_array($template, self::$woocommerce_post_types)) { $template_post = '{post_image_html}{post_title} - {price}Posted by {post_author} on {post_date_formatted}'; }elseif(!$template_post){ $template_post = '{post_image_html}{post_title} Posted by {post_author} on {post_date_formatted}'; } break; default: $template_post = get_option('sf_template_'.$template); if(!$template_post) { $template_post = '{post_image_html}{post_title} Posted by {post_author} on {post_date_formatted}'; } break; } return $template_post; } function category($name, $taxonomy = 'category', $show_category_posts = false, $limit = 5) { global $wpdb; $categories = array(); $setting = (object)$this->get_setting($taxonomy); $excludes = ""; $excludes_array = array(); if(isset($setting->excludes) && sizeof($setting->excludes) > 0 && is_array($setting->excludes)){ $excludes = " AND $wpdb->terms.term_id NOT IN (".implode(',', $setting->excludes).")"; $excludes_array = $setting->excludes; } $results = null; $query = " SELECT distinct($wpdb->terms.name) , $wpdb->terms.term_id , $wpdb->term_taxonomy.taxonomy FROM $wpdb->terms , $wpdb->term_taxonomy WHERE name LIKE '%%%s%%' AND $wpdb->term_taxonomy.taxonomy = '$taxonomy' AND $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id $excludes LIMIT 0, %d"; $query = apply_filters("sf_category_query", $wpdb->prepare($query, $name, $setting->limit), $name, $excludes_array, $setting->limit); $results = $wpdb->get_results($query); if(sizeof($results) > 0 && is_array($results) && !is_wp_error($results)) { $unset_array = array('term_group', 'term_taxonomy_id', 'taxonomy', 'parent', 'count', 'cat_ID', 'cat_name', 'category_parent'); foreach($results as $result) { $cat = get_term($result->term_id, $result->taxonomy); if($cat != null && !is_wp_error($cat)) { $cat_object = new stdclass(); $category_link = get_term_link($cat); $cat_object->category_link = $category_link; $matches = array(); $template = $this->get_templates( $taxonomy, 'taxonomy' ); preg_match_all ("/\{.*?\}/", $template, $matches); foreach($matches[0] as $match){ $match = str_replace(array('{', '}'), '', $match); if(isset($cat->{$match})) { $cat_object->{$match} = $cat->{$match}; } } if($show_category_posts) { $limit = isset($setting->limit_posts) ? $setting->limit_posts : 5; $psts = $this->posts_by_term($cat->term_id, $taxonomy, $limit); if(sizeof($psts) > 0) { $categories[$cat->term_id] = array('name' => $cat->name,'posts' => $this->posts_by_term($cat->term_id, $limit)); } } else { $categories[] = $cat_object; } } } } return $categories; } function author($name, $show_author_posts = false, $limit = 5) { global $wpdb; $authors = array(); $results = null; $query = " SELECT * FROM $wpdb->users WHERE ID IN ( SELECT user_id FROM $wpdb->usermeta WHERE (meta_key = 'first_name' AND meta_value LIKE '%%%s%%') OR (meta_key = 'last_name' AND meta_value LIKE '%%%s%%' ) OR (meta_key = 'nickname' AND meta_value LIKE '%%%s%%' ) ) "; $query = apply_filters("sf_authors_query", $wpdb->prepare($query, $name, $name, $name), $name); $results = $wpdb->get_results($query); if(sizeof($results) > 0 && is_array($results) && !is_wp_error($results)) { foreach($results as $result) { $authors[] = new WP_User($result->ID); } } return $authors; } function filter_authors_by_role($authors, $role) { $users = array(); $setting = (object)$this->get_setting('role_'.$role, false); $excludes = ""; $excludes_array = array(); if(isset($setting->excludes) && sizeof($setting->excludes) > 0 && is_array($setting->excludes)){ $excludes_array = $setting->excludes; } $template = $this->get_templates( 'role_'.$role, 'author' ); $matches = array(); preg_match_all ("/\{.*?\}/", $template, $matches); if(sizeof($matches) > 0) { foreach($authors as $author) { if(in_array($role, $author->roles) && !in_array($author->ID,$excludes_array)) { $user = new stdClass(); foreach($matches[0] as $match) { $match = str_replace(array('{', '}'), '', $match); $method = "get_".$match; if(method_exists ($author->data, $method)){ $user->{$match} = call_user_func(array($author->data,$method)); }elseif(method_exists ($author, $match)){ $user->{$match} = call_user_func(array($author->data,$match)); }elseif(property_exists ($author->data, $match)){ $user->{$match} = $author->data->{$match}; } } if(in_array('{author_link}', $matches[0])){ $user->author_link = get_author_posts_url($author->ID); } $users[] = $user; } } } return $users; } function posts($name, $post_type='post', $term_id = false) { global $wpdb; $posts = array(); $setting = (object)$this->get_setting($post_type); $excludes = ""; $excludes_array = array(); if(isset($setting->excludes) && sizeof($setting->excludes) > 0 && is_array($setting->excludes)){ $excludes = " AND ID NOT IN (".implode(',', $setting->excludes).")"; $excludes_array = $setting->excludes; } $order_results = ($setting->order_results ? " ORDER BY ".$setting->order_results : ""); $results = array(); $query = " SELECT $wpdb->posts.ID FROM $wpdb->posts WHERE (post_title LIKE '%%%s%%' ".($setting->search_content == 1 ? "or post_content LIKE '%%%s%%')":")")." AND post_status='publish' AND post_type='".$post_type."' $excludes $order_results LIMIT 0, %d"; /* $meta_query = " SELECT project_id.post_id , height.height , width.width FROM $wpdb->postmeta WHERE meta_key = '$meta_key' AND meta_value LIKE '$meta_value' "; */ $query = apply_filters("sf_posts_query", ($setting->search_content == 1 ? $wpdb->prepare($query, $name, $name, $setting->limit) :$wpdb->prepare($query, $name, $setting->limit)), $name, $post_type, $excludes_array, $setting->search_content, $order_results, $setting->limit); $results = $wpdb->get_results( $query ); if(sizeof($results) > 0 && is_array($results) && !is_wp_error($results)) { $template = $this->get_templates( $post_type, 'post_type' ); $matches = array(); preg_match_all ("/\{.*?\}/", $template, $matches); if(sizeof($matches) > 0) { foreach($results as $result) { $pst = $this->post_object($result->ID, $term_id, $matches[0]); if($pst){ $posts[] = $pst; } } } } return $posts; } function posts_by_term($term_id, $taxonomy, $limit = 5){ $posts = array(); $args = array( 'showposts' => $limit, 'tax_query' => array( array( 'taxonomy' => $taxonomy, 'terms' => $term_id, 'field' => 'term_id', ) ), 'orderby' => 'date', 'order' => 'DESC' ); $term_query = new WP_Query( $args ); if($term_query->have_posts()) : $psts = apply_filters('sf_pre_term_posts', $term_query->posts); if(sizeof($psts) > 0) { foreach($psts as $p) { $matches = array(); $template = $this->get_templates( $p->post_type, 'post_type' ); preg_match_all ("/\{.*?\}/", $template, $matches); $posts[] = $this->post_object($p->ID, false, $matches[0]); } } $posts = apply_filters('sf_term_posts', $posts); endif; return $posts; } function post_object($id, $term_id = false, $matches = false) { $unset_array = array('post_date_gmt', 'post_status', 'comment_status', 'ping_status', 'post_password', 'post_content_filtered', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'guid', 'menu_order', 'post_mime_type', 'comment_count', 'ancestors', 'filter'); global $post; $date_format = get_option( 'date_format' ); $post = get_post($id); if($term_id) { if(!in_category($term_id, $post->ID)){ return false; } } $size = array('height' => $this->get_style_setting('thumb_height' , 50), 'width' => $this->get_style_setting('thumb_width' , 50)); if($post != null) { $post_object = new stdclass(); $post_link = get_permalink($post->ID); if(in_array('{post_image}', $matches) || in_array('{post_image_html}', $matches)) { $post_thumbnail_id = get_post_thumbnail_id( $post->ID); if( $post_thumbnail_id > 0) { $thumb = wp_get_attachment_image_src( $post_thumbnail_id, array($size['height'], $size['width']) ); $post_object->post_image = (trim($thumb[0]) == "" ? AJAXY_SF_NO_IMAGE : $thumb[0]); if(in_array('{post_image_html}', $matches)) { $post_object->post_image_html = 'Ajaxy: Track your live search and improve your website search with live search keyword tracker - Download | No Thanks - Dismiss