/** * Installation procedure. Save default settings and insert database tables. * * @since 1.0 */ private function install() { /** * Save our default settings so we have a working search engine on activation * that matches what WordPress does out of the box; include post types that are * not specifically set to exclude_from_search */ $settings = array('engines' => array('default' => array())); $post_types = array_merge(array('post' => 'post', 'page' => 'page'), get_post_types(array('exclude_from_search' => false, '_builtin' => false))); foreach ($post_types as $post_type) { $settings['engines']['default'][$post_type] = array('enabled' => true, 'weights' => array()); $postTypeObject = get_post_type_object($post_type); // set default title weight if applicable if (post_type_supports($postTypeObject->name, 'title')) { $settings['engines']['default'][$post_type]['weights']['title'] = searchwp_get_engine_weight(null, 'title'); } // set default content weight if applicable if (post_type_supports($postTypeObject->name, 'editor')) { $settings['engines']['default'][$post_type]['weights']['content'] = searchwp_get_engine_weight(null, 'content'); } // set default slug weight if applicable if ('page' == $postTypeObject->name || $postTypeObject->publicly_queryable) { $settings['engines']['default'][$post_type]['weights']['slug'] = searchwp_get_engine_weight(null, 'slug'); } // set default taxonomy weight(s) if applicable $taxonomies = get_object_taxonomies($postTypeObject->name); if (is_array($taxonomies) && count($taxonomies)) { $settings['engines']['default'][$post_type]['weights']['tax'] = array(); foreach ($taxonomies as $taxonomy) { if ('post_format' != $taxonomy) { // we don't want Post Formats here $settings['engines']['default'][$post_type]['weights']['tax'][$taxonomy] = searchwp_get_engine_weight(null, 'tax'); } } } // set default excerpt weight if applicable if (post_type_supports($postTypeObject->name, 'excerpt')) { $settings['engines']['default'][$post_type]['weights']['excerpt'] = searchwp_get_engine_weight(null, 'excerpt'); } // set default comment weight if applicable if (post_type_supports($postTypeObject->name, 'comments')) { $settings['engines']['default'][$post_type]['weights']['comment'] = searchwp_get_engine_weight(null, 'comment'); } // set our default options $settings['engines']['default'][$post_type]['options'] = array('exclude' => '', 'attribute_to' => '', 'stem' => ''); } // allow developers to filter the default engine settings $settings['engines'] = apply_filters('searchwp_initial_engine_settings', $settings['engines']); searchwp_generate_settings($settings['engines']); $this->create_tables(); }
/** * @deprecated as of 2.5.7 * * @param array $weights * @param string $type * @param null $subtype * * @return int */ function searchwpGetEngineWeight($weights = array(), $type = 'title', $subtype = null) { return searchwp_get_engine_weight($weights, $type, $subtype); }
/** * Generate the SQL used to open the per-post type sub-query * * @param $args array Arguments for the post type * @since 1.8 */ private function query_post_type_open($args) { global $wpdb; $defaults = array('post_type' => 'post', 'post_column' => 'ID', 'title_weight' => function_exists('searchwp_get_engine_weight') ? searchwp_get_engine_weight('title') : 20, 'slug_weight' => function_exists('searchwp_get_engine_weight') ? searchwp_get_engine_weight('slug') : 10, 'content_weight' => function_exists('searchwp_get_engine_weight') ? searchwp_get_engine_weight('content') : 2, 'comment_weight' => function_exists('searchwp_get_engine_weight') ? searchwp_get_engine_weight('comment') : 1, 'excerpt_weight' => function_exists('searchwp_get_engine_weight') ? searchwp_get_engine_weight('excerpt') : 6, 'custom_fields' => 0, 'taxonomies' => 0, 'attributed_to' => false); // process our arguments $args = wp_parse_args($args, $defaults); if (!post_type_exists($args['post_type'])) { wp_die('Invalid request', 'searchwp'); } $post_type = $args['post_type']; $post_column = $args['post_column']; if (!in_array($post_column, array('post_parent', 'ID'))) { $post_column = 'ID'; } $title_weight = absint($args['title_weight']); $slug_weight = absint($args['slug_weight']); $content_weight = absint($args['content_weight']); $comment_weight = absint($args['comment_weight']); $excerpt_weight = absint($args['excerpt_weight']); $this->sql .= "\n LEFT JOIN (\n SELECT {$wpdb->prefix}posts.{$post_column} AS post_id,\n ( SUM( {$this->db_prefix}index.title ) * {$title_weight} ) +\n ( SUM( {$this->db_prefix}index.slug ) * {$slug_weight} ) +\n ( SUM( {$this->db_prefix}index.content ) * {$content_weight} ) +\n ( SUM( {$this->db_prefix}index.comment ) * {$comment_weight} ) +\n ( SUM( {$this->db_prefix}index.excerpt ) * {$excerpt_weight} ) +\n {$args['custom_fields']} + {$args['taxonomies']}"; // allow developers to inject their own weight modifications $this->sql .= apply_filters('searchwp_weight_mods', ''); // the identifier is different if we're attributing $this->sql .= !empty($args['attributed_to']) ? " AS `{$post_type}attr` " : " AS `{$post_type}weight` "; $this->sql .= "\n FROM {$this->db_prefix}terms\n LEFT JOIN {$this->db_prefix}index ON {$this->db_prefix}terms.id = {$this->db_prefix}index.term\n LEFT JOIN {$wpdb->prefix}posts ON {$this->db_prefix}index.post_id = {$wpdb->prefix}posts.ID\n {$this->sql_join}\n "; }